s6

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

s6-fdholder-setdump.c (2312B)


      1 /* ISC license. */
      2 
      3 #include <string.h>
      4 #include <stdlib.h>
      5 #include <skalibs/types.h>
      6 #include <skalibs/strerr.h>
      7 #include <skalibs/sgetopt.h>
      8 #include <skalibs/tai.h>
      9 #include <s6/fdholder.h>
     10 
     11 #define USAGE "s6-fdholder-setdump [ -t timeout ] socket"
     12 #define dieusage() strerr_dieusage(100, USAGE)
     13 
     14 int main (int argc, char const *const *argv)
     15 {
     16   s6_fdholder_t a = S6_FDHOLDER_ZERO ;
     17   tain deadline ;
     18   unsigned int dumplen ;
     19   char const *x ;
     20   PROG = "s6-fdholder-setdump" ;
     21   {
     22     unsigned int t = 0 ;
     23     subgetopt l = SUBGETOPT_ZERO ;
     24     for (;;)
     25     {
     26       int opt = subgetopt_r(argc, argv, "t:", &l) ;
     27       if (opt == -1) break ;
     28       switch (opt)
     29       {
     30         case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ;
     31         default : dieusage() ;
     32       }
     33     }
     34     argc -= l.ind ; argv += l.ind ;
     35     if (t) tain_from_millisecs(&deadline, t) ;
     36     else deadline = tain_infinite_relative ;
     37   }
     38   if (!argc) dieusage() ;
     39 
     40   x = getenv("S6_FD#") ;
     41   if (!x) strerr_dienotset(100, "S6_FD#") ;
     42   if (!uint0_scan(x, &dumplen)) strerr_dieinvalid(100, "S6_FD#") ;
     43   tain_now_set_stopwatch_g() ;
     44   tain_add_g(&deadline, &deadline) ;
     45   if (!s6_fdholder_start_g(&a, argv[0], &deadline))
     46     strerr_diefu2sys(111, "connect to a fd-holder daemon at ", argv[0]) ;
     47   if (dumplen)
     48   {
     49     unsigned int i = 0 ;
     50     s6_fdholder_fd_t dump[dumplen] ;
     51     char s[11 + UINT_FMT] ;
     52     for (; i < dumplen ; i++)
     53     {
     54       size_t len ;
     55       unsigned int fd ;
     56       memcpy(s, "S6_FD_", 6) ;
     57       s[6 + uint_fmt(s+6, i)] = 0 ;
     58       x = getenv(s) ;
     59       if (!x) strerr_dienotset(100, s) ;
     60       if (!uint0_scan(x, &fd)) strerr_dieinvalid(100, s) ;
     61       dump[i].fd = fd ;
     62       memcpy(s, "S6_FDID_", 8) ;
     63       s[8 + uint_fmt(s+8, i)] = 0 ;
     64       x = getenv(s) ;
     65       if (!x) strerr_dienotset(100, s) ;
     66       len = strlen(x) ;
     67       if (!len || len > S6_FDHOLDER_ID_SIZE) strerr_dieinvalid(100, s) ;
     68       memcpy(dump[i].id, x, len+1) ;
     69       memcpy(s, "S6_FDLIMIT_", 11) ;
     70       s[11 + uint_fmt(s+11, i)] = 0 ;
     71       x = getenv(s) ;
     72       if (!x) tain_add_g(&dump[i].limit, &tain_infinite_relative) ;
     73       else if (!timestamp_scan(x, &dump[i].limit)) strerr_dieinvalid(100, s) ;
     74     }
     75     if (!s6_fdholder_setdump_g(&a, dump, dumplen, &deadline))
     76       strerr_diefu1sys(1, "set dump") ;
     77   }
     78   return 0 ;
     79 }