Move status to runscript.sh so init scripts can override it, #159

Return 32 if crashed, #163
This commit is contained in:
Roy Marples 2009-04-18 00:09:03 +00:00
parent d4c71415a9
commit c9e24a0494
4 changed files with 43 additions and 15 deletions

View File

@ -45,6 +45,31 @@ describe()
done done
} }
# Report status
status()
{
if service_stopping; then
ewarn "status: stopping"
return 4
elif service_starting; then
ewarn "status: starting"
return 8
elif service_inactive; then
ewarn "status: inactive"
return 16
elif service_started; then
if service_crashed; then
eerror "status: crashed"
return 32
fi
einfo "status: started"
return 0
else
einfo "status: stopped"
return 1
fi
}
# Template start / stop functions # Template start / stop functions
start() start()
{ {
@ -134,7 +159,7 @@ unset _f
while [ -n "$1" ]; do while [ -n "$1" ]; do
# See if we have the required function and run it # See if we have the required function and run it
for _cmd in describe start stop ${extra_commands:-${opts}} \ for _cmd in describe start stop status ${extra_commands:-${opts}} \
${extra_started_commands}; do ${extra_started_commands}; do
if [ "${_cmd}" = "$1" ]; then if [ "${_cmd}" = "$1" ]; then
if [ "$(command -v "$1")" = "$1" ]; then if [ "$(command -v "$1")" = "$1" ]; then

View File

@ -18,7 +18,7 @@ RC_BINLINKS= einfon einfo ewarnn ewarn eerrorn eerror ebegin eend ewend \
service_starting service_started \ service_starting service_started \
service_stopping service_stopped \ service_stopping service_stopped \
service_inactive service_wasinactive \ service_inactive service_wasinactive \
service_hotplugged service_started_daemon \ service_hotplugged service_started_daemon service_crashed \
checkpath fstabinfo mountinfo rc-depend \ checkpath fstabinfo mountinfo rc-depend \
service_get_value service_set_value get_options save_options \ service_get_value service_set_value get_options save_options \
shell_var is_newer_than is_older_than shell_var is_newer_than is_older_than

View File

@ -295,6 +295,10 @@ static int do_service(int argc, char **argv)
} }
ok = rc_service_started_daemon(service, exec, NULL, idx); ok = rc_service_started_daemon(service, exec, NULL, idx);
} else if (strcmp(applet, "service_crashed") == 0) {
ok = (_rc_can_find_pids() &&
rc_service_daemons_crashed(service) &&
errno != EACCES);
} else } else
eerrorx("%s: unknown applet", applet); eerrorx("%s: unknown applet", applet);

View File

@ -352,11 +352,10 @@ write_prefix(const char *buffer, size_t bytes, bool *prefixed)
return ret; return ret;
} }
static bool static int
svc_exec(const char *arg1, const char *arg2) svc_exec(const char *arg1, const char *arg2)
{ {
bool execok; int ret, fdout = fileno(stdout);
int fdout = fileno(stdout);
struct termios tt; struct termios tt;
struct winsize ws; struct winsize ws;
int i; int i;
@ -467,13 +466,13 @@ svc_exec(const char *arg1, const char *arg2)
master_tty = -1; master_tty = -1;
} }
execok = rc_waitpid(service_pid) == 0 ? true : false; ret = WEXITSTATUS(rc_waitpid(service_pid));
if (!execok && errno == ECHILD) if (ret != 0 && errno == ECHILD)
/* killall5 -9 could cause this */ /* killall5 -9 could cause this */
execok = true; ret = 0;
service_pid = 0; service_pid = 0;
return execok; return ret;
} }
static bool static bool
@ -788,7 +787,7 @@ svc_start(bool deps)
setenv("IN_BACKGROUND", ibsave, 1); setenv("IN_BACKGROUND", ibsave, 1);
hook_out = RC_HOOK_SERVICE_START_DONE; hook_out = RC_HOOK_SERVICE_START_DONE;
rc_plugin_run(RC_HOOK_SERVICE_START_NOW, applet); rc_plugin_run(RC_HOOK_SERVICE_START_NOW, applet);
started = svc_exec("start", NULL); started = (svc_exec("start", NULL) == 0);
if (ibsave) if (ibsave)
unsetenv("IN_BACKGROUND"); unsetenv("IN_BACKGROUND");
@ -961,7 +960,7 @@ svc_stop(bool deps)
setenv("IN_BACKGROUND", ibsave, 1); setenv("IN_BACKGROUND", ibsave, 1);
hook_out = RC_HOOK_SERVICE_STOP_DONE; hook_out = RC_HOOK_SERVICE_STOP_DONE;
rc_plugin_run(RC_HOOK_SERVICE_STOP_NOW, applet); rc_plugin_run(RC_HOOK_SERVICE_STOP_NOW, applet);
stopped = svc_exec("stop", NULL); stopped = (svc_exec("stop", NULL) == 0);
if (ibsave) if (ibsave)
unsetenv("IN_BACKGROUND"); unsetenv("IN_BACKGROUND");
@ -1286,10 +1285,10 @@ runscript(int argc, char **argv)
rc_stringlist_free(services); rc_stringlist_free(services);
services = NULL; services = NULL;
} else if (strcmp (optarg, "status") == 0) { } else if (strcmp (optarg, "status") == 0) {
RC_SERVICE r = svc_status(); save = prefix;
retval = (int) r; eprefix(NULL);
if (retval & RC_SERVICE_STARTED) prefix = NULL;
retval = 0; retval = svc_exec("status", NULL);
} else { } else {
if (strcmp(optarg, "conditionalrestart") == 0 || if (strcmp(optarg, "conditionalrestart") == 0 ||
strcmp(optarg, "condrestart") == 0) strcmp(optarg, "condrestart") == 0)