*
* Version: @(#)init.c 2.86 30-Jul-2004 miquels@cistron.nl
*/
-#define VERSION "2.88"
-#define DATE "31-Jul-2004"
+#define VERSION "2.89"
+#define DATE "26-Mar-2010"
/*
* This file is part of the sysvinit suite,
* Copyright (C) 1991-2004 Miquel van Smoorenburg.
#include <utmp.h>
#include <ctype.h>
#include <stdarg.h>
+#include <sys/ttydefaults.h>
#include <sys/syslog.h>
#include <sys/time.h>
#ifdef WITH_SELINUX
-#include <selinux/selinux.h>
+# include <selinux/selinux.h>
#endif
-
#ifdef __i386__
# ifdef __GLIBC__
/* GNU libc 2.x */
/* Set a signal handler. */
#define SETSIG(sa, sig, fun, flags) \
do { \
+ memset(&sa, 0, sizeof(sa)); \
sa.sa_handler = fun; \
sa.sa_flags = flags; \
sigemptyset(&sa.sa_mask); \
}
static
-char *istrdup(char *s)
+char *istrdup(const char *s)
{
char *m;
int l;
f = fdopen(fd, "r");
- if (get_cmd(f) != C_VER)
+ if (get_cmd(f) != C_VER) {
+ fclose(f);
return -1;
+ }
get_string(old_version, sizeof(old_version), f);
oops_error = 0;
for (pp = &family; (*pp = get_record(f)) != NULL; pp = &((*pp)->next))
*/
for(f = 0; f < 5; f++) {
if ((fd = open(console_dev, m)) >= 0) break;
- usleep(100);
+ usleep(10000);
}
if (fd < 0) return fd;
return;
}
+#ifdef __FreeBSD_kernel__
+ /*
+ * The kernel of FreeBSD expects userland to set TERM. Usually, we want
+ * "cons25". Later, gettys might disagree on this (i.e. we're not using
+ * syscons) but some boot scripts, like /etc/init.d/xserver-xorg, still
+ * need a non-dumb terminal.
+ */
+ putenv ("TERM=cons25");
+#endif
+
(void) tcgetattr(fd, &tty);
tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
tty.c_cc[VEOF] = CEOF;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
+#ifdef VSWTC /* not defined on FreeBSD */
tty.c_cc[VSWTC] = _POSIX_VDISABLE;
+#endif /* VSWTC */
tty.c_cc[VSTART] = CSTART;
tty.c_cc[VSTOP] = CSTOP;
tty.c_cc[VSUSP] = CSUSP;
/*
* Set pre and post processing
*/
- tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY;
-#ifdef IUTF8 /* Not defined on FreeBSD */
- tty.c_iflag |= IUTF8;
+ tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY
+#ifdef IUTF8 /* Not defined on FreeBSD */
+ | (tty.c_iflag & IUTF8)
#endif /* IUTF8 */
+ ;
tty.c_oflag = OPOST|ONLCR;
tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE;
(void) close(fd);
}
+static ssize_t
+safe_write(int fd, const char *buffer, size_t count)
+{
+ ssize_t offset = 0;
+
+ while (count > 0) {
+ ssize_t block = write(fd, &buffer[offset], count);
+
+ if (block < 0 && errno == EINTR)
+ continue;
+ if (block <= 0)
+ return offset ? offset : block;
+ offset += block;
+ count -= block;
+ }
+ return offset;
+}
+
/*
* Print to the system console
*/
int fd;
if ((fd = console_open(O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
- write(fd, s, strlen(s));
+ safe_write(fd, s, strlen(s));
close(fd);
}
}
}
}
+/*
+ * Add or replace specific environment value
+ */
+int addnewenv(const char *new, char **curr, int n)
+{
+ size_t nlen = strcspn(new, "=");
+ int i;
+ for (i = 0; i < n; i++) {
+ if (nlen != strcspn(curr[i], "="))
+ continue;
+ if (strncmp (new, curr[i], nlen) == 0)
+ break;
+ }
+ if (i >= n)
+ curr[n++] = istrdup(new);
+ else {
+ free(curr[i]);
+ curr[i] = istrdup(new);
+ }
+ return n;
+}
/*
* Build a new environment for execve().
{
char i_lvl[] = "RUNLEVEL=x";
char i_prev[] = "PREVLEVEL=x";
- char i_cons[32];
+ char i_cons[128];
char i_shell[] = "SHELL=" SHELL;
char **e;
int n, i;
for (n = 0; environ[n]; n++)
;
- n += NR_EXTRA_ENV + 8;
- e = calloc(n, sizeof(char *));
+ n += NR_EXTRA_ENV + 1; /* Also room for last NULL */
+ if (child)
+ n += 8;
+
+ while ((e = (char**)calloc(n, sizeof(char *))) == NULL) {
+ initlog(L_VB, "out of memory");
+ do_sleep(5);
+ }
for (n = 0; environ[n]; n++)
e[n] = istrdup(environ[n]);
- for (i = 0; i < NR_EXTRA_ENV; i++)
- if (extra_env[i])
- e[n++] = istrdup(extra_env[i]);
+ for (i = 0; i < NR_EXTRA_ENV; i++) {
+ if (extra_env[i] == NULL || *extra_env[i] == '\0')
+ continue;
+ n = addnewenv(extra_env[i], e, n);
+ }
if (child) {
snprintf(i_cons, sizeof(i_cons), "CONSOLE=%s", console_dev);
i_lvl[9] = thislevel;
i_prev[10] = prevlevel;
- e[n++] = istrdup(i_shell);
- e[n++] = istrdup(i_lvl);
- e[n++] = istrdup(i_prev);
- e[n++] = istrdup(i_cons);
- e[n++] = istrdup(E_VERSION);
+ n = addnewenv(i_shell, e, n);
+ n = addnewenv(i_lvl, e, n);
+ n = addnewenv(i_prev, e, n);
+ n = addnewenv(i_cons, e, n);
+ n = addnewenv(E_VERSION, e, n);
}
e[n++] = NULL;
*
*/
static
-int spawn(CHILD *ch, int *res)
+pid_t spawn(CHILD *ch, int *res)
{
char *args[16]; /* Argv array */
char buf[136]; /* Line buffer */
- int f, st, rc; /* Scratch variables */
+ int f, st; /* Scratch variables */
char *ptr; /* Ditto */
time_t t; /* System time */
int oldAlarm; /* Previous alarm value */
sigprocmask(SIG_SETMASK, &omask, NULL);
- /*
- * Update utmp/wtmp file prior to starting
- * any child. This MUST be done right here in
- * the child process in order to prevent a race
- * condition that occurs when the child
- * process' time slice executes before the
- * parent (can and does happen in a uniprocessor
- * environment). If the child is a getty and
- * the race condition happens, then init's utmp
- * update will happen AFTER the getty runs
- * and expects utmp to be updated already!
- *
- * Do NOT log if process field starts with '+'
- * FIXME: that's for compatibility with *very*
- * old getties - probably it can be taken out.
- */
- if (ch->action == RESPAWN && ch->process[0] != '+')
- write_utmp_wtmp("", ch->id, getpid(), INIT_PROCESS, "");
-
/*
* In sysinit, boot, bootwait or single user mode:
* for any wait-type subprocess we _force_ the console
exit(1);
}
if (pid > 0) {
+ pid_t rc;
/*
* Ignore keyboard signals etc.
* Then wait for child to exit.
dup(f);
}
+ /*
+ * Update utmp/wtmp file prior to starting
+ * any child. This MUST be done right here in
+ * the child process in order to prevent a race
+ * condition that occurs when the child
+ * process' time slice executes before the
+ * parent (can and does happen in a uniprocessor
+ * environment). If the child is a getty and
+ * the race condition happens, then init's utmp
+ * update will happen AFTER the getty runs
+ * and expects utmp to be updated already!
+ *
+ * Do NOT log if process field starts with '+'
+ * FIXME: that's for compatibility with *very*
+ * old getties - probably it can be taken out.
+ */
+ if (ch->process[0] != '+')
+ write_utmp_wtmp("", ch->id, getpid(), INIT_PROCESS, "");
+
/* Reset all the signals, set up environment */
for(f = 1; f < NSIG; f++) SETSIG(sa, f, SIG_DFL, SA_RESTART);
environ = init_buildenv(1);
execvp(args[1], args + 1);
}
initlog(L_VB, "cannot execute \"%s\"", args[1]);
+
+ if (ch->process[0] != '+')
+ write_utmp_wtmp("", ch->id, getpid(), DEAD_PROCESS, NULL);
exit(1);
}
*res = pid;
case POWEROKWAIT:
case CTRLALTDEL:
if (!(ch->flags & XECUTED)) ch->flags |= WAITING;
+ /* Fall through */
case KBREQUEST:
case BOOT:
case POWERFAIL:
#endif
/*
- * Open INITTAB and real line by line.
+ * Open INITTAB and read line by line.
*/
if ((fp = fopen(INITTAB, "r")) == NULL)
initlog(L_VB, "No inittab file found");
case 0: /* Send TERM signal */
if (talk)
initlog(L_CO,
- "Sending processes the TERM signal");
+ "Sending processes configured via /etc/inittab the TERM signal");
kill(-(ch->pid), SIGTERM);
foundOne = 1;
break;
case 1: /* Send KILL signal and collect status */
if (talk)
initlog(L_CO,
- "Sending processes the KILL signal");
+ "Sending processes configured via /etc/inittab the KILL signal");
kill(-(ch->pid), SIGKILL);
break;
}
if (fd < 0) return('S');
while(!strchr("0123456789S", lvl)) {
- write(fd, prompt, sizeof(prompt) - 1);
- buf[0] = 0;
- read(fd, buf, sizeof(buf));
+ safe_write(fd, prompt, sizeof(prompt) - 1);
+ if (read(fd, buf, sizeof(buf)) <= 0)
+ buf[0] = 0;
if (buf[0] != 0 && (buf[1] == '\r' || buf[1] == '\n'))
lvl = buf[0];
if (islower(lvl)) lvl = toupper(lvl);
{
int fds[2];
- pipe(fds);
+ if (pipe(fds)) {
+ initlog(L_VB, "pipe: %m");
+ return -1;
+ }
dup2(fds[0], fd);
close(fds[0]);
fcntl(fds[1], F_SETFD, 1);
fcntl(fd, F_SETFD, 0);
- write(fds[1], Signature, 8);
+ safe_write(fds[1], Signature, 8);
return fds[1];
}
/*
* construct a pipe fd --> STATE_PIPE and write a signature
*/
- fd = make_pipe(STATE_PIPE);
+ if ((fd = make_pipe(STATE_PIPE)) < 0) {
+ sigprocmask(SIG_SETMASK, &oldset, NULL);
+ initlog(L_CO, "Attempt to re-exec failed");
+ }
/*
* It's a backup day today, so I'm pissed off. Being a BOFH, however,
* We shouldn't be here, something failed.
* Bitch, close the state pipe, unblock signals and return.
*/
+ init_freeenv(env);
close(fd);
close(STATE_PIPE);
sigprocmask(SIG_SETMASK, &oldset, NULL);
- init_freeenv(env);
initlog(L_CO, "Attempt to re-exec failed");
}
static
void initcmd_setenv(char *data, int size)
{
- char *env, *p, *e, *eq;
- int i, sz;
+ char *env, *p, *e;
+ size_t sz;
+ int i, eq;
e = data + size;
while (*data && data < e) {
- eq = NULL;
for (p = data; *p && p < e; p++)
- if (*p == '=') eq = p;
+ ;
if (*p) break;
env = data;
data = ++p;
- sz = eq ? (eq - env) : (p - env);
-
- /*initlog(L_SY, "init_setenv: %s, %s, %d", env, eq, sz);*/
-
/*
* We only allow INIT_* to be set.
*/
if (strncmp(env, "INIT_", 5) != 0)
continue;
+ sz = strcspn(env, "=");
+ eq = (env[sz] == '=');
+
+ /*initlog(L_SY, "init_setenv: %s, %d, %d", env, eq, sz);*/
+
/* Free existing vars. */
for (i = 0; i < NR_EXTRA_ENV; i++) {
- if (extra_env[i] == NULL) continue;
- if (!strncmp(extra_env[i], env, sz) &&
- extra_env[i][sz] == '=') {
+ if (extra_env[i] == NULL)
+ continue;
+ if (sz != strcspn(extra_env[i], "="))
+ continue;
+ if (strncmp(extra_env[i], env, sz) == 0) {
free(extra_env[i]);
extra_env[i] = NULL;
}
}
+ if (eq == 0)
+ continue;
+
/* Set new vars if needed. */
- if (eq == NULL) continue;
for (i = 0; i < NR_EXTRA_ENV; i++) {
if (extra_env[i] == NULL) {
extra_env[i] = istrdup(env);
/* See _what_ kind of SIGPWR this is. */
pwrstat = 0;
if ((fd = open(PWRSTAT, O_RDONLY)) >= 0) {
- c = 0;
- read(fd, &c, 1);
+ if (read(fd, &c, 1) != 1)
+ c = 0;
pwrstat = c;
close(fd);
unlink(PWRSTAT);
- }
+ } else if ((fd = open(PWRSTAT_OLD, O_RDONLY)) >= 0) {
+ /* Path changed 2010-03-20. Look for the old path for a while. */
+ initlog(L_VB, "warning: found obsolete path %s, use %s instead",
+ PWRSTAT_OLD, PWRSTAT);
+ if (read(fd, &c, 1) != 1)
+ c = 0;
+ pwrstat = c;
+ close(fd);
+ unlink(PWRSTAT_OLD);
+ }
do_power_fail(pwrstat);
DELSET(got_signals, SIGPWR);
}
CHILD *ch;
struct sigaction sa;
sigset_t sgt;
- pid_t rc;
int f, st;
if (!reload) {
while((rc = wait(&st)) != f)
if (rc < 0 && errno == ECHILD)
break;
- write(1, killmsg, sizeof(killmsg) - 1);
+ safe_write(1, killmsg, sizeof(killmsg) - 1);
while(1) pause();
}
#endif
* See if we have to start an emergency shell.
*/
if (emerg_shell) {
+ pid_t rc;
SETSIG(sa, SIGCHLD, SIG_DFL, SA_RESTART);
if (spawn(&ch_emerg, &f) > 0) {
while((rc = wait(&st)) != f)
if (!strchr("0123456789SsQqAaBbCcUu", argv[optind][0]))
usage(progname);
request.cmd = INIT_CMD_RUNLVL;
- request.runlevel = env ? 0 : argv[optind][0];
+ request.runlevel = argv[optind][0];
request.sleeptime = sltime;
}
p = argv[0];
/* Common umask */
- umask(022);
+ umask(umask(077) | 022);
/* Quick check */
if (geteuid() != 0) {
}
#ifdef WITH_SELINUX
- if (getenv("SELINUX_INIT") == NULL && !is_selinux_enabled()) {
- putenv("SELINUX_INIT=YES");
- if (selinux_init_load_policy(&enforce) == 0 ) {
- execv(myname, argv);
- } else {
- if (enforce > 0) {
- /* SELinux in enforcing mode but load_policy failed */
- /* At this point, we probably can't open /dev/console, so log() won't work */
- fprintf(stderr,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.\n");
- exit(1);
+ if (getenv("SELINUX_INIT") == NULL) {
+ if (is_selinux_enabled() != 1) {
+ if (selinux_init_load_policy(&enforce) == 0) {
+ putenv("SELINUX_INIT=YES");
+ execv(myname, argv);
+ } else {
+ if (enforce > 0) {
+ /* SELinux in enforcing mode but load_policy failed */
+ /* At this point, we probably can't open /dev/console, so log() won't work */
+ fprintf(stderr,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.\n");
+ exit(1);
+ }
}
}
}