2
* Init A System-V Init Clone.
5
* init [0123456SsQqAaBbCc]
6
* telinit [0123456SsQqAaBbCc]
8
* Version: @(#)init.c 2.86 30-Jul-2004 miquels@cistron.nl
10
#define VERSION "2.88"
11
#define DATE "26-Mar-2010"
13
* This file is part of the sysvinit suite,
14
* Copyright (C) 1991-2004 Miquel van Smoorenburg.
16
* This program is free software; you can redistribute it and/or modify
17
* it under the terms of the GNU General Public License as published by
18
* the Free Software Foundation; either version 2 of the License, or
19
* (at your option) any later version.
21
* This program is distributed in the hope that it will be useful,
22
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
* GNU General Public License for more details.
26
* You should have received a copy of the GNU General Public License
27
* along with this program; if not, write to the Free Software
28
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32
#include <sys/types.h>
34
#include <sys/ioctl.h>
39
#include <sys/resource.h>
52
#include <sys/syslog.h>
56
# include <selinux/selinux.h>
57
# include <sys/mount.h>
58
# ifndef MNT_DETACH /* present in glibc 2.10, missing in 2.7 */
66
# define STACK_DEBUG 1
67
# if (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
68
/* Only glibc 2.0 needs this */
69
# include <sigcontext.h>
70
# elif ( __GLIBC__ > 2) && ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1))
71
# include <bits/sigcontext.h>
83
# define SIGPWR SIGUSR2
93
/* Set a signal handler. */
94
#define SETSIG(sa, sig, fun, flags) \
96
sa.sa_handler = fun; \
97
sa.sa_flags = flags; \
98
sigemptyset(&sa.sa_mask); \
99
sigaction(sig, &sa, NULL); \
102
/* Version information */
103
char *Version = "@(#) init " VERSION " " DATE " miquels@cistron.nl";
104
char *bootmsg = "version " VERSION " %s";
105
#define E_VERSION "INIT_VERSION=sysvinit-" VERSION
107
CHILD *family = NULL; /* The linked list of all entries */
108
CHILD *newFamily = NULL; /* The list after inittab re-read */
110
CHILD ch_emerg = { /* Emergency shell */
120
char runlevel = 'S'; /* The current run level */
121
char thislevel = 'S'; /* The current runlevel */
122
char prevlevel = 'N'; /* Previous runlevel */
123
int dfl_level = 0; /* Default runlevel */
124
sig_atomic_t got_cont = 0; /* Set if we received the SIGCONT signal */
125
sig_atomic_t got_signals; /* Set if we received a signal. */
126
int emerg_shell = 0; /* Start emergency shell? */
127
int wrote_wtmp_reboot = 1; /* Set when we wrote the reboot record */
128
int wrote_utmp_reboot = 1; /* Set when we wrote the reboot record */
129
int wrote_wtmp_rlevel = 1; /* Set when we wrote the runlevel record */
130
int wrote_utmp_rlevel = 1; /* Set when we wrote the runlevel record */
131
int sltime = 5; /* Sleep time between TERM and KILL */
132
char *argv0; /* First arguments; show up in ps listing */
133
int maxproclen; /* Maximal length of argv[0] with \0 */
134
struct utmp utproto; /* Only used for sizeof(utproto.ut_id) */
135
char *console_dev; /* Console device. */
136
int pipe_fd = -1; /* /dev/initctl */
137
int did_boot = 0; /* Did we already do BOOT* stuff? */
138
int main(int, char **);
140
/* Used by re-exec part */
141
int reload = 0; /* Should we do initialization stuff? */
142
char *myname="/sbin/init"; /* What should we exec */
143
int oops_error; /* Used by some of the re-exec code. */
144
const char *Signature = "12567362"; /* Signature for re-exec fd */
146
/* Macro to see if this is a special action */
147
#define ISPOWER(i) ((i) == POWERWAIT || (i) == POWERFAIL || \
148
(i) == POWEROKWAIT || (i) == POWERFAILNOW || \
151
/* ascii values for the `action' field. */
156
{ "respawn", RESPAWN },
160
{ "bootwait", BOOTWAIT },
161
{ "powerfail", POWERFAIL },
162
{ "powerfailnow",POWERFAILNOW },
163
{ "powerwait", POWERWAIT },
164
{ "powerokwait", POWEROKWAIT },
165
{ "ctrlaltdel", CTRLALTDEL },
167
{ "ondemand", ONDEMAND },
168
{ "initdefault", INITDEFAULT },
169
{ "sysinit", SYSINIT },
170
{ "kbrequest", KBREQUEST },
175
* State parser token table (see receive_state)
188
{ "CMD", C_PROCESS },
191
{ "-RL", D_RUNLEVEL },
192
{ "-TL", D_THISLEVEL },
193
{ "-PL", D_PREVLEVEL },
194
{ "-SI", D_GOTSIGN },
195
{ "-WR", D_WROTE_WTMP_REBOOT},
196
{ "-WU", D_WROTE_UTMP_REBOOT},
198
{ "-DB", D_DIDBOOT },
199
{ "-LW", D_WROTE_WTMP_RLEVEL},
200
{ "-LU", D_WROTE_UTMP_RLEVEL},
214
#define NR_EXTRA_ENV 16
215
char *extra_env[NR_EXTRA_ENV];
219
* Sleep a number of seconds.
221
* This only works correctly because the linux select updates
222
* the elapsed time in the struct timeval passed to select!
225
void do_sleep(int sec)
232
while(select(0, NULL, NULL, NULL, &tv) < 0 && errno == EINTR)
238
* Non-failing allocation routines (init cannot fail).
241
void *imalloc(size_t size)
245
while ((m = malloc(size)) == NULL) {
246
initlog(L_VB, "out of memory");
254
char *istrdup(char *s)
267
* Send the state info of the previous running init to
268
* the new one, in a version-independant way.
271
void send_state(int fd)
279
fprintf(fp, "VER%s\n", Version);
280
fprintf(fp, "-RL%c\n", runlevel);
281
fprintf(fp, "-TL%c\n", thislevel);
282
fprintf(fp, "-PL%c\n", prevlevel);
283
fprintf(fp, "-SI%u\n", got_signals);
284
fprintf(fp, "-WR%d\n", wrote_wtmp_reboot);
285
fprintf(fp, "-WU%d\n", wrote_utmp_reboot);
286
fprintf(fp, "-ST%d\n", sltime);
287
fprintf(fp, "-DB%d\n", did_boot);
289
for (p = family; p; p = p->next) {
290
fprintf(fp, "REC%s\n", p->id);
291
fprintf(fp, "LEV%s\n", p->rlevel);
292
for (i = 0, val = p->flags; flags[i].mask; i++)
293
if (val & flags[i].mask) {
294
val &= ~flags[i].mask;
295
fprintf(fp, "FL %s\n",flags[i].name);
297
fprintf(fp, "PID%d\n",p->pid);
298
fprintf(fp, "EXS%u\n",p->exstat);
299
for(i = 0; actions[i].act; i++)
300
if (actions[i].act == p->action) {
301
fprintf(fp, "AC %s\n", actions[i].name);
304
fprintf(fp, "CMD%s\n", p->process);
305
fprintf(fp, "EOR\n");
307
fprintf(fp, "END\n");
312
* Read a string from a file descriptor.
313
* FIXME: why not use fgets() ?
315
static int get_string(char *p, int size, FILE *f)
319
while ((c = getc(f)) != EOF && c != '\n') {
324
return (c != EOF) && (size > 0);
328
* Read trailing data from the state pipe until we see a newline.
330
static int get_void(FILE *f)
334
while ((c = getc(f)) != EOF && c != '\n')
341
* Read the next "command" from the state pipe.
343
static int get_cmd(FILE *f)
348
if (fread(cmd, 1, sizeof(cmd) - 1, f) != sizeof(cmd) - 1)
351
for(i = 0; cmds[i].cmd && strcmp(cmds[i].name, cmd) != 0; i++)
357
* Read a CHILD * from the state pipe.
359
static CHILD *get_record(FILE *f)
367
switch (cmd = get_cmd(f)) {
377
fscanf(f, "%c\n", &runlevel);
380
fscanf(f, "%c\n", &thislevel);
383
fscanf(f, "%c\n", &prevlevel);
386
fscanf(f, "%u\n", &got_signals);
388
case D_WROTE_WTMP_REBOOT:
389
fscanf(f, "%d\n", &wrote_wtmp_reboot);
391
case D_WROTE_UTMP_REBOOT:
392
fscanf(f, "%d\n", &wrote_utmp_reboot);
395
fscanf(f, "%d\n", &sltime);
398
fscanf(f, "%d\n", &did_boot);
400
case D_WROTE_WTMP_RLEVEL:
401
fscanf(f, "%d\n", &wrote_wtmp_rlevel);
403
case D_WROTE_UTMP_RLEVEL:
404
fscanf(f, "%d\n", &wrote_utmp_rlevel);
407
if (cmd > 0 || cmd == C_EOF) {
412
} while (cmd != C_REC);
414
p = imalloc(sizeof(CHILD));
415
get_string(p->id, sizeof(p->id), f);
417
do switch(cmd = get_cmd(f)) {
423
fscanf(f, "%d\n", &(p->pid));
426
fscanf(f, "%u\n", &(p->exstat));
429
get_string(p->rlevel, sizeof(p->rlevel), f);
432
get_string(p->process, sizeof(p->process), f);
435
get_string(s, sizeof(s), f);
436
for(i = 0; flags[i].name; i++) {
437
if (strcmp(flags[i].name,s) == 0)
440
p->flags |= flags[i].mask;
443
get_string(s, sizeof(s), f);
444
for(i = 0; actions[i].name; i++) {
445
if (strcmp(actions[i].name, s) == 0)
448
p->action = actions[i].act ? actions[i].act : OFF;
454
} while( cmd != C_EOR);
460
* Read the complete state info from the state pipe.
461
* Returns 0 on success
464
int receive_state(int fd)
467
char old_version[256];
472
if (get_cmd(f) != C_VER)
474
get_string(old_version, sizeof(old_version), f);
476
for (pp = &family; (*pp = get_record(f)) != NULL; pp = &((*pp)->next))
483
* Set the process title.
486
__attribute__ ((format (printf, 1, 2)))
488
static int setproctitle(char *fmt, ...)
497
len = vsnprintf(buf, sizeof(buf), fmt, ap);
500
if (maxproclen > 1) {
501
memset(argv0, 0, maxproclen);
502
strncpy(argv0, buf, maxproclen - 1);
509
* Set console_dev to a working console.
512
void console_init(void)
515
int tried_devcons = 0;
516
int tried_vtmaster = 0;
519
if ((s = getenv("CONSOLE")) != NULL)
522
console_dev = CONSOLE;
526
while ((fd = open(console_dev, O_RDONLY|O_NONBLOCK)) < 0) {
527
if (!tried_devcons) {
529
console_dev = CONSOLE;
532
if (!tried_vtmaster) {
534
console_dev = VT_MASTER;
540
console_dev = "/dev/null";
547
* Open the console with retries.
550
int console_open(int mode)
556
* Open device in nonblocking mode.
558
m = mode | O_NONBLOCK;
561
* Retry the open five times.
563
for(f = 0; f < 5; f++) {
564
if ((fd = open(console_dev, m)) >= 0) break;
568
if (fd < 0) return fd;
571
* Set original flags.
574
fcntl(fd, F_SETFL, mode);
579
* We got a signal (HUP PWR WINCH ALRM INT)
582
void signal_handler(int sig)
584
ADDSET(got_signals, sig);
588
* SIGCHLD: one of our children has died.
592
void chld_handler(int sig __attribute__((unused)))
594
void chld_handler(int sig)
599
int saved_errno = errno;
602
* Find out which process(es) this was (were)
604
while((pid = waitpid(-1, &st, WNOHANG)) != 0) {
605
if (errno == ECHILD) break;
606
for( ch = family; ch; ch = ch->next )
607
if ( ch->pid == pid && (ch->flags & RUNNING) ) {
609
"chld_handler: marked %d as zombie",
611
ADDSET(got_signals, SIGCHLD);
615
ch->new->exstat = st;
616
ch->new->flags |= ZOMBIE;
621
INITDBG(L_VB, "chld_handler: unknown child %d exited.",
629
* Linux ignores all signals sent to init when the
630
* SIG_DFL handler is installed. Therefore we must catch SIGTSTP
631
* and SIGCONT, or else they won't work....
633
* The SIGCONT handler
637
void cont_handler(int sig __attribute__((unused)))
639
void cont_handler(int sig)
646
* Fork and dump core in /.
651
static int dumped = 0;
658
if (fork() != 0) return;
661
sigprocmask(SIG_SETMASK, &mask, NULL);
663
rlim.rlim_cur = RLIM_INFINITY;
664
rlim.rlim_max = RLIM_INFINITY;
665
setrlimit(RLIMIT_CORE, &rlim);
668
signal(SIGSEGV, SIG_DFL);
670
sigdelset(&mask, SIGSEGV);
671
sigprocmask(SIG_SETMASK, &mask, NULL);
678
* OOPS: segmentation violation!
679
* If we have the info, print where it occured.
680
* Then sleep 30 seconds and try to continue.
683
#if defined(STACK_DEBUG) && defined(__linux__)
685
void segv_handler(int sig __attribute__((unused)), struct sigcontext ctx)
687
void segv_handler(int sig, struct sigcontext ctx)
691
int saved_errno = errno;
693
if ((void *)ctx.eip >= (void *)do_sleep &&
694
(void *)ctx.eip < (void *)main)
696
initlog(L_VB, "PANIC: segmentation violation at %p%s! "
697
"sleeping for 30 seconds.", (void *)ctx.eip, p);
704
void segv_handler(int sig __attribute__((unused)))
706
void segv_handler(int sig)
709
int saved_errno = errno;
712
"PANIC: segmentation violation! sleeping for 30 seconds.");
720
* The SIGSTOP & SIGTSTP handler
724
void stop_handler(int sig __attribute__((unused)))
726
void stop_handler(int sig)
729
int saved_errno = errno;
732
while(!got_cont) pause();
738
* Set terminal settings to reasonable defaults
741
void console_stty(void)
746
if ((fd = console_open(O_RDWR|O_NOCTTY)) < 0) {
747
initlog(L_VB, "can't open %s", console_dev);
751
#ifdef __FreeBSD_kernel__
753
* The kernel of FreeBSD expects userland to set TERM. Usually, we want
754
* "cons25". Later, gettys might disagree on this (i.e. we're not using
755
* syscons) but some boot scripts, like /etc/init.d/xserver-xorg, still
756
* need a non-dumb terminal.
758
putenv ("TERM=cons25");
761
(void) tcgetattr(fd, &tty);
763
tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
764
tty.c_cflag |= HUPCL|CLOCAL|CREAD;
766
tty.c_cc[VINTR] = CINTR;
767
tty.c_cc[VQUIT] = CQUIT;
768
tty.c_cc[VERASE] = CERASE; /* ASCII DEL (0177) */
769
tty.c_cc[VKILL] = CKILL;
770
tty.c_cc[VEOF] = CEOF;
773
#ifdef VSWTC /* not defined on FreeBSD */
774
tty.c_cc[VSWTC] = _POSIX_VDISABLE;
776
tty.c_cc[VSTART] = CSTART;
777
tty.c_cc[VSTOP] = CSTOP;
778
tty.c_cc[VSUSP] = CSUSP;
779
tty.c_cc[VEOL] = _POSIX_VDISABLE;
780
tty.c_cc[VREPRINT] = CREPRINT;
781
tty.c_cc[VDISCARD] = CDISCARD;
782
tty.c_cc[VWERASE] = CWERASE;
783
tty.c_cc[VLNEXT] = CLNEXT;
784
tty.c_cc[VEOL2] = _POSIX_VDISABLE;
787
* Set pre and post processing
789
tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY;
790
#ifdef IUTF8 /* Not defined on FreeBSD */
791
tty.c_iflag |= IUTF8;
793
tty.c_oflag = OPOST|ONLCR;
794
tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE;
796
#if defined(SANE_TIO) && (SANE_TIO == 1)
798
* Disable flow control (-ixon), ignore break (ignbrk),
799
* and make nl/cr more usable (sane).
801
tty.c_iflag |= IGNBRK;
802
tty.c_iflag &= ~(BRKINT|INLCR|IGNCR|IXON);
803
tty.c_oflag &= ~(OCRNL|ONLRET);
806
* Now set the terminal line.
807
* We don't care about non-transmitted output data
808
* and non-read input data.
810
(void) tcsetattr(fd, TCSANOW, &tty);
811
(void) tcflush(fd, TCIOFLUSH);
816
* Print to the system console
822
if ((fd = console_open(O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
823
write(fd, s, strlen(s));
829
* Log something to a logfile and the console.
832
__attribute__ ((format (printf, 2, 3)))
834
void initlog(int loglevel, char *s, ...)
838
sigset_t nmask, omask;
840
va_start(va_alist, s);
841
vsnprintf(buf, sizeof(buf), s, va_alist);
844
if (loglevel & L_SY) {
846
* Re-establish connection with syslogd every time.
847
* Block signals while talking to syslog.
850
sigprocmask(SIG_BLOCK, &nmask, &omask);
851
openlog("init", 0, LOG_DAEMON);
852
syslog(LOG_INFO, "%s", buf);
854
sigprocmask(SIG_SETMASK, &omask, NULL);
858
* And log to the console.
860
if (loglevel & L_CO) {
869
* Build a new environment for execve().
871
char **init_buildenv(int child)
873
char i_lvl[] = "RUNLEVEL=x";
874
char i_prev[] = "PREVLEVEL=x";
876
char i_shell[] = "SHELL=" SHELL;
880
for (n = 0; environ[n]; n++)
885
e = calloc(n, sizeof(char *));
887
for (n = 0; environ[n]; n++)
888
e[n] = istrdup(environ[n]);
890
for (i = 0; i < NR_EXTRA_ENV; i++) {
892
e[n++] = istrdup(extra_env[i]);
896
snprintf(i_cons, sizeof(i_cons), "CONSOLE=%s", console_dev);
897
i_lvl[9] = thislevel;
898
i_prev[10] = prevlevel;
899
e[n++] = istrdup(i_shell);
900
e[n++] = istrdup(i_lvl);
901
e[n++] = istrdup(i_prev);
902
e[n++] = istrdup(i_cons);
903
e[n++] = istrdup(E_VERSION);
912
void init_freeenv(char **e)
916
for (n = 0; e[n]; n++)
925
* This function is too long and indents too deep.
929
pid_t spawn(CHILD *ch, int *res)
931
char *args[16]; /* Argv array */
932
char buf[136]; /* Line buffer */
933
int f, st; /* Scratch variables */
934
char *ptr; /* Ditto */
935
time_t t; /* System time */
936
int oldAlarm; /* Previous alarm value */
937
char *proc = ch->process; /* Command line */
938
pid_t pid, pgrp; /* child, console process group. */
939
sigset_t nmask, omask; /* For blocking SIGCHLD */
943
buf[sizeof(buf) - 1] = 0;
945
/* Skip '+' if it's there */
946
if (proc[0] == '+') proc++;
948
ch->flags |= XECUTED;
950
if (ch->action == RESPAWN || ch->action == ONDEMAND) {
951
/* Is the date stamp from less than 2 minutes ago? */
953
if (ch->tm + TESTTIME > t) {
960
/* Do we try to respawn too fast? */
961
if (ch->count >= MAXSPAWN) {
964
"Id \"%s\" respawning too fast: disabled for %d minutes",
965
ch->id, SLEEPTIME / 60);
966
ch->flags &= ~RUNNING;
967
ch->flags |= FAILING;
969
/* Remember the time we stopped */
972
/* Try again in 5 minutes */
974
if (oldAlarm > SLEEPTIME || oldAlarm <= 0) oldAlarm = SLEEPTIME;
980
/* See if there is an "initscript" (except in single user mode). */
981
if (access(INITSCRIPT, R_OK) == 0 && runlevel != 'S') {
982
/* Build command line using "initscript" */
984
args[2] = INITSCRIPT;
986
args[4] = ch->rlevel;
988
for(f = 0; actions[f].name; f++) {
989
if (ch->action == actions[f].act) {
990
args[5] = actions[f].name;
996
} else if (strpbrk(proc, "~`!$^&*()=|\\{}[];\"'<>?")) {
997
/* See if we need to fire off a shell for this command */
998
/* Give command line to shell */
1001
strcpy(buf, "exec ");
1002
strncat(buf, proc, sizeof(buf) - strlen(buf) - 1);
1006
/* Split up command line arguments */
1008
strncat(buf, proc, sizeof(buf) - 1);
1010
for(f = 1; f < 15; f++) {
1011
/* Skip white space */
1012
while(*ptr == ' ' || *ptr == '\t') ptr++;
1015
/* May be trailing space.. */
1016
if (*ptr == 0) break;
1018
/* Skip this `word' */
1019
while(*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#')
1022
/* If end-of-line, break */
1023
if (*ptr == '#' || *ptr == 0) {
1028
/* End word with \0 and continue */
1036
* Block sigchild while forking.
1038
sigemptyset(&nmask);
1039
sigaddset(&nmask, SIGCHLD);
1040
sigprocmask(SIG_BLOCK, &nmask, &omask);
1042
if ((pid = fork()) == 0) {
1047
if (pipe_fd >= 0) close(pipe_fd);
1049
sigprocmask(SIG_SETMASK, &omask, NULL);
1052
* In sysinit, boot, bootwait or single user mode:
1053
* for any wait-type subprocess we _force_ the console
1054
* to be its controlling tty.
1056
if (strchr("*#sS", runlevel) && ch->flags & WAITING) {
1058
* We fork once extra. This is so that we can
1059
* wait and change the process group and session
1060
* of the console after exit of the leader.
1063
if ((f = console_open(O_RDWR|O_NOCTTY)) >= 0) {
1064
/* Take over controlling tty by force */
1065
(void)ioctl(f, TIOCSCTTY, 1);
1071
* 4 Sep 2001, Andrea Arcangeli:
1072
* Fix a race in spawn() that is used to deadlock init in a
1073
* waitpid() loop: must set the childhandler as default before forking
1074
* off the child or the chld_handler could run before the waitpid loop
1075
* has a chance to find its zombie-child.
1077
SETSIG(sa, SIGCHLD, SIG_DFL, SA_RESTART);
1078
if ((pid = fork()) < 0) {
1079
initlog(L_VB, "cannot fork: %s",
1086
* Ignore keyboard signals etc.
1087
* Then wait for child to exit.
1089
SETSIG(sa, SIGINT, SIG_IGN, SA_RESTART);
1090
SETSIG(sa, SIGTSTP, SIG_IGN, SA_RESTART);
1091
SETSIG(sa, SIGQUIT, SIG_IGN, SA_RESTART);
1093
while ((rc = waitpid(pid, &st, 0)) != pid)
1094
if (rc < 0 && errno == ECHILD)
1098
* Small optimization. See if stealing
1099
* controlling tty back is needed.
1101
pgrp = tcgetpgrp(f);
1102
if (pgrp != getpid())
1106
* Steal controlling tty away. We do
1107
* this with a temporary process.
1109
if ((pid = fork()) < 0) {
1110
initlog(L_VB, "cannot fork: %s",
1116
(void)ioctl(f, TIOCSCTTY, 1);
1119
while((rc = waitpid(pid, &st, 0)) != pid)
1120
if (rc < 0 && errno == ECHILD)
1125
/* Set ioctl settings to default ones */
1130
if ((f = console_open(O_RDWR|O_NOCTTY)) < 0) {
1131
initlog(L_VB, "open(%s): %s", console_dev,
1133
f = open("/dev/null", O_RDWR);
1140
* Update utmp/wtmp file prior to starting
1141
* any child. This MUST be done right here in
1142
* the child process in order to prevent a race
1143
* condition that occurs when the child
1144
* process' time slice executes before the
1145
* parent (can and does happen in a uniprocessor
1146
* environment). If the child is a getty and
1147
* the race condition happens, then init's utmp
1148
* update will happen AFTER the getty runs
1149
* and expects utmp to be updated already!
1151
* Do NOT log if process field starts with '+'
1152
* FIXME: that's for compatibility with *very*
1153
* old getties - probably it can be taken out.
1155
if (ch->process[0] != '+')
1156
write_utmp_wtmp("", ch->id, getpid(), INIT_PROCESS, "");
1158
/* Reset all the signals, set up environment */
1159
for(f = 1; f < NSIG; f++) SETSIG(sa, f, SIG_DFL, SA_RESTART);
1160
environ = init_buildenv(1);
1163
* Execute prog. In case of ENOEXEC try again
1164
* as a shell script.
1166
execvp(args[1], args + 1);
1167
if (errno == ENOEXEC) {
1170
strcpy(buf, "exec ");
1171
strncat(buf, proc, sizeof(buf) - strlen(buf) - 1);
1174
execvp(args[1], args + 1);
1176
initlog(L_VB, "cannot execute \"%s\"", args[1]);
1178
if (ch->process[0] != '+')
1179
write_utmp_wtmp("", ch->id, getpid(), DEAD_PROCESS, NULL);
1183
sigprocmask(SIG_SETMASK, &omask, NULL);
1185
INITDBG(L_VB, "Started id %s (pid %d)", ch->id, pid);
1188
initlog(L_VB, "cannot fork, retry..");
1197
* Start a child running!
1200
void startup(CHILD *ch)
1203
* See if it's disabled
1205
if (ch->flags & FAILING) return;
1207
switch(ch->action) {
1216
if (!(ch->flags & XECUTED)) ch->flags |= WAITING;
1221
if (ch->flags & XECUTED) break;
1224
ch->flags |= RUNNING;
1225
(void)spawn(ch, &(ch->pid));
1232
* Read the inittab file.
1235
void read_inittab(void)
1237
FILE *fp; /* The INITTAB file */
1238
CHILD *ch, *old, *i; /* Pointers to CHILD structure */
1239
CHILD *head = NULL; /* Head of linked list */
1241
struct stat st; /* To stat INITLVL */
1243
sigset_t nmask, omask; /* For blocking SIGCHLD. */
1244
char buf[256]; /* Line buffer */
1245
char err[64]; /* Error message. */
1247
*action, *process; /* Fields of a line */
1249
int lineNo = 0; /* Line number in INITTAB file */
1250
int actionNo; /* Decoded action field */
1251
int f; /* Counter */
1252
int round; /* round 0 for SIGTERM, 1 for SIGKILL */
1253
int foundOne = 0; /* No killing no sleep */
1254
int talk; /* Talk to the user */
1255
int done = 0; /* Ready yet? */
1258
if (newFamily != NULL) {
1259
INITDBG(L_VB, "PANIC newFamily != NULL");
1262
INITDBG(L_VB, "Reading inittab");
1266
* Open INITTAB and real line by line.
1268
if ((fp = fopen(INITTAB, "r")) == NULL)
1269
initlog(L_VB, "No inittab file found");
1273
* Add single user shell entry at the end.
1275
if (fp == NULL || fgets(buf, sizeof(buf), fp) == NULL) {
1278
* See if we have a single user entry.
1280
for(old = newFamily; old; old = old->next)
1281
if (strpbrk(old->rlevel, "S")) break;
1283
snprintf(buf, sizeof(buf), "~~:S:wait:%s\n", SULOGIN);
1289
* Skip comments and empty lines
1291
for(p = buf; *p == ' ' || *p == '\t'; p++)
1293
if (*p == '#' || *p == '\n') continue;
1298
id = strsep(&p, ":");
1299
rlevel = strsep(&p, ":");
1300
action = strsep(&p, ":");
1301
process = strsep(&p, "\n");
1304
* Check if syntax is OK. Be very verbose here, to
1305
* avoid newbie postings on comp.os.linux.setup :)
1308
if (!id || !*id) strcpy(err, "missing id field");
1309
if (!rlevel) strcpy(err, "missing runlevel field");
1310
if (!process) strcpy(err, "missing process field");
1311
if (!action || !*action)
1312
strcpy(err, "missing action field");
1313
if (id && strlen(id) > sizeof(utproto.ut_id))
1314
sprintf(err, "id field too long (max %d characters)",
1315
(int)sizeof(utproto.ut_id));
1316
if (rlevel && strlen(rlevel) > 11)
1317
strcpy(err, "rlevel field too long (max 11 characters)");
1318
if (process && strlen(process) > 127)
1319
strcpy(err, "process field too long");
1320
if (action && strlen(action) > 32)
1321
strcpy(err, "action field too long");
1323
initlog(L_VB, "%s[%d]: %s", INITTAB, lineNo, err);
1324
INITDBG(L_VB, "%s:%s:%s:%s", id, rlevel, action, process);
1329
* Decode the "action" field
1332
for(f = 0; actions[f].name; f++)
1333
if (strcasecmp(action, actions[f].name) == 0) {
1334
actionNo = actions[f].act;
1337
if (actionNo == -1) {
1338
initlog(L_VB, "%s[%d]: %s: unknown action field",
1339
INITTAB, lineNo, action);
1344
* See if the id field is unique
1346
for(old = newFamily; old; old = old->next) {
1347
if(strcmp(old->id, id) == 0 && strcmp(id, "~~")) {
1348
initlog(L_VB, "%s[%d]: duplicate ID field \"%s\"",
1349
INITTAB, lineNo, id);
1356
* Allocate a CHILD structure
1358
ch = imalloc(sizeof(CHILD));
1363
ch->action = actionNo;
1364
strncpy(ch->id, id, sizeof(utproto.ut_id) + 1); /* Hack for different libs. */
1365
strncpy(ch->process, process, sizeof(ch->process) - 1);
1367
for(f = 0; f < (int)sizeof(rlevel) - 1 && rlevel[f]; f++) {
1368
ch->rlevel[f] = rlevel[f];
1369
if (ch->rlevel[f] == 's') ch->rlevel[f] = 'S';
1371
strncpy(ch->rlevel, rlevel, sizeof(ch->rlevel) - 1);
1373
strcpy(ch->rlevel, "0123456789");
1374
if (ISPOWER(ch->action))
1375
strcpy(ch->rlevel, "S0123456789");
1378
* We have the fake runlevel '#' for SYSINIT and
1379
* '*' for BOOT and BOOTWAIT.
1381
if (ch->action == SYSINIT) strcpy(ch->rlevel, "#");
1382
if (ch->action == BOOT || ch->action == BOOTWAIT)
1383
strcpy(ch->rlevel, "*");
1386
* Now add it to the linked list. Special for powerfail.
1388
if (ISPOWER(ch->action)) {
1391
* Disable by default
1393
ch->flags |= XECUTED;
1396
* Tricky: insert at the front of the list..
1399
for(i = newFamily; i; i = i->next) {
1400
if (!ISPOWER(i->action)) break;
1404
* Now add after entry "old"
1409
if (i == NULL) head = ch;
1411
ch->next = newFamily;
1413
if (ch->next == NULL) head = ch;
1417
* Just add at end of the list
1419
if (ch->action == KBREQUEST) ch->flags |= XECUTED;
1429
* Walk through the old list comparing id fields
1431
for(old = family; old; old = old->next)
1432
if (strcmp(old->id, ch->id) == 0) {
1443
* Loop through the list of children, and see if they need to
1447
INITDBG(L_VB, "Checking for children to kill");
1448
for(round = 0; round < 2; round++) {
1450
for(ch = family; ch; ch = ch->next) {
1451
ch->flags &= ~KILLME;
1454
* Is this line deleted?
1456
if (ch->new == NULL) ch->flags |= KILLME;
1459
* If the entry has changed, kill it anyway. Note that
1460
* we do not check ch->process, only the "action" field.
1461
* This way, you can turn an entry "off" immediately, but
1462
* changes in the command line will only become effective
1463
* after the running version has exited.
1465
if (ch->new && ch->action != ch->new->action) ch->flags |= KILLME;
1468
* Only BOOT processes may live in all levels
1470
if (ch->action != BOOT &&
1471
strchr(ch->rlevel, runlevel) == NULL) {
1473
* Ondemand procedures live always,
1474
* except in single user
1476
if (runlevel == 'S' || !(ch->flags & DEMAND))
1477
ch->flags |= KILLME;
1481
* Now, if this process may live note so in the new list
1483
if ((ch->flags & KILLME) == 0) {
1484
ch->new->flags = ch->flags;
1485
ch->new->pid = ch->pid;
1486
ch->new->exstat = ch->exstat;
1492
* Is this process still around?
1494
if ((ch->flags & RUNNING) == 0) {
1495
ch->flags &= ~KILLME;
1498
INITDBG(L_VB, "Killing \"%s\"", ch->process);
1500
case 0: /* Send TERM signal */
1503
"Sending processes the TERM signal");
1504
kill(-(ch->pid), SIGTERM);
1507
case 1: /* Send KILL signal and collect status */
1510
"Sending processes the KILL signal");
1511
kill(-(ch->pid), SIGKILL);
1518
* See if we have to wait 5 seconds
1520
if (foundOne && round == 0) {
1522
* Yup, but check every second if we still have children.
1524
for(f = 0; f < sltime; f++) {
1525
for(ch = family; ch; ch = ch->next) {
1526
if (!(ch->flags & KILLME)) continue;
1527
if ((ch->flags & RUNNING) && !(ch->flags & ZOMBIE))
1532
* No running children, skip SIGKILL
1535
foundOne = 0; /* Skip the sleep below. */
1544
* Now give all processes the chance to die and collect exit statuses.
1546
if (foundOne) do_sleep(1);
1547
for(ch = family; ch; ch = ch->next)
1548
if (ch->flags & KILLME) {
1549
if (!(ch->flags & ZOMBIE))
1550
initlog(L_CO, "Pid %d [id %s] seems to hang", ch->pid,
1553
INITDBG(L_VB, "Updating utmp for pid %d [id %s]",
1555
ch->flags &= ~RUNNING;
1556
if (ch->process[0] != '+')
1557
write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
1562
* Both rounds done; clean up the list.
1564
sigemptyset(&nmask);
1565
sigaddset(&nmask, SIGCHLD);
1566
sigprocmask(SIG_BLOCK, &nmask, &omask);
1567
for(ch = family; ch; ch = old) {
1572
for(ch = family; ch; ch = ch->next) ch->new = NULL;
1574
sigprocmask(SIG_SETMASK, &omask, NULL);
1578
* Dispose of INITLVL file.
1580
if (lstat(INITLVL, &st) >= 0 && S_ISLNK(st.st_mode)) {
1582
* INITLVL is a symbolic link, so just truncate the file.
1584
close(open(INITLVL, O_WRONLY|O_TRUNC));
1587
* Delete INITLVL file.
1594
* Dispose of INITLVL2 file.
1596
if (lstat(INITLVL2, &st) >= 0 && S_ISLNK(st.st_mode)) {
1598
* INITLVL2 is a symbolic link, so just truncate the file.
1600
close(open(INITLVL2, O_WRONLY|O_TRUNC));
1603
* Delete INITLVL2 file.
1611
* Walk through the family list and start up children.
1612
* The entries that do not belong here at all are removed
1616
void start_if_needed(void)
1618
CHILD *ch; /* Pointer to child */
1619
int delete; /* Delete this entry from list? */
1621
INITDBG(L_VB, "Checking for children to start");
1623
for(ch = family; ch; ch = ch->next) {
1626
if (ch->rlevel[0] == 'C') {
1627
INITDBG(L_VB, "%s: flags %d", ch->process, ch->flags);
1631
/* Are we waiting for this process? Then quit here. */
1632
if (ch->flags & WAITING) break;
1634
/* Already running? OK, don't touch it */
1635
if (ch->flags & RUNNING) continue;
1637
/* See if we have to start it up */
1639
if (strchr(ch->rlevel, runlevel) ||
1640
((ch->flags & DEMAND) && !strchr("#*Ss", runlevel))) {
1646
/* FIXME: is this OK? */
1647
ch->flags &= ~(RUNNING|WAITING);
1648
if (!ISPOWER(ch->action) && ch->action != KBREQUEST)
1649
ch->flags &= ~XECUTED;
1652
/* Do we have to wait for this process? */
1653
if (ch->flags & WAITING) break;
1659
* Ask the user on the console for a runlevel
1662
int ask_runlevel(void)
1664
const char prompt[] = "\nEnter runlevel: ";
1670
fd = console_open(O_RDWR|O_NOCTTY);
1672
if (fd < 0) return('S');
1674
while(!strchr("0123456789S", lvl)) {
1675
write(fd, prompt, sizeof(prompt) - 1);
1677
read(fd, buf, sizeof(buf));
1678
if (buf[0] != 0 && (buf[1] == '\r' || buf[1] == '\n'))
1680
if (islower(lvl)) lvl = toupper(lvl);
1687
* Search the INITTAB file for the 'initdefault' field, with the default
1688
* runlevel. If this fails, ask the user to supply a runlevel.
1691
int get_init_default(void)
1698
* Look for initdefault.
1700
for(ch = family; ch; ch = ch->next)
1701
if (ch->action == INITDEFAULT) {
1704
if (*p > lvl) lvl = *p;
1710
* See if level is valid
1713
if (islower(lvl)) lvl = toupper(lvl);
1714
if (strchr("0123456789S", lvl) == NULL) {
1716
"Initdefault level '%c' is invalid", lvl);
1721
* Ask for runlevel on console if needed.
1723
if (lvl <= 0) lvl = ask_runlevel();
1726
* Log the fact that we have a runlevel now.
1735
* Do actions for the new level. If we are compatible with
1736
* the "old" INITLVL and arg == 0, try to read the new
1737
* runlevel from that file first.
1740
int read_level(int arg)
1742
CHILD *ch; /* Walk through list */
1743
unsigned char foo = 'X'; /* Contents of INITLVL */
1758
if (stat(INITLVL, &stt) != 0 || stt.st_size != 0L)
1759
fp = fopen(INITLVL, "r");
1762
(stat(INITLVL2, &stt) != 0 || stt.st_size != 0L))
1763
fp = fopen(INITLVL2, "r");
1766
/* INITLVL file empty or not there - act as 'init q' */
1767
initlog(L_SY, "Re-reading inittab");
1770
ok = fscanf(fp, "%c %d", &foo, &st);
1773
/* We go to the new runlevel passed as an argument. */
1777
if (ok == 2) sltime = st;
1779
#endif /* INITLVL */
1781
if (islower(foo)) foo = toupper(foo);
1782
if (ok < 1 || ok > 2 || strchr("QS0123456789ABCU", foo) == NULL) {
1783
initlog(L_VB, "bad runlevel: %c", foo);
1787
/* Log this action */
1790
initlog(L_VB, "Going single user");
1793
initlog(L_SY, "Re-reading inittab");
1799
"Activating demand-procedures for '%c'", foo);
1802
initlog(L_SY, "Trying to re-exec init");
1805
initlog(L_VB, "Switching to runlevel: %c", foo);
1809
#if defined(SIGINT_ONLYONCE) && (SIGINT_ONLYONCE == 1)
1810
/* Re-enable signal from keyboard */
1811
struct sigaction sa;
1812
SETSIG(sa, SIGINT, signal_handler, 0);
1817
/* Check if this is a runlevel a, b or c */
1818
if (strchr("ABC", foo)) {
1819
if (runlevel == 'S') return(runlevel);
1821
/* Read inittab again first! */
1824
/* Mark those special tasks */
1825
for(ch = family; ch; ch = ch->next)
1826
if (strchr(ch->rlevel, foo) != NULL ||
1827
strchr(ch->rlevel, tolower(foo)) != NULL) {
1828
ch->flags |= DEMAND;
1829
ch->flags &= ~XECUTED;
1831
"Marking (%s) as ondemand, flags %d",
1837
/* Store both the old and the new runlevel. */
1838
wrote_utmp_rlevel = 0;
1839
wrote_wtmp_rlevel = 0;
1840
write_utmp_wtmp("runlevel", "~~", foo + 256*runlevel, RUN_LVL, "~");
1842
prevlevel = runlevel;
1848
* This procedure is called after every signal (SIGHUP, SIGALRM..)
1850
* Only clear the 'failing' flag if the process is sleeping
1851
* longer than 5 minutes, or inittab was read again due
1852
* to user interaction.
1855
void fail_check(void)
1857
CHILD *ch; /* Pointer to child structure */
1858
time_t t; /* System time */
1859
time_t next_alarm = 0; /* When to set next alarm */
1863
for(ch = family; ch; ch = ch->next) {
1865
if (ch->flags & FAILING) {
1866
/* Can we free this sucker? */
1867
if (ch->tm + SLEEPTIME < t) {
1868
ch->flags &= ~FAILING;
1872
/* No, we'll look again later */
1873
if (next_alarm == 0 ||
1874
ch->tm + SLEEPTIME > next_alarm)
1875
next_alarm = ch->tm + SLEEPTIME;
1881
if (next_alarm < 1) next_alarm = 1;
1886
/* Set all 'Fail' timers to 0 */
1888
void fail_cancel(void)
1892
for(ch = family; ch; ch = ch->next) {
1895
ch->flags &= ~FAILING;
1900
* Start up powerfail entries.
1903
void do_power_fail(int pwrstat)
1908
* Tell powerwait & powerfail entries to start up
1910
for (ch = family; ch; ch = ch->next) {
1911
if (pwrstat == 'O') {
1913
* The power is OK again.
1915
if (ch->action == POWEROKWAIT)
1916
ch->flags &= ~XECUTED;
1917
} else if (pwrstat == 'L') {
1919
* Low battery, shut down now.
1921
if (ch->action == POWERFAILNOW)
1922
ch->flags &= ~XECUTED;
1925
* Power is failing, shutdown imminent
1927
if (ch->action == POWERFAIL || ch->action == POWERWAIT)
1928
ch->flags &= ~XECUTED;
1934
* Check for state-pipe presence
1937
int check_pipe(int fd)
1945
t.tv_sec = t.tv_usec = 0;
1947
if (select(fd+1, &s, NULL, NULL, &t) != 1)
1949
if (read(fd, signature, 8) != 8)
1951
return strncmp(Signature, signature, 8) == 0;
1955
* Make a state-pipe.
1958
int make_pipe(int fd)
1965
fcntl(fds[1], F_SETFD, 1);
1966
fcntl(fd, F_SETFD, 0);
1967
write(fds[1], Signature, 8);
1973
* Attempt to re-exec.
1979
sigset_t mask, oldset;
1984
if (strchr("S0123456",runlevel) == NULL)
1988
* Reset the alarm, and block all signals.
1992
sigprocmask(SIG_BLOCK, &mask, &oldset);
1995
* construct a pipe fd --> STATE_PIPE and write a signature
1997
fd = make_pipe(STATE_PIPE);
2000
* It's a backup day today, so I'm pissed off. Being a BOFH, however,
2001
* does have it's advantages...
2006
DELSET(got_signals, SIGCHLD);
2007
DELSET(got_signals, SIGHUP);
2008
DELSET(got_signals, SIGUSR1);
2011
* That should be cleaned.
2013
for(ch = family; ch; ch = ch->next)
2014
if (ch->flags & ZOMBIE) {
2015
INITDBG(L_VB, "Child died, PID= %d", ch->pid);
2016
ch->flags &= ~(RUNNING|ZOMBIE|WAITING);
2017
if (ch->process[0] != '+')
2018
write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
2021
if ((pid = fork()) == 0) {
2023
* Child sends state information to the parent.
2030
* The existing init process execs a new init binary.
2032
env = init_buildenv(0);
2033
execle(myname, myname, "--init", NULL, env);
2036
* We shouldn't be here, something failed.
2037
* Bitch, close the state pipe, unblock signals and return.
2041
sigprocmask(SIG_SETMASK, &oldset, NULL);
2043
initlog(L_CO, "Attempt to re-exec failed");
2047
* Redo utmp/wtmp entries if required or requested
2048
* Check for written records and size of utmp
2051
void redo_utmp_wtmp(void)
2054
const int ret = stat(UTMP_FILE, &ustat);
2056
if ((ret < 0) || (ustat.st_size == 0))
2057
wrote_utmp_rlevel = wrote_utmp_reboot = 0;
2059
if ((wrote_wtmp_reboot == 0) || (wrote_utmp_reboot == 0))
2060
write_utmp_wtmp("reboot", "~~", 0, BOOT_TIME, "~");
2062
if ((wrote_wtmp_rlevel == 0) || (wrote_wtmp_rlevel == 0))
2063
write_utmp_wtmp("runlevel", "~~", thislevel + 256 * prevlevel, RUN_LVL, "~");
2067
* We got a change runlevel request through the
2068
* init.fifo. Process it.
2071
void fifo_new_level(int level)
2078
if (level == runlevel) return;
2081
/* Are we waiting for a child? */
2082
for(ch = family; ch; ch = ch->next)
2083
if (ch->flags & WAITING) break;
2087
/* We need to go into a new runlevel */
2088
oldlevel = runlevel;
2089
runlevel = read_level(level);
2090
if (runlevel == 'U') {
2091
runlevel = oldlevel;
2094
if (oldlevel != 'S' && runlevel == 'S') console_stty();
2095
if (runlevel == '6' || runlevel == '0' ||
2096
runlevel == '1') console_stty();
2097
if (runlevel > '1' && runlevel < '6') redo_utmp_wtmp();
2100
setproctitle("init [%c]", (int)runlevel);
2107
* Set/unset environment variables. The variables are
2108
* encoded as KEY=VAL\0KEY=VAL\0\0. With "=VAL" it means
2109
* setenv, without it means unsetenv.
2112
void initcmd_setenv(char *data, int size)
2114
char *env, *p, *e, *eq;
2119
while (*data && data < e) {
2121
for (p = data; *p && p < e; p++)
2122
if (*p == '=') eq = p;
2127
sz = eq ? (eq - env) : (p - env);
2129
/*initlog(L_SY, "init_setenv: %s, %s, %d", env, eq, sz);*/
2132
* We only allow INIT_* to be set.
2134
if (strncmp(env, "INIT_", 5) != 0)
2137
/* Free existing vars. */
2138
for (i = 0; i < NR_EXTRA_ENV; i++) {
2139
if (extra_env[i] == NULL) continue;
2140
if (!strncmp(extra_env[i], env, sz) &&
2141
extra_env[i][sz] == '=') {
2143
extra_env[i] = NULL;
2147
/* Set new vars if needed. */
2148
if (eq == NULL) continue;
2149
for (i = 0; i < NR_EXTRA_ENV; i++) {
2150
if (extra_env[i] == NULL) {
2151
extra_env[i] = istrdup(env);
2160
* Read from the init FIFO. Processes like telnetd and rlogind can
2161
* ask us to create login processes on their behalf.
2163
* FIXME: this needs to be finished. NOT that it is buggy, but we need
2164
* to add the telnetd/rlogind stuff so people can start using it.
2165
* Maybe move to using an AF_UNIX socket so we can use
2166
* the 2.2 kernel credential stuff to see who we're talking to.
2170
void check_init_fifo(void)
2172
struct init_request request;
2174
struct stat st, st2;
2180
* First, try to create /dev/initctl if not present.
2182
if (stat(INIT_FIFO, &st2) < 0 && errno == ENOENT)
2183
(void)mkfifo(INIT_FIFO, 0600);
2186
* If /dev/initctl is open, stat the file to see if it
2187
* is still the _same_ inode.
2190
fstat(pipe_fd, &st);
2191
if (stat(INIT_FIFO, &st2) < 0 ||
2192
st.st_dev != st2.st_dev ||
2193
st.st_ino != st2.st_ino) {
2200
* Now finally try to open /dev/initctl
2203
if ((pipe_fd = open(INIT_FIFO, O_RDWR|O_NONBLOCK)) >= 0) {
2204
fstat(pipe_fd, &st);
2205
if (!S_ISFIFO(st.st_mode)) {
2206
initlog(L_VB, "%s is not a fifo", INIT_FIFO);
2213
* Don't use fd's 0, 1 or 2.
2215
(void) dup2(pipe_fd, PIPE_FD);
2220
* Return to caller - we'll be back later.
2225
/* Wait for data to appear, _if_ the pipe was opened. */
2226
if (pipe_fd >= 0) while(!quit) {
2228
/* Do select, return on EINTR. */
2230
FD_SET(pipe_fd, &fds);
2233
n = select(pipe_fd + 1, &fds, NULL, NULL, &tv);
2235
if (n == 0 || errno == EINTR) return;
2239
/* Read the data, return on EINTR. */
2240
n = read(pipe_fd, &request, sizeof(request));
2243
* End of file. This can't happen under Linux (because
2244
* the pipe is opened O_RDWR - see select() in the
2245
* kernel) but you never know...
2252
if (errno == EINTR) return;
2253
initlog(L_VB, "error reading initrequest");
2258
* This is a convenient point to also try to
2259
* find the console device or check if it changed.
2266
if (request.magic != INIT_MAGIC || n != sizeof(request)) {
2267
initlog(L_VB, "got bogus initrequest");
2270
switch(request.cmd) {
2271
case INIT_CMD_RUNLVL:
2272
sltime = request.sleeptime;
2273
fifo_new_level(request.runlevel);
2276
case INIT_CMD_POWERFAIL:
2277
sltime = request.sleeptime;
2281
case INIT_CMD_POWERFAILNOW:
2282
sltime = request.sleeptime;
2286
case INIT_CMD_POWEROK:
2287
sltime = request.sleeptime;
2291
case INIT_CMD_SETENV:
2292
initcmd_setenv(request.i.data, sizeof(request.i.data));
2295
initlog(L_VB, "got unimplemented initrequest.");
2301
* We come here if the pipe couldn't be opened.
2303
if (pipe_fd < 0) pause();
2309
* This function is used in the transition
2310
* sysinit (-> single user) boot -> multi-user.
2313
void boot_transitions()
2316
static int newlevel = 0;
2317
static int warn = 1;
2321
/* Check if there is something to wait for! */
2322
for( ch = family; ch; ch = ch->next )
2323
if ((ch->flags & RUNNING) && ch->action != BOOT) break;
2326
/* No processes left in this level, proceed to next level. */
2330
case '#': /* SYSINIT -> BOOT */
2331
INITDBG(L_VB, "SYSINIT -> BOOT");
2333
/* Write a boot record. */
2334
wrote_utmp_reboot = 0;
2335
wrote_wtmp_reboot = 0;
2336
write_utmp_wtmp("reboot", "~~", 0, BOOT_TIME, "~");
2338
/* Get our run level */
2339
newlevel = dfl_level ? dfl_level : get_init_default();
2340
if (newlevel == 'S') {
2341
runlevel = newlevel;
2342
/* Not really 'S' but show anyway. */
2343
setproctitle("init [S]");
2347
case '*': /* BOOT -> NORMAL */
2348
INITDBG(L_VB, "BOOT -> NORMAL");
2349
if (runlevel != newlevel)
2350
loglevel = newlevel;
2351
runlevel = newlevel;
2355
case 'S': /* Ended SU mode */
2357
INITDBG(L_VB, "END SU MODE");
2358
newlevel = get_init_default();
2359
if (!did_boot && newlevel != 'S')
2362
if (runlevel != newlevel)
2363
loglevel = newlevel;
2364
runlevel = newlevel;
2368
for(ch = family; ch; ch = ch->next)
2369
if (strcmp(ch->rlevel, "S") == 0)
2370
ch->flags &= ~(FAILING|WAITING|XECUTED);
2375
"no more processes left in this runlevel");
2378
if (got_signals == 0)
2383
initlog(L_VB, "Entering runlevel: %c", runlevel);
2384
wrote_utmp_rlevel = 0;
2385
wrote_wtmp_rlevel = 0;
2386
write_utmp_wtmp("runlevel", "~~", runlevel + 256 * oldlevel, RUN_LVL, "~");
2387
thislevel = runlevel;
2388
prevlevel = oldlevel;
2389
setproctitle("init [%c]", (int)runlevel);
2395
* Init got hit by a signal. See which signal it is,
2396
* and act accordingly.
2399
void process_signals()
2407
if (ISMEMBER(got_signals, SIGPWR)) {
2408
INITDBG(L_VB, "got SIGPWR");
2409
/* See _what_ kind of SIGPWR this is. */
2411
if ((fd = open(PWRSTAT, O_RDONLY)) >= 0) {
2417
} else if ((fd = open(PWRSTAT_OLD, O_RDONLY)) >= 0) {
2418
/* Path changed 2010-03-20. Look for the old path for a while. */
2419
initlog(L_VB, "warning: found obsolete path %s, use %s instead",
2420
PWRSTAT_OLD, PWRSTAT);
2425
unlink(PWRSTAT_OLD);
2427
do_power_fail(pwrstat);
2428
DELSET(got_signals, SIGPWR);
2431
if (ISMEMBER(got_signals, SIGINT)) {
2432
#if defined(SIGINT_ONLYONCE) && (SIGINT_ONLYONCE == 1)
2433
/* Ignore any further signal from keyboard */
2434
struct sigaction sa;
2435
SETSIG(sa, SIGINT, SIG_IGN, SA_RESTART);
2437
INITDBG(L_VB, "got SIGINT");
2438
/* Tell ctrlaltdel entry to start up */
2439
for(ch = family; ch; ch = ch->next)
2440
if (ch->action == CTRLALTDEL)
2441
ch->flags &= ~XECUTED;
2442
DELSET(got_signals, SIGINT);
2445
if (ISMEMBER(got_signals, SIGWINCH)) {
2446
INITDBG(L_VB, "got SIGWINCH");
2447
/* Tell kbrequest entry to start up */
2448
for(ch = family; ch; ch = ch->next)
2449
if (ch->action == KBREQUEST)
2450
ch->flags &= ~XECUTED;
2451
DELSET(got_signals, SIGWINCH);
2454
if (ISMEMBER(got_signals, SIGALRM)) {
2455
INITDBG(L_VB, "got SIGALRM");
2456
/* The timer went off: check it out */
2457
DELSET(got_signals, SIGALRM);
2460
if (ISMEMBER(got_signals, SIGCHLD)) {
2461
INITDBG(L_VB, "got SIGCHLD");
2462
/* First set flag to 0 */
2463
DELSET(got_signals, SIGCHLD);
2465
/* See which child this was */
2466
for(ch = family; ch; ch = ch->next)
2467
if (ch->flags & ZOMBIE) {
2468
INITDBG(L_VB, "Child died, PID= %d", ch->pid);
2469
ch->flags &= ~(RUNNING|ZOMBIE|WAITING);
2470
if (ch->process[0] != '+')
2471
write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
2476
if (ISMEMBER(got_signals, SIGHUP)) {
2477
INITDBG(L_VB, "got SIGHUP");
2479
/* Are we waiting for a child? */
2480
for(ch = family; ch; ch = ch->next)
2481
if (ch->flags & WAITING) break;
2485
/* We need to go into a new runlevel */
2486
oldlevel = runlevel;
2488
runlevel = read_level(0);
2490
if (runlevel == 'U') {
2491
runlevel = oldlevel;
2494
if (oldlevel != 'S' && runlevel == 'S') console_stty();
2495
if (runlevel == '6' || runlevel == '0' ||
2496
runlevel == '1') console_stty();
2499
setproctitle("init [%c]", (int)runlevel);
2500
DELSET(got_signals, SIGHUP);
2504
if (ISMEMBER(got_signals, SIGUSR1)) {
2506
* SIGUSR1 means close and reopen /dev/initctl
2508
INITDBG(L_VB, "got SIGUSR1");
2511
DELSET(got_signals, SIGUSR1);
2519
void init_main(void)
2522
struct sigaction sa;
2530
* Fork so we can debug the init process.
2532
if ((f = fork()) > 0) {
2533
static const char killmsg[] = "PRNT: init killed.\r\n";
2536
while((rc = wait(&st)) != f)
2537
if (rc < 0 && errno == ECHILD)
2539
write(1, killmsg, sizeof(killmsg) - 1);
2546
* Tell the kernel to send us SIGINT when CTRL-ALT-DEL
2547
* is pressed, and that we want to handle keyboard signals.
2549
init_reboot(BMAGIC_SOFT);
2550
if ((f = open(VT_MASTER, O_RDWR | O_NOCTTY)) >= 0) {
2551
(void) ioctl(f, KDSIGACCEPT, SIGWINCH);
2554
(void) ioctl(0, KDSIGACCEPT, SIGWINCH);
2558
* Ignore all signals.
2560
for(f = 1; f <= NSIG; f++)
2561
SETSIG(sa, f, SIG_IGN, SA_RESTART);
2564
SETSIG(sa, SIGALRM, signal_handler, 0);
2565
SETSIG(sa, SIGHUP, signal_handler, 0);
2566
SETSIG(sa, SIGINT, signal_handler, 0);
2567
SETSIG(sa, SIGCHLD, chld_handler, SA_RESTART);
2568
SETSIG(sa, SIGPWR, signal_handler, 0);
2569
SETSIG(sa, SIGWINCH, signal_handler, 0);
2570
SETSIG(sa, SIGUSR1, signal_handler, 0);
2571
SETSIG(sa, SIGSTOP, stop_handler, SA_RESTART);
2572
SETSIG(sa, SIGTSTP, stop_handler, SA_RESTART);
2573
SETSIG(sa, SIGCONT, cont_handler, SA_RESTART);
2574
SETSIG(sa, SIGSEGV, (void (*)(int))segv_handler, SA_RESTART);
2581
/* Close whatever files are open, and reset the console. */
2589
* Set default PATH variable.
2591
setenv("PATH", PATH_DEFAULT, 1 /* Overwrite */);
2594
* Initialize /var/run/utmp (only works if /var is on
2595
* root and mounted rw)
2597
if ((fd = open(UTMP_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0)
2601
* Say hello to the world
2603
initlog(L_CO, bootmsg, "booting");
2606
* See if we have to start an emergency shell.
2610
SETSIG(sa, SIGCHLD, SIG_DFL, SA_RESTART);
2611
if (spawn(&ch_emerg, &f) > 0) {
2612
while((rc = wait(&st)) != f)
2613
if (rc < 0 && errno == ECHILD)
2616
SETSIG(sa, SIGCHLD, chld_handler, SA_RESTART);
2620
* Start normal boot procedure.
2627
* Restart: unblock signals and let the show go on
2629
initlog(L_CO, bootmsg, "reloading");
2631
sigprocmask(SIG_UNBLOCK, &sgt, NULL);
2634
* Set default PATH variable.
2636
setenv("PATH", PATH_DEFAULT, 0 /* Don't overwrite */);
2642
/* See if we need to make the boot transitions. */
2644
INITDBG(L_VB, "init_main: waiting..");
2646
/* Check if there are processes to be waited on. */
2647
for(ch = family; ch; ch = ch->next)
2648
if ((ch->flags & RUNNING) && ch->action != BOOT) break;
2651
/* Wait until we get hit by some signal. */
2652
while (ch != NULL && got_signals == 0) {
2653
if (ISMEMBER(got_signals, SIGHUP)) {
2654
/* See if there are processes to be waited on. */
2655
for(ch = family; ch; ch = ch->next)
2656
if (ch->flags & WAITING) break;
2658
if (ch != NULL) check_init_fifo();
2660
#else /* CHANGE_WAIT */
2661
if (ch != NULL && got_signals == 0) check_init_fifo();
2662
#endif /* CHANGE_WAIT */
2664
/* Check the 'failing' flags */
2667
/* Process any signals. */
2670
/* See what we need to start up (again) */
2677
* Tell the user about the syntax we expect.
2682
fprintf(stderr, "Usage: %s {-e VAR[=VAL] | [-t SECONDS] {0|1|2|3|4|5|6|S|s|Q|q|A|a|B|b|C|c|U|u}}\n", s);
2687
int telinit(char *progname, int argc, char **argv)
2689
#ifdef TELINIT_USES_INITLVL
2692
struct init_request request;
2693
struct sigaction sa;
2697
memset(&request, 0, sizeof(request));
2698
request.magic = INIT_MAGIC;
2700
while ((f = getopt(argc, argv, "t:e:")) != EOF) switch(f) {
2702
sltime = atoi(optarg);
2706
env = request.i.data;
2708
if (env + l + 2 > request.i.data + sizeof(request.i.data)) {
2709
fprintf(stderr, "%s: -e option data "
2710
"too large\n", progname);
2713
memcpy(env, optarg, l);
2722
if (env) *env++ = 0;
2727
request.cmd = INIT_CMD_SETENV;
2729
if (argc - optind != 1 || strlen(argv[optind]) != 1)
2731
if (!strchr("0123456789SsQqAaBbCcUu", argv[optind][0]))
2733
request.cmd = INIT_CMD_RUNLVL;
2734
request.runlevel = env ? 0 : argv[optind][0];
2735
request.sleeptime = sltime;
2738
/* Change to the root directory. */
2741
/* Open the fifo and write a command. */
2742
/* Make sure we don't hang on opening /dev/initctl */
2743
SETSIG(sa, SIGALRM, signal_handler, 0);
2745
if ((fd = open(INIT_FIFO, O_WRONLY)) >= 0) {
2747
size_t s = sizeof(request);
2748
void *ptr = &request;
2751
p = write(fd, ptr, s);
2753
if (errno == EINTR || errno == EAGAIN)
2765
#ifdef TELINIT_USES_INITLVL
2766
if (request.cmd == INIT_CMD_RUNLVL) {
2767
/* Fallthrough to the old method. */
2769
/* Now write the new runlevel. */
2770
if ((fp = fopen(INITLVL, "w")) == NULL) {
2771
fprintf(stderr, "%s: cannot create %s\n",
2775
fprintf(fp, "%s %d", argv[optind], sltime);
2778
/* And tell init about the pending runlevel change. */
2779
if (kill(INITPID, SIGHUP) < 0) perror(progname);
2785
fprintf(stderr, "%s: ", progname);
2786
if (ISMEMBER(got_signals, SIGALRM)) {
2787
fprintf(stderr, "timeout opening/writing control channel %s\n",
2796
* Main entry for init and telinit.
2798
int main(int argc, char **argv)
2807
/* Get my own name */
2808
if ((p = strrchr(argv[0], '/')) != NULL)
2817
if (geteuid() != 0) {
2818
fprintf(stderr, "%s: must be superuser.\n", p);
2823
* Is this telinit or init ?
2825
isinit = (getpid() == 1);
2826
for (f = 1; f < argc; f++) {
2827
if (!strcmp(argv[f], "-i") || !strcmp(argv[f], "--init")) {
2832
if (!isinit) exit(telinit(p, argc, argv));
2837
if (check_pipe(STATE_PIPE)) {
2839
receive_state(STATE_PIPE);
2841
myname = istrdup(argv[0]);
2844
for (f = 0; f < argc; f++)
2845
maxproclen += strlen(argv[f]) + 1;
2847
setproctitle("init [%c]", (int)runlevel);
2852
/* Check command line arguments */
2853
maxproclen = strlen(argv[0]) + 1;
2854
for(f = 1; f < argc; f++) {
2855
if (!strcmp(argv[f], "single") || !strcmp(argv[f], "-s"))
2857
else if (!strcmp(argv[f], "-a") || !strcmp(argv[f], "auto"))
2858
putenv("AUTOBOOT=YES");
2859
else if (!strcmp(argv[f], "-b") || !strcmp(argv[f],"emergency"))
2861
else if (!strcmp(argv[f], "-z")) {
2863
if (argv[f + 1]) f++;
2864
} else if (strchr("0123456789sS", argv[f][0])
2865
&& strlen(argv[f]) == 1)
2866
dfl_level = argv[f][0];
2867
/* "init u" in the very beginning makes no sense */
2868
if (dfl_level == 's') dfl_level = 'S';
2869
maxproclen += strlen(argv[f]) + 1;
2873
if (getenv("SELINUX_INIT") == NULL) {
2874
const int rc = mount("proc", "/proc", "proc", 0, 0);
2875
if (is_selinux_enabled() > 0) {
2876
putenv("SELINUX_INIT=YES");
2877
if (rc == 0) umount2("/proc", MNT_DETACH);
2878
if (selinux_init_load_policy(&enforce) == 0) {
2879
execv(myname, argv);
2882
/* SELinux in enforcing mode but load_policy failed */
2883
/* At this point, we probably can't open /dev/console, so log() won't work */
2884
fprintf(stderr,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.\n");
2889
if (rc == 0) umount2("/proc", MNT_DETACH);
2892
/* Start booting. */
2895
setproctitle("init boot");