blob: 8e1ddc5eb34785a1d2fb3107e5377f306e71961b [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
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
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
subrata_modak56207ce2009-03-23 13:35:39 +000022 * fcntl15.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
subrata_modak56207ce2009-03-23 13:35:39 +000025 * Check that file locks are removed when file closed
plars865695b2001-08-27 22:15:12 +000026 *
27 * ALGORITHM
subrata_modak56207ce2009-03-23 13:35:39 +000028 * Use three testcases to check removal of locks when a file is closed.
plars865695b2001-08-27 22:15:12 +000029 *
subrata_modak56207ce2009-03-23 13:35:39 +000030 * Case 1: Parent opens a file and duplicates it, places locks using
31 * both file descriptors then closes one descriptor, all locks should
32 * be removed.
plars865695b2001-08-27 22:15:12 +000033 *
subrata_modak56207ce2009-03-23 13:35:39 +000034 * Case 2: Open same file twice using(open), place locks using both
35 * descriptors then close on descriptor, locks on the file should be
36 * lost
37 *
38 * Case 3: Open file twice, one by each process, set the locks and have
39 * a child check the locks. Remove the first file and have the child
40 * check the locks. Remove the first file and have child check locks
41 * again. Only locks set on first file should have been removed
plars865695b2001-08-27 22:15:12 +000042 *
43 * USAGE
plars363a35f2002-02-18 22:10:07 +000044 * fcntl15
plars865695b2001-08-27 22:15:12 +000045 *
46 * HISTORY
47 * 07/2001 Ported by Wayne Boyer
mridge562dd482004-05-11 21:54:47 +000048 * MODIFIED: - mridge@us.ibm.com -- changed getpid to syscall(get thread ID) for unique ID on NPTL threading
plars865695b2001-08-27 22:15:12 +000049 *
50 * RESTRICTIONS
subrata_modak56207ce2009-03-23 13:35:39 +000051 * None
plars865695b2001-08-27 22:15:12 +000052 */
53
54#include <signal.h>
55#include <fcntl.h>
Garrett Cooper0a643cb2010-12-21 11:21:19 -080056#include "test.h"
mridgedb639212005-01-04 21:04:11 +000057#include <sys/types.h>
58#include <sys/wait.h>
mridge562dd482004-05-11 21:54:47 +000059#include <sys/types.h>
60#include <sys/syscall.h>
61#include <linux/unistd.h>
plars865695b2001-08-27 22:15:12 +000062
63#define DATA "ABCDEFGHIJ"
64#define DUP 0
65#define OPEN 1
66#define FORK_ 2
67
plars363a35f2002-02-18 22:10:07 +000068char *TCID = "fcntl15";
plars865695b2001-08-27 22:15:12 +000069int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000070
subrata_modak56207ce2009-03-23 13:35:39 +000071static int parent, child1, child2, status;
72static volatile sig_atomic_t parent_flag, child_flag, alarm_flag;
73static char tmpname[40];
74struct flock flock;
plars865695b2001-08-27 22:15:12 +000075
robbiewd34d5812005-07-11 22:28:09 +000076#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +000077static char *argv0; /* set by main, passed to self_exec */
robbiewd34d5812005-07-11 22:28:09 +000078#endif
79
robbiewb3f0d5d2003-03-25 22:57:02 +000080/*
81 * cleanup() - performs all ONE TIME cleanup for this test at
82 * completion or premature exit.
83 */
Mike Frysingerc57fba52014-04-09 18:56:30 -040084void cleanup(void)
plars865695b2001-08-27 22:15:12 +000085{
plars865695b2001-08-27 22:15:12 +000086
robbiewb3f0d5d2003-03-25 22:57:02 +000087}
plars865695b2001-08-27 22:15:12 +000088
Mike Frysingere61ddba2014-04-09 23:24:32 -040089void alarm_sig(int sig)
robbiewb3f0d5d2003-03-25 22:57:02 +000090{
Mike Frysingere61ddba2014-04-09 23:24:32 -040091 signal(SIGALRM, alarm_sig);
robbiewd00d6512005-01-11 16:05:34 +000092 alarm_flag = 1;
mridge562dd482004-05-11 21:54:47 +000093 if ((syscall(__NR_gettid)) == parent) {
robbiewb3f0d5d2003-03-25 22:57:02 +000094 tst_resm(TINFO, "Alarm caught by parent");
95 } else {
96 tst_resm(TINFO, "Alarm caught by child");
97 }
98}
99
Mike Frysingere61ddba2014-04-09 23:24:32 -0400100void child_sig(int sig)
robbiewb3f0d5d2003-03-25 22:57:02 +0000101{
Mike Frysingere61ddba2014-04-09 23:24:32 -0400102 signal(SIGUSR1, child_sig);
robbiewb3f0d5d2003-03-25 22:57:02 +0000103 child_flag++;
104}
105
Mike Frysingere61ddba2014-04-09 23:24:32 -0400106void parent_sig(int sig)
robbiewb3f0d5d2003-03-25 22:57:02 +0000107{
Mike Frysingere61ddba2014-04-09 23:24:32 -0400108 signal(SIGUSR2, parent_sig);
robbiewb3f0d5d2003-03-25 22:57:02 +0000109 parent_flag++;
110}
111
112int dochild1(int file_flag, int file_mode)
113{
114 int fd_B;
robbiewd00d6512005-01-11 16:05:34 +0000115 sigset_t newmask, zeromask, oldmask;
robbiewb3f0d5d2003-03-25 22:57:02 +0000116
117 if ((fd_B = open(tmpname, file_flag, file_mode)) < 0) {
118 perror("open on child1 file failed");
119 exit(1);
plars865695b2001-08-27 22:15:12 +0000120 }
121
robbiewb3f0d5d2003-03-25 22:57:02 +0000122 /* initialize lock structure for second 5 bytes of file */
123 flock.l_type = F_WRLCK;
124 flock.l_whence = 0;
125 flock.l_start = 5L;
126 flock.l_len = 5L;
plars865695b2001-08-27 22:15:12 +0000127
robbiewb3f0d5d2003-03-25 22:57:02 +0000128 /* set lock on child file descriptor */
129 if ((fcntl(fd_B, F_SETLK, &flock)) < 0) {
130 perror("child lock failed should have succeeded");
131 exit(1);
132 }
plars865695b2001-08-27 22:15:12 +0000133
robbiewd00d6512005-01-11 16:05:34 +0000134 sigemptyset(&zeromask);
135 sigemptyset(&newmask);
136 sigaddset(&newmask, SIGUSR1);
137 sigaddset(&newmask, SIGUSR2);
138 sigaddset(&newmask, SIGALRM);
139 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
140 perror("child1 sigprocmask SIG_BLOCK fail");
141 exit(1);
142 }
robbiewb3f0d5d2003-03-25 22:57:02 +0000143 /*
144 * send signal to parent here to tell parent we have locked the
145 * file, thus allowing parent to proceed
146 */
147 if ((kill(parent, SIGUSR1)) < 0) {
148 perror("child1 signal to parent failed");
149 exit(1);
150 }
151
152 /*
153 * set alarm to break pause if parent fails to signal then spin till
154 * parent ready
155 */
robbiewd00d6512005-01-11 16:05:34 +0000156 alarm(60);
subrata_modak4bb656a2009-02-26 12:02:09 +0000157 while (parent_flag == 0 && alarm_flag == 0)
robbiewd00d6512005-01-11 16:05:34 +0000158 sigsuspend(&zeromask);
159 alarm((unsigned)0);
160 if (parent_flag != 1) {
161 perror("pause in child1 terminated without "
subrata_modak56207ce2009-03-23 13:35:39 +0000162 "SIGUSR2 signal from parent");
robbiewd00d6512005-01-11 16:05:34 +0000163 exit(1);
plars865695b2001-08-27 22:15:12 +0000164 }
robbiewd00d6512005-01-11 16:05:34 +0000165 parent_flag = 0;
166 alarm_flag = 0;
167 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
168 perror("child1 sigprocmask SIG_SETMASK fail");
169 exit(1);
170 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000171
robbiewb3f0d5d2003-03-25 22:57:02 +0000172 /* wait for child2 to complete then cleanup */
173 sleep(10);
174 close(fd_B);
175 exit(0);
plars865695b2001-08-27 22:15:12 +0000176}
177
robbiewd34d5812005-07-11 22:28:09 +0000178#ifdef UCLINUX
Garrett Cooper4fac2902010-12-21 11:53:17 -0800179int uc_file_flag, uc_file_mode, uc_dup_flag;
robbiewd34d5812005-07-11 22:28:09 +0000180
Mike Frysingerc57fba52014-04-09 18:56:30 -0400181void dochild1_uc(void)
robbiewd34d5812005-07-11 22:28:09 +0000182{
183 dochild1(uc_file_flag, uc_file_mode);
184}
Garrett Cooper4fac2902010-12-21 11:53:17 -0800185
Mike Frysingerc57fba52014-04-09 18:56:30 -0400186void dochild2_uc(void)
Garrett Cooper4fac2902010-12-21 11:53:17 -0800187{
188 dochild2(uc_file_flag, uc_dup_flag);
189}
190#endif
robbiewd34d5812005-07-11 22:28:09 +0000191
robbiewb3f0d5d2003-03-25 22:57:02 +0000192int dofork(int file_flag, int file_mode)
193{
194 /* create child process */
mreed1081534c32006-08-03 05:21:24 +0000195 if ((child1 = FORK_OR_VFORK()) < 0) {
robbiewb3f0d5d2003-03-25 22:57:02 +0000196 perror("Fork failure");
subrata_modak134e8962009-02-26 11:46:54 +0000197 return 1;
robbiewb3f0d5d2003-03-25 22:57:02 +0000198 }
199
200 /* child1 */
201 if (child1 == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000202#ifdef UCLINUX
203 if (self_exec(argv0, "nddds", 1, file_flag, file_mode,
204 parent, tmpname) < 0) {
205 perror("self_exec failure");
subrata_modak134e8962009-02-26 11:46:54 +0000206 return 1;
robbiewd34d5812005-07-11 22:28:09 +0000207 }
208#else
robbiewb3f0d5d2003-03-25 22:57:02 +0000209 dochild1(file_flag, file_mode);
robbiewd34d5812005-07-11 22:28:09 +0000210#endif
robbiewb3f0d5d2003-03-25 22:57:02 +0000211 } else {
212 /*
213 * need to wait for child1 to open, and lock the area of the
214 * file prior to continuing on from here
215 */
robbiewd00d6512005-01-11 16:05:34 +0000216 sigset_t newmask, zeromask, oldmask;
217 sigemptyset(&zeromask);
218 sigemptyset(&newmask);
219 sigaddset(&newmask, SIGUSR1);
220 sigaddset(&newmask, SIGUSR2);
221 sigaddset(&newmask, SIGALRM);
222 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
223 perror("parent sigprocmask SIG_BLOCK fail");
224 exit(1);
225 }
226
227 /*
228 * set alarm to break pause if parent fails to signal then spin till
229 * parent ready
230 */
231 alarm(60);
subrata_modak4bb656a2009-02-26 12:02:09 +0000232 while (child_flag == 0 && alarm_flag == 0)
robbiewd00d6512005-01-11 16:05:34 +0000233 sigsuspend(&zeromask);
234 alarm((unsigned)0);
235 if (child_flag != 1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000236 perror("parent paused without SIGUSR1 " "from child");
robbiewd00d6512005-01-11 16:05:34 +0000237 exit(1);
238 }
239 child_flag = 0;
240 alarm_flag = 0;
241 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
242 perror("parent sigprocmask SIG_SETMASK fail");
243 exit(1);
robbiewb3f0d5d2003-03-25 22:57:02 +0000244 }
245 }
subrata_modak43337a32009-02-26 11:43:51 +0000246 return 0;
robbiewb3f0d5d2003-03-25 22:57:02 +0000247}
248
249int dochild2(int file_flag, int file_mode, int dup_flag)
250{
251 int fd_C;
robbiewd00d6512005-01-11 16:05:34 +0000252 sigset_t newmask, zeromask, oldmask;
robbiewb3f0d5d2003-03-25 22:57:02 +0000253
254 if ((fd_C = open(tmpname, file_flag, file_mode)) < 0) {
255 perror("open on child2 file failed");
256 exit(1);
257 }
258
259 /* initialize lock structure for first 5 bytes of file */
260 flock.l_type = F_WRLCK;
261 flock.l_whence = 0;
262 flock.l_start = 0L;
263 flock.l_len = 5L;
264
265 /* Set lock on child file descriptor */
266 if ((fcntl(fd_C, F_SETLK, &flock)) >= 0) {
267 tst_resm(TFAIL, "First child2 lock succeeded should "
268 "have failed");
269 exit(1);
270 }
271
272 /* initialize lock structure for second 5 bytes of file */
273 flock.l_type = F_WRLCK;
274 flock.l_whence = 0;
275 flock.l_start = 5L;
276 flock.l_len = 5L;
subrata_modakbdbaec52009-02-26 12:14:51 +0000277
robbiewb3f0d5d2003-03-25 22:57:02 +0000278 /* set lock on child file descriptor */
279 if ((fcntl(fd_C, F_SETLK, &flock)) >= 0) {
280 tst_resm(TFAIL, "second child2 lock succeeded should have "
281 "failed");
282 exit(1);
283 }
284
robbiewd00d6512005-01-11 16:05:34 +0000285 sigemptyset(&zeromask);
286 sigemptyset(&newmask);
287 sigaddset(&newmask, SIGUSR1);
288 sigaddset(&newmask, SIGUSR2);
289 sigaddset(&newmask, SIGALRM);
290 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
291 perror("child2 sigprocmask SIG_BLOCK fail");
292 exit(1);
293 }
294 /*
295 * send signal to parent here to tell parent we have locked the
296 * file, thus allowing parent to proceed
297 */
robbiewb3f0d5d2003-03-25 22:57:02 +0000298 if ((kill(parent, SIGUSR1)) < 0) {
robbiewd00d6512005-01-11 16:05:34 +0000299 perror("child2 signal to parent failed");
robbiewb3f0d5d2003-03-25 22:57:02 +0000300 exit(1);
301 }
302
robbiewd00d6512005-01-11 16:05:34 +0000303 /*
304 * set alarm to break pause if parent fails to signal then spin till
305 * parent ready
306 */
307 alarm(60);
subrata_modak4bb656a2009-02-26 12:02:09 +0000308 while (parent_flag == 0 && alarm_flag == 0)
robbiewd00d6512005-01-11 16:05:34 +0000309 sigsuspend(&zeromask);
310 alarm((unsigned)0);
311 if (parent_flag != 1) {
312 perror("pause in child2 terminated without "
subrata_modak56207ce2009-03-23 13:35:39 +0000313 "SIGUSR2 signal from parent");
robbiewd00d6512005-01-11 16:05:34 +0000314 exit(1);
315 }
316 parent_flag = 0;
317 alarm_flag = 0;
318 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
319 perror("child2 sigprocmask SIG_SETMASK fail");
320 exit(1);
robbiewb3f0d5d2003-03-25 22:57:02 +0000321 }
322
323 /* initialize lock structure for first 5 bytes of file */
324 flock.l_type = F_WRLCK;
325 flock.l_whence = 0;
326 flock.l_start = 0L;
327 flock.l_len = 5L;
328
329 /* set lock on child file descriptor */
330 if ((fcntl(fd_C, F_SETLK, &flock)) < 0) {
331 tst_resm(TFAIL, "third child2 lock failed should have "
332 "succeeded");
333 exit(1);
334 }
335
336 /* Initialize lock structure for second 5 bytes of file */
337 flock.l_type = F_WRLCK;
338 flock.l_whence = 0;
339 flock.l_start = 5L;
340 flock.l_len = 5L;
341
342 /* set lock on child file descriptor */
343 if (dup_flag == FORK_) {
344 if ((fcntl(fd_C, F_SETLK, &flock)) >= 0) {
345 tst_resm(TFAIL, "fourth child2 lock succeeded "
346 "should have failed");
347 exit(1);
348 }
349 } else {
350 if ((fcntl(fd_C, F_SETLK, &flock)) < 0) {
351 tst_resm(TFAIL, "fourth child2 lock failed "
352 "should have succeeded");
353 exit(1);
354 }
355 }
356 close(fd_C);
357 exit(0);
358}
359
360/*
361 * setup() - performs all ONE TIME setup for this test.
362 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400363void setup(void)
robbiewb3f0d5d2003-03-25 22:57:02 +0000364{
Garrett Cooper2c282152010-12-16 00:55:50 -0800365
robbiewb3f0d5d2003-03-25 22:57:02 +0000366 tst_sig(FORK, DEF_HANDLER, cleanup);
367
robbiewb3f0d5d2003-03-25 22:57:02 +0000368 TEST_PAUSE;
369}
370
subrata_modak56207ce2009-03-23 13:35:39 +0000371int run_test(int file_flag, int file_mode, int dup_flag)
plars865695b2001-08-27 22:15:12 +0000372{
373 int fd_A, fd_B;
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800374 fd_B = -1;
robbiewd00d6512005-01-11 16:05:34 +0000375 sigset_t newmask, zeromask, oldmask;
plars865695b2001-08-27 22:15:12 +0000376
377 /* setup to catch SIGUSR1 signal from child process */
378 if ((signal(SIGUSR1, child_sig)) == SIG_ERR) {
379 perror("Signal setup for SIGUSR1 failed");
380 }
381
382 /* setup to catch SIGUSR2 signal from parent */
383 if ((signal(SIGUSR2, parent_sig)) == SIG_ERR) {
384 perror("Signal setup for SIGUSR1 failed");
385 }
386
mridge562dd482004-05-11 21:54:47 +0000387 parent = syscall(__NR_gettid);
plars865695b2001-08-27 22:15:12 +0000388
389 tst_tmpdir();
390 /* setup temporary file name */
plars363a35f2002-02-18 22:10:07 +0000391 sprintf(tmpname, "fcntl15.%d", parent);
plars865695b2001-08-27 22:15:12 +0000392
393 /* initialize signal flags */
robbiewd00d6512005-01-11 16:05:34 +0000394 child_flag = parent_flag = alarm_flag = 0;
plars865695b2001-08-27 22:15:12 +0000395
396 if ((fd_A = open(tmpname, file_flag, file_mode)) < 0) {
397 perror("open first parent file failed");
398 tst_rmdir();
subrata_modak134e8962009-02-26 11:46:54 +0000399 return 1;
plars865695b2001-08-27 22:15:12 +0000400 }
401
402 /* write some data to the file */
403 (void)write(fd_A, DATA, 10);
404
405 if (dup_flag) {
406 if (dup_flag == FORK_) {
407 dofork(file_flag, file_mode);
408 } else {
409 if ((fd_B = open(tmpname, file_flag, file_mode)) < 0) {
410 perror("open second parent file failed");
411 tst_rmdir();
subrata_modak134e8962009-02-26 11:46:54 +0000412 return 1;
plars865695b2001-08-27 22:15:12 +0000413 }
414 }
415 } else {
416 /* create a second file descriptor from first file */
417 if ((fd_B = fcntl(fd_A, F_DUPFD, 0)) < 0) {
418 perror("dup of second parent file failed");
419 tst_rmdir();
subrata_modak134e8962009-02-26 11:46:54 +0000420 return 1;
plars865695b2001-08-27 22:15:12 +0000421 }
422 }
423
subrata_modak4bb656a2009-02-26 12:02:09 +0000424 /*
plars865695b2001-08-27 22:15:12 +0000425 * initialize lock structure for first lock on first
426 * 5 bytes of file
427 */
428 flock.l_type = F_WRLCK;
429 flock.l_whence = 0;
430 flock.l_start = 0L;
431 flock.l_len = 5L;
432
433 /* set lock on first file descriptor */
434 if ((fcntl(fd_A, F_SETLK, &flock)) < 0) {
435 perror("Attempt to set first parent lock failed");
436 tst_rmdir();
subrata_modak134e8962009-02-26 11:46:54 +0000437 return 1;
plars865695b2001-08-27 22:15:12 +0000438 }
439
440 if (dup_flag != FORK_) {
441 /* initialize lock structure for last 5 bytes of file */
442 flock.l_type = F_WRLCK;
443 flock.l_whence = 0;
444 flock.l_start = 5L;
445 flock.l_len = 5L;
446
447 /* set lock on second file descriptor */
448 if ((fcntl(fd_B, F_SETLK, &flock)) < 0) {
449 perror("Attempt to set second parent lock failed");
450 tst_rmdir();
subrata_modak134e8962009-02-26 11:46:54 +0000451 return 1;
plars865695b2001-08-27 22:15:12 +0000452 }
453 }
454
455 /* create child process */
subrata_modakbdbaec52009-02-26 12:14:51 +0000456 if ((child2 = FORK_OR_VFORK()) < 0) {
plars865695b2001-08-27 22:15:12 +0000457 perror("Fork failure");
458 tst_rmdir();
subrata_modak134e8962009-02-26 11:46:54 +0000459 return 1;
subrata_modak56207ce2009-03-23 13:35:39 +0000460 } else if (child2 == 0) { /* child */
Garrett Cooper4fac2902010-12-21 11:53:17 -0800461#ifdef UCLINUX
462 if (self_exec(argv0, "ndddds", 2, file_flag, file_mode,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800463 dup_flag, parent, tmpname) < 0)
464 tst_brkm(TBROK | TERRNO, NULL, "self_exec failed");
Garrett Cooper4fac2902010-12-21 11:53:17 -0800465#else
plars865695b2001-08-27 22:15:12 +0000466 dochild2(file_flag, file_mode, dup_flag);
Garrett Cooper4fac2902010-12-21 11:53:17 -0800467#endif
plars865695b2001-08-27 22:15:12 +0000468 }
469
470 /* parent */
471
472 /*
473 * Set alarm to break pause if child fails to signal then spin till
474 * child is ready
475 */
robbiewd00d6512005-01-11 16:05:34 +0000476
477 sigemptyset(&zeromask);
478 sigemptyset(&newmask);
479 sigaddset(&newmask, SIGUSR1);
480 sigaddset(&newmask, SIGUSR2);
481 sigaddset(&newmask, SIGALRM);
482 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
483 perror("parent sigprocmask SIG_BLOCK fail");
484 exit(1);
485 }
486
487 /*
488 * set alarm to break pause if parent fails to signal then spin till
489 * parent ready
490 */
491 alarm(60);
subrata_modak4bb656a2009-02-26 12:02:09 +0000492 while (child_flag == 0 && alarm_flag == 0)
robbiewd00d6512005-01-11 16:05:34 +0000493 sigsuspend(&zeromask);
494 alarm((unsigned)0);
495 if (child_flag != 1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000496 perror("parent paused without SIGUSR1 " "from child");
robbiewd00d6512005-01-11 16:05:34 +0000497 exit(1);
498 }
499 child_flag = 0;
500 alarm_flag = 0;
501 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
502 perror("parent sigprocmask SIG_SETMASK fail");
503 exit(1);
plars865695b2001-08-27 22:15:12 +0000504 }
505
506 /* close the first file then signal child to test locks */
507 close(fd_A);
508 if ((kill(child2, SIGUSR2)) < 0) {
509 perror("Signal to child2 failed");
510 tst_rmdir();
subrata_modak134e8962009-02-26 11:46:54 +0000511 return 1;
plars865695b2001-08-27 22:15:12 +0000512 }
513
514 if (dup_flag == FORK_) {
515 if ((kill(child1, SIGUSR2)) < 0) {
516 perror("Signal to child1 failed");
517 tst_rmdir();
subrata_modak134e8962009-02-26 11:46:54 +0000518 return 1;
plars865695b2001-08-27 22:15:12 +0000519 }
520 }
521 /* wait for child to complete then cleanup */
522 while ((wait(&status)) > 0) {
523 if (status >> 8 != 0) {
524 tst_resm(TFAIL, "Expected 0 got %d", status >> 8);
525 tst_rmdir();
subrata_modak134e8962009-02-26 11:46:54 +0000526 return 1;
plars865695b2001-08-27 22:15:12 +0000527 }
528 }
529 if (dup_flag != FORK_) {
530 close(fd_B);
531 }
532 unlink(tmpname);
533 tst_rmdir();
subrata_modak43337a32009-02-26 11:43:51 +0000534 return 0;
plars865695b2001-08-27 22:15:12 +0000535}
536
robbiewb3f0d5d2003-03-25 22:57:02 +0000537int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000538{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200539 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200540 const char *msg;
plars865695b2001-08-27 22:15:12 +0000541
robbiewb3f0d5d2003-03-25 22:57:02 +0000542 int fail = 0;
plars865695b2001-08-27 22:15:12 +0000543
Garrett Cooper45e285d2010-11-22 12:19:25 -0800544 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800545 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000546 }
robbiewd34d5812005-07-11 22:28:09 +0000547#ifdef UCLINUX
548 maybe_run_child(&dochild1_uc, "nddds", 1, &uc_file_flag,
549 &uc_file_mode, &parent, tmpname);
Garrett Cooper4fac2902010-12-21 11:53:17 -0800550 maybe_run_child(&dochild2_uc, "nddds", 1, &uc_file_flag,
551 &uc_file_mode, &uc_dup_flag, &parent, tmpname);
robbiewd34d5812005-07-11 22:28:09 +0000552 argv0 = av[0];
553#endif
554
robbiewb3f0d5d2003-03-25 22:57:02 +0000555 setup();
plars865695b2001-08-27 22:15:12 +0000556
robbiewb3f0d5d2003-03-25 22:57:02 +0000557 /* Check for looping state if -i option is given */
558 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800559 /* reset tst_count in case we are looping */
560 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000561
robbiewb3f0d5d2003-03-25 22:57:02 +0000562 /* Set up to catch alarm signal */
563 if ((signal(SIGALRM, alarm_sig)) == SIG_ERR) {
564 perror("SIGALRM signal set up failed");
plars865695b2001-08-27 22:15:12 +0000565 exit(1);
566 }
plars865695b2001-08-27 22:15:12 +0000567
mridgedb639212005-01-04 21:04:11 +0000568/* //block1: */
robbiewb3f0d5d2003-03-25 22:57:02 +0000569 tst_resm(TINFO, "Entering block 1");
570 if (run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, DUP)) {
571 tst_resm(TINFO, "Test 1: test with \"dup\" FAILED");
572 fail = 1;
573 } else {
574 tst_resm(TINFO, "Test 1: test with \"dup\" PASSED");
plars865695b2001-08-27 22:15:12 +0000575 }
robbiewb3f0d5d2003-03-25 22:57:02 +0000576 tst_resm(TINFO, "Exiting block 1");
plars865695b2001-08-27 22:15:12 +0000577
mridgedb639212005-01-04 21:04:11 +0000578/* //block2: */
robbiewb3f0d5d2003-03-25 22:57:02 +0000579 tst_resm(TINFO, "Entering block 2");
580 if (run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, OPEN)) {
581 tst_resm(TINFO, "Test 2: test with \"open\" FAILED");
582 fail = 1;
583 } else {
584 tst_resm(TINFO, "Test 2: test with \"open\" PASSED");
plars865695b2001-08-27 22:15:12 +0000585 }
robbiewb3f0d5d2003-03-25 22:57:02 +0000586 tst_resm(TINFO, "Exiting block 2");
587
mridgedb639212005-01-04 21:04:11 +0000588/* //block3: */
robbiewb3f0d5d2003-03-25 22:57:02 +0000589 tst_resm(TINFO, "Entering block 3");
590 if (run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, FORK_)) {
591 tst_resm(TINFO, "Test 3: test with \"fork\" FAILED");
592 fail = 1;
593 } else {
594 tst_resm(TINFO, "Test 3: test with \"fork\" PASSED");
plars865695b2001-08-27 22:15:12 +0000595 }
robbiewb3f0d5d2003-03-25 22:57:02 +0000596 tst_resm(TINFO, "Exiting block 3");
plars865695b2001-08-27 22:15:12 +0000597 }
robbiewb3f0d5d2003-03-25 22:57:02 +0000598 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800599 tst_exit();
Garrett Cooper4fac2902010-12-21 11:53:17 -0800600}