summaryrefslogtreecommitdiffstats
path: root/src/basic/process-util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/basic/process-util.c')
-rw-r--r--src/basic/process-util.c26
1 files changed, 21 insertions, 5 deletions
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index c9d968d..de5a146 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -2066,9 +2066,10 @@ int posix_spawn_wrapper(
_unused_ _cleanup_(posix_spawnattr_destroyp) posix_spawnattr_t *attr_destructor = &attr;
#if HAVE_PIDFD_SPAWN
+ static bool setcgroup_supported = true;
_cleanup_close_ int cgroup_fd = -EBADF;
- if (cgroup) {
+ if (cgroup && setcgroup_supported) {
_cleanup_free_ char *resolved_cgroup = NULL;
r = cg_get_path_and_check(
@@ -2102,6 +2103,19 @@ int posix_spawn_wrapper(
_cleanup_close_ int pidfd = -EBADF;
r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
+ if (r == E2BIG && FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP)) {
+ /* Some kernels (e.g., 5.4) support clone3 but they do not support CLONE_INTO_CGROUP.
+ * Retry pidfd_spawn() after removing the flag. */
+ flags &= ~POSIX_SPAWN_SETCGROUP;
+ r = posix_spawnattr_setflags(&attr, flags);
+ if (r != 0)
+ return -r;
+ r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
+ /* if pidfd_spawn was successful after removing SPAWN_CGROUP,
+ * mark setcgroup_supported as false so that we do not retry every time */
+ if (r == 0)
+ setcgroup_supported = false;
+ }
if (r == 0) {
r = pidref_set_pidfd_consume(ret_pidref, TAKE_FD(pidfd));
if (r < 0)
@@ -2120,10 +2134,12 @@ int posix_spawn_wrapper(
/* Compiled on a newer host, or seccomp&friends blocking clone3()? Fallback, but need to change the
* flags to remove the cgroup one, which is what redirects to clone3() */
- flags &= ~POSIX_SPAWN_SETCGROUP;
- r = posix_spawnattr_setflags(&attr, flags);
- if (r != 0)
- return -r;
+ if (FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP)) {
+ flags &= ~POSIX_SPAWN_SETCGROUP;
+ r = posix_spawnattr_setflags(&attr, flags);
+ if (r != 0)
+ return -r;
+ }
#endif
pid_t pid;