2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
* runscript.c
|
|
|
|
* Handle launching of Gentoo init scripts.
|
|
|
|
*
|
|
|
|
* Copyright 1999-2007 Gentoo Foundation
|
|
|
|
* Distributed under the terms of the GNU General Public License v2
|
|
|
|
*/
|
|
|
|
|
2007-04-17 18:14:32 +05:30
|
|
|
#define APPLET "runscript"
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <sys/types.h>
|
2007-04-25 18:00:24 +05:30
|
|
|
#include <sys/param.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
2007-04-25 18:00:24 +05:30
|
|
|
#include <fcntl.h>
|
2007-04-12 15:38:42 +05:30
|
|
|
#include <getopt.h>
|
2007-04-19 20:24:35 +05:30
|
|
|
#include <libgen.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <limits.h>
|
2007-04-21 00:28:42 +05:30
|
|
|
#include <signal.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "einfo.h"
|
|
|
|
#include "rc.h"
|
|
|
|
#include "rc-misc.h"
|
|
|
|
#include "rc-plugin.h"
|
|
|
|
#include "strlist.h"
|
|
|
|
|
2007-04-12 18:48:52 +05:30
|
|
|
#define RCSCRIPT_HELP RC_LIBDIR "/sh/rc-help.sh"
|
|
|
|
#define SELINUX_LIB RC_LIBDIR "/runscript_selinux.so"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-26 16:54:07 +05:30
|
|
|
#define PREFIX_LOCK RC_SVCDIR "/prefix.lock"
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
static char *applet = NULL;
|
2007-04-30 18:48:42 +05:30
|
|
|
static char *service = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
static char *exclusive = NULL;
|
|
|
|
static char *mtime_test = NULL;
|
|
|
|
static rc_depinfo_t *deptree = NULL;
|
|
|
|
static char **services = NULL;
|
|
|
|
static char **svclist = NULL;
|
|
|
|
static char **tmplist = NULL;
|
|
|
|
static char **providelist = NULL;
|
|
|
|
static char **types = NULL;
|
|
|
|
static char **restart_services = NULL;
|
|
|
|
static char **need_services = NULL;
|
|
|
|
static char **env = NULL;
|
|
|
|
static char *tmp = NULL;
|
|
|
|
static char *softlevel = NULL;
|
|
|
|
static bool sighup = false;
|
|
|
|
static char *ibsave = NULL;
|
|
|
|
static bool in_background = false;
|
|
|
|
static rc_hook_t hook_out = 0;
|
2007-04-21 00:28:42 +05:30
|
|
|
static pid_t service_pid = 0;
|
2007-04-25 18:00:24 +05:30
|
|
|
static char *prefix = NULL;
|
2007-04-26 16:54:07 +05:30
|
|
|
static bool prefix_locked = false;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
static void (*selinux_run_init_old) (void);
|
|
|
|
static void (*selinux_run_init_new) (int argc, char **argv);
|
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
static void setup_selinux (int argc, char **argv);
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __linux__
|
2007-04-25 18:00:24 +05:30
|
|
|
static void setup_selinux (int argc, char **argv)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
void *lib_handle = NULL;
|
|
|
|
|
|
|
|
lib_handle = dlopen (SELINUX_LIB, RTLD_NOW | RTLD_GLOBAL);
|
|
|
|
if (lib_handle) {
|
2007-04-20 18:42:21 +05:30
|
|
|
/*
|
|
|
|
* FIXME: the below code generates the warning
|
|
|
|
* ISO C forbids assignment between function pointer and 'void *'
|
|
|
|
* which sucks ass
|
|
|
|
* http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html
|
|
|
|
*/
|
2007-05-15 01:20:13 +05:30
|
|
|
selinux_run_init_old = (void (*)(void)) dlfunc (lib_handle, "selinux_runscript");
|
|
|
|
selinux_run_init_new = (void (*)(int, char **)) dlfunc (lib_handle, "selinux_runscript2");
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Use new run_init if it rc_exists, else fall back to old */
|
|
|
|
if (selinux_run_init_new)
|
|
|
|
selinux_run_init_new (argc, argv);
|
|
|
|
else if (selinux_run_init_old)
|
|
|
|
selinux_run_init_old ();
|
|
|
|
else
|
|
|
|
/* This shouldnt happen... probably corrupt lib */
|
|
|
|
eerrorx ("run_init is missing from runscript_selinux.so!");
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void handle_signal (int sig)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
int serrno = errno;
|
|
|
|
char signame[10] = { '\0' };
|
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
case SIGHUP:
|
|
|
|
sighup = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGCHLD:
|
|
|
|
do {
|
|
|
|
pid = waitpid (-1, &status, WNOHANG);
|
|
|
|
if (pid < 0) {
|
|
|
|
if (errno != ECHILD)
|
|
|
|
eerror ("waitpid: %s", strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (! WIFEXITED (status) && ! WIFSIGNALED (status));
|
2007-04-21 00:28:42 +05:30
|
|
|
if (pid == service_pid)
|
|
|
|
service_pid = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGINT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGINT");
|
|
|
|
case SIGTERM:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGTERM");
|
|
|
|
case SIGQUIT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGQUIT");
|
2007-04-21 00:28:42 +05:30
|
|
|
/* Send the signal to our children too */
|
|
|
|
if (service_pid > 0)
|
|
|
|
kill (service_pid, sig);
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: caught %s, aborting", applet, signame);
|
|
|
|
|
|
|
|
default:
|
|
|
|
eerror ("%s: caught unknown signal %d", applet, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore errno */
|
|
|
|
errno = serrno;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static time_t get_mtime (const char *pathname, bool follow_link)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct stat buf;
|
|
|
|
int retval;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! pathname)
|
|
|
|
return (0);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
retval = follow_link ? stat (pathname, &buf) : lstat (pathname, &buf);
|
|
|
|
if (! retval)
|
|
|
|
return (buf.st_mtime);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
errno = 0;
|
|
|
|
return (0);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static bool in_control ()
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *path;
|
|
|
|
time_t mtime;
|
|
|
|
const char *tests[] = { "starting", "started", "stopping",
|
|
|
|
"inactive", "wasinactive", NULL };
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (sighup)
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
if (! mtime_test || ! rc_exists (mtime_test))
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
if (rc_service_state (applet, rc_service_stopped))
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
if (! (mtime = get_mtime (mtime_test, false)))
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
while (tests[i]) {
|
|
|
|
path = rc_strcatpaths (RC_SVCDIR, tests[i], applet, (char *) NULL);
|
|
|
|
if (rc_exists (path)) {
|
|
|
|
time_t m = get_mtime (path, false);
|
|
|
|
if (mtime < m && m != 0) {
|
|
|
|
free (path);
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (path);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
static void uncoldplug ()
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-30 18:48:42 +05:30
|
|
|
char *cold = rc_strcatpaths (RC_SVCDIR "coldplugged", applet, (char *) NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (rc_exists (cold) && unlink (cold) != 0)
|
|
|
|
eerror ("%s: unlink `%s': %s", applet, cold, strerror (errno));
|
|
|
|
free (cold);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
static void start_services (char **list) {
|
|
|
|
bool inactive;
|
|
|
|
char *svc;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (! list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
inactive = rc_service_state (service, rc_service_inactive);
|
|
|
|
if (! inactive)
|
|
|
|
inactive = rc_service_state (service, rc_service_wasinactive);
|
|
|
|
|
|
|
|
if (inactive ||
|
|
|
|
rc_service_state (service, rc_service_starting) ||
|
|
|
|
rc_service_state (service, rc_service_started))
|
|
|
|
{
|
|
|
|
STRLIST_FOREACH (list, svc, i) {
|
|
|
|
if (rc_service_state (svc, rc_service_stopped)) {
|
|
|
|
if (inactive) {
|
|
|
|
rc_schedule_start_service (service, svc);
|
|
|
|
ewarn ("WARNING: %s is scheduled to started when %s has started",
|
|
|
|
svc, applet);
|
|
|
|
} else
|
|
|
|
rc_start_service (svc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
static void cleanup (void)
|
|
|
|
{
|
2007-04-26 16:54:07 +05:30
|
|
|
if (prefix_locked)
|
|
|
|
unlink (PREFIX_LOCK);
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Flush our buffered output if any */
|
2007-04-24 16:59:19 +05:30
|
|
|
eclose ();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (hook_out)
|
|
|
|
rc_plugin_run (hook_out, applet);
|
|
|
|
rc_plugin_unload ();
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
if (restart_services ) {
|
|
|
|
start_services (restart_services);
|
|
|
|
rc_strlist_free (restart_services);
|
|
|
|
}
|
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
rc_free_deptree (deptree);
|
|
|
|
rc_strlist_free (services);
|
|
|
|
rc_strlist_free (types);
|
|
|
|
rc_strlist_free (svclist);
|
|
|
|
rc_strlist_free (providelist);
|
|
|
|
rc_strlist_free (need_services);
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
free (ibsave);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (in_control ()) {
|
|
|
|
if (rc_service_state (applet, rc_service_stopping)) {
|
|
|
|
/* If the we're shutting down, do it cleanly */
|
|
|
|
if ((softlevel &&
|
|
|
|
rc_runlevel_stopping () &&
|
|
|
|
(strcmp (softlevel, RC_LEVEL_SHUTDOWN) == 0 ||
|
|
|
|
strcmp (softlevel, RC_LEVEL_REBOOT) == 0)))
|
|
|
|
rc_mark_service (applet, rc_service_stopped);
|
|
|
|
else if (rc_service_state (applet, rc_service_wasinactive))
|
|
|
|
rc_mark_service (applet, rc_service_inactive);
|
|
|
|
else
|
|
|
|
rc_mark_service (applet, rc_service_started);
|
|
|
|
}
|
|
|
|
else if (rc_service_state (applet, rc_service_starting))
|
|
|
|
{
|
|
|
|
if (rc_service_state (applet, rc_service_wasinactive))
|
|
|
|
rc_mark_service (applet, rc_service_inactive);
|
|
|
|
else
|
|
|
|
rc_mark_service (applet, rc_service_stopped);
|
|
|
|
}
|
|
|
|
if (exclusive && rc_exists (exclusive))
|
|
|
|
unlink (exclusive);
|
|
|
|
}
|
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
rc_strlist_free (env);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (mtime_test)
|
|
|
|
{
|
|
|
|
unlink (mtime_test);
|
|
|
|
free (mtime_test);
|
|
|
|
}
|
2007-04-25 18:00:24 +05:30
|
|
|
free (exclusive);
|
|
|
|
free (applet);
|
|
|
|
free (prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_prefix (int fd, const char *buffer, size_t bytes, bool *prefixed) {
|
|
|
|
unsigned int i;
|
|
|
|
int j;
|
|
|
|
const char *ec;
|
|
|
|
const char *ec_normal = ecolor (ecolor_normal);
|
|
|
|
ssize_t ret = 0;
|
|
|
|
|
|
|
|
if (fd == fileno (stdout))
|
|
|
|
ec = ecolor (ecolor_hilite);
|
|
|
|
else
|
|
|
|
ec = ecolor (ecolor_bad);
|
|
|
|
|
|
|
|
for (i = 0; i < bytes; i++) {
|
|
|
|
/* We don't prefix escape codes, like eend */
|
|
|
|
if (buffer[i] == '\033')
|
|
|
|
*prefixed = true;
|
|
|
|
|
|
|
|
if (! *prefixed) {
|
|
|
|
ret += write (fd, ec, strlen (ec));
|
|
|
|
ret += write (fd, applet, strlen (applet));
|
|
|
|
for (j = strlen (applet); j < 11; j++)
|
|
|
|
ret += write (fd, " ", 1);
|
|
|
|
ret += write (fd, ec_normal, strlen (ec_normal));
|
|
|
|
ret += write (fd, " |", 2);
|
|
|
|
*prefixed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[i] == '\n')
|
|
|
|
*prefixed = false;
|
|
|
|
ret += write (fd, buffer + i, 1);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
return (ret);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
static bool svc_exec (const char *arg1, const char *arg2)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-25 18:00:24 +05:30
|
|
|
bool execok;
|
|
|
|
int stdout_pipes[2];
|
|
|
|
int stderr_pipes[2];
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-24 16:59:19 +05:30
|
|
|
/* To ensure any output has hit our ebuffer */
|
|
|
|
fflush (stdout);
|
|
|
|
fflush (stderr);
|
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
/* Setup our pipes for prefixed output */
|
|
|
|
if (rc_is_env ("RC_PREFIX", "yes")) {
|
|
|
|
if (pipe (stdout_pipes))
|
|
|
|
eerror ("pipe: %s", strerror (errno));
|
|
|
|
if (pipe (stderr_pipes))
|
|
|
|
eerror ("pipe: %s", strerror (errno));
|
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* We need to disable our child signal handler now so we block
|
|
|
|
until our script returns. */
|
|
|
|
signal (SIGCHLD, NULL);
|
|
|
|
|
2007-04-21 00:28:42 +05:30
|
|
|
service_pid = vfork();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-21 00:28:42 +05:30
|
|
|
if (service_pid == -1)
|
2007-04-20 18:42:21 +05:30
|
|
|
eerrorx ("%s: vfork: %s", service, strerror (errno));
|
2007-04-21 00:28:42 +05:30
|
|
|
if (service_pid == 0) {
|
2007-04-25 18:00:24 +05:30
|
|
|
if (rc_is_env ("RC_PREFIX", "yes")) {
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if (dup2 (stdout_pipes[1], fileno (stdout)) == -1)
|
|
|
|
eerror ("dup2 stdout: %s", strerror (errno));
|
|
|
|
close (stdout_pipes[0]);
|
|
|
|
if (dup2 (stderr_pipes[1], fileno (stderr)) == -1)
|
|
|
|
eerror ("dup2 stderr: %s", strerror (errno));
|
|
|
|
close (stderr_pipes[0]);
|
|
|
|
|
|
|
|
/* Stop any scripts from inheriting us */
|
|
|
|
if ((flags = fcntl (stdout_pipes[1], F_GETFD, 0)) < 0 ||
|
|
|
|
fcntl (stdout_pipes[1], F_SETFD, flags | FD_CLOEXEC) < 0)
|
|
|
|
eerror ("fcntl: %s", strerror (errno));
|
|
|
|
if ((flags = fcntl (stderr_pipes[1], F_GETFD, 0)) < 0 ||
|
|
|
|
fcntl (stderr_pipes[1], F_SETFD, flags | FD_CLOEXEC) < 0)
|
|
|
|
eerror ("fcntl: %s", strerror (errno));
|
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (rc_exists (RC_SVCDIR "runscript.sh")) {
|
2007-04-20 18:42:21 +05:30
|
|
|
execl (RC_SVCDIR "runscript.sh", service, service, arg1, arg2,
|
2007-04-11 18:14:47 +05:30
|
|
|
(char *) NULL);
|
2007-04-20 18:42:21 +05:30
|
|
|
eerror ("%s: exec `" RC_SVCDIR "runscript.sh': %s",
|
2007-04-21 00:33:56 +05:30
|
|
|
service, strerror (errno));
|
2007-04-20 18:42:21 +05:30
|
|
|
_exit (EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else {
|
2007-04-20 18:42:21 +05:30
|
|
|
execl (RC_LIBDIR "sh/runscript.sh", service, service, arg1, arg2,
|
2007-04-11 18:14:47 +05:30
|
|
|
(char *) NULL);
|
2007-04-20 18:42:21 +05:30
|
|
|
eerror ("%s: exec `" RC_LIBDIR "sh/runscript.sh': %s",
|
2007-04-21 00:33:56 +05:30
|
|
|
service, strerror (errno));
|
2007-04-20 18:42:21 +05:30
|
|
|
_exit (EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
/* Prefix our piped output */
|
|
|
|
if (rc_is_env ("RC_PREFIX", "yes")) {
|
|
|
|
bool stdout_done = false;
|
|
|
|
bool stdout_prefix_shown = false;
|
|
|
|
bool stderr_done = false;
|
|
|
|
bool stderr_prefix_shown = false;
|
|
|
|
char buffer[RC_LINEBUFFER];
|
|
|
|
|
|
|
|
close (stdout_pipes[1]);
|
|
|
|
close (stderr_pipes[1]);
|
|
|
|
|
|
|
|
memset (buffer, 0, RC_LINEBUFFER);
|
|
|
|
while (! stdout_done && ! stderr_done) {
|
|
|
|
fd_set fds;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
FD_ZERO (&fds);
|
|
|
|
FD_SET (stdout_pipes[0], &fds);
|
|
|
|
FD_SET (stderr_pipes[0], &fds);
|
|
|
|
retval = select (MAX (stdout_pipes[0], stderr_pipes[0]) + 1,
|
|
|
|
&fds, 0, 0, 0);
|
|
|
|
if (retval < 0) {
|
2007-04-25 23:42:49 +05:30
|
|
|
if (errno != EINTR) {
|
2007-04-25 18:00:24 +05:30
|
|
|
eerror ("select: %s", strerror (errno));
|
2007-04-25 23:42:49 +05:30
|
|
|
break;
|
|
|
|
}
|
2007-04-25 18:00:24 +05:30
|
|
|
} else if (retval) {
|
|
|
|
ssize_t nr;
|
|
|
|
|
2007-04-26 16:54:07 +05:30
|
|
|
/* Wait until we get a lock */
|
|
|
|
while (true) {
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (mkfifo (PREFIX_LOCK, 0700) == 0) {
|
|
|
|
prefix_locked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno != EEXIST)
|
|
|
|
eerror ("mkfifo `%s': %s\n", PREFIX_LOCK, strerror (errno));
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 20000;
|
|
|
|
select (0, NULL, NULL, NULL, &tv);
|
|
|
|
}
|
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
if (FD_ISSET (stdout_pipes[0], &fds)) {
|
|
|
|
if ((nr = read (stdout_pipes[0], buffer,
|
|
|
|
sizeof (buffer))) <= 0)
|
|
|
|
stdout_done = true;
|
|
|
|
else
|
|
|
|
write_prefix (fileno (stdout), buffer, nr,
|
|
|
|
&stdout_prefix_shown);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
if (FD_ISSET (stderr_pipes[0], &fds)) {
|
|
|
|
if ((nr = read (stderr_pipes[0], buffer,
|
|
|
|
sizeof (buffer))) <= 0)
|
|
|
|
stderr_done = true;
|
|
|
|
else
|
|
|
|
write_prefix (fileno (stderr), buffer, nr,
|
|
|
|
&stderr_prefix_shown);
|
|
|
|
}
|
2007-04-26 16:54:07 +05:30
|
|
|
|
|
|
|
/* Clear the lock */
|
|
|
|
unlink (PREFIX_LOCK);
|
|
|
|
prefix_locked = false;
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Done now, so close the pipes */
|
|
|
|
close(stdout_pipes[0]);
|
|
|
|
close(stderr_pipes[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
execok = rc_waitpid (service_pid) == 0 ? true : false;
|
2007-04-21 00:28:42 +05:30
|
|
|
service_pid = 0;
|
2007-04-25 18:00:24 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Done, so restore the signal handler */
|
|
|
|
signal (SIGCHLD, handle_signal);
|
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
return (execok);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
static rc_service_state_t svc_status ()
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char status[10];
|
|
|
|
int (*e) (const char *fmt, ...) = &einfo;
|
|
|
|
|
|
|
|
rc_service_state_t retval = rc_service_stopped;
|
|
|
|
|
|
|
|
if (rc_service_state (service, rc_service_stopping)) {
|
|
|
|
snprintf (status, sizeof (status), "stopping");
|
|
|
|
e = &ewarn;
|
|
|
|
retval = rc_service_stopping;
|
|
|
|
} else if (rc_service_state (service, rc_service_starting)) {
|
|
|
|
snprintf (status, sizeof (status), "starting");
|
|
|
|
e = &ewarn;
|
|
|
|
retval = rc_service_starting;
|
|
|
|
} else if (rc_service_state (service, rc_service_inactive)) {
|
|
|
|
snprintf (status, sizeof (status), "inactive");
|
|
|
|
e = &ewarn;
|
|
|
|
retval = rc_service_inactive;
|
|
|
|
} else if (rc_service_state (service, rc_service_crashed)) {
|
|
|
|
snprintf (status, sizeof (status), "crashed");
|
|
|
|
e = &eerror;
|
|
|
|
retval = rc_service_crashed;
|
|
|
|
} else if (rc_service_state (service, rc_service_started)) {
|
|
|
|
snprintf (status, sizeof (status), "started");
|
|
|
|
retval = rc_service_started;
|
|
|
|
} else
|
|
|
|
snprintf (status, sizeof (status), "stopped");
|
|
|
|
|
|
|
|
e ("status: %s", status);
|
|
|
|
return (retval);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
static void make_exclusive ()
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *path;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* We create a fifo so that other services can wait until we complete */
|
|
|
|
if (! exclusive)
|
|
|
|
exclusive = rc_strcatpaths (RC_SVCDIR, "exclusive", applet, (char *) NULL);
|
|
|
|
|
|
|
|
if (mkfifo (exclusive, 0600) != 0 && errno != EEXIST &&
|
|
|
|
(errno != EACCES || geteuid () == 0))
|
|
|
|
eerrorx ("%s: unable to create fifo `%s': %s",
|
|
|
|
applet, exclusive, strerror (errno));
|
|
|
|
|
|
|
|
path = rc_strcatpaths (RC_SVCDIR, "exclusive", applet, (char *) NULL);
|
|
|
|
i = strlen (path) + 16;
|
|
|
|
mtime_test = rc_xmalloc (sizeof (char *) * i);
|
|
|
|
snprintf (mtime_test, i, "%s.%d", path, getpid ());
|
|
|
|
free (path);
|
|
|
|
|
|
|
|
if (rc_exists (mtime_test) && unlink (mtime_test) != 0) {
|
|
|
|
eerror ("%s: unlink `%s': %s",
|
|
|
|
applet, mtime_test, strerror (errno));
|
|
|
|
free (mtime_test);
|
|
|
|
mtime_test = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symlink (service, mtime_test) != 0) {
|
|
|
|
eerror ("%s: symlink `%s' to `%s': %s",
|
|
|
|
applet, service, mtime_test, strerror (errno));
|
|
|
|
free (mtime_test);
|
|
|
|
mtime_test = NULL;
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void unlink_mtime_test ()
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
if (unlink (mtime_test) != 0)
|
|
|
|
eerror ("%s: unlink `%s': %s", applet, mtime_test, strerror (errno));
|
|
|
|
free (mtime_test);
|
|
|
|
mtime_test = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void get_started_services ()
|
|
|
|
{
|
2007-04-30 18:48:42 +05:30
|
|
|
char *svc;
|
2007-04-11 18:14:47 +05:30
|
|
|
int i;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = rc_services_in_state (rc_service_inactive);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_strlist_free (restart_services);
|
|
|
|
restart_services = rc_services_in_state (rc_service_started);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
STRLIST_FOREACH (tmplist, svc, i)
|
|
|
|
restart_services = rc_strlist_addsort (restart_services, svc);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
static void svc_start (bool deps)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
bool started;
|
|
|
|
bool background = false;
|
|
|
|
char *svc;
|
|
|
|
char *svc2;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int depoptions = RC_DEP_TRACE;
|
|
|
|
|
|
|
|
rc_plugin_run (rc_hook_service_start_in, applet);
|
|
|
|
hook_out = rc_hook_service_start_out;
|
|
|
|
|
|
|
|
if (rc_is_env ("RC_STRICT_DEPEND", "yes"))
|
|
|
|
depoptions |= RC_DEP_STRICT;
|
|
|
|
|
|
|
|
if (rc_is_env ("IN_HOTPLUG", "1") || in_background) {
|
2007-04-11 20:22:13 +05:30
|
|
|
if (! rc_service_state (service, rc_service_inactive) &&
|
2007-04-12 18:48:52 +05:30
|
|
|
! rc_service_state (service, rc_service_stopped))
|
2007-04-11 18:14:47 +05:30
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
background = true;
|
|
|
|
}
|
|
|
|
|
2007-04-30 14:36:17 +05:30
|
|
|
if (rc_service_state (service, rc_service_started)) {
|
|
|
|
ewarn ("WARNING: %s has already been started", applet);
|
|
|
|
return;
|
|
|
|
} else if (rc_service_state (service, rc_service_starting))
|
2007-04-11 18:14:47 +05:30
|
|
|
ewarnx ("WARNING: %s is already starting", applet);
|
|
|
|
else if (rc_service_state (service, rc_service_stopping))
|
|
|
|
ewarnx ("WARNING: %s is stopping", applet);
|
|
|
|
else if (rc_service_state (service, rc_service_inactive) && ! background)
|
|
|
|
ewarnx ("WARNING: %s has already started, but is inactive", applet);
|
|
|
|
|
|
|
|
if (! rc_mark_service (service, rc_service_starting))
|
|
|
|
eerrorx ("ERROR: %s has been started by something else", applet);
|
|
|
|
|
|
|
|
make_exclusive (service);
|
|
|
|
|
|
|
|
if (deps) {
|
|
|
|
if (! deptree && ((deptree = rc_load_deptree ()) == NULL))
|
|
|
|
eerrorx ("failed to load deptree");
|
|
|
|
|
|
|
|
rc_strlist_free (types);
|
|
|
|
types = rc_strlist_add (NULL, "broken");
|
|
|
|
rc_strlist_free (svclist);
|
|
|
|
svclist = rc_strlist_add (NULL, applet);
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = rc_get_depends (deptree, types, svclist, softlevel, 0);
|
|
|
|
if (services) {
|
|
|
|
eerrorn ("ERROR: `%s' needs ", applet);
|
|
|
|
STRLIST_FOREACH (services, svc, i) {
|
|
|
|
if (i > 0)
|
|
|
|
fprintf (stderr, ", ");
|
|
|
|
fprintf (stderr, "%s", svc);
|
|
|
|
}
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = NULL;
|
|
|
|
|
|
|
|
rc_strlist_free (types);
|
|
|
|
types = rc_strlist_add (NULL, "ineed");
|
|
|
|
rc_strlist_free (need_services);
|
|
|
|
need_services = rc_get_depends (deptree, types, svclist,
|
|
|
|
softlevel, depoptions);
|
|
|
|
types = rc_strlist_add (types, "iuse");
|
|
|
|
if (! rc_runlevel_starting ()) {
|
|
|
|
services = rc_get_depends (deptree, types, svclist,
|
|
|
|
softlevel, depoptions);
|
|
|
|
STRLIST_FOREACH (services, svc, i)
|
2007-04-21 00:28:42 +05:30
|
|
|
if (rc_service_state (svc, rc_service_stopped)) {
|
|
|
|
pid_t pid = rc_start_service (svc);
|
2007-04-24 16:59:19 +05:30
|
|
|
if (! rc_is_env ("RC_PARALLEL", "yes"))
|
2007-04-21 00:28:42 +05:30
|
|
|
rc_waitpid (pid);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
rc_strlist_free (services);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now wait for them to start */
|
|
|
|
types = rc_strlist_add (types, "iafter");
|
|
|
|
services = rc_get_depends (deptree, types, svclist,
|
|
|
|
softlevel, depoptions);
|
|
|
|
|
|
|
|
/* We use tmplist to hold our scheduled by list */
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = NULL;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (services, svc, i) {
|
|
|
|
if (rc_service_state (svc, rc_service_started))
|
|
|
|
continue;
|
|
|
|
if (! rc_wait_service (svc))
|
|
|
|
eerror ("%s: timed out waiting for %s", applet, svc);
|
|
|
|
if (rc_service_state (svc, rc_service_started))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (need_services, svc2, j)
|
|
|
|
if (strcmp (svc, svc2) == 0) {
|
|
|
|
if (rc_service_state (svc, rc_service_inactive) ||
|
|
|
|
rc_service_state (svc, rc_service_wasinactive))
|
|
|
|
tmplist = rc_strlist_add (tmplist, svc);
|
|
|
|
else
|
|
|
|
eerrorx ("ERROR: cannot start %s as %s would not start",
|
|
|
|
applet, svc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmplist) {
|
|
|
|
int n = 0;
|
|
|
|
int len = 0;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* Set the state now, then unlink our exclusive so that
|
|
|
|
our scheduled list is preserved */
|
|
|
|
rc_mark_service (service, rc_service_stopped);
|
|
|
|
unlink_mtime_test ();
|
|
|
|
|
|
|
|
rc_strlist_free (types);
|
|
|
|
types = rc_strlist_add (NULL, "iprovide");
|
|
|
|
STRLIST_FOREACH (tmplist, svc, i) {
|
|
|
|
rc_schedule_start_service (svc, service);
|
|
|
|
|
|
|
|
rc_strlist_free (svclist);
|
|
|
|
svclist = rc_strlist_add (NULL, svc);
|
|
|
|
rc_strlist_free (providelist);
|
|
|
|
providelist = rc_get_depends (deptree, types, svclist,
|
|
|
|
softlevel, depoptions);
|
|
|
|
STRLIST_FOREACH (providelist, svc2, j)
|
|
|
|
rc_schedule_start_service (svc2, service);
|
|
|
|
|
|
|
|
len += strlen (svc) + 2;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
len += 5;
|
|
|
|
tmp = rc_xmalloc (sizeof (char *) * len);
|
|
|
|
p = tmp;
|
|
|
|
STRLIST_FOREACH (tmplist, svc, i) {
|
|
|
|
if (i > 1) {
|
|
|
|
if (i == n - 1)
|
|
|
|
p += snprintf (p, len, " or ");
|
|
|
|
else
|
|
|
|
p += snprintf (p, len, ", ");
|
|
|
|
}
|
|
|
|
p += snprintf (p, len, "%s", svc);
|
|
|
|
}
|
|
|
|
ewarnx ("WARNING: %s is scheduled to start when %s has started",
|
|
|
|
applet, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = NULL;
|
|
|
|
rc_strlist_free (types);
|
|
|
|
types = NULL;
|
|
|
|
rc_strlist_free (svclist);
|
|
|
|
svclist = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ibsave)
|
|
|
|
setenv ("IN_BACKGROUND", ibsave, 1);
|
|
|
|
rc_plugin_run (rc_hook_service_start_now, applet);
|
2007-04-30 18:48:42 +05:30
|
|
|
started = svc_exec ("start", NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ibsave)
|
|
|
|
unsetenv ("IN_BACKGROUND");
|
|
|
|
|
|
|
|
if (in_control ()) {
|
|
|
|
if (! started) {
|
|
|
|
if (rc_service_state (service, rc_service_wasinactive))
|
|
|
|
rc_mark_service (service, rc_service_inactive);
|
|
|
|
else {
|
|
|
|
rc_mark_service (service, rc_service_stopped);
|
|
|
|
if (rc_runlevel_starting ())
|
|
|
|
rc_mark_service (service, rc_service_failed);
|
|
|
|
}
|
|
|
|
rc_plugin_run (rc_hook_service_start_done, applet);
|
|
|
|
eerrorx ("ERROR: %s failed to start", applet);
|
|
|
|
}
|
|
|
|
rc_mark_service (service, rc_service_started);
|
|
|
|
unlink_mtime_test ();
|
|
|
|
rc_plugin_run (rc_hook_service_start_done, applet);
|
|
|
|
} else {
|
|
|
|
rc_plugin_run (rc_hook_service_start_done, applet);
|
|
|
|
if (rc_service_state (service, rc_service_inactive))
|
|
|
|
ewarnx ("WARNING: %s has started, but is inactive", applet);
|
|
|
|
else
|
|
|
|
ewarnx ("WARNING: %s not under our control, aborting", applet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now start any scheduled services */
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = rc_services_scheduled (service);
|
|
|
|
STRLIST_FOREACH (services, svc, i)
|
|
|
|
if (rc_service_state (svc, rc_service_stopped))
|
|
|
|
rc_start_service (svc);
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = NULL;
|
|
|
|
|
|
|
|
/* Do the same for any services we provide */
|
|
|
|
rc_strlist_free (types);
|
|
|
|
types = rc_strlist_add (NULL, "iprovide");
|
|
|
|
rc_strlist_free (svclist);
|
|
|
|
svclist = rc_strlist_add (NULL, applet);
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = rc_get_depends (deptree, types, svclist, softlevel, depoptions);
|
|
|
|
|
|
|
|
STRLIST_FOREACH (tmplist, svc2, j) {
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = rc_services_scheduled (svc2);
|
|
|
|
STRLIST_FOREACH (services, svc, i)
|
|
|
|
if (rc_service_state (svc, rc_service_stopped))
|
|
|
|
rc_start_service (svc);
|
|
|
|
}
|
|
|
|
|
|
|
|
hook_out = 0;
|
|
|
|
rc_plugin_run (rc_hook_service_start_out, applet);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
static void svc_stop (bool deps)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
bool stopped;
|
|
|
|
|
|
|
|
hook_out = rc_hook_service_stop_out;
|
|
|
|
|
|
|
|
if (rc_runlevel_stopping () &&
|
|
|
|
rc_service_state (service, rc_service_failed))
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
|
|
|
if (rc_is_env ("IN_HOTPLUG", "1") || in_background)
|
2007-04-11 20:22:13 +05:30
|
|
|
if (! rc_service_state (service, rc_service_started) &&
|
2007-04-12 18:48:52 +05:30
|
|
|
! rc_service_state (service, rc_service_inactive))
|
2007-04-11 18:14:47 +05:30
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
2007-04-30 14:36:17 +05:30
|
|
|
if (rc_service_state (service, rc_service_stopped)) {
|
|
|
|
ewarn ("WARNING: %s is already stopped", applet);
|
|
|
|
return;
|
|
|
|
} else if (rc_service_state (service, rc_service_stopping))
|
2007-04-11 18:14:47 +05:30
|
|
|
ewarnx ("WARNING: %s is already stopping", applet);
|
|
|
|
|
|
|
|
if (! rc_mark_service (service, rc_service_stopping))
|
|
|
|
eerrorx ("ERROR: %s has been stopped by something else", applet);
|
|
|
|
|
|
|
|
make_exclusive (service);
|
|
|
|
|
|
|
|
if (! rc_runlevel_stopping () &&
|
|
|
|
rc_service_in_runlevel (service, RC_LEVEL_BOOT))
|
|
|
|
ewarn ("WARNING: you are stopping a boot service");
|
|
|
|
|
|
|
|
if (deps || ! rc_service_state (service, rc_service_wasinactive)) {
|
|
|
|
int depoptions = RC_DEP_TRACE;
|
|
|
|
char *svc;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (rc_is_env ("RC_STRICT_DEPEND", "yes"))
|
|
|
|
depoptions |= RC_DEP_STRICT;
|
|
|
|
|
|
|
|
if (! deptree && ((deptree = rc_load_deptree ()) == NULL))
|
|
|
|
eerrorx ("failed to load deptree");
|
|
|
|
|
|
|
|
rc_strlist_free (types);
|
|
|
|
types = rc_strlist_add (NULL, "needsme");
|
|
|
|
rc_strlist_free (svclist);
|
|
|
|
svclist = rc_strlist_add (NULL, applet);
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = NULL;
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = rc_get_depends (deptree, types, svclist,
|
|
|
|
softlevel, depoptions);
|
|
|
|
rc_strlist_reverse (services);
|
|
|
|
STRLIST_FOREACH (services, svc, i) {
|
|
|
|
if (rc_service_state (svc, rc_service_started) ||
|
|
|
|
rc_service_state (svc, rc_service_inactive))
|
|
|
|
{
|
|
|
|
rc_wait_service (svc);
|
|
|
|
if (rc_service_state (svc, rc_service_started) ||
|
|
|
|
rc_service_state (svc, rc_service_inactive))
|
|
|
|
{
|
2007-04-21 00:28:42 +05:30
|
|
|
pid_t pid = rc_stop_service (svc);
|
2007-04-24 16:59:19 +05:30
|
|
|
if (! rc_is_env ("RC_PARALLEL", "yes"))
|
2007-04-21 00:28:42 +05:30
|
|
|
rc_waitpid (pid);
|
2007-04-11 18:14:47 +05:30
|
|
|
tmplist = rc_strlist_add (tmplist, svc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = NULL;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (tmplist, svc, i) {
|
|
|
|
if (rc_service_state (svc, rc_service_stopped))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We used to loop 3 times here - maybe re-do this if needed */
|
|
|
|
rc_wait_service (svc);
|
|
|
|
if (! rc_service_state (svc, rc_service_stopped)) {
|
|
|
|
if (rc_runlevel_stopping ())
|
|
|
|
rc_mark_service (svc, rc_service_failed);
|
|
|
|
eerrorx ("ERROR: cannot stop %s as %s is still up",
|
|
|
|
applet, svc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = NULL;
|
|
|
|
|
|
|
|
/* We now wait for other services that may use us and are stopping
|
|
|
|
This is important when a runlevel stops */
|
|
|
|
types = rc_strlist_add (types, "usesme");
|
|
|
|
types = rc_strlist_add (types, "ibefore");
|
|
|
|
services = rc_get_depends (deptree, types, svclist,
|
|
|
|
softlevel, depoptions);
|
|
|
|
STRLIST_FOREACH (services, svc, i) {
|
|
|
|
if (rc_service_state (svc, rc_service_stopped))
|
|
|
|
continue;
|
|
|
|
rc_wait_service (svc);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = NULL;
|
|
|
|
rc_strlist_free (types);
|
|
|
|
types = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ibsave)
|
|
|
|
setenv ("IN_BACKGROUND", ibsave, 1);
|
|
|
|
rc_plugin_run (rc_hook_service_stop_now, applet);
|
2007-04-30 18:48:42 +05:30
|
|
|
stopped = svc_exec ("stop", NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ibsave)
|
|
|
|
unsetenv ("IN_BACKGROUND");
|
|
|
|
|
|
|
|
if (! in_control ()) {
|
|
|
|
rc_plugin_run (rc_hook_service_stop_done, applet);
|
|
|
|
ewarnx ("WARNING: %s not under our control, aborting", applet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! stopped) {
|
|
|
|
if (rc_service_state (service, rc_service_wasinactive))
|
|
|
|
rc_mark_service (service, rc_service_inactive);
|
|
|
|
else
|
|
|
|
rc_mark_service (service, rc_service_started);
|
|
|
|
rc_plugin_run (rc_hook_service_stop_done, applet);
|
|
|
|
eerrorx ("ERROR: %s failed to stop", applet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_background)
|
|
|
|
rc_mark_service (service, rc_service_inactive);
|
|
|
|
else
|
|
|
|
rc_mark_service (service, rc_service_stopped);
|
|
|
|
|
|
|
|
unlink_mtime_test ();
|
|
|
|
rc_plugin_run (rc_hook_service_stop_done, applet);
|
|
|
|
hook_out = 0;
|
|
|
|
rc_plugin_run (rc_hook_service_stop_out, applet);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
static void svc_restart (bool deps)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
/* This is hairly and a better way needs to be found I think!
|
|
|
|
The issue is this - openvpn need net and dns. net can restart
|
|
|
|
dns via resolvconf, so you could have openvpn trying to restart dnsmasq
|
|
|
|
which in turn is waiting on net which in turn is waiting on dnsmasq.
|
|
|
|
The work around is for resolvconf to restart it's services with --nodeps
|
|
|
|
which means just that. The downside is that there is a small window when
|
|
|
|
our status is invalid.
|
|
|
|
One workaround would be to introduce a new status, or status locking. */
|
|
|
|
if (! deps) {
|
|
|
|
if (rc_service_state (service, rc_service_started) ||
|
|
|
|
rc_service_state (service, rc_service_inactive))
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_exec ("stop", "start");
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_exec ("start", NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! rc_service_state (service, rc_service_stopped)) {
|
|
|
|
get_started_services ();
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_stop (deps);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Flush our buffered output if any */
|
|
|
|
eflush ();
|
|
|
|
}
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_start (deps);
|
|
|
|
start_services (restart_services);
|
|
|
|
rc_strlist_free (restart_services);
|
|
|
|
restart_services = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-17 18:14:32 +05:30
|
|
|
#define getoptstring "dCDNqvh"
|
|
|
|
static struct option longopts[] = {
|
|
|
|
{ "debug", 0, NULL, 'd'},
|
|
|
|
{ "nocolor", 0, NULL, 'C'},
|
|
|
|
{ "nocolour", 0, NULL, 'C'},
|
|
|
|
{ "nodeps", 0, NULL, 'D'},
|
|
|
|
{ "quiet", 0, NULL, 'q'},
|
|
|
|
{ "verbose", 0, NULL, 'v'},
|
|
|
|
{ "help", 0, NULL, 'h'},
|
|
|
|
{ NULL, 0, NULL, 0}
|
|
|
|
};
|
2007-04-19 20:24:35 +05:30
|
|
|
// #include "_usage.c"
|
2007-04-17 18:14:32 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int i;
|
|
|
|
bool deps = true;
|
|
|
|
bool doneone = false;
|
|
|
|
char pid[16];
|
|
|
|
int retval;
|
2007-05-14 17:54:18 +05:30
|
|
|
int opt;
|
2007-04-12 15:38:42 +05:30
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
service = argv[1];
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Show help if insufficient args */
|
|
|
|
if (argc < 3) {
|
|
|
|
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL);
|
|
|
|
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
|
|
|
|
applet, strerror (errno));
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-19 20:24:35 +05:30
|
|
|
applet = rc_xstrdup (basename (service));
|
2007-04-12 16:10:51 +05:30
|
|
|
atexit (cleanup);
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#ifdef __linux__
|
2007-04-11 18:14:47 +05:30
|
|
|
/* coldplug events can trigger init scripts, but we don't want to run them
|
|
|
|
until after rc sysinit has completed so we punt them to the boot runlevel */
|
|
|
|
if (rc_exists ("/dev/.rcsysinit")) {
|
|
|
|
eerror ("%s: cannot run until sysvinit completes", applet);
|
|
|
|
if (mkdir ("/dev/.rcboot", 0755) != 0 && errno != EEXIST)
|
|
|
|
eerrorx ("%s: mkdir `/dev/.rcboot': %s", applet, strerror (errno));
|
|
|
|
tmp = rc_strcatpaths ("/dev/.rcboot", applet, (char *) NULL);
|
|
|
|
symlink (service, tmp);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if ((softlevel = getenv ("RC_SOFTLEVEL")) == NULL) {
|
|
|
|
/* Ensure our environment is pure
|
|
|
|
Also, add our configuration to it */
|
|
|
|
env = rc_filter_env ();
|
|
|
|
env = rc_config_env (env);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (env) {
|
|
|
|
char *p;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifdef __linux__
|
2007-04-11 18:14:47 +05:30
|
|
|
/* clearenv isn't portable, but there's no harm in using it
|
|
|
|
if we have it */
|
|
|
|
clearenv ();
|
2007-04-05 16:48:42 +05:30
|
|
|
#else
|
2007-04-11 18:14:47 +05:30
|
|
|
char *var;
|
|
|
|
/* No clearenv present here then.
|
|
|
|
We could manipulate environ directly ourselves, but it seems that
|
|
|
|
some kernels bitch about this according to the environ man pages
|
|
|
|
so we walk though environ and call unsetenv for each value. */
|
|
|
|
while (environ[0]) {
|
|
|
|
tmp = rc_xstrdup (environ[0]);
|
|
|
|
p = tmp;
|
|
|
|
var = strsep (&p, "=");
|
|
|
|
unsetenv (var);
|
|
|
|
free (tmp);
|
|
|
|
}
|
|
|
|
tmp = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
STRLIST_FOREACH (env, p, i)
|
|
|
|
putenv (p);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* We don't free our list as that would be null in environ */
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
softlevel = rc_get_runlevel ();
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* If not called from RC or another service then don't be parallel */
|
2007-04-24 16:59:19 +05:30
|
|
|
unsetenv ("RC_PARALLEL");
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
setenv ("RC_ELOG", service, 1);
|
|
|
|
setenv ("SVCNAME", applet, 1);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Set an env var so that we always know our pid regardless of any
|
|
|
|
subshells the init script may create so that our mark_service_*
|
|
|
|
functions can always instruct us of this change */
|
|
|
|
snprintf (pid, sizeof (pid), "%d", (int) getpid ());
|
|
|
|
setenv ("RC_RUNSCRIPT_PID", pid, 1);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
/* eprefix is kinda klunky, but it works for our purposes */
|
|
|
|
if (rc_is_env ("RC_PREFIX", "yes")) {
|
|
|
|
int l = strlen (applet);
|
|
|
|
if (l < 13)
|
|
|
|
l = 13;
|
|
|
|
prefix = rc_xmalloc (sizeof (char *) * l);
|
|
|
|
snprintf (prefix, l, "%s%s", applet, " ");
|
|
|
|
eprefix (prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're in parallel and we're not prefixing then we need the ebuffer */
|
|
|
|
if (rc_is_env ("RC_PARALLEL", "yes") && ! rc_is_env ("RC_PREFIX", "yes")) {
|
2007-04-11 18:14:47 +05:30
|
|
|
char ebname[PATH_MAX];
|
|
|
|
char *eb;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
snprintf (ebname, sizeof (ebname), "%s.%s", applet, pid);
|
|
|
|
eb = rc_strcatpaths (RC_SVCDIR "ebuffer", ebname, (char *) NULL);
|
2007-04-24 16:59:19 +05:30
|
|
|
ebuffer (eb);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (eb);
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifdef __linux__
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Ok, we are ready to go, so setup selinux if applicable */
|
|
|
|
setup_selinux (argc, argv);
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-04-12 16:10:51 +05:30
|
|
|
/* Punt the first arg as it's our service name */
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Right then, parse any options there may be */
|
2007-05-14 17:54:18 +05:30
|
|
|
while ((opt = getopt_long (argc, argv, getoptstring,
|
|
|
|
longopts, (int *) 0)) != -1)
|
|
|
|
switch (opt) {
|
2007-04-12 15:38:42 +05:30
|
|
|
case 'd':
|
|
|
|
setenv ("RC_DEBUG", "yes", 1);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL);
|
|
|
|
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
|
|
|
|
applet, strerror (errno));
|
|
|
|
case 'C':
|
|
|
|
setenv ("RC_NOCOLOR", "yes", 1);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
deps = false;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
setenv ("RC_QUIET", "yes", 1);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
setenv ("RC_VERBOSE", "yes", 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-12 16:10:51 +05:30
|
|
|
/* Save the IN_BACKGROUND env flag so it's ONLY passed to the service
|
|
|
|
that is being called and not any dependents */
|
|
|
|
if (getenv ("IN_BACKGROUND")) {
|
|
|
|
in_background = rc_is_env ("IN_BACKGROUND", "true");
|
2007-04-19 20:24:35 +05:30
|
|
|
ibsave = rc_xstrdup (getenv ("IN_BACKGROUND"));
|
2007-04-12 16:10:51 +05:30
|
|
|
unsetenv ("IN_BACKGROUND");
|
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (rc_is_env ("IN_HOTPLUG", "1")) {
|
|
|
|
if (! rc_is_env ("RC_HOTPLUG", "yes") || ! rc_allow_plug (applet))
|
|
|
|
eerrorx ("%s: not allowed to be hotplugged", applet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup a signal handler */
|
|
|
|
signal (SIGHUP, handle_signal);
|
|
|
|
signal (SIGINT, handle_signal);
|
|
|
|
signal (SIGQUIT, handle_signal);
|
|
|
|
signal (SIGTERM, handle_signal);
|
|
|
|
signal (SIGCHLD, handle_signal);
|
|
|
|
|
|
|
|
/* Load our plugins */
|
|
|
|
rc_plugin_load ();
|
|
|
|
|
|
|
|
/* Now run each option */
|
|
|
|
retval = EXIT_SUCCESS;
|
2007-04-12 15:38:42 +05:30
|
|
|
while (optind < argc) {
|
|
|
|
optarg = argv[optind++];
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Abort on a sighup here */
|
|
|
|
if (sighup)
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
|
|
|
/* Export the command we're running.
|
|
|
|
This is important as we stamp on the restart function now but
|
|
|
|
some start/stop routines still need to behave differently if
|
|
|
|
restarting. */
|
|
|
|
unsetenv ("RC_CMD");
|
2007-04-12 15:38:42 +05:30
|
|
|
setenv ("RC_CMD", optarg, 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
doneone = true;
|
2007-04-12 15:38:42 +05:30
|
|
|
if (strcmp (optarg, "conditionalrestart") == 0 ||
|
|
|
|
strcmp (optarg, "condrestart") == 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
|
|
|
if (rc_service_state (service, rc_service_started))
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_restart (deps);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-04-12 15:38:42 +05:30
|
|
|
else if (strcmp (optarg, "restart") == 0)
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_restart (deps);
|
2007-04-12 15:38:42 +05:30
|
|
|
else if (strcmp (optarg, "start") == 0)
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_start (deps);
|
2007-04-12 15:38:42 +05:30
|
|
|
else if (strcmp (optarg, "status") == 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_service_state_t r = svc_status (service);
|
|
|
|
retval = (int) r;
|
2007-04-12 15:38:42 +05:30
|
|
|
} else if (strcmp (optarg, "stop") == 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if (in_background)
|
|
|
|
get_started_services ();
|
|
|
|
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_stop (deps);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (! in_background &&
|
|
|
|
! rc_runlevel_stopping () &&
|
|
|
|
rc_service_state (service, rc_service_stopped))
|
2007-04-30 18:48:42 +05:30
|
|
|
uncoldplug ();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (in_background &&
|
|
|
|
rc_service_state (service, rc_service_inactive))
|
|
|
|
{
|
|
|
|
char *svc;
|
|
|
|
int j;
|
|
|
|
STRLIST_FOREACH (restart_services, svc, j)
|
|
|
|
if (rc_service_state (svc, rc_service_stopped))
|
|
|
|
rc_schedule_start_service (service, svc);
|
|
|
|
}
|
2007-04-12 15:38:42 +05:30
|
|
|
} else if (strcmp (optarg, "zap") == 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
einfo ("Manually resetting %s to stopped state", applet);
|
|
|
|
rc_mark_service (applet, rc_service_stopped);
|
2007-04-30 18:48:42 +05:30
|
|
|
uncoldplug ();
|
2007-04-12 15:38:42 +05:30
|
|
|
} else if (strcmp (optarg, "help") == 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, "help", (char *) NULL);
|
|
|
|
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
|
|
|
|
applet, strerror (errno));
|
|
|
|
}else
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_exec (optarg, NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Flush our buffered output if any */
|
|
|
|
eflush ();
|
|
|
|
|
|
|
|
/* We should ensure this list is empty after an action is done */
|
|
|
|
rc_strlist_free (restart_services);
|
|
|
|
restart_services = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! doneone) {
|
|
|
|
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL);
|
|
|
|
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
|
|
|
|
applet, strerror (errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (retval);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|