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   : flock02
20  *
21  *    EXECUTED BY       : anyone
22  *
23  *    TEST TITLE        : Error condition test for flock(2)
24  *
25  *    TEST CASE TOTAL   : 3
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  * 	This test verifies that
35  *	1)	flock(2) returns -1 and sets error number to EBADF
36  *		if the file descriptor is invalid.
37  *      2)	flock(2) returns -1 and sets error number to EINVAL
38  *		if the argument operation does not include LOCK_SH,LOCK_EX,LOCK_UN.$
39  *	3)	flock(2) returns -1 and sets error number to EINVAL
40  *		if an invalid combination of locking modes is used i.e LOCK_SH with LOCK_EX
41  *
42  *	Setup:
43  *        Setup signal handling.
44  *        Pause for SIGUSR1 if option specified.
45  *        Create a temporary directory and chdir to it.
46  * 	  Create a temporary file
47  *
48  *	Test:
49  *	Loop if proper options are given.
50  *		Execute system call
51  *		Check return code,
52  *			if system call failed (return == -1) and errno == expected_errno
53  *				Issue system call fails with expected return value and error number
54  *			else
55  *				Issue system call failed to produce expected error.
56  *
57  *      Cleanup:
58  *        Print errno log and/or timing stats if options given
59  *	  Deletes temporary directory.
60  *
61  * USAGE:  <for command-line>
62  *      flock02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
63  *                      where,  -c n : Run n copies concurrently.
64  *                              -f   : Turn off functional testing
65  *    				-e   : Turn on errno logging.
66  *                              -h   : Show help screen                        $
67  *				-i n : Execute test n times.
68  *                              -I x : Execute test for x seconds.
69  *                              -p   : Pause for SIGUSR1 before starting
70  *                              -P x : Pause for x seconds between iterations.
71  *                              -t   : Turn on syscall timing.
72  *
73  ****************************************************************/
74 
75 #include <sys/types.h>
76 #include <sys/file.h>
77 #include <sys/wait.h>
78 #include <fcntl.h>
79 #include <errno.h>
80 #include <stdio.h>
81 #include "test.h"
82 
83 void setup(void);
84 void cleanup(void);
85 
86 char *TCID = "flock02";
87 int TST_TOTAL = 3;
88 char filename[100];
89 int fd;
90 
main(int argc,char ** argv)91 int main(int argc, char **argv)
92 {
93 	int lc;
94 
95 	tst_parse_opts(argc, argv, NULL, NULL);
96 
97 	setup();
98 
99 	for (lc = 0; TEST_LOOPING(lc); lc++) {
100 
101 		tst_count = 0;
102 
103 		TEST(flock(-1, LOCK_SH));
104 
105 		if (TEST_RETURN == -1 && TEST_ERRNO == EBADF)
106 			tst_resm(TPASS, "flock failed as expected with EBADF");
107 		else if (TEST_RETURN == 0)
108 			tst_resm(TFAIL, "flock succeeded unexpectedly");
109 		else
110 			tst_resm(TFAIL | TTERRNO, "flock failed unexpectedly");
111 
112 		/* Test system call with invalid argument */
113 		TEST(flock(fd, LOCK_NB));
114 
115 		if (TEST_RETURN == -1 && TEST_ERRNO == EINVAL)
116 			tst_resm(TPASS, "flock failed as expected with EINVAL");
117 		else if (TEST_RETURN == 0)
118 			tst_resm(TFAIL, "flock succeeded unexpectedly");
119 		else
120 			tst_resm(TFAIL | TTERRNO, "flock failed unexpectedly");
121 
122 		/* Test system call with invalid combination of arguments */
123 		TEST(flock(fd, LOCK_SH | LOCK_EX));
124 
125 		if (TEST_RETURN == -1 && TEST_ERRNO == EINVAL) {
126 			tst_resm(TPASS, "flock failed as expected with EINVAL");
127 			continue;	/*next loop for MTKERNEL  */
128 		} else if (TEST_RETURN == 0)
129 			tst_resm(TFAIL, "flock succeeded unexpectedly");
130 		else
131 			tst_resm(TFAIL | TTERRNO, "flock failed unexpectedly");
132 
133 	}
134 
135 	close(fd);
136 
137 	cleanup();
138 
139 	tst_exit();
140 
141 }
142 
setup(void)143 void setup(void)
144 {
145 
146 	tst_sig(FORK, DEF_HANDLER, cleanup);
147 
148 	TEST_PAUSE;
149 
150 	tst_tmpdir();
151 
152 	sprintf(filename, "flock02.%d", getpid());
153 
154 	fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0666);
155 	if (fd < 0)
156 		tst_brkm(TFAIL | TERRNO, cleanup, "creat failed");
157 }
158 
cleanup(void)159 void cleanup(void)
160 {
161 	unlink(filename);
162 	tst_rmdir();
163 
164 }
165