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 13 * with this program; if not, write the Free Software Foundation, Inc., 14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 15 * 16 */ 17 /********************************************************** 18 * 19 * TEST IDENTIFIER : flock05 20 * 21 * EXECUTED BY : anyone 22 * 23 * TEST TITLE : Testing different locks on flock(2) 24 * 25 * TEST CASE TOTAL : 2 26 * 27 * AUTHOR : Vatsal Avasthi <vatsal.avasthi (at) wipro.com> 28 * 29 * SIGNALS 30 * Uses SIGUSR1 to pause before test if option set. 31 * (See the parse_opts(3) man page). 32 * 33 * DESCRIPTION 34 * Tests to verify flock(2) behavior with different locking combinations along 35 * with LOCK_EX. 36 * $ 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 42 * 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 50 * otherwise issue a PASS message 51 * 52 * Cleanup: 53 * Print errno log and/or timing stats if options given 54 * Deletes temporary directory. 55 * 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. 61 * -h : Show help screen $ 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 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 80 void setup(void); 81 void cleanup(void); 82 83 char *TCID = "flock05"; 84 int TST_TOTAL = 2; 85 char filename[100]; 86 int fd, fd1, status; 87 88 int main(int argc, char **argv) 89 { 90 int lc, retval; 91 pid_t pid; 92 93 tst_parse_opts(argc, argv, NULL, NULL); 94 95 /* global setup */ 96 setup(); 97 98 /* The following loop checks looping state if -i option given */ 99 100 for (lc = 0; TEST_LOOPING(lc); lc++) { 101 102 /* reset tst_count in case we are looping */ 103 tst_count = 0; 104 105 /* Testing Shared lock on Exclusive Locked file */ 106 TEST(flock(fd, LOCK_EX)); 107 if (TEST_RETURN == 0) { 108 109 pid = FORK_OR_VFORK(); 110 if (pid == 0) { 111 fd1 = open(filename, O_RDWR); 112 retval = flock(fd1, LOCK_SH | LOCK_NB); 113 if (retval == -1) { 114 tst_resm(TPASS, 115 "flock() failed to acquire shared lock on an already" 116 "exclusive locked file as expected"); 117 } else { 118 tst_resm(TFAIL, 119 "flock() unexpectedly PASSED in acquiring shared lock on " 120 "an already exclusive locked file"); 121 } 122 exit(0); 123 } else { 124 /* parent waiting */ 125 wait(&status); 126 } 127 128 /* Testing Exclusive lock on a Exclusive Locked file */ 129 pid = FORK_OR_VFORK(); 130 131 if (pid == 0) { 132 fd1 = open(filename, O_RDWR); 133 retval = flock(fd1, LOCK_EX | LOCK_NB); 134 if (retval == -1) { 135 tst_resm(TPASS, 136 "flock() failed to acquire exclusive lock on existing " 137 " exclusive locked file as expected"); 138 } else { 139 tst_resm(TFAIL, 140 "flock() unexpectedly passed in acquiring exclusive lock on " 141 "an exclusive locked file"); 142 } 143 exit(0); 144 } else { 145 /* parent waiting */ 146 wait(&status); 147 } 148 TEST(flock(fd, LOCK_UN)); 149 } else { 150 tst_resm(TFAIL, 151 "flock() failed to acquire exclusive lock"); 152 } 153 154 } 155 156 close(fd); 157 close(fd1); 158 cleanup(); 159 tst_exit(); 160 161 } 162 163 /* 164 * setup() 165 * performs all ONE TIME setup for this test 166 */ 167 void setup(void) 168 { 169 170 tst_sig(FORK, DEF_HANDLER, cleanup); 171 172 /* Pause if that option was specified 173 * TEST_PAUSE contains the code to fork the test with the -i option. 174 * You want to make sure you do this before you create your temporary 175 * directory. 176 */ 177 TEST_PAUSE; 178 179 /* Create a unique temporary directory and chdir() to it. */ 180 tst_tmpdir(); 181 182 sprintf(filename, "flock05.%d", getpid()); 183 184 /* creating temporary file */ 185 fd = creat(filename, 0666); 186 if (fd == -1) { 187 tst_resm(TFAIL, "creating a new file failed"); 188 189 /* Removing temp dir */ 190 tst_rmdir(); 191 192 } 193 } 194 195 /* 196 * cleanup() 197 * performs all ONE TIME cleanup for this test at 198 * completion or premature exit 199 */ 200 void cleanup(void) 201 { 202 203 unlink(filename); 204 205 tst_rmdir(); 206 207 } 208