plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 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 | /* |
| 21 | * NAME |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 22 | * fcntl21.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check locking of regions of a file |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Test changing lock sections around a read lock |
| 29 | * |
| 30 | * USAGE |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 31 | * fcntl21 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * |
| 33 | * HISTORY |
| 34 | * 07/2001 Ported by Wayne Boyer |
| 35 | * |
| 36 | * RESTRICTIONS |
| 37 | * None |
| 38 | */ |
| 39 | |
| 40 | #include <fcntl.h> |
| 41 | #include <errno.h> |
| 42 | #include <signal.h> |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | #include <sys/stat.h> |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 45 | #include <sys/wait.h> |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 46 | #include "test.h" |
| 47 | #include "usctest.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
| 49 | #define STRINGSIZE 27 |
| 50 | #define STRING "abcdefghijklmnopqrstuvwxyz\n" |
| 51 | #define STOP 0xFFF0 |
| 52 | |
| 53 | int parent_pipe[2]; |
| 54 | int child_pipe[2]; |
| 55 | int fd; |
plars | a1e518d | 2002-09-09 18:23:52 +0000 | [diff] [blame] | 56 | pid_t parent_pid, child_pid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
| 58 | void parent_put(); |
| 59 | void parent_get(); |
| 60 | void child_put(); |
| 61 | void child_get(); |
| 62 | void stop_child(); |
plars | a1e518d | 2002-09-09 18:23:52 +0000 | [diff] [blame] | 63 | void compare_lock(struct flock *, short, short, int, int, pid_t); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | void unlock_file(); |
| 65 | void do_test(struct flock *, short, short, int, int); |
| 66 | void catch_child(); |
| 67 | char *str_type(); |
| 68 | int do_lock(int, short, short, int, int); |
| 69 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 70 | char *TCID = "fcntl21"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | int TST_TOTAL = 1; |
| 72 | extern int Tst_count; |
| 73 | |
| 74 | void setup(void); |
| 75 | void cleanup(void); |
| 76 | int fail; |
| 77 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * setup |
| 81 | * performs all ONE TIME setup for this test |
| 82 | */ |
| 83 | void |
| 84 | setup() |
| 85 | { |
| 86 | char *buf = STRING; |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 87 | char template[PATH_MAX]; |
robbiew | 1e4cf0c | 2005-02-24 17:03:42 +0000 | [diff] [blame^] | 88 | struct sigaction act; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
| 90 | /* capture signals */ |
| 91 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 92 | |
| 93 | umask(0); |
| 94 | |
| 95 | /* Pause if that option was specified */ |
| 96 | TEST_PAUSE; |
| 97 | |
| 98 | pipe(parent_pipe); |
| 99 | pipe(child_pipe); |
| 100 | parent_pid = getpid(); |
robbiew | 54f7a71 | 2003-04-16 19:20:30 +0000 | [diff] [blame] | 101 | |
| 102 | tst_tmpdir(); |
| 103 | |
| 104 | snprintf(template, PATH_MAX, "fcntl21XXXXXX"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 106 | if ((fd = mkstemp(template)) < 0) { |
| 107 | tst_resm(TFAIL, "Couldn't open temp file! errno = %d", errno); |
| 108 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 110 | if (write(fd, buf, STRINGSIZE) < 0) { |
| 111 | tst_resm(TFAIL, "Couldn't write to temp file! errno = %d", errno); |
| 112 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
robbiew | 1e4cf0c | 2005-02-24 17:03:42 +0000 | [diff] [blame^] | 114 | memset(&act, 0, sizeof(act)); |
| 115 | act.sa_handler = catch_child; |
| 116 | sigemptyset(&act.sa_mask); |
| 117 | sigaddset(&act.sa_mask, SIGCLD); |
| 118 | if ((sigaction(SIGCLD, &act, NULL)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | tst_resm(TFAIL, "SIGCLD signal setup failed, errno: %d", |
| 120 | errno); |
| 121 | fail = 1; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * cleanup() |
| 127 | * performs all ONE TIME cleanup for this test at completion or |
| 128 | * premature exit |
| 129 | */ |
| 130 | void |
| 131 | cleanup() |
| 132 | { |
| 133 | /* |
| 134 | * print timing stats if that option was specified |
| 135 | * print errno log if that option was specified |
| 136 | */ |
| 137 | TEST_CLEANUP; |
| 138 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
robbiew | 54f7a71 | 2003-04-16 19:20:30 +0000 | [diff] [blame] | 140 | tst_rmdir(); |
| 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | /* exit with return code appropriate for results */ |
| 143 | tst_exit(); |
| 144 | } |
| 145 | |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 146 | void do_child() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | { |
| 148 | struct flock fl; |
| 149 | |
| 150 | close(parent_pipe[1]); |
| 151 | close(child_pipe[0]); |
| 152 | while(1) { |
| 153 | child_get(&fl); |
| 154 | if (fcntl(fd, F_GETLK, &fl) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 155 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 156 | errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | fail = 1; |
| 158 | } |
| 159 | child_put(&fl); |
| 160 | } |
| 161 | } |
| 162 | |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 163 | int do_lock(int cmd, short type, short whence, int start, int len) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | { |
| 165 | struct flock fl; |
| 166 | |
| 167 | fl.l_type = type; |
| 168 | fl.l_whence = whence; |
| 169 | fl.l_start = start; |
| 170 | fl.l_len = len; |
| 171 | return(fcntl(fd, cmd, &fl)); |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | do_test(struct flock *fl, short type, short whence, int start, int len) |
| 176 | { |
| 177 | fl->l_type = type; |
| 178 | fl->l_whence = whence; |
| 179 | fl->l_start = start; |
| 180 | fl->l_len = len; |
| 181 | fl->l_pid = (short)0; |
| 182 | |
| 183 | parent_put(fl); |
| 184 | parent_get(fl); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | compare_lock(struct flock *fl, short type, short whence, int start, int len, |
plars | a1e518d | 2002-09-09 18:23:52 +0000 | [diff] [blame] | 189 | pid_t pid) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | { |
| 191 | if (fl->l_type != type) { |
| 192 | tst_resm(TFAIL, "lock type is wrong should be %s is %s", |
| 193 | str_type(type), str_type(fl->l_type)); |
| 194 | fail = 1; |
| 195 | } |
| 196 | |
| 197 | if (fl->l_whence != whence) { |
| 198 | tst_resm(TFAIL, "lock whence is wrong should be %d is %d", |
| 199 | whence, fl->l_whence); |
| 200 | fail = 1; |
| 201 | } |
| 202 | |
| 203 | if (fl->l_start != start) { |
| 204 | tst_resm(TFAIL, "region starts in wrong place, should be" |
| 205 | "%d is %d", start, fl->l_start); |
| 206 | fail = 1; |
| 207 | } |
| 208 | |
| 209 | if (fl->l_len != len) { |
| 210 | tst_resm(TFAIL, "region length is wrong, should be %d is %d", |
| 211 | len, fl->l_len); |
| 212 | fail = 1; |
| 213 | } |
| 214 | |
| 215 | if (fl->l_pid != pid) { |
| 216 | tst_resm(TFAIL, "locking pid is wrong, should be %d is %d", |
| 217 | pid, fl->l_pid); |
| 218 | fail = 1; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | unlock_file() |
| 224 | { |
| 225 | struct flock fl; |
| 226 | |
| 227 | if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 228 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 229 | errno); |
| 230 | fail = 1; |
| 231 | } |
| 232 | do_test(&fl, F_WRLCK, 0, 0, 0); |
plars | a1e518d | 2002-09-09 18:23:52 +0000 | [diff] [blame] | 233 | compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t)0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | char * |
| 237 | str_type(int type) |
| 238 | { |
| 239 | static char buf[20]; |
| 240 | |
| 241 | switch (type) { |
| 242 | case 1: |
| 243 | return("F_RDLCK"); |
| 244 | case 2: |
| 245 | return("F_WRLCK"); |
| 246 | case 3: |
| 247 | return("F_UNLCK"); |
| 248 | default: |
| 249 | sprintf(buf, "BAD VALUE: %d", type); |
| 250 | return(buf); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void |
| 255 | parent_put(struct flock *l) |
| 256 | { |
| 257 | if (write(parent_pipe[1], l, sizeof(*l)) != sizeof(*l)) { |
| 258 | tst_resm(TFAIL, "couldn't send message to child"); |
| 259 | fail = 1; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void |
| 264 | parent_get(struct flock *l) |
| 265 | { |
| 266 | if (read(child_pipe[0], l, sizeof(*l)) != sizeof(*l)) { |
| 267 | tst_resm(TFAIL, "couldn't get message from child"); |
| 268 | fail = 1; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void |
| 273 | child_put(struct flock *l) |
| 274 | { |
| 275 | if (write(child_pipe[1], l, sizeof(*l)) != sizeof(*l)) { |
| 276 | tst_resm(TFAIL, "couldn't send message to parent"); |
| 277 | fail = 1; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | child_get(struct flock *l) |
| 283 | { |
| 284 | if (read(parent_pipe[0], l, sizeof(*l)) != sizeof(*l)) { |
| 285 | tst_resm(TFAIL, "couldn't get message from parent"); |
| 286 | cleanup(); |
| 287 | } else if (l->l_type == (short)STOP) { |
| 288 | exit(0); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void |
| 293 | stop_child() |
| 294 | { |
| 295 | struct flock fl; |
| 296 | |
| 297 | (void) signal(SIGCLD, (void (*)())SIG_DFL); |
| 298 | fl.l_type = STOP; |
| 299 | parent_put(&fl); |
| 300 | wait(0); |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | catch_child() |
| 305 | { |
| 306 | tst_resm(TFAIL, "Unexpected death of child process"); |
| 307 | cleanup(); |
| 308 | } |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 309 | |
| 310 | int main(int ac, char **av) |
| 311 | { |
| 312 | struct flock tl; |
| 313 | |
| 314 | int lc; /* loop counter */ |
| 315 | char *msg; /* message returned from parse_opts */ |
| 316 | |
| 317 | /* parse standard options */ |
| 318 | if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ |
| 319 | tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); |
| 320 | } |
| 321 | |
| 322 | setup(); /* global setup */ |
| 323 | |
| 324 | /* Check for looping state if -i option is given */ |
| 325 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 326 | /* reset Tst_count in case we are looping */ |
| 327 | Tst_count = 0; |
| 328 | |
| 329 | if ((child_pid = fork()) == 0) { |
| 330 | do_child(); |
| 331 | } |
| 332 | if (child_pid < 0) { |
| 333 | tst_resm(TFAIL, "Fork failed"); |
| 334 | cleanup(); |
| 335 | } |
| 336 | |
| 337 | (void)close(parent_pipe[0]); |
| 338 | (void)close(child_pipe[1]); |
| 339 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 340 | /* //block1: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 341 | tst_resm(TINFO, "Enter block 1"); |
| 342 | fail = 0; |
| 343 | /* |
| 344 | * Set a read lock on the whole file |
| 345 | */ |
| 346 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 0, 0) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 347 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 348 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 349 | fail = 1; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Test to make sure it's there. |
| 354 | */ |
| 355 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 356 | compare_lock(&tl, (short)F_RDLCK, (short)0, 0, 0, parent_pid); |
| 357 | |
| 358 | /* |
| 359 | * remove the lock set above |
| 360 | */ |
| 361 | unlock_file(); |
| 362 | |
| 363 | if (fail) { |
| 364 | tst_resm(TINFO, "Test block 1: FAILED"); |
| 365 | } else { |
| 366 | tst_resm(TINFO, "Test block 1: PASSED"); |
| 367 | } |
| 368 | tst_resm(TINFO, "Exit block 1"); |
| 369 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 370 | /* //block2: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 371 | tst_resm(TINFO, "Enter block 2"); |
| 372 | fail = 0; |
| 373 | |
| 374 | /* |
| 375 | * Set a write lock on the whole file |
| 376 | */ |
| 377 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 0, 0) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 378 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 379 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 380 | fail = 1; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Test to make sure its there |
| 385 | */ |
| 386 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 387 | compare_lock(&tl, (short)F_WRLCK, (short)0, 0, 0, parent_pid); |
| 388 | |
| 389 | /* |
| 390 | * remove the lock set above |
| 391 | */ |
| 392 | unlock_file(); |
| 393 | |
| 394 | if (fail) { |
| 395 | tst_resm(TINFO, "Test block 2: FAILED"); |
| 396 | } else { |
| 397 | tst_resm(TINFO, "Test block 2: PASSED"); |
| 398 | } |
| 399 | |
| 400 | tst_resm(TINFO, "Exit block 2"); |
| 401 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 402 | /* //block3: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 403 | tst_resm(TINFO, "Enter block 3"); |
| 404 | fail = 0; |
| 405 | |
| 406 | /* |
| 407 | * Add a read lock to the middle of the file and a write |
| 408 | * at the begining |
| 409 | */ |
| 410 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 411 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 412 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 413 | fail = 1; |
| 414 | } |
| 415 | |
| 416 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 1, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 417 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 418 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 419 | fail = 1; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Test write lock |
| 424 | */ |
| 425 | do_test(&tl, F_WRLCK, 0, 0, 0); |
| 426 | compare_lock(&tl, (short)F_WRLCK, (short)0, 1, 5, parent_pid); |
| 427 | |
| 428 | /* |
| 429 | * Test read lock |
| 430 | */ |
| 431 | do_test(&tl, F_WRLCK, 0, 6, 0); |
| 432 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid); |
| 433 | |
| 434 | /* |
| 435 | * Test that the rest of the file is unlocked |
| 436 | */ |
| 437 | do_test(&tl, F_WRLCK, 0, 15, 0); |
| 438 | compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0); |
| 439 | |
| 440 | /* |
| 441 | * remove all the locks set above |
| 442 | */ |
| 443 | unlock_file(); |
| 444 | |
| 445 | if (fail) { |
| 446 | tst_resm(TINFO, "Test block 3: FAILED"); |
| 447 | } else { |
| 448 | tst_resm(TINFO, "Test block 3 : PASSED"); |
| 449 | } |
| 450 | tst_resm(TINFO, "Exit block 3"); |
| 451 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 452 | /* //block4: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 453 | tst_resm(TINFO, "Enter block 4"); |
| 454 | fail = 0; |
| 455 | |
| 456 | /* |
| 457 | * Set a read lock at the middle of the file and a |
| 458 | * write lock just before |
| 459 | */ |
| 460 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 461 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 462 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 463 | fail = 1; |
| 464 | } |
| 465 | |
| 466 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 467 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 468 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 469 | fail = 1; |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Test the write lock |
| 474 | */ |
| 475 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 476 | compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 5, parent_pid); |
| 477 | |
| 478 | /* |
| 479 | * Test the read lock. |
| 480 | */ |
| 481 | do_test(&tl, (short)F_WRLCK, (short)0, 10, 0); |
| 482 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid); |
| 483 | |
| 484 | /* |
| 485 | * Test to make sure the rest of the file is unlocked. |
| 486 | */ |
| 487 | do_test(&tl, (short)F_WRLCK, (short)0, 15, 0); |
| 488 | compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0); |
| 489 | |
| 490 | /* |
| 491 | * remove all the locks set above |
| 492 | */ |
| 493 | unlock_file(); |
| 494 | |
| 495 | if (fail) { |
| 496 | tst_resm(TINFO, "Test block 4: FAILED"); |
| 497 | } else { |
| 498 | tst_resm(TINFO, "Test block 4: PASSED"); |
| 499 | } |
| 500 | tst_resm(TINFO, "Exit block 4"); |
| 501 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 502 | /* //block5: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 503 | tst_resm(TINFO, "Enter block 5"); |
| 504 | fail = 0; |
| 505 | |
| 506 | /* |
| 507 | * Set a read lock in the middle and a write lock that |
| 508 | * ends at the first byte of the read lock |
| 509 | */ |
| 510 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 511 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 512 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 513 | fail = 1; |
| 514 | } |
| 515 | |
| 516 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 6) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 517 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 518 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 519 | fail = 1; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Test write lock |
| 524 | */ |
| 525 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 526 | compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 6, parent_pid); |
| 527 | |
| 528 | /* |
| 529 | * Test read lock |
| 530 | */ |
| 531 | do_test(&tl, (short)F_WRLCK, (short)0, 11, 0); |
| 532 | compare_lock(&tl, (short)F_RDLCK, (short)0, 11, 4, parent_pid); |
| 533 | |
| 534 | /* |
| 535 | * Test to make sure the rest of the file is unlocked. |
| 536 | */ |
| 537 | do_test(&tl, (short)F_WRLCK, (short)0, 15, 0); |
| 538 | compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0); |
| 539 | |
| 540 | /* |
| 541 | * remove all the locks set above |
| 542 | */ |
| 543 | unlock_file(); |
| 544 | |
| 545 | if (fail) { |
| 546 | tst_resm(TINFO, "Test block 5: FAILED"); |
| 547 | } else { |
| 548 | tst_resm(TINFO, "Test block 5: PASSED"); |
| 549 | } |
| 550 | tst_resm(TINFO, "Exit block 5"); |
| 551 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 552 | /* //block6: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 553 | tst_resm(TINFO, "Enter block 6"); |
| 554 | fail = 0; |
| 555 | |
| 556 | /* |
| 557 | * Set a read lock on the middle of the file and a write |
| 558 | * lock that overlaps the front of the read. |
| 559 | */ |
| 560 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 561 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 562 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 563 | fail = 1; |
| 564 | } |
| 565 | |
| 566 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 5, 8) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 567 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 568 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 569 | fail = 1; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Test the write lock |
| 574 | */ |
| 575 | do_test(&tl, (short)F_WRLCK, (short)0, 5, 0); |
| 576 | compare_lock(&tl, (short)F_WRLCK, (short)0, 5, 8, parent_pid); |
| 577 | |
| 578 | /* |
| 579 | * Test the read lock |
| 580 | */ |
| 581 | do_test(&tl, (short)F_WRLCK, (short)0, 13, 0); |
| 582 | compare_lock(&tl, (short)F_RDLCK, (short)0, 13, 2, parent_pid); |
| 583 | |
| 584 | /* |
| 585 | * Test to make sure the rest of the file is unlocked. |
| 586 | */ |
| 587 | do_test(&tl, (short)F_WRLCK, (short)0, 15, 0); |
| 588 | compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, 0); |
| 589 | |
| 590 | /* |
| 591 | * remove all the locks set above |
| 592 | */ |
| 593 | unlock_file(); |
| 594 | |
| 595 | if (fail) { |
| 596 | tst_resm(TINFO, "Test block 6 FAILED"); |
| 597 | } else { |
| 598 | tst_resm(TINFO, "Test block 6 PASSED"); |
| 599 | } |
| 600 | tst_resm(TINFO, "Exit block 6"); |
| 601 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 602 | /* //block7: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 603 | tst_resm(TINFO, "Enter block 7"); |
| 604 | fail = 0; |
| 605 | |
| 606 | /* |
| 607 | * Set a read lock in the middle of a file and a write |
| 608 | * lock in the middle of it |
| 609 | */ |
| 610 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 10) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 611 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 612 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 613 | fail = 1; |
| 614 | } |
| 615 | |
| 616 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 617 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 618 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 619 | fail = 1; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Test the first read lock |
| 624 | */ |
| 625 | do_test(&tl, (short)F_WRLCK, (short)0 , 0, 0); |
| 626 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid); |
| 627 | |
| 628 | /* |
| 629 | * Test the write lock |
| 630 | */ |
| 631 | do_test(&tl, (short)F_WRLCK, (short)0, 13, 0); |
| 632 | compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid); |
| 633 | |
| 634 | /* |
| 635 | * Test the second read lock |
| 636 | */ |
| 637 | do_test(&tl, (short)F_WRLCK, (short)0, 18, 0); |
| 638 | compare_lock(&tl, (short)F_RDLCK, (short)0, 18, 2, parent_pid); |
| 639 | |
| 640 | /* |
| 641 | * Test to make sure the rest of the file is unlocked |
| 642 | */ |
| 643 | do_test(&tl, (short)F_WRLCK, (short)0, 20, 0); |
| 644 | compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0); |
| 645 | |
| 646 | /* |
| 647 | * remove all the locks set above. |
| 648 | */ |
| 649 | unlock_file(); |
| 650 | if (fail) { |
| 651 | tst_resm(TINFO, "Test block 7: FAILED"); |
| 652 | } else { |
| 653 | tst_resm(TINFO, "Test block 7: PASSED"); |
| 654 | } |
| 655 | tst_resm(TINFO, "Exit block 7"); |
| 656 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 657 | /* //block8: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 658 | tst_resm(TINFO, "Enter block 8"); |
| 659 | fail = 0; |
| 660 | /* |
| 661 | * Set a read lock in the middle of the file and a write |
| 662 | * lock that overlaps the end |
| 663 | */ |
| 664 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 665 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 666 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 667 | fail = 1; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * Set a write lock on the whole file |
| 672 | */ |
| 673 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 13, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 674 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 675 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 676 | fail = 1; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Test the read lock |
| 681 | */ |
| 682 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 683 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid); |
| 684 | |
| 685 | /* |
| 686 | * Test the write lock |
| 687 | */ |
| 688 | do_test(&tl, (short)F_WRLCK, (short)0, 13, 0); |
| 689 | compare_lock(&tl, (short)F_WRLCK, (short)0, 13, 5, parent_pid); |
| 690 | |
| 691 | /* |
| 692 | * Test to make sure the rest of the file is unlocked |
| 693 | */ |
| 694 | do_test(&tl, (short)F_WRLCK, (short)0, 18, 0); |
| 695 | compare_lock(&tl, (short)F_UNLCK, (short)0, 18, 0, 0); |
| 696 | |
| 697 | /* |
| 698 | * remove all the locks set above |
| 699 | */ |
| 700 | unlock_file(); |
| 701 | |
| 702 | if (fail) { |
| 703 | tst_resm(TINFO, "Test block 8: FAILED"); |
| 704 | } else { |
| 705 | tst_resm(TINFO, "Test block 8: PASSED"); |
| 706 | } |
| 707 | tst_resm(TINFO, "Exit block 8"); |
| 708 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 709 | /* //block9: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 710 | tst_resm(TINFO, "Enter block 9"); |
| 711 | fail = 0; |
| 712 | |
| 713 | /* |
| 714 | * Set a read lock in the middle of the file and a write |
| 715 | * lock starting at the last byte of the read lock |
| 716 | */ |
| 717 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 718 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 719 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 720 | fail = 1; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Set a write lock on the whole file. |
| 725 | */ |
| 726 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 14, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 727 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 728 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 729 | fail = 1; |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Test read lock |
| 734 | */ |
| 735 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 736 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 4, parent_pid); |
| 737 | |
| 738 | /* |
| 739 | * Test the write lock |
| 740 | */ |
| 741 | do_test(&tl, (short)F_WRLCK, (short)0, 14, 0); |
| 742 | compare_lock(&tl, (short)F_WRLCK, (short)0, 14, 5, parent_pid); |
| 743 | |
| 744 | /* |
| 745 | * Test to make sure the end of the file is unlocked |
| 746 | */ |
| 747 | do_test(&tl, (short)F_WRLCK, (short)0, 19, 0); |
| 748 | compare_lock(&tl, (short)F_UNLCK, (short)0, 19, 0, 0); |
| 749 | |
| 750 | /* |
| 751 | * remove all the locks set above |
| 752 | */ |
| 753 | unlock_file(); |
| 754 | |
| 755 | if (fail) { |
| 756 | tst_resm(TINFO, "Test block 9: FAILED"); |
| 757 | } else { |
| 758 | tst_resm(TINFO, "Test block 9: PASSED"); |
| 759 | } |
| 760 | tst_resm(TINFO, "Exit block 9"); |
| 761 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 762 | /* //block10: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 763 | tst_resm(TINFO, "Enter block 10"); |
| 764 | fail = 0; |
| 765 | |
| 766 | /* |
| 767 | * Set a read lock in the middle of the file and a write |
| 768 | * lock that starts just after the last byte of the |
| 769 | * read lock. |
| 770 | */ |
| 771 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 772 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 773 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 774 | fail = 1; |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | * Set a write lock on the whole file |
| 779 | */ |
| 780 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 15, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 781 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 782 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 783 | fail = 1; |
| 784 | } |
| 785 | |
| 786 | /* |
| 787 | * Test the read lock |
| 788 | */ |
| 789 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 790 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid); |
| 791 | |
| 792 | /* |
| 793 | * Test the write lock |
| 794 | */ |
| 795 | do_test(&tl, (short)F_WRLCK, (short)0, 15, 0); |
| 796 | compare_lock(&tl, (short)F_WRLCK, (short)0, 15, 5, parent_pid); |
| 797 | |
| 798 | /* |
| 799 | * Test to make sure the rest of the file is unlocked |
| 800 | */ |
| 801 | do_test(&tl, (short)F_WRLCK, (short)0, 20, 0); |
| 802 | compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, 0); |
| 803 | |
| 804 | /* |
| 805 | * remove all the locks set above |
| 806 | */ |
| 807 | unlock_file(); |
| 808 | |
| 809 | if (fail) { |
| 810 | tst_resm(TINFO, "Test block 10: FAILED"); |
| 811 | } else { |
| 812 | tst_resm(TINFO, "Test block 10: PASSED"); |
| 813 | } |
| 814 | tst_resm(TINFO, "Exit block 10"); |
| 815 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 816 | /* //block11: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 817 | tst_resm(TINFO, "Enter block 11"); |
| 818 | fail = 0; |
| 819 | |
| 820 | /* |
| 821 | * Set a read lock at the middle of the file and a write |
| 822 | * lock that starts past the end of the read lock. |
| 823 | */ |
| 824 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 825 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 826 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 827 | fail = 1; |
| 828 | } |
| 829 | |
| 830 | if (do_lock(F_SETLK, (short)F_WRLCK, (short)0, 16, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 831 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 832 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 833 | fail = 1; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Test the read lock |
| 838 | */ |
| 839 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 840 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid); |
| 841 | |
| 842 | /* |
| 843 | * Test that byte in between is unlocked |
| 844 | */ |
| 845 | do_test(&tl, (short)F_WRLCK, (short)0, 15, 1); |
| 846 | compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 1, 0); |
| 847 | |
| 848 | /* |
| 849 | * Test the write lock |
| 850 | */ |
| 851 | do_test(&tl, (short)F_WRLCK, (short)0, 16, 0); |
| 852 | compare_lock(&tl, (short)F_WRLCK, (short)0, 16, 5, parent_pid); |
| 853 | |
| 854 | /* |
| 855 | * Test to make sure the rest of the file is unlocked |
| 856 | */ |
| 857 | do_test(&tl, (short)F_WRLCK, (short)0, 21, 0); |
| 858 | compare_lock(&tl, (short)F_UNLCK, (short)0, 21, 0, 0); |
| 859 | |
| 860 | /* |
| 861 | * remove all the locks set above |
| 862 | */ |
| 863 | unlock_file(); |
| 864 | |
| 865 | if (fail) { |
| 866 | tst_resm(TINFO, "Test block 11: FAILED"); |
| 867 | } else { |
| 868 | tst_resm(TINFO, "Test block 11: PASSED"); |
| 869 | } |
| 870 | tst_resm(TINFO, "Exit block 11"); |
| 871 | |
| 872 | stop_child(); |
| 873 | close(fd); |
| 874 | } |
| 875 | cleanup(); |
| 876 | return(0); |
| 877 | } |