From f38ae92a38a66b597dbd6975219d6ddb4f52fa4f Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Mon, 18 Mar 2024 14:52:04 +0100 Subject: libutil: make AutoCloseFD a better resource add a reset() method to close the wrapped fd instead of assigning magic constants. also make the from-fd constructor explicit so you can't accidentally assign the *wrong* magic constant, or even an unrelated integer that also just happens to be an fd by pure chance. Change-Id: I51311b0f6e040240886b5103d39d1794a6acc325 --- src/nix/daemon.cc | 8 ++++---- src/nix/prefetch.cc | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/nix') diff --git a/src/nix/daemon.cc b/src/nix/daemon.cc index 4f9907ad7..9d4afb6d9 100644 --- a/src/nix/daemon.cc +++ b/src/nix/daemon.cc @@ -294,7 +294,7 @@ static void daemonLoop(std::optional forceTrustClientOpt) if (listenFds) { if (getEnv("LISTEN_PID") != std::to_string(getpid()) || listenFds != "1") throw Error("unexpected systemd environment variables"); - fdSocket = SD_LISTEN_FDS_START; + fdSocket = AutoCloseFD{SD_LISTEN_FDS_START}; closeOnExec(fdSocket.get()); } @@ -315,8 +315,8 @@ static void daemonLoop(std::optional forceTrustClientOpt) struct sockaddr_un remoteAddr; socklen_t remoteAddrLen = sizeof(remoteAddr); - AutoCloseFD remote = accept(fdSocket.get(), - (struct sockaddr *) &remoteAddr, &remoteAddrLen); + AutoCloseFD remote{accept(fdSocket.get(), + (struct sockaddr *) &remoteAddr, &remoteAddrLen)}; checkInterrupt(); if (!remote) { if (errno == EINTR) continue; @@ -348,7 +348,7 @@ static void daemonLoop(std::optional forceTrustClientOpt) options.dieWithParent = false; options.runExitHandlers = true; startProcess([&]() { - fdSocket = -1; + fdSocket.reset(); // Background the daemon. if (setsid() == -1) diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc index 0104635fb..8f74984e0 100644 --- a/src/nix/prefetch.cc +++ b/src/nix/prefetch.cc @@ -92,7 +92,7 @@ std::tuple prefetchFile( if (executable) mode = 0700; - AutoCloseFD fd = open(tmpFile.c_str(), O_WRONLY | O_CREAT | O_EXCL, mode); + AutoCloseFD fd{open(tmpFile.c_str(), O_WRONLY | O_CREAT | O_EXCL, mode)}; if (!fd) throw SysError("creating temporary file '%s'", tmpFile); FdSink sink(fd.get()); -- cgit v1.2.3