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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/* ISC license. */
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <skalibs/uint32.h>
#include <skalibs/sgetopt.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr.h>
#include <skalibs/djbunix.h>
#include <s6/config.h>
#include <s6/supervise.h>
#define USAGE "s6-frontend-alias-sv [ -v ] [ -w sec ] command services..."
#define dieusage() strerr_dieusage(100, USAGE)
#define dienomem() strerr_diefu1sys(111, "stralloc_catb")
typedef int exec_func (char const *, char const *const *) ;
typedef exec_func *exec_func_ref ;
typedef struct info_s info_t, *info_t_ref ;
struct info_s
{
char const *name ;
exec_func_ref f ;
} ;
static int dowait = 0 ;
static uint32_t secs = 7 ;
static void warnnolog (void)
{
strerr_warnw1x("s6-svc only sends commands to a single service, even if it has a dedicated logger") ;
}
static void warnnokill (void)
{
strerr_warnw1x("s6-supervise pilots a kill signal via the timeout-kill file in the service directory") ;
}
static int info_cmp (void const *a, void const *b)
{
char const *name = a ;
info_t const *info = b ;
return strcmp(name, info->name) ;
}
static int spawnit (char const *const *argv, char const *const *envp)
{
int wstat ;
pid_t r ;
pid_t pid = child_spawn0(argv[0], argv, envp) ;
if (!pid)
{
strerr_warnwu2sys("spawn ", argv[0]) ;
return 1 ;
}
r = wait_pid(pid, &wstat) ;
if (r != pid)
{
strerr_warnwu2sys("wait for ", argv[0]) ;
return 1 ;
}
return !!WIFSIGNALED(wstat) || !!WEXITSTATUS(wstat) ;
}
static int simple_svc (char const *dir, char const *options, char const *const *envp)
{
char const *argv[5] = { S6_EXTBINPREFIX "s6-svc", options, "--", dir, 0 } ;
return spawnit(argv, envp) ;
}
static int complex_svc (char const *dir, char const *order, char waitfor, char const *const *envp)
{
char warg[4] = "-w?" ;
char fmt[2 + UINT32_FMT] = "-T" ;
char const *argv[7] = { S6_EXTBINPREFIX "s6-svc", warg, fmt, order, "--", dir, 0 } ;
fmt[2 + uint32_fmt(fmt + 2, 1000 * secs)] = 0 ;
warg[2] = waitfor ;
return spawnit(argv, envp) ;
}
static int status (char const *dir, char const *const *envp)
{
int e ;
char const *argv[4] = { S6_EXTBINPREFIX "s6-svstat", "--", dir, 0 } ;
size_t dirlen = strlen(dir) ;
buffer_puts(buffer_1, dir) ;
buffer_putsflush(buffer_1, ": ") ;
e = spawnit(argv, envp) ;
if (dirlen < 5 || strcmp(dir + dirlen - 4, "/log"))
{
struct stat st ;
char log[dirlen + 5] ;
memcpy(log, dir, dirlen) ;
memcpy(log + dirlen, "/log", 5) ;
if (stat(log, &st) < 0)
{
if (errno != ENOENT)
{
strerr_warnwu2sys("stat", log) ;
e = 1 ;
}
}
else if (S_ISDIR(st.st_mode))
{
argv[2] = log ;
buffer_puts(buffer_1, log) ;
buffer_putsflush(buffer_1, ": ") ;
e |= spawnit(argv, envp) ;
}
}
return e ;
}
static int action (char const *dir, char const *simple, char const *cplx, char waitchar, char const *const *envp)
{
if (dowait)
{
int e = complex_svc(dir, cplx, waitchar, envp) ;
return e | status(dir, envp) ;
}
else return simple_svc(dir, simple, envp) ;
}
static int usr1_h (char const *dir, char const *const *envp)
{
return simple_svc(dir, "-1", envp) ;
}
static int usr2_h (char const *dir, char const *const *envp)
{
return simple_svc(dir, "-2", envp) ;
}
static int alarm_h (char const *dir, char const *const *envp)
{
return simple_svc(dir, "-a", envp) ;
}
static int cont_h (char const *dir, char const *const *envp)
{
return action(dir, "-c", "-o", 'U', envp) ;
}
static int down (char const *dir, char const *const *envp)
{
return action(dir, "-d", "-d", 'D', envp) ;
}
static int forcedown (char const *dir, char const *const *envp)
{
warnnokill() ;
return down(dir, envp) ;
}
static int bail (char const *dir, char const *const *envp)
{
int e ;
warnnolog() ;
e = action(dir, "-xd", "-d", 'D', envp) ;
if (dowait) e |= simple_svc(dir, "-x", envp) ;
return e ;
}
static int forcebail (char const *dir, char const *const *envp)
{
warnnokill() ;
return bail(dir, envp) ;
}
static int hup_h (char const *dir, char const *const *envp)
{
return simple_svc(dir, "-h", envp) ;
}
static int int_h (char const *dir, char const *const *envp)
{
return simple_svc(dir, "-i", envp) ;
}
static int kill_h (char const *dir, char const *const *envp)
{
return simple_svc(dir, "-k", envp) ;
}
static int once (char const *dir, char const *const *envp)
{
return action(dir, "-o", "-o", 'U', envp) ;
}
static int pause_h (char const *dir, char const *const *envp)
{
return simple_svc(dir, "-p", envp) ;
}
static int quit_h (char const *dir, char const *const *envp)
{
return simple_svc(dir, "-q", envp) ;
}
static int term_h (char const *dir, char const *const *envp)
{
return action(dir, "-t", "-r", 'R', envp) ;
}
static int up (char const *dir, char const *const *envp)
{
return action(dir, "-u", "-u", 'U', envp) ;
}
static int check (char const *dir, char const *const *envp)
{
int e ;
s6_svstatus_t svst ;
char warg[3] = "-?" ;
char fmt[2 + UINT32_FMT] = "-t" ;
char const *argv[6] = { S6_EXTBINPREFIX "s6-svwait", warg, fmt, "--", dir, 0 } ;
fmt[2 + uint32_fmt(fmt + 2, 1000 * secs)] = 0 ;
if (!s6_svstatus_read(dir, &svst)) return 1 ;
warg[1] = svst.flagwantup ? 'U' : 'D' ;
e = spawnit(argv, envp) ;
return e | status(dir, envp) ;
}
static int lsb_reload (char const *dir, char const *const *envp)
{
hup_h(dir, envp) ;
return status(dir, envp) ;
}
static int lsb_restart (char const *dir, char const *const *envp)
{
int e = complex_svc(dir, "-ru", 'U', envp) ;
return e | status(dir, envp) ;
}
static int lsb_start (char const *dir, char const *const *envp)
{
int e = complex_svc(dir, "-u", 'U', envp) ;
return e | status(dir, envp) ;
}
static int lsb_stop (char const *dir, char const *const *envp)
{
int e = complex_svc(dir, "-d", 'D', envp) ;
return e | status(dir, envp) ;
}
static int lsb_shutdown (char const *dir, char const *const *envp)
{
int e ;
warnnolog() ;
e = complex_svc(dir, "-d", 'D', envp) ;
e |= status(dir, envp) ;
return e | simple_svc(dir, "-x", envp) ;
}
static int lsb_forcereload (char const *dir, char const *const *envp)
{
warnnokill() ;
return lsb_reload(dir, envp) ;
}
static int lsb_forcerestart (char const *dir, char const *const *envp)
{
warnnokill() ;
return lsb_restart(dir, envp) ;
}
static int lsb_forcestop (char const *dir, char const *const *envp)
{
warnnokill() ;
return lsb_stop(dir, envp) ;
}
static int lsb_forceshutdown (char const *dir, char const *const *envp)
{
warnnokill() ;
return lsb_shutdown(dir, envp) ;
}
static int lsb_tryrestart (char const *dir, char const *const *envp)
{
int e = 0 ;
s6_svstatus_t svst ;
if (s6_svstatus_read(dir, &svst) && svst.flagwantup && svst.pid && !svst.flagfinishing)
e = complex_svc(dir, "-r", 'U', envp) ;
return e | status(dir, envp) ;
}
static int lsb_forcetryrestart (char const *dir, char const *const *envp)
{
warnnokill() ;
return lsb_tryrestart(dir, envp) ;
}
static info_t const commands[] =
{
{ .name = "1", .f = &usr1_h },
{ .name = "2", .f = &usr2_h },
{ .name = "D", .f = &forcedown },
{ .name = "E", .f = &forcebail },
{ .name = "T", .f = &lsb_forcetryrestart },
{ .name = "X", .f = &forcebail },
{ .name = "a", .f = &alarm_h },
{ .name = "al", .f = &alarm_h },
{ .name = "ala", .f = &alarm_h },
{ .name = "alar", .f = &alarm_h },
{ .name = "alarm", .f = &alarm_h },
{ .name = "c", .f = &cont_h },
{ .name = "check", .f = &check },
{ .name = "co", .f = &cont_h },
{ .name = "con", .f = &cont_h },
{ .name = "cont", .f = &cont_h },
{ .name = "d", .f = &down },
{ .name = "do", .f = &down },
{ .name = "dow", .f = &down },
{ .name = "down", .f = &down },
{ .name = "e", .f = &bail },
{ .name = "ex", .f = &bail },
{ .name = "exi", .f = &bail },
{ .name = "exit", .f = &bail },
{ .name = "force-reload", .f = &lsb_forcereload },
{ .name = "force-restart", .f = &lsb_forcerestart },
{ .name = "force-shutdown", .f = &lsb_forceshutdown },
{ .name = "force-stop", .f = &lsb_forcestop },
{ .name = "h", .f = &hup_h },
{ .name = "hu", .f = &hup_h },
{ .name = "hup", .f = &hup_h },
{ .name = "i", .f = &int_h },
{ .name = "in", .f = &int_h },
{ .name = "int", .f = &int_h },
{ .name = "inte", .f = &int_h },
{ .name = "inter", .f = &int_h },
{ .name = "interr", .f = &int_h },
{ .name = "interru", .f = &int_h },
{ .name = "interrup", .f = &int_h },
{ .name = "interrupt", .f = &int_h },
{ .name = "k", .f = &kill_h },
{ .name = "ki", .f = &kill_h },
{ .name = "kil", .f = &kill_h },
{ .name = "kill", .f = &kill_h },
{ .name = "o", .f = &once },
{ .name = "on", .f = &once },
{ .name = "onc", .f = &once },
{ .name = "once", .f = &once },
{ .name = "p", .f = &pause_h },
{ .name = "pa", .f = &pause_h },
{ .name = "pau", .f = &pause_h },
{ .name = "paus", .f = &pause_h },
{ .name = "pause", .f = &pause_h },
{ .name = "q", .f = &quit_h },
{ .name = "qu", .f = &quit_h },
{ .name = "qui", .f = &quit_h },
{ .name = "quit", .f = &quit_h },
{ .name = "reload", .f = &lsb_reload },
{ .name = "restart", .f = &lsb_restart },
{ .name = "s", .f = &status },
{ .name = "shutdown", .f = &lsb_shutdown },
{ .name = "st", .f = &status },
{ .name = "sta", .f = &status },
{ .name = "start", .f = &lsb_start },
{ .name = "stat", .f = &status },
{ .name = "statu", .f = &status },
{ .name = "status", .f = &status },
{ .name = "stop", .f = &lsb_stop },
{ .name = "t", .f = &term_h },
{ .name = "te", .f = &term_h },
{ .name = "ter", .f = &term_h },
{ .name = "term", .f = &term_h },
{ .name = "try-restart", .f = &lsb_tryrestart },
{ .name = "u", .f = &up },
{ .name = "up", .f = &up },
{ .name = "x", .f = &bail }
} ;
int main (int argc, char const *const *argv, char const *const *envp)
{
int e = 0 ;
info_t *p ;
char const *x = getenv("SVWAIT") ;
char const *scandir = getenv("SVDIR") ;
size_t scandirlen ;
PROG = "s6-frontend-alias-sv" ;
if (!scandir) scandir = "/run/service" ; /* TODO: infer from s6li config */
scandirlen = strlen(scandir) ;
if (x)
{
if (!uint320_scan(x, &secs))
strerr_warnw1x("invalid SVWAIT value") ;
}
{
subgetopt l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc, argv, "vw:", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'v' : dowait = 1 ; break ;
case 'w' : if (!uint320_scan(l.arg, &secs)) dieusage() ; break ;
default : dieusage() ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (argc < 2) dieusage() ;
p = bsearch(argv[0], commands, sizeof(commands) / sizeof(info_t), sizeof(info_t), &info_cmp) ;
if (!p) strerr_dief2x(100, "unknown command: ", argv[0]) ;
for (argv++ ; *argv ; argv++)
{
if (!argv[0][0]) continue ;
if (argv[0][0] == '/' || argv[0][strlen(argv[0]) - 1] == '/' || (argv[0][0] == '.' && (!argv[0][1] || argv[0][1] == '/' || (argv[0][1] == '.' && (!argv[0][2] || argv[0][2] == '/')))))
e += (*p->f)(*argv, envp) ;
else
{
int what = 1 ;
struct stat st ;
size_t len = strlen(*argv) ;
char fn[scandirlen + len + 2] ;
memcpy(fn, scandir, scandirlen) ;
fn[scandirlen] = '/' ;
memcpy(fn + scandirlen + 1, *argv, len + 1) ;
if (stat(fn, &st) < 0) /* XXX: TOCTOU but we don't care */
{
if (errno != ENOENT)
{
e++ ;
what = 0 ;
strerr_warnwu2sys("stat ", fn) ;
}
}
else if (S_ISDIR(st.st_mode)) what = 2 ;
if (what) e += (*p->f)(what > 1 ? fn : *argv, envp) ;
}
}
return e ;
}
|