Skip to content

Commit 2116700

Browse files
YHNdnzjbluca
authored andcommitted
core/cgroup: avoid one unnecessary strjoina()
(cherry picked from commit 42aee39) (cherry picked from commit 80acea4) (cherry picked from commit b5fd146)
1 parent 0c05050 commit 2116700

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

src/core/cgroup.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2963,31 +2963,27 @@ static int unit_update_cgroup(
29632963
return 0;
29642964
}
29652965

2966-
static int unit_attach_pid_to_cgroup_via_bus(Unit *u, pid_t pid, const char *suffix_path) {
2966+
static int unit_attach_pid_to_cgroup_via_bus(Unit *u, const char *cgroup_path, pid_t pid) {
29672967
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2968-
char *pp;
29692968
int r;
29702969

29712970
assert(u);
2971+
assert(cgroup_path);
2972+
assert(pid_is_valid(pid));
29722973

29732974
if (MANAGER_IS_SYSTEM(u->manager))
29742975
return -EINVAL;
29752976

29762977
if (!u->manager->system_bus)
29772978
return -EIO;
29782979

2979-
CGroupRuntime *crt = unit_get_cgroup_runtime(u);
2980-
if (!crt || !crt->cgroup_path)
2981-
return -EOWNERDEAD;
2982-
29832980
/* Determine this unit's cgroup path relative to our cgroup root */
2984-
pp = path_startswith(crt->cgroup_path, u->manager->cgroup_root);
2981+
const char *pp = path_startswith_full(cgroup_path,
2982+
u->manager->cgroup_root,
2983+
PATH_STARTSWITH_RETURN_LEADING_SLASH|PATH_STARTSWITH_REFUSE_DOT_DOT);
29852984
if (!pp)
29862985
return -EINVAL;
29872986

2988-
pp = strjoina("/", pp, suffix_path);
2989-
path_simplify(pp);
2990-
29912987
r = bus_call_method(u->manager->system_bus,
29922988
bus_systemd_mgr,
29932989
"AttachProcessesToUnit",
@@ -3028,8 +3024,10 @@ int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path) {
30283024
CGroupRuntime *crt = ASSERT_PTR(unit_get_cgroup_runtime(u));
30293025

30303026
if (isempty(suffix_path))
3031-
p = crt->cgroup_path;
3027+
p = empty_to_root(crt->cgroup_path);
30323028
else {
3029+
assert(path_is_absolute(suffix_path));
3030+
30333031
joined = path_join(crt->cgroup_path, suffix_path);
30343032
if (!joined)
30353033
return -ENOMEM;
@@ -3047,7 +3045,7 @@ int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path) {
30473045
* before we use it */
30483046
r = pidref_verify(pid);
30493047
if (r < 0) {
3050-
log_unit_info_errno(u, r, "PID " PID_FMT " vanished before we could move it to target cgroup '%s', skipping: %m", pid->pid, empty_to_root(p));
3048+
log_unit_info_errno(u, r, "PID " PID_FMT " vanished before we could move it to target cgroup '%s', skipping: %m", pid->pid, p);
30513049
continue;
30523050
}
30533051

@@ -3058,7 +3056,7 @@ int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path) {
30583056

30593057
log_unit_full_errno(u, again ? LOG_DEBUG : LOG_INFO, r,
30603058
"Couldn't move process "PID_FMT" to%s requested cgroup '%s': %m",
3061-
pid->pid, again ? " directly" : "", empty_to_root(p));
3059+
pid->pid, again ? " directly" : "", p);
30623060

30633061
if (again) {
30643062
int z;
@@ -3068,9 +3066,9 @@ int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path) {
30683066
* Since it's more privileged it might be able to move the process across the
30693067
* leaves of a subtree whose top node is not owned by us. */
30703068

3071-
z = unit_attach_pid_to_cgroup_via_bus(u, pid->pid, suffix_path);
3069+
z = unit_attach_pid_to_cgroup_via_bus(u, p, pid->pid);
30723070
if (z < 0)
3073-
log_unit_info_errno(u, z, "Couldn't move process "PID_FMT" to requested cgroup '%s' (directly or via the system bus): %m", pid->pid, empty_to_root(p));
3071+
log_unit_info_errno(u, z, "Couldn't move process "PID_FMT" to requested cgroup '%s' (directly or via the system bus): %m", pid->pid, p);
30743072
else {
30753073
if (ret >= 0)
30763074
ret++; /* Count successful additions */

0 commit comments

Comments
 (0)