summaryrefslogtreecommitdiff
path: root/src/instance/s6-instance-status.c
blob: 2c00a6e5c8f0fb8578fa020956d1b1bb8309aaf9 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* ISC license. */

#include <string.h>

#include <skalibs/bytestr.h>
#include <skalibs/strerr.h>
#include <skalibs/sgetopt.h>
#include <skalibs/exec.h>

#include <s6/config.h>

#define USAGE "s6-instance-status [ -uwNrpest | -o up,wantedup,normallyup,ready,paused,pid,exitcode,signal,signum,updownsince,readysince,updownfor,readyfor ] [ -n ] service name"
#define dieusage() strerr_dieusage(100, USAGE)

#define MAXFIELDS 16
#define checkfields() if (n++ >= MAXFIELDS) strerr_dief1x(100, "too many option fields")

static unsigned int check_options (char const *arg, unsigned int n)
{
  static char const *table[] =
  {
    "up",
    "wantedup",
    "normallyup",
    "ready",
    "paused",
    "pid",
    "exitcode",
    "signal",
    "signum",
    "updownsince",
    "readysince",
    "updownfor",
    "readyfor",
    0
  } ;
  while (*arg)
  {
    size_t pos = str_chr(arg, ',') ;
    char const *const *p = table ;
    if (!pos) strerr_dief1x(100, "invalid null option field") ;
    for (; *p ; p++) if (!strncmp(arg, *p, pos)) break ;
    if (!p)
    {
      char blah[pos+1] ;
      memcpy(blah, arg, pos) ;
      blah[pos] = 0 ;
      strerr_dief2x(100, "invalid option field: ", blah) ;
    }
    checkfields() ;
    arg += pos ; if (*arg) arg++ ;
  }
  return n ;
}

int main (int argc, char const **argv)
{
  char const **fullargv = argv ;
  size_t namelen ;
  unsigned int n = 0 ;
  PROG = "s6-instance-status" ;
  {
    subgetopt l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "no:uwNrpest", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'n' : break ;
        case 'o' : n = check_options(l.arg, n) ; break ;
        case 'u' :
        case 'w' :
        case 'N' :
        case 'r' :
        case 'p' :
        case 'e' :
        case 's' :
        case 't' : if (n++ >= MAXFIELDS) strerr_dief1x(100, "too many option fields") ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (argc < 2) dieusage() ;
  namelen = strlen(argv[1]) ;
  if (!argv[0][0]) strerr_dief1x(100, "invalid service name") ;
  if (!argv[1][0] || argv[1][0] == '.' || byte_in(argv[1], namelen, " \t\f\r\n", 5) < namelen)
    strerr_dief1x(100, "invalid instance name") ;

  {
    size_t svlen = strlen(argv[0]) ;
    char fn[svlen + 11 + namelen] ;
    memcpy(fn, argv[0], svlen) ;
    memcpy(fn + svlen, "/instance/", 10) ;
    memcpy(fn + svlen + 10, argv[1], namelen + 1) ;
    argv[0] = fn ;
    argv[1] = 0 ;
    fullargv[0] = S6_BINPREFIX "s6-svstat" ;
    xexec(fullargv) ;
  }
}