blob: 26c526f58eea480ad7428830f54b387a4863b977 [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
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"
96#include "usctest.h"
97
vapieraa354722006-05-26 06:26:37 +000098void setup();
99void cleanup();
robbiew25ae5452004-03-02 22:55:42 +0000100
subrata_modak56207ce2009-03-23 13:35:39 +0000101char *TCID = "fcntl23"; /* Test program identifier. */
102int TST_TOTAL = 1; /* Total number of test cases. */
robbiew25ae5452004-03-02 22:55:42 +0000103extern int Tst_count; /* Test Case counter for tst_* routines */
104
subrata_modak56207ce2009-03-23 13:35:39 +0000105int exp_enos[] = { 0, 0 };
robbiew25ae5452004-03-02 22:55:42 +0000106
107char fname[255];
108int fd;
109
subrata_modak56207ce2009-03-23 13:35:39 +0000110int main(int ac, char **av)
robbiew25ae5452004-03-02 22:55:42 +0000111{
subrata_modak56207ce2009-03-23 13:35:39 +0000112 int lc; /* loop counter */
113 char *msg; /* message returned from parse_opts */
114
robbiew25ae5452004-03-02 22:55:42 +0000115 /***************************************************************
116 * parse standard options
117 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800118 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800119 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew25ae5452004-03-02 22:55:42 +0000120
121 /***************************************************************
122 * perform global setup for test
123 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000124 setup();
robbiew25ae5452004-03-02 22:55:42 +0000125
subrata_modak56207ce2009-03-23 13:35:39 +0000126 /*
127 * check if the current filesystem is nfs
128 */
129 if (tst_is_cwd_nfs()) {
130 tst_brkm(TCONF, cleanup,
131 "Cannot do fcntl on a file located on an NFS filesystem");
132 }
subrata_modak5387ba02008-05-05 08:49:34 +0000133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 /* set the expected errnos... */
135 TEST_EXP_ENOS(exp_enos);
robbiew25ae5452004-03-02 22:55:42 +0000136
137 /***************************************************************
138 * check looping state if -c option given
139 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000140 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew25ae5452004-03-02 22:55:42 +0000141
subrata_modak56207ce2009-03-23 13:35:39 +0000142 /* reset Tst_count in case we are looping. */
143 Tst_count = 0;
robbiew25ae5452004-03-02 22:55:42 +0000144
robbiewe93a3242005-08-31 20:27:12 +0000145#ifdef F_SETLEASE
subrata_modak56207ce2009-03-23 13:35:39 +0000146 /*
147 * Call fcntl(2) with F_SETLEASE & F_RDLCK argument on fname
148 */
149 TEST(fcntl(fd, F_SETLEASE, F_RDLCK));
subrata_modakbdbaec52009-02-26 12:14:51 +0000150
subrata_modak56207ce2009-03-23 13:35:39 +0000151 /* check return code */
152 if (TEST_RETURN == -1) {
153 TEST_ERROR_LOG(TEST_ERRNO);
154 tst_resm(TFAIL,
155 "fcntl(%s, F_SETLEASE, F_RDLCK) Failed, errno=%d : %s",
156 fname, TEST_ERRNO, strerror(TEST_ERRNO));
157 } else {
158
robbiew25ae5452004-03-02 22:55:42 +0000159 /***************************************************************
160 * only perform functional verification if flag set (-f not given)
161 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000162 if (STD_FUNCTIONAL_TEST) {
163 TEST(fcntl(fd, F_GETLEASE));
164 if (TEST_RETURN != F_RDLCK)
165 tst_resm(TFAIL,
subrata_modak358c3ee2009-10-26 14:55:46 +0000166 "fcntl(%s, F_GETLEASE) did not return F_RDLCK, returned %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000167 fname, TEST_RETURN);
168 else {
169 TEST(fcntl(fd, F_SETLEASE, F_UNLCK));
170 if (TEST_RETURN != 0)
171 tst_resm(TFAIL,
subrata_modak358c3ee2009-10-26 14:55:46 +0000172 "fcntl(%s, F_SETLEASE, F_UNLCK) did not return 0, returned %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000173 fname, TEST_RETURN);
174 else
175 tst_resm(TPASS,
176 "fcntl(%s, F_SETLEASE, F_RDLCK)",
177 fname);
178 }
179 }
180 if (close(TEST_RETURN) == -1) {
181 tst_resm(TWARN,
182 "close(%s) Failed, errno=%d : %s",
183 fname, errno, strerror(errno));
184 }
185 }
vapier75467512007-09-24 05:34:19 +0000186#else
187 tst_resm(TINFO, "F_SETLEASE not defined, skipping test");
robbiewe93a3242005-08-31 20:27:12 +0000188#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000189 } /* End for TEST_LOOPING */
robbiew25ae5452004-03-02 22:55:42 +0000190
191 /***************************************************************
192 * cleanup and exit
193 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000194 cleanup();
robbiew25ae5452004-03-02 22:55:42 +0000195
subrata_modak56207ce2009-03-23 13:35:39 +0000196 return 0;
197} /* End main */
robbiew25ae5452004-03-02 22:55:42 +0000198
199/***************************************************************
200 * setup() - performs all ONE TIME setup for this test.
201 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000202void setup()
robbiew25ae5452004-03-02 22:55:42 +0000203{
subrata_modak56207ce2009-03-23 13:35:39 +0000204 /* capture signals */
205 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew25ae5452004-03-02 22:55:42 +0000206
subrata_modak56207ce2009-03-23 13:35:39 +0000207 /* Pause if that option was specified */
208 TEST_PAUSE;
robbiew25ae5452004-03-02 22:55:42 +0000209
subrata_modak56207ce2009-03-23 13:35:39 +0000210 /* make a temp directory and cd to it */
211 tst_tmpdir();
robbiew25ae5452004-03-02 22:55:42 +0000212
subrata_modak56207ce2009-03-23 13:35:39 +0000213 sprintf(fname, "tfile_%d", getpid());
214 if ((fd = open(fname, O_RDONLY | O_CREAT, 0777)) == -1) {
215 tst_brkm(TBROK, cleanup,
216 "open(%s, O_RDONLY|O_CREAT,0777) Failed, errno=%d : %s",
217 fname, errno, strerror(errno));
218 }
219} /* End setup() */
robbiew25ae5452004-03-02 22:55:42 +0000220
221/***************************************************************
222 * cleanup() - performs all ONE TIME cleanup for this test at
223 * completion or premature exit.
224 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000225void cleanup()
robbiew25ae5452004-03-02 22:55:42 +0000226{
subrata_modak56207ce2009-03-23 13:35:39 +0000227 /*
228 * print timing stats if that option was specified.
229 * print errno log if that option was specified.
230 */
231 TEST_CLEANUP;
robbiew25ae5452004-03-02 22:55:42 +0000232
subrata_modak56207ce2009-03-23 13:35:39 +0000233 /* close the file we've had open */
234 if (close(fd) == -1) {
235 tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno,
236 strerror(errno));
237 }
robbiew25ae5452004-03-02 22:55:42 +0000238
subrata_modak56207ce2009-03-23 13:35:39 +0000239 /* Remove tmp dir and all files in it */
240 tst_rmdir();
robbiew25ae5452004-03-02 22:55:42 +0000241
subrata_modak56207ce2009-03-23 13:35:39 +0000242 /* exit with return code appropriate for results */
243 tst_exit();
244} /* End cleanup() */