nix: Guard against removing temporary roots of living processes.
authorLudovic Courtès <ludo@gnu.org>
Fri, 7 Oct 2022 20:51:06 +0000 (16:51 -0400)
committerMaxim Cournoyer <maxim.cournoyer@gmail.com>
Fri, 7 Oct 2022 20:54:20 +0000 (16:54 -0400)
Fixes <https://issues.guix.gnu.org/25018>.

* nix/libstore/gc.cc (readTempRoots): Add a check to guard against removing
the temporary roots of a living process.

Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com>
nix/libstore/gc.cc

index 1651911..f101de9 100644 (file)
@@ -2,6 +2,7 @@
 #include "misc.hh"
 #include "local-store.hh"
 
+#include <string>
 #include <functional>
 #include <queue>
 #include <algorithm>
@@ -226,10 +227,10 @@ static void readTempRoots(PathSet & tempRoots, FDs & fds)
         //FDPtr fd(new AutoCloseFD(openLockFile(path, false)));
         //if (*fd == -1) continue;
 
-        /* Try to acquire a write lock without blocking.  This can
-           only succeed if the owning process has died.  In that case
-           we don't care about its temporary roots. */
-        if (lockFile(*fd, ltWrite, false)) {
+        /* Try to acquire a write lock without blocking.  This can only
+           succeed if the owning process has died, in which case we don't care
+           about its temporary roots, or if we are the owning process.  */
+        if (i.name != std::to_string(getpid()) && lockFile(*fd, ltWrite, false)) {
             printMsg(lvlError, format("removing stale temporary roots file `%1%'") % path);
             unlink(path.c_str());
             writeFull(*fd, "d");