blob: 342bb1d106010591f1d939ec8061e47c281dacfa [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
13 * with this program; if not, write the Free Software Foundation, Inc., 59
14 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
15 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiew70940422002-12-26 20:30:16 +000019 * TEST IDENTIFIER : flock04
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_SH.
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 * 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.
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"
79#include "usctest.h"
subrata_modak4bb656a2009-02-26 12:02:09 +000080
robbiew70940422002-12-26 20:30:16 +000081void setup(void);
82void cleanup(void);
83
subrata_modak56207ce2009-03-23 13:35:39 +000084int exp_enos[] = { EWOULDBLOCK, EAGAIN, 0 };
robbiew70940422002-12-26 20:30:16 +000085
subrata_modak56207ce2009-03-23 13:35:39 +000086char *TCID = "flock04"; /* Test program identifier */
87int TST_TOTAL = 2; /* Total number of test cases */
robbiew70940422002-12-26 20:30:16 +000088char filename[100];
subrata_modak56207ce2009-03-23 13:35:39 +000089int fd, fd1, status;
subrata_modakbdbaec52009-02-26 12:14:51 +000090
robbiew70940422002-12-26 20:30:16 +000091int main(int argc, char **argv)
92{
subrata_modak56207ce2009-03-23 13:35:39 +000093 int lc, retval;
subrata_modak56207ce2009-03-23 13:35:39 +000094 char *msg; /* message returned from parse_opts */
robbiew70940422002-12-26 20:30:16 +000095 pid_t pid;
96
Garrett Cooper15697992010-12-18 06:31:28 -080097 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080098 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew70940422002-12-26 20:30:16 +000099
robbiew70940422002-12-26 20:30:16 +0000100 setup();
101
robbiew70940422002-12-26 20:30:16 +0000102 for (lc = 0; TEST_LOOPING(lc); lc++) {
103
robbiew70940422002-12-26 20:30:16 +0000104 Tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106 TEST(flock(fd, LOCK_SH));
107 if (TEST_RETURN == 0) {
subrata_modakbdbaec52009-02-26 12:14:51 +0000108
robbiewd34d5812005-07-11 22:28:09 +0000109 pid = FORK_OR_VFORK();
Garrett Cooper15697992010-12-18 06:31:28 -0800110 if (pid == -1)
111 tst_brkm(TBROK|TERRNO, cleanup, "fork failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000112 if (pid == 0) {
113 fd1 = open(filename, O_RDONLY);
114 retval = flock(fd1, LOCK_SH | LOCK_NB);
Garrett Cooper15697992010-12-18 06:31:28 -0800115 if (retval == -1)
subrata_modak56207ce2009-03-23 13:35:39 +0000116 tst_resm(TFAIL,
117 "flock() FAILED to acquire shared lock on existing "
118 "Share Locked file");
Garrett Cooper15697992010-12-18 06:31:28 -0800119 else
subrata_modak56207ce2009-03-23 13:35:39 +0000120 tst_resm(TPASS,
121 "flock() PASSED in acquiring shared lock on "
122 "Share Locked file");
subrata_modak56207ce2009-03-23 13:35:39 +0000123 exit(0);
Garrett Cooper15697992010-12-18 06:31:28 -0800124 } else
125 if (wait(&status) == -1)
Garrett Cooper47427392010-12-18 21:35:21 -0800126 tst_brkm(TBROK|TERRNO, cleanup,
127 "wait failed");
subrata_modakbdbaec52009-02-26 12:14:51 +0000128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 pid = FORK_OR_VFORK();
Garrett Cooper15697992010-12-18 06:31:28 -0800130 if (pid == -1)
131 tst_brkm(TBROK|TERRNO, cleanup, "fork failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000132
133 if (pid == 0) {
134 fd1 = open(filename, O_RDWR);
Garrett Cooper15697992010-12-18 06:31:28 -0800135 retval = flock(fd1, LOCK_EX|LOCK_NB);
subrata_modak56207ce2009-03-23 13:35:39 +0000136 if (retval == -1) {
137 tst_resm(TPASS,
138 "flock() failed to acquire exclusive lock on existing "
139 "share locked file as expected");
140 } else {
141 tst_resm(TFAIL,
142 "flock() unexpectedly passed in acquiring exclusive lock on "
143 "Share Locked file");
144 }
145 exit(0);
Garrett Cooper15697992010-12-18 06:31:28 -0800146 } else
147 if (wait(&status) == -1)
Garrett Cooper47427392010-12-18 21:35:21 -0800148 tst_resm(TBROK|TERRNO, "wait failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000149 TEST(flock(fd, LOCK_UN));
Garrett Cooper15697992010-12-18 06:31:28 -0800150 } else
151 tst_resm(TFAIL|TERRNO, "flock failed");
robbiew70940422002-12-26 20:30:16 +0000152
subrata_modak56207ce2009-03-23 13:35:39 +0000153 close(fd);
154 close(fd1);
Garrett Cooper2c282152010-12-16 00:55:50 -0800155 }
robbiew70940422002-12-26 20:30:16 +0000156
157 cleanup();
Garrett Cooper15697992010-12-18 06:31:28 -0800158 tst_exit();
159}
Garrett Cooper2c282152010-12-16 00:55:50 -0800160
subrata_modak56207ce2009-03-23 13:35:39 +0000161void setup(void)
robbiew70940422002-12-26 20:30:16 +0000162{
Garrett Cooper2c282152010-12-16 00:55:50 -0800163
robbiew70940422002-12-26 20:30:16 +0000164 tst_sig(FORK, DEF_HANDLER, cleanup);
165
robbiew70940422002-12-26 20:30:16 +0000166 TEST_EXP_ENOS(exp_enos);
167
robbiew70940422002-12-26 20:30:16 +0000168 TEST_PAUSE;
169
robbiew70940422002-12-26 20:30:16 +0000170 tst_tmpdir();
171
172 sprintf(filename, "flock04.%d", getpid());
173
robbiew70940422002-12-26 20:30:16 +0000174 fd = creat(filename, 0666);
Garrett Cooper15697992010-12-18 06:31:28 -0800175 if (fd == -1)
176 tst_brkm(TFAIL, cleanup, "creating a new file failed");
robbiew70940422002-12-26 20:30:16 +0000177}
178
subrata_modak56207ce2009-03-23 13:35:39 +0000179void cleanup(void)
robbiew70940422002-12-26 20:30:16 +0000180{
robbiew70940422002-12-26 20:30:16 +0000181 TEST_CLEANUP;
182
183 unlink(filename);
184
185 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700186}