skalibs

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

cspawn.h (7702B)


      1 /* ISC license. */
      2 
      3 #ifndef SKALIBS_CSPAWN_H
      4 #define SKALIBS_CSPAWN_H
      5 
      6 #include <sys/types.h>
      7 #include <stdint.h>
      8 
      9 #define CSPAWN_FLAGS_SELFPIPE_FINISH 0x0001U
     10 #define CSPAWN_FLAGS_SIGBLOCKNONE 0x0002U
     11 #define CSPAWN_FLAGS_SETSID 0x0004U
     12 #define CSPAWN_FLAGS_LINUX_NEWPID 0x0100U
     13 
     14 enum cspawn_fileaction_type_e
     15 {
     16   CSPAWN_FA_CLOSE,
     17   CSPAWN_FA_COPY,
     18   CSPAWN_FA_MOVE,
     19   CSPAWN_FA_OPEN,
     20   CSPAWN_FA_CHDIR,
     21   CSPAWN_FA_FCHDIR
     22 } ;
     23 
     24 struct cspawn_fa_openinfo_s
     25 {
     26   int fd ;
     27   char const *file ;
     28   int oflag ;
     29   mode_t mode ;
     30 } ;
     31 
     32 union cspawn_fileaction_u
     33 {
     34   int fd ;
     35   int fd2[2] ;
     36   char const *path ;
     37   struct cspawn_fa_openinfo_s openinfo ;
     38 } ;
     39 
     40 typedef struct cspawn_fileaction_s cspawn_fileaction, *cspawn_fileaction_ref ;
     41 struct cspawn_fileaction_s
     42 {
     43   enum cspawn_fileaction_type_e type ;
     44   union cspawn_fileaction_u x ;
     45 } ;
     46 
     47 
     48  /* Generic interface for posix_spawn() with a fork()+execve() fallback */
     49 
     50 extern pid_t cspawn (char const *, char const *const *, char const *const *, uint16_t, cspawn_fileaction const *, size_t) ;
     51 
     52 
     53  /* Simple spawn functions with 0 or 1 communicating fds. */
     54 
     55 extern pid_t child_spawn0 (char const *, char const *const *, char const *const *) ;
     56 extern pid_t child_spawn1_pipe (char const *, char const *const *, char const *const *, int *, int) ;
     57 extern pid_t child_spawn1_socket (char const *, char const *const *, char const *const *, int *) ;
     58 
     59 
     60  /*
     61     Spawn function with 2 communicating pipes. The int * points to 2 fds.
     62     Input: fds[0] and fds[1] are the fds to move the pipes to in the child.
     63     Output: fds[0] and fds[1] contain the pipes to the child.
     64  */
     65 
     66 extern pid_t child_spawn2 (char const *, char const *const *, char const *const *, int *) ;
     67 
     68 
     69  /*
     70     Same, with an additional pipe from the child to the parent.
     71     The int * points to 3 fds.
     72     The additional fd# is available to the child in the defined env variable.
     73  */
     74 
     75 #define SKALIBS_CHILD_SPAWN_FDS_ENVVAR "SKALIBS_CHILD_SPAWN_FDS"
     76 
     77 extern pid_t child_spawn3 (char const *, char const *const *, char const *const *, int *) ;
     78 
     79 
     80  /*
     81     Generalization of the previous functions.
     82     * requests n (the last arg) communication fds between parent and child. Uses pipes.
     83     * if n=1, equivalent to child_spawn1_pipe; child writes, parent reads.
     84     * if n>=2, parent reads on even and writes on odd.
     85  */
     86 
     87 extern pid_t child_spawn (char const *, char const *const *, char const *const *, int *, size_t) ;
     88 
     89 
     90  /* cspawn, but running as a grandchild. Uses one fork(). */
     91 
     92 extern pid_t gcspawn (char const *, char const *const *, char const *const *, uint16_t, cspawn_fileaction const *, size_t) ;
     93 
     94 
     95  /* mexec (see skalibs/exec.h), but with cspawn instead */
     96 
     97 extern int env_mspawn (char const *, char const *) ;
     98 
     99 extern pid_t mspawn_afn (char const *, char const *const *, char const *const *, size_t, char const *, size_t, size_t, uint16_t, cspawn_fileaction const *, size_t) ;
    100 extern pid_t mspawn_afm (char const *, char const *const *, char const *const *, size_t, char const *, size_t, uint16_t, cspawn_fileaction const *, size_t) ;
    101 extern pid_t mspawn_af (char const *, char const *const *, char const *const *, size_t, uint16_t, cspawn_fileaction const *, size_t) ;
    102 
    103 #define mspawn_aen(file, argv, envp, modif, modiflen, modifn, flags, fa, n) mspawn_afn(file, argv, envp, env_len(envp), modif, modiflen, modifn, flags, fa, n)
    104 #define mspawn_aem(file, argv, envp, modif, modiflen, flags, fa, n) mspawn_afm(file, argv, envp, env_len(envp), modif, modiflen, flags, fa, n)
    105 #define mspawn_ae(file, argv, envp, flags, fa, n) mspawn_af(file, argv, (envp), env_len(envp), flags, fa, n)
    106 
    107 #define mspawn_an(file, argv, modif, modiflen, modifn, flags, fa, n) mspawn_aen(file, argv, (char const *const *)environ, modif, modiflen, modifn, flags, fa, n)
    108 #define mspawn_am(file, argv, modif, modiflen, flags, fa, n) mspawn_aem(file, argv, (char const *const *)environ, modif, modiflen, flaga, fa, n)
    109 #define mspawn_a(file, argv, flags, fa, n) mspawn_ae(file, (argv), (char const *const *)environ, flags, fa, n)
    110 
    111 #define mspawn_fn(argv, envp, envlen, modif, modiflen, modifn, flags, fa, n) mspawn_afn((argv)[0], (argv), envp, envlen, modif, modiflen, modifn, flags, fa, n)
    112 #define mspawn_fm(argv, envp, envlen, modif, modiflen, flags, fa, n) mspawn_afm((argv)[0], (argv), envp, envlen, modif, modiflen, flags, fa, n)
    113 #define mspawn_f(argv, envp, envlen, flags, fa, n) mspawn_af((argv)[0], (argv), envp, envlen, flags, fa, n)
    114 
    115 #define mspawn_en(argv, envp, modif, modiflen, modifn, flags, fa, n) mspawn_aen((argv)[0], (argv), envp, modif, modiflen, modifn, flags, fa, n)
    116 #define mspawn_em(argv, envp, modif, modiflen, flags, fa, n) mspawn_aem((argv)[0], (argv), envp, modif, modiflen, flags, fa, n)
    117 #define mspawn_e(argv, envp, flags, fa, n) mspawn_ae((argv)[0], (argv), envp, flags, fa, n)
    118 
    119 #define mspawn_n(argv, modif, modiflen, modifn, flags, fa, n) mspawn_an((argv)[0], (argv), modif, modiflen, modifn, flags, fa, n)
    120 #define mspawn_m(argv, modif, modiflen, flags, fa, n) mspawn_am((argv)[0], (argv), modif, modiflen, flags, fa, n)
    121 #define mspawn(argv, flags, fa, n) mspawn_a((argv)[0], (argv), flags, fa, n)
    122 
    123 extern pid_t xmspawn_afn (char const *, char const *const *, char const *const *, size_t, char const *, size_t, size_t, uint16_t, cspawn_fileaction const *, size_t) ;
    124 extern pid_t xmspawn_afm (char const *, char const *const *, char const *const *, size_t, char const *, size_t, uint16_t, cspawn_fileaction const *, size_t) ;
    125 extern pid_t xmspawn_af (char const *, char const *const *, char const *const *, size_t, uint16_t, cspawn_fileaction const *, size_t) ;
    126 
    127 #define xmspawn_aen(file, argv, envp, modif, modiflen, modifn, flags, fa, n) xmspawn_afn(file, argv, envp, env_len(envp), modif, modiflen, modifn, flags, fa, n)
    128 #define xmspawn_aem(file, argv, envp, modif, modiflen, flags, fa, n) xmspawn_afm(file, argv, envp, env_len(envp), modif, modiflen, flags, fa, n)
    129 #define xmspawn_ae(file, argv, envp, flags, fa, n) xmspawn_af(file, argv, envp, env_len(envp), flags, fa, n)
    130 
    131 #define xmspawn_an(file, argv, modif, modiflen, modifn, flags, fa, n) xmspawn_aen(file, argv, (char const *const *)environ, modif, modiflen, modifn, flags, fa, n)
    132 #define xmspawn_am(file, argv, modif, modiflen, flags, fa, n) xmspawn_aem(file, argv, (char const *const *)environ, modif, modiflen, flags, fa, n)
    133 #define xmspawn_a(file, argv, flags, fa, n) xmspawn_ae(file, argv, (char const *const *)environ, flags, fa, n)
    134 
    135 #define xmspawn_fn(argv, envp, envlen, modif, modiflen, modifn, flags, fa, n) xmspawn_afn((argv)[0], (argv), envp, envlen, modif, modiflen, modifn, flags, fa, n)
    136 #define xmspawn_fm(argv, envp, envlen, modif, modiflen, flags, fa, n) xmspawn_afm((argv)[0], (argv), envp, envlen, modif, modiflen, flags, fa, n)
    137 #define xmspawn_f(argv, envp, envlen, flags, fa, n) xmspawn_af((argv)[0], (argv), envp, envlen, flags, fa, n)
    138 
    139 #define xmspawn_en(argv, envp, modif, modiflen, modifn, flags, fa, n) xmspawn_aen((argv)[0], (argv), envp, modif, modiflen, modifn, flags, fa, n)
    140 #define xmspawn_em(argv, envp, modif, modiflen, flags, fa, n) xmspawn_aem((argv)[0], (argv), envp, modif, modiflen, flags, fa, n)
    141 #define xmspawn_e(argv, envp, flags, fa, n) xmspawn_ae((argv)[0], (argv), envp, flags, fa, n)
    142 
    143 #define xmspawn_n(argv, modif, modiflen, modifn, flags, fa, n) xmspawn_an((argv)[0], (argv), modif, modiflen, modifn, flags, fa, n)
    144 #define xmspawn_m(argv, modif, modiflen, flags, fa, n) xmspawn_am((argv)[0], (argv), modif, modiflen, flags, fa, n)
    145 #define xmspawn(argv, flags, fa, n) xmspawn_a((argv)[0], (argv), flags, fa, n)
    146 
    147 extern pid_t gmspawn_afn (char const *, char const *const *, char const *const *, size_t, char const *, size_t, size_t, uint16_t, cspawn_fileaction const *, size_t) ;
    148 
    149 #endif