summaryrefslogtreecommitdiffstats
path: root/src/test/test-process-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2025-01-13 11:09:49 +0100
committerLennart Poettering <lennart@poettering.net>2025-01-16 11:55:21 +0100
commitd6267b9b18a30c81dd3335230ef71af04e1ea330 (patch)
tree60b96bc3bf489d04ac4b005dc02fecc1db4e5662 /src/test/test-process-util.c
parenttest-process-util: don't run rest of test suite in forked off child (diff)
downloadsystemd-d6267b9b18a30c81dd3335230ef71af04e1ea330.tar.xz
systemd-d6267b9b18a30c81dd3335230ef71af04e1ea330.zip
process-util: port pid_from_same_root_fs() to pidref, and port three places over to it
Diffstat (limited to '')
-rw-r--r--src/test/test-process-util.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c
index 48c5f44e01..166c0fa2c9 100644
--- a/src/test/test-process-util.c
+++ b/src/test/test-process-util.c
@@ -3,6 +3,7 @@
#include <fcntl.h>
#include <linux/oom.h>
#include <pthread.h>
+#include <sys/eventfd.h>
#include <sys/mount.h>
#include <sys/personality.h>
#include <sys/prctl.h>
@@ -1017,6 +1018,53 @@ TEST(pid_get_start_time) {
ASSERT_GE(start_time2, start_time);
}
+TEST(pidref_from_same_root_fs) {
+ int r;
+
+ _cleanup_(pidref_done) PidRef pid1 = PIDREF_NULL, self = PIDREF_NULL;
+
+ ASSERT_OK(pidref_set_self(&self));
+ ASSERT_OK(pidref_set_pid(&pid1, 1));
+
+ ASSERT_OK_POSITIVE(pidref_from_same_root_fs(&self, &self));
+ ASSERT_OK_POSITIVE(pidref_from_same_root_fs(&pid1, &pid1));
+
+ r = pidref_from_same_root_fs(&pid1, &self);
+ if (ERRNO_IS_NEG_PRIVILEGE(r))
+ return (void) log_tests_skipped("skipping pidref_from_same_root_fs() test, lacking privileged.");
+ ASSERT_OK(r);
+ log_info("PID1 and us have the same rootfs: %s", yes_no(r));
+
+ int q = pidref_from_same_root_fs(&self, &pid1);
+ ASSERT_OK(q);
+ ASSERT_EQ(r, q);
+
+ _cleanup_(pidref_done_sigkill_wait) PidRef child1 = PIDREF_NULL;
+ ASSERT_OK(pidref_safe_fork("(child1)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG|FORK_FREEZE, &child1));
+ ASSERT_OK_POSITIVE(pidref_from_same_root_fs(&self, &child1));
+
+ _cleanup_close_ int efd = eventfd(0, EFD_CLOEXEC);
+ ASSERT_OK_ERRNO(efd);
+
+ _cleanup_(pidref_done_sigkill_wait) PidRef child2 = PIDREF_NULL;
+ r = pidref_safe_fork("(child2)", FORK_RESET_SIGNALS|FORK_REOPEN_LOG, &child2);
+ ASSERT_OK(r);
+
+ if (r == 0) {
+ ASSERT_OK_ERRNO(chroot("/usr"));
+ uint64_t u = 1;
+
+ ASSERT_OK_EQ_ERRNO(write(efd, &u, sizeof(u)), (ssize_t) sizeof(u));
+ freeze();
+ }
+
+ uint64_t u;
+ ASSERT_OK_EQ_ERRNO(read(efd, &u, sizeof(u)), (ssize_t) sizeof(u));
+
+ ASSERT_OK_ZERO(pidref_from_same_root_fs(&self, &child2));
+ ASSERT_OK_ZERO(pidref_from_same_root_fs(&child2, &self));
+}
+
static int intro(void) {
log_show_color(true);
return EXIT_SUCCESS;