skalibs

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

child_spawn2.c (960B)


      1 /* ISC license. */
      2 
      3 #include <unistd.h>
      4 
      5 #include <skalibs/djbunix.h>
      6 #include <skalibs/cspawn.h>
      7 
      8 pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const *envp, int *fds)
      9 {
     10   pid_t pid ;
     11   int p[2][2] ;
     12   if (pipe(p[0]) == -1) return 0 ;
     13   if (ndelay_on(p[0][0]) == -1 || coe(p[0][0]) == -1 || pipe(p[1]) == -1) goto errp ;
     14   if (ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1) goto err ;
     15 
     16   {
     17     cspawn_fileaction fa[2] =
     18     {
     19       [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[0], [1] = p[1][0] } } },
     20       [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[1], [1] = p[0][1] } } }
     21     } ;
     22     pid = cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2) ;
     23     if (!pid) goto err ;
     24   }
     25 
     26   fd_close(p[0][1]) ;
     27   fd_close(p[1][0]) ;
     28   fds[0] = p[0][0] ;
     29   fds[1] = p[1][1] ;
     30   return pid ;
     31 
     32  err:
     33   fd_close(p[1][1]) ;
     34   fd_close(p[1][0]) ;
     35  errp:
     36   fd_close(p[0][1]) ;
     37   fd_close(p[0][0]) ;
     38   return 0 ;
     39 }