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
|
/* 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/strerr2.h>
#include <skalibs/djbunix.h>
#include <s6/config.h>
#include <s6/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 execfunc_t (char const *, char const *const *) ;
typedef execfunc_t *execfunc_t_ref ;
typedef struct info_s info_t, *info_t_ref ;
struct info_s
{
char const *name ;
execfunc_t_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)
{
char const *argv[4] = { S6_EXTBINPREFIX "s6-svstat", "--", dir, 0 } ;
return spawnit(argv, 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)
{
if (dowait)
{
complex_svc(dir, "-o", 'U', envp) ;
return status(dir, envp) ;
}
else return simple_svc(dir, "-c", envp) ;
}
static int down (char const *dir, char const *const *envp)
{
if (dowait)
{
complex_svc(dir, "-d", 'D', envp) ;
return status(dir, envp) ;
}
else return simple_svc(dir, "-d", envp) ;
}
static int bail (char const *dir, char const *const *envp)
{
warnnolog() ;
if (dowait)
{
complex_svc(dir, "-d", 'D', envp) ;
status(dir, envp) ;
}
return simple_svc(dir, "-xd", 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)
{
if (dowait)
{
complex_svc(dir, "-o", 'U', envp) ;
return status(dir, envp) ;
}
else return simple_svc(dir, "-o", 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)
{
if (dowait)
{
complex_svc(dir, "-r", 'R', envp) ;
return status(dir, envp) ;
}
else return simple_svc(dir, "-t", envp) ;
}
static int up (char const *dir, char const *const *envp)
{
if (dowait)
{
complex_svc(dir, "-u", 'U', envp) ;
return status(dir, envp) ;
}
else return simple_svc(dir, "-u", envp) ;
}
static int check (char const *dir, char const *const *envp)
{
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' ;
spawnit(argv, envp) ;
return 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)
{
complex_svc(dir, "-ru", 'U', envp) ;
return status(dir, envp) ;
}
static int lsb_start (char const *dir, char const *const *envp)
{
complex_svc(dir, "-u", 'U', envp) ;
return status(dir, envp) ;
}
static int lsb_stop (char const *dir, char const *const *envp)
{
complex_svc(dir, "-d", 'D', envp) ;
return status(dir, envp) ;
}
static int lsb_shutdown (char const *dir, char const *const *envp)
{
warnnolog() ;
complex_svc(dir, "-d", 'D', envp) ;
status(dir, envp) ;
return 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)
{
s6_svstatus_t svst ;
if (s6_svstatus_read(dir, &svst) && svst.flagwantup && svst.pid && !svst.flagfinishing)
complex_svc(dir, "-r", 'U', envp) ;
return status(dir, envp) ;
}
static info_t const commands[] =
{
{ .name = "1", .f = &usr1_h },
{ .name = "2", .f = &usr2_h },
{ .name = "a", .f = &alarm_h },
{ .name = "alarm", .f = &alarm_h },
{ .name = "c", .f = &cont_h },
{ .name = "check", .f = &check },
{ .name = "cont", .f = &cont_h },
{ .name = "d", .f = &down },
{ .name = "down", .f = &down },
{ .name = "e", .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 = "hup", .f = &hup_h },
{ .name = "i", .f = &int_h },
{ .name = "interrupt", .f = &int_h },
{ .name = "k", .f = &kill_h },
{ .name = "kill", .f = &kill_h },
{ .name = "o", .f = &once },
{ .name = "once", .f = &once },
{ .name = "p", .f = &pause_h },
{ .name = "pause", .f = &pause_h },
{ .name = "q", .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 = "start", .f = &lsb_start },
{ .name = "status", .f = &status },
{ .name = "stop", .f = &lsb_stop },
{ .name = "t", .f = &term_h },
{ .name = "term", .f = &term_h },
{ .name = "try-restart", .f = &lsb_tryrestart },
{ .name = "u", .f = &up },
{ .name = "up", .f = &up }
} ;
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_t 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] == '.' && (argv[0][1] == '/' || (argv[0][1] == '.' && argv[0][2] == '/'))) || argv[0][0] == '/')
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 ;
}
|