2007-04-05 16:48:42 +05:30
|
|
|
/*
|
2008-01-29 19:22:54 +05:30
|
|
|
* rc.c
|
|
|
|
* rc - manager for init scripts which control the startup, shutdown
|
|
|
|
* and the running of daemons.
|
|
|
|
*
|
|
|
|
* Also a multicall binary for various commands that can be used in shell
|
|
|
|
* scripts to query service state, mark service state and provide the
|
|
|
|
* einfo family of informational functions.
|
|
|
|
*/
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-01-14 10:35:22 +05:30
|
|
|
/*
|
2008-03-26 23:23:37 +05:30
|
|
|
* Copyright 2007-2008 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-01-04 16:36:58 +05:30
|
|
|
const char rc_copyright[] = "Copyright (c) 2007-2008 Roy Marples";
|
2007-11-19 22:11:36 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <sys/types.h>
|
2007-10-31 21:16:56 +05:30
|
|
|
#include <sys/ioctl.h>
|
2008-02-01 06:02:50 +05:30
|
|
|
#include <sys/param.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <sys/wait.h>
|
2008-03-16 22:30:56 +05:30
|
|
|
|
|
|
|
/* So we can coldplug net devices */
|
|
|
|
#ifdef BSD
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <ifaddrs.h>
|
|
|
|
#endif
|
|
|
|
|
2008-03-18 02:57:37 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
# include <asm/setup.h> /* for COMMAND_LINE_SIZE */
|
|
|
|
#endif
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <errno.h>
|
2007-10-05 15:46:14 +05:30
|
|
|
#include <dirent.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <ctype.h>
|
2007-06-28 21:14:14 +05:30
|
|
|
#include <getopt.h>
|
2008-03-16 22:30:56 +05:30
|
|
|
#include <libgen.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
2007-10-09 18:22:09 +05:30
|
|
|
#include <strings.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
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"
|
2007-10-31 21:16:56 +05:30
|
|
|
#include "rc-logger.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-11-19 22:11:36 +05:30
|
|
|
#include "version.h"
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
#define INITSH RC_LIBDIR "/sh/init.sh"
|
|
|
|
#define INITEARLYSH RC_LIBDIR "/sh/init-early.sh"
|
|
|
|
#define HALTSH RC_INITDIR "/halt.sh"
|
2007-08-28 14:47:04 +05:30
|
|
|
|
|
|
|
#define SHUTDOWN "/sbin/shutdown"
|
2007-05-03 19:16:38 +05:30
|
|
|
#define SULOGIN "/sbin/sulogin"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
#define INTERACTIVE RC_SVCDIR "/interactive"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
#define DEVBOOT "/dev/.rcboot"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-11 18:32:05 +05:30
|
|
|
static char *RUNLEVEL = NULL;
|
|
|
|
static char *PREVLEVEL = NULL;
|
|
|
|
|
2008-01-14 19:54:20 +05:30
|
|
|
const char *applet = NULL;
|
2007-09-29 22:50:52 +05:30
|
|
|
static char *runlevel = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
static RC_STRINGLIST *coldplugged_services = NULL;
|
|
|
|
static RC_STRINGLIST *stop_services = NULL;
|
|
|
|
static RC_STRINGLIST *start_services = NULL;
|
|
|
|
static RC_STRINGLIST *types_n = NULL;
|
|
|
|
static RC_STRINGLIST *types_nua = NULL;
|
|
|
|
static RC_DEPTREE *deptree = NULL;
|
|
|
|
static RC_HOOK hook_out = 0;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-20 18:42:21 +05:30
|
|
|
struct termios *termios_orig = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PIDLIST service_pids;
|
2007-10-15 20:10:53 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void clean_failed(void)
|
2007-12-30 00:29:24 +05:30
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t l;
|
2007-12-30 00:29:24 +05:30
|
|
|
char *path;
|
|
|
|
|
|
|
|
/* Clean the failed services state dir now */
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((dp = opendir(RC_SVCDIR "/failed"))) {
|
|
|
|
while ((d = readdir(dp))) {
|
2007-12-30 00:29:24 +05:30
|
|
|
if (d->d_name[0] == '.' &&
|
2008-01-11 21:21:40 +05:30
|
|
|
(d->d_name[1] == '\0' ||
|
|
|
|
(d->d_name[1] == '.' && d->d_name[2] == '\0')))
|
2007-12-30 00:29:24 +05:30
|
|
|
continue;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
l = strlen(RC_SVCDIR "/failed/") + strlen(d->d_name) + 1;
|
|
|
|
path = xmalloc(sizeof(char) * l);
|
|
|
|
snprintf(path, l, RC_SVCDIR "/failed/%s", d->d_name);
|
2007-12-30 00:29:24 +05:30
|
|
|
if (path) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (unlink(path))
|
|
|
|
eerror("%s: unlink `%s': %s", applet, path,
|
|
|
|
strerror(errno));
|
|
|
|
free(path);
|
2007-12-30 00:29:24 +05:30
|
|
|
}
|
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
closedir(dp);
|
2007-12-30 00:29:24 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void cleanup(void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
if (applet && strcmp(applet, "rc") == 0) {
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PID *p1 = LIST_FIRST(&service_pids);
|
|
|
|
RC_PID *p2;
|
2007-04-21 00:28:42 +05:30
|
|
|
|
2008-03-05 02:11:25 +05:30
|
|
|
if (hook_out)
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(hook_out, runlevel);
|
2008-03-05 02:11:25 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_unload();
|
2008-01-11 21:21:40 +05:30
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
if (! rc_in_plugin && termios_orig) {
|
2008-03-16 22:30:56 +05:30
|
|
|
tcsetattr(fileno(stdin), TCSANOW, termios_orig);
|
|
|
|
free(termios_orig);
|
2007-07-31 21:35:56 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
while (p1) {
|
|
|
|
p2 = LIST_NEXT(p1, entries);
|
|
|
|
free(p1);
|
|
|
|
p1 = p2;
|
2007-07-31 21:35:56 +05:30
|
|
|
}
|
2007-04-21 00:28:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_free(coldplugged_services);
|
|
|
|
rc_stringlist_free(stop_services);
|
|
|
|
rc_stringlist_free(start_services);
|
|
|
|
rc_stringlist_free(types_n);
|
|
|
|
rc_stringlist_free(types_nua);
|
|
|
|
rc_deptree_free(deptree);
|
2007-07-31 21:35:56 +05:30
|
|
|
|
|
|
|
/* Clean runlevel start, stop markers */
|
2007-10-31 21:16:56 +05:30
|
|
|
if (! rc_in_plugin && ! rc_in_logger) {
|
2008-03-16 22:30:56 +05:30
|
|
|
rmdir(RC_STARTING);
|
|
|
|
rmdir(RC_STOPPING);
|
|
|
|
clean_failed();
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_logger_close();
|
2007-07-31 21:35:56 +05:30
|
|
|
}
|
2007-09-29 22:50:52 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
free(runlevel);
|
2007-07-04 21:32:01 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-07-23 17:06:12 +05:30
|
|
|
#ifdef __linux__
|
2008-03-16 22:30:56 +05:30
|
|
|
static char *proc_getent(const char *ent)
|
2007-07-23 17:06:12 +05:30
|
|
|
{
|
|
|
|
FILE *fp;
|
2008-03-18 02:57:37 +05:30
|
|
|
char proc[COMMAND_LINE_SIZE];
|
2007-07-23 17:06:12 +05:30
|
|
|
char *p;
|
|
|
|
char *value = NULL;
|
|
|
|
int i;
|
2007-08-22 19:40:46 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (! exists("/proc/cmdline"))
|
|
|
|
return NULL;
|
2007-08-22 19:40:46 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (! (fp = fopen("/proc/cmdline", "r"))) {
|
|
|
|
eerror("failed to open `/proc/cmdline': %s", strerror(errno));
|
|
|
|
return NULL;
|
2007-07-23 17:06:12 +05:30
|
|
|
}
|
|
|
|
|
2008-03-18 02:57:37 +05:30
|
|
|
memset(proc, 0, sizeof(proc));
|
|
|
|
fgets(proc, sizeof(proc), fp);
|
|
|
|
if (*proc && (p = strstr(proc, ent))) {
|
2008-01-07 17:59:30 +05:30
|
|
|
i = p - proc;
|
|
|
|
if (i == '\0' || proc[i - 1] == ' ') {
|
2008-03-16 22:30:56 +05:30
|
|
|
p += strlen(ent);
|
2007-07-23 17:06:12 +05:30
|
|
|
if (*p == '=')
|
|
|
|
p++;
|
2008-03-16 22:30:56 +05:30
|
|
|
value = xstrdup(strsep(&p, " "));
|
2007-07-23 17:06:12 +05:30
|
|
|
}
|
|
|
|
} else
|
|
|
|
errno = ENOENT;
|
2008-03-16 22:30:56 +05:30
|
|
|
fclose(fp);
|
2007-07-23 17:06:12 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return value;
|
2007-07-23 17:06:12 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static char read_key(bool block)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct termios termios;
|
|
|
|
char c = 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
int fd = fileno(stdin);
|
2007-10-05 15:46:14 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (! isatty(fd))
|
|
|
|
return false;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Now save our terminal settings. We need to restore them at exit as we
|
2008-01-29 19:22:54 +05:30
|
|
|
* will be changing it for non-blocking reads for Interactive */
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! termios_orig) {
|
2008-03-16 22:30:56 +05:30
|
|
|
termios_orig = xmalloc(sizeof(*termios_orig));
|
|
|
|
tcgetattr(fd, termios_orig);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
tcgetattr(fd, &termios);
|
2007-04-11 18:14:47 +05:30
|
|
|
termios.c_lflag &= ~(ICANON | ECHO);
|
|
|
|
if (block)
|
|
|
|
termios.c_cc[VMIN] = 1;
|
|
|
|
else {
|
|
|
|
termios.c_cc[VMIN] = 0;
|
|
|
|
termios.c_cc[VTIME] = 0;
|
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
tcsetattr(fd, TCSANOW, &termios);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
read(fd, &c, 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
tcsetattr(fd, TCSANOW, termios_orig);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return c;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static bool want_interactive(void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-05-14 18:18:37 +05:30
|
|
|
char c;
|
2007-11-23 17:34:11 +05:30
|
|
|
static bool gotinteractive;
|
|
|
|
static bool interactive;
|
2007-05-14 18:18:37 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (rc_yesno(getenv("EINFO_QUIET")))
|
|
|
|
return false;
|
2007-12-06 16:46:27 +05:30
|
|
|
|
2007-07-11 18:32:05 +05:30
|
|
|
if (PREVLEVEL &&
|
2008-03-16 22:30:56 +05:30
|
|
|
strcmp(PREVLEVEL, "N") != 0 &&
|
|
|
|
strcmp(PREVLEVEL, "S") != 0 &&
|
|
|
|
strcmp(PREVLEVEL, "1") != 0)
|
|
|
|
return false;
|
2007-07-11 18:32:05 +05:30
|
|
|
|
2007-11-23 17:34:11 +05:30
|
|
|
if (! gotinteractive) {
|
|
|
|
gotinteractive = true;
|
2008-03-16 22:30:56 +05:30
|
|
|
interactive = rc_conf_yesno("rc_interactive");
|
2007-11-23 17:34:11 +05:30
|
|
|
}
|
|
|
|
if (! interactive)
|
2008-03-16 22:30:56 +05:30
|
|
|
return false;
|
2007-05-14 18:18:37 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
c = read_key(false);
|
|
|
|
return (c == 'I' || c == 'i') ? true : false;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void mark_interactive(void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
FILE *fp = fopen(INTERACTIVE, "w");
|
2007-04-11 18:14:47 +05:30
|
|
|
if (fp)
|
2008-03-16 22:30:56 +05:30
|
|
|
fclose(fp);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-04-28 21:34:16 +05:30
|
|
|
static void run_program(const char *prog)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2008-02-05 03:49:40 +05:30
|
|
|
struct sigaction sa;
|
|
|
|
sigset_t full;
|
|
|
|
sigset_t old;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
/* We need to block signals until we have forked */
|
2008-03-16 22:30:56 +05:30
|
|
|
memset(&sa, 0, sizeof(sa));
|
2008-02-05 03:49:40 +05:30
|
|
|
sa.sa_handler = SIG_DFL;
|
2008-03-16 22:30:56 +05:30
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigfillset(&full);
|
|
|
|
sigprocmask(SIG_SETMASK, &full, &old);
|
2008-04-28 21:34:16 +05:30
|
|
|
pid = fork();
|
2008-02-05 03:49:40 +05:30
|
|
|
|
|
|
|
if (pid == -1)
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("%s: fork: %s", applet, strerror(errno));
|
2008-02-05 03:49:40 +05:30
|
|
|
if (pid == 0) {
|
|
|
|
/* Restore default handlers */
|
2008-03-16 22:30:56 +05:30
|
|
|
sigaction(SIGCHLD, &sa, NULL);
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
sigaction(SIGQUIT, &sa, NULL);
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
sigaction(SIGUSR1, &sa, NULL);
|
|
|
|
sigaction(SIGWINCH, &sa, NULL);
|
2008-02-05 03:49:40 +05:30
|
|
|
|
|
|
|
/* Unmask signals */
|
2008-03-16 22:30:56 +05:30
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
2008-02-05 03:49:40 +05:30
|
|
|
|
|
|
|
if (termios_orig)
|
2008-03-16 22:30:56 +05:30
|
|
|
tcsetattr(fileno(stdin), TCSANOW, termios_orig);
|
2008-02-05 03:49:40 +05:30
|
|
|
|
2008-04-28 21:34:16 +05:30
|
|
|
execl(prog, prog, (char *) NULL);
|
|
|
|
eerror("%s: unable to exec `%s': %s", applet, prog,
|
2008-03-16 22:30:56 +05:30
|
|
|
strerror(errno));
|
|
|
|
_exit(EXIT_FAILURE);
|
2008-02-05 03:49:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Unmask signals and wait for child */
|
2008-03-16 22:30:56 +05:30
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
2008-04-28 21:34:16 +05:30
|
|
|
if (rc_waitpid(pid) == -1)
|
|
|
|
eerrorx("%s: failed to exec `%s'", applet, prog);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sulogin(bool cont)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
const char *sys = rc_sys();
|
|
|
|
|
|
|
|
/* VSERVER and OPENVZ systems cannot do a sulogin */
|
|
|
|
if (sys && (strcmp(sys, "VSERVER") == 0 || strcmp(sys, "OPENVZ") == 0)) {
|
|
|
|
execl("/sbin/halt", "/sbin/halt", "-f", (char *) NULL);
|
|
|
|
eerrorx("%s: unable to exec `/sbin/halt': %s",
|
|
|
|
applet, strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (! cont) {
|
|
|
|
rc_logger_close();
|
|
|
|
#ifdef __linux__
|
|
|
|
execl("/sbin/sulogin", "/sbin/sulogin", (char *) NULL);
|
|
|
|
eerrorx("%s: unable to exec `/sbin/sulogin': %s",
|
|
|
|
applet, strerror(errno));
|
|
|
|
#else
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
run_program(SULOGIN);
|
|
|
|
#else
|
|
|
|
run_program("/bin/sh");
|
|
|
|
#endif
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void single_user(void)
|
2007-04-20 18:42:21 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_logger_close();
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
execl(SHUTDOWN, SHUTDOWN, "now", (char *) NULL);
|
|
|
|
eerrorx("%s: unable to exec `" SHUTDOWN "': %s",
|
|
|
|
applet, strerror(errno));
|
2007-04-20 18:42:21 +05:30
|
|
|
}
|
|
|
|
|
2008-03-21 16:04:09 +05:30
|
|
|
static bool set_krunlevel(const char *level)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
|
2007-09-29 22:50:52 +05:30
|
|
|
if (! level ||
|
2008-03-16 22:30:56 +05:30
|
|
|
strcmp(level, getenv ("RC_BOOTLEVEL")) == 0 ||
|
|
|
|
strcmp(level, RC_LEVEL_SINGLE) == 0 ||
|
|
|
|
strcmp(level, RC_LEVEL_SYSINIT) == 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
2008-03-19 22:41:50 +05:30
|
|
|
if (exists(RC_KRUNLEVEL) &&
|
|
|
|
unlink(RC_KRUNLEVEL) != 0)
|
|
|
|
eerror("unlink `%s': %s", RC_KRUNLEVEL, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
return false;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-19 22:41:50 +05:30
|
|
|
if (! (fp = fopen(RC_KRUNLEVEL, "w"))) {
|
|
|
|
eerror("fopen `%s': %s", RC_KRUNLEVEL, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
return false;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
fprintf(fp, "%s", level);
|
|
|
|
fclose(fp);
|
|
|
|
return true;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-03-21 16:04:09 +05:30
|
|
|
static int get_krunlevel(char *buffer, int buffer_len)
|
2007-07-10 17:12:56 +05:30
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int i = 0;
|
|
|
|
|
2008-03-19 22:41:50 +05:30
|
|
|
if (! exists(RC_KRUNLEVEL))
|
2008-03-16 22:30:56 +05:30
|
|
|
return 0;
|
2007-07-10 17:12:56 +05:30
|
|
|
|
2008-03-19 22:41:50 +05:30
|
|
|
if (! (fp = fopen(RC_KRUNLEVEL, "r"))) {
|
|
|
|
eerror("fopen `%s': %s", RC_KRUNLEVEL, strerror(errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
return -1;
|
2007-07-10 17:12:56 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (fgets(buffer, buffer_len, fp)) {
|
|
|
|
i = strlen(buffer) - 1;
|
2007-07-10 17:12:56 +05:30
|
|
|
if (buffer[i] == '\n')
|
|
|
|
buffer[i] = 0;
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
fclose(fp);
|
|
|
|
return i;
|
2007-07-10 17:12:56 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void add_pid(pid_t pid)
|
2007-04-21 00:28:42 +05:30
|
|
|
{
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PID *p = xmalloc(sizeof(*p));
|
2008-03-16 22:30:56 +05:30
|
|
|
p->pid = pid;
|
|
|
|
LIST_INSERT_HEAD(&service_pids, p, entries);
|
2007-04-21 00:28:42 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void remove_pid(pid_t pid)
|
2007-04-21 00:28:42 +05:30
|
|
|
{
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PID *p;
|
2007-04-21 00:28:42 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
LIST_FOREACH(p, &service_pids, entries)
|
|
|
|
if (p->pid == pid) {
|
|
|
|
LIST_REMOVE(p, entries);
|
|
|
|
free(p);
|
|
|
|
return;
|
2007-04-21 00:28:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void wait_for_services(void)
|
2007-10-31 21:16:56 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
while (waitpid(0, 0, 0) != -1);
|
2007-10-31 21:16:56 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void handle_signal(int sig)
|
2007-04-09 22:23:21 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int serrno = errno;
|
|
|
|
char signame[10] = { '\0' };
|
2007-04-21 00:28:42 +05:30
|
|
|
pid_t pid;
|
2008-03-17 15:29:52 +05:30
|
|
|
RC_PID *pi;
|
2007-04-21 00:28:42 +05:30
|
|
|
int status = 0;
|
2007-10-31 21:16:56 +05:30
|
|
|
struct winsize ws;
|
2008-02-01 18:50:19 +05:30
|
|
|
sigset_t sset;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
switch (sig) {
|
2008-03-16 22:30:56 +05:30
|
|
|
case SIGCHLD:
|
|
|
|
do {
|
|
|
|
pid = waitpid(-1, &status, WNOHANG);
|
|
|
|
if (pid < 0) {
|
|
|
|
if (errno != ECHILD)
|
|
|
|
eerror("waitpid: %s", strerror(errno));
|
|
|
|
return;
|
2007-10-31 21:16:56 +05:30
|
|
|
}
|
2008-04-26 20:27:39 +05:30
|
|
|
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
/* Remove that pid from our list */
|
|
|
|
if (pid > 0)
|
|
|
|
remove_pid(pid);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGWINCH:
|
|
|
|
if (rc_logger_tty >= 0) {
|
|
|
|
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
|
|
|
|
ioctl(rc_logger_tty, TIOCSWINSZ, &ws);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGINT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf(signame, sizeof(signame), "SIGINT");
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SIGTERM:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf(signame, sizeof(signame), "SIGTERM");
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SIGQUIT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf(signame, sizeof(signame), "SIGQUIT");
|
|
|
|
eerrorx("%s: caught %s, aborting", applet, signame);
|
|
|
|
/* NOTREACHED */
|
|
|
|
case SIGUSR1:
|
|
|
|
eerror("rc: Aborting!");
|
|
|
|
|
|
|
|
/* Block child signals */
|
|
|
|
sigemptyset(&sset);
|
|
|
|
sigaddset(&sset, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &sset, NULL);
|
|
|
|
|
|
|
|
/* Kill any running services we have started */
|
|
|
|
LIST_FOREACH(pi, &service_pids, entries)
|
|
|
|
kill(pi->pid, SIGTERM);
|
|
|
|
|
|
|
|
/* Notify plugins we are aborting */
|
|
|
|
rc_plugin_run(RC_HOOK_ABORT, NULL);
|
|
|
|
|
|
|
|
/* Only drop into single user mode if we're booting */
|
|
|
|
if ((PREVLEVEL &&
|
|
|
|
(strcmp(PREVLEVEL, "S") == 0 ||
|
|
|
|
strcmp(PREVLEVEL, "1") == 0)) ||
|
|
|
|
(RUNLEVEL &&
|
|
|
|
(strcmp(RUNLEVEL, "S") == 0 ||
|
|
|
|
strcmp(RUNLEVEL, "1") == 0)))
|
|
|
|
single_user();
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
default:
|
|
|
|
eerror("%s: caught unknown signal %d", applet, sig);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore errno */
|
|
|
|
errno = serrno;
|
2007-04-09 22:23:21 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void do_coldplug(void)
|
2008-01-29 19:22:54 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
size_t l;
|
2008-01-29 19:22:54 +05:30
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
|
|
|
char *service;
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRING *s;
|
2008-03-11 16:33:34 +05:30
|
|
|
#ifdef BSD
|
|
|
|
struct ifaddrs *ifap;
|
|
|
|
struct ifaddrs *ifa;
|
2008-03-16 22:30:56 +05:30
|
|
|
char *p;
|
2008-03-11 16:33:34 +05:30
|
|
|
#endif
|
2008-01-29 19:22:54 +05:30
|
|
|
|
2008-03-24 18:39:42 +05:30
|
|
|
errno = 0;
|
|
|
|
if (!rc_conf_yesno("rc_coldplug") && errno != ENOENT)
|
2008-01-29 19:22:54 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
/* We need to ensure our state dirs exist.
|
|
|
|
* We should have a better call than this, but oh well. */
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_deptree_update_needed();
|
2008-01-29 19:22:54 +05:30
|
|
|
|
|
|
|
#ifdef BSD
|
2008-03-11 16:33:34 +05:30
|
|
|
if (getifaddrs(&ifap) == 0) {
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_LINK)
|
|
|
|
continue;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
l = strlen("net.") + strlen(ifa->ifa_name) + 1;
|
|
|
|
service = xmalloc(sizeof (char) * l);
|
|
|
|
snprintf(service, l, "net.%s", ifa->ifa_name);
|
|
|
|
if (rc_service_exists(service) &&
|
|
|
|
service_plugable(service))
|
|
|
|
rc_service_mark(service, RC_SERVICE_COLDPLUGGED);
|
|
|
|
free(service);
|
2008-01-29 19:22:54 +05:30
|
|
|
}
|
2008-03-11 16:33:34 +05:30
|
|
|
freeifaddrs (ifap);
|
2008-01-29 19:22:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* The mice are a little more tricky.
|
|
|
|
* If we coldplug anything else, we'll probably do it here. */
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((dp = opendir("/dev"))) {
|
|
|
|
while ((d = readdir(dp))) {
|
|
|
|
if (strncmp(d->d_name, "psm", 3) == 0 ||
|
|
|
|
strncmp(d->d_name, "ums", 3) == 0)
|
2008-01-29 19:22:54 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
p = d->d_name + 3;
|
2008-04-17 15:49:58 +05:30
|
|
|
if (p && isdigit((unsigned char)*p)) {
|
2008-03-16 22:30:56 +05:30
|
|
|
l = strlen("moused.") + strlen(d->d_name) + 1;
|
|
|
|
service = xmalloc(sizeof(char) * l);
|
|
|
|
snprintf (service, l, "moused.%s", d->d_name);
|
|
|
|
if (rc_service_exists (service) &&
|
|
|
|
service_plugable (service))
|
|
|
|
rc_service_mark (service, RC_SERVICE_COLDPLUGGED);
|
|
|
|
free(service);
|
2008-01-29 19:22:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir (dp);
|
|
|
|
}
|
|
|
|
|
2008-03-11 16:33:34 +05:30
|
|
|
#else
|
|
|
|
|
2008-01-29 19:22:54 +05:30
|
|
|
/* udev likes to start services before we're ready when it does
|
|
|
|
* its coldplugging thing. runscript knows when we're not ready so it
|
|
|
|
* stores a list of coldplugged services in DEVBOOT for us to pick up
|
|
|
|
* here when we are ready for them */
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((dp = opendir(DEVBOOT))) {
|
|
|
|
while ((d = readdir(dp))) {
|
2008-01-29 19:22:54 +05:30
|
|
|
if (d->d_name[0] == '.' &&
|
|
|
|
(d->d_name[1] == '\0' ||
|
|
|
|
(d->d_name[1] == '.' && d->d_name[2] == '\0')))
|
|
|
|
continue;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (rc_service_exists(d->d_name) &&
|
|
|
|
service_plugable(d->d_name))
|
|
|
|
rc_service_mark(d->d_name, RC_SERVICE_COLDPLUGGED);
|
|
|
|
|
|
|
|
l = strlen(DEVBOOT "/") + strlen(d->d_name) + 1;
|
|
|
|
service = xmalloc(sizeof (char) * l);
|
|
|
|
snprintf(service, l, DEVBOOT "/%s", d->d_name);
|
|
|
|
if (unlink(service))
|
|
|
|
eerror("%s: unlink `%s': %s", applet, service,
|
|
|
|
strerror(errno));
|
|
|
|
free(service);
|
2008-01-29 19:22:54 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
closedir(dp);
|
|
|
|
rmdir(DEVBOOT);
|
2008-01-29 19:22:54 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (rc_yesno(getenv("EINFO_QUIET")))
|
2008-01-29 19:22:54 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
/* Load our list of coldplugged services and display them */
|
2008-03-16 22:30:56 +05:30
|
|
|
einfon("Device initiated services:%s", ecolor(ECOLOR_HILITE));
|
|
|
|
coldplugged_services = rc_services_in_state(RC_SERVICE_COLDPLUGGED);
|
2008-03-24 19:00:24 +05:30
|
|
|
if (coldplugged_services)
|
|
|
|
TAILQ_FOREACH(s, coldplugged_services, entries)
|
|
|
|
printf(" %s", s->value);
|
2008-03-16 22:30:56 +05:30
|
|
|
printf ("%s\n", ecolor(ECOLOR_NORMAL));
|
|
|
|
}
|
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
static void do_newlevel(const char *newlevel)
|
|
|
|
{
|
|
|
|
struct utsname uts;
|
|
|
|
const char *sys;
|
|
|
|
#ifdef __linux__
|
|
|
|
char *cmd;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (strcmp(newlevel, RC_LEVEL_SYSINIT) == 0
|
|
|
|
#ifndef PREFIX
|
|
|
|
&& RUNLEVEL &&
|
|
|
|
(strcmp(RUNLEVEL, "S") == 0 ||
|
|
|
|
strcmp(RUNLEVEL, "1") == 0)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
/* OK, we're either in runlevel 1 or single user mode */
|
|
|
|
|
|
|
|
/* exec init-early.sh if it exists
|
|
|
|
* This should just setup the console to use the correct
|
|
|
|
* font. Maybe it should setup the keyboard too? */
|
|
|
|
if (exists(INITEARLYSH))
|
2008-04-28 21:34:16 +05:30
|
|
|
run_program(INITEARLYSH);
|
2008-03-17 18:55:56 +05:30
|
|
|
|
|
|
|
uname(&uts);
|
|
|
|
printf("\n %sOpenRC %s" VERSION "%s is starting up %s",
|
|
|
|
ecolor(ECOLOR_GOOD), ecolor(ECOLOR_HILITE),
|
|
|
|
ecolor(ECOLOR_NORMAL), ecolor(ECOLOR_BRACKET));
|
|
|
|
#ifdef BRANDING
|
|
|
|
printf(BRANDING " (%s)", uts.machine);
|
|
|
|
#else
|
|
|
|
printf("%s %s (%s)",
|
|
|
|
uts.sysname,
|
|
|
|
uts.release,
|
|
|
|
uts.machine);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((sys = rc_sys()))
|
|
|
|
printf(" [%s]", sys);
|
|
|
|
|
|
|
|
printf("%s\n\n", ecolor(ECOLOR_NORMAL));
|
|
|
|
|
|
|
|
if (! rc_yesno(getenv ("EINFO_QUIET")) &&
|
|
|
|
rc_conf_yesno("rc_interactive"))
|
|
|
|
printf("Press %sI%s to enter interactive boot mode\n\n",
|
|
|
|
ecolor(ECOLOR_GOOD), ecolor(ECOLOR_NORMAL));
|
|
|
|
|
2008-03-19 22:41:50 +05:30
|
|
|
setenv("RC_RUNLEVEL", newlevel, 1);
|
2008-03-17 18:55:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_RUNLEVEL_START_IN, newlevel);
|
|
|
|
hook_out = RC_HOOK_RUNLEVEL_START_OUT;
|
2008-04-28 21:34:16 +05:30
|
|
|
run_program(INITSH);
|
2008-03-17 18:55:56 +05:30
|
|
|
|
|
|
|
#ifdef __linux__
|
2008-04-08 21:29:56 +05:30
|
|
|
/* If we requested a runlevel, save it now */
|
|
|
|
if ((cmd = proc_getent("rc_runlevel"))) {
|
2008-03-21 16:04:09 +05:30
|
|
|
set_krunlevel(cmd);
|
2008-03-17 18:55:56 +05:30
|
|
|
free(cmd);
|
2008-04-08 21:29:56 +05:30
|
|
|
} else if ((cmd = proc_getent("softlevel"))) {
|
|
|
|
set_krunlevel(cmd);
|
|
|
|
free(cmd);
|
|
|
|
} else
|
|
|
|
set_krunlevel(NULL);
|
2008-03-17 18:55:56 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Setup our coldplugged services now */
|
|
|
|
do_coldplug();
|
|
|
|
|
|
|
|
rc_plugin_run(RC_HOOK_RUNLEVEL_START_OUT, newlevel);
|
|
|
|
hook_out = 0;
|
|
|
|
|
|
|
|
if (want_interactive())
|
|
|
|
mark_interactive();
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (strcmp(newlevel, RC_LEVEL_SINGLE) == 0) {
|
|
|
|
#ifndef PREFIX
|
|
|
|
if (! RUNLEVEL ||
|
|
|
|
(strcmp(RUNLEVEL, "S") != 0 &&
|
|
|
|
strcmp(RUNLEVEL, "1") != 0))
|
|
|
|
{
|
|
|
|
/* Remember the current runlevel for when we come back */
|
2008-03-21 16:04:09 +05:30
|
|
|
set_krunlevel(runlevel);
|
2008-03-17 18:55:56 +05:30
|
|
|
single_user();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else if (strcmp(newlevel, RC_LEVEL_REBOOT) == 0) {
|
|
|
|
if (! RUNLEVEL ||
|
|
|
|
strcmp(RUNLEVEL, "6") != 0)
|
|
|
|
{
|
|
|
|
rc_logger_close();
|
|
|
|
execl(SHUTDOWN, SHUTDOWN, "-r", "now", (char *) NULL);
|
|
|
|
eerrorx("%s: unable to exec `" SHUTDOWN "': %s",
|
|
|
|
applet, strerror(errno));
|
|
|
|
}
|
|
|
|
} else if (strcmp(newlevel, RC_LEVEL_SHUTDOWN) == 0) {
|
|
|
|
if (! RUNLEVEL ||
|
|
|
|
strcmp(RUNLEVEL, "0") != 0)
|
|
|
|
{
|
|
|
|
rc_logger_close();
|
|
|
|
execl(SHUTDOWN, SHUTDOWN,
|
|
|
|
#ifdef __linux__
|
|
|
|
"-h",
|
|
|
|
#else
|
|
|
|
"-p",
|
|
|
|
#endif
|
|
|
|
"now", (char *) NULL);
|
|
|
|
eerrorx("%s: unable to exec `" SHUTDOWN "': %s",
|
|
|
|
applet, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static bool runlevel_config(const char *service, const char *level)
|
|
|
|
{
|
|
|
|
char *init = rc_service_resolve(service);
|
2008-04-29 16:38:02 +05:30
|
|
|
char *conf, *dir;
|
2008-03-16 22:30:56 +05:30
|
|
|
size_t l;
|
|
|
|
bool retval;
|
|
|
|
|
2008-04-29 16:38:02 +05:30
|
|
|
dir = dirname(init);
|
|
|
|
dir = dirname(init);
|
|
|
|
l = strlen(dir) + strlen(level) + strlen(service) + 10;
|
2008-03-16 22:30:56 +05:30
|
|
|
conf = xmalloc(sizeof(char) * l);
|
2008-04-29 16:38:02 +05:30
|
|
|
snprintf(conf, l, "%s/conf.d/%s.%s", dir, service, level);
|
2008-03-16 22:30:56 +05:30
|
|
|
retval = exists(conf);
|
|
|
|
free(conf);
|
2008-04-29 16:38:02 +05:30
|
|
|
free(init);
|
2008-03-16 22:30:56 +05:30
|
|
|
|
|
|
|
return retval;
|
2008-01-29 19:22:54 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
static void do_stop_services(const char *newlevel, bool going_down, bool parallel)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
RC_STRING *service, *svc1, *svc2;
|
|
|
|
RC_STRINGLIST *deporder, *tmplist;
|
2008-03-28 14:12:05 +05:30
|
|
|
RC_SERVICE state;
|
2008-03-17 18:55:56 +05:30
|
|
|
|
|
|
|
if (! types_n) {
|
|
|
|
types_n = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(types_n, "needsme");
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_FOREACH_REVERSE(service, stop_services, rc_stringlist, entries)
|
|
|
|
{
|
2008-03-28 14:12:05 +05:30
|
|
|
state = rc_service_state(service->value);
|
|
|
|
if (state & RC_SERVICE_STOPPED || state & RC_SERVICE_FAILED)
|
2008-03-17 18:55:56 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We always stop the service when in these runlevels */
|
2008-03-17 20:01:44 +05:30
|
|
|
if (going_down || ! start_services) {
|
2008-03-28 14:12:05 +05:30
|
|
|
pid = service_stop(service->value);
|
2008-03-17 18:55:56 +05:30
|
|
|
if (pid > 0 && ! parallel)
|
|
|
|
rc_waitpid(pid);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're in the start list then don't bother stopping us */
|
|
|
|
TAILQ_FOREACH(svc1, start_services, entries)
|
|
|
|
if (strcmp (svc1->value, service->value) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (svc1) {
|
|
|
|
if (newlevel && strcmp(runlevel, newlevel) != 0) {
|
|
|
|
/* So we're in the start list. But we should
|
|
|
|
* be stopped if we have a runlevel
|
|
|
|
* configuration file for either the current
|
|
|
|
* or next so we use the correct one. */
|
|
|
|
if (! runlevel_config(service->value, runlevel) &&
|
|
|
|
! runlevel_config(service->value, newlevel))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We got this far! Or last check is to see if any any service
|
|
|
|
* that going to be started depends on us */
|
|
|
|
if (! svc1) {
|
|
|
|
tmplist = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(tmplist, service->value);
|
|
|
|
deporder = rc_deptree_depends(deptree, types_n, tmplist,
|
|
|
|
runlevel, RC_DEP_STRICT);
|
|
|
|
rc_stringlist_free(tmplist);
|
|
|
|
svc2 = NULL;
|
|
|
|
TAILQ_FOREACH (svc1, deporder, entries) {
|
|
|
|
TAILQ_FOREACH(svc2, start_services, entries)
|
|
|
|
if (strcmp (svc1->value, svc2->value) == 0)
|
|
|
|
break;
|
|
|
|
if (svc2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc_stringlist_free(deporder);
|
|
|
|
|
|
|
|
if (svc2)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* After all that we can finally stop the blighter! */
|
2008-03-28 14:12:05 +05:30
|
|
|
pid = service_stop(service->value);
|
2008-03-17 18:55:56 +05:30
|
|
|
if (pid > 0) {
|
|
|
|
add_pid(pid);
|
|
|
|
if (! parallel) {
|
|
|
|
rc_waitpid(pid);
|
|
|
|
remove_pid(pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_start_services(bool parallel)
|
|
|
|
{
|
|
|
|
RC_STRING *service;
|
|
|
|
pid_t pid;
|
|
|
|
bool interactive = false;
|
2008-03-28 14:12:05 +05:30
|
|
|
RC_SERVICE state;
|
2008-03-17 18:55:56 +05:30
|
|
|
|
|
|
|
if (! rc_yesno(getenv("EINFO_QUIET")))
|
|
|
|
interactive = exists(INTERACTIVE);
|
|
|
|
|
|
|
|
TAILQ_FOREACH(service, start_services, entries) {
|
2008-03-28 14:12:05 +05:30
|
|
|
state = rc_service_state(service->value);
|
|
|
|
if (!(state & RC_SERVICE_STOPPED) || state & RC_SERVICE_FAILED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (! interactive)
|
|
|
|
interactive = want_interactive();
|
2008-03-17 18:55:56 +05:30
|
|
|
|
2008-03-28 14:12:05 +05:30
|
|
|
if (interactive) {
|
2008-03-17 18:55:56 +05:30
|
|
|
interactive_retry:
|
2008-03-28 14:12:05 +05:30
|
|
|
printf("\n");
|
|
|
|
einfo("About to start the service %s",
|
|
|
|
service->value);
|
|
|
|
eindent();
|
|
|
|
einfo("1) Start the service\t\t2) Skip the service");
|
|
|
|
einfo("3) Continue boot process\t\t4) Exit to shell");
|
|
|
|
eoutdent();
|
2008-03-17 18:55:56 +05:30
|
|
|
interactive_option:
|
2008-03-28 14:12:05 +05:30
|
|
|
switch (read_key(true)) {
|
2008-03-17 18:55:56 +05:30
|
|
|
case '1': break;
|
|
|
|
case '2': continue;
|
|
|
|
case '3': interactive = false; break;
|
|
|
|
case '4': sulogin(true); goto interactive_retry;
|
|
|
|
default: goto interactive_option;
|
|
|
|
}
|
2008-03-28 14:12:05 +05:30
|
|
|
}
|
2008-03-17 18:55:56 +05:30
|
|
|
|
2008-03-28 14:12:05 +05:30
|
|
|
pid = service_start(service->value);
|
2008-03-17 18:55:56 +05:30
|
|
|
|
2008-03-28 14:12:05 +05:30
|
|
|
/* Remember the pid if we're running in parallel */
|
|
|
|
if (pid > 0) {
|
|
|
|
add_pid(pid);
|
|
|
|
|
|
|
|
if (! parallel) {
|
|
|
|
rc_waitpid(pid);
|
|
|
|
remove_pid(pid);
|
2008-03-17 18:55:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store our interactive status for boot */
|
|
|
|
if (interactive && strcmp(runlevel, getenv("RC_BOOTLEVEL")) == 0)
|
|
|
|
mark_interactive();
|
|
|
|
else {
|
|
|
|
if (exists(INTERACTIVE))
|
|
|
|
unlink(INTERACTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-03-24 17:13:03 +05:30
|
|
|
#ifdef RC_DEBUG
|
|
|
|
static void handle_bad_signal(int sig)
|
2008-03-23 04:32:30 +05:30
|
|
|
{
|
2008-03-24 17:13:03 +05:30
|
|
|
char pid[10];
|
|
|
|
int status;
|
2008-03-23 04:32:30 +05:30
|
|
|
pid_t crashed_pid = getpid();
|
|
|
|
|
|
|
|
switch (fork()) {
|
2008-03-24 17:13:03 +05:30
|
|
|
case -1:
|
|
|
|
_exit(sig);
|
|
|
|
/* NOTREACHED */
|
|
|
|
case 0:
|
2008-03-23 04:32:30 +05:30
|
|
|
sprintf(pid, "%i", crashed_pid);
|
|
|
|
printf("\nAuto launching gdb!\n\n");
|
|
|
|
_exit(execlp("gdb", "gdb", "--quiet", "--pid", pid, "-ex", "bt full", NULL));
|
2008-03-24 17:13:03 +05:30
|
|
|
/* NOTREACHED */
|
|
|
|
default:
|
2008-03-23 04:32:30 +05:30
|
|
|
wait(&status);
|
|
|
|
}
|
|
|
|
|
|
|
|
_exit(1);
|
2008-03-24 17:13:03 +05:30
|
|
|
/* NOTREACHED */
|
2008-03-23 04:32:30 +05:30
|
|
|
}
|
2008-03-24 17:13:03 +05:30
|
|
|
#endif
|
2008-03-23 04:32:30 +05:30
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
#include "_usage.h"
|
2007-10-29 21:32:18 +05:30
|
|
|
#define getoptstring "o:" getoptstring_COMMON
|
2008-02-02 01:24:46 +05:30
|
|
|
static const struct option longopts[] = {
|
2007-10-29 21:32:18 +05:30
|
|
|
{ "override", 1, NULL, 'o' },
|
2008-03-19 22:41:50 +05:30
|
|
|
{ "service", 1, NULL, 's' },
|
2008-04-07 01:36:07 +05:30
|
|
|
{ "sys", 0, NULL, 'S' },
|
2007-06-28 21:14:14 +05:30
|
|
|
longopts_COMMON
|
|
|
|
};
|
2007-09-25 21:51:38 +05:30
|
|
|
static const char * const longopts_help[] = {
|
2007-10-29 21:32:18 +05:30
|
|
|
"override the next runlevel to change into\nwhen leaving single user or boot runlevels",
|
2008-03-19 22:41:50 +05:30
|
|
|
"runs the service specified with the rest\nof the arguments",
|
2008-04-07 01:36:07 +05:30
|
|
|
"output the RC system type, if any",
|
2007-09-25 21:51:38 +05:30
|
|
|
longopts_help_COMMON
|
|
|
|
};
|
2007-06-28 21:14:14 +05:30
|
|
|
#include "_usage.c"
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
int main(int argc, char **argv)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
const char *bootlevel = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *newlevel = NULL;
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRINGLIST *deporder = NULL;
|
|
|
|
RC_STRINGLIST *tmplist;
|
|
|
|
RC_STRING *service;
|
2007-04-11 18:14:47 +05:30
|
|
|
bool going_down = false;
|
|
|
|
int depoptions = RC_DEP_STRICT | RC_DEP_TRACE;
|
2008-03-21 16:04:09 +05:30
|
|
|
char krunlevel [PATH_MAX];
|
2008-04-06 18:54:10 +05:30
|
|
|
char pidstr[10];
|
2007-06-28 21:14:14 +05:30
|
|
|
int opt;
|
2007-11-23 17:34:11 +05:30
|
|
|
bool parallel;
|
2007-12-14 17:53:13 +05:30
|
|
|
int regen = 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
char *proc;
|
|
|
|
char *p;
|
|
|
|
char *token;
|
|
|
|
#endif
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-24 17:13:03 +05:30
|
|
|
#ifdef RC_DEBUG
|
|
|
|
signal_setup(SIGBUS, handle_bad_signal);
|
|
|
|
signal_setup(SIGILL, handle_bad_signal);
|
|
|
|
signal_setup(SIGSEGV, handle_bad_signal);
|
|
|
|
#endif
|
2008-03-23 04:32:30 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
applet = basename_c(argv[0]);
|
|
|
|
LIST_INIT(&service_pids);
|
|
|
|
atexit(cleanup);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! applet)
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("arguments required");
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (argc > 1 && (strcmp(argv[1], "--version") == 0)) {
|
|
|
|
printf("%s (OpenRC", applet);
|
2008-03-17 18:55:56 +05:30
|
|
|
if ((bootlevel = rc_sys()))
|
|
|
|
printf(" [%s]", bootlevel);
|
2008-03-16 22:30:56 +05:30
|
|
|
printf(") " VERSION
|
2008-03-05 23:01:19 +05:30
|
|
|
#ifdef BRANDING
|
2008-03-16 22:30:56 +05:30
|
|
|
" (" BRANDING ")"
|
2008-03-05 23:01:19 +05:30
|
|
|
#endif
|
2008-03-16 22:30:56 +05:30
|
|
|
"\n");
|
|
|
|
exit(EXIT_SUCCESS);
|
2007-11-19 22:11:36 +05:30
|
|
|
}
|
|
|
|
|
2008-01-14 19:54:20 +05:30
|
|
|
/* Run our built in applets. If we ran one, we don't return. */
|
2008-03-16 22:30:56 +05:30
|
|
|
run_applets(argc, argv);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-01-15 01:23:56 +05:30
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
2007-06-26 14:50:42 +05:30
|
|
|
/* Change dir to / to ensure all scripts don't use stuff in pwd */
|
2008-03-16 22:30:56 +05:30
|
|
|
chdir("/");
|
2007-06-26 14:50:42 +05:30
|
|
|
|
2007-07-11 18:32:05 +05:30
|
|
|
/* RUNLEVEL is set by sysvinit as is a magic number
|
2008-03-19 22:41:50 +05:30
|
|
|
* RC_RUNLEVEL is set by us and is the name for this magic number */
|
2008-03-16 22:30:56 +05:30
|
|
|
RUNLEVEL = getenv("RUNLEVEL");
|
|
|
|
PREVLEVEL = getenv("PREVLEVEL");
|
2007-07-11 18:32:05 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Ensure our environment is pure
|
2008-01-29 19:22:54 +05:30
|
|
|
* Also, add our configuration to it */
|
2008-03-16 22:30:56 +05:30
|
|
|
env_filter();
|
|
|
|
env_config();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
argc++;
|
|
|
|
argv--;
|
2008-03-16 22:30:56 +05:30
|
|
|
while ((opt = getopt_long(argc, argv, getoptstring,
|
|
|
|
longopts, (int *) 0)) != -1)
|
2007-06-28 21:14:14 +05:30
|
|
|
{
|
|
|
|
switch (opt) {
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'o':
|
|
|
|
if (*optarg == '\0')
|
|
|
|
optarg = NULL;
|
2008-03-21 16:04:09 +05:30
|
|
|
exit(set_krunlevel(optarg) ? EXIT_SUCCESS : EXIT_FAILURE);
|
2008-03-16 22:30:56 +05:30
|
|
|
/* NOTREACHED */
|
2008-03-19 22:41:50 +05:30
|
|
|
case 's':
|
|
|
|
newlevel = rc_service_resolve(optarg);
|
|
|
|
if (!newlevel)
|
|
|
|
eerrorx("%s: service `%s' does not exist",
|
|
|
|
applet, optarg);
|
|
|
|
argv += optind - 1;
|
|
|
|
*argv = newlevel;
|
|
|
|
execv(*argv, argv);
|
|
|
|
eerrorx("%s: %s", applet, strerror(errno));
|
|
|
|
/* NOTREACHED */
|
2008-04-07 01:36:07 +05:30
|
|
|
case 'S':
|
|
|
|
bootlevel = rc_sys();
|
|
|
|
if (bootlevel)
|
|
|
|
printf("%s\n", bootlevel);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
/* NOTREACHED */
|
2008-03-16 22:30:56 +05:30
|
|
|
case_RC_COMMON_GETOPT
|
2007-06-28 21:14:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newlevel = argv[optind++];
|
2007-07-10 17:12:56 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Enable logging */
|
2008-03-16 22:30:56 +05:30
|
|
|
setenv("EINFO_LOG", "rc", 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-20 18:42:21 +05:30
|
|
|
/* Export our PID */
|
2008-03-16 22:30:56 +05:30
|
|
|
snprintf(pidstr, sizeof(pidstr), "%d", getpid());
|
|
|
|
setenv("RC_PID", pidstr, 1);
|
2007-04-20 18:42:21 +05:30
|
|
|
|
2008-04-08 21:29:56 +05:30
|
|
|
/* Load current runlevel */
|
2008-03-16 22:30:56 +05:30
|
|
|
bootlevel = getenv("RC_BOOTLEVEL");
|
|
|
|
runlevel = rc_runlevel_get();
|
2007-07-10 17:12:56 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_logger_open(newlevel ? newlevel : runlevel);
|
2007-10-31 21:16:56 +05:30
|
|
|
|
|
|
|
/* Setup a signal handler */
|
2008-04-28 21:34:16 +05:30
|
|
|
signal_setup(SIGINT, handle_signal);
|
2008-03-16 22:30:56 +05:30
|
|
|
signal_setup(SIGQUIT, handle_signal);
|
|
|
|
signal_setup(SIGTERM, handle_signal);
|
|
|
|
signal_setup(SIGUSR1, handle_signal);
|
|
|
|
signal_setup(SIGWINCH, handle_signal);
|
|
|
|
|
|
|
|
rc_plugin_load();
|
2007-10-31 21:16:56 +05:30
|
|
|
|
2007-07-10 17:12:56 +05:30
|
|
|
/* Check we're in the runlevel requested, ie from
|
2008-01-29 19:22:54 +05:30
|
|
|
* rc single
|
|
|
|
* rc shutdown
|
|
|
|
* rc reboot
|
|
|
|
*/
|
2008-03-17 18:55:56 +05:30
|
|
|
if (newlevel)
|
|
|
|
do_newlevel(newlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-21 00:28:42 +05:30
|
|
|
/* Now we start handling our children */
|
2008-03-16 22:30:56 +05:30
|
|
|
signal_setup(SIGCHLD, handle_signal);
|
2007-04-21 00:28:42 +05:30
|
|
|
|
2008-03-21 16:04:09 +05:30
|
|
|
/* We should only use krunlevel if we were in single user mode
|
|
|
|
* If not, we need to erase krunlevel now. */
|
2007-07-10 17:12:56 +05:30
|
|
|
if (PREVLEVEL &&
|
2008-03-16 22:30:56 +05:30
|
|
|
(strcmp(PREVLEVEL, "1") == 0 ||
|
|
|
|
strcmp(PREVLEVEL, "S") == 0 ||
|
|
|
|
strcmp(PREVLEVEL, "N") == 0))
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
2008-03-21 16:04:09 +05:30
|
|
|
/* Try not to join boot and krunlevels together */
|
2007-08-28 18:36:44 +05:30
|
|
|
if (! newlevel ||
|
2008-03-16 22:30:56 +05:30
|
|
|
strcmp(newlevel, getenv("RC_BOOTLEVEL")) != 0)
|
2008-03-21 16:04:09 +05:30
|
|
|
if (get_krunlevel(krunlevel, sizeof(krunlevel)))
|
|
|
|
newlevel = krunlevel;
|
2007-07-10 17:12:56 +05:30
|
|
|
} else if (! RUNLEVEL ||
|
2008-03-16 22:30:56 +05:30
|
|
|
(strcmp(RUNLEVEL, "1") != 0 &&
|
|
|
|
strcmp(RUNLEVEL, "S") != 0 &&
|
|
|
|
strcmp(RUNLEVEL, "N") != 0))
|
2007-07-10 17:12:56 +05:30
|
|
|
{
|
2008-03-21 16:04:09 +05:30
|
|
|
set_krunlevel(NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (newlevel &&
|
2008-03-16 22:30:56 +05:30
|
|
|
(strcmp(newlevel, RC_LEVEL_REBOOT) == 0 ||
|
|
|
|
strcmp(newlevel, RC_LEVEL_SHUTDOWN) == 0 ||
|
|
|
|
strcmp(newlevel, RC_LEVEL_SINGLE) == 0))
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
|
|
|
going_down = true;
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_runlevel_set(newlevel);
|
|
|
|
setenv("RC_RUNLEVEL", newlevel, 1);
|
2007-10-31 21:16:56 +05:30
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
/* FIXME: we shouldn't have todo this */
|
|
|
|
/* For some reason, wait_for_services waits for the logger proccess
|
|
|
|
* to finish as well, but only on FreeBSD. We cannot allow this so
|
|
|
|
* we stop logging now. */
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_logger_close();
|
2007-10-31 21:16:56 +05:30
|
|
|
#endif
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_RUNLEVEL_STOP_IN, newlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else {
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_RUNLEVEL_STOP_IN, runlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-03-05 02:11:25 +05:30
|
|
|
hook_out = RC_HOOK_RUNLEVEL_STOP_OUT;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Check if runlevel is valid if we're changing */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (newlevel && strcmp(runlevel, newlevel) != 0 && ! going_down) {
|
|
|
|
if (! rc_runlevel_exists(newlevel))
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: is not a valid runlevel", newlevel);
|
|
|
|
}
|
|
|
|
|
2008-01-29 19:22:54 +05:30
|
|
|
/* Load our deptree */
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((deptree = _rc_deptree_load(®en)) == NULL)
|
|
|
|
eerrorx("failed to load deptree");
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-01-29 19:22:54 +05:30
|
|
|
/* Clean the failed services state dir */
|
2008-03-16 22:30:56 +05:30
|
|
|
clean_failed();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (mkdir(RC_STOPPING, 0755) != 0) {
|
2008-03-06 17:09:05 +05:30
|
|
|
if (errno == EACCES)
|
2008-03-16 22:30:56 +05:30
|
|
|
eerrorx("%s: superuser access required", applet);
|
|
|
|
eerrorx("%s: failed to create stopping dir `%s': %s",
|
|
|
|
applet, RC_STOPPING, strerror(errno));
|
2008-03-06 17:09:05 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Build a list of all services to stop and then work out the
|
2008-01-29 19:22:54 +05:30
|
|
|
* correct order for stopping them */
|
2008-03-16 22:30:56 +05:30
|
|
|
stop_services = rc_services_in_state(RC_SERVICE_STARTED);
|
|
|
|
tmplist = rc_services_in_state(RC_SERVICE_INACTIVE);
|
2008-03-17 18:55:56 +05:30
|
|
|
if (tmplist) {
|
|
|
|
if (stop_services) {
|
|
|
|
TAILQ_CONCAT(stop_services, tmplist, entries);
|
|
|
|
free(tmplist);
|
|
|
|
} else
|
|
|
|
stop_services = tmplist;
|
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
tmplist = rc_services_in_state(RC_SERVICE_STARTING);
|
2008-03-17 18:55:56 +05:30
|
|
|
if (tmplist) {
|
|
|
|
if (stop_services) {
|
|
|
|
TAILQ_CONCAT(stop_services, tmplist, entries);
|
|
|
|
free(tmplist);
|
|
|
|
} else
|
|
|
|
stop_services = tmplist;
|
|
|
|
}
|
2008-03-17 20:01:44 +05:30
|
|
|
if (stop_services)
|
|
|
|
rc_stringlist_sort(&stop_services);
|
2008-03-16 22:30:56 +05:30
|
|
|
|
|
|
|
types_nua = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(types_nua, "ineed");
|
|
|
|
rc_stringlist_add(types_nua, "iuse");
|
|
|
|
rc_stringlist_add(types_nua, "iafter");
|
|
|
|
|
2008-03-17 20:01:44 +05:30
|
|
|
if (stop_services) {
|
|
|
|
tmplist = rc_deptree_depends(deptree, types_nua, stop_services,
|
|
|
|
runlevel, depoptions | RC_DEP_STOP);
|
|
|
|
rc_stringlist_free(stop_services);
|
|
|
|
stop_services = tmplist;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Load our list of coldplugged services */
|
2008-03-16 22:30:56 +05:30
|
|
|
coldplugged_services = rc_services_in_state(RC_SERVICE_COLDPLUGGED);
|
|
|
|
if (strcmp(newlevel ? newlevel : runlevel, RC_LEVEL_SINGLE) != 0 &&
|
|
|
|
strcmp(newlevel ? newlevel : runlevel, RC_LEVEL_SHUTDOWN) != 0 &&
|
|
|
|
strcmp(newlevel ? newlevel : runlevel, RC_LEVEL_REBOOT) != 0)
|
2008-01-29 19:22:54 +05:30
|
|
|
{
|
|
|
|
/* We need to include the boot runlevel services if we're not in it */
|
2008-03-16 22:30:56 +05:30
|
|
|
start_services = rc_services_in_runlevel(bootlevel);
|
2008-01-29 19:22:54 +05:30
|
|
|
if (strcmp (newlevel ? newlevel : runlevel, bootlevel) != 0) {
|
2008-03-16 22:30:56 +05:30
|
|
|
tmplist = rc_services_in_runlevel(newlevel ? newlevel : runlevel);
|
2008-03-17 18:55:56 +05:30
|
|
|
if (tmplist) {
|
|
|
|
if (start_services) {
|
|
|
|
TAILQ_CONCAT(start_services, tmplist, entries);
|
|
|
|
free(tmplist);
|
|
|
|
} else
|
|
|
|
start_services = tmplist;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2008-01-29 19:22:54 +05:30
|
|
|
|
2008-03-22 01:32:10 +05:30
|
|
|
if (coldplugged_services) {
|
2008-03-26 10:38:38 +05:30
|
|
|
if (!start_services)
|
|
|
|
start_services = rc_stringlist_new();
|
|
|
|
TAILQ_FOREACH(service, coldplugged_services, entries)
|
|
|
|
rc_stringlist_addu(start_services, service->value);
|
2008-03-22 01:32:10 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-04-08 21:29:56 +05:30
|
|
|
/* Save our runlevel now */
|
2007-04-11 18:14:47 +05:30
|
|
|
if (going_down)
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_runlevel_set(newlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
parallel = rc_conf_yesno("rc_parallel");
|
2007-11-23 17:34:11 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Now stop the services that shouldn't be running */
|
2008-03-17 18:55:56 +05:30
|
|
|
if (stop_services)
|
2008-03-19 04:36:02 +05:30
|
|
|
do_stop_services(newlevel, going_down, parallel);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Wait for our services to finish */
|
2008-03-16 22:30:56 +05:30
|
|
|
wait_for_services();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Notify the plugins we have finished */
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_RUNLEVEL_STOP_OUT, runlevel);
|
2008-03-05 02:11:25 +05:30
|
|
|
hook_out = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rmdir(RC_STOPPING);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Store the new runlevel */
|
|
|
|
if (newlevel) {
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_runlevel_set(newlevel);
|
|
|
|
free(runlevel);
|
|
|
|
runlevel = xstrdup(newlevel);
|
|
|
|
setenv("RC_RUNLEVEL", runlevel, 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Run the halt script if needed */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strcmp(runlevel, RC_LEVEL_SHUTDOWN) == 0 ||
|
|
|
|
strcmp(runlevel, RC_LEVEL_REBOOT) == 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_logger_close();
|
|
|
|
execl(HALTSH, HALTSH, runlevel, (char *) NULL);
|
|
|
|
eerrorx("%s: unable to exec `%s': %s",
|
|
|
|
applet, HALTSH, strerror(errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Single user is done now */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strcmp(runlevel, RC_LEVEL_SINGLE) == 0) {
|
|
|
|
if (exists(INTERACTIVE))
|
|
|
|
unlink(INTERACTIVE);
|
|
|
|
sulogin(false);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
mkdir(RC_STARTING, 0755);
|
|
|
|
rc_plugin_run(RC_HOOK_RUNLEVEL_START_IN, runlevel);
|
2008-03-05 02:11:25 +05:30
|
|
|
hook_out = RC_HOOK_RUNLEVEL_START_OUT;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Re-add our coldplugged services if they stopped */
|
2008-03-17 18:55:56 +05:30
|
|
|
if (coldplugged_services)
|
|
|
|
TAILQ_FOREACH(service, coldplugged_services, entries)
|
|
|
|
rc_service_mark(service->value, RC_SERVICE_COLDPLUGGED);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Order the services to start */
|
2008-03-17 18:55:56 +05:30
|
|
|
if (start_services) {
|
|
|
|
rc_stringlist_sort(&start_services);
|
|
|
|
deporder = rc_deptree_depends(deptree, types_nua, start_services,
|
2008-03-16 22:30:56 +05:30
|
|
|
runlevel, depoptions | RC_DEP_START);
|
2008-03-17 18:55:56 +05:30
|
|
|
rc_stringlist_free(start_services);
|
|
|
|
start_services = deporder;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-23 17:06:12 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
/* mark any services skipped as started */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (PREVLEVEL && strcmp(PREVLEVEL, "N") == 0) {
|
|
|
|
proc = p = proc_getent("noinitd");
|
|
|
|
if (proc) {
|
|
|
|
while ((token = strsep(&p, ",")))
|
|
|
|
rc_service_mark(token, RC_SERVICE_STARTED);
|
|
|
|
free(proc);
|
2007-07-23 17:06:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
if (start_services) {
|
|
|
|
do_start_services(parallel);
|
2007-04-21 00:28:42 +05:30
|
|
|
|
2008-03-17 18:55:56 +05:30
|
|
|
/* Wait for our services to finish */
|
|
|
|
wait_for_services();
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_plugin_run(RC_HOOK_RUNLEVEL_START_OUT, runlevel);
|
2008-03-05 02:11:25 +05:30
|
|
|
hook_out = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-23 17:06:12 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
/* mark any services skipped as stopped */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (PREVLEVEL && strcmp(PREVLEVEL, "N") == 0) {
|
|
|
|
proc = p = proc_getent("noinitd");
|
|
|
|
if (proc) {
|
|
|
|
while ((token = strsep(&p, ",")))
|
|
|
|
rc_service_mark(token, RC_SERVICE_STOPPED);
|
|
|
|
free(proc);
|
2007-07-23 17:06:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-14 17:53:13 +05:30
|
|
|
/* If we're in the boot runlevel and we regenerated our dependencies
|
|
|
|
* we need to delete them so that they are regenerated again in the
|
|
|
|
* default runlevel as they may depend on things that are now available */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (regen && strcmp(runlevel, bootlevel) == 0)
|
|
|
|
unlink(RC_DEPTREE_CACHE);
|
2007-12-14 17:53:13 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return EXIT_SUCCESS;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|