robbiew | e5bf4d0 | 2002-12-02 18:23:30 +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 |
| 13 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 14 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 15 | * |
| 16 | */ |
| 17 | /********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : flock01 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | * |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 21 | * EXECUTED BY : anyone |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 23 | * TEST TITLE : Basic test for flock(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 25 | * TEST CASE TOTAL : 3 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +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 | e5bf4d0 | 2002-12-02 18:23:30 +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 |
| 34 | * Test to verify flock(2) succeds with all kind of locks. |
| 35 | * Intends to provide a limited exposure of system call. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 36 | * $ |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +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 | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 43 | * Test: |
| 44 | * Loop if proper options are given. |
| 45 | * Execute system call |
| 46 | * Check return code, if system call failed (return == -1) |
| 47 | * Log the error number and issue a FAIL message |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 48 | * otherwise issue a PASS message |
| 49 | * |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 50 | * Cleanup: |
| 51 | * Print errno log and/or timing stats if options given |
| 52 | * Deletes temporary directory. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 53 | * |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 54 | * USAGE: <for command-line> |
| 55 | * flock01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p] |
| 56 | * where, -c n : Run n copies concurrently. |
| 57 | * -f : Turn off functional testing |
| 58 | * -e : Turn on errno logging. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | * -h : Show help screen $ |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 60 | * -i n : Execute test n times. |
| 61 | * -I x : Execute test for x seconds. |
| 62 | * -p : Pause for SIGUSR1 before starting |
| 63 | * -P x : Pause for x seconds between iterations. |
| 64 | * -t : Turn on syscall timing. |
| 65 | * |
| 66 | ****************************************************************/ |
| 67 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 68 | #include <errno.h> |
| 69 | #include <stdio.h> |
| 70 | #include <sys/wait.h> |
| 71 | #include <sys/file.h> |
| 72 | #include "test.h" |
| 73 | #include "usctest.h" |
| 74 | |
| 75 | void setup(void); |
| 76 | void cleanup(void); |
| 77 | |
| 78 | /* 0 terminated list of expected errnos */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | int exp_enos[] = { EWOULDBLOCK, EAGAIN, EINVAL, 0 }; |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | char *TCID = "flock01"; /* Test program identifier */ |
| 82 | int TST_TOTAL = 3; /* Total number of test cases */ |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 83 | extern int Tst_count; |
| 84 | char filename[100]; |
| 85 | int fd; |
| 86 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | struct test_case_t { |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 88 | int operation; |
| 89 | char *opt; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | } test_cases[] = { |
| 91 | { |
| 92 | LOCK_SH, "Shared Lock"}, { |
| 93 | LOCK_UN, "Unlock"}, { |
| 94 | LOCK_EX, "Exclusive Lock"},}; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 95 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 96 | int main(int argc, char **argv) |
| 97 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | int lc, i; |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 99 | /* loop counter */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | char *msg; /* message returned from parse_opts */ |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 101 | |
| 102 | /* parse standard options */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 103 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != |
| 104 | NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame^] | 105 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | /*NOTREACHED*/} |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 107 | |
| 108 | /* global setup */ |
| 109 | setup(); |
| 110 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 111 | /* The following loop checks looping state if -i option given */ |
| 112 | |
| 113 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 114 | |
| 115 | /* reset Tst_count in case we are looping */ |
| 116 | Tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | |
| 118 | for (i = 0; i < TST_TOTAL; ++i) { |
| 119 | |
| 120 | /* Testing system call */ |
| 121 | TEST(flock(fd, test_cases[i].operation)); |
| 122 | |
| 123 | if (TEST_RETURN == -1) { |
| 124 | TEST_ERROR_LOG(TEST_ERRNO); |
| 125 | tst_resm(TFAIL, |
| 126 | "flock() failed to get %s, error number=%d : %s", |
| 127 | test_cases[i].opt, TEST_ERRNO, |
| 128 | strerror(TEST_ERRNO)); |
| 129 | continue; /*next loop for MTKERNEL */ |
| 130 | } else { |
| 131 | tst_resm(TPASS, |
| 132 | "flock() succeded with %s, returned error number=%d", |
| 133 | test_cases[i].opt, TEST_ERRNO); |
| 134 | } |
| 135 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 136 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | |
| 138 | } /* End of TEST_LOOPING */ |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 139 | |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 140 | close(fd); |
| 141 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 142 | cleanup(); |
| 143 | |
| 144 | return 0; |
| 145 | |
| 146 | } |
| 147 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 148 | /* |
| 149 | * setup() |
| 150 | * performs all ONE TIME setup for this test |
| 151 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | void setup(void) |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 153 | { |
| 154 | /* capture signals */ |
| 155 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 156 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 157 | /* Set up the expected error numbers for -e option */ |
| 158 | TEST_EXP_ENOS(exp_enos); |
| 159 | |
| 160 | /* Pause if that option was specified |
| 161 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 162 | * You want to make sure you do this before you create your temporary |
| 163 | * directory. |
| 164 | */ |
| 165 | TEST_PAUSE; |
| 166 | |
| 167 | /* Create a unique temporary directory and chdir() to it. */ |
| 168 | tst_tmpdir(); |
| 169 | |
| 170 | sprintf(filename, "flock01.%d", getpid()); |
| 171 | |
| 172 | /* creating temporary file */ |
| 173 | fd = creat(filename, 0644); |
| 174 | if (fd < 0) { |
| 175 | tst_resm(TFAIL, "creating a new file failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 177 | TEST_CLEANUP; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 179 | /* Removing temp directory */ |
| 180 | tst_rmdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 182 | /* exit with resturn code appropriate for result */ |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 183 | tst_exit(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 184 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * cleanup() |
| 190 | * performs all ONE TIME cleanup for this test at |
| 191 | * completion or premature exit |
| 192 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 193 | void cleanup(void) |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 194 | { |
| 195 | /* |
| 196 | * print timing stats if that option was specified. |
| 197 | * print errno log if that option was specified. |
| 198 | */ |
| 199 | TEST_CLEANUP; |
| 200 | |
| 201 | unlink(filename); |
| 202 | tst_rmdir(); |
| 203 | |
| 204 | /* exit with return code appropriate for results */ |
| 205 | tst_exit(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 206 | /*NOTREACHED*/} |