job-control: set CLOEXEC on pty processes. #5986

Before this change, new processes started with libuv prevented SIGHUP
from reaching pty processes (by keeping the ptmx file descriptor open).
This commit is contained in:
Matthew Malcomson
2017-01-21 14:11:30 +00:00
committed by Justin M. Keyes
parent ad1884be0d
commit f6946c68ae
7 changed files with 77 additions and 35 deletions

View File

@@ -391,6 +391,35 @@ int os_open(const char* path, int flags, int mode)
return r;
}
/// Sets file descriptor `fd` to close-on-exec.
//
// @return -1 if failed to set, 0 otherwise.
int os_set_cloexec(const int fd)
{
#ifdef HAVE_FD_CLOEXEC
int e;
int fdflags = fcntl(fd, F_GETFD);
if (fdflags < 0) {
e = errno;
ELOG("Failed to get flags on descriptor %d: %s", fd, strerror(e));
errno = e;
return -1;
}
if ((fdflags & FD_CLOEXEC) == 0
&& fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) {
e = errno;
ELOG("Failed to set CLOEXEC on descriptor %d: %s", fd, strerror(e));
errno = e;
return -1;
}
return 0;
#endif
// No FD_CLOEXEC flag. On Windows, the file should have been opened with
// O_NOINHERIT anyway.
return -1;
}
/// Close a file
///
/// @return 0 or libuv error code on failure.