2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
* runscript.c
|
2007-11-14 20:52:04 +05:30
|
|
|
* Handle launching of init scripts.
|
|
|
|
*/
|
|
|
|
|
2008-01-14 10:35:22 +05:30
|
|
|
/*
|
2008-02-22 17:37:34 +05:30
|
|
|
* Copyright 2007-2008 Roy Marples
|
2007-11-14 20:52:04 +05:30
|
|
|
* 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.
|
2007-04-05 16:48:42 +05:30
|
|
|
*
|
2007-11-14 20:52:04 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
|
2007-04-05 16:48:42 +05:30
|
|
|
*/
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
#include <sys/select.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <sys/types.h>
|
2007-09-21 14:19:43 +05:30
|
|
|
#include <sys/ioctl.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 <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-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>
|
2007-09-21 14:19:43 +05:30
|
|
|
#include <termios.h>
|
2007-12-07 20:01:51 +05:30
|
|
|
#include <time.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-09-21 14:19:43 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
# include <pty.h>
|
2008-01-10 04:52:04 +05:30
|
|
|
#elif defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
# include <util.h>
|
2007-09-21 14:19:43 +05:30
|
|
|
#else
|
|
|
|
# include <libutil.h>
|
|
|
|
#endif
|
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
#include "builtins.h"
|
2008-01-06 19:17:39 +05:30
|
|
|
#include "einfo.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
#include "rc.h"
|
2008-01-06 19:17:39 +05:30
|
|
|
#include "rc-misc.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
#include "rc-plugin.h"
|
2008-01-06 19:17:39 +05:30
|
|
|
#include "strlist.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-12 18:48:52 +05:30
|
|
|
#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-12-05 23:18:07 +05:30
|
|
|
/* usecs to wait while we poll the fifo */
|
|
|
|
#define WAIT_INTERVAL 20000000
|
|
|
|
|
|
|
|
/* max secs to wait until a service comes up */
|
|
|
|
#define WAIT_MAX 300
|
|
|
|
|
|
|
|
#define ONE_SECOND 1000000000
|
|
|
|
|
2007-12-19 18:16:08 +05:30
|
|
|
static const char *applet = NULL;
|
2008-01-18 16:57:49 +05:30
|
|
|
static char **applet_list = NULL;
|
2007-11-21 21:08:07 +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 **tmplist = NULL;
|
|
|
|
static char **providelist = NULL;
|
|
|
|
static char **restart_services = NULL;
|
|
|
|
static char **need_services = NULL;
|
2007-07-03 14:08:27 +05:30
|
|
|
static char **use_services = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
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-09-21 14:19:43 +05:30
|
|
|
static int signal_pipe[2] = { -1, -1 };
|
|
|
|
static int master_tty = -1;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
2007-12-20 20:32:04 +05:30
|
|
|
static const char *const types_b[] = { "broken", NULL };
|
|
|
|
static const char *const types_n[] = { "ineed", NULL };
|
|
|
|
static const char *const types_nu[] = { "ineed", "iuse", NULL };
|
|
|
|
static const char *const types_nua[] = { "ineed", "iuse", "iafter", NULL };
|
2007-10-15 20:10:53 +05:30
|
|
|
|
2007-12-20 20:32:04 +05:30
|
|
|
static const char *const types_m[] = { "needsme", NULL };
|
|
|
|
static const char *const types_mua[] = { "needsme", "usesme", "beforeme", NULL };
|
2007-10-15 20:10:53 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#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
|
|
|
|
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;
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
if (! exists (SELINUX_LIB))
|
2007-08-08 08:37:42 +05:30
|
|
|
return;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
lib_handle = dlopen (SELINUX_LIB, RTLD_NOW | RTLD_GLOBAL);
|
2007-08-08 08:37:42 +05:30
|
|
|
if (! lib_handle) {
|
|
|
|
eerror ("dlopen: %s", dlerror ());
|
|
|
|
return;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-08-08 08:37:42 +05:30
|
|
|
|
2007-08-09 20:03:20 +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-08-08 08:37:42 +05:30
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
/* Use new run_init if it exists, else fall back to old */
|
2007-08-08 08:37:42 +05:30
|
|
|
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!");
|
|
|
|
|
|
|
|
dlclose (lib_handle);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void handle_signal (int sig)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int serrno = errno;
|
|
|
|
char signame[10] = { '\0' };
|
2007-09-21 14:19:43 +05:30
|
|
|
struct winsize ws;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
case SIGHUP:
|
|
|
|
sighup = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGCHLD:
|
2007-09-21 14:19:43 +05:30
|
|
|
if (signal_pipe[1] > -1) {
|
|
|
|
if (write (signal_pipe[1], &sig, sizeof (sig)) == -1)
|
|
|
|
eerror ("%s: send: %s", service, strerror (errno));
|
2007-10-23 01:03:42 +05:30
|
|
|
} else
|
|
|
|
rc_waitpid (-1);
|
2007-09-21 14:19:43 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGWINCH:
|
|
|
|
if (master_tty >= 0) {
|
|
|
|
ioctl (fileno (stdout), TIOCGWINSZ, &ws);
|
|
|
|
ioctl (master_tty, TIOCSWINSZ, &ws);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGINT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGINT");
|
2008-01-22 16:37:39 +05:30
|
|
|
/* FALLTHROUGH */
|
2007-04-11 18:14:47 +05:30
|
|
|
case SIGTERM:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGTERM");
|
2008-01-22 16:37:39 +05:30
|
|
|
/* FALLTHROUGH */
|
2007-04-11 18:14:47 +05:30
|
|
|
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 */
|
2008-01-14 10:35:22 +05:30
|
|
|
if (service_pid > 0)
|
2007-04-21 00:28:42 +05:30
|
|
|
kill (service_pid, sig);
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: caught %s, aborting", applet, signame);
|
2008-01-22 16:37:39 +05:30
|
|
|
/* NOTREACHED */
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
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);
|
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
if (! mtime_test || ! exists (mtime_test))
|
2007-04-11 18:14:47 +05:30
|
|
|
return (false);
|
|
|
|
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (applet) & RC_SERVICE_STOPPED)
|
2007-04-11 18:14:47 +05:30
|
|
|
return (false);
|
|
|
|
|
|
|
|
if (! (mtime = get_mtime (mtime_test, false)))
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
while (tests[i]) {
|
|
|
|
path = rc_strcatpaths (RC_SVCDIR, tests[i], applet, (char *) NULL);
|
2007-10-08 16:41:21 +05:30
|
|
|
if (exists (path)) {
|
2007-04-11 18:14:47 +05:30
|
|
|
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-08-09 20:03:20 +05:30
|
|
|
char *cold = rc_strcatpaths (RC_SVCDIR, "coldplugged", applet, (char *) NULL);
|
2007-10-08 16:41:21 +05:30
|
|
|
if (exists (cold) && unlink (cold) != 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
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) {
|
|
|
|
char *svc;
|
|
|
|
int i;
|
2007-09-28 20:23:38 +05:30
|
|
|
rc_service_state_t state = rc_service_state (service);
|
2007-04-30 18:48:42 +05:30
|
|
|
|
|
|
|
if (! list)
|
|
|
|
return;
|
|
|
|
|
2007-10-26 17:52:26 +05:30
|
|
|
if (state & RC_SERVICE_INACTIVE ||
|
2008-01-11 21:21:40 +05:30
|
|
|
state & RC_SERVICE_WASINACTIVE ||
|
|
|
|
state & RC_SERVICE_STARTING ||
|
|
|
|
state & RC_SERVICE_STARTED)
|
2007-04-30 18:48:42 +05:30
|
|
|
{
|
|
|
|
STRLIST_FOREACH (list, svc, i) {
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (svc) & RC_SERVICE_STOPPED) {
|
|
|
|
if (state & RC_SERVICE_INACTIVE ||
|
2008-01-11 21:21:40 +05:30
|
|
|
state & RC_SERVICE_WASINACTIVE)
|
2007-09-28 20:23:38 +05:30
|
|
|
{
|
2007-10-03 19:48:52 +05:30
|
|
|
rc_service_schedule_start (service, svc);
|
2007-04-30 18:48:42 +05:30
|
|
|
ewarn ("WARNING: %s is scheduled to started when %s has started",
|
2008-01-11 21:21:40 +05:30
|
|
|
svc, applet);
|
2007-04-30 18:48:42 +05:30
|
|
|
} else
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_start (svc);
|
2007-04-30 18:48:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-10 02:50:10 +05:30
|
|
|
static void restore_state (void)
|
|
|
|
{
|
|
|
|
rc_service_state_t state;
|
|
|
|
|
|
|
|
if (rc_in_plugin || ! in_control ())
|
|
|
|
return;
|
|
|
|
|
|
|
|
state = rc_service_state (applet);
|
|
|
|
if (state & RC_SERVICE_STOPPING) {
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-10-10 02:50:10 +05:30
|
|
|
if (state & RC_SERVICE_WASINACTIVE)
|
|
|
|
rc_service_mark (applet, RC_SERVICE_INACTIVE);
|
|
|
|
else
|
|
|
|
rc_service_mark (applet, RC_SERVICE_STARTED);
|
|
|
|
if (rc_runlevel_stopping ())
|
|
|
|
rc_service_mark (applet, RC_SERVICE_FAILED);
|
|
|
|
} else if (state & RC_SERVICE_STARTING) {
|
|
|
|
if (state & RC_SERVICE_WASINACTIVE)
|
|
|
|
rc_service_mark (applet, RC_SERVICE_INACTIVE);
|
|
|
|
else
|
|
|
|
rc_service_mark (applet, RC_SERVICE_STOPPED);
|
|
|
|
if (rc_runlevel_starting ())
|
|
|
|
rc_service_mark (applet, RC_SERVICE_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exclusive)
|
|
|
|
unlink (exclusive);
|
|
|
|
free (exclusive);
|
|
|
|
exclusive = NULL;
|
|
|
|
}
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
static void cleanup (void)
|
|
|
|
{
|
2007-10-10 02:50:10 +05:30
|
|
|
restore_state ();
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-07-11 19:58:54 +05:30
|
|
|
if (! rc_in_plugin) {
|
|
|
|
if (prefix_locked)
|
|
|
|
unlink (PREFIX_LOCK);
|
2007-10-10 02:50:10 +05:30
|
|
|
if (hook_out) {
|
2007-07-11 19:58:54 +05:30
|
|
|
rc_plugin_run (hook_out, applet);
|
2007-10-10 02:50:10 +05:30
|
|
|
if (hook_out == RC_HOOK_SERVICE_START_DONE)
|
|
|
|
rc_plugin_run (RC_HOOK_SERVICE_START_OUT, applet);
|
|
|
|
else if (hook_out == RC_HOOK_SERVICE_STOP_DONE)
|
|
|
|
rc_plugin_run (RC_HOOK_SERVICE_STOP_OUT, applet);
|
|
|
|
}
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-07-11 19:58:54 +05:30
|
|
|
if (restart_services)
|
|
|
|
start_services (restart_services);
|
2007-04-30 18:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-07-11 19:58:54 +05:30
|
|
|
rc_plugin_unload ();
|
2007-09-29 22:12:08 +05:30
|
|
|
rc_deptree_free (deptree);
|
2007-04-25 18:00:24 +05:30
|
|
|
rc_strlist_free (services);
|
|
|
|
rc_strlist_free (providelist);
|
|
|
|
rc_strlist_free (need_services);
|
2007-07-03 14:08:27 +05:30
|
|
|
rc_strlist_free (use_services);
|
2007-07-11 19:58:54 +05:30
|
|
|
rc_strlist_free (restart_services);
|
2008-01-18 16:57:49 +05:30
|
|
|
rc_strlist_free (applet_list);
|
2007-04-25 18:00:24 +05:30
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
free (ibsave);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
rc_strlist_free (env);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (mtime_test)
|
|
|
|
{
|
2007-07-04 21:32:01 +05:30
|
|
|
if (! rc_in_plugin)
|
|
|
|
unlink (mtime_test);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (mtime_test);
|
|
|
|
}
|
2007-04-25 18:00:24 +05:30
|
|
|
free (exclusive);
|
2007-11-21 21:08:07 +05:30
|
|
|
free (service);
|
2007-04-25 18:00:24 +05:30
|
|
|
free (prefix);
|
2007-09-25 21:08:21 +05:30
|
|
|
free (softlevel);
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
|
2007-09-21 14:19:43 +05:30
|
|
|
static int write_prefix (const char *buffer, size_t bytes, bool *prefixed) {
|
2007-04-25 18:00:24 +05:30
|
|
|
unsigned int i;
|
2007-09-28 17:59:23 +05:30
|
|
|
const char *ec = ecolor (ECOLOR_HILITE);
|
|
|
|
const char *ec_normal = ecolor (ECOLOR_NORMAL);
|
2007-04-25 18:00:24 +05:30
|
|
|
ssize_t ret = 0;
|
2007-09-21 14:19:43 +05:30
|
|
|
int fd = fileno (stdout);
|
2007-04-25 18:00:24 +05:30
|
|
|
|
|
|
|
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));
|
2007-07-06 21:20:40 +05:30
|
|
|
ret += write (fd, prefix, strlen (prefix));
|
2007-04-25 18:00:24 +05:30
|
|
|
ret += write (fd, ec_normal, strlen (ec_normal));
|
2007-07-11 17:36:37 +05:30
|
|
|
ret += write (fd, "|", 1);
|
2007-04-25 18:00:24 +05:30
|
|
|
*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;
|
2007-09-21 14:19:43 +05:30
|
|
|
int fdout = fileno (stdout);
|
|
|
|
struct termios tt;
|
|
|
|
struct winsize ws;
|
|
|
|
int i;
|
2007-10-09 18:22:09 +05:30
|
|
|
int flags = 0;
|
2007-09-21 14:19:43 +05:30
|
|
|
fd_set rset;
|
|
|
|
int s;
|
2007-10-12 05:31:33 +05:30
|
|
|
char *buffer;
|
2007-09-21 14:19:43 +05:30
|
|
|
size_t bytes;
|
|
|
|
bool prefixed = false;
|
|
|
|
int selfd;
|
|
|
|
int slave_tty;
|
|
|
|
|
|
|
|
/* Setup our signal pipe */
|
|
|
|
if (pipe (signal_pipe) == -1)
|
|
|
|
eerrorx ("%s: pipe: %s", service, applet);
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
if ((flags = fcntl (signal_pipe[i], F_GETFD, 0) == -1 ||
|
2008-01-11 21:21:40 +05:30
|
|
|
fcntl (signal_pipe[i], F_SETFD, flags | FD_CLOEXEC) == -1))
|
2007-09-21 14:19:43 +05:30
|
|
|
eerrorx ("%s: fcntl: %s", service, strerror (errno));
|
|
|
|
|
|
|
|
/* Open a pty for our prefixed output
|
|
|
|
* We do this instead of mapping pipes to stdout, stderr so that
|
|
|
|
* programs can tell if they're attached to a tty or not.
|
|
|
|
* The only loss is that we can no longer tell the difference
|
|
|
|
* between the childs stdout or stderr */
|
|
|
|
master_tty = slave_tty = -1;
|
|
|
|
if (prefix && isatty (fdout)) {
|
|
|
|
tcgetattr (fdout, &tt);
|
|
|
|
ioctl (fdout, TIOCGWINSZ, &ws);
|
|
|
|
|
|
|
|
/* If the below call fails due to not enough ptys then we don't
|
|
|
|
* prefix the output, but we still work */
|
|
|
|
openpty (&master_tty, &slave_tty, NULL, &tt, &ws);
|
2008-02-29 02:56:53 +05:30
|
|
|
|
|
|
|
if (master_tty >= 0 &&
|
|
|
|
(flags = fcntl (master_tty, F_GETFD, 0)) == 0)
|
|
|
|
fcntl (master_tty, F_SETFD, flags | FD_CLOEXEC);
|
|
|
|
|
|
|
|
if (slave_tty >=0 &&
|
|
|
|
(flags = fcntl (slave_tty, F_GETFD, 0)) == 0)
|
|
|
|
fcntl (slave_tty, F_SETFD, flags | FD_CLOEXEC);
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
|
2008-02-20 03:15:01 +05:30
|
|
|
service_pid = fork();
|
2007-04-21 00:28:42 +05:30
|
|
|
if (service_pid == -1)
|
2008-02-20 03:15:01 +05:30
|
|
|
eerrorx ("%s: fork: %s", service, strerror (errno));
|
2007-04-21 00:28:42 +05:30
|
|
|
if (service_pid == 0) {
|
2007-09-21 14:19:43 +05:30
|
|
|
if (slave_tty >= 0) {
|
|
|
|
dup2 (slave_tty, 1);
|
|
|
|
dup2 (slave_tty, 2);
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
if (exists (RC_SVCDIR "/runscript.sh")) {
|
2007-11-22 17:39:46 +05:30
|
|
|
execl (RC_SVCDIR "/runscript.sh", RC_SVCDIR "/runscript.sh",
|
2008-01-11 21:21:40 +05:30
|
|
|
service, arg1, arg2, (char *) NULL);
|
2007-08-09 20:03:20 +05:30
|
|
|
eerror ("%s: exec `" RC_SVCDIR "/runscript.sh': %s",
|
2008-01-11 21:21:40 +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-11-22 17:39:46 +05:30
|
|
|
execl (RC_LIBDIR "/sh/runscript.sh", RC_LIBDIR "/sh/runscript.sh",
|
2008-01-11 21:21:40 +05:30
|
|
|
service, arg1, arg2, (char *) NULL);
|
2007-08-09 20:03:20 +05:30
|
|
|
eerror ("%s: exec `" RC_LIBDIR "/sh/runscript.sh': %s",
|
2008-01-11 21:21:40 +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-09-21 14:19:43 +05:30
|
|
|
selfd = MAX (master_tty, signal_pipe[0]) + 1;
|
2008-01-07 17:59:30 +05:30
|
|
|
buffer = xmalloc (sizeof (char) * BUFSIZ);
|
2008-01-21 21:40:38 +05:30
|
|
|
for (;;) {
|
2007-09-21 14:19:43 +05:30
|
|
|
FD_ZERO (&rset);
|
|
|
|
FD_SET (signal_pipe[0], &rset);
|
|
|
|
if (master_tty >= 0)
|
|
|
|
FD_SET (master_tty, &rset);
|
|
|
|
|
|
|
|
if ((s = select (selfd, &rset, NULL, NULL, NULL)) == -1) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
eerror ("%s: select: %s", service, strerror (errno));
|
|
|
|
break;
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
}
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-09-21 14:19:43 +05:30
|
|
|
if (s > 0) {
|
|
|
|
if (master_tty >= 0 && FD_ISSET (master_tty, &rset)) {
|
2008-01-07 17:59:30 +05:30
|
|
|
bytes = read (master_tty, buffer, BUFSIZ);
|
2007-09-21 14:19:43 +05:30
|
|
|
write_prefix (buffer, bytes, &prefixed);
|
|
|
|
}
|
2007-10-23 01:03:42 +05:30
|
|
|
|
|
|
|
/* Only SIGCHLD signals come down this pipe */
|
|
|
|
if (FD_ISSET (signal_pipe[0], &rset))
|
|
|
|
break;
|
2007-09-21 14:19:43 +05:30
|
|
|
}
|
|
|
|
}
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-10-12 05:31:33 +05:30
|
|
|
free (buffer);
|
2007-09-21 14:19:43 +05:30
|
|
|
close (signal_pipe[0]);
|
|
|
|
close (signal_pipe[1]);
|
|
|
|
signal_pipe[0] = signal_pipe[1] = -1;
|
|
|
|
|
|
|
|
if (master_tty >= 0) {
|
2008-02-01 18:50:19 +05:30
|
|
|
/* Why did we do this? */
|
|
|
|
/* signal (SIGWINCH, SIG_IGN); */
|
2007-09-21 14:19:43 +05:30
|
|
|
close (master_tty);
|
|
|
|
master_tty = -1;
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
|
2007-10-23 01:03:42 +05:30
|
|
|
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
|
|
|
|
|
|
|
return (execok);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-12-05 23:18:07 +05:30
|
|
|
static bool svc_wait (rc_depinfo_t *depinfo, const char *svc)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
char *fifo;
|
|
|
|
struct timespec ts;
|
|
|
|
int nloops = WAIT_MAX * (ONE_SECOND / WAIT_INTERVAL);
|
|
|
|
bool retval = false;
|
|
|
|
bool forever = false;
|
|
|
|
char **keywords = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (! service)
|
|
|
|
return (false);
|
|
|
|
|
2008-01-31 23:31:20 +05:30
|
|
|
/* Some services don't have a timeout, like fsck */
|
2008-02-19 20:06:22 +05:30
|
|
|
keywords = rc_deptree_depend (depinfo, svc, "keyword");
|
2007-12-05 23:18:07 +05:30
|
|
|
STRLIST_FOREACH (keywords, s, i) {
|
|
|
|
if (strcmp (s, "notimeout") == 0) {
|
|
|
|
forever = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc_strlist_free (keywords);
|
|
|
|
|
2007-12-19 19:23:52 +05:30
|
|
|
fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename_c (svc), (char *) NULL);
|
2007-12-05 23:18:07 +05:30
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = WAIT_INTERVAL;
|
|
|
|
|
|
|
|
while (nloops) {
|
|
|
|
if (! exists (fifo)) {
|
|
|
|
retval = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nanosleep (&ts, NULL) == -1) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! forever)
|
|
|
|
nloops --;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! exists (fifo))
|
|
|
|
retval = true;
|
|
|
|
free (fifo);
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2007-09-28 20:23:38 +05:30
|
|
|
rc_service_state_t state = rc_service_state (service);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-28 20:23:38 +05:30
|
|
|
if (state & RC_SERVICE_STOPPING) {
|
2007-04-11 18:14:47 +05:30
|
|
|
snprintf (status, sizeof (status), "stopping");
|
|
|
|
e = &ewarn;
|
2007-11-01 04:04:26 +05:30
|
|
|
} else if (state & RC_SERVICE_STARTING) {
|
2007-04-11 18:14:47 +05:30
|
|
|
snprintf (status, sizeof (status), "starting");
|
|
|
|
e = &ewarn;
|
2007-09-28 20:23:38 +05:30
|
|
|
} else if (state & RC_SERVICE_INACTIVE) {
|
2007-04-11 18:14:47 +05:30
|
|
|
snprintf (status, sizeof (status), "inactive");
|
|
|
|
e = &ewarn;
|
2007-09-28 20:23:38 +05:30
|
|
|
} else if (state & RC_SERVICE_STARTED) {
|
2007-09-28 20:34:15 +05:30
|
|
|
if (geteuid () == 0 && rc_service_daemons_crashed (service)) {
|
|
|
|
snprintf (status, sizeof (status), "crashed");
|
|
|
|
e = &eerror;
|
|
|
|
} else
|
|
|
|
snprintf (status, sizeof (status), "started");
|
2007-04-11 18:14:47 +05:30
|
|
|
} else
|
|
|
|
snprintf (status, sizeof (status), "stopped");
|
|
|
|
|
|
|
|
e ("status: %s", status);
|
2007-09-28 20:23:38 +05:30
|
|
|
return (state);
|
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;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t l;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* 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 &&
|
2008-01-11 21:21:40 +05:30
|
|
|
(errno != EACCES || geteuid () == 0))
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: unable to create fifo `%s': %s",
|
2008-01-11 21:21:40 +05:30
|
|
|
applet, exclusive, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
path = rc_strcatpaths (RC_SVCDIR, "exclusive", applet, (char *) NULL);
|
2008-01-22 16:37:39 +05:30
|
|
|
l = strlen (path) + 16;
|
|
|
|
mtime_test = xmalloc (sizeof (char) * l);
|
|
|
|
snprintf (mtime_test, l, "%s.%d", path, getpid ());
|
2007-04-11 18:14:47 +05:30
|
|
|
free (path);
|
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
if (exists (mtime_test) && unlink (mtime_test) != 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
eerror ("%s: unlink `%s': %s",
|
2008-01-11 21:21:40 +05:30
|
|
|
applet, mtime_test, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
free (mtime_test);
|
|
|
|
mtime_test = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symlink (service, mtime_test) != 0) {
|
|
|
|
eerror ("%s: symlink `%s' to `%s': %s",
|
2008-01-11 21:21:40 +05:30
|
|
|
applet, service, mtime_test, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
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-11 18:14:47 +05:30
|
|
|
rc_strlist_free (tmplist);
|
2007-09-28 17:59:23 +05:30
|
|
|
tmplist = rc_services_in_state (RC_SERVICE_INACTIVE);
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_strlist_free (restart_services);
|
2007-09-28 17:59:23 +05:30
|
|
|
restart_services = rc_services_in_state (RC_SERVICE_STARTED);
|
2007-10-26 17:52:26 +05:30
|
|
|
rc_strlist_join (&restart_services, tmplist);
|
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;
|
2007-09-28 20:23:38 +05:30
|
|
|
rc_service_state_t state;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-28 20:23:38 +05:30
|
|
|
state = rc_service_state (service);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-11-23 17:34:11 +05:30
|
|
|
if (rc_yesno (getenv ("IN_HOTPLUG")) || in_background) {
|
2007-09-28 20:23:38 +05:30
|
|
|
if (! state & RC_SERVICE_INACTIVE &&
|
2008-01-11 21:21:40 +05:30
|
|
|
! state & RC_SERVICE_STOPPED)
|
2007-04-11 18:14:47 +05:30
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
background = true;
|
|
|
|
}
|
|
|
|
|
2007-11-16 17:25:08 +05:30
|
|
|
if (state & RC_SERVICE_STARTED) {
|
|
|
|
ewarn ("WARNING: %s has already been started", applet);
|
|
|
|
return;
|
|
|
|
} else if (state & RC_SERVICE_STARTING)
|
2007-04-11 18:14:47 +05:30
|
|
|
ewarnx ("WARNING: %s is already starting", applet);
|
2007-09-28 20:23:38 +05:30
|
|
|
else if (state & RC_SERVICE_STOPPING)
|
2007-04-11 18:14:47 +05:30
|
|
|
ewarnx ("WARNING: %s is stopping", applet);
|
2007-09-28 20:23:38 +05:30
|
|
|
else if (state & RC_SERVICE_INACTIVE && ! background)
|
2007-04-11 18:14:47 +05:30
|
|
|
ewarnx ("WARNING: %s has already started, but is inactive", applet);
|
|
|
|
|
2007-11-01 04:04:26 +05:30
|
|
|
if (! rc_service_mark (service, RC_SERVICE_STARTING))
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("ERROR: %s has been started by something else", applet);
|
|
|
|
|
|
|
|
make_exclusive (service);
|
|
|
|
|
2007-11-19 19:16:09 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_START_OUT;
|
|
|
|
rc_plugin_run (RC_HOOK_SERVICE_START_IN, applet);
|
|
|
|
|
2007-11-23 17:34:11 +05:30
|
|
|
if (rc_conf_yesno ("rc_depend_strict"))
|
2007-07-21 18:19:51 +05:30
|
|
|
depoptions |= RC_DEP_STRICT;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (deps) {
|
2007-12-14 17:53:13 +05:30
|
|
|
if (! deptree && ((deptree = _rc_deptree_load (NULL)) == NULL))
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("failed to load deptree");
|
|
|
|
|
|
|
|
rc_strlist_free (services);
|
2008-01-18 16:57:49 +05:30
|
|
|
services = rc_deptree_depends (deptree, types_b,
|
|
|
|
(const char * const *) applet_list,
|
|
|
|
softlevel, 0);
|
2007-04-11 18:14:47 +05:30
|
|
|
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 (need_services);
|
2008-01-18 16:57:49 +05:30
|
|
|
need_services = rc_deptree_depends (deptree, types_n,
|
|
|
|
(const char * const *) applet_list,
|
2008-01-11 21:21:40 +05:30
|
|
|
softlevel, depoptions);
|
2007-07-03 14:08:27 +05:30
|
|
|
|
|
|
|
rc_strlist_free (use_services);
|
2008-01-18 16:57:49 +05:30
|
|
|
use_services = rc_deptree_depends (deptree, types_nu,
|
|
|
|
(const char * const *) applet_list,
|
2008-01-11 21:21:40 +05:30
|
|
|
softlevel, depoptions);
|
2007-07-03 14:08:27 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! rc_runlevel_starting ()) {
|
2007-07-03 14:08:27 +05:30
|
|
|
STRLIST_FOREACH (use_services, svc, i)
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (svc) & RC_SERVICE_STOPPED) {
|
2007-09-29 22:24:58 +05:30
|
|
|
pid_t pid = rc_service_start (svc);
|
2007-11-23 17:34:11 +05:30
|
|
|
if (! rc_conf_yesno ("rc_parallel"))
|
2007-10-23 01:03:42 +05:30
|
|
|
rc_waitpid (pid);
|
2007-04-21 00:28:42 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Now wait for them to start */
|
2008-01-18 16:57:49 +05:30
|
|
|
services = rc_deptree_depends (deptree, types_nua,
|
|
|
|
(const char * const *) applet_list,
|
2008-01-11 21:21:40 +05:30
|
|
|
softlevel, depoptions);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* We use tmplist to hold our scheduled by list */
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = NULL;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (services, svc, i) {
|
2007-09-28 20:23:38 +05:30
|
|
|
rc_service_state_t svcs = rc_service_state (svc);
|
|
|
|
if (svcs & RC_SERVICE_STARTED)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
2007-07-03 14:08:27 +05:30
|
|
|
|
|
|
|
/* Don't wait for services which went inactive but are now in
|
|
|
|
* starting state which we are after */
|
2007-11-01 04:04:26 +05:30
|
|
|
if (svcs & RC_SERVICE_STARTING &&
|
2008-01-11 21:21:40 +05:30
|
|
|
svcs & RC_SERVICE_WASINACTIVE) {
|
2007-07-03 14:08:27 +05:30
|
|
|
bool use = false;
|
|
|
|
STRLIST_FOREACH (use_services, svc2, j)
|
|
|
|
if (strcmp (svc, svc2) == 0) {
|
|
|
|
use = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (! use)
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-12-05 23:18:07 +05:30
|
|
|
if (! svc_wait (deptree, svc))
|
2007-04-11 18:14:47 +05:30
|
|
|
eerror ("%s: timed out waiting for %s", applet, svc);
|
2007-09-28 20:23:38 +05:30
|
|
|
if ((svcs = rc_service_state (svc)) & RC_SERVICE_STARTED)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (need_services, svc2, j)
|
|
|
|
if (strcmp (svc, svc2) == 0) {
|
2007-09-28 20:23:38 +05:30
|
|
|
if (svcs & RC_SERVICE_INACTIVE ||
|
2008-01-11 21:21:40 +05:30
|
|
|
svcs & RC_SERVICE_WASINACTIVE)
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&tmplist, svc);
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
|
|
|
eerrorx ("ERROR: cannot start %s as %s would not start",
|
2008-01-11 21:21:40 +05:30
|
|
|
applet, svc);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmplist) {
|
|
|
|
int n = 0;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t len = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* Set the state now, then unlink our exclusive so that
|
|
|
|
our scheduled list is preserved */
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (service, RC_SERVICE_STOPPED);
|
2007-04-11 18:14:47 +05:30
|
|
|
unlink_mtime_test ();
|
|
|
|
|
|
|
|
STRLIST_FOREACH (tmplist, svc, i) {
|
2007-10-03 19:48:52 +05:30
|
|
|
rc_service_schedule_start (svc, service);
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_strlist_free (providelist);
|
2008-01-14 10:35:22 +05:30
|
|
|
providelist = rc_deptree_depend (deptree, "iprovide", svc);
|
|
|
|
STRLIST_FOREACH (providelist, svc2, j)
|
2007-10-03 19:48:52 +05:30
|
|
|
rc_service_schedule_start (svc2, service);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
len += strlen (svc) + 2;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
len += 5;
|
2007-10-12 04:47:53 +05:30
|
|
|
tmp = xmalloc (sizeof (char) * len);
|
2007-04-11 18:14:47 +05:30
|
|
|
p = tmp;
|
|
|
|
STRLIST_FOREACH (tmplist, svc, i) {
|
|
|
|
if (i > 1) {
|
2007-07-11 19:58:54 +05:30
|
|
|
if (i == n)
|
2007-04-11 18:14:47 +05:30
|
|
|
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",
|
2008-01-11 21:21:40 +05:30
|
|
|
applet, tmp);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ibsave)
|
|
|
|
setenv ("IN_BACKGROUND", ibsave, 1);
|
2007-10-10 02:50:10 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_START_DONE;
|
2007-09-28 17:59:23 +05:30
|
|
|
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 ()) {
|
2007-10-10 02:50:10 +05:30
|
|
|
if (! started)
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("ERROR: %s failed to start", applet);
|
|
|
|
} else {
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (service) & RC_SERVICE_INACTIVE)
|
2007-04-11 18:14:47 +05:30
|
|
|
ewarnx ("WARNING: %s has started, but is inactive", applet);
|
|
|
|
else
|
|
|
|
ewarnx ("WARNING: %s not under our control, aborting", applet);
|
|
|
|
}
|
|
|
|
|
2007-10-10 02:50:10 +05:30
|
|
|
rc_service_mark (service, RC_SERVICE_STARTED);
|
|
|
|
unlink_mtime_test ();
|
|
|
|
hook_out = RC_HOOK_SERVICE_START_OUT;
|
|
|
|
rc_plugin_run (RC_HOOK_SERVICE_START_DONE, applet);
|
|
|
|
|
|
|
|
if (exclusive)
|
|
|
|
unlink (exclusive);
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Now start any scheduled services */
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = rc_services_scheduled (service);
|
|
|
|
STRLIST_FOREACH (services, svc, i)
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (svc) & RC_SERVICE_STOPPED)
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_start (svc);
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_strlist_free (services);
|
|
|
|
services = NULL;
|
|
|
|
|
|
|
|
/* Do the same for any services we provide */
|
|
|
|
rc_strlist_free (tmplist);
|
2007-12-05 23:18:07 +05:30
|
|
|
tmplist = rc_deptree_depend (deptree, "iprovide", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
STRLIST_FOREACH (tmplist, svc2, j) {
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = rc_services_scheduled (svc2);
|
|
|
|
STRLIST_FOREACH (services, svc, i)
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (svc) & RC_SERVICE_STOPPED)
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_start (svc);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
hook_out = 0;
|
2007-09-28 17:59:23 +05:30
|
|
|
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;
|
2007-09-28 20:23:38 +05:30
|
|
|
rc_service_state_t state = rc_service_state (service);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_runlevel_stopping () &&
|
2008-01-11 21:21:40 +05:30
|
|
|
state & RC_SERVICE_FAILED)
|
2007-04-11 18:14:47 +05:30
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
2007-11-23 17:34:11 +05:30
|
|
|
if (rc_yesno (getenv ("IN_HOTPLUG")) || in_background)
|
2008-01-14 10:35:22 +05:30
|
|
|
if (! (state & RC_SERVICE_STARTED) &&
|
2008-01-11 21:21:40 +05:30
|
|
|
! (state & RC_SERVICE_INACTIVE))
|
2007-04-11 18:14:47 +05:30
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
2007-11-16 17:25:08 +05:30
|
|
|
if (state & RC_SERVICE_STOPPED) {
|
|
|
|
ewarn ("WARNING: %s is already stopped", applet);
|
|
|
|
return;
|
|
|
|
} else if (state & RC_SERVICE_STOPPING)
|
2007-04-11 18:14:47 +05:30
|
|
|
ewarnx ("WARNING: %s is already stopping", applet);
|
|
|
|
|
2007-09-29 22:24:58 +05:30
|
|
|
if (! rc_service_mark (service, RC_SERVICE_STOPPING))
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("ERROR: %s has been stopped by something else", applet);
|
|
|
|
|
|
|
|
make_exclusive (service);
|
|
|
|
|
2007-11-19 19:16:09 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_STOP_OUT;
|
|
|
|
rc_plugin_run (RC_HOOK_SERVICE_STOP_IN, applet);
|
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! rc_runlevel_stopping () &&
|
2008-01-11 21:21:40 +05:30
|
|
|
rc_service_in_runlevel (service, RC_LEVEL_BOOT))
|
2007-04-11 18:14:47 +05:30
|
|
|
ewarn ("WARNING: you are stopping a boot service");
|
|
|
|
|
2007-09-28 20:23:38 +05:30
|
|
|
if (deps && ! (state & RC_SERVICE_WASINACTIVE)) {
|
2007-04-11 18:14:47 +05:30
|
|
|
int depoptions = RC_DEP_TRACE;
|
|
|
|
char *svc;
|
|
|
|
int i;
|
|
|
|
|
2007-11-28 22:32:02 +05:30
|
|
|
if (rc_conf_yesno ("rc_depend_strict"))
|
2007-04-11 18:14:47 +05:30
|
|
|
depoptions |= RC_DEP_STRICT;
|
|
|
|
|
2007-12-14 17:53:13 +05:30
|
|
|
if (! deptree && ((deptree = _rc_deptree_load (NULL)) == NULL))
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("failed to load deptree");
|
|
|
|
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = NULL;
|
|
|
|
rc_strlist_free (services);
|
2008-01-18 16:57:49 +05:30
|
|
|
services = rc_deptree_depends (deptree, types_m,
|
|
|
|
(const char * const *) applet_list,
|
2008-01-11 21:21:40 +05:30
|
|
|
softlevel, depoptions);
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_strlist_reverse (services);
|
|
|
|
STRLIST_FOREACH (services, svc, i) {
|
2007-09-28 20:23:38 +05:30
|
|
|
rc_service_state_t svcs = rc_service_state (svc);
|
2008-01-14 10:35:22 +05:30
|
|
|
if (svcs & RC_SERVICE_STARTED ||
|
2008-01-11 21:21:40 +05:30
|
|
|
svcs & RC_SERVICE_INACTIVE)
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
2007-12-05 23:18:07 +05:30
|
|
|
svc_wait (deptree, svc);
|
2007-09-28 20:23:38 +05:30
|
|
|
svcs = rc_service_state (svc);
|
2008-01-14 10:35:22 +05:30
|
|
|
if (svcs & RC_SERVICE_STARTED ||
|
2008-01-11 21:21:40 +05:30
|
|
|
svcs & RC_SERVICE_INACTIVE)
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
2007-09-29 22:24:58 +05:30
|
|
|
pid_t pid = rc_service_stop (svc);
|
2007-11-23 17:34:11 +05:30
|
|
|
if (! rc_conf_yesno ("rc_parallel"))
|
2007-10-23 01:03:42 +05:30
|
|
|
rc_waitpid (pid);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&tmplist, svc);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = NULL;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (tmplist, svc, i) {
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (svc) & RC_SERVICE_STOPPED)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We used to loop 3 times here - maybe re-do this if needed */
|
2007-12-05 23:18:07 +05:30
|
|
|
svc_wait (deptree, svc);
|
2007-09-28 20:23:38 +05:30
|
|
|
if (! (rc_service_state (svc) & RC_SERVICE_STOPPED)) {
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_runlevel_stopping ()) {
|
2007-08-17 17:01:36 +05:30
|
|
|
/* If shutting down, we should stop even if a dependant failed */
|
|
|
|
if (softlevel &&
|
2008-01-11 21:21:40 +05:30
|
|
|
(strcmp (softlevel, RC_LEVEL_SHUTDOWN) == 0 ||
|
|
|
|
strcmp (softlevel, RC_LEVEL_REBOOT) == 0 ||
|
|
|
|
strcmp (softlevel, RC_LEVEL_SINGLE) == 0))
|
2007-08-17 17:01:36 +05:30
|
|
|
continue;
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (service, RC_SERVICE_FAILED);
|
2007-08-17 17:01:36 +05:30
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("ERROR: cannot stop %s as %s is still up",
|
2008-01-11 21:21:40 +05:30
|
|
|
applet, svc);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
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 */
|
2008-01-18 16:57:49 +05:30
|
|
|
services = rc_deptree_depends (deptree, types_mua,
|
|
|
|
(const char * const *) applet_list,
|
2008-01-11 21:21:40 +05:30
|
|
|
softlevel, depoptions);
|
2007-04-11 18:14:47 +05:30
|
|
|
STRLIST_FOREACH (services, svc, i) {
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (svc) & RC_SERVICE_STOPPED)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
2007-12-05 23:18:07 +05:30
|
|
|
svc_wait (deptree, svc);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
rc_strlist_free (services);
|
|
|
|
services = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ibsave)
|
|
|
|
setenv ("IN_BACKGROUND", ibsave, 1);
|
2007-10-10 02:50:10 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_STOP_DONE;
|
2007-09-28 17:59:23 +05:30
|
|
|
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");
|
|
|
|
|
2007-10-10 11:54:08 +05:30
|
|
|
if (! in_control ())
|
2007-04-11 18:14:47 +05:30
|
|
|
ewarnx ("WARNING: %s not under our control, aborting", applet);
|
|
|
|
|
2007-10-10 02:50:10 +05:30
|
|
|
if (! stopped)
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("ERROR: %s failed to stop", applet);
|
|
|
|
|
|
|
|
if (in_background)
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (service, RC_SERVICE_INACTIVE);
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (service, RC_SERVICE_STOPPED);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
unlink_mtime_test ();
|
2007-10-10 02:50:10 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_STOP_OUT;
|
2007-09-28 17:59:23 +05:30
|
|
|
rc_plugin_run (RC_HOOK_SERVICE_STOP_DONE, applet);
|
2007-10-10 02:50:10 +05:30
|
|
|
if (exclusive)
|
|
|
|
unlink (exclusive);
|
2007-04-11 18:14:47 +05:30
|
|
|
hook_out = 0;
|
2007-09-28 17:59:23 +05:30
|
|
|
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) {
|
2007-09-28 20:23:38 +05:30
|
|
|
rc_service_state_t state = rc_service_state (service);
|
|
|
|
if (state & RC_SERVICE_STARTED || state & 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;
|
|
|
|
}
|
|
|
|
|
2007-09-28 20:23:38 +05:30
|
|
|
if (! (rc_service_state (service) & RC_SERVICE_STOPPED)) {
|
2007-04-11 18:14:47 +05:30
|
|
|
get_started_services ();
|
2007-04-30 18:48:42 +05:30
|
|
|
svc_stop (deps);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
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-08-28 18:36:44 +05:30
|
|
|
#include "_usage.h"
|
2007-09-26 04:47:25 +05:30
|
|
|
#define getoptstring "dDsv" getoptstring_COMMON
|
2007-12-18 23:31:05 +05:30
|
|
|
#define extraopts "stop | start | restart | describe | zap"
|
2008-02-02 01:24:46 +05:30
|
|
|
static const struct option longopts[] = {
|
2007-04-17 18:14:32 +05:30
|
|
|
{ "debug", 0, NULL, 'd'},
|
2007-08-28 18:36:44 +05:30
|
|
|
{ "ifstarted", 0, NULL, 's'},
|
2007-04-17 18:14:32 +05:30
|
|
|
{ "nodeps", 0, NULL, 'D'},
|
2007-08-28 18:36:44 +05:30
|
|
|
longopts_COMMON
|
2007-04-17 18:14:32 +05:30
|
|
|
};
|
2007-09-25 21:51:38 +05:30
|
|
|
static const char * const longopts_help[] = {
|
2007-12-18 23:31:05 +05:30
|
|
|
"set xtrace when running the script",
|
|
|
|
"only run commands when started",
|
|
|
|
"ignore dependencies",
|
2007-09-25 21:51:38 +05:30
|
|
|
longopts_help_COMMON
|
|
|
|
};
|
2007-08-28 18:36:44 +05:30
|
|
|
#include "_usage.c"
|
2007-04-17 18:14:32 +05:30
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
int runscript (int argc, char **argv)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t i;
|
2007-04-11 18:14:47 +05:30
|
|
|
bool deps = true;
|
|
|
|
bool doneone = false;
|
|
|
|
char pid[16];
|
|
|
|
int retval;
|
2007-05-14 17:54:18 +05:30
|
|
|
int opt;
|
2007-07-10 20:56:49 +05:30
|
|
|
char *svc;
|
2007-04-12 15:38:42 +05:30
|
|
|
|
2007-11-14 21:49:56 +05:30
|
|
|
/* Show help if insufficient args */
|
2007-12-18 23:31:05 +05:30
|
|
|
if (argc < 2 || ! exists (argv[1])) {
|
|
|
|
fprintf (stderr, "runscript is not meant to be to run directly\n");
|
|
|
|
exit (EXIT_FAILURE);
|
2007-11-14 21:49:56 +05:30
|
|
|
}
|
2007-07-04 20:26:59 +05:30
|
|
|
|
2007-12-19 19:23:52 +05:30
|
|
|
applet = basename_c (argv[1]);
|
2007-12-18 23:31:05 +05:30
|
|
|
if (argc < 3)
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
2007-11-21 21:08:07 +05:30
|
|
|
if (*argv[1] == '/')
|
|
|
|
service = xstrdup (argv[1]);
|
|
|
|
else {
|
|
|
|
char d[PATH_MAX];
|
|
|
|
getcwd (d, sizeof (d));
|
|
|
|
i = strlen (d) + strlen (argv[1]) + 2;
|
|
|
|
service = xmalloc (sizeof (char) * i);
|
|
|
|
snprintf (service, i, "%s/%s", d, argv[1]);
|
|
|
|
}
|
|
|
|
|
2007-05-15 21:26:35 +05:30
|
|
|
atexit (cleanup);
|
2007-06-26 14:17:46 +05:30
|
|
|
|
|
|
|
/* Change dir to / to ensure all init scripts don't use stuff in pwd */
|
|
|
|
chdir ("/");
|
2007-11-01 04:04:26 +05:30
|
|
|
|
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 */
|
2007-10-08 16:41:21 +05:30
|
|
|
if (exists ("/dev/.rcsysinit")) {
|
2007-04-11 18:14:47 +05:30
|
|
|
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-10-08 16:46:22 +05:30
|
|
|
if ((softlevel = xstrdup (getenv ("RC_SOFTLEVEL"))) == NULL) {
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Ensure our environment is pure
|
|
|
|
Also, add our configuration to it */
|
2008-01-06 02:13:32 +05:30
|
|
|
char *p;
|
2007-10-08 16:37:39 +05:30
|
|
|
env = env_filter ();
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (env) {
|
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]) {
|
2007-10-08 16:46:22 +05:30
|
|
|
tmp = xstrdup (environ[0]);
|
2007-04-11 18:14:47 +05:30
|
|
|
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
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-01-06 02:13:32 +05:30
|
|
|
tmplist = env_config ();
|
|
|
|
rc_strlist_join (&env, tmplist);
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = NULL;
|
|
|
|
STRLIST_FOREACH (env, p, i)
|
|
|
|
putenv (p);
|
|
|
|
/* We don't free our list as that would be null in environ */
|
|
|
|
|
2007-10-02 15:27:23 +05:30
|
|
|
softlevel = rc_runlevel_get ();
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-12-06 16:18:00 +05:30
|
|
|
setenv ("EINFO_LOG", service, 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
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 */
|
2007-11-23 17:34:11 +05:30
|
|
|
if (rc_conf_yesno ("rc_parallel")) {
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t l = 0;
|
|
|
|
size_t ll;
|
2007-07-06 21:20:40 +05:30
|
|
|
|
|
|
|
/* Get the longest service name */
|
|
|
|
services = rc_services_in_runlevel (NULL);
|
|
|
|
STRLIST_FOREACH (services, svc, i) {
|
2007-07-06 22:37:29 +05:30
|
|
|
ll = strlen (svc);
|
2007-07-06 21:20:40 +05:30
|
|
|
if (ll > l)
|
|
|
|
l = ll;
|
|
|
|
}
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-07-06 22:37:29 +05:30
|
|
|
/* Make our prefix string */
|
2008-02-20 03:15:01 +05:30
|
|
|
prefix = xmalloc (sizeof (char) * l + 1);
|
2007-07-06 22:37:29 +05:30
|
|
|
ll = strlen (applet);
|
|
|
|
memcpy (prefix, applet, ll);
|
|
|
|
memset (prefix + ll, ' ', l - ll);
|
|
|
|
memset (prefix + l, 0, 1);
|
2007-04-25 18:00:24 +05:30
|
|
|
eprefix (prefix);
|
|
|
|
}
|
|
|
|
|
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,
|
2008-01-11 21:21:40 +05:30
|
|
|
longopts, (int *) 0)) != -1)
|
2007-05-14 17:54:18 +05:30
|
|
|
switch (opt) {
|
2007-04-12 15:38:42 +05:30
|
|
|
case 'd':
|
|
|
|
setenv ("RC_DEBUG", "yes", 1);
|
|
|
|
break;
|
2007-08-28 18:36:44 +05:30
|
|
|
case 's':
|
2007-09-28 20:23:38 +05:30
|
|
|
if (! (rc_service_state (service) & RC_SERVICE_STARTED))
|
2007-08-28 18:36:44 +05:30
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
break;
|
2007-04-12 15:38:42 +05:30
|
|
|
case 'D':
|
|
|
|
deps = false;
|
|
|
|
break;
|
2007-11-01 04:04:26 +05:30
|
|
|
case_RC_COMMON_GETOPT
|
2007-04-12 15:38:42 +05:30
|
|
|
}
|
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")) {
|
2007-10-08 16:46:22 +05:30
|
|
|
ibsave = xstrdup (getenv ("IN_BACKGROUND"));
|
2007-11-23 17:34:11 +05:30
|
|
|
in_background = rc_yesno (ibsave);
|
2007-04-12 16:10:51 +05:30
|
|
|
unsetenv ("IN_BACKGROUND");
|
|
|
|
}
|
|
|
|
|
2007-11-23 17:34:11 +05:30
|
|
|
if (rc_yesno (getenv ("IN_HOTPLUG"))) {
|
2008-01-13 23:17:23 +05:30
|
|
|
if (! rc_conf_yesno ("rc_hotplug") || ! service_plugable (applet))
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: not allowed to be hotplugged", applet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup a signal handler */
|
2008-02-01 18:50:19 +05:30
|
|
|
signal_setup (SIGHUP, handle_signal);
|
|
|
|
signal_setup (SIGINT, handle_signal);
|
|
|
|
signal_setup (SIGQUIT, handle_signal);
|
|
|
|
signal_setup (SIGTERM, handle_signal);
|
|
|
|
signal_setup (SIGCHLD, handle_signal);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2007-07-03 01:27:18 +05:30
|
|
|
if (strcmp (optarg, "status") != 0 &&
|
2008-01-11 21:21:40 +05:30
|
|
|
strcmp (optarg, "help") != 0) {
|
2007-07-03 01:27:18 +05:30
|
|
|
/* Only root should be able to run us */
|
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* 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;
|
2008-01-18 16:57:49 +05:30
|
|
|
rc_strlist_add (&applet_list, applet);
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-12-20 23:09:13 +05:30
|
|
|
if (strcmp (optarg, "describe") == 0 ||
|
2008-01-11 21:21:40 +05:30
|
|
|
strcmp (optarg, "help") == 0)
|
2007-12-20 23:09:13 +05:30
|
|
|
{
|
2007-12-18 23:31:05 +05:30
|
|
|
char *save = prefix;
|
|
|
|
|
|
|
|
eprefix (NULL);
|
|
|
|
prefix = NULL;
|
2007-07-11 01:41:42 +05:30
|
|
|
svc_exec (optarg, NULL);
|
2007-12-18 23:31:05 +05:30
|
|
|
eprefix (save);
|
2007-07-10 20:56:49 +05:30
|
|
|
} else if (strcmp (optarg, "ineed") == 0 ||
|
2008-01-11 21:21:40 +05:30
|
|
|
strcmp (optarg, "iuse") == 0 ||
|
|
|
|
strcmp (optarg, "needsme") == 0 ||
|
|
|
|
strcmp (optarg, "usesme") == 0 ||
|
|
|
|
strcmp (optarg, "iafter") == 0 ||
|
|
|
|
strcmp (optarg, "ibefore") == 0 ||
|
2008-01-18 16:57:49 +05:30
|
|
|
strcmp (optarg, "iprovide") == 0)
|
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
int depoptions = RC_DEP_TRACE;
|
|
|
|
|
2007-11-23 17:34:11 +05:30
|
|
|
if (rc_conf_yesno ("rc_depend_strict"))
|
2007-07-21 18:19:51 +05:30
|
|
|
depoptions |= RC_DEP_STRICT;
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-12-14 17:53:13 +05:30
|
|
|
if (! deptree && ((deptree = _rc_deptree_load (NULL)) == NULL))
|
2007-07-10 20:56:49 +05:30
|
|
|
eerrorx ("failed to load deptree");
|
|
|
|
|
|
|
|
rc_strlist_free (services);
|
2008-01-18 16:57:49 +05:30
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
rc_strlist_add (&tmplist, optarg);
|
|
|
|
services = rc_deptree_depends (deptree,
|
|
|
|
(const char * const *) tmplist,
|
|
|
|
(const char * const *) applet_list,
|
|
|
|
softlevel, depoptions);
|
2007-07-10 20:56:49 +05:30
|
|
|
STRLIST_FOREACH (services, svc, i)
|
|
|
|
printf ("%s%s", i == 1 ? "" : " ", svc);
|
2007-08-17 18:53:43 +05:30
|
|
|
if (services)
|
|
|
|
printf ("\n");
|
2007-07-11 00:39:41 +05:30
|
|
|
} else if (strcmp (optarg, "status") == 0) {
|
|
|
|
rc_service_state_t r = svc_status (service);
|
|
|
|
retval = (int) r;
|
2007-10-09 21:17:25 +05:30
|
|
|
if (retval & RC_SERVICE_STARTED)
|
2007-10-09 21:14:22 +05:30
|
|
|
retval = 0;
|
2007-07-11 00:39:41 +05:30
|
|
|
} else {
|
2008-03-03 02:43:21 +05:30
|
|
|
#ifndef PREFIX
|
2007-07-11 00:39:41 +05:30
|
|
|
if (geteuid () != 0)
|
|
|
|
eerrorx ("%s: root access required", applet);
|
2008-03-03 02:43:21 +05:30
|
|
|
#endif
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-11 00:39:41 +05:30
|
|
|
if (strcmp (optarg, "conditionalrestart") == 0 ||
|
2008-01-11 21:21:40 +05:30
|
|
|
strcmp (optarg, "condrestart") == 0)
|
2007-07-11 00:39:41 +05:30
|
|
|
{
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (service) & RC_SERVICE_STARTED)
|
2007-07-11 00:39:41 +05:30
|
|
|
svc_restart (deps);
|
|
|
|
} else if (strcmp (optarg, "restart") == 0) {
|
|
|
|
svc_restart (deps);
|
|
|
|
} else if (strcmp (optarg, "start") == 0) {
|
|
|
|
svc_start (deps);
|
|
|
|
} else if (strcmp (optarg, "stop") == 0) {
|
2007-08-28 18:36:44 +05:30
|
|
|
if (deps && in_background)
|
2007-07-11 00:39:41 +05:30
|
|
|
get_started_services ();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-11 00:39:41 +05:30
|
|
|
svc_stop (deps);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-08-28 18:36:44 +05:30
|
|
|
if (deps) {
|
|
|
|
if (! in_background &&
|
2008-01-11 21:21:40 +05:30
|
|
|
! rc_runlevel_stopping () &&
|
|
|
|
rc_service_state (service) & RC_SERVICE_STOPPED)
|
2007-08-28 18:36:44 +05:30
|
|
|
uncoldplug ();
|
|
|
|
|
|
|
|
if (in_background &&
|
2008-01-11 21:21:40 +05:30
|
|
|
rc_service_state (service) & RC_SERVICE_INACTIVE)
|
2007-08-28 18:36:44 +05:30
|
|
|
{
|
|
|
|
int j;
|
|
|
|
STRLIST_FOREACH (restart_services, svc, j)
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (svc) & RC_SERVICE_STOPPED)
|
2007-10-03 19:48:52 +05:30
|
|
|
rc_service_schedule_start (service, svc);
|
2007-08-28 18:36:44 +05:30
|
|
|
}
|
2007-07-11 00:39:41 +05:30
|
|
|
}
|
|
|
|
} else if (strcmp (optarg, "zap") == 0) {
|
|
|
|
einfo ("Manually resetting %s to stopped state", applet);
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (applet, RC_SERVICE_STOPPED);
|
2007-07-11 00:39:41 +05:30
|
|
|
uncoldplug ();
|
2007-07-11 01:41:42 +05:30
|
|
|
} else
|
2007-07-11 00:39:41 +05:30
|
|
|
svc_exec (optarg, NULL);
|
|
|
|
|
|
|
|
/* We should ensure this list is empty after an action is done */
|
|
|
|
rc_strlist_free (restart_services);
|
|
|
|
restart_services = NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-20 23:09:13 +05:30
|
|
|
if (! doneone)
|
|
|
|
usage (EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return (retval);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|