about summary refs log tree commit diff
path: root/src/supervision/s6-svstat.c
blob: 502991a0b31f4293cff4abc20123c04397b09e91 (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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* ISC license. */

#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

#include <skalibs/uint64.h>
#include <skalibs/types.h>
#include <skalibs/bytestr.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr.h>
#include <skalibs/sgetopt.h>
#include <skalibs/sig.h>
#include <skalibs/tai.h>
#include <skalibs/djbunix.h>

#include <s6/supervise.h>

#define USAGE "s6-svstat [ -uwNrpgest | -o up,wantedup,normallyup,ready,paused,pid,pgid,exitcode,signal,signum,updownsince,readysince,updownfor,readyfor ] [ -n ] servicedir"
#define dieusage() strerr_dieusage(100, USAGE)

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

static int normallyup ;

typedef void pr_func (buffer *, s6_svstatus_t const *) ;
typedef pr_func * pr_func_ref ;

typedef struct funcmap_s funcmap_t ;
struct funcmap_s
{
  char const *s ;
  pr_func_ref f ;
} ;

static void pr_up (buffer *b, s6_svstatus_t const *st)
{
  buffer_putsnoflush(b, st->pid && !st->flagfinishing ? "true" : "false") ;
}

static void pr_wantedup (buffer *b, s6_svstatus_t const *st)
{
  buffer_putsnoflush(b, st->flagwantup ? "true" : "false") ;
}

static void pr_ready (buffer *b, s6_svstatus_t const *st)
{
  buffer_putsnoflush(b, st->pid && st->flagready ? "true" : "false") ;
}

static void pr_paused (buffer *b, s6_svstatus_t const *st)
{
  buffer_putsnoflush(b, st->flagpaused ? "true" : "false") ;
}

static void pr_pid (buffer *b, s6_svstatus_t const *st)
{
  if (st->pid && !st->flagfinishing)
  {
    char fmt[PID_FMT] ;
    buffer_putnoflush(b, fmt, pid_fmt(fmt, st->pid)) ;
  }
  else buffer_putsnoflush(b, "-1") ;
}

static void pr_pgid (buffer *b, s6_svstatus_t const *st)
{
  if (st->pgid > 0)
  {
    char fmt[PID_FMT] ;
    buffer_putnoflush(b, fmt, pid_fmt(fmt, st->pgid)) ;
  }
  else buffer_putsnoflush(b, "-1") ;
}

static void pr_tain (buffer *b, tain const *a)
{
  char fmt[TIMESTAMP] ;
  buffer_putnoflush(b, fmt, timestamp_fmt(fmt, a)) ;
}

static void pr_stamp (buffer *b, s6_svstatus_t const *st)
{
  pr_tain(b, &st->stamp) ;
}

static void pr_readystamp (buffer *b, s6_svstatus_t const *st)
{
  pr_tain(b, &st->readystamp) ;
}

static void pr_seconds (buffer *b, tain const *a)
{
  tain d ;
  char fmt[UINT64_FMT] ;
  tain_sub(&d, &STAMP, a) ;
  buffer_putnoflush(b, fmt, uint64_fmt(fmt, tai_sec(tain_secp(&d)))) ;
}

static void pr_upseconds (buffer *b, s6_svstatus_t const *st)
{
  pr_seconds(b, &st->stamp) ;
}

static void pr_readyseconds (buffer *b, s6_svstatus_t const *st)
{
  pr_seconds(b, &st->readystamp) ;
}

static void pr_exitcode (buffer *b, s6_svstatus_t const *st)
{
  int e = st->pid && !st->flagfinishing ? -1 :
          WIFEXITED(st->wstat) ? WEXITSTATUS(st->wstat) : -1 ;
  char fmt[INT_FMT] ;
  buffer_putnoflush(b, fmt, int_fmt(fmt, e)) ;
}

static void pr_signum (buffer *b, s6_svstatus_t const *st)
{
  int e = st->pid && !st->flagfinishing ? -1 :
            WIFSIGNALED(st->wstat) ? WTERMSIG(st->wstat) : -1 ;
  char fmt[INT_FMT] ;
  buffer_putnoflush(b, fmt, int_fmt(fmt, e)) ;
}

static void pr_signal (buffer *b, s6_svstatus_t const *st)
{
  int e = st->pid && !st->flagfinishing ? -1 :
            WIFSIGNALED(st->wstat) ? WTERMSIG(st->wstat) : -1 ;
  if (e == -1) buffer_putsnoflush(b, "NA") ;
  else
  {
    buffer_putsnoflush(b, "SIG") ;
    buffer_putsnoflush(b, sig_name(e)) ;
  }
}

static void pr_normallyup (buffer *b, s6_svstatus_t const *st)
{
  buffer_putsnoflush(b, normallyup ? "true" : "false") ;
  (void)st ;
}

static funcmap_t const fmtable[] =
{
  { .s = "exitcode", .f = &pr_exitcode },
  { .s = "normallyup", .f = &pr_normallyup },
  { .s = "paused", .f = &pr_paused },
  { .s = "pgid", .f = &pr_pgid },
  { .s = "pid", .f = &pr_pid },
  { .s = "ready", .f = &pr_ready },
  { .s = "readyfor", .f = &pr_readyseconds },
  { .s = "readysince", .f = &pr_readystamp },
  { .s = "signal", .f = &pr_signal },
  { .s = "signum", .f = &pr_signum },
  { .s = "up", .f = &pr_up },
  { .s = "updownfor", .f = &pr_upseconds },
  { .s = "updownsince", .f = &pr_stamp },
  { .s = "wantedup", .f = &pr_wantedup },
} ;

static int funcmap_bcmp (void const *a, void const *b)
{
  return strcmp((char const *)a, ((funcmap_t const *)b)->s) ;
}

#define BSEARCH(key, array) bsearch(key, (array), sizeof(array)/sizeof(funcmap_t), sizeof(funcmap_t), &funcmap_bcmp)

static unsigned int parse_options (char const *arg, pr_func_ref *fields, unsigned int n)
{
  while (*arg)
  {
    funcmap_t const *p ;
    size_t pos = str_chr(arg, ',') ;
    char blah[pos+1] ;
    memcpy(blah, arg, pos) ;
    blah[pos] = 0 ;
    p = BSEARCH(blah, fmtable) ;
    if (!p) strerr_dief2x(100, "invalid option field: ", blah) ;
    checkfields() ;
    fields[n++] = p->f ;
    arg += pos ; if (*arg) arg++ ;
  }
  return n ;
}

static void legacy (s6_svstatus_t *st, int flagnum)
{
  s6_svstatus_t status = *st ;
  int isup = status.pid && !status.flagfinishing ;
  char fmt[UINT64_FMT] ;

  if (isup)
  {
    buffer_putnoflush(buffer_1small,"up (pid ", 8) ;
    buffer_putnoflush(buffer_1small, fmt, pid_fmt(fmt, status.pid)) ;
    buffer_putnoflush(buffer_1small, " pgid ", 6) ;
    buffer_putnoflush(buffer_1small, fmt, pid_fmt(fmt, status.pgid)) ;
    buffer_putnoflush(buffer_1small, ") ", 2) ;
  }
  else
  {
    buffer_putnoflush(buffer_1small, "down (", 6) ;
    if (WIFSIGNALED(status.wstat))
    {
      buffer_putnoflush(buffer_1small, "signal ", 7) ;
      if (flagnum)
        buffer_putnoflush(buffer_1small, fmt, uint_fmt(fmt, WTERMSIG(status.wstat))) ;
      else
      {
        buffer_putnoflush(buffer_1small, "SIG", 3) ;
        buffer_putsnoflush(buffer_1small, sig_name(WTERMSIG(status.wstat))) ;
      }
    }
    else
    {
      buffer_putnoflush(buffer_1small, "exitcode ", 9) ;
      buffer_putnoflush(buffer_1small, fmt, uint_fmt(fmt, WEXITSTATUS(status.wstat))) ;
    }
    buffer_putnoflush(buffer_1small, ") ", 2) ;
  }

  tain_sub(&status.stamp, &STAMP, &status.stamp) ;
  buffer_putnoflush(buffer_1small, fmt, uint64_fmt(fmt, status.stamp.sec.x)) ;
  buffer_putnoflush(buffer_1small, " seconds", 8) ;

  if (isup && !normallyup)
    buffer_putnoflush(buffer_1small, ", normally down", 15) ;
  if (!isup && normallyup)
    buffer_putnoflush(buffer_1small, ", normally up", 13) ;
  if (isup && status.flagpaused)
    buffer_putnoflush(buffer_1small, ", paused", 8) ;
  if (!isup && status.flagwantup)
    buffer_putnoflush(buffer_1small, ", want up", 9) ;
  if (isup && !status.flagwantup)
    buffer_putnoflush(buffer_1small, ", want down", 11) ;

  if (status.flagready)
  {
    tain_sub(&status.readystamp, &STAMP, &status.readystamp) ;
    buffer_putnoflush(buffer_1small, ", ready ", 8) ;
    buffer_putnoflush(buffer_1small, fmt, uint64_fmt(fmt, status.readystamp.sec.x)) ;
    buffer_putnoflush(buffer_1small, " seconds", 8) ;
  }
}

int main (int argc, char const *const *argv)
{
  s6_svstatus_t status ;
  int flagnum = 0 ;
  pr_func_ref fields[MAXFIELDS] ;
  unsigned int n = 0 ;
  PROG = "s6-svstat" ;

  {
    subgetopt l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "no:uwNrpgest", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'n' : flagnum = 1 ; break ;
        case 'o' : n = parse_options(l.arg, fields, n) ; break ;
        case 'u' : checkfields() ; fields[n++] = &pr_up ; break ;
        case 'w' : checkfields() ; fields[n++] = &pr_wantedup ; break ;
        case 'N' : checkfields() ; fields[n++] = &pr_normallyup ; break ;
        case 'r' : checkfields() ; fields[n++] = &pr_ready ; break ;
        case 'p' : checkfields() ; fields[n++] = &pr_pid ; break ;
        case 'g' : checkfields() ; fields[n++] = &pr_pgid ; break ;
        case 'e' : checkfields() ; fields[n++] = &pr_exitcode ; break ;
        case 's' : checkfields() ; fields[n++] = &pr_signal ; break ;
        case 't' : checkfields() ; fields[n++] = &pr_upseconds ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) dieusage() ;
  fields[n] = 0 ;

  {
    int r = s6_svc_ok(argv[0]) ;
    if (r < 0) strerr_diefu2sys(111, "check ", argv[0]) ;
    if (!r) strerr_diefu3x(1, "read status for ", argv[0], ": s6-supervise not running") ;
  }
  if (!s6_svstatus_read(argv[0], &status))
    strerr_diefu2sys(111, "read status for ", argv[0]) ;

  tain_wallclock_read_g() ;
  if (tain_future(&status.stamp)) tain_copynow(&status.stamp) ;

  {
    size_t dirlen = strlen(*argv) ;
    char fn[dirlen + 6] ;
    memcpy(fn, *argv, dirlen) ;
    memcpy(fn + dirlen, "/down", 6) ;
    if (access(fn, F_OK) < 0)
      if (errno != ENOENT) strerr_diefu2sys(111, "access ", fn) ;
      else normallyup = 1 ;
    else normallyup = 0 ;
  }

  if (!n) legacy(&status, flagnum) ;
  else
  {
    unsigned int i = 0 ;
    for (; fields[i] ; i++)
    {
      (*fields[i])(buffer_1small, &status) ;
      buffer_putsnoflush(buffer_1small, " ") ;
    }
    buffer_unput(buffer_1small, 1) ;
  }

  if (buffer_putflush(buffer_1small, "\n", 1) < 0)
    strerr_diefu1sys(111, "write to stdout") ;
  return 0 ;
}