1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-12-24 17:51:17 +03:00

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 <fweimer@redhat.com>
This commit is contained in:
Adhemerval Zanella Netto
2023-08-24 13:42:17 -03:00
committed by Adhemerval Zanella
parent ad77b1bcca
commit ce2bfb8569
46 changed files with 449 additions and 5 deletions

View File

@@ -380,14 +380,19 @@ __spawnix (pid_t * pid, const char *file,
need for CLONE_SETTLS. Although parent and child share the same TLS
namespace, there will be no concurrent access for TLS variables (errno
for instance). */
bool set_cgroup = attrp ? (attrp->__flags & POSIX_SPAWN_SETCGROUP) : false;
struct clone_args clone_args =
{
/* Unsupported flags like CLONE_CLEAR_SIGHAND will be cleared up by
__clone_internal_fallback. */
.flags = CLONE_CLEAR_SIGHAND | CLONE_VM | CLONE_VFORK,
.flags = (set_cgroup ? CLONE_INTO_CGROUP : 0)
| CLONE_CLEAR_SIGHAND
| CLONE_VM
| CLONE_VFORK,
.exit_signal = SIGCHLD,
.stack = (uintptr_t) stack,
.stack_size = stack_size,
.cgroup = (set_cgroup ? attrp->__cgroup : 0)
};
#ifdef HAVE_CLONE3_WRAPPER
args.use_clone3 = true;
@@ -398,8 +403,19 @@ __spawnix (pid_t * pid, const char *file,
#endif
{
args.use_clone3 = false;
new_pid = __clone_internal_fallback (&clone_args, __spawni_child,
&args);
if (!set_cgroup)
new_pid = __clone_internal_fallback (&clone_args, __spawni_child,
&args);
else
{
/* No fallback for POSIX_SPAWN_SETCGROUP if clone3 is not
supported. */
new_pid = -1;
#ifdef HAVE_CLONE3_WRAPPER
if (errno == ENOSYS)
#endif
errno = ENOTSUP;
}
}
/* It needs to collect the case where the auxiliary process was created