diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2024-09-12 11:53:13 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2024-09-12 11:53:13 +0000 |
commit | f867414f4e1f8115d1c9796f8f91677673739368 (patch) | |
tree | e350efd8dff46f8f4adfc758d9f4bb292b0622ec /src/libposixplz/fork_newpid.c | |
parent | a7973043c92dc0f9d083e600142a47ce6dd1c9f6 (diff) | |
download | skalibs-f867414f4e1f8115d1c9796f8f91677673739368.tar.xz |
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libposixplz/fork_newpid.c')
-rw-r--r-- | src/libposixplz/fork_newpid.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/libposixplz/fork_newpid.c b/src/libposixplz/fork_newpid.c new file mode 100644 index 0000000..7887b53 --- /dev/null +++ b/src/libposixplz/fork_newpid.c @@ -0,0 +1,67 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> + +#ifdef SKALIBS_HASCLONENEWPID + +#include <skalibs/nonposix.h> + +#include <sys/syscall.h> +#include <stdint.h> +#include <unistd.h> +#include <signal.h> +#include <sched.h> +#include <errno.h> + +#include <skalibs/uint64.h> + +struct clone_args +{ + uint64_t flags ; + uint64_t pidfd ; + uint64_t child_tid ; + uint64_t parent_tid ; + uint64_t exit_signal ; + uint64_t stack ; + uint64_t stack_size ; + uint64_t tls ; + uint64_t set_tid ; + uint64_t set_tid_size ; + uint64_t cgroup ; +} ; + +pid_t fork_newpid (void) +{ + pid_t pid ; + pid_t settid = 1 ; + struct clone_args args = + { + .flags = CLONE_NEWPID | CLONE_PARENT_SETTID, + .pidfd = 0, + .child_tid = 0, + .parent_tid = (uint64_t)&pid, + .exit_signal = SIGCHLD, + .stack = 0, + .stack_size = 0, + .tls = 0, + .set_tid = (uint64_t)&settid, + .set_tid_size = 1, + .cgroup = 0 + } ; + long r = syscall(SYS_clone3, &args, sizeof(args)) ; + if (r < 0) return (errno = -r, -1) ; + return r ? pid : 0 ; +} + +#else + +#include <unistd.h> +#include <errno.h> + +pid_t fork_newpid (void) +{ + errno = ENOSYS ; + return -1 ; +} + +#endif |