test-cgroup-util: allow ESRCH in cg_pidref_get_path() and friends

As the process may be already dead.

Follow-ups for ca82f0cbe2.
This commit is contained in:
Yu Watanabe
2025-03-15 10:51:28 +09:00
parent d9000d708c
commit e92d699dde
+34 -21
View File
@@ -216,44 +216,57 @@ TEST(proc, .sd_booted = true) {
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
uid_t uid = UID_INVALID;
r = proc_dir_read_pidref(d, &pid);
assert_se(r >= 0);
ASSERT_OK(r = proc_dir_read_pidref(d, &pid));
if (r == 0)
break;
if (pidref_is_kernel_thread(&pid) != 0)
continue;
ASSERT_OK_ZERO(cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, &pid, &path));
r = cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, &pid, &path);
if (r == -ESRCH)
continue;
ASSERT_OK(r);
/* Test may run in a container with supervising/monitor processes that don't belong to our
* cgroup tree (slices/leaves) */
if (hidden_cgroup(path))
continue;
ASSERT_OK_ZERO(cg_pid_get_path_shifted(pid.pid, NULL, &path_shifted));
ASSERT_OK_ZERO(cg_pidref_get_unit(&pid, &unit));
ASSERT_OK_ZERO(cg_pid_get_slice(pid.pid, &slice));
r = cg_pid_get_path_shifted(pid.pid, NULL, &path_shifted);
if (r != -ESRCH)
ASSERT_OK(r);
r = cg_pidref_get_unit(&pid, &unit);
if (r != -ESRCH)
ASSERT_OK(r);
r = cg_pid_get_slice(pid.pid, &slice);
if (r != -ESRCH)
ASSERT_OK(r);
/* Not all processes belong to a specific user or a machine */
r = cg_pidref_get_owner_uid(&pid, &uid);
ASSERT_TRUE(r == 0 || r == -ENXIO);
if (!IN_SET(r, -ESRCH, -ENXIO))
ASSERT_OK(r);
r = cg_pidref_get_session(&pid, &session);
ASSERT_TRUE(r == 0 || r == -ENXIO);
if (!IN_SET(r, -ESRCH, -ENXIO))
ASSERT_OK(r);
r = cg_pid_get_user_unit(pid.pid, &user_unit);
ASSERT_TRUE(r == 0 || r == -ENXIO);
if (!IN_SET(r, -ESRCH, -ENXIO))
ASSERT_OK(r);
r = cg_pid_get_machine_name(pid.pid, &machine);
ASSERT_TRUE(r == 0 || r == -ENOENT);
if (!IN_SET(r, -ESRCH, -ENOENT))
ASSERT_OK(r);
printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n",
pid.pid,
path,
path_shifted,
uid,
session,
unit,
user_unit,
machine,
slice);
log_debug(PID_FMT": %s, %s, "UID_FMT", %s, %s, %s, %s, %s",
pid.pid,
path,
strna(path_shifted),
uid,
strna(session),
strna(unit),
strna(user_unit),
strna(machine),
strna(slice));
}
}