2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
rc.c
|
|
|
|
rc - manager for init scripts which control the startup, shutdown
|
|
|
|
and the running of daemons on a Gentoo system.
|
|
|
|
|
|
|
|
Also a multicall binary for various commands that can be used in shell
|
|
|
|
scripts to query service state, mark service state and provide the
|
|
|
|
Gentoo einfo family of informational functions.
|
2007-04-10 16:54:58 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
Released under the GPLv2
|
|
|
|
*/
|
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
#define APPLET "rc"
|
|
|
|
|
2007-08-16 22:23:20 +05:30
|
|
|
#define SYSLOG_NAMES
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2007-06-28 21:14:14 +05:30
|
|
|
#include <getopt.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <libgen.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
2007-08-16 22:23:20 +05:30
|
|
|
#include <syslog.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"
|
2007-04-05 16:48:42 +05:30
|
|
|
#include "einfo.h"
|
|
|
|
#include "rc.h"
|
|
|
|
#include "rc-misc.h"
|
|
|
|
#include "rc-plugin.h"
|
|
|
|
#include "strlist.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
|
|
|
|
2007-04-17 15:02:18 +05:30
|
|
|
#define DEVBOOT "/dev/.rcboot"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
/* Cleanup anything in main */
|
2007-04-11 18:14:47 +05:30
|
|
|
#define CHAR_FREE(_item) if (_item) { \
|
|
|
|
free (_item); \
|
|
|
|
_item = NULL; \
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
2007-07-11 18:32:05 +05:30
|
|
|
static char *RUNLEVEL = NULL;
|
|
|
|
static char *PREVLEVEL = NULL;
|
|
|
|
|
2007-04-09 22:23:21 +05:30
|
|
|
static char *applet = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
static char **env = NULL;
|
|
|
|
static char **newenv = NULL;
|
2007-04-20 18:42:21 +05:30
|
|
|
static char **coldplugged_services = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
static char **stop_services = NULL;
|
|
|
|
static char **start_services = NULL;
|
|
|
|
static rc_depinfo_t *deptree = NULL;
|
|
|
|
static char **types = NULL;
|
|
|
|
static char *tmp = NULL;
|
|
|
|
|
2007-04-20 18:42:21 +05:30
|
|
|
struct termios *termios_orig = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-21 00:28:42 +05:30
|
|
|
typedef struct pidlist
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
struct pidlist *next;
|
|
|
|
} pidlist_t;
|
|
|
|
static pidlist_t *service_pids = NULL;
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
static void cleanup (void)
|
|
|
|
{
|
2007-07-31 21:35:56 +05:30
|
|
|
if (applet && strcmp (applet, "rc") == 0) {
|
|
|
|
pidlist_t *pl = service_pids;
|
2007-04-21 00:28:42 +05:30
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
rc_plugin_unload ();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
if (! rc_in_plugin && termios_orig) {
|
|
|
|
tcsetattr (fileno (stdin), TCSANOW, termios_orig);
|
|
|
|
free (termios_orig);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
while (pl) {
|
|
|
|
pidlist_t *p = pl->next;
|
|
|
|
free (pl);
|
|
|
|
pl = p;
|
|
|
|
}
|
2007-04-21 00:28:42 +05:30
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
rc_strlist_free (env);
|
|
|
|
rc_strlist_free (newenv);
|
|
|
|
rc_strlist_free (coldplugged_services);
|
|
|
|
rc_strlist_free (stop_services);
|
|
|
|
rc_strlist_free (start_services);
|
2007-09-29 22:12:08 +05:30
|
|
|
rc_deptree_free (deptree);
|
2007-07-31 21:35:56 +05:30
|
|
|
rc_strlist_free (types);
|
|
|
|
|
|
|
|
/* Clean runlevel start, stop markers */
|
|
|
|
if (! rc_in_plugin) {
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_is_dir (RC_STARTING))
|
2007-08-28 14:43:46 +05:30
|
|
|
rc_rm_dir (RC_STARTING, true);
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_is_dir (RC_STOPPING))
|
2007-08-28 14:43:46 +05:30
|
|
|
rc_rm_dir (RC_STOPPING, true);
|
2007-07-31 21:35:56 +05:30
|
|
|
}
|
2007-07-04 21:32:01 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-20 18:42:21 +05:30
|
|
|
free (applet);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-08-16 22:23:20 +05:30
|
|
|
static int syslog_decode (char *name, CODE *codetab)
|
|
|
|
{
|
|
|
|
CODE *c;
|
|
|
|
|
|
|
|
if (isdigit (*name))
|
|
|
|
return (atoi (name));
|
|
|
|
|
|
|
|
for (c = codetab; c->c_name; c++)
|
|
|
|
if (! strcasecmp (name, c->c_name))
|
|
|
|
return (c->c_val);
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
static int do_e (int argc, char **argv)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int retval = EXIT_SUCCESS;
|
|
|
|
int i;
|
|
|
|
int l = 0;
|
|
|
|
char *message = NULL;
|
|
|
|
char *p;
|
|
|
|
char *fmt = NULL;
|
2007-08-16 22:23:20 +05:30
|
|
|
int level = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-17 15:02:18 +05:30
|
|
|
if (strcmp (applet, "eval_ecolors") == 0) {
|
|
|
|
printf ("GOOD='%s'\nWARN='%s'\nBAD='%s'\nHILITE='%s'\nBRACKET='%s'\nNORMAL='%s'\n",
|
2007-09-28 17:59:23 +05:30
|
|
|
ecolor (ECOLOR_GOOD),
|
|
|
|
ecolor (ECOLOR_WARN),
|
|
|
|
ecolor (ECOLOR_BAD),
|
|
|
|
ecolor (ECOLOR_HILITE),
|
|
|
|
ecolor (ECOLOR_BRACKET),
|
|
|
|
ecolor (ECOLOR_NORMAL));
|
2007-04-17 15:02:18 +05:30
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2007-08-16 22:23:20 +05:30
|
|
|
if (argc > 0) {
|
|
|
|
|
|
|
|
if (strcmp (applet, "eend") == 0 ||
|
|
|
|
strcmp (applet, "ewend") == 0 ||
|
|
|
|
strcmp (applet, "veend") == 0 ||
|
|
|
|
strcmp (applet, "vweend") == 0)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
errno = 0;
|
|
|
|
retval = strtol (argv[0], NULL, 0);
|
|
|
|
if (errno != 0)
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
else {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
2007-08-16 22:23:20 +05:30
|
|
|
} else if (strcmp (applet, "esyslog") == 0 ||
|
|
|
|
strcmp (applet, "elog") == 0) {
|
|
|
|
char *dot = strchr (argv[0], '.');
|
|
|
|
if ((level = syslog_decode (dot + 1, prioritynames)) == -1)
|
|
|
|
eerrorx ("%s: invalid log level `%s'", applet, argv[0]);
|
2007-08-16 22:39:57 +05:30
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
eerrorx ("%s: not enough arguments", applet);
|
|
|
|
|
|
|
|
unsetenv ("RC_ELOG");
|
|
|
|
setenv ("RC_ELOG", argv[1], 1);
|
|
|
|
|
|
|
|
argc -= 2;
|
|
|
|
argv += 2;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 0) {
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
l += strlen (argv[i]) + 1;
|
|
|
|
|
|
|
|
message = rc_xmalloc (l);
|
|
|
|
p = message;
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
*p++ = ' ';
|
|
|
|
memcpy (p, argv[i], strlen (argv[i]));
|
|
|
|
p += strlen (argv[i]);
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message)
|
2007-04-19 20:24:35 +05:30
|
|
|
fmt = rc_xstrdup ("%s");
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (strcmp (applet, "einfo") == 0)
|
|
|
|
einfo (fmt, message);
|
|
|
|
else if (strcmp (applet, "einfon") == 0)
|
|
|
|
einfon (fmt, message);
|
|
|
|
else if (strcmp (applet, "ewarn") == 0)
|
|
|
|
ewarn (fmt, message);
|
|
|
|
else if (strcmp (applet, "ewarnn") == 0)
|
|
|
|
ewarnn (fmt, message);
|
|
|
|
else if (strcmp (applet, "eerror") == 0) {
|
|
|
|
eerror (fmt, message);
|
|
|
|
retval = 1;
|
|
|
|
} else if (strcmp (applet, "eerrorn") == 0) {
|
|
|
|
eerrorn (fmt, message);
|
|
|
|
retval = 1;
|
|
|
|
} else if (strcmp (applet, "ebegin") == 0)
|
|
|
|
ebegin (fmt, message);
|
|
|
|
else if (strcmp (applet, "eend") == 0)
|
|
|
|
eend (retval, fmt, message);
|
|
|
|
else if (strcmp (applet, "ewend") == 0)
|
|
|
|
ewend (retval, fmt, message);
|
2007-08-16 22:23:20 +05:30
|
|
|
else if (strcmp (applet, "esyslog") == 0)
|
|
|
|
elog (level, fmt, message);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "veinfo") == 0)
|
|
|
|
einfov (fmt, message);
|
|
|
|
else if (strcmp (applet, "veinfon") == 0)
|
|
|
|
einfovn (fmt, message);
|
|
|
|
else if (strcmp (applet, "vewarn") == 0)
|
|
|
|
ewarnv (fmt, message);
|
|
|
|
else if (strcmp (applet, "vewarnn") == 0)
|
|
|
|
ewarnvn (fmt, message);
|
|
|
|
else if (strcmp (applet, "vebegin") == 0)
|
|
|
|
ebeginv (fmt, message);
|
|
|
|
else if (strcmp (applet, "veend") == 0)
|
|
|
|
eendv (retval, fmt, message);
|
|
|
|
else if (strcmp (applet, "vewend") == 0)
|
|
|
|
ewendv (retval, fmt, message);
|
|
|
|
else if (strcmp (applet, "eindent") == 0)
|
|
|
|
eindent ();
|
|
|
|
else if (strcmp (applet, "eoutdent") == 0)
|
|
|
|
eoutdent ();
|
|
|
|
else if (strcmp (applet, "veindent") == 0)
|
|
|
|
eindentv ();
|
|
|
|
else if (strcmp (applet, "veoutdent") == 0)
|
|
|
|
eoutdentv ();
|
|
|
|
else {
|
|
|
|
eerror ("%s: unknown applet", applet);
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fmt)
|
|
|
|
free (fmt);
|
|
|
|
if (message)
|
|
|
|
free (message);
|
|
|
|
return (retval);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int do_service (int argc, char **argv)
|
|
|
|
{
|
2007-09-25 23:00:07 +05:30
|
|
|
bool ok = false;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0)
|
|
|
|
eerrorx ("%s: no service specified", applet);
|
|
|
|
|
|
|
|
if (strcmp (applet, "service_started") == 0)
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (rc_service_state (argv[0]) & RC_SERVICE_STARTED);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "service_stopped") == 0)
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (rc_service_state (argv[0]) & RC_SERVICE_STOPPED);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "service_inactive") == 0)
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (rc_service_state (argv[0]) & RC_SERVICE_INACTIVE);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "service_starting") == 0)
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (rc_service_state (argv[0]) & RC_SERVICE_STOPPING);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "service_stopping") == 0)
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (rc_service_state (argv[0]) & RC_SERVICE_STOPPING);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "service_coldplugged") == 0)
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (rc_service_state (argv[0]) & RC_SERVICE_COLDPLUGGED);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "service_wasinactive") == 0)
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (rc_service_state (argv[0]) & RC_SERVICE_WASINACTIVE);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "service_started_daemon") == 0) {
|
|
|
|
int idx = 0;
|
|
|
|
if (argc > 2)
|
|
|
|
sscanf (argv[2], "%d", &idx);
|
2007-09-25 23:00:07 +05:30
|
|
|
exit (rc_service_started_daemon (argv[0], argv[1], idx)
|
|
|
|
? 0 : 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else
|
|
|
|
eerrorx ("%s: unknown applet", applet);
|
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int do_mark_service (int argc, char **argv)
|
|
|
|
{
|
2007-09-25 23:00:07 +05:30
|
|
|
bool ok = false;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *svcname = getenv ("SVCNAME");
|
|
|
|
|
|
|
|
if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0)
|
|
|
|
eerrorx ("%s: no service specified", applet);
|
|
|
|
|
|
|
|
if (strcmp (applet, "mark_service_started") == 0)
|
2007-09-29 22:24:58 +05:30
|
|
|
ok = rc_service_mark (argv[0], RC_SERVICE_STARTED);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "mark_service_stopped") == 0)
|
2007-09-29 22:24:58 +05:30
|
|
|
ok = rc_service_mark (argv[0], RC_SERVICE_STOPPED);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "mark_service_inactive") == 0)
|
2007-09-29 22:24:58 +05:30
|
|
|
ok = rc_service_mark (argv[0], RC_SERVICE_INACTIVE);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "mark_service_starting") == 0)
|
2007-09-29 22:24:58 +05:30
|
|
|
ok = rc_service_mark (argv[0], RC_SERVICE_STOPPING);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "mark_service_stopping") == 0)
|
2007-09-29 22:24:58 +05:30
|
|
|
ok = rc_service_mark (argv[0], RC_SERVICE_STOPPING);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "mark_service_coldplugged") == 0)
|
2007-09-29 22:24:58 +05:30
|
|
|
ok = rc_service_mark (argv[0], RC_SERVICE_COLDPLUGGED);
|
2007-04-11 18:14:47 +05:30
|
|
|
else
|
|
|
|
eerrorx ("%s: unknown applet", applet);
|
|
|
|
|
|
|
|
/* If we're marking ourselves then we need to inform our parent runscript
|
|
|
|
process so they do not mark us based on our exit code */
|
2007-09-25 23:00:07 +05:30
|
|
|
if (ok && svcname && strcmp (svcname, argv[0]) == 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
char *runscript_pid = getenv ("RC_RUNSCRIPT_PID");
|
|
|
|
char *mtime;
|
|
|
|
pid_t pid = 0;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
if (runscript_pid && sscanf (runscript_pid, "%d", &pid) == 1)
|
|
|
|
if (kill (pid, SIGHUP) != 0)
|
|
|
|
eerror ("%s: failed to signal parent %d: %s",
|
|
|
|
applet, pid, strerror (errno));
|
|
|
|
|
|
|
|
/* Remove the exclsive time test. This ensures that it's not
|
|
|
|
in control as well */
|
|
|
|
l = strlen (RC_SVCDIR "exclusive") +
|
|
|
|
strlen (svcname) +
|
|
|
|
strlen (runscript_pid) +
|
|
|
|
4;
|
|
|
|
mtime = rc_xmalloc (l);
|
|
|
|
snprintf (mtime, l, RC_SVCDIR "exclusive/%s.%s",
|
|
|
|
svcname, runscript_pid);
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_exists (mtime) && unlink (mtime) != 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
eerror ("%s: unlink: %s", applet, strerror (errno));
|
|
|
|
free (mtime);
|
|
|
|
}
|
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int do_options (int argc, char **argv)
|
|
|
|
{
|
2007-09-25 23:00:07 +05:30
|
|
|
bool ok = false;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *service = getenv ("SVCNAME");
|
|
|
|
|
|
|
|
if (! service)
|
|
|
|
eerrorx ("%s: no service specified", applet);
|
|
|
|
|
|
|
|
if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0)
|
|
|
|
eerrorx ("%s: no option specified", applet);
|
|
|
|
|
|
|
|
if (strcmp (applet, "get_options") == 0) {
|
2007-09-25 21:08:21 +05:30
|
|
|
char *option = rc_get_service_option (service, argv[0]);
|
|
|
|
if (option) {
|
|
|
|
printf ("%s", option);
|
|
|
|
free (option);
|
2007-09-25 23:00:07 +05:30
|
|
|
ok = true;
|
2007-09-25 21:08:21 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
} else if (strcmp (applet, "save_options") == 0)
|
|
|
|
ok = rc_set_service_option (service, argv[0], argv[1]);
|
|
|
|
else
|
|
|
|
eerrorx ("%s: unknown applet", applet);
|
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-07-23 17:06:12 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
static char *proc_getent (const char *ent)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char buffer[RC_LINEBUFFER];
|
|
|
|
char *p;
|
|
|
|
char *value = NULL;
|
|
|
|
int i;
|
2007-08-22 19:40:46 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! rc_exists ("/proc/cmdline"))
|
2007-08-22 19:40:46 +05:30
|
|
|
return (NULL);
|
|
|
|
|
2007-07-23 17:06:12 +05:30
|
|
|
if (! (fp = fopen ("/proc/cmdline", "r"))) {
|
|
|
|
eerror ("failed to open `/proc/cmdline': %s", strerror (errno));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (buffer, 0, sizeof (buffer));
|
|
|
|
if (fgets (buffer, RC_LINEBUFFER, fp) &&
|
|
|
|
(p = strstr (buffer, ent)))
|
|
|
|
{
|
|
|
|
i = p - buffer;
|
|
|
|
if (i == '\0' || buffer[i - 1] == ' ') {
|
|
|
|
/* Trim the trailing carriage return if present */
|
|
|
|
i = strlen (buffer) - 1;
|
|
|
|
if (buffer[i] == '\n')
|
|
|
|
buffer[i] = 0;
|
|
|
|
|
|
|
|
p += strlen (ent);
|
|
|
|
if (*p == '=')
|
|
|
|
p++;
|
|
|
|
value = strdup (strsep (&p, " "));
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
errno = ENOENT;
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
static char read_key (bool block)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct termios termios;
|
|
|
|
char c = 0;
|
2007-07-11 18:32:05 +05:30
|
|
|
int fd = fileno (stdin);
|
|
|
|
|
|
|
|
if (! isatty (fd))
|
2007-04-11 18:14:47 +05:30
|
|
|
return (false);
|
|
|
|
|
|
|
|
/* Now save our terminal settings. We need to restore them at exit as we
|
|
|
|
will be changing it for non-blocking reads for Interactive */
|
|
|
|
if (! termios_orig) {
|
|
|
|
termios_orig = rc_xmalloc (sizeof (struct termios));
|
2007-07-11 18:32:05 +05:30
|
|
|
tcgetattr (fd, termios_orig);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2007-07-11 18:32:05 +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;
|
|
|
|
}
|
2007-07-11 18:32:05 +05:30
|
|
|
tcsetattr (fd, TCSANOW, &termios);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-11 18:32:05 +05:30
|
|
|
read (fd, &c, 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-11 18:32:05 +05:30
|
|
|
tcsetattr (fd, TCSANOW, termios_orig);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
return (c);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static bool want_interactive (void)
|
|
|
|
{
|
2007-05-14 18:18:37 +05:30
|
|
|
char c;
|
|
|
|
|
2007-07-11 18:32:05 +05:30
|
|
|
if (PREVLEVEL &&
|
|
|
|
strcmp (PREVLEVEL, "N") != 0 &&
|
|
|
|
strcmp (PREVLEVEL, "S") != 0 &&
|
|
|
|
strcmp (PREVLEVEL, "1") != 0)
|
|
|
|
return (false);
|
|
|
|
|
2007-09-26 12:35:33 +05:30
|
|
|
if (! rc_env_bool ("RC_INTERACTIVE"))
|
2007-05-14 18:18:37 +05:30
|
|
|
return (false);
|
|
|
|
|
|
|
|
c = read_key (false);
|
2007-04-11 18:14:47 +05:30
|
|
|
return ((c == 'I' || c == 'i') ? true : false);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void mark_interactive (void)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp = fopen (INTERACTIVE, "w");
|
|
|
|
if (fp)
|
|
|
|
fclose (fp);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void sulogin (bool cont)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
2007-04-20 18:42:21 +05:30
|
|
|
char *e = getenv ("RC_SYS");
|
|
|
|
|
|
|
|
/* VPS systems cannot do an sulogin */
|
|
|
|
if (e && strcmp (e, "VPS") == 0) {
|
|
|
|
execl ("/sbin/halt", "/sbin/halt", "-f", (char *) NULL);
|
|
|
|
eerrorx ("%s: unable to exec `/sbin/halt': %s", applet, strerror (errno));
|
|
|
|
}
|
2007-04-21 00:28:42 +05:30
|
|
|
#endif
|
2007-04-20 18:42:21 +05:30
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
newenv = rc_filter_env ();
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (cont) {
|
|
|
|
int status = 0;
|
2007-05-03 19:16:38 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
char *tty = ttyname (fileno (stdout));
|
|
|
|
#endif
|
|
|
|
|
2007-04-25 18:00:24 +05:30
|
|
|
pid_t pid = vfork ();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (pid == -1)
|
2007-04-20 18:42:21 +05:30
|
|
|
eerrorx ("%s: vfork: %s", applet, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
if (pid == 0) {
|
2007-04-21 00:28:42 +05:30
|
|
|
#ifdef __linux__
|
2007-05-03 19:16:38 +05:30
|
|
|
if (tty)
|
|
|
|
execle (SULOGIN, SULOGIN, tty, (char *) NULL, newenv);
|
|
|
|
else
|
|
|
|
execle (SULOGIN, SULOGIN, (char *) NULL, newenv);
|
|
|
|
|
|
|
|
eerror ("%s: unable to exec `%s': %s", applet, SULOGIN,
|
2007-04-20 18:42:21 +05:30
|
|
|
strerror (errno));
|
2007-04-21 00:28:42 +05:30
|
|
|
#else
|
|
|
|
execle ("/bin/sh", "/bin/sh", (char *) NULL, newenv);
|
|
|
|
eerror ("%s: unable to exec `/bin/sh': %s", applet,
|
|
|
|
strerror (errno));
|
|
|
|
#endif
|
2007-04-20 18:42:21 +05:30
|
|
|
_exit (EXIT_FAILURE);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
waitpid (pid, &status, 0);
|
|
|
|
} else {
|
2007-08-28 18:40:07 +05:30
|
|
|
#ifdef __linux__
|
2007-04-25 21:18:54 +05:30
|
|
|
execle ("/sbin/sulogin", "/sbin/sulogin", (char *) NULL, newenv);
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: unable to exec `/sbin/sulogin': %s", applet, strerror (errno));
|
2007-04-05 16:48:42 +05:30
|
|
|
#else
|
2007-04-21 00:28:42 +05:30
|
|
|
exit (EXIT_SUCCESS);
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
2007-04-21 00:28:42 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-20 18:42:21 +05:30
|
|
|
static void single_user (void)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
execl ("/sbin/telinit", "/sbin/telinit", "S", (char *) NULL);
|
|
|
|
eerrorx ("%s: unable to exec `/sbin/telinit': %s",
|
|
|
|
applet, strerror (errno));
|
|
|
|
#else
|
|
|
|
if (kill (1, SIGTERM) != 0)
|
|
|
|
eerrorx ("%s: unable to send SIGTERM to init (pid 1): %s",
|
|
|
|
applet, strerror (errno));
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
static void set_ksoftlevel (const char *runlevel)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (! runlevel ||
|
2007-07-21 18:19:51 +05:30
|
|
|
strcmp (runlevel, getenv ("RC_BOOTLEVEL")) == 0 ||
|
2007-04-11 18:14:47 +05:30
|
|
|
strcmp (runlevel, RC_LEVEL_SINGLE) == 0 ||
|
|
|
|
strcmp (runlevel, RC_LEVEL_SYSINIT) == 0)
|
|
|
|
{
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_exists (RC_KSOFTLEVEL) &&
|
2007-08-28 14:43:46 +05:30
|
|
|
unlink (RC_KSOFTLEVEL) != 0)
|
|
|
|
eerror ("unlink `%s': %s", RC_KSOFTLEVEL, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-28 14:43:46 +05:30
|
|
|
if (! (fp = fopen (RC_KSOFTLEVEL, "w"))) {
|
|
|
|
eerror ("fopen `%s': %s", RC_KSOFTLEVEL, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf (fp, "%s", runlevel);
|
|
|
|
fclose (fp);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-07-10 17:12:56 +05:30
|
|
|
static int get_ksoftlevel (char *buffer, int buffer_len)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int i = 0;
|
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! rc_exists (RC_KSOFTLEVEL))
|
2007-08-28 14:20:04 +05:30
|
|
|
return (0);
|
2007-07-10 17:12:56 +05:30
|
|
|
|
2007-08-28 14:43:46 +05:30
|
|
|
if (! (fp = fopen (RC_KSOFTLEVEL, "r"))) {
|
|
|
|
eerror ("fopen `%s': %s", RC_KSOFTLEVEL, strerror (errno));
|
2007-08-28 14:20:04 +05:30
|
|
|
return (-1);
|
2007-07-10 17:12:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (fgets (buffer, buffer_len, fp)) {
|
|
|
|
i = strlen (buffer) - 1;
|
|
|
|
if (buffer[i] == '\n')
|
|
|
|
buffer[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fp);
|
2007-08-28 14:20:04 +05:30
|
|
|
return (i);
|
2007-07-10 17:12:56 +05:30
|
|
|
}
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
static void wait_for_services ()
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int status = 0;
|
|
|
|
while (wait (&status) != -1);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-04-21 00:28:42 +05:30
|
|
|
static void add_pid (pid_t pid)
|
|
|
|
{
|
|
|
|
pidlist_t *sp = service_pids;
|
|
|
|
if (sp) {
|
|
|
|
while (sp->next)
|
|
|
|
sp = sp->next;
|
|
|
|
sp->next = rc_xmalloc (sizeof (pidlist_t));
|
|
|
|
sp = sp->next;
|
|
|
|
} else
|
|
|
|
sp = service_pids = rc_xmalloc (sizeof (pidlist_t));
|
|
|
|
memset (sp, 0, sizeof (pidlist_t));
|
|
|
|
sp->pid = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_pid (pid_t pid)
|
|
|
|
{
|
|
|
|
pidlist_t *last = NULL;
|
|
|
|
pidlist_t *pl;
|
|
|
|
|
|
|
|
for (pl = service_pids; pl; pl = pl->next) {
|
|
|
|
if (pl->pid == pid) {
|
|
|
|
if (last)
|
|
|
|
last->next = pl->next;
|
|
|
|
else
|
|
|
|
service_pids = pl->next;
|
|
|
|
free (pl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last = pl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-09 22:23:21 +05:30
|
|
|
static void handle_signal (int sig)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int serrno = errno;
|
|
|
|
char signame[10] = { '\0' };
|
2007-04-21 00:28:42 +05:30
|
|
|
pidlist_t *pl;
|
|
|
|
pid_t pid;
|
|
|
|
int status = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
switch (sig) {
|
2007-04-21 00:28:42 +05:30
|
|
|
case SIGCHLD:
|
|
|
|
do {
|
|
|
|
pid = waitpid (-1, &status, WNOHANG);
|
|
|
|
if (pid < 0) {
|
|
|
|
if (errno != ECHILD)
|
|
|
|
eerror ("waitpid: %s", strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (! WIFEXITED (status) && ! WIFSIGNALED (status));
|
|
|
|
|
|
|
|
/* Remove that pid from our list */
|
|
|
|
if (pid > 0)
|
|
|
|
remove_pid (pid);
|
|
|
|
break;
|
2007-04-21 00:33:56 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
case SIGINT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGINT");
|
|
|
|
case SIGTERM:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGTERM");
|
|
|
|
case SIGQUIT:
|
|
|
|
if (! signame[0])
|
|
|
|
snprintf (signame, sizeof (signame), "SIGQUIT");
|
|
|
|
eerrorx ("%s: caught %s, aborting", applet, signame);
|
2007-04-20 18:42:21 +05:30
|
|
|
case SIGUSR1:
|
|
|
|
eerror ("rc: Aborting!");
|
|
|
|
/* Kill any running services we have started */
|
2007-04-21 00:33:56 +05:30
|
|
|
|
2007-04-21 00:28:42 +05:30
|
|
|
signal (SIGCHLD, SIG_IGN);
|
|
|
|
for (pl = service_pids; pl; pl = pl->next)
|
|
|
|
kill (pl->pid, SIGTERM);
|
2007-04-21 00:33:56 +05:30
|
|
|
|
2007-04-20 20:32:13 +05:30
|
|
|
/* Notify plugins we are aborting */
|
2007-09-28 17:59:23 +05:30
|
|
|
rc_plugin_run (RC_HOOK_ABORT, NULL);
|
2007-04-21 00:33:56 +05:30
|
|
|
|
2007-04-21 00:28:42 +05:30
|
|
|
/* Only drop into single user mode if we're booting */
|
2007-07-11 18:32:05 +05:30
|
|
|
if ((PREVLEVEL &&
|
|
|
|
(strcmp (PREVLEVEL, "S") == 0 ||
|
|
|
|
strcmp (PREVLEVEL, "1") == 0)) ||
|
|
|
|
(RUNLEVEL &&
|
|
|
|
(strcmp (RUNLEVEL, "S") == 0 ||
|
|
|
|
strcmp (RUNLEVEL, "1") == 0)))
|
2007-04-20 18:42:21 +05:30
|
|
|
single_user ();
|
|
|
|
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
default:
|
|
|
|
eerror ("%s: caught unknown signal %d", applet, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore errno */
|
|
|
|
errno = serrno;
|
2007-04-09 22:23:21 +05:30
|
|
|
}
|
|
|
|
|
2007-05-22 17:55:43 +05:30
|
|
|
static void run_script (const char *script) {
|
|
|
|
int status = 0;
|
|
|
|
pid_t pid = vfork ();
|
2007-07-10 17:12:56 +05:30
|
|
|
|
2007-05-22 17:55:43 +05:30
|
|
|
if (pid < 0)
|
|
|
|
eerrorx ("%s: vfork: %s", applet, strerror (errno));
|
|
|
|
else if (pid == 0) {
|
|
|
|
execl (script, script, (char *) NULL);
|
|
|
|
eerror ("%s: unable to exec `%s': %s",
|
|
|
|
script, applet, strerror (errno));
|
|
|
|
_exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
pid_t wpid = waitpid (pid, &status, 0);
|
|
|
|
if (wpid < 1)
|
|
|
|
eerror ("waitpid: %s", strerror (errno));
|
|
|
|
} while (! WIFEXITED (status) && ! WIFSIGNALED (status));
|
|
|
|
|
|
|
|
if (! WIFEXITED (status) || ! WEXITSTATUS (status) == 0)
|
2007-08-05 02:05:05 +05:30
|
|
|
eerrorx ("%s: failed to exec `%s'", applet, script);
|
2007-05-22 17:55:43 +05:30
|
|
|
}
|
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
#include "_usage.h"
|
2007-09-25 21:51:38 +05:30
|
|
|
#define getoptstring getoptstring_COMMON
|
2007-06-28 21:14:14 +05:30
|
|
|
static struct option longopts[] = {
|
|
|
|
longopts_COMMON
|
|
|
|
{ NULL, 0, NULL, 0}
|
|
|
|
};
|
2007-09-25 21:51:38 +05:30
|
|
|
static const char * const longopts_help[] = {
|
|
|
|
longopts_help_COMMON
|
|
|
|
};
|
2007-06-28 21:14:14 +05:30
|
|
|
#include "_usage.c"
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *runlevel = NULL;
|
2007-07-21 18:19:51 +05:30
|
|
|
const char *bootlevel = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *newlevel = NULL;
|
|
|
|
char *service = NULL;
|
|
|
|
char **deporder = NULL;
|
2007-09-18 21:13:19 +05:30
|
|
|
char **tmplist;
|
2007-04-11 18:14:47 +05:30
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
bool going_down = false;
|
|
|
|
bool interactive = false;
|
|
|
|
int depoptions = RC_DEP_STRICT | RC_DEP_TRACE;
|
|
|
|
char ksoftbuffer [PATH_MAX];
|
2007-04-20 18:42:21 +05:30
|
|
|
char pidstr[6];
|
2007-06-28 21:14:14 +05:30
|
|
|
int opt;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
atexit (cleanup);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (argv[0])
|
2007-04-19 20:24:35 +05:30
|
|
|
applet = rc_xstrdup (basename (argv[0]));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (! applet)
|
|
|
|
eerrorx ("arguments required");
|
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
/* These used to be programs in their own right, so we shouldn't
|
|
|
|
* touch argc or argv for them */
|
|
|
|
if (strcmp (applet, "env-update") == 0)
|
|
|
|
exit (env_update (argc, argv));
|
|
|
|
else if (strcmp (applet, "fstabinfo") == 0)
|
|
|
|
exit (fstabinfo (argc, argv));
|
|
|
|
else if (strcmp (applet, "mountinfo") == 0)
|
|
|
|
exit (mountinfo (argc, argv));
|
|
|
|
else if (strcmp (applet, "rc-depend") == 0)
|
|
|
|
exit (rc_depend (argc, argv));
|
|
|
|
else if (strcmp (applet, "rc-status") == 0)
|
|
|
|
exit (rc_status (argc, argv));
|
2007-08-04 20:35:12 +05:30
|
|
|
else if (strcmp (applet, "rc-update") == 0 ||
|
|
|
|
strcmp (applet, "update-rc") == 0)
|
|
|
|
exit (rc_update (argc, argv));
|
2007-07-31 21:35:56 +05:30
|
|
|
else if (strcmp (applet, "runscript") == 0)
|
|
|
|
exit (runscript (argc, argv));
|
|
|
|
else if (strcmp (applet, "start-stop-daemon") == 0)
|
|
|
|
exit (start_stop_daemon (argc, argv));
|
2007-09-19 21:57:37 +05:30
|
|
|
else if (strcmp (applet, "checkown") == 0)
|
|
|
|
exit (checkown (argc, argv));
|
2007-07-31 21:35:56 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
/* Handle multicall stuff */
|
|
|
|
if (applet[0] == 'e' || (applet[0] == 'v' && applet[1] == 'e'))
|
|
|
|
exit (do_e (argc, argv));
|
|
|
|
|
|
|
|
if (strncmp (applet, "service_", strlen ("service_")) == 0)
|
|
|
|
exit (do_service (argc, argv));
|
|
|
|
|
|
|
|
if (strcmp (applet, "get_options") == 0 ||
|
|
|
|
strcmp (applet, "save_options") == 0)
|
|
|
|
exit (do_options (argc, argv));
|
|
|
|
|
|
|
|
if (strncmp (applet, "mark_service_", strlen ("mark_service_")) == 0)
|
|
|
|
exit (do_mark_service (argc, argv));
|
|
|
|
|
|
|
|
if (strcmp (applet, "is_runlevel_start") == 0)
|
2007-09-25 23:00:07 +05:30
|
|
|
exit (rc_runlevel_starting () ? 0 : 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
else if (strcmp (applet, "is_runlevel_stop") == 0)
|
2007-09-25 23:00:07 +05:30
|
|
|
exit (rc_runlevel_stopping () ? 0 : 1);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-20 18:42:21 +05:30
|
|
|
if (strcmp (applet, "rc-abort") == 0) {
|
|
|
|
char *p = getenv ("RC_PID");
|
|
|
|
pid_t pid = 0;
|
|
|
|
|
|
|
|
if (p && sscanf (p, "%d", &pid) == 1) {
|
|
|
|
if (kill (pid, SIGUSR1) != 0)
|
|
|
|
eerrorx ("rc-abort: failed to signal parent %d: %s",
|
|
|
|
pid, strerror (errno));
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (strcmp (applet, "rc" ) != 0)
|
|
|
|
eerrorx ("%s: unknown applet", applet);
|
|
|
|
|
2007-06-26 14:50:42 +05:30
|
|
|
/* Change dir to / to ensure all scripts don't use stuff in pwd */
|
|
|
|
chdir ("/");
|
|
|
|
|
2007-07-11 18:32:05 +05:30
|
|
|
/* RUNLEVEL is set by sysvinit as is a magic number
|
|
|
|
RC_SOFTLEVEL is set by us and is the name for this magic number
|
|
|
|
even though all our userland documentation refers to runlevel */
|
|
|
|
RUNLEVEL = getenv ("RUNLEVEL");
|
|
|
|
PREVLEVEL = getenv ("PREVLEVEL");
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Setup a signal handler */
|
|
|
|
signal (SIGINT, handle_signal);
|
|
|
|
signal (SIGQUIT, handle_signal);
|
|
|
|
signal (SIGTERM, handle_signal);
|
2007-04-20 18:42:21 +05:30
|
|
|
signal (SIGUSR1, handle_signal);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Ensure our environment is pure
|
|
|
|
Also, add our configuration to it */
|
|
|
|
env = rc_filter_env ();
|
2007-09-18 21:13:19 +05:30
|
|
|
tmplist = rc_make_env ();
|
|
|
|
rc_strlist_join (&env, tmplist);
|
|
|
|
rc_strlist_free (tmplist);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (env) {
|
|
|
|
char *p;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifdef __linux__
|
2007-04-11 18:14:47 +05:30
|
|
|
/* clearenv isn't portable, but there's no harm in using it
|
|
|
|
if we have it */
|
|
|
|
clearenv ();
|
2007-04-05 16:48:42 +05:30
|
|
|
#else
|
2007-04-11 18:14:47 +05:30
|
|
|
char *var;
|
|
|
|
/* No clearenv present here then.
|
|
|
|
We could manipulate environ directly ourselves, but it seems that
|
|
|
|
some kernels bitch about this according to the environ man pages
|
|
|
|
so we walk though environ and call unsetenv for each value. */
|
|
|
|
while (environ[0]) {
|
|
|
|
tmp = rc_xstrdup (environ[0]);
|
|
|
|
p = tmp;
|
|
|
|
var = strsep (&p, "=");
|
|
|
|
unsetenv (var);
|
|
|
|
free (tmp);
|
|
|
|
}
|
|
|
|
tmp = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
STRLIST_FOREACH (env, p, i)
|
|
|
|
if (strcmp (p, "RC_SOFTLEVEL") != 0 && strcmp (p, "SOFTLEVEL") != 0)
|
|
|
|
putenv (p);
|
|
|
|
|
|
|
|
/* We don't free our list as that would be null in environ */
|
|
|
|
}
|
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
argc++;
|
|
|
|
argv--;
|
|
|
|
while ((opt = getopt_long (argc, argv, getoptstring,
|
|
|
|
longopts, (int *) 0)) != -1)
|
|
|
|
{
|
|
|
|
switch (opt) {
|
|
|
|
case_RC_COMMON_GETOPT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newlevel = argv[optind++];
|
2007-07-10 17:12:56 +05:30
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
/* OK, so we really are the main RC process
|
|
|
|
Only root should be able to run us */
|
|
|
|
if (geteuid () != 0)
|
|
|
|
eerrorx ("%s: root access required", applet);
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Enable logging */
|
|
|
|
setenv ("RC_ELOG", "rc", 1);
|
|
|
|
|
2007-04-20 18:42:21 +05:30
|
|
|
/* Export our PID */
|
|
|
|
snprintf (pidstr, sizeof (pidstr), "%d", getpid ());
|
|
|
|
setenv ("RC_PID", pidstr, 1);
|
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
interactive = rc_exists (INTERACTIVE);
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_plugin_load ();
|
|
|
|
|
2007-07-10 17:12:56 +05:30
|
|
|
/* Load current softlevel */
|
2007-07-21 18:19:51 +05:30
|
|
|
bootlevel = getenv ("RC_BOOTLEVEL");
|
2007-07-10 17:12:56 +05:30
|
|
|
runlevel = rc_get_runlevel ();
|
|
|
|
|
|
|
|
/* Check we're in the runlevel requested, ie from
|
|
|
|
rc single
|
|
|
|
rc shutdown
|
|
|
|
rc reboot
|
|
|
|
*/
|
|
|
|
if (newlevel) {
|
|
|
|
if (strcmp (newlevel, RC_LEVEL_SYSINIT) == 0 &&
|
|
|
|
RUNLEVEL &&
|
|
|
|
(strcmp (RUNLEVEL, "S") == 0 ||
|
|
|
|
strcmp (RUNLEVEL, "1") == 0))
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
/* OK, we're either in runlevel 1 or single user mode */
|
2007-07-10 17:12:56 +05:30
|
|
|
struct utsname uts;
|
2007-04-05 16:48:42 +05:30
|
|
|
#ifdef __linux__
|
2007-07-23 17:06:12 +05:30
|
|
|
char *cmd;
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-07-10 17:12:56 +05:30
|
|
|
/* 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? */
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_exists (INITEARLYSH))
|
2007-07-10 17:12:56 +05:30
|
|
|
run_script (INITEARLYSH);
|
2007-04-26 16:54:07 +05:30
|
|
|
|
2007-07-10 17:12:56 +05:30
|
|
|
uname (&uts);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-10 17:12:56 +05:30
|
|
|
printf ("\n");
|
|
|
|
printf (" %sGentoo/%s; %shttp://www.gentoo.org/%s"
|
|
|
|
"\n Copyright 1999-2007 Gentoo Foundation; "
|
|
|
|
"Distributed under the GPLv2\n\n",
|
2007-09-28 17:59:23 +05:30
|
|
|
ecolor (ECOLOR_GOOD), uts.sysname, ecolor (ECOLOR_BRACKET),
|
|
|
|
ecolor (ECOLOR_NORMAL));
|
2007-04-17 15:02:18 +05:30
|
|
|
|
2007-09-26 12:35:33 +05:30
|
|
|
if (rc_env_bool ("RC_INTERACTIVE"))
|
2007-07-10 17:12:56 +05:30
|
|
|
printf ("Press %sI%s to enter interactive boot mode\n\n",
|
2007-09-28 17:59:23 +05:30
|
|
|
ecolor (ECOLOR_GOOD), ecolor (ECOLOR_NORMAL));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-10 17:12:56 +05:30
|
|
|
setenv ("RC_SOFTLEVEL", newlevel, 1);
|
2007-09-28 17:59:23 +05:30
|
|
|
rc_plugin_run (RC_HOOK_RUNLEVEL_START_IN, newlevel);
|
2007-07-10 17:12:56 +05:30
|
|
|
run_script (INITSH);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#ifdef __linux__
|
2007-07-23 17:06:12 +05:30
|
|
|
/* If we requested a softlevel, save it now */
|
2007-07-10 17:12:56 +05:30
|
|
|
set_ksoftlevel (NULL);
|
2007-07-23 17:06:12 +05:30
|
|
|
if ((cmd = proc_getent ("softlevel"))) {
|
|
|
|
set_ksoftlevel (cmd);
|
|
|
|
free (cmd);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-07-23 17:06:12 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
2007-09-28 17:59:23 +05:30
|
|
|
rc_plugin_run (RC_HOOK_RUNLEVEL_START_OUT, newlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-10 17:12:56 +05:30
|
|
|
if (want_interactive ())
|
|
|
|
mark_interactive ();
|
|
|
|
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
} else if (strcmp (newlevel, RC_LEVEL_SINGLE) == 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! RUNLEVEL ||
|
|
|
|
(strcmp (RUNLEVEL, "S") != 0 &&
|
|
|
|
strcmp (RUNLEVEL, "1") != 0))
|
|
|
|
{
|
|
|
|
/* Remember the current runlevel for when we come back */
|
|
|
|
set_ksoftlevel (runlevel);
|
2007-04-20 18:42:21 +05:30
|
|
|
single_user ();
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
} else if (strcmp (newlevel, RC_LEVEL_REBOOT) == 0) {
|
|
|
|
if (! RUNLEVEL ||
|
|
|
|
strcmp (RUNLEVEL, "6") != 0)
|
|
|
|
{
|
2007-08-28 14:47:04 +05:30
|
|
|
execl (SHUTDOWN, SHUTDOWN, "-r", "now", (char *) NULL);
|
|
|
|
eerrorx ("%s: unable to exec `" SHUTDOWN "': %s",
|
2007-04-20 18:42:21 +05:30
|
|
|
applet, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
} else if (strcmp (newlevel, RC_LEVEL_SHUTDOWN) == 0) {
|
|
|
|
if (! RUNLEVEL ||
|
|
|
|
strcmp (RUNLEVEL, "0") != 0)
|
|
|
|
{
|
2007-08-28 14:47:04 +05:30
|
|
|
execl (SHUTDOWN, SHUTDOWN,
|
2007-08-28 18:40:07 +05:30
|
|
|
#ifdef __linux__
|
2007-04-20 18:42:21 +05:30
|
|
|
"-h",
|
2007-04-05 16:48:42 +05:30
|
|
|
#else
|
2007-04-20 18:42:21 +05:30
|
|
|
"-p",
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
2007-04-20 18:42:21 +05:30
|
|
|
"now", (char *) NULL);
|
2007-08-28 14:47:04 +05:30
|
|
|
eerrorx ("%s: unable to exec `" SHUTDOWN "': %s",
|
2007-04-20 18:42:21 +05:30
|
|
|
applet, strerror (errno));
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-21 00:28:42 +05:30
|
|
|
/* Now we start handling our children */
|
|
|
|
signal (SIGCHLD, handle_signal);
|
|
|
|
|
2007-07-10 17:12:56 +05:30
|
|
|
/* We should only use ksoftlevel if we were in single user mode
|
|
|
|
If not, we need to erase ksoftlevel now. */
|
|
|
|
if (PREVLEVEL &&
|
|
|
|
(strcmp (PREVLEVEL, "1") == 0 ||
|
|
|
|
strcmp (PREVLEVEL, "S") == 0 ||
|
|
|
|
strcmp (PREVLEVEL, "N") == 0))
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
2007-08-28 18:36:44 +05:30
|
|
|
/* Try not to join boot and ksoftlevels together */
|
|
|
|
if (! newlevel ||
|
|
|
|
strcmp (newlevel, getenv ("RC_BOOTLEVEL")) != 0)
|
|
|
|
if (get_ksoftlevel (ksoftbuffer, sizeof (ksoftbuffer)))
|
|
|
|
newlevel = ksoftbuffer;
|
2007-07-10 17:12:56 +05:30
|
|
|
} else if (! RUNLEVEL ||
|
|
|
|
(strcmp (RUNLEVEL, "1") != 0 &&
|
|
|
|
strcmp (RUNLEVEL, "S") != 0 &&
|
|
|
|
strcmp (RUNLEVEL, "N") != 0))
|
|
|
|
{
|
|
|
|
set_ksoftlevel (NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (newlevel &&
|
|
|
|
(strcmp (newlevel, RC_LEVEL_REBOOT) == 0 ||
|
|
|
|
strcmp (newlevel, RC_LEVEL_SHUTDOWN) == 0 ||
|
|
|
|
strcmp (newlevel, RC_LEVEL_SINGLE) == 0))
|
|
|
|
{
|
|
|
|
going_down = true;
|
|
|
|
rc_set_runlevel (newlevel);
|
|
|
|
setenv ("RC_SOFTLEVEL", newlevel, 1);
|
2007-09-28 17:59:23 +05:30
|
|
|
rc_plugin_run (RC_HOOK_RUNLEVEL_STOP_IN, newlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else {
|
2007-09-28 17:59:23 +05:30
|
|
|
rc_plugin_run (RC_HOOK_RUNLEVEL_STOP_IN, runlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if runlevel is valid if we're changing */
|
|
|
|
if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) {
|
|
|
|
tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel, (char *) NULL);
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! rc_is_dir (tmp))
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: is not a valid runlevel", newlevel);
|
|
|
|
CHAR_FREE (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load our deptree now */
|
2007-09-29 22:12:08 +05:30
|
|
|
if ((deptree = _rc_deptree_load ()) == NULL)
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("failed to load deptree");
|
|
|
|
|
|
|
|
/* Clean the failed services state dir now */
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_is_dir (RC_SVCDIR "/failed"))
|
2007-08-28 21:58:33 +05:30
|
|
|
rc_rm_dir (RC_SVCDIR "/failed", false);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-08-28 14:43:46 +05:30
|
|
|
mkdir (RC_STOPPING, 0755);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifdef __linux__
|
2007-04-11 18:14:47 +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 */
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_is_dir (DEVBOOT)) {
|
2007-09-18 17:34:51 +05:30
|
|
|
start_services = rc_ls_dir (DEVBOOT, RC_LS_INITD);
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_rm_dir (DEVBOOT, true);
|
|
|
|
|
|
|
|
STRLIST_FOREACH (start_services, service, i)
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_allow_plug (service))
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (service, RC_SERVICE_COLDPLUGGED);
|
2007-04-11 18:14:47 +05:30
|
|
|
/* We need to dump this list now.
|
|
|
|
This may seem redunant, but only Linux needs this and saves on
|
|
|
|
code bloat. */
|
|
|
|
rc_strlist_free (start_services);
|
|
|
|
start_services = NULL;
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
#else
|
2007-04-11 18:14:47 +05:30
|
|
|
/* BSD's on the other hand populate /dev automagically and use devd.
|
|
|
|
The only downside of this approach and ours is that we have to hard code
|
|
|
|
the device node to the init script to simulate the coldplug into
|
|
|
|
runlevel for our dependency tree to work. */
|
2007-07-21 18:19:51 +05:30
|
|
|
if (newlevel && strcmp (newlevel, bootlevel) == 0 &&
|
2007-04-11 18:14:47 +05:30
|
|
|
(strcmp (runlevel, RC_LEVEL_SINGLE) == 0 ||
|
|
|
|
strcmp (runlevel, RC_LEVEL_SYSINIT) == 0) &&
|
2007-09-26 12:35:33 +05:30
|
|
|
rc_env_bool ("RC_COLDPLUG"))
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
2007-07-26 02:28:23 +05:30
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__)
|
2007-04-11 18:14:47 +05:30
|
|
|
/* The net interfaces are easy - they're all in net /dev/net :) */
|
2007-09-18 17:34:51 +05:30
|
|
|
start_services = rc_ls_dir ("/dev/net", 0);
|
2007-04-11 18:14:47 +05:30
|
|
|
STRLIST_FOREACH (start_services, service, i) {
|
|
|
|
j = (strlen ("net.") + strlen (service) + 1);
|
|
|
|
tmp = rc_xmalloc (sizeof (char *) * j);
|
|
|
|
snprintf (tmp, j, "net.%s", service);
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_service_exists (tmp) && rc_allow_plug (tmp))
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (tmp, RC_SERVICE_COLDPLUGGED);
|
2007-04-11 18:14:47 +05:30
|
|
|
CHAR_FREE (tmp);
|
|
|
|
}
|
|
|
|
rc_strlist_free (start_services);
|
2007-07-26 02:28:23 +05:30
|
|
|
#endif
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* The mice are a little more tricky.
|
|
|
|
If we coldplug anything else, we'll probably do it here. */
|
2007-09-18 17:34:51 +05:30
|
|
|
start_services = rc_ls_dir ("/dev", 0);
|
2007-04-11 18:14:47 +05:30
|
|
|
STRLIST_FOREACH (start_services, service, i) {
|
|
|
|
if (strncmp (service, "psm", 3) == 0 ||
|
|
|
|
strncmp (service, "ums", 3) == 0)
|
|
|
|
{
|
|
|
|
char *p = service + 3;
|
|
|
|
if (p && isdigit (*p)) {
|
|
|
|
j = (strlen ("moused.") + strlen (service) + 1);
|
|
|
|
tmp = rc_xmalloc (sizeof (char *) * j);
|
|
|
|
snprintf (tmp, j, "moused.%s", service);
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_service_exists (tmp) && rc_allow_plug (tmp))
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (tmp, RC_SERVICE_COLDPLUGGED);
|
2007-04-11 18:14:47 +05:30
|
|
|
CHAR_FREE (tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc_strlist_free (start_services);
|
|
|
|
start_services = NULL;
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Build a list of all services to stop and then work out the
|
|
|
|
correct order for stopping them */
|
2007-09-18 17:34:51 +05:30
|
|
|
stop_services = rc_ls_dir (RC_SVCDIR_STARTING, RC_LS_INITD);
|
2007-09-18 21:13:19 +05:30
|
|
|
|
|
|
|
tmplist = rc_ls_dir (RC_SVCDIR_INACTIVE, RC_LS_INITD);
|
|
|
|
rc_strlist_join (&stop_services, tmplist);
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
|
|
|
|
tmplist = rc_ls_dir (RC_SVCDIR_STARTED, RC_LS_INITD);
|
|
|
|
rc_strlist_join (&stop_services, tmplist);
|
|
|
|
rc_strlist_free (tmplist);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
types = NULL;
|
|
|
|
rc_strlist_add (&types, "ineed");
|
|
|
|
rc_strlist_add (&types, "iuse");
|
|
|
|
rc_strlist_add (&types, "iafter");
|
2007-09-18 21:13:19 +05:30
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
deporder = rc_deptree_depends (deptree, types, stop_services,
|
2007-07-21 18:19:51 +05:30
|
|
|
runlevel, depoptions | RC_DEP_STOP);
|
2007-09-18 21:13:19 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_strlist_free (stop_services);
|
|
|
|
rc_strlist_free (types);
|
2007-09-18 17:06:55 +05:30
|
|
|
types = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
stop_services = deporder;
|
|
|
|
deporder = NULL;
|
|
|
|
rc_strlist_reverse (stop_services);
|
|
|
|
|
|
|
|
/* Load our list of coldplugged services */
|
2007-09-18 17:34:51 +05:30
|
|
|
coldplugged_services = rc_ls_dir (RC_SVCDIR_COLDPLUGGED, RC_LS_INITD);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Load our start services now.
|
|
|
|
We have different rules dependent on runlevel. */
|
2007-07-21 18:19:51 +05:30
|
|
|
if (newlevel && strcmp (newlevel, bootlevel) == 0) {
|
2007-04-11 18:14:47 +05:30
|
|
|
if (coldplugged_services) {
|
|
|
|
einfon ("Device initiated services:");
|
|
|
|
STRLIST_FOREACH (coldplugged_services, service, i) {
|
|
|
|
printf (" %s", service);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&start_services, service);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel ? newlevel : runlevel,
|
|
|
|
(char *) NULL);
|
2007-09-18 21:13:19 +05:30
|
|
|
tmplist = rc_ls_dir (tmp, RC_LS_INITD);
|
|
|
|
rc_strlist_join (&start_services, tmplist);
|
|
|
|
rc_strlist_free (tmplist);
|
2007-04-11 18:14:47 +05:30
|
|
|
CHAR_FREE (tmp);
|
|
|
|
} else {
|
|
|
|
/* Store our list of coldplugged services */
|
2007-09-24 14:37:00 +05:30
|
|
|
tmplist = rc_ls_dir (RC_SVCDIR_COLDPLUGGED, RC_LS_INITD);
|
|
|
|
rc_strlist_join (&coldplugged_services, tmplist);
|
|
|
|
rc_strlist_free (tmplist);
|
2007-04-11 18:14:47 +05:30
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* We need to include the boot runlevel services if we're not in it */
|
2007-09-18 21:13:19 +05:30
|
|
|
tmplist = rc_services_in_runlevel (bootlevel);
|
|
|
|
rc_strlist_join (&start_services, tmplist);
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
tmplist = rc_services_in_runlevel (newlevel ? newlevel : runlevel);
|
|
|
|
rc_strlist_join (&start_services, tmplist);
|
|
|
|
rc_strlist_free (tmplist);
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
STRLIST_FOREACH (coldplugged_services, service, i)
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&start_services, service);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save out softlevel now */
|
|
|
|
if (going_down)
|
|
|
|
rc_set_runlevel (newlevel);
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
types = NULL;
|
|
|
|
rc_strlist_add (&types, "needsme");
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Now stop the services that shouldn't be running */
|
|
|
|
STRLIST_FOREACH (stop_services, service, i) {
|
|
|
|
bool found = false;
|
|
|
|
char *conf = NULL;
|
|
|
|
char **stopdeps = NULL;
|
|
|
|
char *svc1 = NULL;
|
|
|
|
char *svc2 = NULL;
|
|
|
|
int k;
|
|
|
|
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (service) & RC_SERVICE_STOPPED)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We always stop the service when in these runlevels */
|
|
|
|
if (going_down) {
|
2007-09-29 22:24:58 +05:30
|
|
|
pid_t pid = rc_service_stop (service);
|
2007-09-26 12:35:33 +05:30
|
|
|
if (pid > 0 && ! rc_env_bool ("RC_PARALLEL"))
|
2007-04-21 00:28:42 +05:30
|
|
|
rc_waitpid (pid);
|
2007-04-26 16:54:07 +05:30
|
|
|
continue;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're in the start list then don't bother stopping us */
|
|
|
|
STRLIST_FOREACH (start_services, svc1, j)
|
|
|
|
if (strcmp (svc1, service) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unless we would use a different config file */
|
|
|
|
if (found) {
|
|
|
|
int len;
|
|
|
|
if (! newlevel)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
len = strlen (service) + strlen (runlevel) + 2;
|
|
|
|
tmp = rc_xmalloc (sizeof (char *) * len);
|
|
|
|
snprintf (tmp, len, "%s.%s", service, runlevel);
|
|
|
|
conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL);
|
2007-09-25 23:00:07 +05:30
|
|
|
found = rc_exists (conf);
|
2007-04-11 18:14:47 +05:30
|
|
|
CHAR_FREE (conf);
|
|
|
|
CHAR_FREE (tmp);
|
|
|
|
if (! found) {
|
|
|
|
len = strlen (service) + strlen (newlevel) + 2;
|
|
|
|
tmp = rc_xmalloc (sizeof (char *) * len);
|
|
|
|
snprintf (tmp, len, "%s.%s", service, newlevel);
|
|
|
|
conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL);
|
2007-09-25 23:00:07 +05:30
|
|
|
found = rc_exists (conf);
|
2007-04-11 18:14:47 +05:30
|
|
|
CHAR_FREE (conf);
|
|
|
|
CHAR_FREE (tmp);
|
|
|
|
if (!found)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Allow coldplugged services not to be in the runlevels list */
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (service) & RC_SERVICE_COLDPLUGGED)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We got this far! Or last check is to see if any any service that
|
|
|
|
going to be started depends on us */
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&stopdeps, service);
|
2007-09-29 22:12:08 +05:30
|
|
|
deporder = rc_deptree_depends (deptree, types, stopdeps,
|
2007-04-11 18:14:47 +05:30
|
|
|
runlevel, RC_DEP_STRICT);
|
|
|
|
rc_strlist_free (stopdeps);
|
|
|
|
stopdeps = NULL;
|
|
|
|
found = false;
|
|
|
|
STRLIST_FOREACH (deporder, svc1, j) {
|
|
|
|
STRLIST_FOREACH (start_services, svc2, k)
|
|
|
|
if (strcmp (svc1, svc2) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc_strlist_free (deporder);
|
|
|
|
deporder = NULL;
|
|
|
|
|
|
|
|
/* After all that we can finally stop the blighter! */
|
2007-04-21 00:28:42 +05:30
|
|
|
if (! found) {
|
2007-09-29 22:24:58 +05:30
|
|
|
pid_t pid = rc_service_stop (service);
|
2007-09-26 12:35:33 +05:30
|
|
|
if (pid > 0 && ! rc_env_bool ("RC_PARALLEL"))
|
2007-04-21 00:28:42 +05:30
|
|
|
rc_waitpid (pid);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
rc_strlist_free (types);
|
|
|
|
types = NULL;
|
|
|
|
|
|
|
|
/* Wait for our services to finish */
|
2007-04-21 00:28:42 +05:30
|
|
|
wait_for_services ();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Notify the plugins we have finished */
|
2007-09-28 17:59:23 +05:30
|
|
|
rc_plugin_run (RC_HOOK_RUNLEVEL_STOP_OUT, runlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-08-28 14:43:46 +05:30
|
|
|
rmdir (RC_STOPPING);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Store the new runlevel */
|
|
|
|
if (newlevel) {
|
|
|
|
rc_set_runlevel (newlevel);
|
|
|
|
runlevel = newlevel;
|
|
|
|
setenv ("RC_SOFTLEVEL", runlevel, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run the halt script if needed */
|
|
|
|
if (strcmp (runlevel, RC_LEVEL_SHUTDOWN) == 0 ||
|
|
|
|
strcmp (runlevel, RC_LEVEL_REBOOT) == 0)
|
|
|
|
{
|
2007-04-20 18:42:21 +05:30
|
|
|
execl (HALTSH, HALTSH, runlevel, (char *) NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
eerrorx ("%s: unable to exec `%s': %s",
|
|
|
|
applet, HALTSH, strerror (errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Single user is done now */
|
|
|
|
if (strcmp (runlevel, RC_LEVEL_SINGLE) == 0) {
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_exists (INTERACTIVE))
|
2007-04-11 18:14:47 +05:30
|
|
|
unlink (INTERACTIVE);
|
|
|
|
sulogin (false);
|
|
|
|
}
|
|
|
|
|
2007-08-28 14:43:46 +05:30
|
|
|
mkdir (RC_STARTING, 0755);
|
2007-09-28 17:59:23 +05:30
|
|
|
rc_plugin_run (RC_HOOK_RUNLEVEL_START_IN, runlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Re-add our coldplugged services if they stopped */
|
|
|
|
STRLIST_FOREACH (coldplugged_services, service, i)
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (service, RC_SERVICE_COLDPLUGGED);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Order the services to start */
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&types, "ineed");
|
|
|
|
rc_strlist_add (&types, "iuse");
|
|
|
|
rc_strlist_add (&types, "iafter");
|
2007-09-29 22:12:08 +05:30
|
|
|
deporder = rc_deptree_depends (deptree, types, start_services,
|
2007-07-21 18:19:51 +05:30
|
|
|
runlevel, depoptions | RC_DEP_START);
|
2007-04-11 18:14:47 +05:30
|
|
|
rc_strlist_free (types);
|
|
|
|
types = NULL;
|
|
|
|
rc_strlist_free (start_services);
|
|
|
|
start_services = deporder;
|
|
|
|
deporder = NULL;
|
|
|
|
|
2007-07-23 17:06:12 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
/* mark any services skipped as started */
|
|
|
|
if (PREVLEVEL && strcmp (PREVLEVEL, "N") == 0) {
|
|
|
|
if ((service = proc_getent ("noinitd"))) {
|
|
|
|
char *p = service;
|
|
|
|
char *token;
|
|
|
|
|
|
|
|
while ((token = strsep (&p, ",")))
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (token, RC_SERVICE_STARTED);
|
2007-07-23 17:06:12 +05:30
|
|
|
free (service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
STRLIST_FOREACH (start_services, service, i) {
|
2007-09-28 20:23:38 +05:30
|
|
|
if (rc_service_state (service) & RC_SERVICE_STOPPED) {
|
2007-04-21 00:28:42 +05:30
|
|
|
pid_t pid;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! interactive)
|
|
|
|
interactive = want_interactive ();
|
|
|
|
|
|
|
|
if (interactive) {
|
2007-04-05 16:48:42 +05:30
|
|
|
interactive_retry:
|
2007-04-11 18:14:47 +05:30
|
|
|
printf ("\n");
|
|
|
|
einfo ("About to start the service %s", service);
|
|
|
|
eindent ();
|
|
|
|
einfo ("1) Start the service\t\t2) Skip the service");
|
|
|
|
einfo ("3) Continue boot process\t\t4) Exit to shell");
|
|
|
|
eoutdent ();
|
2007-04-05 16:48:42 +05:30
|
|
|
interactive_option:
|
2007-04-11 18:14:47 +05:30
|
|
|
switch (read_key (true)) {
|
|
|
|
case '1': break;
|
|
|
|
case '2': continue;
|
|
|
|
case '3': interactive = false; break;
|
|
|
|
case '4': sulogin (true); goto interactive_retry;
|
|
|
|
default: goto interactive_option;
|
|
|
|
}
|
|
|
|
}
|
2007-04-21 00:28:42 +05:30
|
|
|
|
|
|
|
/* Remember the pid if we're running in parallel */
|
2007-09-29 22:24:58 +05:30
|
|
|
if ((pid = rc_service_start (service)))
|
2007-04-21 00:28:42 +05:30
|
|
|
add_pid (pid);
|
|
|
|
|
2007-09-26 12:35:33 +05:30
|
|
|
if (! rc_env_bool ("RC_PARALLEL")) {
|
2007-04-21 00:28:42 +05:30
|
|
|
rc_waitpid (pid);
|
|
|
|
remove_pid (pid);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for our services to finish */
|
2007-04-20 15:09:47 +05:30
|
|
|
wait_for_services ();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-28 17:59:23 +05:30
|
|
|
rc_plugin_run (RC_HOOK_RUNLEVEL_START_OUT, runlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-23 17:06:12 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
/* mark any services skipped as stopped */
|
|
|
|
if (PREVLEVEL && strcmp (PREVLEVEL, "N") == 0) {
|
|
|
|
if ((service = proc_getent ("noinitd"))) {
|
|
|
|
char *p = service;
|
|
|
|
char *token;
|
|
|
|
|
|
|
|
while ((token = strsep (&p, ",")))
|
2007-09-29 22:24:58 +05:30
|
|
|
rc_service_mark (token, RC_SERVICE_STOPPED);
|
2007-07-23 17:06:12 +05:30
|
|
|
free (service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Store our interactive status for boot */
|
2007-07-21 18:19:51 +05:30
|
|
|
if (interactive && strcmp (runlevel, bootlevel) == 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
mark_interactive ();
|
|
|
|
else {
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_exists (INTERACTIVE))
|
2007-04-11 18:14:47 +05:30
|
|
|
unlink (INTERACTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (EXIT_SUCCESS);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|