robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 21 | * |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 22 | * TEST IDENTIFIER : flock06 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 23 | * |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 24 | * EXECUTED BY : anyone |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 26 | * TEST TITLE : Error condition test for flock(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 28 | * TEST CASE TOTAL : 1 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 30 | * AUTHOR : Matthew Wilcox <willy@debian.org> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 31 | * |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 32 | * 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 39 | * |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 40 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 47 | * |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 48 | * 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 | |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 62 | #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" |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 70 | |
| 71 | void setup(void); |
| 72 | void cleanup(void); |
| 73 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 74 | char *TCID = "flock06"; |
| 75 | int TST_TOTAL = 3; |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 76 | char filename[100]; |
| 77 | |
| 78 | int main(int argc, char **argv) |
| 79 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 80 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 81 | const char *msg; |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 82 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 84 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | } |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 86 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | setup(); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | /* The following loop checks looping state if -i option given */ |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 90 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 92 | int fd1, fd2; |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 93 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 94 | /* reset tst_count in case we are looping */ |
| 95 | tst_count = 0; |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | fd1 = open(filename, O_RDWR); |
| 98 | if (fd1 == -1) |
| 99 | tst_brkm(TFAIL, cleanup, "failed to open the" |
| 100 | "file, errno %d", errno); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 101 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | 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"); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | fd2 = open(filename, O_RDWR); |
| 110 | if (fd2 == -1) |
| 111 | tst_brkm(TFAIL, cleanup, "failed to open the" |
| 112 | "file, errno %d", errno); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | 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!"); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 119 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | 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"); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 126 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | 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); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 134 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 135 | } |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 136 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 138 | tst_exit(); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 139 | |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 140 | } |
| 141 | |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 142 | /* |
| 143 | * setup() |
| 144 | * performs all ONE TIME setup for this test |
| 145 | */ |
| 146 | void setup(void) |
| 147 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | int fd; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 149 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | tst_sig(FORK, DEF_HANDLER, cleanup); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 151 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | /* 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; |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 158 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | /* Create a unique temporary directory and chdir() to it. */ |
| 160 | tst_tmpdir(); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 161 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | sprintf(filename, "flock06.%d", getpid()); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 163 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | /* creating temporary file */ |
| 165 | fd = creat(filename, 0666); |
| 166 | if (fd < 0) { |
| 167 | tst_resm(TFAIL, "creating a new file failed"); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 168 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | /* Removing temp dir */ |
| 170 | tst_rmdir(); |
| 171 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | tst_exit(); |
| 173 | } |
| 174 | close(fd); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * cleanup() |
| 179 | * performs all ONE TIME cleanup for this test at |
| 180 | * completion or premature exit |
| 181 | */ |
| 182 | void cleanup(void) |
| 183 | { |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 184 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | unlink(filename); |
| 186 | tst_rmdir(); |
robbiew | 39714a2 | 2003-03-03 16:19:20 +0000 | [diff] [blame] | 187 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | } |