libutil: save cwd fd in restoreMountNamespace

This doesn't work very well (maybe I'm misunderstanding the desired
implementation):

    : ~/w/vc/nix ; doas outputs/out/bin/nix --experimental-features 'nix-command flakes' develop -c pwd
    pwd: couldn't find directory entry in ‘../../../..’ with matching i-node
This commit is contained in:
Cole Helbling 2022-04-04 10:16:30 -07:00
parent f89b0f7846
commit 10b9c1b2b2

View file

@ -1690,7 +1690,6 @@ void setStackSize(size_t stackSize)
#if __linux__ #if __linux__
static AutoCloseFD fdSavedMountNamespace; static AutoCloseFD fdSavedMountNamespace;
static AutoCloseFD fdSavedCwd;
#endif #endif
void saveMountNamespace() void saveMountNamespace()
@ -1702,11 +1701,6 @@ void saveMountNamespace()
if (!fd) if (!fd)
throw SysError("saving parent mount namespace"); throw SysError("saving parent mount namespace");
fdSavedMountNamespace = std::move(fd); fdSavedMountNamespace = std::move(fd);
fd = open("/proc/self/cwd", O_RDONLY);
if (!fd)
throw SysError("saving cwd");
fdSavedCwd = std::move(fd);
}); });
#endif #endif
} }
@ -1715,6 +1709,10 @@ void restoreMountNamespace()
{ {
#if __linux__ #if __linux__
try { try {
AutoCloseFD fdSavedCwd = open("/proc/self/cwd", O_RDONLY);
if (!fdSavedCwd) {
throw SysError("saving cwd");
}
if (fdSavedMountNamespace && setns(fdSavedMountNamespace.get(), CLONE_NEWNS) == -1) if (fdSavedMountNamespace && setns(fdSavedMountNamespace.get(), CLONE_NEWNS) == -1)
throw SysError("restoring parent mount namespace"); throw SysError("restoring parent mount namespace");
if (fdSavedCwd && fchdir(fdSavedCwd.get()) == -1) { if (fdSavedCwd && fchdir(fdSavedCwd.get()) == -1) {