1 /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
2 /* TODO: depends on runit_lib.c - review and reduce/eliminate */
10 static const char *acts;
11 static const char *varservice = "/var/service/";
12 static char **service;
13 static char **servicex;
14 static unsigned services;
16 static unsigned verbose;
17 static unsigned long waitsec = 7;
19 static struct taia tstart, tnow, tdiff;
20 static struct tai tstatus;
22 static int (*act)(const char*);
23 static int (*cbk)(const char*);
25 static int curdir, fd, r;
26 static char svstatus[20];
28 #define usage() bb_show_usage()
30 static void fatal_cannot(const char *m1)
32 bb_perror_msg("fatal: cannot %s", m1);
36 static void out(const char *p, const char *m1)
38 printf("%s%s: %s", p, *service, m1);
40 printf(": %s", strerror(errno));
42 puts(""); /* will also flush the output */
46 #define WARN "warning: "
49 #define FINISH "finish: "
51 #define TIMEOUT "timeout: "
54 static void fail(const char *m1) { ++rc; out(FAIL, m1); }
55 static void failx(const char *m1) { errno = 0; fail(m1); }
56 static void warn_cannot(const char *m1) { ++rc; out("warning: cannot ", m1); }
57 static void warnx_cannot(const char *m1) { errno = 0; warn_cannot(m1); }
58 static void ok(const char *m1) { errno = 0; out(OK, m1); }
60 static int svstatus_get(void)
62 fd = open_write("supervise/ok");
64 if (errno == ENODEV) {
65 *acts == 'x' ? ok("runsv not running")
66 : failx("runsv not running");
69 warn_cannot("open supervise/ok");
73 fd = open_read("supervise/status");
75 warn_cannot("open supervise/status");
78 r = read(fd, svstatus, 20);
82 case -1: warn_cannot("read supervise/status"); return -1;
83 default: warnx_cannot("read supervise/status: bad format"); return -1;
88 static unsigned svstatus_print(const char *m)
94 if (stat("down", &s) == -1) {
95 if (errno != ENOENT) {
96 bb_perror_msg(WARN"cannot stat %s/down", *service);
101 pid = (unsigned char) svstatus[15];
102 pid <<= 8; pid += (unsigned char)svstatus[14];
103 pid <<= 8; pid += (unsigned char)svstatus[13];
104 pid <<= 8; pid += (unsigned char)svstatus[12];
105 tai_unpack(svstatus, &tstatus);
107 switch (svstatus[19]) {
108 case 1: printf(RUN); break;
109 case 2: printf(FINISH); break;
111 printf("%s: (pid %d) ", m, pid);
114 printf(DOWN"%s: ", m);
116 printf("%lus", (unsigned long)(tnow.sec.x < tstatus.x ? 0 : tnow.sec.x-tstatus.x));
117 if (pid && !normallyup) printf(", normally down");
118 if (!pid && normallyup) printf(", normally up");
119 if (pid && svstatus[16]) printf(", paused");
120 if (!pid && (svstatus[17] == 'u')) printf(", want up");
121 if (pid && (svstatus[17] == 'd')) printf(", want down");
122 if (pid && svstatus[18]) printf(", got TERM");
126 static int status(const char *unused)
129 switch (r) { case -1: case 0: return 0; }
130 r = svstatus_print(*service);
131 if (chdir("log") == -1) {
132 if (errno != ENOENT) {
133 printf("; log: "WARN"cannot change to log service directory: %s",
136 } else if (svstatus_get()) {
138 svstatus_print("log");
140 puts(""); /* will also flush the output */
144 static int checkscript(void)
150 if (stat("check", &s) == -1) {
151 if (errno == ENOENT) return 1;
152 bb_perror_msg(WARN"cannot stat %s/check", *service);
155 /* if (!(s.st_mode & S_IXUSR)) return 1; */
156 if ((pid = fork()) == -1) {
157 bb_perror_msg(WARN"cannot fork for %s/check", *service);
161 prog[0] = (char*)"./check";
164 execve("check", prog, environ);
165 bb_perror_msg(WARN"cannot run %s/check", *service);
168 while (wait_pid(&w, pid) == -1) {
169 if (errno == EINTR) continue;
170 bb_perror_msg(WARN"cannot wait for child %s/check", *service);
173 return !wait_exitcode(w);
176 static int check(const char *a)
180 if ((r = svstatus_get()) == -1) return -1;
181 if (r == 0) { if (*a == 'x') return 1; return -1; }
182 pid = (unsigned char)svstatus[15];
183 pid <<= 8; pid += (unsigned char)svstatus[14];
184 pid <<= 8; pid += (unsigned char)svstatus[13];
185 pid <<= 8; pid += (unsigned char)svstatus[12];
189 if (!pid || svstatus[19] != 1) return 0;
190 if (!checkscript()) return 0;
192 case 'd': if (pid) return 0; break;
193 case 'c': if (pid) if (!checkscript()) return 0; break;
195 if (!pid && svstatus[17] == 'd') break;
196 tai_unpack(svstatus, &tstatus);
197 if ((tstart.sec.x > tstatus.x) || !pid || svstatus[18] || !checkscript())
201 tai_unpack(svstatus, &tstatus);
202 if ((!pid && tstart.sec.x > tstatus.x) || (pid && svstatus[17] != 'd'))
206 svstatus_print(*service);
207 puts(""); /* will also flush the output */
211 static int control(const char *a)
213 if (svstatus_get() <= 0) return -1;
214 if (svstatus[17] == *a) return 0;
215 fd = open_write("supervise/control");
218 warn_cannot("open supervise/control");
220 *a == 'x' ? ok("runsv not running") : failx("runsv not running");
223 r = write(fd, a, strlen(a));
225 if (r != strlen(a)) {
226 warn_cannot("write to supervise/control");
232 int sv_main(int argc, char **argv)
235 unsigned i, want_exit;
238 for (i = strlen(*argv); i; --i)
239 if ((*argv)[i-1] == '/')
244 if ((x = getenv("SVDIR"))) varservice = x;
245 if ((x = getenv("SVWAIT"))) waitsec = xatoul(x);
246 /* TODO: V can be handled internally by getopt_ulflags */
247 opt = getopt32(argc, argv, "w:vV", &x);
248 if (opt & 1) waitsec = xatoul(x);
249 if (opt & 2) verbose = 1;
250 if (opt & 4) usage();
251 if (!(action = *argv++)) usage();
255 if (!*service) usage();
259 curdir = open_read(".");
261 fatal_cannot("open current directory");
271 acts = "x"; kll = 1; cbk = ✓ break;
273 acts = "d"; kll = 1; cbk = ✓ break;
275 acts = "tc"; kll = 1; cbk = ✓ break;
277 if (!str_diff(action, "check")) {
283 case 'u': case 'd': case 'o': case 't': case 'p': case 'h':
284 case 'a': case 'i': case 'k': case 'q': case '1': case '2':
285 action[1] = 0; acts = action; break;
287 if (!str_diff(action, "shutdown")) {
292 if (!str_diff(action, "start")) {
297 if (!str_diff(action, "stop")) {
306 if (!str_diff(action, "restart")) {
313 if (!str_diff(action, "force-reload"))
314 { acts = "tc"; kll = 1; cbk = ✓ break; }
315 if (!str_diff(action, "force-restart"))
316 { acts = "tcu"; kll = 1; cbk = ✓ break; }
317 if (!str_diff(action, "force-shutdown"))
318 { acts = "x"; kll = 1; cbk = ✓ break; }
319 if (!str_diff(action, "force-stop"))
320 { acts = "d"; kll = 1; cbk = ✓ break; }
326 for (i = 0; i < services; ++i) {
327 if ((**service != '/') && (**service != '.')) {
328 if ((chdir(varservice) == -1) || (chdir(*service) == -1)) {
329 fail("cannot change to service directory");
332 } else if (chdir(*service) == -1) {
333 fail("cannot change to service directory");
336 if (*service) if (act && (act(acts) == -1)) *service = 0;
337 if (fchdir(curdir) == -1) fatal_cannot("change to original directory");
343 //TODO: tdiff resolution is way too high. seconds will be enough
344 taia_sub(&tdiff, &tnow, &tstart);
345 service = servicex; want_exit = 1;
346 for (i = 0; i < services; ++i, ++service) {
349 if ((**service != '/') && (**service != '.')) {
350 if (chdir(varservice) == -1)
353 if (chdir(*service) == -1) {
355 fail("cannot change to service directory");
356 goto nullify_service;
359 goto nullify_service;
361 //if (taia_approx(&tdiff) > waitsec)
362 if (tdiff.sec.x >= waitsec) {
363 kll ? printf(KILL) : printf(TIMEOUT);
364 if (svstatus_get() > 0) {
365 svstatus_print(*service);
368 puts(""); /* will also flush the output */
374 if (fchdir(curdir) == -1)
375 fatal_cannot("change to original directory");
377 if (want_exit) break;
382 return rc > 99 ? 99 : rc;