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 : flock04 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_SH. 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 * 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. 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 = "flock04"; 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 setup(); 96 97 for (lc = 0; TEST_LOOPING(lc); lc++) { 98 99 tst_count = 0; 100 101 TEST(flock(fd, LOCK_SH)); 102 if (TEST_RETURN == 0) { 103 104 pid = FORK_OR_VFORK(); 105 if (pid == -1) 106 tst_brkm(TBROK | TERRNO, cleanup, 107 "fork failed"); 108 if (pid == 0) { 109 fd1 = open(filename, O_RDONLY); 110 retval = flock(fd1, LOCK_SH | LOCK_NB); 111 if (retval == -1) 112 tst_resm(TFAIL, 113 "flock() FAILED to acquire shared lock on existing " 114 "Share Locked file"); 115 else 116 tst_resm(TPASS, 117 "flock() PASSED in acquiring shared lock on " 118 "Share Locked file"); 119 exit(0); 120 } else if (wait(&status) == -1) 121 tst_brkm(TBROK | TERRNO, cleanup, 122 "wait failed"); 123 124 pid = FORK_OR_VFORK(); 125 if (pid == -1) 126 tst_brkm(TBROK | TERRNO, cleanup, 127 "fork failed"); 128 129 if (pid == 0) { 130 fd1 = open(filename, O_RDWR); 131 retval = flock(fd1, LOCK_EX | LOCK_NB); 132 if (retval == -1) { 133 tst_resm(TPASS, 134 "flock() failed to acquire exclusive lock on existing " 135 "share locked file as expected"); 136 } else { 137 tst_resm(TFAIL, 138 "flock() unexpectedly passed in acquiring exclusive lock on " 139 "Share Locked file"); 140 } 141 exit(0); 142 } else if (wait(&status) == -1) 143 tst_resm(TBROK | TERRNO, "wait failed"); 144 TEST(flock(fd, LOCK_UN)); 145 } else 146 tst_resm(TFAIL | TERRNO, "flock failed"); 147 148 close(fd); 149 close(fd1); 150 } 151 152 cleanup(); 153 tst_exit(); 154 } 155 156 void setup(void) 157 { 158 159 tst_sig(FORK, DEF_HANDLER, cleanup); 160 161 TEST_PAUSE; 162 163 tst_tmpdir(); 164 165 sprintf(filename, "flock04.%d", getpid()); 166 167 fd = creat(filename, 0666); 168 if (fd == -1) 169 tst_brkm(TFAIL, cleanup, "creating a new file failed"); 170 } 171 172 void cleanup(void) 173 { 174 unlink(filename); 175 176 tst_rmdir(); 177 } 178