s6_clone3_newpid.patch (1926B)
1 diff --git a/src/supervision/s6-supervise.c b/src/supervision/s6-supervise.c 2 index cc6779a..8b7e56b 100644 3 --- a/src/supervision/s6-supervise.c 4 +++ b/src/supervision/s6-supervise.c 5 @@ -12,6 +12,14 @@ 6 #include <fcntl.h> 7 #include <sys/stat.h> 8 #include <sys/wait.h> 9 +#ifdef WANT_CLONE_NEWPID 10 +# include <syscall.h> /* For calling clone3 syscall (currently not in libc) */ 11 +# include <linux/types.h> /* For vendored definition of struct clone_args */ 12 +#if 0 13 +# include <linux/sched.h> /* Definition of struct clone_args */ 14 +#endif 15 +# include <sched.h> /* Definition of CLONE_* constants */ 16 +#endif 17 18 #include <skalibs/allreadwrite.h> 19 #include <skalibs/bytestr.h> 20 @@ -231,6 +239,50 @@ static void failcoe (int fd) 21 errno = e ; 22 } 23 24 +#ifdef WANT_CLONE_NEWPID 25 +pid_t newpid_fork(void) 26 +{ 27 + struct clone_args { 28 + __aligned_u64 flags; 29 + __aligned_u64 pidfd; 30 + __aligned_u64 child_tid; 31 + __aligned_u64 parent_tid; 32 + __aligned_u64 exit_signal; 33 + __aligned_u64 stack; 34 + __aligned_u64 stack_size; 35 + __aligned_u64 tls; 36 + __aligned_u64 set_tid; 37 + __aligned_u64 set_tid_size; 38 + __aligned_u64 cgroup; 39 + } args = { 40 + .flags = CLONE_NEWPID, 41 + .exit_signal = SIGCHLD, 42 + }; 43 + 44 + return syscall(__NR_clone3, &args, sizeof(args)); 45 +} 46 + 47 +pid_t conditional_newpid_fork(void) 48 +{ 49 + if (access("clone-newpid", F_OK) < 0) 50 + { 51 + if(errno == ENOENT) { 52 + return fork(); 53 + } 54 + else 55 + { 56 + return -1; /* propagate the filesystem access error upwards */ 57 + } 58 + } 59 + else 60 + { 61 + return newpid_fork(); 62 + } 63 +} 64 +#else 65 +#define conditional_newpid_fork fork 66 +#endif /* defined(WANT_CLONE_NEWPID) */ 67 + 68 static void trystart (void) 69 { 70 int p[2] ; 71 @@ -296,7 +348,7 @@ static void trystart (void) 72 strerr_warnwu2sys("pipe", " (waiting 60 seconds)") ; 73 goto errn ; 74 } 75 - pid = fork() ; 76 + pid = conditional_newpid_fork() ; 77 if (pid < 0) 78 { 79 settimeout(60) ;