1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 /**********************************************************
18  *
19  *    TEST IDENTIFIER   : flock04
20  *
21  *    EXECUTED BY       : anyone
22  *
23  *    TEST TITLE        : Testing different locks on flock(2)
24  *
25  *    TEST CASE TOTAL   : 2
26  *
27  *    AUTHOR            : Vatsal Avasthi <vatsal.avasthi@wipro.com>
28  *
29  *    SIGNALS
30  *      Uses SIGUSR1 to pause before test if option set.
31  *      (See the parse_opts(3) man page).
32  *
33  *    DESCRIPTION
34  * 	Tests to verify flock(2) behavior with different locking combinations along
35  *	with LOCK_SH.
36  *    $
37  *	Setup:
38  *        Setup signal handling.
39  *        Pause for SIGUSR1 if option specified.
40  *        Create a temporary directory and chdir to it.
41  * 	  Create a temporary file
42  *
43  *	Test:
44  *	Loop if proper options are given.
45  *		Parent flocks(2) a file
46  *		fork() a child process
47  * 		Child tries to flock() the already flocked file with different types of locks
48  *		Check return code, if system call failed (return == -1)
49  *				Log the error number and issue a FAIL message
50  *		otherwise issue a PASS message
51  *
52  *      Cleanup:
53  *        Print errno log and/or timing stats if options given
54  *	  Deletes temporary directory.
55  *
56  * USAGE:  <for command-line>
57  *      flock04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
58  *                      where,  -c n : Run n copies concurrently.
59  *                              -f   : Turn off functional testing
60  *    				-e   : Turn on errno logging.
61  *                              -h   : Show help screen                        $
62  *				-i n : Execute test n times.
63  *                              -I x : Execute test for x seconds.
64  *                              -p   : Pause for SIGUSR1 before starting
65  *                              -P x : Pause for x seconds between iterations.
66  *                              -t   : Turn on syscall timing.
67  *
68  ****************************************************************/
69 
70 #include <errno.h>
71 #include <stdio.h>
72 #include <unistd.h>
73 #include <sys/types.h>
74 #include <sys/wait.h>
75 #include <sys/file.h>
76 #include <sys/stat.h>
77 #include <fcntl.h>
78 #include "test.h"
79 #include "safe_macros.h"
80 
81 void setup(void);
82 void cleanup(void);
83 
84 char *TCID = "flock04";
85 int TST_TOTAL = 2;
86 char filename[100];
87 int fd, fd1, status;
88 
main(int argc,char ** argv)89 int main(int argc, char **argv)
90 {
91 	int lc, retval;
92 	pid_t pid;
93 
94 	tst_parse_opts(argc, argv, NULL, NULL);
95 
96 	setup();
97 
98 	for (lc = 0; TEST_LOOPING(lc); lc++) {
99 
100 		tst_count = 0;
101 
102 		TEST(flock(fd, LOCK_SH));
103 		if (TEST_RETURN == 0) {
104 
105 			pid = FORK_OR_VFORK();
106 			if (pid == -1)
107 				tst_brkm(TBROK | TERRNO, cleanup,
108 					 "fork failed");
109 			if (pid == 0) {
110 				fd1 = open(filename, O_RDONLY);
111 				retval = flock(fd1, LOCK_SH | LOCK_NB);
112 				if (retval == -1)
113 					tst_resm(TFAIL,
114 						 "flock() FAILED to acquire shared lock on existing "
115 						 "Share Locked file");
116 				else
117 					tst_resm(TPASS,
118 						 "flock() PASSED in acquiring shared lock on "
119 						 "Share Locked file");
120 				exit(0);
121 			} else {
122 				SAFE_WAIT(cleanup, &status);
123 			}
124 
125 			pid = FORK_OR_VFORK();
126 			if (pid == -1)
127 				tst_brkm(TBROK | TERRNO, cleanup,
128 					 "fork failed");
129 
130 			if (pid == 0) {
131 				fd1 = open(filename, O_RDWR);
132 				retval = flock(fd1, LOCK_EX | LOCK_NB);
133 				if (retval == -1) {
134 					tst_resm(TPASS,
135 						 "flock() failed to acquire exclusive lock on existing "
136 						 "share locked file as expected");
137 				} else {
138 					tst_resm(TFAIL,
139 						 "flock() unexpectedly passed in acquiring exclusive lock on "
140 						 "Share Locked file");
141 				}
142 				exit(0);
143 			} else if (wait(&status) == -1)
144 				tst_resm(TBROK | TERRNO, "wait failed");
145 			TEST(flock(fd, LOCK_UN));
146 		} else
147 			tst_resm(TFAIL | TERRNO, "flock failed");
148 
149 		close(fd);
150 		close(fd1);
151 	}
152 
153 	cleanup();
154 	tst_exit();
155 }
156 
setup(void)157 void setup(void)
158 {
159 
160 	tst_sig(FORK, DEF_HANDLER, cleanup);
161 
162 	TEST_PAUSE;
163 
164 	tst_tmpdir();
165 
166 	sprintf(filename, "flock04.%d", getpid());
167 
168 	fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0666);
169 	if (fd == -1)
170 		tst_brkm(TFAIL, cleanup, "creating a new file failed");
171 }
172 
cleanup(void)173 void cleanup(void)
174 {
175 	unlink(filename);
176 
177 	tst_rmdir();
178 }
179