about summary refs log tree commit diff
path: root/src/supervision/s6-svwait.c
blob: 35788b69fde5b5c5ed9942b4a8abbe808e2a522a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ISC license. */

#include <stdint.h>
#include <signal.h>

#include <skalibs/sgetopt.h>
#include <skalibs/types.h>
#include <skalibs/bitarray.h>
#include <skalibs/sig.h>
#include <skalibs/tai.h>
#include <skalibs/strerr2.h>

#include "s6-svlisten.h"

#define USAGE "s6-svwait [ -U | -u | -d | -D | -r | -R ] [ -a | -o ] [ -t timeout ] servicedir..."
#define dieusage() strerr_dieusage(100, USAGE)

int main (int argc, char const *const *argv)
{
  tain deadline ;
  int or = 0 ;
  int wantup = 1, wantready = 0 ;
  PROG = "s6-svwait" ;
  {
    unsigned int t = 0 ;
    subgetopt l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "UudDrRaot:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'U' : wantup = 1 ; wantready = 1 ; break ;
        case 'u' : wantup = 1 ; wantready = 0 ; break ;
        case 'd' : wantup = 0 ; wantready = 0 ; break ;
        case 'D' : wantup = 0 ; wantready = 1 ; break ;
        case 'r' : wantup = 2 ; wantready = 0 ; break ;
        case 'R' : wantup = 2 ; wantready = 1 ; break ;
        case 'a' : or = 0 ; break ;
        case 'o' : or = 1 ; break ;
        case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
    if (t) tain_from_millisecs(&deadline, t) ; else deadline = tain_infinite_relative ;
  }
  if (!argc) dieusage() ;
  tain_now_set_stopwatch_g() ;
  tain_add_g(&deadline, &deadline) ;

  {
    s6_svlisten_t foo = S6_SVLISTEN_ZERO ;
    int e ;
    uint16_t ids[argc] ;
    unsigned char upstate[bitarray_div8(argc)] ;
    unsigned char readystate[bitarray_div8(argc)] ;
    if (!sig_ignore(SIGPIPE)) strerr_diefu1sys(111, "ignore SIGPIPE") ;
    s6_svlisten_init(argc, argv, &foo, ids, upstate, readystate, &deadline) ;
    if (wantup == 2)
    {
      wantup = 1 ;
      e = s6_svlisten_loop(&foo, 0, 0, 0, &deadline, -1, 0) ;
      if (e < 0) strerr_dief1x(102, "supervisor died") ;
      else if (e > 0) strerr_dief1x(e, "some services reported permanent failure") ; 
    }
    e = s6_svlisten_loop(&foo, wantup, wantready, or, &deadline, -1, 0) ;
    if (e < 0) strerr_dief1x(102, "supervisor died") ;
    else if (e > 0) strerr_dief1x(e, "some services reported permanent failure") ; 
  }
  return 0 ;
}