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 |
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 | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 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 |
Sedat Dilek | 385d792 | 2013-01-16 15:54:15 +0100 | [diff] [blame] | 34 | * Test to verify flock(2) succeeds with all kind of locks. |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 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 |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 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" |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 73 | |
| 74 | void setup(void); |
| 75 | void cleanup(void); |
| 76 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 77 | char *TCID = "flock01"; |
| 78 | int TST_TOTAL = 3; |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 79 | char filename[100]; |
| 80 | int fd; |
| 81 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | struct test_case_t { |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 83 | int operation; |
| 84 | char *opt; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | } test_cases[] = { |
| 86 | { |
| 87 | LOCK_SH, "Shared Lock"}, { |
| 88 | LOCK_UN, "Unlock"}, { |
| 89 | LOCK_EX, "Exclusive Lock"},}; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 90 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +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, i; |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 94 | /* loop counter */ |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 95 | const char *msg; |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 96 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +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); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | } |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 100 | |
| 101 | /* global setup */ |
| 102 | setup(); |
| 103 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 104 | /* The following loop checks looping state if -i option given */ |
| 105 | |
| 106 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 107 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 108 | /* reset tst_count in case we are looping */ |
| 109 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | |
| 111 | for (i = 0; i < TST_TOTAL; ++i) { |
| 112 | |
| 113 | /* Testing system call */ |
| 114 | TEST(flock(fd, test_cases[i].operation)); |
| 115 | |
| 116 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | tst_resm(TFAIL, |
| 118 | "flock() failed to get %s, error number=%d : %s", |
| 119 | test_cases[i].opt, TEST_ERRNO, |
| 120 | strerror(TEST_ERRNO)); |
| 121 | continue; /*next loop for MTKERNEL */ |
| 122 | } else { |
| 123 | tst_resm(TPASS, |
Sedat Dilek | 385d792 | 2013-01-16 15:54:15 +0100 | [diff] [blame] | 124 | "flock() succeeded with %s, returned error number=%d", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | test_cases[i].opt, TEST_ERRNO); |
| 126 | } |
| 127 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 128 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 130 | } |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 131 | |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 132 | close(fd); |
| 133 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 134 | cleanup(); |
| 135 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 136 | tst_exit(); |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 137 | |
| 138 | } |
| 139 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 140 | /* |
| 141 | * setup() |
| 142 | * performs all ONE TIME setup for this test |
| 143 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | void setup(void) |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 145 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 147 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 148 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 149 | /* Pause if that option was specified |
| 150 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 151 | * You want to make sure you do this before you create your temporary |
| 152 | * directory. |
| 153 | */ |
| 154 | TEST_PAUSE; |
| 155 | |
| 156 | /* Create a unique temporary directory and chdir() to it. */ |
| 157 | tst_tmpdir(); |
| 158 | |
| 159 | sprintf(filename, "flock01.%d", getpid()); |
| 160 | |
| 161 | /* creating temporary file */ |
| 162 | fd = creat(filename, 0644); |
| 163 | if (fd < 0) { |
| 164 | tst_resm(TFAIL, "creating a new file failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 166 | /* Removing temp directory */ |
| 167 | tst_rmdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 169 | /* exit with resturn code appropriate for result */ |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 170 | tst_exit(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * cleanup() |
| 177 | * performs all ONE TIME cleanup for this test at |
| 178 | * completion or premature exit |
| 179 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | void cleanup(void) |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 181 | { |
robbiew | e5bf4d0 | 2002-12-02 18:23:30 +0000 | [diff] [blame] | 182 | |
| 183 | unlink(filename); |
| 184 | tst_rmdir(); |
| 185 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 186 | } |