-/* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
+/*
+Copyright (c) 2001-2006, Gerrit Pape
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/* Taken from http://smarden.sunsite.dk/runit/sv.8.html:
+
+sv - control and manage services monitored by runsv
+
+sv [-v] [-w sec] command services
+/etc/init.d/service [-w sec] command
+
+The sv program reports the current status and controls the state of services
+monitored by the runsv(8) supervisor.
+
+services consists of one or more arguments, each argument naming a directory
+service used by runsv(8). If service doesn't start with a dot or slash,
+it is searched in the default services directory /var/service/, otherwise
+relative to the current directory.
+
+command is one of up, down, status, once, pause, cont, hup, alarm, interrupt,
+1, 2, term, kill, or exit, or start, stop, restart, shutdown, force-stop,
+force-reload, force-restart, force-shutdown.
+
+The sv program can be sym-linked to /etc/init.d/ to provide an LSB init
+script interface. The service to be controlled then is specified by the
+base name of the "init script".
+
+status
+ Report the current status of the service, and the appendant log service
+ if available, to standard output.
+up
+ If the service is not running, start it. If the service stops, restart it.
+down
+ If the service is running, send it the TERM signal, and the CONT signal.
+ If ./run exits, start ./finish if it exists. After it stops, do not
+ restart service.
+once
+ If the service is not running, start it. Do not restart it if it stops.
+pause cont hup alarm interrupt quit 1 2 term kill
+ If the service is running, send it the STOP, CONT, HUP, ALRM, INT, QUIT,
+ USR1, USR2, TERM, or KILL signal respectively.
+exit
+ If the service is running, send it the TERM signal, and the CONT signal.
+ Do not restart the service. If the service is down, and no log service
+ exists, runsv(8) exits. If the service is down and a log service exists,
+ send the TERM signal to the log service. If the log service is down,
+ runsv(8) exits. This command is ignored if it is given to an appendant
+ log service.
+
+sv actually looks only at the first character of above commands.
+
+Commands compatible to LSB init script actions:
+
+status
+ Same as status.
+start
+ Same as up, but wait up to 7 seconds for the command to take effect.
+ Then report the status or timeout. If the script ./check exists in
+ the service directory, sv runs this script to check whether the service
+ is up and available; it's considered to be available if ./check exits
+ with 0.
+stop
+ Same as down, but wait up to 7 seconds for the service to become down.
+ Then report the status or timeout.
+restart
+ Send the commands term, cont, and up to the service, and wait up to
+ 7 seconds for the service to restart. Then report the status or timeout.
+ If the script ./check exists in the service directory, sv runs this script
+ to check whether the service is up and available again; it's considered
+ to be available if ./check exits with 0.
+shutdown
+ Same as exit, but wait up to 7 seconds for the runsv(8) process
+ to terminate. Then report the status or timeout.
+force-stop
+ Same as down, but wait up to 7 seconds for the service to become down.
+ Then report the status, and on timeout send the service the kill command.
+force-reload
+ Send the service the term and cont commands, and wait up to
+ 7 seconds for the service to restart. Then report the status,
+ and on timeout send the service the kill command.
+force-restart
+ Send the service the term, cont and up commands, and wait up to
+ 7 seconds for the service to restart. Then report the status, and
+ on timeout send the service the kill command. If the script ./check
+ exists in the service directory, sv runs this script to check whether
+ the service is up and available again; it's considered to be available
+ if ./check exits with 0.
+force-shutdown
+ Same as exit, but wait up to 7 seconds for the runsv(8) process to
+ terminate. Then report the status, and on timeout send the service
+ the kill command.
+
+Additional Commands
+
+check
+ Check for the service to be in the state that's been requested. Wait up to
+ 7 seconds for the service to reach the requested state, then report
+ the status or timeout. If the requested state of the service is up,
+ and the script ./check exists in the service directory, sv runs
+ this script to check whether the service is up and running;
+ it's considered to be up if ./check exits with 0.
+
+Options
+
+-v
+ wait up to 7 seconds for the command to take effect.
+ Then report the status or timeout.
+-w sec
+ Override the default timeout of 7 seconds with sec seconds. Implies -v.
+
+Environment
+
+SVDIR
+ The environment variable $SVDIR overrides the default services directory
+ /var/service.
+SVWAIT
+ The environment variable $SVWAIT overrides the default 7 seconds to wait
+ for a command to take effect. It is overridden by the -w option.
+
+Exit Codes
+ sv exits 0, if the command was successfully sent to all services, and,
+ if it was told to wait, the command has taken effect to all services.
+
+ For each service that caused an error (e.g. the directory is not
+ controlled by a runsv(8) process, or sv timed out while waiting),
+ sv increases the exit code by one and exits non zero. The maximum
+ is 99. sv exits 100 on error.
+*/
+
+/* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
/* TODO: depends on runit_lib.c - review and reduce/eliminate */
#include <sys/poll.h>
#include <sys/file.h>
-#include "busybox.h"
+#include "libbb.h"
#include "runit_lib.h"
-static char *action;
-static char *acts;
-static char *varservice = "/var/service/";
-static char **service;
-static char **servicex;
-static unsigned services;
-static unsigned rc = 0;
-static unsigned verbose = 0;
-static unsigned long waitsec = 7;
-static unsigned kll = 0;
-static struct taia tstart, tnow, tdiff;
-static struct tai tstatus;
+struct globals {
+ const char *acts;
+ char **service;
+ unsigned rc;
+/* "Bernstein" time format: unix + 0x400000000000000aULL */
+ uint64_t tstart, tnow;
+ svstatus_t svstatus;
+};
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define acts (G.acts )
+#define service (G.service )
+#define rc (G.rc )
+#define tstart (G.tstart )
+#define tnow (G.tnow )
+#define svstatus (G.svstatus )
+#define INIT_G() do { } while (0)
-static int (*act)(char*) = 0;
-static int (*cbk)(char*) = 0;
-static int curdir, fd, r;
-static char svstatus[20];
-
-#define usage() bb_show_usage()
-
-static void fatal_cannot(char *m1)
+static void fatal_cannot(const char *m1) NORETURN;
+static void fatal_cannot(const char *m1)
{
- bb_perror_msg("fatal: cannot %s", m1);
+ bb_perror_msg("fatal: can't %s", m1);
_exit(151);
}
-static void out(char *p, char *m1)
+static void out(const char *p, const char *m1)
{
printf("%s%s: %s", p, *service, m1);
if (errno) {
printf(": %s", strerror(errno));
}
- puts(""); /* will also flush the output */
+ bb_putchar('\n'); /* will also flush the output */
}
-#define FAIL "fail: "
#define WARN "warning: "
#define OK "ok: "
-#define RUN "run: "
-#define FINISH "finish: "
-#define DOWN "down: "
-#define TIMEOUT "timeout: "
-#define KILL "kill: "
-
-static void fail(char *m1) { ++rc; out(FAIL, m1); }
-static void failx(char *m1) { errno = 0; fail(m1); }
-static void warn_cannot(char *m1) { ++rc; out("warning: cannot ", m1); }
-static void warnx_cannot(char *m1) { errno = 0; warn_cannot(m1); }
-static void ok(char *m1) { errno = 0; out(OK, m1); }
+
+static void fail(const char *m1)
+{
+ ++rc;
+ out("fail: ", m1);
+}
+static void failx(const char *m1)
+{
+ errno = 0;
+ fail(m1);
+}
+static void warn(const char *m1)
+{
+ ++rc;
+ /* "warning: <service>: <m1>\n" */
+ out("warning: ", m1);
+}
+static void ok(const char *m1)
+{
+ errno = 0;
+ out(OK, m1);
+}
static int svstatus_get(void)
{
- if ((fd = open_write("supervise/ok")) == -1) {
+ int fd, r;
+
+ fd = open_write("supervise/ok");
+ if (fd == -1) {
if (errno == ENODEV) {
*acts == 'x' ? ok("runsv not running")
: failx("runsv not running");
return 0;
}
- warn_cannot("open supervise/ok");
+ warn("can't open supervise/ok");
return -1;
}
close(fd);
- if ((fd = open_read("supervise/status")) == -1) {
- warn_cannot("open supervise/status");
+ fd = open_read("supervise/status");
+ if (fd == -1) {
+ warn("can't open supervise/status");
return -1;
}
- r = read(fd, svstatus, 20);
+ r = read(fd, &svstatus, 20);
close(fd);
- switch(r) {
- case 20: break;
- case -1: warn_cannot("read supervise/status"); return -1;
- default: warnx_cannot("read supervise/status: bad format"); return -1;
+ switch (r) {
+ case 20:
+ break;
+ case -1:
+ warn("can't read supervise/status");
+ return -1;
+ default:
+ errno = 0;
+ warn("can't read supervise/status: bad format");
+ return -1;
}
return 1;
}
-static unsigned svstatus_print(char *m)
+static unsigned svstatus_print(const char *m)
{
+ int diff;
int pid;
int normallyup = 0;
struct stat s;
-
+ uint64_t timestamp;
+
if (stat("down", &s) == -1) {
if (errno != ENOENT) {
- bb_perror_msg(WARN"cannot stat %s/down", *service);
+ bb_perror_msg(WARN"can't stat %s/down", *service);
return 0;
}
normallyup = 1;
}
- pid = (unsigned char) svstatus[15];
- pid <<= 8; pid += (unsigned char)svstatus[14];
- pid <<= 8; pid += (unsigned char)svstatus[13];
- pid <<= 8; pid += (unsigned char)svstatus[12];
- tai_unpack(svstatus, &tstatus);
+ pid = SWAP_LE32(svstatus.pid_le32);
+ timestamp = SWAP_BE64(svstatus.time_be64);
if (pid) {
- switch (svstatus[19]) {
- case 1: printf(RUN); break;
- case 2: printf(FINISH); break;
+ switch (svstatus.run_or_finish) {
+ case 1: printf("run: "); break;
+ case 2: printf("finish: "); break;
}
printf("%s: (pid %d) ", m, pid);
+ } else {
+ printf("down: %s: ", m);
}
- else {
- printf(DOWN"%s: ", m);
+ diff = tnow - timestamp;
+ printf("%us", (diff < 0 ? 0 : diff));
+ if (pid) {
+ if (!normallyup) printf(", normally down");
+ if (svstatus.paused) printf(", paused");
+ if (svstatus.want == 'd') printf(", want down");
+ if (svstatus.got_term) printf(", got TERM");
+ } else {
+ if (normallyup) printf(", normally up");
+ if (svstatus.want == 'u') printf(", want up");
}
- printf("%lus", (unsigned long)(tnow.sec.x < tstatus.x ? 0 : tnow.sec.x-tstatus.x));
- if (pid && !normallyup) printf(", normally down");
- if (!pid && normallyup) printf(", normally up");
- if (pid && svstatus[16]) printf(", paused");
- if (!pid && (svstatus[17] == 'u')) printf(", want up");
- if (pid && (svstatus[17] == 'd')) printf(", want down");
- if (pid && svstatus[18]) printf(", got TERM");
return pid ? 1 : 2;
}
-static int status(char *unused)
+static int status(const char *unused UNUSED_PARAM)
{
- r = svstatus_get();
- switch(r) { case -1: case 0: return 0; }
+ int r;
+
+ if (svstatus_get() <= 0)
+ return 0;
+
r = svstatus_print(*service);
if (chdir("log") == -1) {
if (errno != ENOENT) {
- printf("; log: "WARN"cannot change to log service directory: %s",
+ printf("; log: "WARN"can't change to log service directory: %s",
strerror(errno));
}
} else if (svstatus_get()) {
printf("; ");
svstatus_print("log");
}
- puts(""); /* will also flush the output */
+ bb_putchar('\n'); /* will also flush the output */
return r;
}
if (stat("check", &s) == -1) {
if (errno == ENOENT) return 1;
- bb_perror_msg(WARN"cannot stat %s/check", *service);
+ bb_perror_msg(WARN"can't stat %s/check", *service);
return 0;
}
/* if (!(s.st_mode & S_IXUSR)) return 1; */
- if ((pid = fork()) == -1) {
- bb_perror_msg(WARN"cannot fork for %s/check", *service);
+ prog[0] = (char*)"./check";
+ prog[1] = NULL;
+ pid = spawn(prog);
+ if (pid <= 0) {
+ bb_perror_msg(WARN"can't %s child %s/check", "run", *service);
return 0;
}
- if (!pid) {
- prog[0] = "./check";
- prog[1] = 0;
- close(1);
- execve("check", prog, environ);
- bb_perror_msg(WARN"cannot run %s/check", *service);
- _exit(0);
- }
- while (wait_pid(&w, pid) == -1) {
- if (errno == EINTR) continue;
- bb_perror_msg(WARN"cannot wait for child %s/check", *service);
+ while (safe_waitpid(pid, &w, 0) == -1) {
+ bb_perror_msg(WARN"can't %s child %s/check", "wait for", *service);
return 0;
}
- return !wait_exitcode(w);
+ return WEXITSTATUS(w) == 0;
}
-static int check(char *a)
+static int check(const char *a)
{
- unsigned pid;
-
- if ((r = svstatus_get()) == -1) return -1;
- if (r == 0) { if (*a == 'x') return 1; return -1; }
- pid = (unsigned char)svstatus[15];
- pid <<= 8; pid += (unsigned char)svstatus[14];
- pid <<= 8; pid += (unsigned char)svstatus[13];
- pid <<= 8; pid += (unsigned char)svstatus[12];
+ int r;
+ unsigned pid_le32;
+ uint64_t timestamp;
+
+ r = svstatus_get();
+ if (r == -1)
+ return -1;
+ if (r == 0) {
+ if (*a == 'x')
+ return 1;
+ return -1;
+ }
+ pid_le32 = svstatus.pid_le32;
switch (*a) {
- case 'x': return 0;
+ case 'x':
+ return 0;
case 'u':
- if (!pid || svstatus[19] != 1) return 0;
+ if (!pid_le32 || svstatus.run_or_finish != 1) return 0;
if (!checkscript()) return 0;
break;
- case 'd': if (pid) return 0; break;
- case 'c': if (pid) if (!checkscript()) return 0; break;
+ case 'd':
+ if (pid_le32) return 0;
+ break;
+ case 'c':
+ if (pid_le32 && !checkscript()) return 0;
+ break;
case 't':
- if (!pid && svstatus[17] == 'd') break;
- tai_unpack(svstatus, &tstatus);
- if ((tstart.sec.x > tstatus.x) || !pid || svstatus[18] || !checkscript())
+ if (!pid_le32 && svstatus.want == 'd') break;
+ timestamp = SWAP_BE64(svstatus.time_be64);
+ if ((tstart > timestamp) || !pid_le32 || svstatus.got_term || !checkscript())
return 0;
break;
case 'o':
- tai_unpack(svstatus, &tstatus);
- if ((!pid && tstart.sec.x > tstatus.x) || (pid && svstatus[17] != 'd'))
+ timestamp = SWAP_BE64(svstatus.time_be64);
+ if ((!pid_le32 && tstart > timestamp) || (pid_le32 && svstatus.want != 'd'))
return 0;
}
- printf(OK); svstatus_print(*service); puts(""); /* will also flush the output */
+ printf(OK);
+ svstatus_print(*service);
+ bb_putchar('\n'); /* will also flush the output */
return 1;
}
-static int control(char *a)
+static int control(const char *a)
{
- if (svstatus_get() <= 0) return -1;
- if (svstatus[17] == *a) return 0;
- if ((fd = open_write("supervise/control")) == -1) {
+ int fd, r, l;
+
+/* Is it an optimization?
+ It causes problems with "sv o SRV; ...; sv d SRV"
+ ('d' is not passed to SRV because its .want == 'd'):
+ if (svstatus_get() <= 0)
+ return -1;
+ if (svstatus.want == *a)
+ return 0;
+*/
+ fd = open_write("supervise/control");
+ if (fd == -1) {
if (errno != ENODEV)
- warn_cannot("open supervise/control");
+ warn("can't open supervise/control");
else
*a == 'x' ? ok("runsv not running") : failx("runsv not running");
return -1;
}
- r = write(fd, a, strlen(a));
+ l = strlen(a);
+ r = write(fd, a, l);
close(fd);
- if (r != strlen(a)) {
- warn_cannot("write to supervise/control");
+ if (r != l) {
+ warn("can't write to supervise/control");
return -1;
}
return 1;
}
-int sv_main(int argc, char **argv)
+int sv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int sv_main(int argc UNUSED_PARAM, char **argv)
{
unsigned opt;
- unsigned i, want_exit;
char *x;
+ char *action;
+ const char *varservice = CONFIG_SV_DEFAULT_SERVICE_DIR;
+ unsigned waitsec = 7;
+ smallint kll = 0;
+ int verbose = 0;
+ int (*act)(const char*);
+ int (*cbk)(const char*);
+ int curdir;
- for (i = strlen(*argv); i; --i)
- if ((*argv)[i-1] == '/')
- break;
- *argv += i;
- service = argv;
- services = 1;
- if ((x = getenv("SVDIR"))) varservice = x;
- if ((x = getenv("SVWAIT"))) waitsec = xatoul(x);
- /* TODO: V can be handled internally by getopt_ulflags */
- opt = getopt32(argc, argv, "w:vV", &x);
- if (opt & 1) waitsec = xatoul(x);
- if (opt & 2) verbose = 1;
- if (opt & 4) usage();
- if (!(action = *argv++)) usage();
- --argc;
- service = argv; services = argc;
- if (!*service) usage();
-
- taia_now(&tnow); tstart = tnow;
- if ((curdir = open_read(".")) == -1)
+ INIT_G();
+
+ xfunc_error_retval = 100;
+
+ x = getenv("SVDIR");
+ if (x) varservice = x;
+ x = getenv("SVWAIT");
+ if (x) waitsec = xatou(x);
+
+ opt_complementary = "w+:vv"; /* -w N, -v is a counter */
+ opt = getopt32(argv, "w:v", &waitsec, &verbose);
+ argv += optind;
+ action = *argv++;
+ if (!action || !*argv) bb_show_usage();
+
+ tnow = time(NULL) + 0x400000000000000aULL;
+ tstart = tnow;
+ curdir = open_read(".");
+ if (curdir == -1)
fatal_cannot("open current directory");
- act = &control; acts = "s";
- if (verbose) cbk = ✓
+ act = &control;
+ acts = "s";
+ cbk = ✓
+
switch (*action) {
- case 'x': case 'e':
- acts = "x"; break;
- case 'X': case 'E':
- acts = "x"; kll = 1; cbk = ✓ break;
+ case 'x':
+ case 'e':
+ acts = "x";
+ if (!verbose) cbk = NULL;
+ break;
+ case 'X':
+ case 'E':
+ acts = "x";
+ kll = 1;
+ break;
case 'D':
- acts = "d"; kll = 1; cbk = ✓ break;
+ acts = "d";
+ kll = 1;
+ break;
case 'T':
- acts = "tc"; kll = 1; cbk = ✓ break;
+ acts = "tc";
+ kll = 1;
+ break;
case 'c':
- if (!str_diff(action, "check")) {
- act = 0;
+ if (str_equal(action, "check")) {
+ act = NULL;
acts = "c";
- cbk = ✓
break;
}
case 'u': case 'd': case 'o': case 't': case 'p': case 'h':
case 'a': case 'i': case 'k': case 'q': case '1': case '2':
- action[1] = 0; acts = action; break;
+ action[1] = '\0';
+ acts = action;
+ if (!verbose) cbk = NULL;
+ break;
case 's':
- if (!str_diff(action, "shutdown")) {
+ if (str_equal(action, "shutdown")) {
acts = "x";
- cbk = ✓
break;
}
- if (!str_diff(action, "start")) {
+ if (str_equal(action, "start")) {
acts = "u";
- cbk = ✓
break;
}
- if (!str_diff(action, "stop")) {
+ if (str_equal(action, "stop")) {
acts = "d";
- cbk = ✓
break;
}
- act = &status; cbk = 0; break;
+ /* "status" */
+ act = &status;
+ cbk = NULL;
+ break;
case 'r':
- if (!str_diff(action, "restart")) {
+ if (str_equal(action, "restart")) {
acts = "tcu";
- cbk = ✓
break;
}
- usage();
+ bb_show_usage();
case 'f':
- if (!str_diff(action, "force-reload"))
- { acts = "tc"; kll = 1; cbk = ✓ break; }
- if (!str_diff(action, "force-restart"))
- { acts = "tcu"; kll = 1; cbk = ✓ break; }
- if (!str_diff(action, "force-shutdown"))
- { acts = "x"; kll = 1; cbk = ✓ break; }
- if (!str_diff(action, "force-stop"))
- { acts = "d"; kll = 1; cbk = ✓ break; }
+ if (str_equal(action, "force-reload")) {
+ acts = "tc";
+ kll = 1;
+ break;
+ }
+ if (str_equal(action, "force-restart")) {
+ acts = "tcu";
+ kll = 1;
+ break;
+ }
+ if (str_equal(action, "force-shutdown")) {
+ acts = "x";
+ kll = 1;
+ break;
+ }
+ if (str_equal(action, "force-stop")) {
+ acts = "d";
+ kll = 1;
+ break;
+ }
default:
- usage();
+ bb_show_usage();
}
- servicex = service;
- for (i = 0; i < services; ++i) {
- if ((**service != '/') && (**service != '.')) {
- if ((chdir(varservice) == -1) || (chdir(*service) == -1)) {
- fail("cannot change to service directory");
- *service = 0;
- }
- } else if (chdir(*service) == -1) {
- fail("cannot change to service directory");
- *service = 0;
+ service = argv;
+ while ((x = *service) != NULL) {
+ if (x[0] != '/' && x[0] != '.') {
+ if (chdir(varservice) == -1)
+ goto chdir_failed_0;
+ }
+ if (chdir(x) == -1) {
+ chdir_failed_0:
+ fail("can't change to service directory");
+ goto nullify_service_0;
+ }
+ if (act && (act(acts) == -1)) {
+ nullify_service_0:
+ *service = (char*) -1L; /* "dead" */
}
- if (*service) if (act && (act(acts) == -1)) *service = 0;
- if (fchdir(curdir) == -1) fatal_cannot("change to original directory");
+ if (fchdir(curdir) == -1)
+ fatal_cannot("change to original directory");
service++;
}
- if (*cbk)
- for (;;) {
- taia_sub(&tdiff, &tnow, &tstart);
- service = servicex; want_exit = 1;
- for (i = 0; i < services; ++i, ++service) {
- if (!*service) continue;
- if ((**service != '/') && (**service != '.')) {
- if ((chdir(varservice) == -1) || (chdir(*service) == -1)) {
- fail("cannot change to service directory");
- *service = 0;
- }
- } else if (chdir(*service) == -1) {
- fail("cannot change to service directory");
- *service = 0;
- }
- if (*service) { if (cbk(acts) != 0) *service = 0; else want_exit = 0; }
- if (*service && taia_approx(&tdiff) > waitsec) {
- kll ? printf(KILL) : printf(TIMEOUT);
- if (svstatus_get() > 0) { svstatus_print(*service); ++rc; }
- puts(""); /* will also flush the output */
- if (kll) control("k");
- *service = 0;
+ if (cbk) while (1) {
+ int want_exit;
+ int diff;
+
+ diff = tnow - tstart;
+ service = argv;
+ want_exit = 1;
+ while ((x = *service) != NULL) {
+ if (x == (char*) -1L) /* "dead" */
+ goto next;
+ if (x[0] != '/' && x[0] != '.') {
+ if (chdir(varservice) == -1)
+ goto chdir_failed;
+ }
+ if (chdir(x) == -1) {
+ chdir_failed:
+ fail("can't change to service directory");
+ goto nullify_service;
+ }
+ if (cbk(acts) != 0)
+ goto nullify_service;
+ want_exit = 0;
+ if (diff >= waitsec) {
+ printf(kll ? "kill: " : "timeout: ");
+ if (svstatus_get() > 0) {
+ svstatus_print(x);
+ ++rc;
}
- if (fchdir(curdir) == -1)
- fatal_cannot("change to original directory");
+ bb_putchar('\n'); /* will also flush the output */
+ if (kll)
+ control("k");
+ nullify_service:
+ *service = (char*) -1L; /* "dead" */
}
- if (want_exit) break;
- usleep(420000);
- taia_now(&tnow);
+ if (fchdir(curdir) == -1)
+ fatal_cannot("change to original directory");
+ next:
+ service++;
}
+ if (want_exit) break;
+ usleep(420000);
+ tnow = time(NULL) + 0x400000000000000aULL;
+ }
return rc > 99 ? 99 : rc;
}