2007-03-13 05:04:52 +05:30
|
|
|
/*
|
|
|
|
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.
|
|
|
|
|
2017-08-02 17:56:33 +05:30
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
2007-03-13 05:04:52 +05:30
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-05-16 01:09:51 +05:30
|
|
|
/* Taken from http://smarden.org/runit/sv.8.html:
|
2007-03-13 05:04:52 +05:30
|
|
|
|
|
|
|
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
|
2017-05-16 01:09:51 +05:30
|
|
|
service used by runsv(8). If service doesn't start with a dot or slash and
|
|
|
|
doesn't end with a slash, it is searched in the default services directory
|
|
|
|
/var/service/, otherwise relative to the current directory.
|
2007-03-13 05:04:52 +05:30
|
|
|
|
|
|
|
command is one of up, down, status, once, pause, cont, hup, alarm, interrupt,
|
2017-05-16 01:09:51 +05:30
|
|
|
1, 2, term, kill, or exit, or start, stop, reload, restart, shutdown,
|
|
|
|
force-stop, force-reload, force-restart, force-shutdown, try-restart.
|
2007-03-13 05:04:52 +05:30
|
|
|
|
|
|
|
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,
|
2017-05-16 01:09:51 +05:30
|
|
|
runsv(8) closes the standard input of the log service and waits for it to
|
|
|
|
terminate. If the log service is down, runsv(8) exits. This command is
|
|
|
|
ignored if it is given to an appendant log service.
|
2007-03-13 05:04:52 +05:30
|
|
|
|
|
|
|
sv actually looks only at the first character of above commands.
|
|
|
|
|
2007-08-03 16:28:12 +05:30
|
|
|
Commands compatible to LSB init script actions:
|
|
|
|
|
2007-03-13 05:04:52 +05:30
|
|
|
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.
|
2017-05-16 01:09:51 +05:30
|
|
|
reload
|
|
|
|
Same as hup, and additionally report the status afterwards.
|
2007-03-13 05:04:52 +05:30
|
|
|
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
|
2007-08-03 16:28:12 +05:30
|
|
|
the service is up and available again; it's considered to be available
|
2007-03-13 05:04:52 +05:30
|
|
|
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.
|
2017-05-16 01:09:51 +05:30
|
|
|
try-restart
|
|
|
|
if the service is running, send it the term and cont commands, and wait up to
|
|
|
|
7 seconds for the service to restart. Then report the status or timeout.
|
2007-03-13 05:04:52 +05:30
|
|
|
|
|
|
|
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
|
2017-05-16 01:09:51 +05:30
|
|
|
If the command is up, down, term, once, cont, or exit, then wait up to 7
|
|
|
|
seconds for the command to take effect. Then report the status or timeout.
|
2007-03-13 05:04:52 +05:30
|
|
|
-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.
|
|
|
|
*/
|
|
|
|
|
2008-03-02 18:23:15 +05:30
|
|
|
/* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
|
2015-10-19 04:11:28 +05:30
|
|
|
|
|
|
|
//config:config SV
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "sv (7.8 kb)"
|
2015-10-19 04:11:28 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: sv reports the current status and controls the state of services
|
|
|
|
//config: monitored by the runsv supervisor.
|
2015-10-19 04:11:28 +05:30
|
|
|
//config:
|
|
|
|
//config:config SV_DEFAULT_SERVICE_DIR
|
|
|
|
//config: string "Default directory for services"
|
|
|
|
//config: default "/var/service"
|
|
|
|
//config: depends on SV
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Default directory for services.
|
|
|
|
//config: Defaults to "/var/service"
|
2016-12-05 18:26:40 +05:30
|
|
|
//config:
|
|
|
|
//config:config SVC
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "svc (7.8 kb)"
|
2016-12-05 18:26:40 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: svc controls the state of services monitored by the runsv supervisor.
|
|
|
|
//config: It is comaptible with daemontools command with the same name.
|
2015-10-19 04:11:28 +05:30
|
|
|
|
|
|
|
//applet:IF_SV(APPLET(sv, BB_DIR_USR_BIN, BB_SUID_DROP))
|
2016-12-08 16:54:48 +05:30
|
|
|
//applet:IF_SVC(APPLET(svc, BB_DIR_USR_BIN, BB_SUID_DROP))
|
2015-10-19 04:11:28 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SV) += sv.o
|
2016-12-05 18:26:40 +05:30
|
|
|
//kbuild:lib-$(CONFIG_SVC) += sv.o
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2006-11-18 00:28:49 +05:30
|
|
|
#include <sys/file.h>
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2006-11-18 00:28:49 +05:30
|
|
|
#include "runit_lib.h"
|
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
struct globals {
|
|
|
|
const char *acts;
|
|
|
|
char **service;
|
|
|
|
unsigned rc;
|
2007-08-20 22:57:40 +05:30
|
|
|
/* "Bernstein" time format: unix + 0x400000000000000aULL */
|
2007-09-28 15:59:17 +05:30
|
|
|
uint64_t tstart, tnow;
|
|
|
|
svstatus_t svstatus;
|
2017-05-16 01:09:51 +05:30
|
|
|
unsigned islog;
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2007-09-28 15:59:17 +05:30
|
|
|
#define acts (G.acts )
|
|
|
|
#define service (G.service )
|
|
|
|
#define rc (G.rc )
|
|
|
|
#define tstart (G.tstart )
|
|
|
|
#define tnow (G.tnow )
|
|
|
|
#define svstatus (G.svstatus )
|
2017-05-16 01:09:51 +05:30
|
|
|
#define islog (G.islog )
|
2016-04-21 21:48:48 +05:30
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
2006-11-18 00:28:49 +05:30
|
|
|
|
|
|
|
|
2016-11-28 05:52:57 +05:30
|
|
|
#define str_equal(s,t) (strcmp((s), (t)) == 0)
|
2010-10-13 16:23:27 +05:30
|
|
|
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void fatal_cannot(const char *m1) NORETURN;
|
2007-01-30 05:13:52 +05:30
|
|
|
static void fatal_cannot(const char *m1)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
2008-07-21 19:16:54 +05:30
|
|
|
bb_perror_msg("fatal: can't %s", m1);
|
2006-11-18 00:28:49 +05:30
|
|
|
_exit(151);
|
|
|
|
}
|
|
|
|
|
2007-01-30 05:13:52 +05:30
|
|
|
static void out(const char *p, const char *m1)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
2017-05-16 01:09:51 +05:30
|
|
|
printf("%s%s%s: %s", p, *service, islog ? "/log" : "", m1);
|
2006-11-18 00:28:49 +05:30
|
|
|
if (errno) {
|
|
|
|
printf(": %s", strerror(errno));
|
|
|
|
}
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n'); /* will also flush the output */
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#define WARN "warning: "
|
|
|
|
#define OK "ok: "
|
|
|
|
|
2007-04-17 04:02:04 +05:30
|
|
|
static void fail(const char *m1)
|
|
|
|
{
|
2007-02-02 06:49:09 +05:30
|
|
|
++rc;
|
|
|
|
out("fail: ", m1);
|
|
|
|
}
|
2007-04-17 04:02:04 +05:30
|
|
|
static void failx(const char *m1)
|
|
|
|
{
|
2007-02-02 06:49:09 +05:30
|
|
|
errno = 0;
|
|
|
|
fail(m1);
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
static void warn(const char *m1)
|
2007-04-17 04:02:04 +05:30
|
|
|
{
|
2007-02-02 06:49:09 +05:30
|
|
|
++rc;
|
2007-08-20 22:57:40 +05:30
|
|
|
/* "warning: <service>: <m1>\n" */
|
|
|
|
out("warning: ", m1);
|
2007-02-02 06:49:09 +05:30
|
|
|
}
|
2007-04-17 04:02:04 +05:30
|
|
|
static void ok(const char *m1)
|
|
|
|
{
|
2007-02-02 06:49:09 +05:30
|
|
|
errno = 0;
|
|
|
|
out(OK, m1);
|
|
|
|
}
|
2006-11-18 00:28:49 +05:30
|
|
|
|
|
|
|
static int svstatus_get(void)
|
|
|
|
{
|
2007-02-02 06:49:09 +05:30
|
|
|
int fd, r;
|
|
|
|
|
2010-10-13 16:23:27 +05:30
|
|
|
fd = open("supervise/ok", O_WRONLY|O_NDELAY);
|
2007-01-30 05:13:52 +05:30
|
|
|
if (fd == -1) {
|
2006-11-18 00:28:49 +05:30
|
|
|
if (errno == ENODEV) {
|
|
|
|
*acts == 'x' ? ok("runsv not running")
|
|
|
|
: failx("runsv not running");
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-13 13:38:27 +05:30
|
|
|
warn("can't open supervise/ok");
|
2006-11-18 00:28:49 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
2010-10-13 16:23:27 +05:30
|
|
|
fd = open("supervise/status", O_RDONLY|O_NDELAY);
|
2007-01-30 05:13:52 +05:30
|
|
|
if (fd == -1) {
|
2009-11-13 13:38:27 +05:30
|
|
|
warn("can't open supervise/status");
|
2006-11-18 00:28:49 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
r = read(fd, &svstatus, 20);
|
2006-11-18 00:28:49 +05:30
|
|
|
close(fd);
|
2006-12-26 16:12:51 +05:30
|
|
|
switch (r) {
|
2007-08-20 22:57:40 +05:30
|
|
|
case 20:
|
|
|
|
break;
|
|
|
|
case -1:
|
2009-11-13 13:38:27 +05:30
|
|
|
warn("can't read supervise/status");
|
2007-08-20 22:57:40 +05:30
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
errno = 0;
|
2009-11-13 13:38:27 +05:30
|
|
|
warn("can't read supervise/status: bad format");
|
2007-08-20 22:57:40 +05:30
|
|
|
return -1;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-01-30 05:13:52 +05:30
|
|
|
static unsigned svstatus_print(const char *m)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
2007-08-20 22:57:40 +05:30
|
|
|
int diff;
|
2006-11-18 00:28:49 +05:30
|
|
|
int pid;
|
|
|
|
int normallyup = 0;
|
|
|
|
struct stat s;
|
2007-08-20 22:57:40 +05:30
|
|
|
uint64_t timestamp;
|
2007-01-11 22:50:00 +05:30
|
|
|
|
2006-11-18 00:28:49 +05:30
|
|
|
if (stat("down", &s) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg(WARN"can't stat %s/down", *service);
|
2006-11-18 00:28:49 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
normallyup = 1;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
pid = SWAP_LE32(svstatus.pid_le32);
|
|
|
|
timestamp = SWAP_BE64(svstatus.time_be64);
|
2017-05-16 01:09:51 +05:30
|
|
|
switch (svstatus.run_or_finish) {
|
|
|
|
case 0: printf("down: "); break;
|
2007-02-02 06:49:09 +05:30
|
|
|
case 1: printf("run: "); break;
|
|
|
|
case 2: printf("finish: "); break;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2017-05-16 01:09:51 +05:30
|
|
|
printf("%s: ", m);
|
|
|
|
if (svstatus.run_or_finish)
|
|
|
|
printf("(pid %d) ", pid);
|
2007-08-20 22:57:40 +05:30
|
|
|
diff = tnow - timestamp;
|
|
|
|
printf("%us", (diff < 0 ? 0 : diff));
|
2007-02-02 06:49:09 +05:30
|
|
|
if (pid) {
|
|
|
|
if (!normallyup) printf(", normally down");
|
2007-08-20 22:57:40 +05:30
|
|
|
if (svstatus.paused) printf(", paused");
|
|
|
|
if (svstatus.want == 'd') printf(", want down");
|
|
|
|
if (svstatus.got_term) printf(", got TERM");
|
2007-02-02 06:49:09 +05:30
|
|
|
} else {
|
|
|
|
if (normallyup) printf(", normally up");
|
2007-08-20 22:57:40 +05:30
|
|
|
if (svstatus.want == 'u') printf(", want up");
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
return pid ? 1 : 2;
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static int status(const char *unused UNUSED_PARAM)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
2007-02-02 06:49:09 +05:30
|
|
|
int r;
|
|
|
|
|
2009-07-15 03:54:08 +05:30
|
|
|
if (svstatus_get() <= 0)
|
|
|
|
return 0;
|
2007-02-02 06:49:09 +05:30
|
|
|
|
2006-11-18 00:28:49 +05:30
|
|
|
r = svstatus_print(*service);
|
2017-05-16 01:09:51 +05:30
|
|
|
islog = 1;
|
2006-11-18 00:28:49 +05:30
|
|
|
if (chdir("log") == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2017-05-16 01:09:51 +05:30
|
|
|
printf("; ");
|
|
|
|
warn("can't change directory");
|
|
|
|
} else
|
|
|
|
bb_putchar('\n');
|
|
|
|
} else {
|
2006-11-18 00:28:49 +05:30
|
|
|
printf("; ");
|
2017-05-16 01:09:51 +05:30
|
|
|
if (svstatus_get()) {
|
|
|
|
r = svstatus_print("log");
|
|
|
|
bb_putchar('\n');
|
|
|
|
}
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2017-05-16 01:09:51 +05:30
|
|
|
islog = 0;
|
2006-11-18 00:28:49 +05:30
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int checkscript(void)
|
|
|
|
{
|
|
|
|
char *prog[2];
|
|
|
|
struct stat s;
|
|
|
|
int pid, w;
|
|
|
|
|
|
|
|
if (stat("check", &s) == -1) {
|
|
|
|
if (errno == ENOENT) return 1;
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg(WARN"can't stat %s/check", *service);
|
2006-11-18 00:28:49 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* if (!(s.st_mode & S_IXUSR)) return 1; */
|
2007-03-26 19:05:09 +05:30
|
|
|
prog[0] = (char*)"./check";
|
|
|
|
prog[1] = NULL;
|
|
|
|
pid = spawn(prog);
|
|
|
|
if (pid <= 0) {
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg(WARN"can't %s child %s/check", "run", *service);
|
2006-11-18 00:28:49 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2008-01-03 01:25:04 +05:30
|
|
|
while (safe_waitpid(pid, &w, 0) == -1) {
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg(WARN"can't %s child %s/check", "wait for", *service);
|
2006-11-18 00:28:49 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2009-06-07 19:32:00 +05:30
|
|
|
return WEXITSTATUS(w) == 0;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
|
2007-01-30 05:13:52 +05:30
|
|
|
static int check(const char *a)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
2007-02-02 06:49:09 +05:30
|
|
|
int r;
|
2009-07-15 03:54:08 +05:30
|
|
|
unsigned pid_le32;
|
2007-08-20 22:57:40 +05:30
|
|
|
uint64_t timestamp;
|
2006-11-18 00:28:49 +05:30
|
|
|
|
2007-02-02 06:49:09 +05:30
|
|
|
r = svstatus_get();
|
|
|
|
if (r == -1)
|
|
|
|
return -1;
|
2017-05-16 01:09:51 +05:30
|
|
|
while (*a) {
|
|
|
|
if (r == 0) {
|
|
|
|
if (*a == 'x')
|
|
|
|
return 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pid_le32 = svstatus.pid_le32;
|
|
|
|
switch (*a) {
|
|
|
|
case 'x':
|
2006-11-18 00:28:49 +05:30
|
|
|
return 0;
|
2017-05-16 01:09:51 +05:30
|
|
|
case 'u':
|
|
|
|
if (!pid_le32 || svstatus.run_or_finish != 1)
|
|
|
|
return 0;
|
|
|
|
if (!checkscript())
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (pid_le32 || svstatus.run_or_finish != 0)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
if (pid_le32 && !checkscript())
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
case 'k':
|
|
|
|
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':
|
|
|
|
timestamp = SWAP_BE64(svstatus.time_be64);
|
|
|
|
if ((!pid_le32 && tstart > timestamp) || (pid_le32 && svstatus.want != 'd'))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
if (pid_le32 && !svstatus.paused)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (pid_le32 && svstatus.paused)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++a;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2007-01-30 05:13:52 +05:30
|
|
|
printf(OK);
|
|
|
|
svstatus_print(*service);
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n'); /* will also flush the output */
|
2006-11-18 00:28:49 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-01-30 05:13:52 +05:30
|
|
|
static int control(const char *a)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
2009-07-15 03:54:08 +05:30
|
|
|
int fd, r, l;
|
2007-02-02 06:49:09 +05:30
|
|
|
|
2007-08-20 22:57:40 +05:30
|
|
|
if (svstatus_get() <= 0)
|
|
|
|
return -1;
|
2017-05-16 01:09:51 +05:30
|
|
|
if (svstatus.want == *a && (*a != 'd' || svstatus.got_term == 1))
|
2007-08-20 22:57:40 +05:30
|
|
|
return 0;
|
2010-10-13 16:23:27 +05:30
|
|
|
fd = open("supervise/control", O_WRONLY|O_NDELAY);
|
2007-01-30 05:13:52 +05:30
|
|
|
if (fd == -1) {
|
2006-11-18 00:28:49 +05:30
|
|
|
if (errno != ENODEV)
|
2009-11-13 13:38:27 +05:30
|
|
|
warn("can't open supervise/control");
|
2006-11-18 00:28:49 +05:30
|
|
|
else
|
|
|
|
*a == 'x' ? ok("runsv not running") : failx("runsv not running");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-07-15 03:54:08 +05:30
|
|
|
l = strlen(a);
|
|
|
|
r = write(fd, a, l);
|
2006-11-18 00:28:49 +05:30
|
|
|
close(fd);
|
2009-07-15 03:54:08 +05:30
|
|
|
if (r != l) {
|
2009-11-13 13:38:27 +05:30
|
|
|
warn("can't write to supervise/control");
|
2006-11-18 00:28:49 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:26:40 +05:30
|
|
|
//usage:#define sv_trivial_usage
|
|
|
|
//usage: "[-v] [-w SEC] CMD SERVICE_DIR..."
|
|
|
|
//usage:#define sv_full_usage "\n\n"
|
|
|
|
//usage: "Control services monitored by runsv supervisor.\n"
|
|
|
|
//usage: "Commands (only first character is enough):\n"
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "status: query service status\n"
|
|
|
|
//usage: "up: if service isn't running, start it. If service stops, restart it\n"
|
|
|
|
//usage: "once: like 'up', but if service stops, don't restart it\n"
|
|
|
|
//usage: "down: send TERM and CONT signals. If ./run exits, start ./finish\n"
|
|
|
|
//usage: " if it exists. After it stops, don't restart service\n"
|
|
|
|
//usage: "exit: send TERM and CONT signals to service and log service. If they exit,\n"
|
|
|
|
//usage: " runsv exits too\n"
|
|
|
|
//usage: "pause, cont, hup, alarm, interrupt, quit, 1, 2, term, kill: send\n"
|
|
|
|
//usage: "STOP, CONT, HUP, ALRM, INT, QUIT, USR1, USR2, TERM, KILL signal to service"
|
|
|
|
static int sv(char **argv)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
|
|
|
char *x;
|
2007-02-02 06:49:09 +05:30
|
|
|
char *action;
|
2008-11-07 04:37:42 +05:30
|
|
|
const char *varservice = CONFIG_SV_DEFAULT_SERVICE_DIR;
|
2007-08-20 22:57:40 +05:30
|
|
|
unsigned waitsec = 7;
|
2007-02-02 06:49:09 +05:30
|
|
|
smallint kll = 0;
|
2008-03-17 14:39:09 +05:30
|
|
|
int verbose = 0;
|
2007-02-02 06:49:09 +05:30
|
|
|
int (*act)(const char*);
|
|
|
|
int (*cbk)(const char*);
|
|
|
|
int curdir;
|
2006-11-18 00:28:49 +05:30
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2007-02-02 06:49:09 +05:30
|
|
|
xfunc_error_retval = 100;
|
|
|
|
|
|
|
|
x = getenv("SVDIR");
|
|
|
|
if (x) varservice = x;
|
|
|
|
x = getenv("SVWAIT");
|
2007-08-20 22:57:40 +05:30
|
|
|
if (x) waitsec = xatou(x);
|
2007-02-02 06:49:09 +05:30
|
|
|
|
2016-07-07 01:28:02 +05:30
|
|
|
opt_complementary = "vv"; /* -w N, -v is a counter */
|
|
|
|
getopt32(argv, "w:+v", &waitsec, &verbose);
|
2007-02-02 06:49:09 +05:30
|
|
|
argv += optind;
|
|
|
|
action = *argv++;
|
2007-08-20 22:57:40 +05:30
|
|
|
if (!action || !*argv) bb_show_usage();
|
2006-11-18 00:28:49 +05:30
|
|
|
|
2009-02-02 16:18:06 +05:30
|
|
|
tnow = time(NULL) + 0x400000000000000aULL;
|
2007-01-30 05:13:52 +05:30
|
|
|
tstart = tnow;
|
2010-10-13 16:23:27 +05:30
|
|
|
curdir = open(".", O_RDONLY|O_NDELAY);
|
2007-01-30 05:13:52 +05:30
|
|
|
if (curdir == -1)
|
2006-11-18 00:28:49 +05:30
|
|
|
fatal_cannot("open current directory");
|
|
|
|
|
2007-01-30 05:13:52 +05:30
|
|
|
act = &control;
|
|
|
|
acts = "s";
|
2007-02-02 06:49:09 +05:30
|
|
|
cbk = ✓
|
|
|
|
|
2006-11-18 00:28:49 +05:30
|
|
|
switch (*action) {
|
2007-02-02 06:49:09 +05:30
|
|
|
case 'x':
|
|
|
|
case 'e':
|
|
|
|
acts = "x";
|
|
|
|
if (!verbose) cbk = NULL;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
case 'E':
|
|
|
|
acts = "x";
|
|
|
|
kll = 1;
|
|
|
|
break;
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'D':
|
2007-02-02 06:49:09 +05:30
|
|
|
acts = "d";
|
|
|
|
kll = 1;
|
|
|
|
break;
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'T':
|
2007-02-02 06:49:09 +05:30
|
|
|
acts = "tc";
|
|
|
|
kll = 1;
|
|
|
|
break;
|
2017-05-16 01:09:51 +05:30
|
|
|
case 't':
|
|
|
|
if (str_equal(action, "try-restart")) {
|
|
|
|
acts = "tc";
|
|
|
|
break;
|
|
|
|
}
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'c':
|
2007-08-20 22:57:40 +05:30
|
|
|
if (str_equal(action, "check")) {
|
2007-02-02 06:49:09 +05:30
|
|
|
act = NULL;
|
2017-05-16 01:09:51 +05:30
|
|
|
acts = "C";
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
}
|
2017-05-16 01:09:51 +05:30
|
|
|
case 'u': case 'd': case 'o': case 'p': case 'h':
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'a': case 'i': case 'k': case 'q': case '1': case '2':
|
2007-02-02 06:49:09 +05:30
|
|
|
action[1] = '\0';
|
|
|
|
acts = action;
|
2017-05-16 01:09:51 +05:30
|
|
|
if (!verbose)
|
|
|
|
cbk = NULL;
|
2007-02-02 06:49:09 +05:30
|
|
|
break;
|
2006-11-18 00:28:49 +05:30
|
|
|
case 's':
|
2007-08-20 22:57:40 +05:30
|
|
|
if (str_equal(action, "shutdown")) {
|
2006-11-18 00:28:49 +05:30
|
|
|
acts = "x";
|
|
|
|
break;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
if (str_equal(action, "start")) {
|
2006-11-18 00:28:49 +05:30
|
|
|
acts = "u";
|
|
|
|
break;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
if (str_equal(action, "stop")) {
|
2006-11-18 00:28:49 +05:30
|
|
|
acts = "d";
|
|
|
|
break;
|
|
|
|
}
|
2007-02-02 06:49:09 +05:30
|
|
|
/* "status" */
|
2007-01-30 05:13:52 +05:30
|
|
|
act = &status;
|
|
|
|
cbk = NULL;
|
|
|
|
break;
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'r':
|
2007-08-20 22:57:40 +05:30
|
|
|
if (str_equal(action, "restart")) {
|
2006-11-18 00:28:49 +05:30
|
|
|
acts = "tcu";
|
|
|
|
break;
|
|
|
|
}
|
2017-05-16 01:09:51 +05:30
|
|
|
if (str_equal(action, "reload")) {
|
|
|
|
acts = "h";
|
|
|
|
break;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
bb_show_usage();
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'f':
|
2007-08-20 22:57:40 +05:30
|
|
|
if (str_equal(action, "force-reload")) {
|
2007-02-02 06:49:09 +05:30
|
|
|
acts = "tc";
|
|
|
|
kll = 1;
|
|
|
|
break;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
if (str_equal(action, "force-restart")) {
|
2007-02-02 06:49:09 +05:30
|
|
|
acts = "tcu";
|
|
|
|
kll = 1;
|
|
|
|
break;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
if (str_equal(action, "force-shutdown")) {
|
2007-02-02 06:49:09 +05:30
|
|
|
acts = "x";
|
|
|
|
kll = 1;
|
|
|
|
break;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
if (str_equal(action, "force-stop")) {
|
2007-02-02 06:49:09 +05:30
|
|
|
acts = "d";
|
|
|
|
kll = 1;
|
|
|
|
break;
|
|
|
|
}
|
2006-11-18 00:28:49 +05:30
|
|
|
default:
|
2007-08-20 22:57:40 +05:30
|
|
|
bb_show_usage();
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
|
2009-07-15 03:54:08 +05:30
|
|
|
service = argv;
|
|
|
|
while ((x = *service) != NULL) {
|
2017-05-16 01:09:51 +05:30
|
|
|
if (x[0] != '/' && x[0] != '.'
|
|
|
|
&& x[0] != '\0' && x[strlen(x) - 1] != '/'
|
|
|
|
) {
|
2007-02-02 06:49:09 +05:30
|
|
|
if (chdir(varservice) == -1)
|
|
|
|
goto chdir_failed_0;
|
|
|
|
}
|
2009-07-15 03:54:08 +05:30
|
|
|
if (chdir(x) == -1) {
|
2007-02-02 06:49:09 +05:30
|
|
|
chdir_failed_0:
|
2009-11-13 13:38:27 +05:30
|
|
|
fail("can't change to service directory");
|
2007-02-02 06:49:09 +05:30
|
|
|
goto nullify_service_0;
|
|
|
|
}
|
|
|
|
if (act && (act(acts) == -1)) {
|
|
|
|
nullify_service_0:
|
2009-07-15 03:54:08 +05:30
|
|
|
*service = (char*) -1L; /* "dead" */
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2007-02-02 06:49:09 +05:30
|
|
|
if (fchdir(curdir) == -1)
|
|
|
|
fatal_cannot("change to original directory");
|
2006-11-18 00:28:49 +05:30
|
|
|
service++;
|
|
|
|
}
|
|
|
|
|
2007-02-02 06:49:09 +05:30
|
|
|
if (cbk) while (1) {
|
2009-07-15 03:54:08 +05:30
|
|
|
int want_exit;
|
2007-08-20 22:57:40 +05:30
|
|
|
int diff;
|
2007-02-02 06:49:09 +05:30
|
|
|
|
2007-08-20 22:57:40 +05:30
|
|
|
diff = tnow - tstart;
|
2009-07-15 03:54:08 +05:30
|
|
|
service = argv;
|
2007-02-02 06:49:09 +05:30
|
|
|
want_exit = 1;
|
2009-07-15 03:54:08 +05:30
|
|
|
while ((x = *service) != NULL) {
|
|
|
|
if (x == (char*) -1L) /* "dead" */
|
|
|
|
goto next;
|
|
|
|
if (x[0] != '/' && x[0] != '.') {
|
2007-02-02 06:49:09 +05:30
|
|
|
if (chdir(varservice) == -1)
|
|
|
|
goto chdir_failed;
|
|
|
|
}
|
2009-07-15 03:54:08 +05:30
|
|
|
if (chdir(x) == -1) {
|
2007-01-28 03:51:52 +05:30
|
|
|
chdir_failed:
|
2009-11-13 13:38:27 +05:30
|
|
|
fail("can't change to service directory");
|
2007-02-02 06:49:09 +05:30
|
|
|
goto nullify_service;
|
|
|
|
}
|
|
|
|
if (cbk(acts) != 0)
|
|
|
|
goto nullify_service;
|
|
|
|
want_exit = 0;
|
|
|
|
if (diff >= waitsec) {
|
|
|
|
printf(kll ? "kill: " : "timeout: ");
|
|
|
|
if (svstatus_get() > 0) {
|
2009-07-15 03:54:08 +05:30
|
|
|
svstatus_print(x);
|
2007-02-02 06:49:09 +05:30
|
|
|
++rc;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n'); /* will also flush the output */
|
2007-02-02 06:49:09 +05:30
|
|
|
if (kll)
|
|
|
|
control("k");
|
2007-01-28 03:51:52 +05:30
|
|
|
nullify_service:
|
2009-07-15 03:54:08 +05:30
|
|
|
*service = (char*) -1L; /* "dead" */
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2007-02-02 06:49:09 +05:30
|
|
|
if (fchdir(curdir) == -1)
|
|
|
|
fatal_cannot("change to original directory");
|
2009-07-15 03:54:08 +05:30
|
|
|
next:
|
|
|
|
service++;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2007-02-02 06:49:09 +05:30
|
|
|
if (want_exit) break;
|
|
|
|
usleep(420000);
|
2009-02-02 16:18:06 +05:30
|
|
|
tnow = time(NULL) + 0x400000000000000aULL;
|
2007-01-28 03:51:52 +05:30
|
|
|
}
|
2006-11-18 00:28:49 +05:30
|
|
|
return rc > 99 ? 99 : rc;
|
|
|
|
}
|
2016-12-05 18:26:40 +05:30
|
|
|
|
|
|
|
#if ENABLE_SV
|
|
|
|
int sv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int sv_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
return sv(argv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//usage:#define svc_trivial_usage
|
|
|
|
//usage: "[-udopchaitkx] SERVICE_DIR..."
|
|
|
|
//usage:#define svc_full_usage "\n\n"
|
|
|
|
//usage: "Control services monitored by runsv supervisor"
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "\n"" -u If service is not running, start it; restart if it stops"
|
|
|
|
//usage: "\n"" -d If service is running, send TERM+CONT signals; do not restart it"
|
|
|
|
//usage: "\n"" -o Once: if service is not running, start it; do not restart it"
|
|
|
|
//usage: "\n"" -pchaitk Send STOP, CONT, HUP, ALRM, INT, TERM, KILL signal to service"
|
|
|
|
//usage: "\n"" -x Exit: runsv will exit as soon as the service is down"
|
|
|
|
#if ENABLE_SVC
|
|
|
|
int svc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int svc_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
char command[2];
|
|
|
|
const char *optstring;
|
|
|
|
unsigned opts;
|
|
|
|
|
|
|
|
INIT_G();
|
|
|
|
|
|
|
|
optstring = "udopchaitkx";
|
|
|
|
opts = getopt32(argv, optstring);
|
|
|
|
argv += optind;
|
|
|
|
if (!argv[0] || !opts)
|
|
|
|
bb_show_usage();
|
|
|
|
|
|
|
|
argv -= 2;
|
|
|
|
if (optind > 2) {
|
|
|
|
argv--;
|
|
|
|
argv[2] = (char*)"--";
|
|
|
|
}
|
|
|
|
argv[0] = (char*)"sv";
|
|
|
|
argv[1] = command;
|
|
|
|
command[1] = '\0';
|
|
|
|
|
|
|
|
/* getopt32() was already called:
|
|
|
|
* reset the libc getopt() function, which keeps internal state.
|
|
|
|
*/
|
2017-04-12 03:28:46 +05:30
|
|
|
GETOPT_RESET();
|
2016-12-05 18:26:40 +05:30
|
|
|
|
|
|
|
do {
|
|
|
|
if (opts & 1) {
|
|
|
|
int r;
|
|
|
|
command[0] = *optstring;
|
|
|
|
r = sv(argv);
|
|
|
|
if (r)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
optstring++;
|
|
|
|
opts >>= 1;
|
|
|
|
} while (opts);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|