robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2004 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 21 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 22 | * TEST IDENTIFIER : fcntl23 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 23 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 24 | * EXECUTED BY : anyone |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 26 | * TEST TITLE : Basic test for fcntl(2) using F_SETLEASE & F_RDLCK argument. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 28 | * TEST CASE TOTAL : 1 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 30 | * WALL CLOCK TIME : 1 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 31 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 32 | * CPU TYPES : ALL |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 33 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 34 | * AUTHOR : Robbie Williamson |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 35 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 36 | * TEST CASES |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 37 | * |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 38 | * 1.) fcntl(2) returns...(See Description) |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 39 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 40 | * INPUT SPECIFICATIONS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 41 | * The standard options for system call tests are accepted. |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 42 | * (See the parse_opts(3) man page). |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 43 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 44 | * OUTPUT SPECIFICATIONS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 46 | * DURATION |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 47 | * Terminates - with frequency and infinite modes. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 48 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 49 | * SIGNALS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 50 | * Uses SIGUSR1 to pause before test if option set. |
| 51 | * (See the parse_opts(3) man page). |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 52 | * |
| 53 | * RESOURCES |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 54 | * None |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 55 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 56 | * ENVIRONMENTAL NEEDS |
| 57 | * No run-time environmental needs. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 58 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 59 | * SPECIAL PROCEDURAL REQUIREMENTS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 60 | * None |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 61 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 62 | * INTERCASE DEPENDENCIES |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | * None |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 64 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 65 | * DETAILED DESCRIPTION |
| 66 | * This is a Phase I test for the fcntl(2) system call. It is intended |
| 67 | * to provide a limited exposure of the system call, for now. It |
| 68 | * should/will be extended when full functional tests are written for |
| 69 | * fcntl(2). |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 70 | * |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | * Setup: |
| 72 | * Setup signal handling. |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 73 | * Pause for SIGUSR1 if option specified. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 74 | * |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | * Test: |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 76 | * Loop if the proper options are given. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | * Execute system call |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 78 | * Check return code, if system call failed (return=-1) |
| 79 | * Log the errno and Issue a FAIL message. |
| 80 | * Otherwise, Issue a PASS message. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 81 | * |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | * Cleanup: |
| 83 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 84 | * |
| 85 | * |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 86 | *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ |
| 87 | |
| 88 | #include <sys/types.h> |
| 89 | #include <sys/stat.h> |
| 90 | #include <fcntl.h> |
| 91 | #include <unistd.h> |
| 92 | #include <errno.h> |
| 93 | #include <string.h> |
| 94 | #include <signal.h> |
| 95 | #include "test.h" |
| 96 | #include "usctest.h" |
| 97 | |
vapier | aa35472 | 2006-05-26 06:26:37 +0000 | [diff] [blame] | 98 | void setup(); |
| 99 | void cleanup(); |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | char *TCID = "fcntl23"; /* Test program identifier. */ |
| 102 | int TST_TOTAL = 1; /* Total number of test cases. */ |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 103 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
| 104 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | int exp_enos[] = { 0, 0 }; |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 106 | |
| 107 | char fname[255]; |
| 108 | int fd; |
| 109 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | int main(int ac, char **av) |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 111 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | int lc; /* loop counter */ |
| 113 | char *msg; /* message returned from parse_opts */ |
| 114 | |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 115 | /*************************************************************** |
| 116 | * parse standard options |
| 117 | ***************************************************************/ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 118 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame^] | 119 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 120 | |
| 121 | /*************************************************************** |
| 122 | * perform global setup for test |
| 123 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | setup(); |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 125 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | /* |
| 127 | * check if the current filesystem is nfs |
| 128 | */ |
| 129 | if (tst_is_cwd_nfs()) { |
| 130 | tst_brkm(TCONF, cleanup, |
| 131 | "Cannot do fcntl on a file located on an NFS filesystem"); |
| 132 | } |
subrata_modak | 5387ba0 | 2008-05-05 08:49:34 +0000 | [diff] [blame] | 133 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | /* set the expected errnos... */ |
| 135 | TEST_EXP_ENOS(exp_enos); |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 136 | |
| 137 | /*************************************************************** |
| 138 | * check looping state if -c option given |
| 139 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 141 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | /* reset Tst_count in case we are looping. */ |
| 143 | Tst_count = 0; |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 144 | |
robbiew | e93a324 | 2005-08-31 20:27:12 +0000 | [diff] [blame] | 145 | #ifdef F_SETLEASE |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | /* |
| 147 | * Call fcntl(2) with F_SETLEASE & F_RDLCK argument on fname |
| 148 | */ |
| 149 | TEST(fcntl(fd, F_SETLEASE, F_RDLCK)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 150 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | /* check return code */ |
| 152 | if (TEST_RETURN == -1) { |
| 153 | TEST_ERROR_LOG(TEST_ERRNO); |
| 154 | tst_resm(TFAIL, |
| 155 | "fcntl(%s, F_SETLEASE, F_RDLCK) Failed, errno=%d : %s", |
| 156 | fname, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 157 | } else { |
| 158 | |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 159 | /*************************************************************** |
| 160 | * only perform functional verification if flag set (-f not given) |
| 161 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | if (STD_FUNCTIONAL_TEST) { |
| 163 | TEST(fcntl(fd, F_GETLEASE)); |
| 164 | if (TEST_RETURN != F_RDLCK) |
| 165 | tst_resm(TFAIL, |
subrata_modak | 358c3ee | 2009-10-26 14:55:46 +0000 | [diff] [blame] | 166 | "fcntl(%s, F_GETLEASE) did not return F_RDLCK, returned %ld", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | fname, TEST_RETURN); |
| 168 | else { |
| 169 | TEST(fcntl(fd, F_SETLEASE, F_UNLCK)); |
| 170 | if (TEST_RETURN != 0) |
| 171 | tst_resm(TFAIL, |
subrata_modak | 358c3ee | 2009-10-26 14:55:46 +0000 | [diff] [blame] | 172 | "fcntl(%s, F_SETLEASE, F_UNLCK) did not return 0, returned %ld", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | fname, TEST_RETURN); |
| 174 | else |
| 175 | tst_resm(TPASS, |
| 176 | "fcntl(%s, F_SETLEASE, F_RDLCK)", |
| 177 | fname); |
| 178 | } |
| 179 | } |
| 180 | if (close(TEST_RETURN) == -1) { |
| 181 | tst_resm(TWARN, |
| 182 | "close(%s) Failed, errno=%d : %s", |
| 183 | fname, errno, strerror(errno)); |
| 184 | } |
| 185 | } |
vapier | 7546751 | 2007-09-24 05:34:19 +0000 | [diff] [blame] | 186 | #else |
| 187 | tst_resm(TINFO, "F_SETLEASE not defined, skipping test"); |
robbiew | e93a324 | 2005-08-31 20:27:12 +0000 | [diff] [blame] | 188 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | } /* End for TEST_LOOPING */ |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 190 | |
| 191 | /*************************************************************** |
| 192 | * cleanup and exit |
| 193 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | cleanup(); |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 195 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | return 0; |
| 197 | } /* End main */ |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 198 | |
| 199 | /*************************************************************** |
| 200 | * setup() - performs all ONE TIME setup for this test. |
| 201 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | void setup() |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 203 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 204 | /* capture signals */ |
| 205 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 206 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | /* Pause if that option was specified */ |
| 208 | TEST_PAUSE; |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 209 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | /* make a temp directory and cd to it */ |
| 211 | tst_tmpdir(); |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 212 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | sprintf(fname, "tfile_%d", getpid()); |
| 214 | if ((fd = open(fname, O_RDONLY | O_CREAT, 0777)) == -1) { |
| 215 | tst_brkm(TBROK, cleanup, |
| 216 | "open(%s, O_RDONLY|O_CREAT,0777) Failed, errno=%d : %s", |
| 217 | fname, errno, strerror(errno)); |
| 218 | } |
| 219 | } /* End setup() */ |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 220 | |
| 221 | /*************************************************************** |
| 222 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 223 | * completion or premature exit. |
| 224 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | void cleanup() |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 226 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 227 | /* |
| 228 | * print timing stats if that option was specified. |
| 229 | * print errno log if that option was specified. |
| 230 | */ |
| 231 | TEST_CLEANUP; |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 232 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 233 | /* close the file we've had open */ |
| 234 | if (close(fd) == -1) { |
| 235 | tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno, |
| 236 | strerror(errno)); |
| 237 | } |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 238 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | /* Remove tmp dir and all files in it */ |
| 240 | tst_rmdir(); |
robbiew | 25ae545 | 2004-03-02 22:55:42 +0000 | [diff] [blame] | 241 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 242 | /* exit with return code appropriate for results */ |
| 243 | tst_exit(); |
| 244 | } /* End cleanup() */ |