s6_clone_newpid.patch.old (3279B)
1 from https://www.mail-archive.com/skaware@list.skarnet.org/msg01006.html 2 3 diff --git a/src/supervision/s6-supervise.c 4 b/src/supervision/s6-supervise.c index 2e8fa38..7605a82 100644 5 --- a/src/supervision/s6-supervise.c 6 +++ b/src/supervision/s6-supervise.c 7 @@ -9,6 +9,9 @@ 8 #include <errno.h> 9 #include <fcntl.h> 10 #include <signal.h> 11 +#ifdef WANT_CLONE_NEWPID 12 +# include <sched.h> 13 +#endif 14 #include <skalibs/allreadwrite.h> 15 #include <skalibs/bytestr.h> 16 #include <skalibs/uint.h> 17 @@ -203,6 +206,67 @@ static int maybesetsid (void) 18 return 1 ; 19 } 20 21 + static void exec_run(int p[2], int notifyp[2], int fd) gccattr_noreturn ; 22 + static void exec_run(int p[2], int notifyp[2], int fd) 23 + { 24 + char const *cargv[2] = { "run", 0 } ; 25 + PROG = "s6-supervise (child)" ; 26 + selfpipe_finish() ; 27 + if (notifyp[0] >= 0) close(notifyp[0]) ; 28 + close(p[0]) ; 29 + if (notifyp[1] >= 0 && fd_move(fd, notifyp[1]) < 0) 30 + { 31 + failcoe(p[1]) ; 32 + strerr_diefu1sys(127, "move notification descriptor") ; 33 + } 34 + if (!maybesetsid()) 35 + { 36 + failcoe(p[1]) ; 37 + strerr_diefu1sys(127, "access ./nosetsid") ; 38 + } 39 + execve("./run", (char *const *)cargv, (char *const *)environ) ; 40 + failcoe(p[1]) ; 41 + strerr_dieexec(127, "run") ; 42 +} 43 + 44 +static pid_t spawn_run_fork(int p[2], int notifyp[2], int fd) 45 +{ 46 + pid_t pid = fork() ; 47 + if (!pid) exec_run(p, notifyp, fd) ; 48 + return pid ; 49 +} 50 + 51 +#ifdef WANT_CLONE_NEWPID 52 +typedef struct 53 +{ 54 + int p[2] ; 55 + int notifyp[2] ; 56 + int fd ; 57 +} exec_run_t ; 58 + 59 +static int exec_run_shim(void *ctx) gccattr_noreturn ; 60 +static int exec_run_shim(void *ctx) 61 +{ 62 + exec_run_t *er = (exec_run_t *) ctx ; 63 + exec_run(er->p, er->notifyp, er->fd) ; 64 +} 65 + 66 +static pid_t spawn_run(int p[2], int notifyp[2], int fd) 67 +{ 68 + exec_run_t arg = { { p[0], p[1] }, { notifyp[0], notifyp[1] }, fd } ; 69 + char child_stack[SIGSTKSZ] ; 70 + if (access("clone-newpid", F_OK) < 0 && errno == ENOENT) 71 + return spawn_run_fork(p, notifyp, fd) ; 72 + return (pid_t) clone(&exec_run_shim, child_stack + sizeof(child_stack), 73 + CLONE_NEWPID | SIGCHLD, &arg) ; 74 +} 75 +#else /* if !defined(WANT_CLONE_NEWPID) */ 76 +static pid_t spawn_run(int p[2], int notifyp[2], int fd) 77 +{ 78 + return spawn_run_fork(p, notifyp, fd) ; 79 +} 80 +#endif /* defined(WANT_CLONE_NEWPID) */ 81 + 82 static void trystart (void) 83 { 84 int p[2] ; 85 @@ -222,7 +286,7 @@ static void trystart (void) 86 fd_close(p[1]) ; fd_close(p[0]) ; 87 return ; 88 } 89 - pid = fork() ; 90 + pid = spawn_run(p, notifyp, (int)fd) ; 91 if (pid < 0) 92 { 93 settimeout(60) ; 94 @@ -232,27 +296,6 @@ static void trystart (void) 95 fd_close(p[1]) ; fd_close(p[0]) ; 96 return ; 97 } 98 - else if (!pid) 99 - { 100 - char const *cargv[2] = { "run", 0 } ; 101 - PROG = "s6-supervise (child)" ; 102 - selfpipe_finish() ; 103 - if (notifyp[0] >= 0) close(notifyp[0]) ; 104 - close(p[0]) ; 105 - if (notifyp[1] >= 0 && fd_move((int)fd, notifyp[1]) < 0) 106 - { 107 - failcoe(p[1]) ; 108 - strerr_diefu1sys(127, "move notification descriptor") ; 109 - } 110 - if (!maybesetsid()) 111 - { 112 - failcoe(p[1]) ; 113 - strerr_diefu1sys(127, "access ./nosetsid") ; 114 - } 115 - execve("./run", (char *const *)cargv, (char *const *)environ) ; 116 - failcoe(p[1]) ; 117 - strerr_dieexec(127, "run") ; 118 - } 119 if (notifyp[1] >= 0) fd_close(notifyp[1]) ; 120 fd_close(p[1]) ; 121 {