s6

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

s6-usertree-maker.c (7573B)


      1 /* ISC license. */
      2 
      3 #include <stdint.h>
      4 #include <string.h>
      5 #include <sys/stat.h>
      6 #include <sys/uio.h>
      7 #include <errno.h>
      8 
      9 #include <skalibs/config.h>
     10 #include <skalibs/uint64.h>
     11 #include <skalibs/types.h>
     12 #include <skalibs/bytestr.h>
     13 #include <skalibs/buffer.h>
     14 #include <skalibs/sgetopt.h>
     15 #include <skalibs/strerr.h>
     16 #include <skalibs/stralloc.h>
     17 #include <skalibs/djbunix.h>
     18 #include <skalibs/skamisc.h>
     19 
     20 #include <execline/config.h>
     21 
     22 #include <s6/config.h>
     23 #include <s6/auto.h>
     24 
     25 #define USAGE "s6-usertree-maker [ -d userscandir ] [ -p path ] [ -E envdir [ -e var ... ] ] [ -r service/logger[/pipeline] ] [ -l loguser ] [ -t stamptype ] [ -n nfiles ] [ -s filesize ] [ -S maxsize ] [ -P prefix ] user logdir dir"
     26 #define dieusage() strerr_dieusage(100, USAGE)
     27 
     28 #define VARS_MAX 64
     29 
     30 static stralloc sa = STRALLOC_ZERO ;
     31 
     32 typedef struct svinfo_s svinfo, *svinfo_ref ;
     33 struct svinfo_s
     34 {
     35   char const *user ;
     36   char const *sc ;
     37   char const *logger ;
     38   char const *path ;
     39   char const *userenvdir ;
     40   char const **vars ;
     41   size_t varlen ;
     42 } ;
     43 
     44 static int write_run (buffer *b, void *data)
     45 {
     46   svinfo *t = data ;
     47   if (!string_quote(&sa, t->user, strlen(t->user))) return 0 ;
     48   if (buffer_puts(b, "#!" EXECLINE_SHEBANGPREFIX "execlineb -P\n"
     49     EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n"
     50     EXECLINE_EXTBINPREFIX "emptyenv -p\n"
     51     EXECLINE_EXTBINPREFIX "export USER ") < 0
     52    || buffer_put(b, sa.s, sa.len) < 0
     53    || buffer_puts(b, "\n"
     54     S6_EXTBINPREFIX "s6-envuidgid -i -- ") < 0
     55    || buffer_put(b, sa.s, sa.len) < 0
     56    || buffer_puts(b, "\n"
     57     S6_EXTBINPREFIX "s6-applyuidgid -U --\n"
     58     EXECLINE_EXTBINPREFIX "backtick -in HOME { "
     59     EXECLINE_EXTBINPREFIX "homeof ") < 0
     60    || buffer_put(b, sa.s, sa.len) < 0
     61    || buffer_put(b, " }\n", 3) < 0) goto err ;
     62   sa.len = 0 ;
     63   if (t->userenvdir)
     64   {
     65     if (!string_quote(&sa, t->userenvdir, strlen(t->userenvdir))) return 0 ;
     66     if (buffer_puts(b, S6_EXTBINPREFIX "s6-envdir -i -- ") < 0
     67      || buffer_put(b, sa.s, sa.len) < 0
     68      || buffer_put(b, "\n", 1) < 0) goto err ;
     69     sa.len = 0 ;
     70     if (t->varlen)
     71     {
     72       if (buffer_puts(b, EXECLINE_EXTBINPREFIX "multisubstitute\n{\n") < 0) return 0 ;
     73       for (size_t i = 0 ; i < t->varlen ; i++)
     74       {
     75         if (!string_quote(&sa, t->vars[i], strlen(t->vars[i]))) return 0 ;
     76         if (buffer_puts(b, "  importas -D \"\" -- ") < 0
     77          || buffer_put(b, sa.s, sa.len) < 0
     78          || buffer_put(b, " ", 1) < 0
     79          || buffer_put(b, sa.s, sa.len) < 0
     80          || buffer_put(b, "\n", 1) < 0) goto err ;
     81         sa.len = 0 ;
     82       }
     83       if (buffer_put(b, "}\n", 2) < 0) return 0 ;
     84     }
     85   }
     86   if (buffer_puts(b, EXECLINE_EXTBINPREFIX "multisubstitute\n{\n"
     87    "  importas -i USER USER\n"
     88    "  importas -i HOME HOME\n"
     89    "  importas -i UID UID\n"
     90    "  importas -i GID GID\n"
     91    "  importas -i GIDLIST GIDLIST\n}\n") < 0) return 0 ;
     92   if (t->userenvdir && t->varlen)
     93   {
     94     for (size_t i = 0 ; i < t->varlen ; i++)
     95     {
     96       if (!string_quote(&sa, t->vars[i], strlen(t->vars[i]))) return 0 ;
     97       if (buffer_puts(b, EXECLINE_EXTBINPREFIX "export ") < 0
     98        || buffer_put(b, sa.s, sa.len) < 0
     99        || buffer_put(b, " ${", 3) < 0
    100        || buffer_put(b, sa.s, sa.len) < 0
    101        || buffer_put(b, "}\n", 2) < 0) goto err ;
    102       sa.len = 0 ;
    103     }
    104   }
    105   if (!string_quote(&sa, t->path, strlen(t->path))) return 0 ;
    106   if (buffer_puts(b, EXECLINE_EXTBINPREFIX "export PATH ") < 0
    107    || buffer_put(b, sa.s, sa.len) < 0) goto err ;
    108   sa.len = 0 ;
    109   if (!string_quote(&sa, t->sc, strlen(t->sc))) return 0 ;
    110   if (buffer_puts(b, "\n"
    111     S6_EXTBINPREFIX "s6-svscan -d3 -- ") < 0
    112    || buffer_put(b, sa.s, sa.len) < 0) goto err ;
    113   sa.len = 0 ;
    114   if (!buffer_putflush(b, "\n", 1)) return 0 ;
    115   return 1 ;
    116 
    117  err:
    118   sa.len = 0 ;
    119   return 0 ;
    120 }
    121 
    122 int main (int argc, char *const *argv)
    123 {
    124   char const *vars[VARS_MAX] ;
    125   svinfo t =
    126   {
    127     .sc = "${HOME}/service",
    128     .logger = 0,
    129     .path = SKALIBS_DEFAULTPATH,
    130     .userenvdir = 0,
    131     .vars = vars,
    132     .varlen = 0
    133   } ;
    134   char *rcinfo[3] = { 0, 0, 0 } ;
    135   char const *loguser = 0 ;
    136   unsigned int stamptype = 1 ;
    137   unsigned int nfiles = 10 ;
    138   uint64_t filesize = 1000000 ;
    139   uint64_t maxsize = 0 ;
    140   char const *prefix = 0 ;
    141   size_t dirlen ;
    142   PROG = "s6-usertree-maker" ;
    143   {
    144     subgetopt l = SUBGETOPT_ZERO ;
    145     for (;;)
    146     {
    147       int opt = subgetopt_r(argc, (char const *const *)argv, "d:p:E:e:r:l:t:n:s:S:P:", &l) ;
    148       if (opt == -1) break ;
    149       switch (opt)
    150       {
    151         case 'd' : t.sc = l.arg ; break ;
    152         case 'p' : t.path = l.arg ; break ;
    153         case 'E' : t.userenvdir = l.arg ; break ;
    154         case 'e' :
    155           if (t.varlen >= VARS_MAX) strerr_dief1x(100, "too many -v variables") ;
    156           if (strchr(l.arg, '=')) strerr_dief2x(100, "invalid variable name: ", l.arg) ;
    157           t.vars[t.varlen++] = l.arg ;
    158           break ;
    159         case 'r' : rcinfo[0] = (char *)l.arg ; break ;
    160         case 'l' : loguser = l.arg ; break ;
    161         case 't' : if (!uint0_scan(l.arg, &stamptype)) dieusage() ; break ;
    162         case 'n' : if (!uint0_scan(l.arg, &nfiles)) dieusage() ; break ;
    163         case 's' : if (!uint640_scan(l.arg, &filesize)) dieusage() ; break ;
    164         case 'S' : if (!uint640_scan(l.arg, &maxsize)) dieusage() ; break ;
    165         case 'P' : prefix = l.arg ; break ;
    166         default : dieusage() ;
    167       }
    168     }
    169     argc -= l.ind ; argv += l.ind ;
    170   }
    171   if (argc < 3) dieusage() ;
    172   if (argv[1][0] != '/') strerr_dief1x(100, "logdir must be absolute") ;
    173   if (t.sc[0] != '/' && !str_start(t.sc, "${HOME}/"))
    174     strerr_dief1x(100, "userscandir must be absolute or start with ${HOME}/") ;
    175   if (stamptype > 3) strerr_dief1x(100, "stamptype must be 0, 1, 2 or 3") ;
    176   if (rcinfo[0])
    177   {
    178     if (strchr(rcinfo[0], '\n'))
    179       strerr_dief2x(100, "newlines", " are forbidden in s6-rc names") ;
    180     if (rcinfo[0][0] == '/')
    181       strerr_dief2x(100, "service", " name cannot be empty") ;
    182     rcinfo[1] = strchr(rcinfo[0], '/') ;
    183     if (!rcinfo[1]) strerr_dief1x(100, "argument to -r must be: service/logger or service/logger/pipeline") ;
    184     *rcinfo[1]++ = 0 ;
    185     if (!rcinfo[1][0]) strerr_dief1x(100, "argument to -r must be: service/logger or service/logger/pipeline") ;
    186     if (rcinfo[1][0] == '/')
    187       strerr_dief2x(100, "logger", " name cannot be empty") ;
    188     rcinfo[2] = strchr(rcinfo[1], '/') ;
    189     if (rcinfo[2])
    190     {
    191       *rcinfo[2]++ = 0 ;
    192       if (!rcinfo[2][0]) strerr_dief2x(100, "pipeline", " name cannot be empty") ;
    193       if (strchr(rcinfo[2], '/')) strerr_dief2x(100, "slashes", " are forbidden in s6-rc names") ;
    194     }
    195   }
    196   t.user = argv[0] ;
    197   dirlen = strlen(argv[2]) ;
    198 
    199   if (rcinfo[0])
    200   {
    201     size_t svclen = strlen(rcinfo[0]) ;
    202     size_t loglen = strlen(rcinfo[1]) ;
    203     char dir[dirlen + 2 + (svclen > loglen ? svclen : loglen)] ;
    204     memcpy(dir, argv[2], dirlen) ;
    205     dir[dirlen] = '/' ;
    206     if (mkdir(argv[2], 0755) < 0 && errno != EEXIST)
    207       strerr_diefu2sys(111, "mkdir ", argv[2]) ;
    208     t.logger = rcinfo[1] ;
    209     memcpy(dir + dirlen + 1, rcinfo[0], svclen + 1) ;
    210     s6_auto_write_service(dir, 3, &write_run, &t, rcinfo[1]) ;
    211     memcpy(dir + dirlen + 1, rcinfo[1], loglen + 1) ;
    212     s6_auto_write_logger_tmp(dir, loguser, argv[1], stamptype, nfiles, filesize, maxsize, prefix, rcinfo[0], rcinfo[2], &sa) ;
    213   }
    214   else
    215   {
    216     char dir[dirlen + 5] ;
    217     memcpy(dir, argv[2], dirlen) ;
    218     memcpy(dir + dirlen, "/log", 5) ;
    219     s6_auto_write_service(argv[2], 3, &write_run, &t, 0) ;
    220     s6_auto_write_logger_tmp(dir, loguser, argv[1], stamptype, nfiles, filesize, maxsize, prefix, 0, 0, &sa) ;
    221   }
    222   return 0 ;
    223 }