|
46 | 46 | * Test #31: check if dup3(0) fails if oldfd == newfd. |
47 | 47 | * Test #32: check if dup3(O_CLOEXEC) to a fd > current maximum number of |
48 | 48 | * open files limit work. |
| 49 | + * Tests #33-50 : Same as #15-32 with O_CLOFORK instead of O_CLOEXEC |
49 | 50 | */ |
50 | 51 |
|
51 | 52 | #include <sys/types.h> |
|
60 | 61 |
|
61 | 62 | static int getafile(void); |
62 | 63 |
|
| 64 | +static int test = 0; |
| 65 | + |
63 | 66 | static int |
64 | 67 | getafile(void) |
65 | 68 | { |
|
78 | 81 | main(int __unused argc, char __unused *argv[]) |
79 | 82 | { |
80 | 83 | struct rlimit rlp; |
81 | | - int orgfd, fd1, fd2, test = 0; |
| 84 | + int orgfd, fd1, fd2; |
82 | 85 |
|
83 | 86 | orgfd = getafile(); |
84 | 87 |
|
@@ -380,5 +383,139 @@ main(int __unused argc, char __unused *argv[]) |
380 | 383 | printf("ok %d - dup3(O_CLOEXEC) didn't bypass NOFILE limit\n", |
381 | 384 | test); |
382 | 385 |
|
| 386 | + /* Does fcntl(F_DUPFD_CLOFORK) work? */ |
| 387 | + if ((fd2 = fcntl(fd1, F_DUPFD_CLOFORK, 10)) < 0) |
| 388 | + err(1, "fcntl(F_DUPFD_CLOFORK)"); |
| 389 | + if (fd2 < 10) |
| 390 | + printf("not ok %d - fcntl(F_DUPFD_CLOFORK) returned wrong fd %d\n", |
| 391 | + ++test, fd2); |
| 392 | + else |
| 393 | + printf("ok %d - fcntl(F_DUPFD_CLOFORK) works\n", ++test); |
| 394 | + |
| 395 | + /* Was close-on-fork cleared? */ |
| 396 | + ++test; |
| 397 | + if (fcntl(fd2, F_GETFD) != FD_CLOFORK) |
| 398 | + printf( |
| 399 | + "not ok %d - fcntl(F_DUPFD_CLOFORK) didn't set close-on-fork\n", |
| 400 | + test); |
| 401 | + else |
| 402 | + printf("ok %d - fcntl(F_DUPFD_CLOFORK) set close-on-fork\n", |
| 403 | + test); |
| 404 | + |
| 405 | + /* If fcntl(F_DUP2FD_CLOFORK) ever work? */ |
| 406 | + if ((fd2 = fcntl(fd1, F_DUP2FD_CLOFORK, fd1 + 1)) < 0) |
| 407 | + err(1, "fcntl(F_DUP2FD_CLOFORK)"); |
| 408 | + printf("ok %d - fcntl(F_DUP2FD_CLOFORK) works\n", ++test); |
| 409 | + |
| 410 | + /* Do we get the right fd? */ |
| 411 | + ++test; |
| 412 | + if (fd2 != fd1 + 1) |
| 413 | + printf( |
| 414 | + "no ok %d - fcntl(F_DUP2FD_CLOFORK) didn't give us the right fd\n", |
| 415 | + test); |
| 416 | + else |
| 417 | + printf("ok %d - fcntl(F_DUP2FD_CLOFORK) returned a correct fd\n", |
| 418 | + test); |
| 419 | + |
| 420 | + /* Was close-on-fork set? */ |
| 421 | + ++test; |
| 422 | + if (fcntl(fd2, F_GETFD) != FD_CLOFORK) |
| 423 | + printf( |
| 424 | + "not ok %d - fcntl(F_DUP2FD_CLOFORK) didn't set close-on-fork\n", |
| 425 | + test); |
| 426 | + else |
| 427 | + printf("ok %d - fcntl(F_DUP2FD_CLOFORK) set close-on-fork\n", |
| 428 | + test); |
| 429 | + |
| 430 | + /* |
| 431 | + * It is unclear what F_DUP2FD_CLOFORK should do when duplicating a |
| 432 | + * file descriptor onto itself. |
| 433 | + */ |
| 434 | + |
| 435 | + ++test; |
| 436 | + if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) |
| 437 | + err(1, "getrlimit"); |
| 438 | + if ((fd2 = fcntl(fd1, F_DUP2FD_CLOFORK, rlp.rlim_cur + 1)) >= 0) |
| 439 | + printf("not ok %d - fcntl(F_DUP2FD_CLOFORK) bypassed NOFILE limit\n", |
| 440 | + test); |
| 441 | + else |
| 442 | + printf("ok %d - fcntl(F_DUP2FD_CLOFORK) didn't bypass NOFILE limit\n", |
| 443 | + test); |
| 444 | + |
| 445 | + /* Does dup3(O_CLOFORK) ever work? */ |
| 446 | + if ((fd2 = dup3(fd1, fd1 + 1, O_CLOFORK)) < 0) |
| 447 | + err(1, "dup3(O_CLOFORK)"); |
| 448 | + printf("ok %d - dup3(O_CLOFORK) works\n", ++test); |
| 449 | + |
| 450 | + /* Do we get the right fd? */ |
| 451 | + ++test; |
| 452 | + if (fd2 != fd1 + 1) |
| 453 | + printf( |
| 454 | + "no ok %d - dup3(O_CLOFORK) didn't give us the right fd\n", |
| 455 | + test); |
| 456 | + else |
| 457 | + printf("ok %d - dup3(O_CLOFORK) returned a correct fd\n", |
| 458 | + test); |
| 459 | + |
| 460 | + /* Was close-on-fork set? */ |
| 461 | + ++test; |
| 462 | + if (fcntl(fd2, F_GETFD) != FD_CLOFORK) |
| 463 | + printf( |
| 464 | + "not ok %d - dup3(O_CLOFORK) didn't set close-on-fork\n", |
| 465 | + test); |
| 466 | + else |
| 467 | + printf("ok %d - dup3(O_CLOFORK) set close-on-fork\n", |
| 468 | + test); |
| 469 | + |
| 470 | + /* Does dup3(0) ever work? */ |
| 471 | + if ((fd2 = dup3(fd1, fd1 + 1, 0)) < 0) |
| 472 | + err(1, "dup3(0)"); |
| 473 | + printf("ok %d - dup3(0) works\n", ++test); |
| 474 | + |
| 475 | + /* Do we get the right fd? */ |
| 476 | + ++test; |
| 477 | + if (fd2 != fd1 + 1) |
| 478 | + printf( |
| 479 | + "no ok %d - dup3(0) didn't give us the right fd\n", |
| 480 | + test); |
| 481 | + else |
| 482 | + printf("ok %d - dup3(0) returned a correct fd\n", |
| 483 | + test); |
| 484 | + |
| 485 | + /* Was close-on-fork cleared? */ |
| 486 | + ++test; |
| 487 | + if (fcntl(fd2, F_GETFD) != 0) |
| 488 | + printf( |
| 489 | + "not ok %d - dup3(0) didn't clear close-on-fork\n", |
| 490 | + test); |
| 491 | + else |
| 492 | + printf("ok %d - dup3(0) cleared close-on-fork\n", |
| 493 | + test); |
| 494 | + |
| 495 | + /* dup3() does not allow duplicating to the same fd */ |
| 496 | + ++test; |
| 497 | + if (dup3(fd1, fd1, O_CLOFORK) != -1) |
| 498 | + printf( |
| 499 | + "not ok %d - dup3(fd1, fd1, O_CLOFORK) succeeded\n", test); |
| 500 | + else |
| 501 | + printf("ok %d - dup3(fd1, fd1, O_CLOFORK) failed\n", test); |
| 502 | + |
| 503 | + ++test; |
| 504 | + if (dup3(fd1, fd1, 0) != -1) |
| 505 | + printf( |
| 506 | + "not ok %d - dup3(fd1, fd1, 0) succeeded\n", test); |
| 507 | + else |
| 508 | + printf("ok %d - dup3(fd1, fd1, 0) failed\n", test); |
| 509 | + |
| 510 | + ++test; |
| 511 | + if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) |
| 512 | + err(1, "getrlimit"); |
| 513 | + if ((fd2 = dup3(fd1, rlp.rlim_cur + 1, O_CLOFORK)) >= 0) |
| 514 | + printf("not ok %d - dup3(O_CLOFORK) bypassed NOFILE limit\n", |
| 515 | + test); |
| 516 | + else |
| 517 | + printf("ok %d - dup3(O_CLOFORK) didn't bypass NOFILE limit\n", |
| 518 | + test); |
| 519 | + |
383 | 520 | return (0); |
384 | 521 | } |
0 commit comments