glibc/posix/spawnattr_setflags.c
Adhemerval Zanella Netto ce2bfb8569 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>
2023-09-05 13:08:48 -03:00

46 lines
1.5 KiB
C

/* Copyright (C) 2000-2023 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <spawn.h>
#include <string.h>
#define ALL_FLAGS (POSIX_SPAWN_RESETIDS \
| POSIX_SPAWN_SETPGROUP \
| POSIX_SPAWN_SETSIGDEF \
| POSIX_SPAWN_SETSIGMASK \
| POSIX_SPAWN_SETSCHEDPARAM \
| POSIX_SPAWN_SETSCHEDULER \
| POSIX_SPAWN_SETSID \
| POSIX_SPAWN_USEVFORK \
| POSIX_SPAWN_SETCGROUP)
/* Store flags in the attribute structure. */
int
__posix_spawnattr_setflags (posix_spawnattr_t *attr, short int flags)
{
/* Check no invalid bits are set. */
if (flags & ~ALL_FLAGS)
return EINVAL;
/* Store the flag word. */
attr->__flags = flags;
return 0;
}
weak_alias (__posix_spawnattr_setflags, posix_spawnattr_setflags)