libutil: save fd to cwd instead of cwd itself
This commit is contained in:
parent
e5b70d47aa
commit
e135d223f6
|
@ -1690,7 +1690,7 @@ void setStackSize(size_t stackSize)
|
||||||
|
|
||||||
#if __linux__
|
#if __linux__
|
||||||
static AutoCloseFD fdSavedMountNamespace;
|
static AutoCloseFD fdSavedMountNamespace;
|
||||||
std::optional<Path> savedCwd;
|
static AutoCloseFD fdSavedCwd;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void saveMountNamespace()
|
void saveMountNamespace()
|
||||||
|
@ -1698,11 +1698,15 @@ void saveMountNamespace()
|
||||||
#if __linux__
|
#if __linux__
|
||||||
static std::once_flag done;
|
static std::once_flag done;
|
||||||
std::call_once(done, []() {
|
std::call_once(done, []() {
|
||||||
savedCwd = absPath(".");
|
|
||||||
AutoCloseFD fd = open("/proc/self/ns/mnt", O_RDONLY);
|
AutoCloseFD fd = open("/proc/self/ns/mnt", O_RDONLY);
|
||||||
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
|
||||||
}
|
}
|
||||||
|
@ -1716,7 +1720,7 @@ void restoreMountNamespace()
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
debug(e.msg());
|
debug(e.msg());
|
||||||
}
|
}
|
||||||
if (savedCwd && chdir(savedCwd->c_str()) == -1) {
|
if (fdSavedCwd && fchdir(fdSavedCwd.get()) == -1) {
|
||||||
throw SysError("restoring cwd");
|
throw SysError("restoring cwd");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue