blob: 50314fe52cf4c52bbfba6a4e0547d130b7624f37 [file] [log] [blame]
robbiew25ae5452004-03-02 22:55:42 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew25ae5452004-03-02 22:55:42 +000018 */
19
20/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000021 *
robbiew25ae5452004-03-02 22:55:42 +000022 * TEST IDENTIFIER : fcntl23
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
robbiew25ae5452004-03-02 22:55:42 +000024 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
robbiew25ae5452004-03-02 22:55:42 +000026 * TEST TITLE : Basic test for fcntl(2) using F_SETLEASE & F_RDLCK argument.
subrata_modak4bb656a2009-02-26 12:02:09 +000027 *
robbiew25ae5452004-03-02 22:55:42 +000028 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000029 *
robbiew25ae5452004-03-02 22:55:42 +000030 * WALL CLOCK TIME : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000031 *
robbiew25ae5452004-03-02 22:55:42 +000032 * CPU TYPES : ALL
subrata_modak4bb656a2009-02-26 12:02:09 +000033 *
robbiew25ae5452004-03-02 22:55:42 +000034 * AUTHOR : Robbie Williamson
subrata_modak4bb656a2009-02-26 12:02:09 +000035 *
robbiew25ae5452004-03-02 22:55:42 +000036 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
subrata_modak56207ce2009-03-23 13:35:39 +000038 * 1.) fcntl(2) returns...(See Description)
subrata_modakbdbaec52009-02-26 12:14:51 +000039 *
robbiew25ae5452004-03-02 22:55:42 +000040 * INPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000041 * The standard options for system call tests are accepted.
robbiew25ae5452004-03-02 22:55:42 +000042 * (See the parse_opts(3) man page).
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
robbiew25ae5452004-03-02 22:55:42 +000044 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000045 *
robbiew25ae5452004-03-02 22:55:42 +000046 * DURATION
subrata_modak56207ce2009-03-23 13:35:39 +000047 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000048 *
robbiew25ae5452004-03-02 22:55:42 +000049 * SIGNALS
subrata_modak56207ce2009-03-23 13:35:39 +000050 * Uses SIGUSR1 to pause before test if option set.
51 * (See the parse_opts(3) man page).
robbiew25ae5452004-03-02 22:55:42 +000052 *
53 * RESOURCES
subrata_modak56207ce2009-03-23 13:35:39 +000054 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000055 *
robbiew25ae5452004-03-02 22:55:42 +000056 * ENVIRONMENTAL NEEDS
57 * No run-time environmental needs.
subrata_modak4bb656a2009-02-26 12:02:09 +000058 *
robbiew25ae5452004-03-02 22:55:42 +000059 * SPECIAL PROCEDURAL REQUIREMENTS
subrata_modak56207ce2009-03-23 13:35:39 +000060 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000061 *
robbiew25ae5452004-03-02 22:55:42 +000062 * INTERCASE DEPENDENCIES
subrata_modak56207ce2009-03-23 13:35:39 +000063 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000064 *
robbiew25ae5452004-03-02 22:55:42 +000065 * DETAILED DESCRIPTION
66 * This is a Phase I test for the fcntl(2) system call. It is intended
67 * to provide a limited exposure of the system call, for now. It
68 * should/will be extended when full functional tests are written for
69 * fcntl(2).
subrata_modak4bb656a2009-02-26 12:02:09 +000070 *
subrata_modak56207ce2009-03-23 13:35:39 +000071 * Setup:
72 * Setup signal handling.
robbiew25ae5452004-03-02 22:55:42 +000073 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000074 *
subrata_modak56207ce2009-03-23 13:35:39 +000075 * Test:
robbiew25ae5452004-03-02 22:55:42 +000076 * Loop if the proper options are given.
subrata_modak56207ce2009-03-23 13:35:39 +000077 * Execute system call
robbiew25ae5452004-03-02 22:55:42 +000078 * Check return code, if system call failed (return=-1)
79 * Log the errno and Issue a FAIL message.
80 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +000081 *
subrata_modak56207ce2009-03-23 13:35:39 +000082 * Cleanup:
83 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000084 *
85 *
robbiew25ae5452004-03-02 22:55:42 +000086 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
87
88#include <sys/types.h>
89#include <sys/stat.h>
90#include <fcntl.h>
91#include <unistd.h>
92#include <errno.h>
93#include <string.h>
94#include <signal.h>
95#include "test.h"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010096#include "tst_fs_type.h"
robbiew25ae5452004-03-02 22:55:42 +000097
vapieraa354722006-05-26 06:26:37 +000098void setup();
99void cleanup();
robbiew25ae5452004-03-02 22:55:42 +0000100
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200101char *TCID = "fcntl23";
102int TST_TOTAL = 1;
robbiew25ae5452004-03-02 22:55:42 +0000103
robbiew25ae5452004-03-02 22:55:42 +0000104char fname[255];
105int fd;
106
subrata_modak56207ce2009-03-23 13:35:39 +0000107int main(int ac, char **av)
robbiew25ae5452004-03-02 22:55:42 +0000108{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200109 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200110 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000111
robbiew25ae5452004-03-02 22:55:42 +0000112 /***************************************************************
113 * parse standard options
114 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800115 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800116 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew25ae5452004-03-02 22:55:42 +0000117
118 /***************************************************************
119 * perform global setup for test
120 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000121 setup();
robbiew25ae5452004-03-02 22:55:42 +0000122
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100123 if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC) {
subrata_modak56207ce2009-03-23 13:35:39 +0000124 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100125 "Cannot do fcntl on a file on NFS filesystem");
subrata_modak56207ce2009-03-23 13:35:39 +0000126 }
subrata_modak5387ba02008-05-05 08:49:34 +0000127
robbiew25ae5452004-03-02 22:55:42 +0000128 /***************************************************************
129 * check looping state if -c option given
130 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000131 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew25ae5452004-03-02 22:55:42 +0000132
Caspar Zhangd59a6592013-03-07 14:59:12 +0800133 tst_count = 0;
robbiew25ae5452004-03-02 22:55:42 +0000134
robbiewe93a3242005-08-31 20:27:12 +0000135#ifdef F_SETLEASE
subrata_modak56207ce2009-03-23 13:35:39 +0000136 /*
137 * Call fcntl(2) with F_SETLEASE & F_RDLCK argument on fname
138 */
139 TEST(fcntl(fd, F_SETLEASE, F_RDLCK));
subrata_modakbdbaec52009-02-26 12:14:51 +0000140
subrata_modak56207ce2009-03-23 13:35:39 +0000141 /* check return code */
142 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000143 tst_resm(TFAIL,
144 "fcntl(%s, F_SETLEASE, F_RDLCK) Failed, errno=%d : %s",
145 fname, TEST_ERRNO, strerror(TEST_ERRNO));
146 } else {
147
Cyril Hrubise38b9612014-06-02 17:20:57 +0200148 TEST(fcntl(fd, F_GETLEASE));
149 if (TEST_RETURN != F_RDLCK)
150 tst_resm(TFAIL,
151 "fcntl(%s, F_GETLEASE) did not return F_RDLCK, returned %ld",
152 fname, TEST_RETURN);
153 else {
154 TEST(fcntl(fd, F_SETLEASE, F_UNLCK));
155 if (TEST_RETURN != 0)
subrata_modak56207ce2009-03-23 13:35:39 +0000156 tst_resm(TFAIL,
Cyril Hrubise38b9612014-06-02 17:20:57 +0200157 "fcntl(%s, F_SETLEASE, F_UNLCK) did not return 0, returned %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000158 fname, TEST_RETURN);
Cyril Hrubise38b9612014-06-02 17:20:57 +0200159 else
160 tst_resm(TPASS,
161 "fcntl(%s, F_SETLEASE, F_RDLCK)",
162 fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000163 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200164
subrata_modak56207ce2009-03-23 13:35:39 +0000165 if (close(TEST_RETURN) == -1) {
166 tst_resm(TWARN,
167 "close(%s) Failed, errno=%d : %s",
168 fname, errno, strerror(errno));
169 }
170 }
vapier75467512007-09-24 05:34:19 +0000171#else
172 tst_resm(TINFO, "F_SETLEASE not defined, skipping test");
robbiewe93a3242005-08-31 20:27:12 +0000173#endif
Garrett Cooper2c282152010-12-16 00:55:50 -0800174 }
robbiew25ae5452004-03-02 22:55:42 +0000175
subrata_modak56207ce2009-03-23 13:35:39 +0000176 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800177 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800178}
robbiew25ae5452004-03-02 22:55:42 +0000179
180/***************************************************************
181 * setup() - performs all ONE TIME setup for this test.
182 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400183void setup(void)
robbiew25ae5452004-03-02 22:55:42 +0000184{
Garrett Cooper2c282152010-12-16 00:55:50 -0800185
subrata_modak56207ce2009-03-23 13:35:39 +0000186 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew25ae5452004-03-02 22:55:42 +0000187
subrata_modak56207ce2009-03-23 13:35:39 +0000188 TEST_PAUSE;
robbiew25ae5452004-03-02 22:55:42 +0000189
subrata_modak56207ce2009-03-23 13:35:39 +0000190 tst_tmpdir();
robbiew25ae5452004-03-02 22:55:42 +0000191
subrata_modak56207ce2009-03-23 13:35:39 +0000192 sprintf(fname, "tfile_%d", getpid());
193 if ((fd = open(fname, O_RDONLY | O_CREAT, 0777)) == -1) {
194 tst_brkm(TBROK, cleanup,
195 "open(%s, O_RDONLY|O_CREAT,0777) Failed, errno=%d : %s",
196 fname, errno, strerror(errno));
197 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800198}
robbiew25ae5452004-03-02 22:55:42 +0000199
200/***************************************************************
201 * cleanup() - performs all ONE TIME cleanup for this test at
202 * completion or premature exit.
203 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400204void cleanup(void)
robbiew25ae5452004-03-02 22:55:42 +0000205{
robbiew25ae5452004-03-02 22:55:42 +0000206
subrata_modak56207ce2009-03-23 13:35:39 +0000207 /* close the file we've had open */
208 if (close(fd) == -1) {
209 tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno,
210 strerror(errno));
211 }
robbiew25ae5452004-03-02 22:55:42 +0000212
subrata_modak56207ce2009-03-23 13:35:39 +0000213 tst_rmdir();
robbiew25ae5452004-03-02 22:55:42 +0000214
Chris Dearmanec6edca2012-10-17 19:54:01 -0700215}