Compare commits

...

2 Commits

Author SHA1 Message Date
Jarred Sumner
373d3fb92e Merge branch 'main' into jarred/cloexec 2024-06-03 20:14:26 -07:00
Jarred Sumner
af55a21b30 CLOEXEC more 2024-05-29 20:18:30 -07:00
2 changed files with 13 additions and 9 deletions

View File

@@ -1262,6 +1262,7 @@ pub fn spawnProcessPosix(
const stdios: [3]*?bun.FileDescriptor = .{ &spawned.stdin, &spawned.stdout, &spawned.stderr };
var dup_stdout_to_stderr: bool = false;
const sock_flags = if (comptime Environment.isLinux) std.os.SOCK.STREAM | std.os.SOCK.CLOEXEC else std.os.SOCK.STREAM;
for (0..3) |i| {
const stdio = stdios[i];
@@ -1319,14 +1320,16 @@ pub fn spawnProcessPosix(
const fds: [2]bun.FileDescriptor = brk: {
var fds_: [2]std.c.fd_t = undefined;
const rc = std.c.socketpair(std.os.AF.UNIX, std.os.SOCK.STREAM, 0, &fds_);
const rc = std.c.socketpair(std.os.AF.UNIX, sock_flags, 0, &fds_);
if (rc != 0) {
return error.SystemResources;
}
{
const before = std.c.fcntl(fds_[if (i == 0) 1 else 0], std.os.F.GETFD);
_ = std.c.fcntl(fds_[if (i == 0) 1 else 0], std.os.F.SETFD, before | std.os.FD_CLOEXEC);
if (comptime !Environment.isLinux) {
for (fds_) |fd| {
const before = std.c.fcntl(fd, std.os.F.GETFD);
_ = std.c.fcntl(fd, std.os.F.SETFD, before | std.os.FD_CLOEXEC);
}
}
if (comptime Environment.isMac) {
@@ -1411,7 +1414,7 @@ pub fn spawnProcessPosix(
.buffer => {
const fds: [2]bun.FileDescriptor = brk: {
var fds_: [2]std.c.fd_t = undefined;
const rc = std.c.socketpair(std.os.AF.UNIX, std.os.SOCK.STREAM, 0, &fds_);
const rc = std.c.socketpair(std.os.AF.UNIX, sock_flags, 0, &fds_);
if (rc != 0) {
return error.SystemResources;
}

View File

@@ -65,7 +65,7 @@ extern "C" ssize_t posix_spawn_bun(
const auto childFailed = [&]() -> ssize_t {
res = errno;
status = res;
bun_close_range(0, ~0U, 0);
bun_close_range(0, ~0U, CLOSE_RANGE_CLOEXEC);
_exit(127);
// should never be reached
@@ -164,10 +164,11 @@ extern "C" ssize_t posix_spawn_bun(
sigprocmask(SIG_SETMASK, &childmask, 0);
if (!envp)
envp = environ;
// If CLOSE_RANGE_CLOEXEC is unsupported, we risk leaking some file descriptors
// This is better than potentially causing a different thread (like the watcher) to crash.
bun_close_range(current_max_fd + 1, ~0U, CLOSE_RANGE_CLOEXEC);
if (bun_close_range(current_max_fd + 1, ~0U, CLOSE_RANGE_CLOEXEC) != 0) {
bun_close_range(current_max_fd + 1, ~0U, 0);
}
if (execve(path, argv, envp) == -1) {
return childFailed();
}