From ce2bfb856987526c2f27fb934b5eedd70d3472d7 Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Netto Date: Thu, 24 Aug 2023 13:42:17 -0300 Subject: linux: Add posix_spawnattr_{get, set}cgroup_np (BZ 26371) These functions allow to posix_spawn and posix_spawnp to use CLONE_INTO_CGROUP with clone3, allowing the child process to be created in a different cgroup version 2. These are GNU extensions that are available only for Linux, and also only for the architectures that implement clone3 wrapper (HAVE_CLONE3_WRAPPER). To create a process on a different cgroupv2, one can use the: posix_spawnattr_t attr; posix_spawnattr_init (&attr); posix_spawnattr_setflags (&attr, POSIX_SPAWN_SETCGROUP); posix_spawnattr_setcgroup_np (&attr, cgroup); posix_spawn (...) Similar to other posix_spawn flags, POSIX_SPAWN_SETCGROUP control whether the cgroup file descriptor will be used or not with clone3. There is no fallback if either clone3 does not support the flag or if the architecture does not provide the clone3 wrapper, in this case posix_spawn returns EOPNOTSUPP. Checked on x86_64-linux-gnu. Reviewed-by: Florian Weimer --- posix/Makefile | 1 + posix/spawn.h | 6 +++++- posix/spawnattr_setflags.c | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) (limited to 'posix') diff --git a/posix/Makefile b/posix/Makefile index 3d368b91f6..70faad4b63 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -37,6 +37,7 @@ headers := \ bits/pthreadtypes-arch.h \ bits/pthreadtypes.h \ bits/sched.h \ + bits/spawn_ext.h \ bits/thread-shared-types.h \ bits/types.h \ bits/types/idtype_t.h \ diff --git a/posix/spawn.h b/posix/spawn.h index 04cc525fa5..731862cc5a 100644 --- a/posix/spawn.h +++ b/posix/spawn.h @@ -34,7 +34,8 @@ typedef struct sigset_t __ss; struct sched_param __sp; int __policy; - int __pad[16]; + int __cgroup; + int __pad[15]; } posix_spawnattr_t; @@ -59,6 +60,7 @@ typedef struct #ifdef __USE_GNU # define POSIX_SPAWN_USEVFORK 0x40 # define POSIX_SPAWN_SETSID 0x80 +# define POSIX_SPAWN_SETCGROUP 0x100 #endif @@ -231,4 +233,6 @@ posix_spawn_file_actions_addtcsetpgrp_np (posix_spawn_file_actions_t *, __END_DECLS +#include + #endif /* spawn.h */ diff --git a/posix/spawnattr_setflags.c b/posix/spawnattr_setflags.c index 97153948e4..e7bb217c6a 100644 --- a/posix/spawnattr_setflags.c +++ b/posix/spawnattr_setflags.c @@ -26,7 +26,8 @@ | POSIX_SPAWN_SETSCHEDPARAM \ | POSIX_SPAWN_SETSCHEDULER \ | POSIX_SPAWN_SETSID \ - | POSIX_SPAWN_USEVFORK) + | POSIX_SPAWN_USEVFORK \ + | POSIX_SPAWN_SETCGROUP) /* Store flags in the attribute structure. */ int -- cgit 1.4.1