cgroup-util: drop cgroup v1 support from cg_pid_get_path()

We have dropped cgroup v1 support in v258. Let's drop legacy code.
Then, we can drop 'controller' argument from cg_pid_get_path() and
cg_pidref_get_path().
This commit is contained in:
Yu Watanabe
2025-08-30 06:32:56 +09:00
parent 6475926a59
commit b525a72f7b
18 changed files with 39 additions and 76 deletions

View File

@@ -659,30 +659,14 @@ int cg_remove_xattr(const char *path, const char *name) {
return RET_NERRNO(removexattr(fs, name));
}
int cg_pid_get_path(const char *controller, pid_t pid, char **ret_path) {
int cg_pid_get_path(pid_t pid, char **ret_path) {
_cleanup_fclose_ FILE *f = NULL;
const char *fs, *controller_str = NULL; /* avoid false maybe-uninitialized warning */
int unified, r;
const char *fs;
int r;
assert(pid >= 0);
assert(ret_path);
if (controller) {
if (!cg_controller_is_valid(controller))
return -EINVAL;
} else
controller = SYSTEMD_CGROUP_CONTROLLER;
unified = cg_unified_controller(controller);
if (unified < 0)
return unified;
if (unified == 0) {
if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
controller_str = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
else
controller_str = controller;
}
fs = procfs_file_alloca(pid, "cgroup");
r = fopen_unlocked(fs, "re", &f);
if (r == -ENOENT)
@@ -700,34 +684,13 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **ret_path) {
if (r == 0)
return -ENODATA;
if (unified) {
e = startswith(line, "0:");
if (!e)
continue;
e = startswith(line, "0:");
if (!e)
continue;
e = strchr(e, ':');
if (!e)
continue;
} else {
char *l;
l = strchr(line, ':');
if (!l)
continue;
l++;
e = strchr(l, ':');
if (!e)
continue;
*e = 0;
assert(controller_str);
r = string_contains_word(l, ",", controller_str);
if (r < 0)
return r;
if (r == 0)
continue;
}
e = strchr(e, ':');
if (!e)
continue;
_cleanup_free_ char *path = strdup(e + 1);
if (!path)
@@ -747,7 +710,7 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **ret_path) {
}
}
int cg_pidref_get_path(const char *controller, const PidRef *pidref, char **ret_path) {
int cg_pidref_get_path(const PidRef *pidref, char **ret_path) {
_cleanup_free_ char *path = NULL;
int r;
@@ -762,7 +725,7 @@ int cg_pidref_get_path(const char *controller, const PidRef *pidref, char **ret_
// bit of information from pidfd directly. However, the latter requires privilege and it's
// not entirely clear how to handle cgroups from outer namespace.
r = cg_pid_get_path(controller, pidref->pid, &path);
r = cg_pid_get_path(pidref->pid, &path);
if (r < 0)
return r;
@@ -880,7 +843,7 @@ int cg_get_root_path(char **ret_path) {
assert(ret_path);
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
r = cg_pid_get_path(1, &p);
if (r < 0)
return r;
@@ -922,7 +885,7 @@ int cg_pid_get_path_shifted(pid_t pid, const char *root, char **ret_cgroup) {
assert(pid >= 0);
assert(ret_cgroup);
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
r = cg_pid_get_path(pid, &raw);
if (r < 0)
return r;

View File

@@ -174,8 +174,8 @@ int cg_mangle_path(const char *path, char **ret);
int cg_get_path(const char *controller, const char *path, const char *suffix, char **ret);
int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **ret);
int cg_pid_get_path(const char *controller, pid_t pid, char **ret);
int cg_pidref_get_path(const char *controller, const PidRef *pidref, char **ret);
int cg_pid_get_path(pid_t pid, char **ret);
int cg_pidref_get_path(const PidRef *pidref, char **ret);
int cg_is_threaded(const char *path);

View File

@@ -3260,7 +3260,7 @@ int manager_setup_cgroup(Manager *m) {
/* 1. Determine hierarchy */
m->cgroup_root = mfree(m->cgroup_root);
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &m->cgroup_root);
r = cg_pid_get_path(0, &m->cgroup_root);
if (r < 0)
return log_error_errno(r, "Cannot determine cgroup we are running in: %m");
@@ -3404,7 +3404,7 @@ Unit* manager_get_unit_by_pidref_cgroup(Manager *m, const PidRef *pid) {
assert(m);
if (cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup) < 0)
if (cg_pidref_get_path(pid, &cgroup) < 0)
return NULL;
return manager_get_unit_by_cgroup(m, cgroup);

View File

@@ -1345,7 +1345,7 @@ static int append_process(sd_bus_message *reply, const char *p, PidRef *pid, Set
return r;
if (!p) {
r = cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &buf);
r = cg_pidref_get_path(pid, &buf);
if (r == -ESRCH)
return 0;
if (r < 0)

View File

@@ -132,7 +132,7 @@ static int can_forward_coredump(PidRef *pidref, PidRef *leader) {
}
_cleanup_free_ char *cgroup = NULL;
r = cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, leader, &cgroup);
r = cg_pidref_get_path(leader, &cgroup);
if (r < 0)
return log_debug_errno(r, "Failed to get cgroup of the leader process, ignoring: %m");

View File

@@ -1096,7 +1096,7 @@ int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, PidRef *pidref, pid_t tid
if (missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_USER_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID)) {
if (!c->cgroup) {
r = cg_pid_get_path(NULL, pidref->pid, &c->cgroup);
r = cg_pid_get_path(pidref->pid, &c->cgroup);
if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r))
return r;
}

View File

@@ -1972,7 +1972,7 @@ _public_ int sd_event_add_memory_pressure(
* not delegated to us, or PSI simply not available in the kernel). */
_cleanup_free_ char *cg = NULL;
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cg);
r = cg_pid_get_path(0, &cg);
if (r < 0)
return r;

View File

@@ -105,7 +105,7 @@ _public_ int sd_pid_get_cgroup(pid_t pid, char **ret_cgroup) {
assert_return(pid >= 0, -EINVAL);
_cleanup_free_ char *c = NULL;
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
r = cg_pid_get_path(pid, &c);
if (r < 0)
return r;
@@ -418,7 +418,7 @@ _public_ int sd_peer_get_cgroup(int fd, char **ret_cgroup) {
return r;
_cleanup_free_ char *c = NULL;
r = cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, &pidref, &c);
r = cg_pidref_get_path(&pidref, &c);
if (r < 0)
return r;

View File

@@ -722,7 +722,7 @@ static int method_register_machine(sd_bus_message *message, void *userdata, sd_b
/* If this is not a top-level cgroup, then we need the cgroup path to be able to watch when
* it empties */
r = cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, &m->leader, &m->cgroup);
r = cg_pidref_get_path(&m->leader, &m->cgroup);
if (r < 0) {
r = sd_bus_error_set_errnof(error, r,
"Failed to determine cgroup of process "PID_FMT" : %m",

View File

@@ -109,7 +109,7 @@ int manager_install_sysctl_monitor(Manager *manager) {
if (r < 0)
return log_warning_errno(r, "Failed to load libbpf, not installing sysctl monitor: %m");
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup);
r = cg_pid_get_path(0, &cgroup);
if (r < 0)
return log_warning_errno(r, "Failed to get cgroup path, ignoring: %m.");

View File

@@ -75,9 +75,9 @@ int create_subcgroup(
return log_error_errno(r, "Failed to determine supported controllers: %m");
if (keep_unit)
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup);
r = cg_pid_get_path(0, &cgroup);
else
r = cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup);
r = cg_pidref_get_path(pid, &cgroup);
if (r < 0)
return log_error_errno(r, "Failed to get our control group: %m");
@@ -172,7 +172,7 @@ int bind_mount_cgroup_hierarchy(void) {
/* NB: This must be called from the inner child, with /sys/fs/cgroup/ being a bind mount in mountns! */
r = cg_pid_get_path(NULL, 0, &own_cgroup_path);
r = cg_pid_get_path(0, &own_cgroup_path);
if (r < 0)
return log_error_errno(r, "Failed to determine our own cgroup path: %m");

View File

@@ -349,7 +349,7 @@ static int show_extra_pids(
for (i = 0, j = 0; i < n_pids; i++) {
_cleanup_free_ char *k = NULL;
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pids[i], &k);
r = cg_pid_get_path(pids[i], &k);
if (r < 0)
return r;

View File

@@ -1091,7 +1091,7 @@ static int condition_test_psi(Condition *c, char **env) {
return log_debug_errno(r, "Cannot determine slice \"%s\" cgroup path: %m", slice);
/* We might be running under the user manager, so get the root path and prefix it accordingly. */
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &root_scope);
r = cg_pid_get_path(getpid_cached(), &root_scope);
if (r < 0)
return log_debug_errno(r, "Failed to get root cgroup path: %m");

View File

@@ -53,7 +53,7 @@ static bool is_in_survivor_cgroup(const PidRef *pid) {
assert(pidref_is_set(pid));
r = cg_pidref_get_path(/* controller= */ NULL, pid, &cgroup_path);
r = cg_pidref_get_path(pid, &cgroup_path);
if (r == -EUNATCH) {
log_warning_errno(r, "Process " PID_FMT " appears to originate in foreign namespace, ignoring.", pid->pid);
return true;

View File

@@ -213,7 +213,7 @@ static int allocate_scope(void) {
/* Let's try to run this test in a scope of its own, with delegation turned on, so that PID 1 doesn't
* interfere with our cgroup management. */
if (cg_pid_get_path(NULL, 0, &cgroup_root) >= 0 && cg_is_delegated(cgroup_root) && stderr_is_journal()) {
if (cg_pid_get_path(0, &cgroup_root) >= 0 && cg_is_delegated(cgroup_root) && stderr_is_journal()) {
log_debug("Already running as a unit with delegated cgroup, not allocating a cgroup subroot.");
return 0;
}
@@ -297,9 +297,9 @@ static int enter_cgroup(char **ret_cgroup, bool enter_subroot) {
if (r < 0)
log_warning_errno(r, "Couldn't allocate a scope unit for this test, proceeding without.");
r = cg_pid_get_path(NULL, 0, &cgroup_root);
r = cg_pid_get_path(0, &cgroup_root);
if (IN_SET(r, -ENOMEDIUM, -ENOENT))
return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m");
return log_warning_errno(r, "cg_pid_get_path(0, ...) failed: %m");
ASSERT_OK(r);
if (enter_subroot)

View File

@@ -38,19 +38,19 @@ TEST(cg_create) {
ASSERT_OK_EQ(cg_create(test_c), 1);
ASSERT_OK_ZERO(cg_create_and_attach(test_b, 0));
ASSERT_OK_ZERO(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path));
ASSERT_OK_ZERO(cg_pid_get_path(getpid_cached(), &path));
ASSERT_STREQ(path, test_b);
free(path);
ASSERT_OK_ZERO(cg_attach(test_a, 0));
ASSERT_OK_ZERO(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path));
ASSERT_OK_ZERO(cg_pid_get_path(getpid_cached(), &path));
ASSERT_TRUE(path_equal(path, test_a));
free(path);
ASSERT_OK_EQ(cg_create_and_attach(test_d, 0), 1);
ASSERT_OK_ZERO(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path));
ASSERT_OK_ZERO(cg_pid_get_path(getpid_cached(), &path));
ASSERT_TRUE(path_equal(path, test_d));
free(path);

View File

@@ -252,7 +252,7 @@ TEST(proc, .sd_booted = true) {
if (pidref_is_kernel_thread(&pid) != 0)
continue;
r = cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, &pid, &path);
r = cg_pidref_get_path(&pid, &path);
if (r == -ESRCH)
continue;
ASSERT_OK(r);

View File

@@ -1452,7 +1452,7 @@ int manager_main(Manager *manager) {
assert(manager);
_cleanup_free_ char *cgroup = NULL;
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup);
r = cg_pid_get_path(0, &cgroup);
if (r < 0)
log_debug_errno(r, "Failed to get cgroup, ignoring: %m");
else if (endswith(cgroup, "/udev")) { /* If we are in a subcgroup /udev/ we assume it was delegated to us */