s6

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

s6-svwait.c (2229B)


      1 /* ISC license. */
      2 
      3 #include <stdint.h>
      4 #include <signal.h>
      5 
      6 #include <skalibs/sgetopt.h>
      7 #include <skalibs/types.h>
      8 #include <skalibs/bitarray.h>
      9 #include <skalibs/sig.h>
     10 #include <skalibs/tai.h>
     11 #include <skalibs/strerr.h>
     12 
     13 #include "s6-svlisten.h"
     14 
     15 #define USAGE "s6-svwait [ -U | -u | -d | -D | -r | -R ] [ -a | -o ] [ -t timeout ] servicedir..."
     16 #define dieusage() strerr_dieusage(100, USAGE)
     17 
     18 int main (int argc, char const *const *argv)
     19 {
     20   tain deadline ;
     21   int or = 0 ;
     22   int wantup = 1, wantready = 0 ;
     23   PROG = "s6-svwait" ;
     24   {
     25     unsigned int t = 0 ;
     26     subgetopt l = SUBGETOPT_ZERO ;
     27     for (;;)
     28     {
     29       int opt = subgetopt_r(argc, argv, "UudDrRaot:", &l) ;
     30       if (opt == -1) break ;
     31       switch (opt)
     32       {
     33         case 'U' : wantup = 1 ; wantready = 1 ; break ;
     34         case 'u' : wantup = 1 ; wantready = 0 ; break ;
     35         case 'd' : wantup = 0 ; wantready = 0 ; break ;
     36         case 'D' : wantup = 0 ; wantready = 1 ; break ;
     37         case 'r' : wantup = 2 ; wantready = 0 ; break ;
     38         case 'R' : wantup = 2 ; wantready = 1 ; break ;
     39         case 'a' : or = 0 ; break ;
     40         case 'o' : or = 1 ; break ;
     41         case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ;
     42         default : dieusage() ;
     43       }
     44     }
     45     argc -= l.ind ; argv += l.ind ;
     46     if (t) tain_from_millisecs(&deadline, t) ; else deadline = tain_infinite_relative ;
     47   }
     48   if (!argc) return 0 ;
     49   tain_now_set_stopwatch_g() ;
     50   tain_add_g(&deadline, &deadline) ;
     51 
     52   {
     53     s6_svlisten_t foo = S6_SVLISTEN_ZERO ;
     54     unsigned int e ;
     55     uint16_t ids[argc] ;
     56     unsigned char upstate[bitarray_div8(argc)] ;
     57     unsigned char readystate[bitarray_div8(argc)] ;
     58     if (!sig_ignore(SIGPIPE)) strerr_diefu1sys(111, "ignore SIGPIPE") ;
     59     s6_svlisten_init(argc, argv, &foo, ids, upstate, readystate, &deadline) ;
     60     if (wantup == 2)
     61     {
     62       wantup = 1 ;
     63       e = s6_svlisten_loop(&foo, 0, 0, 0, &deadline, -1, 0) ;
     64       if (e) strerr_dief1x(e, "some services reported permanent failure or their supervisor died") ;
     65     }
     66     e = s6_svlisten_loop(&foo, wantup, wantready, or, &deadline, -1, 0) ;
     67     if (e) strerr_dief1x(e, "some services reported permanent failure or their supervisor died") ; 
     68   }
     69   return 0 ;
     70 }