Skip to content
This repository
Browse code

linux: set close-on-exec flag with ioctl(FIOCLEX)

ioctl(FIOCLEX) is 25% faster than fcntl(F_SETFD) on a stock 2.6.32 kernel.
  • Loading branch information...
commit 0fde10825e0cc65fa476738a74e631c2aab0752b 1 parent e21cdf1
Ben Noordhuis bnoordhuis authored

Showing 1 changed file with 21 additions and 13 deletions. Show diff stats Hide diff stats

  1. +21 13 src/unix/core.c
34 src/unix/core.c
@@ -487,17 +487,34 @@ int uv__accept(int sockfd) {
487 487 }
488 488
489 489
  490 +#if __linux__
  491 +
490 492 int uv__nonblock(int fd, int set) {
491 493 int r;
492 494
493   -#if FIONBIO
494 495 do
495 496 r = ioctl(fd, FIONBIO, &set);
496 497 while (r == -1 && errno == EINTR);
497 498
498 499 return r;
499   -#else
  500 +}
  501 +
  502 +
  503 +int uv__cloexec(int fd, int set) {
  504 + int r;
  505 +
  506 + do
  507 + r = ioctl(fd, set ? FIOCLEX : FIONCLEX);
  508 + while (r == -1 && errno == EINTR);
  509 +
  510 + return r;
  511 +}
  512 +
  513 +#else /* !__linux__ */
  514 +
  515 +int uv__nonblock(int fd, int set) {
500 516 int flags;
  517 + int r;
501 518
502 519 do
503 520 r = fcntl(fd, F_GETFL);
@@ -516,7 +533,6 @@ int uv__nonblock(int fd, int set) {
516 533 while (r == -1 && errno == EINTR);
517 534
518 535 return r;
519   -#endif
520 536 }
521 537
522 538
@@ -524,15 +540,6 @@ int uv__cloexec(int fd, int set) {
524 540 int flags;
525 541 int r;
526 542
527   -#if __linux__
528   - /* Linux knows only FD_CLOEXEC so we can safely omit the fcntl(F_GETFD)
529   - * syscall. CHECKME: That's probably true for other Unices as well.
530   - */
531   - if (set)
532   - flags = FD_CLOEXEC;
533   - else
534   - flags = 0;
535   -#else
536 543 do
537 544 r = fcntl(fd, F_GETFD);
538 545 while (r == -1 && errno == EINTR);
@@ -544,7 +551,6 @@ int uv__cloexec(int fd, int set) {
544 551 flags = r | FD_CLOEXEC;
545 552 else
546 553 flags = r & ~FD_CLOEXEC;
547   -#endif
548 554
549 555 do
550 556 r = fcntl(fd, F_SETFD, flags);
@@ -553,6 +559,8 @@ int uv__cloexec(int fd, int set) {
553 559 return r;
554 560 }
555 561
  562 +#endif /* __linux__ */
  563 +
556 564
557 565 /* This function is not execve-safe, there is a race window
558 566 * between the call to dup() and fcntl(FD_CLOEXEC).

0 comments on commit 0fde108

Please sign in to comment.
Something went wrong with that request. Please try again.