diff options
Diffstat (limited to 'src/daemon.zig')
| -rw-r--r-- | src/daemon.zig | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/daemon.zig b/src/daemon.zig index 84c77f90..55ffe376 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -102,7 +102,8 @@ fn recv(fd: c_int, buf: []u8) ?Got { var cbuf: [cmsgSpace(max_fds)]u8 align(@alignOf(libc.cmsghdr)) = undefined; var msg: libc.msghdr = .{ .name = null, .namelen = 0, .iov = &iov, .iovlen = 1, .control = &cbuf, .controllen = cbuf.len, .flags = 0 }; const n = while (true) { - const r = libc.recvmsg(fd, &msg, libc.MSG.CMSG_CLOEXEC); + // Darwin has no MSG_CMSG_CLOEXEC: its fds are marked below. + const r = libc.recvmsg(fd, &msg, if (builtin.os.tag == .linux) libc.MSG.CMSG_CLOEXEC else 0); if (r >= 0) break r; if (libc.errno(r) != .INTR) return null; }; @@ -112,6 +113,9 @@ fn recv(fd: c_int, buf: []u8) ?Got { if (hdr.level == libc.SOL.SOCKET and hdr.type == libc.SCM.RIGHTS and hdr.len >= data_off) { got.nfds = @min((hdr.len - data_off) / @sizeOf(c_int), max_fds); @memcpy(std.mem.sliceAsBytes(got.fds[0..got.nfds]), cbuf[data_off..][0 .. got.nfds * @sizeOf(c_int)]); + if (builtin.os.tag != .linux) for (got.fds[0..got.nfds]) |f| { + _ = libc.fcntl(f, libc.F.SETFD, @as(c_int, 1)); // FD_CLOEXEC + }; } } // A zero-length datagram is never sent: it is the peer's end. @@ -248,8 +252,11 @@ fn spawn(listener: c_int, lock: c_int) bool { const l = libc.fcntl(listener, libc.F.DUPFD, @as(c_int, 10)); const k = libc.fcntl(lock, libc.F.DUPFD, @as(c_int, 10)); if (l < 0 or k < 0 or libc.dup2(l, 3) < 0 or libc.dup2(k, 4) < 0) libc._exit(127); - // Linux's by its syscall: musl (a static pardes) has no close_range. - const shut = if (comptime builtin.os.tag == .linux) std.os.linux.close_range(5, std.math.maxInt(i32), .{ .UNSHARE = false, .CLOEXEC = false }) == 0 else close_range(5, std.math.maxInt(c_uint), 0) == 0; + // Linux's by its syscall: musl (a static pardes) has no close_range; + // nor has Darwin, which closes them one by one. + const shut = if (comptime builtin.os.tag == .linux) + std.os.linux.close_range(5, std.math.maxInt(i32), .{ .UNSHARE = false, .CLOEXEC = false }) == 0 + else if (comptime builtin.os.tag.isDarwin()) false else close_range(5, std.math.maxInt(c_uint), 0) == 0; if (!shut) { var fd: c_int = 5; while (fd < 1024) : (fd += 1) _ = libc.close(fd); |
