Skip to content

Commit 98f5798

Browse files
DaanDeMeyerbluca
authored andcommitted
cgroup-util: Don't try to open pidfd for pids from cgroup.threads
Opening pidfds for non thread group leaders only works from 6.9 onwards with PIDFD_THREAD. On older kernels or without PIDFD_THREAD pidfd_open() fails with EINVAL. Since we might read non thread group leader IDs from cgroup.threads, we introduce and set CGROUP_NO_PIDFD to avoid trying open pidfd's for them and instead use the pid as is. (cherry picked from commit 8783355)
1 parent 6687731 commit 98f5798

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/basic/cgroup-util.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ int cg_read_pidref(FILE *f, PidRef *ret, CGroupFlags flags) {
116116
if (pid == 0)
117117
return -EREMOTE;
118118

119+
if (FLAGS_SET(flags, CGROUP_NO_PIDFD)) {
120+
*ret = PIDREF_MAKE_FROM_PID(pid);
121+
return 1;
122+
}
123+
119124
r = pidref_set_pid(ret, pid);
120125
if (r >= 0)
121126
return 1;
@@ -331,7 +336,7 @@ static int cg_kill_items(
331336
for (;;) {
332337
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
333338

334-
r = cg_read_pidref(f, &pidref, /* flags = */ 0);
339+
r = cg_read_pidref(f, &pidref, flags);
335340
if (r < 0)
336341
return RET_GATHER(ret, r);
337342
if (r == 0)
@@ -402,7 +407,11 @@ int cg_kill(
402407
if (r == 0)
403408
return ret;
404409

405-
r = cg_kill_items(path, sig, flags, s, log_kill, userdata, "cgroup.threads");
410+
/* Opening pidfds for non thread group leaders only works from 6.9 onwards with PIDFD_THREAD. On
411+
* older kernels or without PIDFD_THREAD pidfd_open() fails with EINVAL. Since we might read non
412+
* thread group leader IDs from cgroup.threads, we set CGROUP_NO_PIDFD to avoid trying open pidfd's
413+
* for them and instead use the regular pid. */
414+
r = cg_kill_items(path, sig, flags|CGROUP_NO_PIDFD, s, log_kill, userdata, "cgroup.threads");
406415
if (r < 0)
407416
return r;
408417

src/basic/cgroup-util.h

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ typedef enum CGroupFlags {
185185
CGROUP_IGNORE_SELF = 1 << 1,
186186
CGROUP_REMOVE = 1 << 2,
187187
CGROUP_DONT_SKIP_UNMAPPED = 1 << 3,
188+
CGROUP_NO_PIDFD = 1 << 4,
188189
} CGroupFlags;
189190

190191
int cg_enumerate_processes(const char *controller, const char *path, FILE **ret);

0 commit comments

Comments
 (0)