blob: 2512e276dba1039236c917a4349cca1fe95879a5 [file] [log] [blame]
robbiew70940422002-12-26 20:30:16 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew70940422002-12-26 20:30:16 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiew70940422002-12-26 20:30:16 +000019 * TEST IDENTIFIER : flock05
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiew70940422002-12-26 20:30:16 +000021 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
23 * TEST TITLE : Testing different locks on flock(2)
24 *
robbiew70940422002-12-26 20:30:16 +000025 * TEST CASE TOTAL : 2
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiew70940422002-12-26 20:30:16 +000027 * AUTHOR : Vatsal Avasthi <vatsal.avasthi@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiew70940422002-12-26 20:30:16 +000029 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
33 * DESCRIPTION
subrata_modak4bb656a2009-02-26 12:02:09 +000034 * Tests to verify flock(2) behavior with different locking combinations along
robbiew70940422002-12-26 20:30:16 +000035 * with LOCK_EX.
subrata_modak56207ce2009-03-23 13:35:39 +000036 * $
robbiew70940422002-12-26 20:30:16 +000037 * 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
subrata_modak4bb656a2009-02-26 12:02:09 +000042 *
robbiew70940422002-12-26 20:30:16 +000043 * 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
subrata_modakbdbaec52009-02-26 12:14:51 +000050 * otherwise issue a PASS message
Garrett Cooper2c282152010-12-16 00:55:50 -080051 *
robbiew70940422002-12-26 20:30:16 +000052 * Cleanup:
53 * Print errno log and/or timing stats if options given
54 * Deletes temporary directory.
subrata_modak4bb656a2009-02-26 12:02:09 +000055 *
robbiew70940422002-12-26 20:30:16 +000056 * USAGE: <for command-line>
57 * flock05 [-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.
subrata_modak56207ce2009-03-23 13:35:39 +000061 * -h : Show help screen $
robbiew70940422002-12-26 20:30:16 +000062 * -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
robbiew70940422002-12-26 20:30:16 +000070#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"
subrata_modak4bb656a2009-02-26 12:02:09 +000079
robbiew70940422002-12-26 20:30:16 +000080void setup(void);
81void cleanup(void);
82
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020083char *TCID = "flock05";
84int TST_TOTAL = 2;
robbiew70940422002-12-26 20:30:16 +000085char filename[100];
subrata_modak56207ce2009-03-23 13:35:39 +000086int fd, fd1, status;
subrata_modakbdbaec52009-02-26 12:14:51 +000087
robbiew70940422002-12-26 20:30:16 +000088int main(int argc, char **argv)
89{
subrata_modak56207ce2009-03-23 13:35:39 +000090 int lc, retval;
robbiew70940422002-12-26 20:30:16 +000091 /* loop counter */
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020092 const char *msg;
robbiew70940422002-12-26 20:30:16 +000093 pid_t pid;
94
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080096 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 }
robbiew70940422002-12-26 20:30:16 +000098
99 /* global setup */
100 setup();
101
robbiew70940422002-12-26 20:30:16 +0000102 /* The following loop checks looping state if -i option given */
103
104 for (lc = 0; TEST_LOOPING(lc); lc++) {
105
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 /* reset tst_count in case we are looping */
107 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000108
subrata_modakbdbaec52009-02-26 12:14:51 +0000109 /* Testing Shared lock on Exclusive Locked file */
subrata_modak56207ce2009-03-23 13:35:39 +0000110 TEST(flock(fd, LOCK_EX));
111 if (TEST_RETURN == 0) {
subrata_modakbdbaec52009-02-26 12:14:51 +0000112
robbiewd34d5812005-07-11 22:28:09 +0000113 pid = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000114 if (pid == 0) {
115 fd1 = open(filename, O_RDWR);
116 retval = flock(fd1, LOCK_SH | LOCK_NB);
117 if (retval == -1) {
118 tst_resm(TPASS,
119 "flock() failed to acquire shared lock on an already"
120 "exclusive locked file as expected");
121 } else {
122 tst_resm(TFAIL,
123 "flock() unexpectedly PASSED in acquiring shared lock on "
124 "an already exclusive locked file");
125 }
126 exit(0);
127 } else {
128 /* parent waiting */
129 wait(&status);
robbiew70940422002-12-26 20:30:16 +0000130 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000131
subrata_modak56207ce2009-03-23 13:35:39 +0000132 /* Testing Exclusive lock on a Exclusive Locked file */
133 pid = FORK_OR_VFORK();
134
135 if (pid == 0) {
136 fd1 = open(filename, O_RDWR);
137 retval = flock(fd1, LOCK_EX | LOCK_NB);
138 if (retval == -1) {
139 tst_resm(TPASS,
140 "flock() failed to acquire exclusive lock on existing "
141 " exclusive locked file as expected");
142 } else {
143 tst_resm(TFAIL,
144 "flock() unexpectedly passed in acquiring exclusive lock on "
145 "an exclusive locked file");
146 }
147 exit(0);
148 } else {
149 /* parent waiting */
150 wait(&status);
robbiew70940422002-12-26 20:30:16 +0000151 }
subrata_modak56207ce2009-03-23 13:35:39 +0000152 TEST(flock(fd, LOCK_UN));
153 } else {
154 tst_resm(TFAIL,
155 "flock() failed to acquire exclusive lock");
robbiew70940422002-12-26 20:30:16 +0000156 }
157
Garrett Cooper2c282152010-12-16 00:55:50 -0800158 }
robbiew70940422002-12-26 20:30:16 +0000159
subrata_modakdad6e1a2007-10-30 10:46:58 +0000160 close(fd);
161 close(fd1);
robbiew70940422002-12-26 20:30:16 +0000162 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800163 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800164
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165}
robbiew70940422002-12-26 20:30:16 +0000166
167/*
168 * setup()
169 * performs all ONE TIME setup for this test
170 */
subrata_modak56207ce2009-03-23 13:35:39 +0000171void setup(void)
robbiew70940422002-12-26 20:30:16 +0000172{
Garrett Cooper2c282152010-12-16 00:55:50 -0800173
robbiew70940422002-12-26 20:30:16 +0000174 tst_sig(FORK, DEF_HANDLER, cleanup);
175
robbiew70940422002-12-26 20:30:16 +0000176 /* Pause if that option was specified
177 * TEST_PAUSE contains the code to fork the test with the -i option.
178 * You want to make sure you do this before you create your temporary
179 * directory.
180 */
181 TEST_PAUSE;
182
183 /* Create a unique temporary directory and chdir() to it. */
184 tst_tmpdir();
185
186 sprintf(filename, "flock05.%d", getpid());
187
188 /* creating temporary file */
189 fd = creat(filename, 0666);
190 if (fd == -1) {
191 tst_resm(TFAIL, "creating a new file failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000192
robbiew70940422002-12-26 20:30:16 +0000193 /* Removing temp dir */
194 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000195
robbiew70940422002-12-26 20:30:16 +0000196 }
197}
198
199/*
200 * cleanup()
201 * performs all ONE TIME cleanup for this test at
202 * completion or premature exit
203 */
subrata_modak56207ce2009-03-23 13:35:39 +0000204void cleanup(void)
robbiew70940422002-12-26 20:30:16 +0000205{
robbiew70940422002-12-26 20:30:16 +0000206
207 unlink(filename);
208
209 tst_rmdir();
210
Wanlong Gao354ebb42012-12-07 10:10:04 +0800211}