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 : flock04 |
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_SH. |
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 | * 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_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" |
| 79 | #include "usctest.h" |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 80 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 81 | void setup(void); |
| 82 | void cleanup(void); |
| 83 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | int exp_enos[] = { EWOULDBLOCK, EAGAIN, 0 }; |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 85 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | char *TCID = "flock04"; /* Test program identifier */ |
| 87 | int TST_TOTAL = 2; /* Total number of test cases */ |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 88 | char filename[100]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | int fd, fd1, status; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 90 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 91 | int main(int argc, char **argv) |
| 92 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | int lc, retval; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 94 | char *msg; |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 95 | pid_t pid; |
| 96 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 97 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 98 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 99 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 100 | setup(); |
| 101 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 102 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 103 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame^] | 104 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | TEST(flock(fd, LOCK_SH)); |
| 107 | if (TEST_RETURN == 0) { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 108 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 109 | pid = FORK_OR_VFORK(); |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 110 | if (pid == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 111 | tst_brkm(TBROK | TERRNO, cleanup, |
| 112 | "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | if (pid == 0) { |
| 114 | fd1 = open(filename, O_RDONLY); |
| 115 | retval = flock(fd1, LOCK_SH | LOCK_NB); |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 116 | if (retval == -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | tst_resm(TFAIL, |
| 118 | "flock() FAILED to acquire shared lock on existing " |
| 119 | "Share Locked file"); |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 120 | else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | tst_resm(TPASS, |
| 122 | "flock() PASSED in acquiring shared lock on " |
| 123 | "Share Locked file"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | exit(0); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | } else if (wait(&status) == -1) |
| 126 | tst_brkm(TBROK | TERRNO, cleanup, |
| 127 | "wait failed"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 128 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | pid = FORK_OR_VFORK(); |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 130 | if (pid == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 131 | tst_brkm(TBROK | TERRNO, cleanup, |
| 132 | "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | |
| 134 | if (pid == 0) { |
| 135 | fd1 = open(filename, O_RDWR); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | retval = flock(fd1, LOCK_EX | LOCK_NB); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | if (retval == -1) { |
| 138 | tst_resm(TPASS, |
| 139 | "flock() failed to acquire exclusive lock on existing " |
| 140 | "share locked file as expected"); |
| 141 | } else { |
| 142 | tst_resm(TFAIL, |
| 143 | "flock() unexpectedly passed in acquiring exclusive lock on " |
| 144 | "Share Locked file"); |
| 145 | } |
| 146 | exit(0); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 147 | } else if (wait(&status) == -1) |
| 148 | tst_resm(TBROK | TERRNO, "wait failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | TEST(flock(fd, LOCK_UN)); |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 150 | } else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | tst_resm(TFAIL | TERRNO, "flock failed"); |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 152 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | close(fd); |
| 154 | close(fd1); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 155 | } |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 156 | |
| 157 | cleanup(); |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 158 | tst_exit(); |
| 159 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 160 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 161 | void setup(void) |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 162 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 163 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 164 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 165 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 166 | TEST_EXP_ENOS(exp_enos); |
| 167 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 168 | TEST_PAUSE; |
| 169 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 170 | tst_tmpdir(); |
| 171 | |
| 172 | sprintf(filename, "flock04.%d", getpid()); |
| 173 | |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 174 | fd = creat(filename, 0666); |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 175 | if (fd == -1) |
| 176 | tst_brkm(TFAIL, cleanup, "creating a new file failed"); |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 177 | } |
| 178 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | void cleanup(void) |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 180 | { |
robbiew | 7094042 | 2002-12-26 20:30:16 +0000 | [diff] [blame] | 181 | TEST_CLEANUP; |
| 182 | |
| 183 | unlink(filename); |
| 184 | |
| 185 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 186 | } |