• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /**********************************************************
21  *
22  *    TEST IDENTIFIER	: fcntl23
23  *
24  *    EXECUTED BY	: anyone
25  *
26  *    TEST TITLE	: Basic test for fcntl(2) using F_SETLEASE & F_RDLCK argument.
27  *
28  *    TEST CASE TOTAL	: 1
29  *
30  *    WALL CLOCK TIME	: 1
31  *
32  *    CPU TYPES		: ALL
33  *
34  *    AUTHOR		: Robbie Williamson
35  *
36  *    TEST CASES
37  *
38  *	1.) fcntl(2) returns...(See Description)
39  *
40  *    INPUT SPECIFICATIONS
41  *	The standard options for system call tests are accepted.
42  *	(See the parse_opts(3) man page).
43  *
44  *    OUTPUT SPECIFICATIONS
45  *
46  *    DURATION
47  *	Terminates - with frequency and infinite modes.
48  *
49  *    SIGNALS
50  *	Uses SIGUSR1 to pause before test if option set.
51  *	(See the parse_opts(3) man page).
52  *
53  *    RESOURCES
54  *	None
55  *
56  *    ENVIRONMENTAL NEEDS
57  *      No run-time environmental needs.
58  *
59  *    SPECIAL PROCEDURAL REQUIREMENTS
60  *	None
61  *
62  *    INTERCASE DEPENDENCIES
63  *	None
64  *
65  *    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).
70  *
71  *	Setup:
72  *	  Setup signal handling.
73  *	  Pause for SIGUSR1 if option specified.
74  *
75  *	Test:
76  *	 Loop if the proper options are given.
77  *	  Execute system call
78  *	  Check return code, if system call failed (return=-1)
79  *		Log the errno and Issue a FAIL message.
80  *	  Otherwise, Issue a PASS message.
81  *
82  *	Cleanup:
83  *	  Print errno log and/or timing stats if options given
84  *
85  *
86  *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
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 
97 void setup();
98 void cleanup();
99 
100 char *TCID = "fcntl23";
101 int TST_TOTAL = 1;
102 
103 char fname[255];
104 int fd;
105 
main(int ac,char ** av)106 int main(int ac, char **av)
107 {
108 	int lc;
109 
110     /***************************************************************
111      * parse standard options
112      ***************************************************************/
113 	tst_parse_opts(ac, av, NULL, NULL);
114 
115     /***************************************************************
116      * perform global setup for test
117      ***************************************************************/
118 	setup();
119 
120 	if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC) {
121 		tst_brkm(TCONF, cleanup,
122 			 "Cannot do fcntl on a file on NFS filesystem");
123 	}
124 
125     /***************************************************************
126      * check looping state if -c option given
127      ***************************************************************/
128 	for (lc = 0; TEST_LOOPING(lc); lc++) {
129 
130 		tst_count = 0;
131 
132 #ifdef F_SETLEASE
133 		/*
134 		 * Call fcntl(2) with F_SETLEASE & F_RDLCK argument on fname
135 		 */
136 		TEST(fcntl(fd, F_SETLEASE, F_RDLCK));
137 
138 		/* check return code */
139 		if (TEST_RETURN == -1) {
140 			tst_resm(TFAIL,
141 				 "fcntl(%s, F_SETLEASE, F_RDLCK) Failed, errno=%d : %s",
142 				 fname, TEST_ERRNO, strerror(TEST_ERRNO));
143 		} else {
144 
145 			TEST(fcntl(fd, F_GETLEASE));
146 			if (TEST_RETURN != F_RDLCK)
147 				tst_resm(TFAIL,
148 					 "fcntl(%s, F_GETLEASE) did not return F_RDLCK, returned %ld",
149 					 fname, TEST_RETURN);
150 			else {
151 				TEST(fcntl(fd, F_SETLEASE, F_UNLCK));
152 				if (TEST_RETURN != 0)
153 					tst_resm(TFAIL,
154 						 "fcntl(%s, F_SETLEASE, F_UNLCK) did not return 0, returned %ld",
155 						 fname, TEST_RETURN);
156 				else
157 					tst_resm(TPASS,
158 						 "fcntl(%s, F_SETLEASE, F_RDLCK)",
159 						 fname);
160 			}
161 
162 			if (close(TEST_RETURN) == -1) {
163 				tst_resm(TWARN,
164 					 "close(%s) Failed, errno=%d : %s",
165 					 fname, errno, strerror(errno));
166 			}
167 		}
168 #else
169 		tst_resm(TINFO, "F_SETLEASE not defined, skipping test");
170 #endif
171 	}
172 
173 	cleanup();
174 	tst_exit();
175 }
176 
177 /***************************************************************
178  * setup() - performs all ONE TIME setup for this test.
179  ***************************************************************/
setup(void)180 void setup(void)
181 {
182 
183 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
184 
185 	TEST_PAUSE;
186 
187 	tst_tmpdir();
188 
189 	sprintf(fname, "tfile_%d", getpid());
190 	if ((fd = open(fname, O_RDONLY | O_CREAT, 0777)) == -1) {
191 		tst_brkm(TBROK, cleanup,
192 			 "open(%s, O_RDONLY|O_CREAT,0777) Failed, errno=%d : %s",
193 			 fname, errno, strerror(errno));
194 	}
195 }
196 
197 /***************************************************************
198  * cleanup() - performs all ONE TIME cleanup for this test at
199  *		completion or premature exit.
200  ***************************************************************/
cleanup(void)201 void cleanup(void)
202 {
203 
204 	/* close the file we've had open */
205 	if (close(fd) == -1) {
206 		tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno,
207 			 strerror(errno));
208 	}
209 
210 	tst_rmdir();
211 
212 }
213