aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/namespaces.cc
diff options
context:
space:
mode:
authoreldritch horrors <pennae@lix.systems>2024-04-05 21:02:04 +0200
committereldritch horrors <pennae@lix.systems>2024-06-23 11:52:49 +0000
commit3d155fc509e19354ba3798b1cc1b9cbcdb789c85 (patch)
treedc3dd72cd77e02e1aeb25d75c6e01bdf34041b28 /src/libutil/namespaces.cc
parentb43a2e84c4b2fa7cb1167693652702e6dac95f53 (diff)
libutil: give Pid proper resource semantics
copy-constructing or assigning from pid_t can easily lead to duplicate Pid instances for the same process if a pid_t was used carelessly, and Pid itself was copy-constructible. both could cause surprising results such as killing processes twice (which could become very problemantic, but luckily modern systems don't reuse PIDs all that quickly), or more than one piece of the code believing it owns a process when neither do Change-Id: Ifea7445f84200b34c1a1d0acc2cdffe0f01e20c6
Diffstat (limited to 'src/libutil/namespaces.cc')
-rw-r--r--src/libutil/namespaces.cc12
1 files changed, 3 insertions, 9 deletions
diff --git a/src/libutil/namespaces.cc b/src/libutil/namespaces.cc
index 98d3cd306..4b7a1b577 100644
--- a/src/libutil/namespaces.cc
+++ b/src/libutil/namespaces.cc
@@ -94,12 +94,7 @@ bool userNamespacesSupported()
static auto res = [&]() -> bool
{
try {
- Pid pid = startProcess([&]()
- {
- _exit(0);
- }, {
- .cloneFlags = CLONE_NEWUSER
- });
+ Pid pid{startProcess([&]() { _exit(0); }, {.cloneFlags = CLONE_NEWUSER})};
auto r = pid.wait();
assert(!r);
@@ -120,8 +115,7 @@ bool mountAndPidNamespacesSupported()
{
try {
- Pid pid = startProcess([&]()
- {
+ Pid pid{startProcess([&]() {
/* Make sure we don't remount the parent's /proc. */
if (mount(0, "/", 0, MS_PRIVATE | MS_REC, 0) == -1)
_exit(1);
@@ -136,7 +130,7 @@ bool mountAndPidNamespacesSupported()
_exit(0);
}, {
.cloneFlags = CLONE_NEWNS | CLONE_NEWPID | (userNamespacesSupported() ? CLONE_NEWUSER : 0)
- });
+ })};
if (pid.wait()) {
debug("PID namespaces do not work on this system: cannot remount /proc");