Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: make uv__cloexec() EINTR resilient
Browse files Browse the repository at this point in the history
It's somewhat underspecified if and when fcntl() can return EINTR. It never
does on Linux for F_GETFD or F_SETFD but let's not make any assumptions.
  • Loading branch information
bnoordhuis authored and piscisaureus committed Jun 13, 2012
1 parent 07c6ac2 commit b3a97f8
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/unix/core.c
Expand Up @@ -487,30 +487,36 @@ int uv__nonblock(int fd, int set) {


int uv__cloexec(int fd, int set) {
int flags;
int r;

#if __linux__
/* Linux knows only FD_CLOEXEC so we can safely omit the fcntl(F_GETFD)
* syscall. CHECKME: That's probably true for other Unices as well.
*/
return fcntl(fd, F_SETFD, set ? FD_CLOEXEC : 0);
if (set)
flags = FD_CLOEXEC;
else
flags = 0;
#else
int flags;
do
r = fcntl(fd, F_GETFD);
while (r == -1 && errno == EINTR);

if ((flags = fcntl(fd, F_GETFD)) == -1) {
if (r == -1)
return -1;
}

if (set) {
flags |= FD_CLOEXEC;
} else {
flags &= ~FD_CLOEXEC;
}
if (set)
flags = r | FD_CLOEXEC;
else
flags = r & ~FD_CLOEXEC;
#endif

if (fcntl(fd, F_SETFD, flags) == -1) {
return -1;
}
do
r = fcntl(fd, F_SETFD, flags);
while (r == -1 && errno == EINTR);

return 0;
#endif
return r;
}


Expand Down

0 comments on commit b3a97f8

Please sign in to comment.