robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | /********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : flock05 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | * |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 21 | * EXECUTED BY : anyone |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
| 23 | * TEST TITLE : Testing different locks on flock(2) |
| 24 | * |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 25 | * TEST CASE TOTAL : 2 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 27 | * AUTHOR : Vatsal Avasthi <vatsal.avasthi@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 29 | * SIGNALS |
| 30 | * Uses SIGUSR1 to pause before test if option set. |
| 31 | * (See the parse_opts(3) man page). |
| 32 | * |
| 33 | * DESCRIPTION |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 34 | * Tests to verify flock(2) behavior with different locking combinations along |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 35 | * with LOCK_EX. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 36 | * $ |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 37 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 42 | * |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 43 | * 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 50 | * otherwise issue a PASS message |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 51 | * |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 52 | * Cleanup: |
| 53 | * Print errno log and/or timing stats if options given |
| 54 | * Deletes temporary directory. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 55 | * |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 56 | * 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | * -h : Show help screen $ |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 62 | * -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 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 70 | #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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 79 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 80 | void setup(void); |
| 81 | void cleanup(void); |
| 82 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 83 | char *TCID = "flock05"; |
| 84 | int TST_TOTAL = 2; |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 85 | char filename[100]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | int fd, fd1, status; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 87 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 88 | int main(int argc, char **argv) |
| 89 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | int lc, retval; |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 91 | /* loop counter */ |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 92 | const char *msg; |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 93 | pid_t pid; |
| 94 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 96 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | } |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 98 | |
| 99 | /* global setup */ |
| 100 | setup(); |
| 101 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 102 | /* The following loop checks looping state if -i option given */ |
| 103 | |
| 104 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 105 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 106 | /* reset tst_count in case we are looping */ |
| 107 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 109 | /* Testing Shared lock on Exclusive Locked file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | TEST(flock(fd, LOCK_EX)); |
| 111 | if (TEST_RETURN == 0) { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 112 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 113 | pid = FORK_OR_VFORK(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | 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); |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 130 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 131 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | /* 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); |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 151 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | TEST(flock(fd, LOCK_UN)); |
| 153 | } else { |
| 154 | tst_resm(TFAIL, |
| 155 | "flock() failed to acquire exclusive lock"); |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 158 | } |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 159 | |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 160 | close(fd); |
| 161 | close(fd1); |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 162 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 163 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 164 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | } |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * setup() |
| 169 | * performs all ONE TIME setup for this test |
| 170 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | void setup(void) |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 172 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 173 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 174 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 175 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 176 | /* 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 193 | /* Removing temp dir */ |
| 194 | tst_rmdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * cleanup() |
| 201 | * performs all ONE TIME cleanup for this test at |
| 202 | * completion or premature exit |
| 203 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 204 | void cleanup(void) |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 205 | { |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 206 | |
| 207 | unlink(filename); |
| 208 | |
| 209 | tst_rmdir(); |
| 210 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 211 | } |