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
|
|
|
/*
|
2009-01-13 05:23:13 +05:30
|
|
|
* Copyright 2007-2009 Roy Marples <roy@marples.name>
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2007-09-21 14:19:43 +05:30
|
|
|
#include <sys/ioctl.h>
|
2008-09-28 00:20:21 +05:30
|
|
|
#include <sys/file.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>
|
2009-04-18 06:27:17 +05:30
|
|
|
#include <sys/wait.h>
|
2008-03-16 22:30:56 +05:30
|
|
|
|
2008-11-28 02:35:39 +05:30
|
|
|
#include <ctype.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
2007-04-25 18:00:24 +05:30
|
|
|
#include <fcntl.h>
|
2008-10-10 14:38:59 +05:30
|
|
|
#include <fnmatch.h>
|
2007-04-12 15:38:42 +05:30
|
|
|
#include <getopt.h>
|
2008-04-29 15:55:45 +05:30
|
|
|
#include <libgen.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <limits.h>
|
2008-11-28 02:35:39 +05:30
|
|
|
#include <poll.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__
|
2009-04-24 03:01:22 +05:30
|
|
|
# include <pty.h>
|
2008-01-10 04:52:04 +05:30
|
|
|
#elif defined(__NetBSD__) || defined(__OpenBSD__)
|
2009-04-24 03:01:22 +05:30
|
|
|
# include <util.h>
|
2007-09-21 14:19:43 +05:30
|
|
|
#else
|
2009-04-24 03:01:22 +05:30
|
|
|
# include <libutil.h>
|
2007-09-21 14:19:43 +05:30
|
|
|
#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"
|
|
|
|
|
2007-04-12 18:48:52 +05:30
|
|
|
#define SELINUX_LIB RC_LIBDIR "/runscript_selinux.so"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
#define PREFIX_LOCK RC_SVCDIR "/prefix.lock"
|
2007-04-26 16:54:07 +05:30
|
|
|
|
2009-04-24 01:50:17 +05:30
|
|
|
#define WAIT_INTERVAL 20000000 /* usecs to poll the lock file */
|
|
|
|
#define WAIT_TIMEOUT 60 /* seconds until we timeout */
|
|
|
|
#define WARN_TIMEOUT 10 /* warn about this every N seconds */
|
2007-12-05 23:18:07 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
static const char *applet;
|
|
|
|
static char *service, *runlevel, *ibsave, *prefix;;
|
|
|
|
static RC_DEPTREE *deptree;
|
|
|
|
static RC_STRINGLIST *applet_list, *services, *tmplist;
|
|
|
|
static RC_STRINGLIST *restart_services, *need_services, *use_services;
|
|
|
|
static RC_HOOK hook_out;
|
|
|
|
static int exclusive_fd = -1, master_tty = -1;
|
|
|
|
static bool sighup, in_background, deps, dry_run;
|
|
|
|
static pid_t service_pid;
|
2007-09-21 14:19:43 +05:30
|
|
|
static int signal_pipe[2] = { -1, -1 };
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
static RC_STRINGLIST *types_b, *types_n, *types_nu, *types_nua, *types_m;
|
2008-03-16 22:30:56 +05:30
|
|
|
static RC_STRINGLIST *types_mua = NULL;
|
2007-10-15 20:10:53 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#ifdef __linux__
|
2008-03-16 22:30:56 +05:30
|
|
|
static void (*selinux_run_init_old)(void);
|
|
|
|
static void (*selinux_run_init_new)(int argc, char **argv);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-10-10 14:07:21 +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
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (! exists(SELINUX_LIB))
|
2007-08-08 08:37:42 +05:30
|
|
|
return;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
lib_handle = dlopen(SELINUX_LIB, RTLD_NOW | RTLD_GLOBAL);
|
2007-08-08 08:37:42 +05:30
|
|
|
if (! lib_handle) {
|
2008-03-16 22:30:56 +05:30
|
|
|
eerror("dlopen: %s", dlerror());
|
2007-08-08 08:37:42 +05:30
|
|
|
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))
|
2009-04-24 03:01:22 +05:30
|
|
|
dlfunc(lib_handle, "selinux_runscript");
|
2007-08-09 20:03:20 +05:30
|
|
|
selinux_run_init_new = (void (*)(int, char **))
|
2009-04-24 03:01:22 +05:30
|
|
|
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)
|
2008-03-16 22:30:56 +05:30
|
|
|
selinux_run_init_new(argc, argv);
|
2007-08-08 08:37:42 +05:30
|
|
|
else if (selinux_run_init_old)
|
2008-03-16 22:30:56 +05:30
|
|
|
selinux_run_init_old();
|
2007-08-08 08:37:42 +05:30
|
|
|
else
|
|
|
|
/* This shouldnt happen... probably corrupt lib */
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("run_init is missing from runscript_selinux.so!");
|
2007-08-08 08:37:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
dlclose(lib_handle);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
|
|
|
handle_signal(int sig)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
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) {
|
2008-03-16 22:30:56 +05:30
|
|
|
case SIGHUP:
|
|
|
|
sighup = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGCHLD:
|
|
|
|
if (signal_pipe[1] > -1) {
|
|
|
|
if (write(signal_pipe[1], &sig, sizeof(sig)) == -1)
|
2008-10-10 14:07:21 +05:30
|
|
|
eerror("%s: send: %s",
|
2009-04-24 03:01:22 +05:30
|
|
|
service, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
} else
|
2008-04-26 20:27:39 +05:30
|
|
|
rc_waitpid(-1);
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
case SIGWINCH:
|
|
|
|
if (master_tty >= 0) {
|
|
|
|
ioctl(fileno(stdout), TIOCGWINSZ, &ws);
|
|
|
|
ioctl(master_tty, TIOCSWINSZ, &ws);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGINT:
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!signame[0])
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(signame, sizeof(signame), "SIGINT");
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SIGTERM:
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!signame[0])
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(signame, sizeof(signame), "SIGTERM");
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SIGQUIT:
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!signame[0])
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(signame, sizeof(signame), "SIGQUIT");
|
|
|
|
/* Send the signal to our children too */
|
|
|
|
if (service_pid > 0)
|
|
|
|
kill(service_pid, sig);
|
|
|
|
eerrorx("%s: caught %s, aborting", applet, signame);
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
default:
|
|
|
|
eerror("%s: caught unknown signal %d", applet, sig);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore errno */
|
|
|
|
errno = serrno;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
2008-10-10 14:38:59 +05:30
|
|
|
unhotplug()
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-17 18:55:56 +05:30
|
|
|
char file[PATH_MAX];
|
|
|
|
|
2008-10-10 14:38:59 +05:30
|
|
|
snprintf(file, sizeof(file), RC_SVCDIR "/hotplugged/%s", applet);
|
2008-03-17 18:55:56 +05:30
|
|
|
if (exists(file) && unlink(file) != 0)
|
|
|
|
eerror("%s: unlink `%s': %s", applet, file, strerror(errno));
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
|
|
|
start_services(RC_STRINGLIST *list)
|
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRING *svc;
|
|
|
|
RC_SERVICE state = rc_service_state (service);
|
2007-04-30 18:48:42 +05:30
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!list)
|
2007-04-30 18:48:42 +05:30
|
|
|
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
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
TAILQ_FOREACH(svc, list, entries) {
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!(rc_service_state(svc->value) &
|
2009-04-24 03:01:22 +05:30
|
|
|
RC_SERVICE_STOPPED))
|
2008-10-10 14:07:21 +05:30
|
|
|
continue;
|
|
|
|
if (state & RC_SERVICE_INACTIVE ||
|
|
|
|
state & RC_SERVICE_WASINACTIVE)
|
|
|
|
{
|
|
|
|
rc_service_schedule_start(service,
|
2009-04-24 03:01:22 +05:30
|
|
|
svc->value);
|
2008-10-10 14:07:21 +05:30
|
|
|
ewarn("WARNING: %s is scheduled to started"
|
2009-04-24 03:01:22 +05:30
|
|
|
" when %s has started",
|
|
|
|
svc->value, applet);
|
2008-10-10 14:07:21 +05:30
|
|
|
} else
|
|
|
|
service_start(svc->value);
|
2007-04-30 18:48:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
|
|
|
restore_state(void)
|
2007-10-10 02:50:10 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_SERVICE state;
|
2007-10-10 02:50:10 +05:30
|
|
|
|
2009-04-18 04:25:11 +05:30
|
|
|
if (rc_in_plugin || exclusive_fd == -1)
|
2007-10-10 02:50:10 +05:30
|
|
|
return;
|
2008-03-16 22:30:56 +05:30
|
|
|
state = rc_service_state(applet);
|
2007-10-10 02:50:10 +05:30
|
|
|
if (state & RC_SERVICE_STOPPING) {
|
|
|
|
if (state & RC_SERVICE_WASINACTIVE)
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_service_mark(applet, RC_SERVICE_INACTIVE);
|
2007-10-10 02:50:10 +05:30
|
|
|
else
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_service_mark(applet, RC_SERVICE_STARTED);
|
|
|
|
if (rc_runlevel_stopping())
|
|
|
|
rc_service_mark(applet, RC_SERVICE_FAILED);
|
2007-10-10 02:50:10 +05:30
|
|
|
} else if (state & RC_SERVICE_STARTING) {
|
|
|
|
if (state & RC_SERVICE_WASINACTIVE)
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_service_mark(applet, RC_SERVICE_INACTIVE);
|
2007-10-10 02:50:10 +05:30
|
|
|
else
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_service_mark(applet, RC_SERVICE_STOPPED);
|
|
|
|
if (rc_runlevel_starting())
|
|
|
|
rc_service_mark(applet, RC_SERVICE_FAILED);
|
2007-10-10 02:50:10 +05:30
|
|
|
}
|
2009-04-18 04:25:11 +05:30
|
|
|
exclusive_fd = svc_unlock(applet, exclusive_fd);
|
2007-10-10 02:50:10 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
|
|
|
cleanup(void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
restore_state();
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!rc_in_plugin) {
|
2007-10-10 02:50:10 +05:30
|
|
|
if (hook_out) {
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(hook_out, applet);
|
2007-10-10 02:50:10 +05:30
|
|
|
if (hook_out == RC_HOOK_SERVICE_START_DONE)
|
2008-10-10 14:07:21 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_START_OUT,
|
2009-04-24 03:01:22 +05:30
|
|
|
applet);
|
2007-10-10 02:50:10 +05:30
|
|
|
else if (hook_out == RC_HOOK_SERVICE_STOP_DONE)
|
2008-10-10 14:07:21 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_STOP_OUT,
|
2009-04-24 03:01:22 +05:30
|
|
|
applet);
|
2007-10-10 02:50:10 +05:30
|
|
|
}
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2007-07-11 19:58:54 +05:30
|
|
|
if (restart_services)
|
2008-03-16 22:30:56 +05:30
|
|
|
start_services(restart_services);
|
2007-04-30 18:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-10-06 20:32:32 +05:30
|
|
|
rc_plugin_unload();
|
|
|
|
|
|
|
|
#ifdef DEBUG_MEMORY
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_free(types_b);
|
|
|
|
rc_stringlist_free(types_n);
|
|
|
|
rc_stringlist_free(types_nu);
|
|
|
|
rc_stringlist_free(types_nua);
|
|
|
|
rc_stringlist_free(types_m);
|
|
|
|
rc_stringlist_free(types_mua);
|
|
|
|
rc_deptree_free(deptree);
|
|
|
|
rc_stringlist_free(restart_services);
|
|
|
|
rc_stringlist_free(need_services);
|
|
|
|
rc_stringlist_free(use_services);
|
|
|
|
rc_stringlist_free(services);
|
|
|
|
rc_stringlist_free(applet_list);
|
|
|
|
rc_stringlist_free(tmplist);
|
2008-10-06 20:32:32 +05:30
|
|
|
free(ibsave);
|
2008-03-16 22:30:56 +05:30
|
|
|
free(service);
|
|
|
|
free(prefix);
|
|
|
|
free(runlevel);
|
2008-10-06 20:32:32 +05:30
|
|
|
#endif
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static int
|
|
|
|
write_prefix(const char *buffer, size_t bytes, bool *prefixed)
|
|
|
|
{
|
2008-11-28 02:35:39 +05:30
|
|
|
size_t i, j;
|
2008-03-16 22:30:56 +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;
|
2008-09-28 00:20:21 +05:30
|
|
|
int fd = fileno(stdout), lock_fd = -1;
|
|
|
|
|
|
|
|
/* Spin until we lock the prefix */
|
|
|
|
for (;;) {
|
|
|
|
lock_fd = open(PREFIX_LOCK, O_WRONLY | O_CREAT, 0664);
|
|
|
|
if (lock_fd != -1)
|
|
|
|
if (flock(lock_fd, LOCK_EX) == 0)
|
|
|
|
break;
|
|
|
|
close(lock_fd);
|
|
|
|
}
|
2007-04-25 18:00:24 +05:30
|
|
|
|
|
|
|
for (i = 0; i < bytes; i++) {
|
2008-11-28 02:35:39 +05:30
|
|
|
/* We don't prefix eend calls (cursor up) */
|
|
|
|
if (buffer[i] == '\033' && !*prefixed) {
|
|
|
|
for (j = i + 1; j < bytes; j++) {
|
|
|
|
if (buffer[j] == 'A')
|
|
|
|
*prefixed = true;
|
|
|
|
if (isalpha((unsigned int)buffer[j]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-04-25 18:00:24 +05:30
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!*prefixed) {
|
2008-03-16 22:30:56 +05:30
|
|
|
ret += write(fd, ec, strlen(ec));
|
|
|
|
ret += write(fd, prefix, strlen(prefix));
|
|
|
|
ret += write(fd, ec_normal, strlen(ec_normal));
|
|
|
|
ret += write(fd, "|", 1);
|
2007-04-25 18:00:24 +05:30
|
|
|
*prefixed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[i] == '\n')
|
|
|
|
*prefixed = false;
|
2008-03-16 22:30:56 +05:30
|
|
|
ret += write(fd, buffer + i, 1);
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-09-28 00:20:21 +05:30
|
|
|
/* Release the lock */
|
|
|
|
close(lock_fd);
|
2008-03-16 22:30:56 +05:30
|
|
|
return ret;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2009-04-18 05:39:03 +05:30
|
|
|
static int
|
2008-10-10 14:07:21 +05:30
|
|
|
svc_exec(const char *arg1, const char *arg2)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2009-04-18 05:39:03 +05:30
|
|
|
int ret, fdout = fileno(stdout);
|
2007-09-21 14:19:43 +05:30
|
|
|
struct termios tt;
|
|
|
|
struct winsize ws;
|
|
|
|
int i;
|
2007-10-09 18:22:09 +05:30
|
|
|
int flags = 0;
|
2008-11-28 02:35:39 +05:30
|
|
|
struct pollfd fd[2];
|
2007-09-21 14:19:43 +05:30
|
|
|
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 slave_tty;
|
|
|
|
|
|
|
|
/* Setup our signal pipe */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (pipe(signal_pipe) == -1)
|
|
|
|
eerrorx("%s: pipe: %s", service, applet);
|
2007-09-21 14:19:43 +05:30
|
|
|
for (i = 0; i < 2; i++)
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((flags = fcntl(signal_pipe[i], F_GETFD, 0) == -1 ||
|
2009-04-24 03:01:22 +05:30
|
|
|
fcntl(signal_pipe[i], F_SETFD, flags | FD_CLOEXEC) == -1))
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("%s: fcntl: %s", service, strerror(errno));
|
2007-09-21 14:19:43 +05:30
|
|
|
|
|
|
|
/* 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;
|
2008-03-16 22:30:56 +05:30
|
|
|
if (prefix && isatty(fdout)) {
|
|
|
|
tcgetattr(fdout, &tt);
|
|
|
|
ioctl(fdout, TIOCGWINSZ, &ws);
|
2007-09-21 14:19:43 +05:30
|
|
|
|
|
|
|
/* If the below call fails due to not enough ptys then we don't
|
|
|
|
* prefix the output, but we still work */
|
2008-03-16 22:30:56 +05:30
|
|
|
openpty(&master_tty, &slave_tty, NULL, &tt, &ws);
|
2008-02-29 02:56:53 +05:30
|
|
|
if (master_tty >= 0 &&
|
2008-03-16 22:30:56 +05:30
|
|
|
(flags = fcntl(master_tty, F_GETFD, 0)) == 0)
|
2009-04-24 03:01:22 +05:30
|
|
|
fcntl(master_tty, F_SETFD, flags | FD_CLOEXEC);
|
2008-02-29 02:56:53 +05:30
|
|
|
|
|
|
|
if (slave_tty >=0 &&
|
2008-03-16 22:30:56 +05:30
|
|
|
(flags = fcntl(slave_tty, F_GETFD, 0)) == 0)
|
2009-04-24 03:01:22 +05:30
|
|
|
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-03-16 22:30:56 +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) {
|
2008-11-28 02:35:39 +05:30
|
|
|
dup2(slave_tty, STDOUT_FILENO);
|
|
|
|
dup2(slave_tty, STDERR_FILENO);
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (exists(RC_SVCDIR "/runscript.sh")) {
|
2008-10-10 14:07:21 +05:30
|
|
|
execl(RC_SVCDIR "/runscript.sh",
|
2009-04-24 03:01:22 +05:30
|
|
|
RC_SVCDIR "/runscript.sh",
|
|
|
|
service, arg1, arg2, (char *) NULL);
|
2008-03-16 22:30:56 +05:30
|
|
|
eerror("%s: exec `" RC_SVCDIR "/runscript.sh': %s",
|
2009-04-24 03:01:22 +05:30
|
|
|
service, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
_exit(EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else {
|
2008-10-10 14:07:21 +05:30
|
|
|
execl(RC_LIBDIR "/sh/runscript.sh",
|
2009-04-24 03:01:22 +05:30
|
|
|
RC_LIBDIR "/sh/runscript.sh",
|
|
|
|
service, arg1, arg2, (char *) NULL);
|
2008-03-16 22:30:56 +05:30
|
|
|
eerror("%s: exec `" RC_LIBDIR "/sh/runscript.sh': %s",
|
2009-04-24 03:01:22 +05:30
|
|
|
service, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
_exit(EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
buffer = xmalloc(sizeof(char) * BUFSIZ);
|
2008-11-28 02:35:39 +05:30
|
|
|
fd[0].fd = signal_pipe[0];
|
|
|
|
fd[0].events = fd[1].events = POLLIN;
|
|
|
|
fd[0].revents = fd[1].revents = 0;
|
|
|
|
if (master_tty >= 0) {
|
|
|
|
fd[1].fd = master_tty;
|
|
|
|
fd[1].events = POLLIN;
|
|
|
|
fd[1].revents = 0;
|
|
|
|
}
|
2007-09-21 14:19:43 +05:30
|
|
|
|
2008-11-28 02:35:39 +05:30
|
|
|
for (;;) {
|
|
|
|
if ((s = poll(fd, master_tty >= 0 ? 2 : 1, -1)) == -1) {
|
2007-09-21 14:19:43 +05:30
|
|
|
if (errno != EINTR) {
|
2008-11-28 02:35:39 +05:30
|
|
|
eerror("%s: poll: %s",
|
2009-04-24 03:01:22 +05:30
|
|
|
service, strerror(errno));
|
2007-09-21 14:19:43 +05:30
|
|
|
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) {
|
2008-11-28 02:35:39 +05:30
|
|
|
if (fd[1].revents & (POLLIN | POLLHUP)) {
|
2008-03-16 22:30:56 +05:30
|
|
|
bytes = read(master_tty, buffer, BUFSIZ);
|
|
|
|
write_prefix(buffer, bytes, &prefixed);
|
2007-09-21 14:19:43 +05:30
|
|
|
}
|
2007-10-23 01:03:42 +05:30
|
|
|
|
|
|
|
/* Only SIGCHLD signals come down this pipe */
|
2008-11-28 02:35:39 +05:30
|
|
|
if (fd[0].revents & (POLLIN | POLLHUP))
|
2007-10-23 01:03:42 +05:30
|
|
|
break;
|
2007-09-21 14:19:43 +05:30
|
|
|
}
|
|
|
|
}
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
free(buffer);
|
|
|
|
close(signal_pipe[0]);
|
|
|
|
close(signal_pipe[1]);
|
2007-09-21 14:19:43 +05:30
|
|
|
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); */
|
2008-03-16 22:30:56 +05:30
|
|
|
close(master_tty);
|
2007-09-21 14:19:43 +05:30
|
|
|
master_tty = -1;
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
|
2009-04-18 05:39:03 +05:30
|
|
|
ret = WEXITSTATUS(rc_waitpid(service_pid));
|
|
|
|
if (ret != 0 && errno == ECHILD)
|
2008-11-03 21:01:01 +05:30
|
|
|
/* killall5 -9 could cause this */
|
2009-04-18 05:39:03 +05:30
|
|
|
ret = 0;
|
2007-04-21 00:28:42 +05:30
|
|
|
service_pid = 0;
|
2007-04-25 18:00:24 +05:30
|
|
|
|
2009-04-18 05:39:03 +05:30
|
|
|
return ret;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static bool
|
|
|
|
svc_wait(const char *svc)
|
2007-12-05 23:18:07 +05:30
|
|
|
{
|
2009-04-18 04:25:11 +05:30
|
|
|
char file[PATH_MAX];
|
|
|
|
int fd;
|
2007-12-05 23:18:07 +05:30
|
|
|
bool forever = false;
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRINGLIST *keywords;
|
2009-04-24 01:50:17 +05:30
|
|
|
struct timespec interval, timeout, warn;
|
2007-12-05 23:18:07 +05:30
|
|
|
|
2008-01-31 23:31:20 +05:30
|
|
|
/* Some services don't have a timeout, like fsck */
|
2008-03-16 22:30:56 +05:30
|
|
|
keywords = rc_deptree_depend(deptree, svc, "keyword");
|
2008-09-18 21:06:22 +05:30
|
|
|
if (rc_stringlist_find(keywords, "notimeout"))
|
|
|
|
forever = true;
|
|
|
|
rc_stringlist_free(keywords);
|
2007-12-05 23:18:07 +05:30
|
|
|
|
2009-04-18 04:25:11 +05:30
|
|
|
snprintf(file, sizeof(file), RC_SVCDIR "/exclusive/%s",
|
2009-04-24 03:01:22 +05:30
|
|
|
basename_c(svc));
|
2007-12-05 23:18:07 +05:30
|
|
|
|
2009-04-24 01:50:17 +05:30
|
|
|
interval.tv_sec = 0;
|
|
|
|
interval.tv_nsec = WAIT_INTERVAL;
|
|
|
|
timeout.tv_sec = WAIT_TIMEOUT;
|
|
|
|
timeout.tv_nsec = 0;
|
|
|
|
warn.tv_sec = WARN_TIMEOUT;
|
|
|
|
warn.tv_nsec = 0;
|
|
|
|
for (;;) {
|
2009-04-18 04:25:11 +05:30
|
|
|
fd = open(file, O_RDONLY | O_NONBLOCK);
|
|
|
|
if (fd != -1) {
|
|
|
|
if (flock(fd, LOCK_SH | LOCK_NB) == 0) {
|
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
close(fd);
|
2007-12-05 23:18:07 +05:30
|
|
|
}
|
2009-04-18 04:25:11 +05:30
|
|
|
if (errno == ENOENT)
|
|
|
|
return true;
|
|
|
|
if (errno != EWOULDBLOCK)
|
2009-04-24 01:50:17 +05:30
|
|
|
eerrorx("%s: open `%s': %s", applet, file,
|
|
|
|
strerror(errno));
|
|
|
|
if (nanosleep(&interval, NULL) == -1) {
|
2007-12-05 23:18:07 +05:30
|
|
|
if (errno != EINTR)
|
2009-04-18 04:25:11 +05:30
|
|
|
return false;
|
2007-12-05 23:18:07 +05:30
|
|
|
}
|
2008-11-24 00:10:43 +05:30
|
|
|
if (!forever) {
|
2009-04-24 01:50:17 +05:30
|
|
|
timespecsub(&timeout, &interval, &timeout);
|
|
|
|
if (timeout.tv_sec <= 0)
|
|
|
|
return false;
|
|
|
|
timespecsub(&warn, &interval, &warn);
|
|
|
|
if (warn.tv_sec <= 0) {
|
2009-04-24 14:02:44 +05:30
|
|
|
ewarn("%s: waiting for %s (%d seconds)",
|
|
|
|
applet, svc, (int)timeout.tv_sec);
|
2009-04-24 01:50:17 +05:30
|
|
|
warn.tv_sec = WARN_TIMEOUT;
|
|
|
|
warn.tv_nsec = 0;
|
2008-11-24 00:10:43 +05:30
|
|
|
}
|
2008-09-27 22:47:15 +05:30
|
|
|
}
|
2007-12-05 23:18:07 +05:30
|
|
|
}
|
2009-04-18 04:25:11 +05:30
|
|
|
return false;
|
2007-12-05 23:18:07 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
|
|
|
get_started_services(void)
|
2008-03-16 22:30:56 +05:30
|
|
|
{
|
|
|
|
RC_STRINGLIST *tmp = rc_services_in_state(RC_SERVICE_INACTIVE);
|
2008-10-10 14:07:21 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_free(restart_services);
|
|
|
|
restart_services = rc_services_in_state(RC_SERVICE_STARTED);
|
2008-10-10 14:07:21 +05:30
|
|
|
TAILQ_CONCAT(restart_services, tmp, entries);
|
|
|
|
free(tmp);
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
|
|
|
setup_types(void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
types_b = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(types_b, "broken");
|
|
|
|
|
|
|
|
types_n = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(types_n, "ineed");
|
|
|
|
|
|
|
|
types_nu = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(types_nu, "ineed");
|
|
|
|
rc_stringlist_add(types_nu, "iuse");
|
|
|
|
|
|
|
|
types_nua = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(types_nua, "ineed");
|
|
|
|
rc_stringlist_add(types_nua, "iuse");
|
|
|
|
rc_stringlist_add(types_nua, "iafter");
|
|
|
|
|
|
|
|
types_m = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(types_m, "needsme");
|
|
|
|
|
|
|
|
types_mua = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(types_mua, "needsme");
|
|
|
|
rc_stringlist_add(types_mua, "usesme");
|
|
|
|
rc_stringlist_add(types_mua, "beforeme");
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
2009-05-01 04:12:01 +05:30
|
|
|
svc_start_check(void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_SERVICE state;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
state = rc_service_state(service);
|
2009-05-01 04:12:01 +05:30
|
|
|
|
|
|
|
if (in_background) {
|
2009-04-30 21:15:18 +05:30
|
|
|
if (!(state & (RC_SERVICE_INACTIVE | RC_SERVICE_STOPPED)))
|
2008-03-16 22:30:56 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2009-05-01 04:12:01 +05:30
|
|
|
if (rc_yesno(getenv("IN_HOTPLUG")))
|
|
|
|
rc_service_mark(service, RC_SERVICE_HOTPLUGGED);
|
2008-12-23 16:10:27 +05:30
|
|
|
if (strcmp(runlevel, RC_LEVEL_SYSINIT) == 0)
|
|
|
|
ewarnx("WARNING: %s will be started in the"
|
2009-04-24 03:01:22 +05:30
|
|
|
" next runlevel", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2009-04-19 01:24:04 +05:30
|
|
|
if (exclusive_fd == -1)
|
2009-04-18 04:25:11 +05:30
|
|
|
exclusive_fd = svc_lock(applet);
|
|
|
|
if (exclusive_fd == -1) {
|
|
|
|
if (errno == EACCES)
|
|
|
|
eerrorx("%s: superuser access required", applet);
|
|
|
|
if (state & RC_SERVICE_STOPPING)
|
|
|
|
ewarnx("WARNING: %s is stopping", applet);
|
|
|
|
else
|
|
|
|
ewarnx("WARNING: %s is already starting", applet);
|
|
|
|
}
|
2009-04-19 01:24:04 +05:30
|
|
|
fcntl(exclusive_fd, F_SETFD,
|
2009-04-24 03:01:22 +05:30
|
|
|
fcntl(exclusive_fd, F_GETFD, 0) | FD_CLOEXEC);
|
2009-04-19 01:24:04 +05:30
|
|
|
|
2007-11-16 17:25:08 +05:30
|
|
|
if (state & RC_SERVICE_STARTED) {
|
2008-03-16 22:30:56 +05:30
|
|
|
ewarn("WARNING: %s has already been started", applet);
|
2007-11-16 17:25:08 +05:30
|
|
|
return;
|
2009-05-01 04:12:01 +05:30
|
|
|
} else if (state & RC_SERVICE_INACTIVE && !in_background)
|
2008-10-10 14:07:21 +05:30
|
|
|
ewarnx("WARNING: %s has already started, but is inactive",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet);
|
2009-04-18 04:25:11 +05:30
|
|
|
|
|
|
|
rc_service_mark(service, RC_SERVICE_STARTING);
|
2007-11-19 19:16:09 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_START_OUT;
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_START_IN, applet);
|
2009-05-01 04:12:01 +05:30
|
|
|
}
|
2007-11-19 19:16:09 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
static void
|
|
|
|
svc_start_deps(void)
|
|
|
|
{
|
|
|
|
bool first;
|
|
|
|
RC_STRING *svc, *svc2;
|
|
|
|
RC_SERVICE state;
|
|
|
|
int depoptions = RC_DEP_TRACE, n;
|
|
|
|
size_t len;
|
|
|
|
char *p, *tmp;
|
|
|
|
pid_t pid;
|
|
|
|
|
2008-04-29 15:55:45 +05:30
|
|
|
errno = 0;
|
|
|
|
if (rc_conf_yesno("rc_depend_strict") || errno == ENOENT)
|
2007-07-21 18:19:51 +05:30
|
|
|
depoptions |= RC_DEP_STRICT;
|
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
if (!deptree && ((deptree = _rc_deptree_load(0, NULL)) == NULL))
|
|
|
|
eerrorx("failed to load deptree");
|
|
|
|
if (!types_b)
|
|
|
|
setup_types();
|
|
|
|
|
|
|
|
services = rc_deptree_depends(deptree, types_b, applet_list,
|
|
|
|
runlevel, 0);
|
|
|
|
if (TAILQ_FIRST(services)) {
|
|
|
|
eerrorn("ERROR: %s needs service(s) ", applet);
|
|
|
|
first = true;
|
|
|
|
TAILQ_FOREACH(svc, services, entries) {
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
fprintf(stderr, "%s", svc->value);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2009-05-01 04:12:01 +05:30
|
|
|
fprintf(stderr, "\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
rc_stringlist_free(services);
|
|
|
|
services = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
need_services = rc_deptree_depends(deptree, types_n,
|
|
|
|
applet_list, runlevel, depoptions);
|
|
|
|
use_services = rc_deptree_depends(deptree, types_nu,
|
|
|
|
applet_list, runlevel, depoptions);
|
|
|
|
|
|
|
|
if (!rc_runlevel_starting()) {
|
|
|
|
TAILQ_FOREACH(svc, use_services, entries) {
|
|
|
|
state = rc_service_state(svc->value);
|
|
|
|
/* Don't stop failed services again.
|
|
|
|
* If you remove this check, ensure that the
|
|
|
|
* exclusive file isn't created. */
|
|
|
|
if (state & RC_SERVICE_FAILED &&
|
|
|
|
rc_runlevel_starting())
|
|
|
|
continue;
|
|
|
|
if (state & RC_SERVICE_STOPPED) {
|
|
|
|
if (dry_run) {
|
|
|
|
printf(" %s", svc->value);
|
2008-04-28 02:34:37 +05:30
|
|
|
continue;
|
2007-04-21 00:28:42 +05:30
|
|
|
}
|
2009-05-01 04:12:01 +05:30
|
|
|
pid = service_start(svc->value);
|
|
|
|
if (!rc_conf_yesno("rc_parallel"))
|
|
|
|
rc_waitpid(pid);
|
2008-04-28 02:34:37 +05:30
|
|
|
}
|
2008-10-10 14:07:21 +05:30
|
|
|
}
|
2009-05-01 04:12:01 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
if (dry_run)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Now wait for them to start */
|
|
|
|
services = rc_deptree_depends(deptree, types_nua, applet_list,
|
|
|
|
runlevel, depoptions);
|
|
|
|
/* We use tmplist to hold our scheduled by list */
|
|
|
|
tmplist = rc_stringlist_new();
|
|
|
|
TAILQ_FOREACH(svc, services, entries) {
|
|
|
|
state = rc_service_state(svc->value);
|
|
|
|
if (state & RC_SERVICE_STARTED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Don't wait for services which went inactive but are
|
|
|
|
* now in starting state which we are after */
|
|
|
|
if (state & RC_SERVICE_STARTING &&
|
|
|
|
state & RC_SERVICE_WASINACTIVE)
|
|
|
|
{
|
|
|
|
if (!rc_stringlist_find(need_services, svc->value) &&
|
|
|
|
!rc_stringlist_find(use_services, svc->value))
|
2008-10-10 14:07:21 +05:30
|
|
|
continue;
|
2009-05-01 04:12:01 +05:30
|
|
|
}
|
2008-10-10 14:07:21 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
if (!svc_wait(svc->value))
|
|
|
|
eerror("%s: timed out waiting for %s",
|
|
|
|
applet, svc->value);
|
|
|
|
state = rc_service_state(svc->value);
|
|
|
|
if (state & RC_SERVICE_STARTED)
|
|
|
|
continue;
|
|
|
|
if (rc_stringlist_find(need_services, svc->value)) {
|
|
|
|
if (state & RC_SERVICE_INACTIVE ||
|
2008-10-10 14:07:21 +05:30
|
|
|
state & RC_SERVICE_WASINACTIVE)
|
|
|
|
{
|
2009-05-01 04:12:01 +05:30
|
|
|
rc_stringlist_add(tmplist, svc->value);
|
|
|
|
} else if (!TAILQ_FIRST(tmplist))
|
|
|
|
eerrorx("ERROR: cannot start %s as"
|
|
|
|
" %s would not start",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet, svc->value);
|
2008-10-10 14:07:21 +05:30
|
|
|
}
|
2009-05-01 04:12:01 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
if (TAILQ_FIRST(tmplist)) {
|
|
|
|
/* Set the state now, then unlink our exclusive so that
|
|
|
|
our scheduled list is preserved */
|
|
|
|
rc_service_mark(service, RC_SERVICE_STOPPED);
|
2008-03-17 20:01:44 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
rc_stringlist_free(use_services);
|
|
|
|
use_services = NULL;
|
|
|
|
len = 0;
|
|
|
|
n = 0;
|
|
|
|
TAILQ_FOREACH(svc, tmplist, entries) {
|
|
|
|
rc_service_schedule_start(svc->value, service);
|
|
|
|
use_services = rc_deptree_depend(deptree,
|
|
|
|
"iprovide", svc->value);
|
|
|
|
TAILQ_FOREACH(svc2, use_services, entries)
|
|
|
|
rc_service_schedule_start(svc2->value, service);
|
2008-10-10 14:07:21 +05:30
|
|
|
rc_stringlist_free(use_services);
|
|
|
|
use_services = NULL;
|
2009-05-01 04:12:01 +05:30
|
|
|
len += strlen(svc->value) + 2;
|
|
|
|
n++;
|
2008-03-17 20:01:44 +05:30
|
|
|
}
|
2008-10-10 14:07:21 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
len += 5;
|
|
|
|
tmp = p = xmalloc(sizeof(char) * len);
|
|
|
|
TAILQ_FOREACH(svc, tmplist, entries) {
|
|
|
|
if (p != tmp)
|
|
|
|
p += snprintf(p, len, ", ");
|
|
|
|
p += snprintf(p, len - (p - tmp),
|
|
|
|
"%s", svc->value);
|
|
|
|
}
|
|
|
|
rc_stringlist_free(tmplist);
|
|
|
|
tmplist = NULL;
|
|
|
|
ewarnx("WARNING: %s is scheduled to start when "
|
|
|
|
"%s has started", applet, tmp);
|
|
|
|
free(tmp);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
rc_stringlist_free(services);
|
|
|
|
services = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void svc_start_real()
|
|
|
|
{
|
|
|
|
bool started;
|
|
|
|
RC_STRING *svc, *svc2;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ibsave)
|
2008-03-16 22:30:56 +05:30
|
|
|
setenv("IN_BACKGROUND", ibsave, 1);
|
2007-10-10 02:50:10 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_START_DONE;
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_START_NOW, applet);
|
2009-04-18 05:39:03 +05:30
|
|
|
started = (svc_exec("start", NULL) == 0);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ibsave)
|
2008-03-16 22:30:56 +05:30
|
|
|
unsetenv("IN_BACKGROUND");
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-04-18 04:25:11 +05:30
|
|
|
if (!started)
|
|
|
|
eerrorx("ERROR: %s failed to start", applet);
|
|
|
|
else {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (rc_service_state(service) & RC_SERVICE_INACTIVE)
|
2008-10-10 14:07:21 +05:30
|
|
|
ewarnx("WARNING: %s has started, but is inactive",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_service_mark(service, RC_SERVICE_STARTED);
|
2009-04-18 04:25:11 +05:30
|
|
|
exclusive_fd = svc_unlock(applet, exclusive_fd);
|
2007-10-10 02:50:10 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_START_OUT;
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_START_DONE, applet);
|
2007-10-10 02:50:10 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Now start any scheduled services */
|
2008-03-16 22:30:56 +05:30
|
|
|
services = rc_services_scheduled(service);
|
2008-10-10 14:07:21 +05:30
|
|
|
TAILQ_FOREACH(svc, services, entries)
|
2009-04-24 03:01:22 +05:30
|
|
|
if (rc_service_state(svc->value) & RC_SERVICE_STOPPED)
|
|
|
|
service_start(svc->value);
|
2008-10-10 14:07:21 +05:30
|
|
|
rc_stringlist_free(services);
|
|
|
|
services = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Do the same for any services we provide */
|
2008-05-12 18:50:35 +05:30
|
|
|
if (deptree) {
|
|
|
|
tmplist = rc_deptree_depend(deptree, "iprovide", applet);
|
2008-10-10 14:07:21 +05:30
|
|
|
TAILQ_FOREACH(svc, tmplist, entries) {
|
|
|
|
services = rc_services_scheduled(svc->value);
|
|
|
|
TAILQ_FOREACH(svc2, services, entries)
|
2009-04-24 03:01:22 +05:30
|
|
|
if (rc_service_state(svc2->value) &
|
|
|
|
RC_SERVICE_STOPPED)
|
|
|
|
service_start(svc2->value);
|
2008-10-10 14:07:21 +05:30
|
|
|
rc_stringlist_free(services);
|
|
|
|
services = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
}
|
|
|
|
rc_stringlist_free(tmplist);
|
|
|
|
tmplist = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
hook_out = 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_START_OUT, applet);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
2009-05-01 04:12:01 +05:30
|
|
|
svc_start(void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2009-05-01 04:12:01 +05:30
|
|
|
if (dry_run)
|
|
|
|
einfon("start:");
|
|
|
|
else
|
|
|
|
svc_start_check();
|
|
|
|
if (deps)
|
|
|
|
svc_start_deps();
|
|
|
|
if (dry_run)
|
|
|
|
printf(" %s\n", applet);
|
|
|
|
else
|
|
|
|
svc_start_real();
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
static void
|
|
|
|
svc_stop_check(RC_SERVICE *state)
|
|
|
|
{
|
|
|
|
*state = rc_service_state(service);
|
2008-10-10 14:07:21 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
if (rc_runlevel_stopping() && *state & RC_SERVICE_FAILED)
|
2008-10-10 14:07:21 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
if (in_background &&
|
|
|
|
!(*state & RC_SERVICE_STARTED) &&
|
|
|
|
!(*state & RC_SERVICE_INACTIVE))
|
|
|
|
exit(EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-04-18 04:25:11 +05:30
|
|
|
if (exclusive_fd == -1)
|
|
|
|
exclusive_fd = svc_lock(applet);
|
|
|
|
if (exclusive_fd == -1) {
|
|
|
|
if (errno == EACCES)
|
|
|
|
eerrorx("%s: superuser access required", applet);
|
2009-05-01 04:12:01 +05:30
|
|
|
if (*state & RC_SERVICE_STOPPING)
|
2009-04-18 04:25:11 +05:30
|
|
|
ewarnx("WARNING: %s is already stopping", applet);
|
2009-05-01 04:12:01 +05:30
|
|
|
eerrorx("ERROR: %s stopped by something else", applet);
|
2009-04-18 04:25:11 +05:30
|
|
|
}
|
2009-04-19 01:24:04 +05:30
|
|
|
fcntl(exclusive_fd, F_SETFD,
|
2009-04-24 03:01:22 +05:30
|
|
|
fcntl(exclusive_fd, F_GETFD, 0) | FD_CLOEXEC);
|
2009-04-19 01:24:04 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
if (*state & RC_SERVICE_STOPPED) {
|
2008-03-16 22:30:56 +05:30
|
|
|
ewarn("WARNING: %s is already stopped", applet);
|
2007-11-16 17:25:08 +05:30
|
|
|
return;
|
2008-03-06 17:04:38 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-04-18 04:25:11 +05:30
|
|
|
rc_service_mark(service, RC_SERVICE_STOPPING);
|
2007-11-19 19:16:09 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_STOP_OUT;
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_STOP_IN, applet);
|
2007-11-19 19:16:09 +05:30
|
|
|
|
2008-12-11 20:20:58 +05:30
|
|
|
if (!rc_runlevel_stopping()) {
|
|
|
|
if (rc_service_in_runlevel(service, RC_LEVEL_SYSINIT))
|
2009-04-18 04:25:11 +05:30
|
|
|
ewarn("WARNING: you are stopping a sysinit service");
|
2008-12-11 20:20:58 +05:30
|
|
|
else if (rc_service_in_runlevel(service, RC_LEVEL_BOOT))
|
2009-04-18 04:25:11 +05:30
|
|
|
ewarn("WARNING: you are stopping a boot service");
|
2008-12-11 20:20:58 +05:30
|
|
|
}
|
2009-05-01 04:12:01 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
static void
|
|
|
|
svc_stop_deps(RC_SERVICE state)
|
|
|
|
{
|
|
|
|
int depoptions = RC_DEP_TRACE;
|
|
|
|
RC_STRING *svc;
|
|
|
|
pid_t pid;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
if (state & RC_SERVICE_WASINACTIVE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (rc_conf_yesno("rc_depend_strict") || errno == ENOENT)
|
|
|
|
depoptions |= RC_DEP_STRICT;
|
2008-03-16 22:30:56 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
if (!deptree && ((deptree = _rc_deptree_load(0, NULL)) == NULL))
|
|
|
|
eerrorx("failed to load deptree");
|
|
|
|
|
|
|
|
if (!types_m)
|
|
|
|
setup_types();
|
|
|
|
|
|
|
|
services = rc_deptree_depends(deptree, types_m, applet_list,
|
|
|
|
runlevel, depoptions);
|
|
|
|
tmplist = rc_stringlist_new();
|
|
|
|
TAILQ_FOREACH_REVERSE(svc, services, rc_stringlist, entries) {
|
|
|
|
state = rc_service_state(svc->value);
|
|
|
|
/* Don't stop failed services again.
|
|
|
|
* If you remove this check, ensure that the
|
|
|
|
* exclusive file isn't created. */
|
|
|
|
if (state & RC_SERVICE_FAILED &&
|
|
|
|
rc_runlevel_stopping())
|
|
|
|
continue;
|
|
|
|
if (state & RC_SERVICE_STARTED ||
|
|
|
|
state & RC_SERVICE_INACTIVE)
|
|
|
|
{
|
|
|
|
if (dry_run) {
|
|
|
|
printf(" %s", svc->value);
|
2008-10-10 14:07:21 +05:30
|
|
|
continue;
|
2009-05-01 04:12:01 +05:30
|
|
|
}
|
|
|
|
svc_wait(svc->value);
|
|
|
|
state = rc_service_state(svc->value);
|
2008-10-10 14:07:21 +05:30
|
|
|
if (state & RC_SERVICE_STARTED ||
|
|
|
|
state & RC_SERVICE_INACTIVE)
|
|
|
|
{
|
2009-05-01 04:12:01 +05:30
|
|
|
pid = service_stop(svc->value);
|
|
|
|
if (!rc_conf_yesno("rc_parallel"))
|
|
|
|
rc_waitpid(pid);
|
|
|
|
rc_stringlist_add(tmplist, svc->value);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
2009-05-01 04:12:01 +05:30
|
|
|
}
|
|
|
|
rc_stringlist_free(services);
|
|
|
|
services = NULL;
|
|
|
|
if (dry_run)
|
|
|
|
return;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
TAILQ_FOREACH(svc, tmplist, entries) {
|
|
|
|
if (rc_service_state(svc->value) & RC_SERVICE_STOPPED)
|
|
|
|
continue;
|
|
|
|
svc_wait(svc->value);
|
|
|
|
if (rc_service_state(svc->value) & RC_SERVICE_STOPPED)
|
|
|
|
continue;
|
|
|
|
if (rc_runlevel_stopping()) {
|
|
|
|
/* If shutting down, we should stop even
|
|
|
|
* if a dependant failed */
|
|
|
|
if (runlevel &&
|
|
|
|
(strcmp(runlevel,
|
|
|
|
RC_LEVEL_SHUTDOWN) == 0 ||
|
|
|
|
strcmp(runlevel,
|
|
|
|
RC_LEVEL_SINGLE) == 0))
|
2008-10-10 14:07:21 +05:30
|
|
|
continue;
|
2009-05-01 04:12:01 +05:30
|
|
|
rc_service_mark(service, RC_SERVICE_FAILED);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2009-05-01 04:12:01 +05:30
|
|
|
eerrorx("ERROR: cannot stop %s as %s "
|
|
|
|
"is still up", applet, svc->value);
|
|
|
|
}
|
|
|
|
rc_stringlist_free(tmplist);
|
|
|
|
tmplist = NULL;
|
2008-10-10 14:07:21 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
/* We now wait for other services that may use us and are
|
|
|
|
* stopping. This is important when a runlevel stops */
|
|
|
|
services = rc_deptree_depends(deptree, types_mua, applet_list,
|
|
|
|
runlevel, depoptions);
|
|
|
|
TAILQ_FOREACH(svc, services, entries) {
|
|
|
|
if (rc_service_state(svc->value) & RC_SERVICE_STOPPED)
|
|
|
|
continue;
|
|
|
|
svc_wait(svc->value);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2009-05-01 04:12:01 +05:30
|
|
|
rc_stringlist_free(services);
|
|
|
|
services = NULL;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
static void
|
|
|
|
svc_stop_real(void)
|
|
|
|
{
|
|
|
|
bool stopped;
|
|
|
|
|
2008-03-05 04:22:48 +05:30
|
|
|
/* If we're stopping localmount, set LC_ALL=C so that
|
|
|
|
* bash doesn't load anything blocking the unmounting of /usr */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strcmp(applet, "localmount") == 0)
|
|
|
|
setenv("LC_ALL", "C", 1);
|
2008-03-05 04:22:48 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ibsave)
|
2008-03-16 22:30:56 +05:30
|
|
|
setenv("IN_BACKGROUND", ibsave, 1);
|
2007-10-10 02:50:10 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_STOP_DONE;
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_STOP_NOW, applet);
|
2009-04-18 05:39:03 +05:30
|
|
|
stopped = (svc_exec("stop", NULL) == 0);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (ibsave)
|
2008-03-16 22:30:56 +05:30
|
|
|
unsetenv("IN_BACKGROUND");
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!stopped)
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("ERROR: %s failed to stop", applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (in_background)
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_service_mark(service, RC_SERVICE_INACTIVE);
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_service_mark(service, RC_SERVICE_STOPPED);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-10-10 02:50:10 +05:30
|
|
|
hook_out = RC_HOOK_SERVICE_STOP_OUT;
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_STOP_DONE, applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
hook_out = 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_SERVICE_STOP_OUT, applet);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static void
|
2009-05-01 04:12:01 +05:30
|
|
|
svc_stop(void)
|
|
|
|
{
|
|
|
|
RC_SERVICE state;
|
|
|
|
|
|
|
|
state = 0;
|
|
|
|
if (dry_run)
|
|
|
|
einfon("stop:");
|
|
|
|
else
|
|
|
|
svc_stop_check(&state);
|
|
|
|
if (deps)
|
|
|
|
svc_stop_deps(state);
|
|
|
|
if (dry_run)
|
|
|
|
printf(" %s\n", applet);
|
|
|
|
else
|
|
|
|
svc_stop_real();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
svc_restart(void)
|
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!
|
2008-10-10 14:07:21 +05:30
|
|
|
* 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) {
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_SERVICE state = rc_service_state(service);
|
2007-09-28 20:23:38 +05:30
|
|
|
if (state & RC_SERVICE_STARTED || state & RC_SERVICE_INACTIVE)
|
2008-03-16 22:30:56 +05:30
|
|
|
svc_exec("stop", "start");
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
2008-03-16 22:30:56 +05:30
|
|
|
svc_exec("start", NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!(rc_service_state(service) & RC_SERVICE_STOPPED)) {
|
2008-03-16 22:30:56 +05:30
|
|
|
get_started_services();
|
2009-05-01 04:12:01 +05:30
|
|
|
svc_stop();
|
|
|
|
if (dry_run)
|
|
|
|
ewarn("Cannot calculate restart start dependencies"
|
|
|
|
" on a dry-run");
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
svc_start();
|
2008-03-16 22:30:56 +05:30
|
|
|
start_services(restart_services);
|
|
|
|
rc_stringlist_free(restart_services);
|
2007-04-30 18:48:42 +05:30
|
|
|
restart_services = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
static bool
|
|
|
|
service_plugable(void)
|
|
|
|
{
|
2008-10-10 14:38:59 +05:30
|
|
|
char *list, *p, *token;
|
|
|
|
bool allow = true, truefalse;
|
|
|
|
char *match = rc_conf_value("rc_hotplug");
|
2008-10-10 14:07:21 +05:30
|
|
|
|
2008-10-10 14:38:59 +05:30
|
|
|
if (!match)
|
|
|
|
match = rc_conf_value("rc_plug_services");
|
|
|
|
if (!match)
|
2008-10-30 20:33:12 +05:30
|
|
|
return false;
|
2008-10-10 14:07:21 +05:30
|
|
|
|
|
|
|
list = xstrdup(match);
|
|
|
|
p = list;
|
|
|
|
while ((token = strsep(&p, " "))) {
|
|
|
|
if (token[0] == '!') {
|
|
|
|
truefalse = false;
|
|
|
|
token++;
|
|
|
|
} else
|
|
|
|
truefalse = true;
|
|
|
|
|
2008-10-10 14:38:59 +05:30
|
|
|
if (fnmatch(token, applet, 0) == 0) {
|
|
|
|
allow = truefalse;
|
|
|
|
break;
|
2008-10-10 14:07:21 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_MEMORY
|
|
|
|
free(list);
|
|
|
|
#endif
|
|
|
|
return allow;
|
|
|
|
}
|
|
|
|
|
2007-08-28 18:36:44 +05:30
|
|
|
#include "_usage.h"
|
2009-05-01 04:12:01 +05:30
|
|
|
#define getoptstring "dDsvl:Z" 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'},
|
2009-05-01 04:12:01 +05:30
|
|
|
{ "dry-run", 0, NULL, 'Z'},
|
2007-08-28 18:36:44 +05:30
|
|
|
{ "ifstarted", 0, NULL, 's'},
|
2007-04-17 18:14:32 +05:30
|
|
|
{ "nodeps", 0, NULL, 'D'},
|
2009-04-18 04:25:11 +05:30
|
|
|
{ "lockfd", 1, NULL, 'l'},
|
2007-08-28 18:36:44 +05:30
|
|
|
longopts_COMMON
|
2007-04-17 18:14:32 +05:30
|
|
|
};
|
2008-10-10 14:07:21 +05:30
|
|
|
static const char *const longopts_help[] = {
|
2007-12-18 23:31:05 +05:30
|
|
|
"set xtrace when running the script",
|
2009-05-01 04:12:01 +05:30
|
|
|
"show what would be done",
|
2007-12-18 23:31:05 +05:30
|
|
|
"only run commands when started",
|
|
|
|
"ignore dependencies",
|
2009-04-18 04:25:11 +05:30
|
|
|
"fd of the exclusive lock from rc",
|
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
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
int
|
|
|
|
runscript(int argc, char **argv)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
bool doneone = false;
|
2009-05-01 04:12:01 +05:30
|
|
|
int retval, opt, depoptions = RC_DEP_TRACE;
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRING *svc;
|
2009-05-01 04:12:01 +05:30
|
|
|
char path[PATH_MAX], lnk[PATH_MAX], *dir, *save = NULL, pidstr[10];
|
|
|
|
size_t l = 0, ll;
|
2008-04-29 15:55:45 +05:30
|
|
|
const char *file;
|
|
|
|
struct stat stbuf;
|
2007-04-12 15:38:42 +05:30
|
|
|
|
2007-11-14 21:49:56 +05:30
|
|
|
/* Show help if insufficient args */
|
2008-10-10 14:07:21 +05:30
|
|
|
if (argc < 2 || !exists(argv[1])) {
|
2008-04-29 15:55:45 +05:30
|
|
|
fprintf(stderr, "runscript should not be run directly\n");
|
2008-03-16 22:30:56 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2007-11-14 21:49:56 +05:30
|
|
|
}
|
2007-07-04 20:26:59 +05:30
|
|
|
|
2008-04-29 15:55:45 +05:30
|
|
|
if (stat(argv[1], &stbuf) != 0) {
|
|
|
|
fprintf(stderr, "runscript `%s': %s\n",
|
2009-04-24 03:01:22 +05:30
|
|
|
argv[1], strerror(errno));
|
2008-04-29 15:55:45 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2007-11-21 21:08:07 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
atexit(cleanup);
|
2007-06-26 14:17:46 +05:30
|
|
|
|
2008-04-29 15:55:45 +05:30
|
|
|
/* We need to work out the real full path to our service.
|
|
|
|
* This works fine, provided that we ONLY allow mulitplexed services
|
|
|
|
* to exist in the same directory as the master link.
|
|
|
|
* Also, the master link as to be a real file in the init dir. */
|
|
|
|
if (!realpath(argv[1], path)) {
|
|
|
|
fprintf(stderr, "realpath: %s\n", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
memset(lnk, 0, sizeof(lnk));
|
|
|
|
if (readlink(argv[1], lnk, sizeof(lnk)-1)) {
|
|
|
|
dir = dirname(path);
|
|
|
|
if (strchr(lnk, '/')) {
|
|
|
|
save = xstrdup(dir);
|
|
|
|
dir = dirname(lnk);
|
|
|
|
if (strcmp(dir, save) == 0)
|
|
|
|
file = basename_c(argv[1]);
|
|
|
|
else
|
|
|
|
file = basename_c(lnk);
|
2008-04-29 16:19:50 +05:30
|
|
|
dir = save;
|
2008-04-29 15:55:45 +05:30
|
|
|
} else
|
|
|
|
file = basename_c(argv[1]);
|
|
|
|
ll = strlen(dir) + strlen(file) + 2;
|
|
|
|
service = xmalloc(ll);
|
|
|
|
snprintf(service, ll, "%s/%s", dir, file);
|
|
|
|
if (stat(service, &stbuf) != 0) {
|
|
|
|
free(service);
|
|
|
|
service = xstrdup(lnk);
|
|
|
|
}
|
2008-04-29 16:19:50 +05:30
|
|
|
free(save);
|
2008-04-29 15:55:45 +05:30
|
|
|
}
|
|
|
|
if (!service)
|
|
|
|
service = xstrdup(path);
|
|
|
|
applet = basename_c(service);
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
usage(EXIT_FAILURE);
|
|
|
|
|
2007-06-26 14:17:46 +05:30
|
|
|
/* Change dir to / to ensure all init scripts don't use stuff in pwd */
|
2009-05-01 00:26:43 +05:30
|
|
|
if (chdir("/") == -1)
|
|
|
|
eerror("chdir: %s", strerror(errno));
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2008-12-23 16:10:27 +05:30
|
|
|
if ((runlevel = xstrdup(getenv("RC_RUNLEVEL"))) == NULL) {
|
2008-03-16 22:30:56 +05:30
|
|
|
env_filter();
|
|
|
|
env_config();
|
|
|
|
runlevel = rc_runlevel_get();
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
setenv("EINFO_LOG", service, 1);
|
2008-03-20 01:27:24 +05:30
|
|
|
setenv("RC_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 */
|
2008-04-06 18:54:10 +05:30
|
|
|
snprintf(pidstr, sizeof(pidstr), "%d", (int) getpid());
|
|
|
|
setenv("RC_RUNSCRIPT_PID", pidstr, 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 */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (rc_conf_yesno("rc_parallel")) {
|
2007-07-06 21:20:40 +05:30
|
|
|
/* Get the longest service name */
|
2008-03-16 22:30:56 +05:30
|
|
|
services = rc_services_in_runlevel(NULL);
|
2008-10-10 14:07:21 +05:30
|
|
|
TAILQ_FOREACH(svc, services, entries) {
|
|
|
|
ll = strlen(svc->value);
|
|
|
|
if (ll > l)
|
|
|
|
l = ll;
|
|
|
|
}
|
|
|
|
rc_stringlist_free(services);
|
|
|
|
services = NULL;
|
|
|
|
ll = strlen(applet);
|
|
|
|
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-03-16 22:30:56 +05:30
|
|
|
prefix = xmalloc(sizeof(char) * l + 1);
|
|
|
|
ll = strlen(applet);
|
|
|
|
memcpy(prefix, applet, ll);
|
|
|
|
memset(prefix + ll, ' ', l - ll);
|
|
|
|
memset(prefix + l, 0, 1);
|
|
|
|
eprefix(prefix);
|
2007-04-25 18:00:24 +05:30
|
|
|
}
|
|
|
|
|
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 */
|
2008-03-16 22:30:56 +05:30
|
|
|
setup_selinux(argc, argv);
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2009-05-01 04:12:01 +05:30
|
|
|
deps = true;
|
|
|
|
|
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 */
|
2008-10-10 14:07:21 +05:30
|
|
|
while ((opt = getopt_long(argc, argv, getoptstring,
|
2009-04-24 03:01:22 +05:30
|
|
|
longopts, (int *)0)) != -1)
|
2007-05-14 17:54:18 +05:30
|
|
|
switch (opt) {
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'd':
|
2009-01-13 16:34:37 +05:30
|
|
|
setenv("RC_DEBUG", "YES", 1);
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
2009-04-18 04:25:11 +05:30
|
|
|
case 'l':
|
|
|
|
exclusive_fd = atoi(optarg);
|
2009-04-19 01:24:04 +05:30
|
|
|
fcntl(exclusive_fd, F_SETFD,
|
|
|
|
fcntl(exclusive_fd, F_GETFD, 0) | FD_CLOEXEC);
|
2009-04-18 04:25:11 +05:30
|
|
|
break;
|
2008-03-16 22:30:56 +05:30
|
|
|
case 's':
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!(rc_service_state(service) & RC_SERVICE_STARTED))
|
2008-03-16 22:30:56 +05:30
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
deps = false;
|
|
|
|
break;
|
2009-05-01 04:12:01 +05:30
|
|
|
case 'Z':
|
|
|
|
dry_run = true;
|
|
|
|
break;
|
|
|
|
case_RC_COMMON_GETOPT;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2009-04-24 01:50:17 +05:30
|
|
|
/* If we're changing runlevels and not called by rc then we cannot
|
|
|
|
work with any dependencies */
|
|
|
|
if (deps && getenv("RC_PID") == NULL &&
|
|
|
|
(rc_runlevel_starting() || rc_runlevel_stopping()))
|
|
|
|
deps = false;
|
|
|
|
|
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 */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (getenv("IN_BACKGROUND")) {
|
|
|
|
ibsave = xstrdup(getenv("IN_BACKGROUND"));
|
|
|
|
in_background = rc_yesno(ibsave);
|
|
|
|
unsetenv("IN_BACKGROUND");
|
2007-04-12 16:10:51 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (rc_yesno(getenv("IN_HOTPLUG"))) {
|
2008-10-10 14:38:59 +05:30
|
|
|
if (!service_plugable())
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("%s: not allowed to be hotplugged", applet);
|
2009-05-01 04:12:01 +05:30
|
|
|
in_background = true;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup a signal handler */
|
2008-03-16 22:30:56 +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 */
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_load();
|
|
|
|
|
|
|
|
applet_list = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(applet_list, applet);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* 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. */
|
2008-03-16 22:30:56 +05:30
|
|
|
unsetenv("RC_CMD");
|
|
|
|
setenv("RC_CMD", optarg, 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
doneone = true;
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
if (strcmp(optarg, "describe") == 0 ||
|
|
|
|
strcmp(optarg, "help") == 0)
|
2007-12-20 23:09:13 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
save = prefix;
|
|
|
|
eprefix(NULL);
|
2007-12-18 23:31:05 +05:30
|
|
|
prefix = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
svc_exec(optarg, NULL);
|
|
|
|
eprefix(save);
|
2008-04-29 16:19:50 +05:30
|
|
|
prefix = save;
|
2008-03-16 22:30:56 +05:30
|
|
|
} else if (strcmp(optarg, "ineed") == 0 ||
|
2009-04-24 03:01:22 +05:30
|
|
|
strcmp(optarg, "iuse") == 0 ||
|
|
|
|
strcmp(optarg, "needsme") == 0 ||
|
|
|
|
strcmp(optarg, "usesme") == 0 ||
|
|
|
|
strcmp(optarg, "iafter") == 0 ||
|
|
|
|
strcmp(optarg, "ibefore") == 0 ||
|
|
|
|
strcmp(optarg, "iprovide") == 0)
|
2008-01-18 16:57:49 +05:30
|
|
|
{
|
2008-04-29 15:55:45 +05:30
|
|
|
errno = 0;
|
2008-10-10 14:07:21 +05:30
|
|
|
if (rc_conf_yesno("rc_depend_strict") ||
|
|
|
|
errno == ENOENT)
|
2007-07-21 18:19:51 +05:30
|
|
|
depoptions |= RC_DEP_STRICT;
|
2007-11-01 04:04:26 +05:30
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!deptree &&
|
2009-01-13 05:23:13 +05:30
|
|
|
((deptree = _rc_deptree_load(0, NULL)) == NULL))
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("failed to load deptree");
|
|
|
|
|
|
|
|
tmplist = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(tmplist, optarg);
|
2008-10-10 14:07:21 +05:30
|
|
|
services = rc_deptree_depends(deptree, tmplist,
|
2009-04-24 03:01:22 +05:30
|
|
|
applet_list,
|
|
|
|
runlevel, depoptions);
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_free(tmplist);
|
2008-10-10 14:07:21 +05:30
|
|
|
TAILQ_FOREACH(svc, services, entries)
|
2009-04-24 03:01:22 +05:30
|
|
|
printf("%s ", svc->value);
|
2008-10-10 14:07:21 +05:30
|
|
|
printf ("\n");
|
|
|
|
rc_stringlist_free(services);
|
|
|
|
services = NULL;
|
2007-07-11 00:39:41 +05:30
|
|
|
} else if (strcmp (optarg, "status") == 0) {
|
2009-04-18 05:39:03 +05:30
|
|
|
save = prefix;
|
|
|
|
eprefix(NULL);
|
|
|
|
prefix = NULL;
|
|
|
|
retval = svc_exec("status", NULL);
|
2007-07-11 00:39:41 +05:30
|
|
|
} else {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strcmp(optarg, "conditionalrestart") == 0 ||
|
|
|
|
strcmp(optarg, "condrestart") == 0)
|
2007-07-11 00:39:41 +05:30
|
|
|
{
|
2008-10-10 14:07:21 +05:30
|
|
|
if (rc_service_state(service) &
|
|
|
|
RC_SERVICE_STARTED)
|
2009-05-01 04:12:01 +05:30
|
|
|
svc_restart();
|
2008-03-16 22:30:56 +05:30
|
|
|
} else if (strcmp(optarg, "restart") == 0) {
|
2009-05-01 04:12:01 +05:30
|
|
|
svc_restart();
|
2008-03-16 22:30:56 +05:30
|
|
|
} else if (strcmp(optarg, "start") == 0) {
|
2009-05-01 04:12:01 +05:30
|
|
|
svc_start();
|
2008-03-16 22:30:56 +05:30
|
|
|
} else if (strcmp(optarg, "stop") == 0) {
|
2007-08-28 18:36:44 +05:30
|
|
|
if (deps && in_background)
|
2008-03-16 22:30:56 +05:30
|
|
|
get_started_services();
|
2009-05-01 04:12:01 +05:30
|
|
|
svc_stop();
|
2007-08-28 18:36:44 +05:30
|
|
|
if (deps) {
|
2009-05-01 04:12:01 +05:30
|
|
|
if (!in_background &&
|
|
|
|
!rc_runlevel_stopping() &&
|
2008-10-10 14:07:21 +05:30
|
|
|
rc_service_state(service) &
|
|
|
|
RC_SERVICE_STOPPED)
|
2008-10-10 14:38:59 +05:30
|
|
|
unhotplug();
|
2007-08-28 18:36:44 +05:30
|
|
|
|
|
|
|
if (in_background &&
|
2008-10-10 14:07:21 +05:30
|
|
|
rc_service_state(service) &
|
|
|
|
RC_SERVICE_INACTIVE)
|
2007-08-28 18:36:44 +05:30
|
|
|
{
|
2008-10-10 14:07:21 +05:30
|
|
|
TAILQ_FOREACH(svc,
|
2009-04-24 03:01:22 +05:30
|
|
|
restart_services,
|
|
|
|
entries)
|
|
|
|
if (rc_service_state(svc->value) &
|
|
|
|
RC_SERVICE_STOPPED)
|
|
|
|
rc_service_schedule_start(service, svc->value);
|
2007-08-28 18:36:44 +05:30
|
|
|
}
|
2007-07-11 00:39:41 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
} else if (strcmp(optarg, "zap") == 0) {
|
2008-10-10 14:07:21 +05:30
|
|
|
einfo("Manually resetting %s to stopped state",
|
2009-04-24 03:01:22 +05:30
|
|
|
applet);
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!rc_service_mark(applet,
|
2009-04-24 03:01:22 +05:30
|
|
|
RC_SERVICE_STOPPED))
|
2008-10-10 14:07:21 +05:30
|
|
|
eerrorx("rc_service_mark: %s",
|
2009-04-24 03:01:22 +05:30
|
|
|
strerror(errno));
|
2008-10-10 14:38:59 +05:30
|
|
|
unhotplug();
|
2007-07-11 01:41:42 +05:30
|
|
|
} else
|
2008-03-16 22:30:56 +05:30
|
|
|
svc_exec(optarg, NULL);
|
2007-07-11 00:39:41 +05:30
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
/* We should ensure this list is empty after
|
|
|
|
* an action is done */
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_free(restart_services);
|
2007-07-11 00:39:41 +05:30
|
|
|
restart_services = NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-10 14:07:21 +05:30
|
|
|
if (!doneone)
|
2008-03-16 22:30:56 +05:30
|
|
|
usage(EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return retval;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|