blob: bdbb6d7148307cfb8be9ab7aa7571e169aa3122d [file] [log] [blame]
robbiew39714a22003-03-03 16:19:20 +00001/*
2 *
3 * Copyright (c) Matthew Wilcox for Hewlett Packard 2003
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
robbiew39714a22003-03-03 16:19:20 +000018 */
19
20/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000021 *
robbiew39714a22003-03-03 16:19:20 +000022 * TEST IDENTIFIER : flock06
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
robbiew39714a22003-03-03 16:19:20 +000024 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
robbiew39714a22003-03-03 16:19:20 +000026 * TEST TITLE : Error condition test for flock(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000027 *
robbiew39714a22003-03-03 16:19:20 +000028 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000029 *
robbiew39714a22003-03-03 16:19:20 +000030 * AUTHOR : Matthew Wilcox <willy@debian.org>
subrata_modak4bb656a2009-02-26 12:02:09 +000031 *
robbiew39714a22003-03-03 16:19:20 +000032 * SIGNALS
33 * Uses SIGUSR1 to pause before test if option set.
34 * (See the parse_opts(3) man page).
35 *
36 * DESCRIPTION
37 * This test verifies that flock locks held on one fd conflict with
38 * flock locks held on a different fd.
Garrett Cooper2c282152010-12-16 00:55:50 -080039 *
robbiew39714a22003-03-03 16:19:20 +000040 * Test:
41 * The process opens two file descriptors on the same file.
42 * It acquires an exclusive flock on the first descriptor,
43 * checks that attempting to acquire an flock on the second
44 * descriptor fails. Then it removes the first descriptor's
45 * lock and attempts to acquire an exclusive lock on the
46 * second descriptor.
subrata_modak4bb656a2009-02-26 12:02:09 +000047 *
robbiew39714a22003-03-03 16:19:20 +000048 * USAGE: <for command-line>
49 * flock06 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
50 * where, -c n : Run n copies concurrently
51 * -f : Turn off functional testing
52 * -e : Turn on errno logging
53 * -h : Show help screen
54 * -i n : Execute test n times
55 * -I x : Execute test for x seconds
56 * -p : Pause for SIGUSR1 before starting
57 * -P x : Pause for x seconds between iterations
58 * -t : Turn on syscall timing
59 *
60 ****************************************************************/
61
robbiew39714a22003-03-03 16:19:20 +000062#include <stdio.h>
63#include <errno.h>
64#include <fcntl.h>
65#include <sys/types.h>
66#include <sys/stat.h>
67#include <sys/file.h>
68#include <sys/wait.h>
69#include "test.h"
robbiew39714a22003-03-03 16:19:20 +000070
71void setup(void);
72void cleanup(void);
73
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020074char *TCID = "flock06";
75int TST_TOTAL = 3;
robbiew39714a22003-03-03 16:19:20 +000076char filename[100];
77
78int main(int argc, char **argv)
79{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020080 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020081 const char *msg;
robbiew39714a22003-03-03 16:19:20 +000082
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080084 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 }
robbiew39714a22003-03-03 16:19:20 +000086
subrata_modak56207ce2009-03-23 13:35:39 +000087 setup();
robbiew39714a22003-03-03 16:19:20 +000088
subrata_modak56207ce2009-03-23 13:35:39 +000089 /* The following loop checks looping state if -i option given */
robbiew39714a22003-03-03 16:19:20 +000090
subrata_modak56207ce2009-03-23 13:35:39 +000091 for (lc = 0; TEST_LOOPING(lc); lc++) {
92 int fd1, fd2;
robbiew39714a22003-03-03 16:19:20 +000093
Caspar Zhangd59a6592013-03-07 14:59:12 +080094 /* reset tst_count in case we are looping */
95 tst_count = 0;
robbiew39714a22003-03-03 16:19:20 +000096
subrata_modak56207ce2009-03-23 13:35:39 +000097 fd1 = open(filename, O_RDWR);
98 if (fd1 == -1)
99 tst_brkm(TFAIL, cleanup, "failed to open the"
100 "file, errno %d", errno);
robbiew39714a22003-03-03 16:19:20 +0000101
subrata_modak56207ce2009-03-23 13:35:39 +0000102 TEST(flock(fd1, LOCK_EX | LOCK_NB));
103 if (TEST_RETURN != 0)
104 tst_resm(TFAIL, "First attempt to flock() failed, "
105 "errno %d", TEST_ERRNO);
106 else
107 tst_resm(TPASS, "First attempt to flock() passed");
robbiew39714a22003-03-03 16:19:20 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 fd2 = open(filename, O_RDWR);
110 if (fd2 == -1)
111 tst_brkm(TFAIL, cleanup, "failed to open the"
112 "file, errno %d", errno);
robbiew39714a22003-03-03 16:19:20 +0000113
subrata_modak56207ce2009-03-23 13:35:39 +0000114 TEST(flock(fd2, LOCK_EX | LOCK_NB));
115 if (TEST_RETURN == -1)
116 tst_resm(TPASS, "Second attempt to flock() denied");
117 else
118 tst_resm(TFAIL, "Second attempt to flock() succeeded!");
robbiew39714a22003-03-03 16:19:20 +0000119
subrata_modak56207ce2009-03-23 13:35:39 +0000120 TEST(flock(fd1, LOCK_UN));
121 if (TEST_RETURN == -1)
122 tst_resm(TFAIL, "Failed to unlock fd1, errno %d",
123 TEST_ERRNO);
124 else
125 tst_resm(TPASS, "Unlocked fd1");
robbiew39714a22003-03-03 16:19:20 +0000126
subrata_modak56207ce2009-03-23 13:35:39 +0000127 TEST(flock(fd2, LOCK_EX | LOCK_NB));
128 if (TEST_RETURN == -1)
129 tst_resm(TFAIL, "Third attempt to flock() denied!");
130 else
131 tst_resm(TPASS, "Third attempt to flock() succeeded");
132 close(fd1);
133 close(fd2);
robbiew39714a22003-03-03 16:19:20 +0000134
Garrett Cooper2c282152010-12-16 00:55:50 -0800135 }
robbiew39714a22003-03-03 16:19:20 +0000136
subrata_modak56207ce2009-03-23 13:35:39 +0000137 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800138 tst_exit();
robbiew39714a22003-03-03 16:19:20 +0000139
robbiew39714a22003-03-03 16:19:20 +0000140}
141
robbiew39714a22003-03-03 16:19:20 +0000142/*
143 * setup()
144 * performs all ONE TIME setup for this test
145 */
146void setup(void)
147{
subrata_modak56207ce2009-03-23 13:35:39 +0000148 int fd;
Garrett Cooper2c282152010-12-16 00:55:50 -0800149
subrata_modak56207ce2009-03-23 13:35:39 +0000150 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiew39714a22003-03-03 16:19:20 +0000151
subrata_modak56207ce2009-03-23 13:35:39 +0000152 /* Pause if that option was specified
153 * TEST_PAUSE contains the code to fork the test with the -i option.
154 * You want to make sure you do this before you create your temporary
155 * directory.
156 */
157 TEST_PAUSE;
robbiew39714a22003-03-03 16:19:20 +0000158
subrata_modak56207ce2009-03-23 13:35:39 +0000159 /* Create a unique temporary directory and chdir() to it. */
160 tst_tmpdir();
robbiew39714a22003-03-03 16:19:20 +0000161
subrata_modak56207ce2009-03-23 13:35:39 +0000162 sprintf(filename, "flock06.%d", getpid());
robbiew39714a22003-03-03 16:19:20 +0000163
subrata_modak56207ce2009-03-23 13:35:39 +0000164 /* creating temporary file */
165 fd = creat(filename, 0666);
166 if (fd < 0) {
167 tst_resm(TFAIL, "creating a new file failed");
robbiew39714a22003-03-03 16:19:20 +0000168
subrata_modak56207ce2009-03-23 13:35:39 +0000169 /* Removing temp dir */
170 tst_rmdir();
171
subrata_modak56207ce2009-03-23 13:35:39 +0000172 tst_exit();
173 }
174 close(fd);
robbiew39714a22003-03-03 16:19:20 +0000175}
176
177/*
178 * cleanup()
179 * performs all ONE TIME cleanup for this test at
180 * completion or premature exit
181 */
182void cleanup(void)
183{
robbiew39714a22003-03-03 16:19:20 +0000184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 unlink(filename);
186 tst_rmdir();
robbiew39714a22003-03-03 16:19:20 +0000187
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188}