about summary refs log tree commit diff
path: root/src/libposixplz/fork_newpid.c
blob: 7887b535866e9123617cba7489d737772009783e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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