blob: 7699e29953264613c641969ea7446859c51426ac [file] [log] [blame]
robbiew7f01a512002-07-08 17:06:30 +00001/*
2 * Copyright (C) Bull S.A. 2001
3 * Copyright (c) International Business Machines Corp., 2001
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
robbiew7f01a512002-07-08 17:06:30 +000018 */
19
20/*
21 * Test Name: fcntl22
22 *
23 * Test Description:
24 * Verify that, fcntl() fails with -1 and sets errno to EAGAIN when
subrata_modak56207ce2009-03-23 13:35:39 +000025 * Operation is prohibited by locks held by other processes.
robbiew7f01a512002-07-08 17:06:30 +000026 *
27 * Expected Result:
28 * fcntl() should fail with return value -1 and sets expected errno.
29 *
30 * Algorithm:
31 * Setup:
32 * Setup signal handling.
33 * Pause for SIGUSR1 if option specified.
34 * Create temporary directory.
35 * Create and open temporary file.
36 * Lock temporary file.
37 *
38 * Test:
39 * Loop if the proper options are given.
40 * Duplicate process
41 * Parent waits for child termination
42 * Child execute system call
43 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000044 * if errno set == expected errno
45 * Issue sys call fails with expected return value and errno.
46 * Otherwise,
47 * Issue sys call fails with unexpected errno.
robbiew7f01a512002-07-08 17:06:30 +000048 * Otherwise,
49 * Issue sys call returns unexpected value.
50 *
51 * Cleanup:
52 * Print errno log and/or timing stats if options given
53 *
54 * Usage: <for command-line>
55 * fcntl22 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
56 * where, -c n : Run n copies concurrently.
57 * -e : Turn on errno logging.
58 * -f : Turn off functionality Testing.
59 * -i n : Execute test n times.
60 * -I x : Execute test for x seconds.
61 * -P x : Pause for x seconds between iterations.
62 * -t : Turn on syscall timing.
63 *
64 * HISTORY
65 * 06/2002 Ported by Jacky Malcles
66 *
67 * RESTRICTIONS:
68 * none.
69 *
70 */
71
72#include <fcntl.h>
73#include <errno.h>
74#include <signal.h>
robbiew7f01a512002-07-08 17:06:30 +000075#include <stdlib.h>
mridgedb639212005-01-04 21:04:11 +000076#include <sys/types.h>
77#include <sys/wait.h>
robbiew5aab8a72003-03-26 18:05:30 +000078#include "test.h"
robbiew7f01a512002-07-08 17:06:30 +000079
80int child_pid;
81int file;
subrata_modak56207ce2009-03-23 13:35:39 +000082struct flock fl; /* struct flock for fcntl */
robbiew7f01a512002-07-08 17:06:30 +000083
robbiew7f01a512002-07-08 17:06:30 +000084char *TCID = "fcntl22";
85int TST_TOTAL = 1;
robbiew7f01a512002-07-08 17:06:30 +000086
87void setup(void);
88void cleanup(void);
89
robbiew5aab8a72003-03-26 18:05:30 +000090int main(int ac, char **av)
robbiew7f01a512002-07-08 17:06:30 +000091{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020092 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020093 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000094 char *test_desc; /* test specific error message */
robbiew7f01a512002-07-08 17:06:30 +000095
Garrett Cooper45e285d2010-11-22 12:19:25 -080096 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080097 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +000098 }
99
100 /* setup */
101 setup();
102
subrata_modak56207ce2009-03-23 13:35:39 +0000103 /* Check for looping state if -i option is given */
104 for (lc = 0; TEST_LOOPING(lc); lc++) {
mridgedb639212005-01-04 21:04:11 +0000105#ifdef __hpux
subrata_modak56207ce2009-03-23 13:35:39 +0000106 test_desc = "EACCES";
mridgedb639212005-01-04 21:04:11 +0000107#else
subrata_modak56207ce2009-03-23 13:35:39 +0000108 test_desc = "EAGAIN";
mridgedb639212005-01-04 21:04:11 +0000109#endif
robbiew7f01a512002-07-08 17:06:30 +0000110
Caspar Zhangd59a6592013-03-07 14:59:12 +0800111 /* reset tst_count in case we are looping */
112 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000113
114 /* duplicate process */
115 if ((child_pid = FORK_OR_VFORK()) == 0) {
116 /* child */
117 /*
118 * Call fcntl(2) with F_SETLK argument on file
119 */
120 TEST(fcntl(file, F_SETLK, &fl));
121
122 /* Check return code from fcntl(2) */
123 if (TEST_RETURN != -1) {
subrata_modak358c3ee2009-10-26 14:55:46 +0000124 tst_resm(TFAIL, "fcntl() returned %ld,"
subrata_modak56207ce2009-03-23 13:35:39 +0000125 "expected -1, errno=%d", TEST_RETURN,
Cyril Hrubis605fa332015-02-04 13:11:20 +0100126 EAGAIN);
subrata_modak56207ce2009-03-23 13:35:39 +0000127 } else {
Cyril Hrubis605fa332015-02-04 13:11:20 +0100128 if (TEST_ERRNO == EAGAIN) {
subrata_modak56207ce2009-03-23 13:35:39 +0000129 tst_resm(TPASS,
130 "fcntl() fails with expected "
131 "error %s errno:%d", test_desc,
132 TEST_ERRNO);
133 } else {
134 tst_resm(TFAIL, "fcntl() fails, %s, "
135 "errno=%d, expected errno=%d",
136 test_desc, TEST_ERRNO,
Cyril Hrubis605fa332015-02-04 13:11:20 +0100137 EAGAIN);
subrata_modak56207ce2009-03-23 13:35:39 +0000138 }
139 }
140 /* end child */
141 } else {
142 if (child_pid < 0) {
143 /* quit if fork fail */
144 tst_resm(TFAIL, "Fork failed");
145 cleanup();
146 } else {
147 /* Parent waits for child termination */
148 wait(0);
149 cleanup();
150 }
151 }
152
153 } /* end for */
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800154 tst_exit();
robbiew7f01a512002-07-08 17:06:30 +0000155}
156
157/*
158 * setup
subrata_modak56207ce2009-03-23 13:35:39 +0000159 * performs all ONE TIME setup for this test
robbiew7f01a512002-07-08 17:06:30 +0000160 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400161void setup(void)
robbiew7f01a512002-07-08 17:06:30 +0000162{
robbiew7f01a512002-07-08 17:06:30 +0000163
subrata_modak56207ce2009-03-23 13:35:39 +0000164 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiew7f01a512002-07-08 17:06:30 +0000165
subrata_modak56207ce2009-03-23 13:35:39 +0000166 TEST_PAUSE;
167
168 /* Make a temp dir and cd to it */
169 tst_tmpdir();
170
171 /* create regular file */
172 if ((file = creat("regfile", 0777)) == -1) {
173 tst_brkm(TBROK, cleanup,
174 "creat(regfile, 0777) failed, errno:%d %s", errno,
175 strerror(errno));
176 }
177 /* struct lock */
178 fl.l_type = F_WRLCK;
179 fl.l_whence = 0;
180 fl.l_start = 0;
181 fl.l_len = 0;
182
183 /* lock file */
184 if (fcntl(file, F_SETLK, &fl) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 tst_resm(TFAIL | TERRNO, "fcntl on file %d failed", file);
subrata_modak56207ce2009-03-23 13:35:39 +0000186 }
robbiew7f01a512002-07-08 17:06:30 +0000187
188}
189
190/*
191 * cleanup()
subrata_modak56207ce2009-03-23 13:35:39 +0000192 * performs all ONE TIME cleanup for this test at completion or
193 * premature exit
robbiew7f01a512002-07-08 17:06:30 +0000194 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400195void cleanup(void)
robbiew7f01a512002-07-08 17:06:30 +0000196{
subrata_modak56207ce2009-03-23 13:35:39 +0000197 /*
198 * print timing stats if that option was specified
199 * print errno log if that option was specified
200 */
201 close(file);
mridge22c90442004-08-25 15:26:18 +0000202
subrata_modak56207ce2009-03-23 13:35:39 +0000203 tst_rmdir();
robbiew7f01a512002-07-08 17:06:30 +0000204
Chris Dearmanec6edca2012-10-17 19:54:01 -0700205}