s6

Mirror/fork of https://skarnet.org/software/s6/
git clone https://ccx.te2000.cz/git/s6
Log | Files | Refs | README | LICENSE

s6-fghack.c (1078B)


      1 /* ISC license. */
      2 
      3 #include <unistd.h>
      4 #include <errno.h>
      5 #include <sys/wait.h>
      6 
      7 #include <skalibs/strerr.h>
      8 #include <skalibs/allreadwrite.h>
      9 #include <skalibs/djbunix.h>
     10 #include <skalibs/cspawn.h>
     11 
     12 #define USAGE "s6-fghack prog..."
     13 
     14 #define N 30
     15 
     16 int main (int argc, char const *const *argv, char const *const *envp)
     17 {
     18   int p[2] ;
     19   int fds[N] ;
     20   pid_t pid ;
     21   cspawn_fileaction fa = { .type = CSPAWN_FA_CLOSE } ;
     22   char c ;
     23 
     24   PROG = "s6-fghack" ;
     25   if (argc < 2) strerr_dieusage(100, USAGE) ;
     26   if (pipe(p) == -1) strerr_diefu1sys(111, "create hackpipe") ;
     27   for (size_t i = 0 ; i < N ; i++)
     28     fds[i] = dup(p[1]) ;
     29   fa.x.fd = p[0] ;
     30   pid = cspawn(argv[1], argv + 1, envp, 0, &fa, 1) ;
     31   if (!pid) strerr_diefu2sys(111, "spawn ", argv[1]) ;
     32   close(p[1]) ;
     33   for (size_t i = 0 ; i < N ; i++) close(fds[i]) ;
     34 
     35   p[1] = fd_read(p[0], &c, 1) ;
     36   if (p[1] == -1) strerr_diefu1sys(111, "read on hackpipe") ;
     37   if (p[1]) strerr_dief2x(102, argv[1], " wrote on hackpipe") ;
     38   if (wait_pid(pid, &p[1]) < 0) strerr_diefu1sys(111, "wait_pid") ;
     39   return wait_estatus(p[1]) ;
     40 }