Merge pull request #10905 from obsidiansystems/platform-namespace

Put some file descriptor functions in unix and windows namespaces
This commit is contained in:
John Ericson 2024-06-14 08:45:31 -04:00 committed by GitHub
commit 2f5fdab06c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 19 additions and 15 deletions

View file

@ -1500,7 +1500,7 @@ void LocalDerivationGoal::startDaemon()
throw SysError("accepting connection"); throw SysError("accepting connection");
} }
closeOnExec(remote.get()); unix::closeOnExec(remote.get());
debug("received daemon connection"); debug("received daemon connection");
@ -1961,7 +1961,7 @@ void LocalDerivationGoal::runChild()
throw SysError("changing into '%1%'", tmpDir); throw SysError("changing into '%1%'", tmpDir);
/* Close all other file descriptors. */ /* Close all other file descriptors. */
closeMostFDs({STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}); unix::closeMostFDs({STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO});
#if __linux__ #if __linux__
linux::setPersonality(drv->platform); linux::setPersonality(drv->platform);

View file

@ -140,6 +140,7 @@ public:
}; };
#ifndef _WIN32 // Not needed on Windows, where we don't fork #ifndef _WIN32 // Not needed on Windows, where we don't fork
namespace unix {
/** /**
* Close all file descriptors except those listed in the given set. * Close all file descriptors except those listed in the given set.
@ -152,13 +153,16 @@ void closeMostFDs(const std::set<Descriptor> & exceptions);
*/ */
void closeOnExec(Descriptor fd); void closeOnExec(Descriptor fd);
} // namespace unix
#endif #endif
#ifdef _WIN32 #if defined(_WIN32) && _WIN32_WINNT >= 0x0600
# if _WIN32_WINNT >= 0x0600 namespace windows (
Path handleToPath(Descriptor handle); Path handleToPath(Descriptor handle);
std::wstring handleToFileName(Descriptor handle); std::wstring handleToFileName(Descriptor handle);
# endif
} // namespace windows
#endif #endif
MakeError(EndOfFile, Error); MakeError(EndOfFile, Error);

View file

@ -546,7 +546,7 @@ std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)
if (!fd) if (!fd)
throw SysError("creating temporary file '%s'", tmpl); throw SysError("creating temporary file '%s'", tmpl);
#ifndef _WIN32 #ifndef _WIN32
closeOnExec(fd.get()); unix::closeOnExec(fd.get());
#endif #endif
return {std::move(fd), tmpl}; return {std::move(fd), tmpl};
} }

View file

@ -24,7 +24,7 @@ AutoCloseFD createUnixDomainSocket()
if (!fdSocket) if (!fdSocket)
throw SysError("cannot create Unix domain socket"); throw SysError("cannot create Unix domain socket");
#ifndef _WIN32 #ifndef _WIN32
closeOnExec(fdSocket.get()); unix::closeOnExec(fdSocket.get());
#endif #endif
return fdSocket; return fdSocket;
} }

View file

@ -110,8 +110,8 @@ void Pipe::create()
if (pipe2(fds, O_CLOEXEC) != 0) throw SysError("creating pipe"); if (pipe2(fds, O_CLOEXEC) != 0) throw SysError("creating pipe");
#else #else
if (pipe(fds) != 0) throw SysError("creating pipe"); if (pipe(fds) != 0) throw SysError("creating pipe");
closeOnExec(fds[0]); unix::closeOnExec(fds[0]);
closeOnExec(fds[1]); unix::closeOnExec(fds[1]);
#endif #endif
readSide = fds[0]; readSide = fds[0];
writeSide = fds[1]; writeSide = fds[1];
@ -120,7 +120,7 @@ void Pipe::create()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void closeMostFDs(const std::set<int> & exceptions) void unix::closeMostFDs(const std::set<int> & exceptions)
{ {
#if __linux__ #if __linux__
try { try {
@ -148,7 +148,7 @@ void closeMostFDs(const std::set<int> & exceptions)
} }
void closeOnExec(int fd) void unix::closeOnExec(int fd)
{ {
int prev; int prev;
if ((prev = fcntl(fd, F_GETFD, 0)) == -1 || if ((prev = fcntl(fd, F_GETFD, 0)) == -1 ||

View file

@ -122,7 +122,7 @@ void Pipe::create()
#if _WIN32_WINNT >= 0x0600 #if _WIN32_WINNT >= 0x0600
std::wstring handleToFileName(HANDLE handle) { std::wstring windows::handleToFileName(HANDLE handle) {
std::vector<wchar_t> buf(0x100); std::vector<wchar_t> buf(0x100);
DWORD dw = GetFinalPathNameByHandleW(handle, buf.data(), buf.size(), FILE_NAME_OPENED); DWORD dw = GetFinalPathNameByHandleW(handle, buf.data(), buf.size(), FILE_NAME_OPENED);
if (dw == 0) { if (dw == 0) {
@ -141,7 +141,7 @@ std::wstring handleToFileName(HANDLE handle) {
} }
Path handleToPath(HANDLE handle) { Path windows::handleToPath(HANDLE handle) {
return os_string_to_string(handleToFileName(handle)); return os_string_to_string(handleToFileName(handle));
} }

View file

@ -295,7 +295,7 @@ static void daemonLoop(std::optional<TrustedFlag> forceTrustClientOpt)
if (getEnv("LISTEN_PID") != std::to_string(getpid()) || listenFds != "1") if (getEnv("LISTEN_PID") != std::to_string(getpid()) || listenFds != "1")
throw Error("unexpected systemd environment variables"); throw Error("unexpected systemd environment variables");
fdSocket = SD_LISTEN_FDS_START; fdSocket = SD_LISTEN_FDS_START;
closeOnExec(fdSocket.get()); unix::closeOnExec(fdSocket.get());
} }
// Otherwise, create and bind to a Unix domain socket. // Otherwise, create and bind to a Unix domain socket.
@ -323,7 +323,7 @@ static void daemonLoop(std::optional<TrustedFlag> forceTrustClientOpt)
throw SysError("accepting connection"); throw SysError("accepting connection");
} }
closeOnExec(remote.get()); unix::closeOnExec(remote.get());
PeerInfo peer { .pidKnown = false }; PeerInfo peer { .pidKnown = false };
TrustedFlag trusted; TrustedFlag trusted;