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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2008-03-02 18:23:15 +05:30
|
|
|
/* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
|
2006-11-18 00:28:49 +05:30
|
|
|
/* TODO: depends on runit_lib.c - review and reduce/eliminate */
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define runsv_trivial_usage
|
|
|
|
//usage: "DIR"
|
|
|
|
//usage:#define runsv_full_usage "\n\n"
|
|
|
|
//usage: "Start and monitor a service and optionally an appendant log service"
|
|
|
|
|
2006-11-18 00:28:49 +05:30
|
|
|
#include <sys/file.h>
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-11-18 00:28:49 +05:30
|
|
|
#include "runit_lib.h"
|
|
|
|
|
2007-09-01 03:15:52 +05:30
|
|
|
#if ENABLE_MONOTONIC_SYSCALL
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
|
|
|
/* libc has incredibly messy way of doing this,
|
|
|
|
* typically requiring -lrt. We just skip all this mess */
|
|
|
|
static void gettimeofday_ns(struct timespec *ts)
|
|
|
|
{
|
|
|
|
syscall(__NR_clock_gettime, CLOCK_REALTIME, ts);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void gettimeofday_ns(struct timespec *ts)
|
|
|
|
{
|
|
|
|
if (sizeof(struct timeval) == sizeof(struct timespec)
|
|
|
|
&& sizeof(((struct timeval*)ts)->tv_usec) == sizeof(ts->tv_nsec)
|
|
|
|
) {
|
|
|
|
/* Cheat */
|
|
|
|
gettimeofday((void*)ts, NULL);
|
|
|
|
ts->tv_nsec *= 1000;
|
|
|
|
} else {
|
|
|
|
extern void BUG_need_to_implement_gettimeofday_ns(void);
|
|
|
|
BUG_need_to_implement_gettimeofday_ns();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Compare possibly overflowing unsigned counters */
|
|
|
|
#define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0)
|
|
|
|
|
2006-11-18 00:28:49 +05:30
|
|
|
/* state */
|
|
|
|
#define S_DOWN 0
|
|
|
|
#define S_RUN 1
|
|
|
|
#define S_FINISH 2
|
|
|
|
/* ctrl */
|
|
|
|
#define C_NOOP 0
|
|
|
|
#define C_TERM 1
|
|
|
|
#define C_PAUSE 2
|
|
|
|
/* want */
|
|
|
|
#define W_UP 0
|
|
|
|
#define W_DOWN 1
|
|
|
|
#define W_EXIT 2
|
|
|
|
|
|
|
|
struct svdir {
|
|
|
|
int pid;
|
2007-03-10 04:16:06 +05:30
|
|
|
smallint state;
|
|
|
|
smallint ctrl;
|
2009-07-15 03:51:49 +05:30
|
|
|
smallint sd_want;
|
2007-03-10 04:16:06 +05:30
|
|
|
smallint islog;
|
2007-08-20 22:57:40 +05:30
|
|
|
struct timespec start;
|
2006-11-18 00:28:49 +05:30
|
|
|
int fdlock;
|
|
|
|
int fdcontrol;
|
|
|
|
int fdcontrolwrite;
|
2009-08-02 05:53:27 +05:30
|
|
|
int wstat;
|
2006-11-18 00:28:49 +05:30
|
|
|
};
|
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
struct globals {
|
|
|
|
smallint haslog;
|
|
|
|
smallint sigterm;
|
|
|
|
smallint pidchanged;
|
2008-02-16 18:50:56 +05:30
|
|
|
struct fd_pair selfpipe;
|
|
|
|
struct fd_pair logpipe;
|
2007-09-28 15:59:17 +05:30
|
|
|
char *dir;
|
|
|
|
struct svdir svd[2];
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2007-09-28 15:59:17 +05:30
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
|
|
|
#define haslog (G.haslog )
|
|
|
|
#define sigterm (G.sigterm )
|
|
|
|
#define pidchanged (G.pidchanged )
|
|
|
|
#define selfpipe (G.selfpipe )
|
|
|
|
#define logpipe (G.logpipe )
|
|
|
|
#define dir (G.dir )
|
|
|
|
#define svd (G.svd )
|
2008-06-25 15:23:17 +05:30
|
|
|
#define INIT_G() do { \
|
|
|
|
pidchanged = 1; \
|
|
|
|
} while (0)
|
2006-11-18 00:28:49 +05:30
|
|
|
|
2007-01-30 04:21:58 +05:30
|
|
|
static void fatal2_cannot(const char *m1, const char *m2)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
2015-10-08 14:54:44 +05:30
|
|
|
bb_perror_msg_and_die("%s: fatal: can't %s%s", dir, m1, m2);
|
2006-11-18 00:28:49 +05:30
|
|
|
/* was exiting 111 */
|
|
|
|
}
|
2007-01-30 04:21:58 +05:30
|
|
|
static void fatal_cannot(const char *m)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
|
|
|
fatal2_cannot(m, "");
|
|
|
|
/* was exiting 111 */
|
|
|
|
}
|
2007-01-30 04:21:58 +05:30
|
|
|
static void fatal2x_cannot(const char *m1, const char *m2)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
2015-10-08 14:54:44 +05:30
|
|
|
bb_error_msg_and_die("%s: fatal: can't %s%s", dir, m1, m2);
|
2006-11-18 00:28:49 +05:30
|
|
|
/* was exiting 111 */
|
|
|
|
}
|
2007-01-30 04:21:58 +05:30
|
|
|
static void warn_cannot(const char *m)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
|
|
|
bb_perror_msg("%s: warning: cannot %s", dir, m);
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void s_child(int sig_no UNUSED_PARAM)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
2008-02-16 18:50:56 +05:30
|
|
|
write(selfpipe.wr, "", 1);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void s_term(int sig_no UNUSED_PARAM)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
|
|
|
sigterm = 1;
|
2008-02-16 18:50:56 +05:30
|
|
|
write(selfpipe.wr, "", 1); /* XXX */
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int open_trunc_or_warn(const char *name)
|
|
|
|
{
|
2010-09-05 22:58:29 +05:30
|
|
|
/* Why O_NDELAY? */
|
|
|
|
int fd = open(name, O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT, 0644);
|
2006-11-18 00:28:49 +05:30
|
|
|
if (fd < 0)
|
|
|
|
bb_perror_msg("%s: warning: cannot open %s",
|
|
|
|
dir, name);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_status(struct svdir *s)
|
|
|
|
{
|
2007-08-20 22:57:40 +05:30
|
|
|
ssize_t sz;
|
2006-11-18 00:28:49 +05:30
|
|
|
int fd;
|
2007-08-20 22:57:40 +05:30
|
|
|
svstatus_t status;
|
2006-11-18 00:28:49 +05:30
|
|
|
|
|
|
|
/* pid */
|
|
|
|
if (pidchanged) {
|
|
|
|
fd = open_trunc_or_warn("supervise/pid.new");
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
if (s->pid) {
|
2007-03-10 04:16:06 +05:30
|
|
|
char spid[sizeof(int)*3 + 2];
|
|
|
|
int size = sprintf(spid, "%u\n", (unsigned)s->pid);
|
2006-11-18 00:28:49 +05:30
|
|
|
write(fd, spid, size);
|
|
|
|
}
|
|
|
|
close(fd);
|
2007-03-10 04:16:06 +05:30
|
|
|
if (rename_or_warn("supervise/pid.new",
|
2013-01-14 20:27:44 +05:30
|
|
|
s->islog ? "log/supervise/pid" : "log/supervise/pid"+4))
|
2006-11-18 00:28:49 +05:30
|
|
|
return;
|
|
|
|
pidchanged = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stat */
|
|
|
|
fd = open_trunc_or_warn("supervise/stat.new");
|
|
|
|
if (fd < -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
{
|
|
|
|
char stat_buf[sizeof("finish, paused, got TERM, want down\n")];
|
|
|
|
char *p = stat_buf;
|
|
|
|
switch (s->state) {
|
|
|
|
case S_DOWN:
|
2011-02-13 11:56:57 +05:30
|
|
|
p = stpcpy(p, "down");
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
case S_RUN:
|
2011-02-13 11:56:57 +05:30
|
|
|
p = stpcpy(p, "run");
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
case S_FINISH:
|
2011-02-13 11:56:57 +05:30
|
|
|
p = stpcpy(p, "finish");
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
}
|
2009-08-02 05:53:27 +05:30
|
|
|
if (s->ctrl & C_PAUSE)
|
2011-02-13 11:56:57 +05:30
|
|
|
p = stpcpy(p, ", paused");
|
2009-08-02 05:53:27 +05:30
|
|
|
if (s->ctrl & C_TERM)
|
2011-02-13 11:56:57 +05:30
|
|
|
p = stpcpy(p, ", got TERM");
|
2006-11-18 00:28:49 +05:30
|
|
|
if (s->state != S_DOWN)
|
2009-07-15 03:51:49 +05:30
|
|
|
switch (s->sd_want) {
|
2006-11-18 00:28:49 +05:30
|
|
|
case W_DOWN:
|
2011-02-13 11:56:57 +05:30
|
|
|
p = stpcpy(p, ", want down");
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
case W_EXIT:
|
2011-02-13 11:56:57 +05:30
|
|
|
p = stpcpy(p, ", want exit");
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
*p++ = '\n';
|
|
|
|
write(fd, stat_buf, p - stat_buf);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2007-03-10 04:16:06 +05:30
|
|
|
rename_or_warn("supervise/stat.new",
|
|
|
|
s->islog ? "log/supervise/stat" : "log/supervise/stat"+4);
|
2006-11-18 00:28:49 +05:30
|
|
|
|
|
|
|
/* supervise compatibility */
|
2007-08-20 22:57:40 +05:30
|
|
|
memset(&status, 0, sizeof(status));
|
|
|
|
status.time_be64 = SWAP_BE64(s->start.tv_sec + 0x400000000000000aULL);
|
|
|
|
status.time_nsec_be32 = SWAP_BE32(s->start.tv_nsec);
|
|
|
|
status.pid_le32 = SWAP_LE32(s->pid);
|
2006-11-18 00:28:49 +05:30
|
|
|
if (s->ctrl & C_PAUSE)
|
2007-08-20 22:57:40 +05:30
|
|
|
status.paused = 1;
|
2009-07-15 03:51:49 +05:30
|
|
|
if (s->sd_want == W_UP)
|
2007-08-20 22:57:40 +05:30
|
|
|
status.want = 'u';
|
2006-11-18 00:28:49 +05:30
|
|
|
else
|
2007-08-20 22:57:40 +05:30
|
|
|
status.want = 'd';
|
2006-11-18 00:28:49 +05:30
|
|
|
if (s->ctrl & C_TERM)
|
2007-08-20 22:57:40 +05:30
|
|
|
status.got_term = 1;
|
|
|
|
status.run_or_finish = s->state;
|
2006-11-18 00:28:49 +05:30
|
|
|
fd = open_trunc_or_warn("supervise/status.new");
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
2007-08-20 22:57:40 +05:30
|
|
|
sz = write(fd, &status, sizeof(status));
|
|
|
|
close(fd);
|
|
|
|
if (sz != sizeof(status)) {
|
2006-11-18 00:28:49 +05:30
|
|
|
warn_cannot("write supervise/status.new");
|
|
|
|
unlink("supervise/status.new");
|
|
|
|
return;
|
|
|
|
}
|
2007-03-10 04:16:06 +05:30
|
|
|
rename_or_warn("supervise/status.new",
|
|
|
|
s->islog ? "log/supervise/status" : "log/supervise/status"+4);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned custom(struct svdir *s, char c)
|
|
|
|
{
|
2008-11-07 04:09:57 +05:30
|
|
|
pid_t pid;
|
2006-11-18 00:28:49 +05:30
|
|
|
int w;
|
|
|
|
char a[10];
|
|
|
|
struct stat st;
|
|
|
|
|
2009-06-07 19:32:00 +05:30
|
|
|
if (s->islog)
|
|
|
|
return 0;
|
2007-03-10 04:16:06 +05:30
|
|
|
strcpy(a, "control/?");
|
2008-03-17 14:49:26 +05:30
|
|
|
a[8] = c; /* replace '?' */
|
2006-11-18 00:28:49 +05:30
|
|
|
if (stat(a, &st) == 0) {
|
|
|
|
if (st.st_mode & S_IXUSR) {
|
2008-03-17 14:49:26 +05:30
|
|
|
pid = vfork();
|
2006-11-18 00:28:49 +05:30
|
|
|
if (pid == -1) {
|
2008-03-17 14:49:26 +05:30
|
|
|
warn_cannot("vfork for control/?");
|
2006-11-18 00:28:49 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2009-08-02 05:53:27 +05:30
|
|
|
if (pid == 0) {
|
2008-03-17 14:49:26 +05:30
|
|
|
/* child */
|
2008-02-16 18:50:56 +05:30
|
|
|
if (haslog && dup2(logpipe.wr, 1) == -1)
|
2006-11-18 00:28:49 +05:30
|
|
|
warn_cannot("setup stdout for control/?");
|
2009-02-26 17:59:59 +05:30
|
|
|
execl(a, a, (char *) NULL);
|
2006-11-18 00:28:49 +05:30
|
|
|
fatal_cannot("run control/?");
|
|
|
|
}
|
2008-03-17 14:49:26 +05:30
|
|
|
/* parent */
|
2009-02-26 17:59:59 +05:30
|
|
|
if (safe_waitpid(pid, &w, 0) == -1) {
|
2006-11-18 00:28:49 +05:30
|
|
|
warn_cannot("wait for child control/?");
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-07 19:32:00 +05:30
|
|
|
return WEXITSTATUS(w) == 0;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2007-03-10 04:16:06 +05:30
|
|
|
} else {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
warn_cannot("stat control/?");
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stopservice(struct svdir *s)
|
|
|
|
{
|
2007-03-10 04:16:06 +05:30
|
|
|
if (s->pid && !custom(s, 't')) {
|
2006-11-18 00:28:49 +05:30
|
|
|
kill(s->pid, SIGTERM);
|
2007-03-10 04:16:06 +05:30
|
|
|
s->ctrl |= C_TERM;
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(s);
|
|
|
|
}
|
2009-07-15 03:51:49 +05:30
|
|
|
if (s->sd_want == W_DOWN) {
|
2006-11-18 00:28:49 +05:30
|
|
|
kill(s->pid, SIGCONT);
|
2007-03-10 04:16:06 +05:30
|
|
|
custom(s, 'd');
|
|
|
|
return;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2009-07-15 03:51:49 +05:30
|
|
|
if (s->sd_want == W_EXIT) {
|
2006-11-18 00:28:49 +05:30
|
|
|
kill(s->pid, SIGCONT);
|
|
|
|
custom(s, 'x');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void startservice(struct svdir *s)
|
|
|
|
{
|
|
|
|
int p;
|
2009-08-02 05:53:27 +05:30
|
|
|
const char *arg[4];
|
|
|
|
char exitcode[sizeof(int)*3 + 2];
|
|
|
|
|
|
|
|
if (s->state == S_FINISH) {
|
|
|
|
/* Two arguments are given to ./finish. The first one is ./run exit code,
|
|
|
|
* or -1 if ./run didnt exit normally. The second one is
|
|
|
|
* the least significant byte of the exit status as determined by waitpid;
|
|
|
|
* for instance it is 0 if ./run exited normally, and the signal number
|
|
|
|
* if ./run was terminated by a signal. If runsv cannot start ./run
|
|
|
|
* for some reason, the exit code is 111 and the status is 0.
|
|
|
|
*/
|
|
|
|
arg[0] = "./finish";
|
|
|
|
arg[1] = "-1";
|
|
|
|
if (WIFEXITED(s->wstat)) {
|
2009-10-07 01:39:50 +05:30
|
|
|
*utoa_to_buf(WEXITSTATUS(s->wstat), exitcode, sizeof(exitcode)) = '\0';
|
2009-08-02 05:53:27 +05:30
|
|
|
arg[1] = exitcode;
|
|
|
|
}
|
|
|
|
//arg[2] = "0";
|
|
|
|
//if (WIFSIGNALED(s->wstat)) {
|
2009-10-07 01:39:50 +05:30
|
|
|
arg[2] = utoa(WTERMSIG(s->wstat));
|
2009-08-02 05:53:27 +05:30
|
|
|
//}
|
|
|
|
arg[3] = NULL;
|
|
|
|
} else {
|
|
|
|
arg[0] = "./run";
|
|
|
|
arg[1] = NULL;
|
2006-11-18 00:28:49 +05:30
|
|
|
custom(s, 'u');
|
|
|
|
}
|
|
|
|
|
2007-04-10 21:12:06 +05:30
|
|
|
if (s->pid != 0)
|
|
|
|
stopservice(s); /* should never happen */
|
2008-03-17 14:49:26 +05:30
|
|
|
while ((p = vfork()) == -1) {
|
|
|
|
warn_cannot("vfork, sleeping");
|
2006-11-18 00:28:49 +05:30
|
|
|
sleep(5);
|
|
|
|
}
|
|
|
|
if (p == 0) {
|
|
|
|
/* child */
|
|
|
|
if (haslog) {
|
2008-02-16 18:50:56 +05:30
|
|
|
/* NB: bug alert! right order is close, then dup2 */
|
2006-11-18 00:28:49 +05:30
|
|
|
if (s->islog) {
|
2007-08-18 19:46:39 +05:30
|
|
|
xchdir("./log");
|
2008-02-16 18:50:56 +05:30
|
|
|
close(logpipe.wr);
|
|
|
|
xdup2(logpipe.rd, 0);
|
2006-11-18 00:28:49 +05:30
|
|
|
} else {
|
2008-02-16 18:50:56 +05:30
|
|
|
close(logpipe.rd);
|
|
|
|
xdup2(logpipe.wr, 1);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
}
|
2008-11-09 05:45:11 +05:30
|
|
|
/* Non-ignored signals revert to SIG_DFL on exec anyway */
|
|
|
|
/*bb_signals(0
|
2008-02-17 04:28:56 +05:30
|
|
|
+ (1 << SIGCHLD)
|
|
|
|
+ (1 << SIGTERM)
|
2008-11-09 05:45:11 +05:30
|
|
|
, SIG_DFL);*/
|
2007-01-28 03:51:52 +05:30
|
|
|
sig_unblock(SIGCHLD);
|
|
|
|
sig_unblock(SIGTERM);
|
2009-08-02 05:53:27 +05:30
|
|
|
execv(arg[0], (char**) arg);
|
|
|
|
fatal2_cannot(s->islog ? "start log/" : "start ", arg[0]);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
2008-03-17 14:49:26 +05:30
|
|
|
/* parent */
|
2006-11-18 00:28:49 +05:30
|
|
|
if (s->state != S_FINISH) {
|
2007-08-20 22:57:40 +05:30
|
|
|
gettimeofday_ns(&s->start);
|
2006-11-18 00:28:49 +05:30
|
|
|
s->state = S_RUN;
|
|
|
|
}
|
|
|
|
s->pid = p;
|
|
|
|
pidchanged = 1;
|
|
|
|
s->ctrl = C_NOOP;
|
|
|
|
update_status(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ctrl(struct svdir *s, char c)
|
|
|
|
{
|
2007-03-10 04:16:06 +05:30
|
|
|
int sig;
|
|
|
|
|
2006-12-26 16:12:51 +05:30
|
|
|
switch (c) {
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'd': /* down */
|
2009-07-15 03:51:49 +05:30
|
|
|
s->sd_want = W_DOWN;
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(s);
|
2007-08-20 22:57:40 +05:30
|
|
|
if (s->pid && s->state != S_FINISH)
|
|
|
|
stopservice(s);
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
case 'u': /* up */
|
2009-07-15 03:51:49 +05:30
|
|
|
s->sd_want = W_UP;
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(s);
|
2007-08-20 22:57:40 +05:30
|
|
|
if (s->pid == 0)
|
|
|
|
startservice(s);
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
case 'x': /* exit */
|
2007-08-20 22:57:40 +05:30
|
|
|
if (s->islog)
|
|
|
|
break;
|
2009-07-15 03:51:49 +05:30
|
|
|
s->sd_want = W_EXIT;
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(s);
|
2007-03-10 04:16:06 +05:30
|
|
|
/* FALLTHROUGH */
|
2006-11-18 00:28:49 +05:30
|
|
|
case 't': /* sig term */
|
2007-08-20 22:57:40 +05:30
|
|
|
if (s->pid && s->state != S_FINISH)
|
|
|
|
stopservice(s);
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
case 'k': /* sig kill */
|
2007-08-20 22:57:40 +05:30
|
|
|
if (s->pid && !custom(s, c))
|
|
|
|
kill(s->pid, SIGKILL);
|
2006-11-18 00:28:49 +05:30
|
|
|
s->state = S_DOWN;
|
|
|
|
break;
|
|
|
|
case 'p': /* sig pause */
|
2007-08-20 22:57:40 +05:30
|
|
|
if (s->pid && !custom(s, c))
|
|
|
|
kill(s->pid, SIGSTOP);
|
2007-03-10 04:16:06 +05:30
|
|
|
s->ctrl |= C_PAUSE;
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(s);
|
|
|
|
break;
|
|
|
|
case 'c': /* sig cont */
|
2007-08-20 22:57:40 +05:30
|
|
|
if (s->pid && !custom(s, c))
|
|
|
|
kill(s->pid, SIGCONT);
|
2009-02-26 18:08:01 +05:30
|
|
|
s->ctrl &= ~C_PAUSE;
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(s);
|
|
|
|
break;
|
|
|
|
case 'o': /* once */
|
2009-07-15 03:51:49 +05:30
|
|
|
s->sd_want = W_DOWN;
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(s);
|
2007-08-20 22:57:40 +05:30
|
|
|
if (!s->pid)
|
|
|
|
startservice(s);
|
2006-11-18 00:28:49 +05:30
|
|
|
break;
|
|
|
|
case 'a': /* sig alarm */
|
2007-03-10 04:16:06 +05:30
|
|
|
sig = SIGALRM;
|
|
|
|
goto sendsig;
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'h': /* sig hup */
|
2007-03-10 04:16:06 +05:30
|
|
|
sig = SIGHUP;
|
|
|
|
goto sendsig;
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'i': /* sig int */
|
2007-03-10 04:16:06 +05:30
|
|
|
sig = SIGINT;
|
|
|
|
goto sendsig;
|
2006-11-18 00:28:49 +05:30
|
|
|
case 'q': /* sig quit */
|
2007-03-10 04:16:06 +05:30
|
|
|
sig = SIGQUIT;
|
|
|
|
goto sendsig;
|
2006-11-18 00:28:49 +05:30
|
|
|
case '1': /* sig usr1 */
|
2007-03-10 04:16:06 +05:30
|
|
|
sig = SIGUSR1;
|
|
|
|
goto sendsig;
|
2006-11-18 00:28:49 +05:30
|
|
|
case '2': /* sig usr2 */
|
2007-03-10 04:16:06 +05:30
|
|
|
sig = SIGUSR2;
|
|
|
|
goto sendsig;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
return 1;
|
2007-03-10 04:16:06 +05:30
|
|
|
sendsig:
|
|
|
|
if (s->pid && !custom(s, c))
|
|
|
|
kill(s->pid, sig);
|
|
|
|
return 1;
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int runsv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int runsv_main(int argc UNUSED_PARAM, char **argv)
|
2006-11-18 00:28:49 +05:30
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
int fd;
|
|
|
|
int r;
|
|
|
|
char buf[256];
|
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2009-11-28 19:48:53 +05:30
|
|
|
dir = single_argv(argv);
|
2006-11-18 00:28:49 +05:30
|
|
|
|
2008-02-16 18:50:56 +05:30
|
|
|
xpiped_pair(selfpipe);
|
|
|
|
close_on_exec_on(selfpipe.rd);
|
|
|
|
close_on_exec_on(selfpipe.wr);
|
|
|
|
ndelay_on(selfpipe.rd);
|
|
|
|
ndelay_on(selfpipe.wr);
|
2007-01-11 22:50:00 +05:30
|
|
|
|
2007-01-28 03:51:52 +05:30
|
|
|
sig_block(SIGCHLD);
|
2009-01-31 06:32:07 +05:30
|
|
|
bb_signals_recursive_norestart(1 << SIGCHLD, s_child);
|
2007-01-28 03:51:52 +05:30
|
|
|
sig_block(SIGTERM);
|
2009-01-31 06:32:07 +05:30
|
|
|
bb_signals_recursive_norestart(1 << SIGTERM, s_term);
|
2006-11-18 00:28:49 +05:30
|
|
|
|
|
|
|
xchdir(dir);
|
2007-03-10 04:16:06 +05:30
|
|
|
/* bss: svd[0].pid = 0; */
|
|
|
|
if (S_DOWN) svd[0].state = S_DOWN; /* otherwise already 0 (bss) */
|
|
|
|
if (C_NOOP) svd[0].ctrl = C_NOOP;
|
2009-07-15 03:51:49 +05:30
|
|
|
if (W_UP) svd[0].sd_want = W_UP;
|
2007-03-10 04:16:06 +05:30
|
|
|
/* bss: svd[0].islog = 0; */
|
|
|
|
/* bss: svd[1].pid = 0; */
|
2007-08-20 22:57:40 +05:30
|
|
|
gettimeofday_ns(&svd[0].start);
|
2009-07-15 03:51:49 +05:30
|
|
|
if (stat("down", &s) != -1)
|
|
|
|
svd[0].sd_want = W_DOWN;
|
2006-11-18 00:28:49 +05:30
|
|
|
|
|
|
|
if (stat("log", &s) == -1) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
warn_cannot("stat ./log");
|
|
|
|
} else {
|
2007-08-20 22:57:40 +05:30
|
|
|
if (!S_ISDIR(s.st_mode)) {
|
|
|
|
errno = 0;
|
|
|
|
warn_cannot("stat log/down: log is not a directory");
|
|
|
|
} else {
|
2006-11-18 00:28:49 +05:30
|
|
|
haslog = 1;
|
|
|
|
svd[1].state = S_DOWN;
|
|
|
|
svd[1].ctrl = C_NOOP;
|
2009-07-15 03:51:49 +05:30
|
|
|
svd[1].sd_want = W_UP;
|
2006-11-18 00:28:49 +05:30
|
|
|
svd[1].islog = 1;
|
2007-08-20 22:57:40 +05:30
|
|
|
gettimeofday_ns(&svd[1].start);
|
2006-11-18 00:28:49 +05:30
|
|
|
if (stat("log/down", &s) != -1)
|
2009-07-15 03:51:49 +05:30
|
|
|
svd[1].sd_want = W_DOWN;
|
2008-02-16 18:50:56 +05:30
|
|
|
xpiped_pair(logpipe);
|
|
|
|
close_on_exec_on(logpipe.rd);
|
|
|
|
close_on_exec_on(logpipe.wr);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkdir("supervise", 0700) == -1) {
|
2007-03-10 04:16:06 +05:30
|
|
|
r = readlink("supervise", buf, sizeof(buf));
|
2006-11-18 00:28:49 +05:30
|
|
|
if (r != -1) {
|
2007-03-10 04:16:06 +05:30
|
|
|
if (r == sizeof(buf))
|
|
|
|
fatal2x_cannot("readlink ./supervise", ": name too long");
|
2006-11-18 00:28:49 +05:30
|
|
|
buf[r] = 0;
|
|
|
|
mkdir(buf, 0700);
|
|
|
|
} else {
|
|
|
|
if ((errno != ENOENT) && (errno != EINVAL))
|
|
|
|
fatal_cannot("readlink ./supervise");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
svd[0].fdlock = xopen3("log/supervise/lock"+4,
|
|
|
|
O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
|
2010-10-13 16:23:27 +05:30
|
|
|
if (flock(svd[0].fdlock, LOCK_EX | LOCK_NB) == -1)
|
2006-11-18 00:28:49 +05:30
|
|
|
fatal_cannot("lock supervise/lock");
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(svd[0].fdlock);
|
2006-11-18 00:28:49 +05:30
|
|
|
if (haslog) {
|
|
|
|
if (mkdir("log/supervise", 0700) == -1) {
|
|
|
|
r = readlink("log/supervise", buf, 256);
|
|
|
|
if (r != -1) {
|
|
|
|
if (r == 256)
|
2007-03-10 04:16:06 +05:30
|
|
|
fatal2x_cannot("readlink ./log/supervise", ": name too long");
|
2006-11-18 00:28:49 +05:30
|
|
|
buf[r] = 0;
|
|
|
|
fd = xopen(".", O_RDONLY|O_NDELAY);
|
|
|
|
xchdir("./log");
|
|
|
|
mkdir(buf, 0700);
|
|
|
|
if (fchdir(fd) == -1)
|
|
|
|
fatal_cannot("change back to service directory");
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((errno != ENOENT) && (errno != EINVAL))
|
|
|
|
fatal_cannot("readlink ./log/supervise");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
svd[1].fdlock = xopen3("log/supervise/lock",
|
|
|
|
O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
|
2010-10-13 16:23:27 +05:30
|
|
|
if (flock(svd[1].fdlock, LOCK_EX) == -1)
|
2006-11-18 00:28:49 +05:30
|
|
|
fatal_cannot("lock log/supervise/lock");
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(svd[1].fdlock);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
|
2007-01-28 03:52:17 +05:30
|
|
|
mkfifo("log/supervise/control"+4, 0600);
|
2006-11-18 00:28:49 +05:30
|
|
|
svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY);
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(svd[0].fdcontrol);
|
2006-11-18 00:28:49 +05:30
|
|
|
svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY);
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(svd[0].fdcontrolwrite);
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(&svd[0]);
|
|
|
|
if (haslog) {
|
2007-01-28 03:52:17 +05:30
|
|
|
mkfifo("log/supervise/control", 0600);
|
2006-11-18 00:28:49 +05:30
|
|
|
svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY);
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(svd[1].fdcontrol);
|
2006-11-18 00:28:49 +05:30
|
|
|
svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY);
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(svd[1].fdcontrolwrite);
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(&svd[1]);
|
|
|
|
}
|
2007-01-28 03:52:17 +05:30
|
|
|
mkfifo("log/supervise/ok"+4, 0600);
|
2006-11-18 00:28:49 +05:30
|
|
|
fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY);
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(fd);
|
2006-11-18 00:28:49 +05:30
|
|
|
if (haslog) {
|
2007-01-28 03:52:17 +05:30
|
|
|
mkfifo("log/supervise/ok", 0600);
|
2006-11-18 00:28:49 +05:30
|
|
|
fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY);
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(fd);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
for (;;) {
|
2007-08-20 22:57:40 +05:30
|
|
|
struct pollfd x[3];
|
|
|
|
unsigned deadline;
|
2006-11-18 00:28:49 +05:30
|
|
|
char ch;
|
|
|
|
|
|
|
|
if (haslog)
|
2009-07-15 03:51:49 +05:30
|
|
|
if (!svd[1].pid && svd[1].sd_want == W_UP)
|
2006-11-18 00:28:49 +05:30
|
|
|
startservice(&svd[1]);
|
|
|
|
if (!svd[0].pid)
|
2009-07-15 03:51:49 +05:30
|
|
|
if (svd[0].sd_want == W_UP || svd[0].state == S_FINISH)
|
2006-11-18 00:28:49 +05:30
|
|
|
startservice(&svd[0]);
|
|
|
|
|
2008-02-16 18:50:56 +05:30
|
|
|
x[0].fd = selfpipe.rd;
|
2007-08-20 22:57:40 +05:30
|
|
|
x[0].events = POLLIN;
|
2006-11-18 00:28:49 +05:30
|
|
|
x[1].fd = svd[0].fdcontrol;
|
2007-08-20 22:57:40 +05:30
|
|
|
x[1].events = POLLIN;
|
|
|
|
/* x[2] is used only if haslog == 1 */
|
|
|
|
x[2].fd = svd[1].fdcontrol;
|
|
|
|
x[2].events = POLLIN;
|
2007-01-28 03:51:52 +05:30
|
|
|
sig_unblock(SIGTERM);
|
|
|
|
sig_unblock(SIGCHLD);
|
2007-08-20 22:57:40 +05:30
|
|
|
poll(x, 2 + haslog, 3600*1000);
|
2007-01-28 03:51:52 +05:30
|
|
|
sig_block(SIGTERM);
|
|
|
|
sig_block(SIGCHLD);
|
2006-11-18 00:28:49 +05:30
|
|
|
|
2008-02-16 18:50:56 +05:30
|
|
|
while (read(selfpipe.rd, &ch, 1) == 1)
|
2007-08-20 22:57:40 +05:30
|
|
|
continue;
|
|
|
|
|
2006-11-18 00:28:49 +05:30
|
|
|
for (;;) {
|
2008-11-07 04:09:57 +05:30
|
|
|
pid_t child;
|
2006-11-18 00:28:49 +05:30
|
|
|
int wstat;
|
2007-01-11 22:50:00 +05:30
|
|
|
|
2008-01-03 01:25:04 +05:30
|
|
|
child = wait_any_nohang(&wstat);
|
2007-08-20 22:57:40 +05:30
|
|
|
if (!child)
|
|
|
|
break;
|
|
|
|
if ((child == -1) && (errno != EINTR))
|
|
|
|
break;
|
2006-11-18 00:28:49 +05:30
|
|
|
if (child == svd[0].pid) {
|
2009-08-02 05:53:27 +05:30
|
|
|
svd[0].wstat = wstat;
|
2006-11-18 00:28:49 +05:30
|
|
|
svd[0].pid = 0;
|
|
|
|
pidchanged = 1;
|
2009-08-02 05:53:27 +05:30
|
|
|
svd[0].ctrl &= ~C_TERM;
|
2007-02-18 16:37:43 +05:30
|
|
|
if (svd[0].state != S_FINISH) {
|
2010-10-13 16:23:27 +05:30
|
|
|
fd = open("finish", O_RDONLY|O_NDELAY);
|
2006-11-18 00:28:49 +05:30
|
|
|
if (fd != -1) {
|
|
|
|
close(fd);
|
|
|
|
svd[0].state = S_FINISH;
|
|
|
|
update_status(&svd[0]);
|
|
|
|
continue;
|
|
|
|
}
|
2007-02-18 16:37:43 +05:30
|
|
|
}
|
2006-11-18 00:28:49 +05:30
|
|
|
svd[0].state = S_DOWN;
|
2007-08-20 22:57:40 +05:30
|
|
|
deadline = svd[0].start.tv_sec + 1;
|
|
|
|
gettimeofday_ns(&svd[0].start);
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(&svd[0]);
|
2007-08-20 22:57:40 +05:30
|
|
|
if (LESS(svd[0].start.tv_sec, deadline))
|
|
|
|
sleep(1);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
if (haslog) {
|
|
|
|
if (child == svd[1].pid) {
|
2009-08-02 05:53:27 +05:30
|
|
|
svd[0].wstat = wstat;
|
2006-11-18 00:28:49 +05:30
|
|
|
svd[1].pid = 0;
|
|
|
|
pidchanged = 1;
|
|
|
|
svd[1].state = S_DOWN;
|
2007-03-10 04:16:06 +05:30
|
|
|
svd[1].ctrl &= ~C_TERM;
|
2007-08-20 22:57:40 +05:30
|
|
|
deadline = svd[1].start.tv_sec + 1;
|
|
|
|
gettimeofday_ns(&svd[1].start);
|
2006-11-18 00:28:49 +05:30
|
|
|
update_status(&svd[1]);
|
2007-08-20 22:57:40 +05:30
|
|
|
if (LESS(svd[1].start.tv_sec, deadline))
|
|
|
|
sleep(1);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
}
|
2008-02-16 18:50:56 +05:30
|
|
|
} /* for (;;) */
|
2006-11-18 00:28:49 +05:30
|
|
|
if (read(svd[0].fdcontrol, &ch, 1) == 1)
|
|
|
|
ctrl(&svd[0], ch);
|
|
|
|
if (haslog)
|
|
|
|
if (read(svd[1].fdcontrol, &ch, 1) == 1)
|
|
|
|
ctrl(&svd[1], ch);
|
|
|
|
|
|
|
|
if (sigterm) {
|
|
|
|
ctrl(&svd[0], 'x');
|
|
|
|
sigterm = 0;
|
|
|
|
}
|
|
|
|
|
2009-07-15 03:51:49 +05:30
|
|
|
if (svd[0].sd_want == W_EXIT && svd[0].state == S_DOWN) {
|
2006-11-18 00:28:49 +05:30
|
|
|
if (svd[1].pid == 0)
|
2008-05-19 14:59:47 +05:30
|
|
|
_exit(EXIT_SUCCESS);
|
2009-07-15 03:51:49 +05:30
|
|
|
if (svd[1].sd_want != W_EXIT) {
|
|
|
|
svd[1].sd_want = W_EXIT;
|
2006-11-18 00:28:49 +05:30
|
|
|
/* stopservice(&svd[1]); */
|
|
|
|
update_status(&svd[1]);
|
2008-02-16 18:50:56 +05:30
|
|
|
close(logpipe.wr);
|
|
|
|
close(logpipe.rd);
|
2006-11-18 00:28:49 +05:30
|
|
|
}
|
|
|
|
}
|
2008-02-16 18:50:56 +05:30
|
|
|
} /* for (;;) */
|
2006-11-18 00:28:49 +05:30
|
|
|
/* not reached */
|
|
|
|
return 0;
|
|
|
|
}
|