Merge pull request #46 from vibe-d/more_cloexec

Set O_CLOEXEC on the remaining FDs that are created by eventcore.
This commit is contained in:
Sönke Ludwig 2017-12-20 01:55:14 +01:00 committed by GitHub
commit 97b154048d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 8 deletions

View file

@ -64,6 +64,9 @@ final class PosixEventDriverEvents(Loop : PosixEventLoop, Sockets : EventDriverS
} else { } else {
sock_t[2] fd; sock_t[2] fd;
version (Posix) { version (Posix) {
import core.sys.posix.fcntl : fcntl, F_SETFL;
import eventcore.drivers.posix.sockets : O_CLOEXEC;
// create a pair of sockets to communicate between threads // create a pair of sockets to communicate between threads
import core.sys.posix.sys.socket : SOCK_DGRAM, AF_UNIX, socketpair; import core.sys.posix.sys.socket : SOCK_DGRAM, AF_UNIX, socketpair;
if (() @trusted { return socketpair(AF_UNIX, SOCK_DGRAM, 0, fd); } () != 0) if (() @trusted { return socketpair(AF_UNIX, SOCK_DGRAM, 0, fd); } () != 0)
@ -72,7 +75,9 @@ final class PosixEventDriverEvents(Loop : PosixEventLoop, Sockets : EventDriverS
assert(fd[0] != fd[1]); assert(fd[0] != fd[1]);
// use the first socket as the async receiver // use the first socket as the async receiver
auto s = m_sockets.adoptDatagramSocketInternal(fd[0]); auto s = m_sockets.adoptDatagramSocketInternal(fd[0], true, true);
() @trusted { fcntl(fd[1], F_SETFL, O_CLOEXEC); } ();
} else { } else {
// fake missing socketpair support on Windows // fake missing socketpair support on Windows
import std.socket : InternetAddress; import std.socket : InternetAddress;

View file

@ -33,7 +33,7 @@ final class SignalFDEventDriverSignals(Loop : PosixEventLoop) : EventDriverSigna
if (sigprocmask(SIG_BLOCK, &sset, null) != 0) if (sigprocmask(SIG_BLOCK, &sset, null) != 0)
return SignalListenID.invalid; return SignalListenID.invalid;
return SignalListenID(signalfd(-1, &sset, SFD_NONBLOCK)); return SignalListenID(signalfd(-1, &sset, SFD_NONBLOCK | SFD_CLOEXEC));
} (); } ();

View file

@ -595,12 +595,12 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets
return adoptDatagramSocketInternal(socket, false); return adoptDatagramSocketInternal(socket, false);
} }
package DatagramSocketFD adoptDatagramSocketInternal(int socket, bool is_internal = true) package DatagramSocketFD adoptDatagramSocketInternal(int socket, bool is_internal = true, bool close_on_exec = false)
{ {
auto fd = DatagramSocketFD(socket); auto fd = DatagramSocketFD(socket);
if (m_loop.m_fds[fd].common.refCount) // FD already in use? if (m_loop.m_fds[fd].common.refCount) // FD already in use?
return DatagramSocketFD.init; return DatagramSocketFD.init;
setSocketNonBlocking(fd); setSocketNonBlocking(fd, close_on_exec);
m_loop.initFD(fd, is_internal ? FDFlags.internal : FDFlags.none); m_loop.initFD(fd, is_internal ? FDFlags.internal : FDFlags.none);
m_loop.registerFD(fd, EventMask.read|EventMask.write|EventMask.status); m_loop.registerFD(fd, EventMask.read|EventMask.write|EventMask.status);
m_loop.m_fds[fd].specific = DgramSocketSlot.init; m_loop.m_fds[fd].specific = DgramSocketSlot.init;

View file

@ -34,7 +34,7 @@ final class InotifyEventDriverWatchers(Events : EventDriverEvents) : EventDriver
import std.range.primitives : walkLength; import std.range.primitives : walkLength;
enum IN_NONBLOCK = 0x800; // value in core.sys.linux.sys.inotify is incorrect enum IN_NONBLOCK = 0x800; // value in core.sys.linux.sys.inotify is incorrect
auto handle = () @trusted { return inotify_init1(IN_NONBLOCK); } (); auto handle = () @trusted { return inotify_init1(IN_NONBLOCK | IN_CLOEXEC); } ();
if (handle == -1) return WatcherID.invalid; if (handle == -1) return WatcherID.invalid;
auto ret = WatcherID(handle); auto ret = WatcherID(handle);