Document a fall through case block (Coverity CID 1164467).
[sysvinit.git] / src / init.c
CommitLineData
a74aeac6
PR
1/*
2 * Init A System-V Init Clone.
3 *
4 * Usage: /sbin/init
5 * init [0123456SsQqAaBbCc]
6 * telinit [0123456SsQqAaBbCc]
7 *
8 * Version: @(#)init.c 2.86 30-Jul-2004 miquels@cistron.nl
9 */
c6fb5841 10#define VERSION "2.89"
23d97522 11#define DATE "26-Mar-2010"
a74aeac6
PR
12/*
13 * This file is part of the sysvinit suite,
14 * Copyright (C) 1991-2004 Miquel van Smoorenburg.
15 *
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.
20 *
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.
25 *
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
29 *
30 */
31
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/ioctl.h>
35#include <sys/wait.h>
36#ifdef __linux__
37#include <sys/kd.h>
38#endif
39#include <sys/resource.h>
40#include <stdlib.h>
41#include <unistd.h>
42#include <errno.h>
43#include <stdio.h>
44#include <time.h>
45#include <fcntl.h>
46#include <string.h>
47#include <signal.h>
48#include <termios.h>
49#include <utmp.h>
50#include <ctype.h>
51#include <stdarg.h>
52#include <sys/syslog.h>
53#include <sys/time.h>
54
55#ifdef WITH_SELINUX
55f242bf 56# include <selinux/selinux.h>
a74aeac6
PR
57#endif
58
a74aeac6 59#ifdef __i386__
5f959b29 60# ifdef __GLIBC__
a74aeac6
PR
61 /* GNU libc 2.x */
62# define STACK_DEBUG 1
63# if (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
64 /* Only glibc 2.0 needs this */
65# include <sigcontext.h>
5f959b29
DWF
66# elif ( __GLIBC__ > 2) && ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1))
67# include <bits/sigcontext.h>
a74aeac6
PR
68# endif
69# endif
70#endif
71
72#include "init.h"
73#include "initreq.h"
74#include "paths.h"
75#include "reboot.h"
76#include "set.h"
77
78#ifndef SIGPWR
79# define SIGPWR SIGUSR2
80#endif
81
82#ifndef CBAUD
83# define CBAUD 0
84#endif
85#ifndef CBAUDEX
86# define CBAUDEX 0
87#endif
88
89/* Set a signal handler. */
90#define SETSIG(sa, sig, fun, flags) \
91 do { \
c5ae561e 92 memset(&sa, 0, sizeof(sa)); \
a74aeac6
PR
93 sa.sa_handler = fun; \
94 sa.sa_flags = flags; \
95 sigemptyset(&sa.sa_mask); \
96 sigaction(sig, &sa, NULL); \
97 } while(0)
98
99/* Version information */
100char *Version = "@(#) init " VERSION " " DATE " miquels@cistron.nl";
101char *bootmsg = "version " VERSION " %s";
102#define E_VERSION "INIT_VERSION=sysvinit-" VERSION
103
104CHILD *family = NULL; /* The linked list of all entries */
105CHILD *newFamily = NULL; /* The list after inittab re-read */
106
107CHILD ch_emerg = { /* Emergency shell */
108 WAITING, 0, 0, 0, 0,
109 "~~",
110 "S",
111 3,
112 "/sbin/sulogin",
113 NULL,
114 NULL
115};
116
117char runlevel = 'S'; /* The current run level */
118char thislevel = 'S'; /* The current runlevel */
119char prevlevel = 'N'; /* Previous runlevel */
120int dfl_level = 0; /* Default runlevel */
121sig_atomic_t got_cont = 0; /* Set if we received the SIGCONT signal */
122sig_atomic_t got_signals; /* Set if we received a signal. */
123int emerg_shell = 0; /* Start emergency shell? */
124int wrote_wtmp_reboot = 1; /* Set when we wrote the reboot record */
125int wrote_utmp_reboot = 1; /* Set when we wrote the reboot record */
63a2c981
DWF
126int wrote_wtmp_rlevel = 1; /* Set when we wrote the runlevel record */
127int wrote_utmp_rlevel = 1; /* Set when we wrote the runlevel record */
a74aeac6
PR
128int sltime = 5; /* Sleep time between TERM and KILL */
129char *argv0; /* First arguments; show up in ps listing */
130int maxproclen; /* Maximal length of argv[0] with \0 */
131struct utmp utproto; /* Only used for sizeof(utproto.ut_id) */
a74aeac6
PR
132char *console_dev; /* Console device. */
133int pipe_fd = -1; /* /dev/initctl */
134int did_boot = 0; /* Did we already do BOOT* stuff? */
135int main(int, char **);
136
137/* Used by re-exec part */
138int reload = 0; /* Should we do initialization stuff? */
139char *myname="/sbin/init"; /* What should we exec */
140int oops_error; /* Used by some of the re-exec code. */
141const char *Signature = "12567362"; /* Signature for re-exec fd */
142
143/* Macro to see if this is a special action */
144#define ISPOWER(i) ((i) == POWERWAIT || (i) == POWERFAIL || \
145 (i) == POWEROKWAIT || (i) == POWERFAILNOW || \
146 (i) == CTRLALTDEL)
147
148/* ascii values for the `action' field. */
149struct actions {
150 char *name;
151 int act;
152} actions[] = {
153 { "respawn", RESPAWN },
154 { "wait", WAIT },
155 { "once", ONCE },
156 { "boot", BOOT },
157 { "bootwait", BOOTWAIT },
158 { "powerfail", POWERFAIL },
159 { "powerfailnow",POWERFAILNOW },
160 { "powerwait", POWERWAIT },
161 { "powerokwait", POWEROKWAIT },
162 { "ctrlaltdel", CTRLALTDEL },
163 { "off", OFF },
164 { "ondemand", ONDEMAND },
165 { "initdefault", INITDEFAULT },
166 { "sysinit", SYSINIT },
167 { "kbrequest", KBREQUEST },
168 { NULL, 0 },
169};
170
171/*
172 * State parser token table (see receive_state)
173 */
174struct {
175 char name[4];
176 int cmd;
177} cmds[] = {
178 { "VER", C_VER },
179 { "END", C_END },
180 { "REC", C_REC },
181 { "EOR", C_EOR },
182 { "LEV", C_LEV },
183 { "FL ", C_FLAG },
184 { "AC ", C_ACTION },
185 { "CMD", C_PROCESS },
186 { "PID", C_PID },
187 { "EXS", C_EXS },
188 { "-RL", D_RUNLEVEL },
189 { "-TL", D_THISLEVEL },
190 { "-PL", D_PREVLEVEL },
191 { "-SI", D_GOTSIGN },
192 { "-WR", D_WROTE_WTMP_REBOOT},
193 { "-WU", D_WROTE_UTMP_REBOOT},
194 { "-ST", D_SLTIME },
195 { "-DB", D_DIDBOOT },
63a2c981
DWF
196 { "-LW", D_WROTE_WTMP_RLEVEL},
197 { "-LU", D_WROTE_UTMP_RLEVEL},
a74aeac6
PR
198 { "", 0 }
199};
200struct {
201 char *name;
202 int mask;
203} flags[]={
204 {"RU",RUNNING},
205 {"DE",DEMAND},
206 {"XD",XECUTED},
207 {"WT",WAITING},
208 {NULL,0}
209};
210
211#define NR_EXTRA_ENV 16
212char *extra_env[NR_EXTRA_ENV];
213
214
215/*
216 * Sleep a number of seconds.
217 *
218 * This only works correctly because the linux select updates
219 * the elapsed time in the struct timeval passed to select!
220 */
5f959b29 221static
a74aeac6
PR
222void do_sleep(int sec)
223{
224 struct timeval tv;
225
226 tv.tv_sec = sec;
227 tv.tv_usec = 0;
228
229 while(select(0, NULL, NULL, NULL, &tv) < 0 && errno == EINTR)
230 ;
231}
232
233
234/*
235 * Non-failing allocation routines (init cannot fail).
236 */
5f959b29 237static
a74aeac6
PR
238void *imalloc(size_t size)
239{
240 void *m;
241
242 while ((m = malloc(size)) == NULL) {
243 initlog(L_VB, "out of memory");
244 do_sleep(5);
245 }
246 memset(m, 0, size);
247 return m;
248}
249
5f959b29 250static
23de6d4a 251char *istrdup(const char *s)
a74aeac6
PR
252{
253 char *m;
254 int l;
255
256 l = strlen(s) + 1;
257 m = imalloc(l);
258 memcpy(m, s, l);
259 return m;
260}
261
262
263/*
264 * Send the state info of the previous running init to
265 * the new one, in a version-independant way.
266 */
5f959b29 267static
a74aeac6
PR
268void send_state(int fd)
269{
270 FILE *fp;
271 CHILD *p;
272 int i,val;
273
274 fp = fdopen(fd,"w");
275
276 fprintf(fp, "VER%s\n", Version);
277 fprintf(fp, "-RL%c\n", runlevel);
278 fprintf(fp, "-TL%c\n", thislevel);
279 fprintf(fp, "-PL%c\n", prevlevel);
280 fprintf(fp, "-SI%u\n", got_signals);
281 fprintf(fp, "-WR%d\n", wrote_wtmp_reboot);
282 fprintf(fp, "-WU%d\n", wrote_utmp_reboot);
283 fprintf(fp, "-ST%d\n", sltime);
284 fprintf(fp, "-DB%d\n", did_boot);
285
286 for (p = family; p; p = p->next) {
287 fprintf(fp, "REC%s\n", p->id);
288 fprintf(fp, "LEV%s\n", p->rlevel);
289 for (i = 0, val = p->flags; flags[i].mask; i++)
290 if (val & flags[i].mask) {
291 val &= ~flags[i].mask;
292 fprintf(fp, "FL %s\n",flags[i].name);
293 }
294 fprintf(fp, "PID%d\n",p->pid);
295 fprintf(fp, "EXS%u\n",p->exstat);
296 for(i = 0; actions[i].act; i++)
297 if (actions[i].act == p->action) {
298 fprintf(fp, "AC %s\n", actions[i].name);
299 break;
300 }
301 fprintf(fp, "CMD%s\n", p->process);
302 fprintf(fp, "EOR\n");
303 }
304 fprintf(fp, "END\n");
305 fclose(fp);
306}
307
308/*
309 * Read a string from a file descriptor.
310 * FIXME: why not use fgets() ?
311 */
312static int get_string(char *p, int size, FILE *f)
313{
314 int c;
315
316 while ((c = getc(f)) != EOF && c != '\n') {
317 if (--size > 0)
318 *p++ = c;
319 }
320 *p = '\0';
321 return (c != EOF) && (size > 0);
322}
323
324/*
325 * Read trailing data from the state pipe until we see a newline.
326 */
327static int get_void(FILE *f)
328{
329 int c;
330
331 while ((c = getc(f)) != EOF && c != '\n')
332 ;
333
334 return (c != EOF);
335}
336
337/*
338 * Read the next "command" from the state pipe.
339 */
340static int get_cmd(FILE *f)
341{
342 char cmd[4] = " ";
343 int i;
344
345 if (fread(cmd, 1, sizeof(cmd) - 1, f) != sizeof(cmd) - 1)
346 return C_EOF;
347
348 for(i = 0; cmds[i].cmd && strcmp(cmds[i].name, cmd) != 0; i++)
349 ;
350 return cmds[i].cmd;
351}
352
353/*
354 * Read a CHILD * from the state pipe.
355 */
356static CHILD *get_record(FILE *f)
357{
358 int cmd;
359 char s[32];
360 int i;
361 CHILD *p;
362
363 do {
364 switch (cmd = get_cmd(f)) {
365 case C_END:
366 get_void(f);
367 return NULL;
368 case 0:
369 get_void(f);
370 break;
371 case C_REC:
372 break;
373 case D_RUNLEVEL:
374 fscanf(f, "%c\n", &runlevel);
375 break;
376 case D_THISLEVEL:
377 fscanf(f, "%c\n", &thislevel);
378 break;
379 case D_PREVLEVEL:
380 fscanf(f, "%c\n", &prevlevel);
381 break;
382 case D_GOTSIGN:
383 fscanf(f, "%u\n", &got_signals);
384 break;
385 case D_WROTE_WTMP_REBOOT:
386 fscanf(f, "%d\n", &wrote_wtmp_reboot);
387 break;
388 case D_WROTE_UTMP_REBOOT:
389 fscanf(f, "%d\n", &wrote_utmp_reboot);
390 break;
391 case D_SLTIME:
392 fscanf(f, "%d\n", &sltime);
393 break;
394 case D_DIDBOOT:
395 fscanf(f, "%d\n", &did_boot);
396 break;
63a2c981
DWF
397 case D_WROTE_WTMP_RLEVEL:
398 fscanf(f, "%d\n", &wrote_wtmp_rlevel);
399 break;
400 case D_WROTE_UTMP_RLEVEL:
401 fscanf(f, "%d\n", &wrote_utmp_rlevel);
402 break;
a74aeac6
PR
403 default:
404 if (cmd > 0 || cmd == C_EOF) {
405 oops_error = -1;
406 return NULL;
407 }
408 }
409 } while (cmd != C_REC);
410
411 p = imalloc(sizeof(CHILD));
412 get_string(p->id, sizeof(p->id), f);
413
414 do switch(cmd = get_cmd(f)) {
415 case 0:
416 case C_EOR:
417 get_void(f);
418 break;
419 case C_PID:
420 fscanf(f, "%d\n", &(p->pid));
421 break;
422 case C_EXS:
423 fscanf(f, "%u\n", &(p->exstat));
424 break;
425 case C_LEV:
426 get_string(p->rlevel, sizeof(p->rlevel), f);
427 break;
428 case C_PROCESS:
429 get_string(p->process, sizeof(p->process), f);
430 break;
431 case C_FLAG:
432 get_string(s, sizeof(s), f);
433 for(i = 0; flags[i].name; i++) {
434 if (strcmp(flags[i].name,s) == 0)
435 break;
436 }
437 p->flags |= flags[i].mask;
438 break;
439 case C_ACTION:
440 get_string(s, sizeof(s), f);
441 for(i = 0; actions[i].name; i++) {
442 if (strcmp(actions[i].name, s) == 0)
443 break;
444 }
445 p->action = actions[i].act ? actions[i].act : OFF;
446 break;
447 default:
448 free(p);
449 oops_error = -1;
450 return NULL;
451 } while( cmd != C_EOR);
452
453 return p;
454}
455
456/*
457 * Read the complete state info from the state pipe.
458 * Returns 0 on success
459 */
5f959b29 460static
a74aeac6
PR
461int receive_state(int fd)
462{
463 FILE *f;
464 char old_version[256];
465 CHILD **pp;
466
467 f = fdopen(fd, "r");
468
469 if (get_cmd(f) != C_VER)
470 return -1;
471 get_string(old_version, sizeof(old_version), f);
472 oops_error = 0;
473 for (pp = &family; (*pp = get_record(f)) != NULL; pp = &((*pp)->next))
474 ;
475 fclose(f);
476 return oops_error;
477}
478
479/*
480 * Set the process title.
481 */
482#ifdef __GNUC__
483__attribute__ ((format (printf, 1, 2)))
484#endif
485static int setproctitle(char *fmt, ...)
486{
487 va_list ap;
488 int len;
489 char buf[256];
490
491 buf[0] = 0;
492
493 va_start(ap, fmt);
494 len = vsnprintf(buf, sizeof(buf), fmt, ap);
495 va_end(ap);
496
497 if (maxproclen > 1) {
498 memset(argv0, 0, maxproclen);
499 strncpy(argv0, buf, maxproclen - 1);
500 }
501
502 return len;
503}
504
505/*
506 * Set console_dev to a working console.
507 */
5f959b29 508static
a74aeac6
PR
509void console_init(void)
510{
511 int fd;
512 int tried_devcons = 0;
513 int tried_vtmaster = 0;
514 char *s;
515
a33820c3 516 if ((s = getenv("CONSOLE")) != NULL)
a74aeac6
PR
517 console_dev = s;
518 else {
519 console_dev = CONSOLE;
520 tried_devcons++;
521 }
522
523 while ((fd = open(console_dev, O_RDONLY|O_NONBLOCK)) < 0) {
524 if (!tried_devcons) {
525 tried_devcons++;
526 console_dev = CONSOLE;
527 continue;
528 }
529 if (!tried_vtmaster) {
530 tried_vtmaster++;
531 console_dev = VT_MASTER;
532 continue;
533 }
534 break;
535 }
536 if (fd < 0)
537 console_dev = "/dev/null";
538 else
539 close(fd);
540}
541
542
543/*
544 * Open the console with retries.
545 */
5f959b29 546static
a74aeac6
PR
547int console_open(int mode)
548{
549 int f, fd = -1;
550 int m;
551
552 /*
553 * Open device in nonblocking mode.
554 */
555 m = mode | O_NONBLOCK;
556
557 /*
558 * Retry the open five times.
559 */
560 for(f = 0; f < 5; f++) {
561 if ((fd = open(console_dev, m)) >= 0) break;
23d97522 562 usleep(10000);
a74aeac6
PR
563 }
564
565 if (fd < 0) return fd;
566
567 /*
568 * Set original flags.
569 */
570 if (m != mode)
571 fcntl(fd, F_SETFL, mode);
572 return fd;
573}
574
575/*
576 * We got a signal (HUP PWR WINCH ALRM INT)
577 */
5f959b29 578static
a74aeac6
PR
579void signal_handler(int sig)
580{
581 ADDSET(got_signals, sig);
582}
583
584/*
585 * SIGCHLD: one of our children has died.
586 */
5f959b29
DWF
587static
588# ifdef __GNUC__
589void chld_handler(int sig __attribute__((unused)))
590# else
591void chld_handler(int sig)
592# endif
a74aeac6
PR
593{
594 CHILD *ch;
595 int pid, st;
596 int saved_errno = errno;
597
598 /*
599 * Find out which process(es) this was (were)
600 */
601 while((pid = waitpid(-1, &st, WNOHANG)) != 0) {
602 if (errno == ECHILD) break;
603 for( ch = family; ch; ch = ch->next )
604 if ( ch->pid == pid && (ch->flags & RUNNING) ) {
605 INITDBG(L_VB,
606 "chld_handler: marked %d as zombie",
607 ch->pid);
608 ADDSET(got_signals, SIGCHLD);
609 ch->exstat = st;
610 ch->flags |= ZOMBIE;
611 if (ch->new) {
612 ch->new->exstat = st;
613 ch->new->flags |= ZOMBIE;
614 }
615 break;
616 }
617 if (ch == NULL) {
618 INITDBG(L_VB, "chld_handler: unknown child %d exited.",
619 pid);
620 }
621 }
622 errno = saved_errno;
623}
624
625/*
626 * Linux ignores all signals sent to init when the
627 * SIG_DFL handler is installed. Therefore we must catch SIGTSTP
628 * and SIGCONT, or else they won't work....
629 *
630 * The SIGCONT handler
631 */
5f959b29
DWF
632static
633# ifdef __GNUC__
634void cont_handler(int sig __attribute__((unused)))
635# else
636void cont_handler(int sig)
637# endif
a74aeac6
PR
638{
639 got_cont = 1;
640}
641
642/*
643 * Fork and dump core in /.
644 */
5f959b29 645static
a74aeac6
PR
646void coredump(void)
647{
648 static int dumped = 0;
649 struct rlimit rlim;
650 sigset_t mask;
651
652 if (dumped) return;
653 dumped = 1;
654
655 if (fork() != 0) return;
656
657 sigfillset(&mask);
658 sigprocmask(SIG_SETMASK, &mask, NULL);
659
660 rlim.rlim_cur = RLIM_INFINITY;
661 rlim.rlim_max = RLIM_INFINITY;
662 setrlimit(RLIMIT_CORE, &rlim);
663 chdir("/");
664
665 signal(SIGSEGV, SIG_DFL);
666 raise(SIGSEGV);
667 sigdelset(&mask, SIGSEGV);
668 sigprocmask(SIG_SETMASK, &mask, NULL);
669
670 do_sleep(5);
671 exit(0);
672}
673
674/*
675 * OOPS: segmentation violation!
676 * If we have the info, print where it occured.
677 * Then sleep 30 seconds and try to continue.
678 */
5f959b29 679static
a74aeac6 680#if defined(STACK_DEBUG) && defined(__linux__)
5f959b29
DWF
681# ifdef __GNUC__
682void segv_handler(int sig __attribute__((unused)), struct sigcontext ctx)
683# else
a74aeac6 684void segv_handler(int sig, struct sigcontext ctx)
5f959b29 685# endif
a74aeac6
PR
686{
687 char *p = "";
688 int saved_errno = errno;
689
690 if ((void *)ctx.eip >= (void *)do_sleep &&
691 (void *)ctx.eip < (void *)main)
692 p = " (code)";
693 initlog(L_VB, "PANIC: segmentation violation at %p%s! "
694 "sleeping for 30 seconds.", (void *)ctx.eip, p);
695 coredump();
696 do_sleep(30);
697 errno = saved_errno;
698}
699#else
5f959b29
DWF
700# ifdef __GNUC__
701void segv_handler(int sig __attribute__((unused)))
702# else
703void segv_handler(int sig)
704# endif
a74aeac6
PR
705{
706 int saved_errno = errno;
707
708 initlog(L_VB,
709 "PANIC: segmentation violation! sleeping for 30 seconds.");
710 coredump();
711 do_sleep(30);
712 errno = saved_errno;
713}
714#endif
715
716/*
717 * The SIGSTOP & SIGTSTP handler
718 */
5f959b29
DWF
719static
720# ifdef __GNUC__
721void stop_handler(int sig __attribute__((unused)))
722# else
723void stop_handler(int sig)
724# endif
a74aeac6
PR
725{
726 int saved_errno = errno;
727
728 got_cont = 0;
729 while(!got_cont) pause();
730 got_cont = 0;
731 errno = saved_errno;
732}
733
734/*
735 * Set terminal settings to reasonable defaults
736 */
5f959b29 737static
a74aeac6
PR
738void console_stty(void)
739{
740 struct termios tty;
741 int fd;
742
743 if ((fd = console_open(O_RDWR|O_NOCTTY)) < 0) {
744 initlog(L_VB, "can't open %s", console_dev);
745 return;
746 }
747
55f242bf
DWF
748#ifdef __FreeBSD_kernel__
749 /*
750 * The kernel of FreeBSD expects userland to set TERM. Usually, we want
751 * "cons25". Later, gettys might disagree on this (i.e. we're not using
752 * syscons) but some boot scripts, like /etc/init.d/xserver-xorg, still
753 * need a non-dumb terminal.
754 */
755 putenv ("TERM=cons25");
756#endif
757
a74aeac6
PR
758 (void) tcgetattr(fd, &tty);
759
760 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
761 tty.c_cflag |= HUPCL|CLOCAL|CREAD;
762
5f959b29
DWF
763 tty.c_cc[VINTR] = CINTR;
764 tty.c_cc[VQUIT] = CQUIT;
765 tty.c_cc[VERASE] = CERASE; /* ASCII DEL (0177) */
766 tty.c_cc[VKILL] = CKILL;
767 tty.c_cc[VEOF] = CEOF;
768 tty.c_cc[VTIME] = 0;
769 tty.c_cc[VMIN] = 1;
fc3def35 770#ifdef VSWTC /* not defined on FreeBSD */
5f959b29 771 tty.c_cc[VSWTC] = _POSIX_VDISABLE;
fc3def35 772#endif /* VSWTC */
5f959b29
DWF
773 tty.c_cc[VSTART] = CSTART;
774 tty.c_cc[VSTOP] = CSTOP;
775 tty.c_cc[VSUSP] = CSUSP;
776 tty.c_cc[VEOL] = _POSIX_VDISABLE;
777 tty.c_cc[VREPRINT] = CREPRINT;
778 tty.c_cc[VDISCARD] = CDISCARD;
779 tty.c_cc[VWERASE] = CWERASE;
780 tty.c_cc[VLNEXT] = CLNEXT;
781 tty.c_cc[VEOL2] = _POSIX_VDISABLE;
a74aeac6
PR
782
783 /*
784 * Set pre and post processing
785 */
33dca826
DWF
786 tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY
787#ifdef IUTF8 /* Not defined on FreeBSD */
788 | (tty.c_iflag & IUTF8)
334256b0 789#endif /* IUTF8 */
33dca826 790 ;
a74aeac6
PR
791 tty.c_oflag = OPOST|ONLCR;
792 tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE;
793
5f959b29
DWF
794#if defined(SANE_TIO) && (SANE_TIO == 1)
795 /*
796 * Disable flow control (-ixon), ignore break (ignbrk),
797 * and make nl/cr more usable (sane).
798 */
799 tty.c_iflag |= IGNBRK;
800 tty.c_iflag &= ~(BRKINT|INLCR|IGNCR|IXON);
801 tty.c_oflag &= ~(OCRNL|ONLRET);
802#endif
a74aeac6
PR
803 /*
804 * Now set the terminal line.
805 * We don't care about non-transmitted output data
806 * and non-read input data.
807 */
808 (void) tcsetattr(fd, TCSANOW, &tty);
809 (void) tcflush(fd, TCIOFLUSH);
810 (void) close(fd);
811}
812
c5ae561e
DWF
813static ssize_t
814safe_write(int fd, const char *buffer, size_t count)
815{
816 ssize_t offset = 0;
817
818 while (count > 0) {
819 ssize_t block = write(fd, &buffer[offset], count);
820
821 if (block < 0 && errno == EINTR)
822 continue;
823 if (block <= 0)
824 return offset ? offset : block;
825 offset += block;
826 count -= block;
827 }
828 return offset;
829}
830
a74aeac6
PR
831/*
832 * Print to the system console
833 */
834void print(char *s)
835{
836 int fd;
837
838 if ((fd = console_open(O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
c5ae561e 839 safe_write(fd, s, strlen(s));
a74aeac6
PR
840 close(fd);
841 }
842}
843
844/*
845 * Log something to a logfile and the console.
846 */
847#ifdef __GNUC__
848__attribute__ ((format (printf, 2, 3)))
849#endif
850void initlog(int loglevel, char *s, ...)
851{
852 va_list va_alist;
853 char buf[256];
854 sigset_t nmask, omask;
855
856 va_start(va_alist, s);
857 vsnprintf(buf, sizeof(buf), s, va_alist);
858 va_end(va_alist);
859
860 if (loglevel & L_SY) {
861 /*
862 * Re-establish connection with syslogd every time.
863 * Block signals while talking to syslog.
864 */
865 sigfillset(&nmask);
866 sigprocmask(SIG_BLOCK, &nmask, &omask);
867 openlog("init", 0, LOG_DAEMON);
868 syslog(LOG_INFO, "%s", buf);
869 closelog();
870 sigprocmask(SIG_SETMASK, &omask, NULL);
871 }
872
873 /*
874 * And log to the console.
875 */
876 if (loglevel & L_CO) {
877 print("\rINIT: ");
878 print(buf);
879 print("\r\n");
880 }
881}
882
23de6d4a
DWF
883/*
884 * Add or replace specific environment value
885 */
886int addnewenv(const char *new, char **curr, int n)
887{
888 size_t nlen = strcspn(new, "=");
889 int i;
890 for (i = 0; i < n; i++) {
891 if (nlen != strcspn(curr[i], "="))
892 continue;
893 if (strncmp (new, curr[i], nlen) == 0)
894 break;
895 }
896 if (i >= n)
897 curr[n++] = istrdup(new);
898 else {
899 free(curr[i]);
900 curr[i] = istrdup(new);
901 }
902 return n;
903}
a74aeac6
PR
904
905/*
906 * Build a new environment for execve().
907 */
908char **init_buildenv(int child)
909{
910 char i_lvl[] = "RUNLEVEL=x";
911 char i_prev[] = "PREVLEVEL=x";
23de6d4a 912 char i_cons[128];
5f959b29 913 char i_shell[] = "SHELL=" SHELL;
a74aeac6
PR
914 char **e;
915 int n, i;
916
917 for (n = 0; environ[n]; n++)
918 ;
fcda5be1 919 n += NR_EXTRA_ENV + 1; /* Also room for last NULL */
fd6f5c50 920 if (child)
23d97522 921 n += 8;
23de6d4a
DWF
922
923 while ((e = (char**)calloc(n, sizeof(char *))) == NULL) {
924 initlog(L_VB, "out of memory");
925 do_sleep(5);
926 }
a74aeac6
PR
927
928 for (n = 0; environ[n]; n++)
929 e[n] = istrdup(environ[n]);
930
23d97522 931 for (i = 0; i < NR_EXTRA_ENV; i++) {
23de6d4a
DWF
932 if (extra_env[i] == NULL || *extra_env[i] == '\0')
933 continue;
934 n = addnewenv(extra_env[i], e, n);
23d97522 935 }
a74aeac6
PR
936
937 if (child) {
938 snprintf(i_cons, sizeof(i_cons), "CONSOLE=%s", console_dev);
939 i_lvl[9] = thislevel;
940 i_prev[10] = prevlevel;
23de6d4a
DWF
941 n = addnewenv(i_shell, e, n);
942 n = addnewenv(i_lvl, e, n);
943 n = addnewenv(i_prev, e, n);
944 n = addnewenv(i_cons, e, n);
945 n = addnewenv(E_VERSION, e, n);
a74aeac6
PR
946 }
947
948 e[n++] = NULL;
949
950 return e;
951}
952
953
954void init_freeenv(char **e)
955{
956 int n;
957
958 for (n = 0; e[n]; n++)
959 free(e[n]);
960 free(e);
961}
962
963
964/*
965 * Fork and execute.
966 *
967 * This function is too long and indents too deep.
968 *
969 */
5f959b29 970static
23d97522 971pid_t spawn(CHILD *ch, int *res)
a74aeac6
PR
972{
973 char *args[16]; /* Argv array */
974 char buf[136]; /* Line buffer */
23d97522 975 int f, st; /* Scratch variables */
a74aeac6
PR
976 char *ptr; /* Ditto */
977 time_t t; /* System time */
978 int oldAlarm; /* Previous alarm value */
979 char *proc = ch->process; /* Command line */
980 pid_t pid, pgrp; /* child, console process group. */
981 sigset_t nmask, omask; /* For blocking SIGCHLD */
982 struct sigaction sa;
983
984 *res = -1;
985 buf[sizeof(buf) - 1] = 0;
986
987 /* Skip '+' if it's there */
988 if (proc[0] == '+') proc++;
989
990 ch->flags |= XECUTED;
991
992 if (ch->action == RESPAWN || ch->action == ONDEMAND) {
993 /* Is the date stamp from less than 2 minutes ago? */
994 time(&t);
995 if (ch->tm + TESTTIME > t) {
996 ch->count++;
997 } else {
998 ch->count = 0;
999 ch->tm = t;
1000 }
1001
1002 /* Do we try to respawn too fast? */
1003 if (ch->count >= MAXSPAWN) {
1004
1005 initlog(L_VB,
1006 "Id \"%s\" respawning too fast: disabled for %d minutes",
1007 ch->id, SLEEPTIME / 60);
1008 ch->flags &= ~RUNNING;
1009 ch->flags |= FAILING;
1010
1011 /* Remember the time we stopped */
1012 ch->tm = t;
1013
1014 /* Try again in 5 minutes */
1015 oldAlarm = alarm(0);
1016 if (oldAlarm > SLEEPTIME || oldAlarm <= 0) oldAlarm = SLEEPTIME;
1017 alarm(oldAlarm);
1018 return(-1);
1019 }
1020 }
1021
1022 /* See if there is an "initscript" (except in single user mode). */
1023 if (access(INITSCRIPT, R_OK) == 0 && runlevel != 'S') {
1024 /* Build command line using "initscript" */
1025 args[1] = SHELL;
1026 args[2] = INITSCRIPT;
1027 args[3] = ch->id;
1028 args[4] = ch->rlevel;
1029 args[5] = "unknown";
1030 for(f = 0; actions[f].name; f++) {
1031 if (ch->action == actions[f].act) {
1032 args[5] = actions[f].name;
1033 break;
1034 }
1035 }
1036 args[6] = proc;
1037 args[7] = NULL;
1038 } else if (strpbrk(proc, "~`!$^&*()=|\\{}[];\"'<>?")) {
1039 /* See if we need to fire off a shell for this command */
1040 /* Give command line to shell */
1041 args[1] = SHELL;
1042 args[2] = "-c";
1043 strcpy(buf, "exec ");
1044 strncat(buf, proc, sizeof(buf) - strlen(buf) - 1);
1045 args[3] = buf;
1046 args[4] = NULL;
1047 } else {
1048 /* Split up command line arguments */
1049 buf[0] = 0;
1050 strncat(buf, proc, sizeof(buf) - 1);
1051 ptr = buf;
1052 for(f = 1; f < 15; f++) {
1053 /* Skip white space */
1054 while(*ptr == ' ' || *ptr == '\t') ptr++;
1055 args[f] = ptr;
1056
1057 /* May be trailing space.. */
1058 if (*ptr == 0) break;
1059
1060 /* Skip this `word' */
1061 while(*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#')
1062 ptr++;
1063
1064 /* If end-of-line, break */
1065 if (*ptr == '#' || *ptr == 0) {
1066 f++;
1067 *ptr = 0;
1068 break;
1069 }
1070 /* End word with \0 and continue */
1071 *ptr++ = 0;
1072 }
1073 args[f] = NULL;
1074 }
1075 args[0] = args[1];
1076 while(1) {
1077 /*
1078 * Block sigchild while forking.
1079 */
1080 sigemptyset(&nmask);
1081 sigaddset(&nmask, SIGCHLD);
1082 sigprocmask(SIG_BLOCK, &nmask, &omask);
1083
1084 if ((pid = fork()) == 0) {
fd6f5c50 1085
a74aeac6
PR
1086 close(0);
1087 close(1);
1088 close(2);
1089 if (pipe_fd >= 0) close(pipe_fd);
1090
1091 sigprocmask(SIG_SETMASK, &omask, NULL);
1092
1093 /*
1094 * In sysinit, boot, bootwait or single user mode:
1095 * for any wait-type subprocess we _force_ the console
1096 * to be its controlling tty.
1097 */
1098 if (strchr("*#sS", runlevel) && ch->flags & WAITING) {
1099 /*
1100 * We fork once extra. This is so that we can
1101 * wait and change the process group and session
1102 * of the console after exit of the leader.
1103 */
1104 setsid();
1105 if ((f = console_open(O_RDWR|O_NOCTTY)) >= 0) {
1106 /* Take over controlling tty by force */
1107 (void)ioctl(f, TIOCSCTTY, 1);
1108 dup(f);
1109 dup(f);
1110 }
63a2c981
DWF
1111
1112 /*
1113 * 4 Sep 2001, Andrea Arcangeli:
1114 * Fix a race in spawn() that is used to deadlock init in a
1115 * waitpid() loop: must set the childhandler as default before forking
1116 * off the child or the chld_handler could run before the waitpid loop
1117 * has a chance to find its zombie-child.
1118 */
a74aeac6
PR
1119 SETSIG(sa, SIGCHLD, SIG_DFL, SA_RESTART);
1120 if ((pid = fork()) < 0) {
1121 initlog(L_VB, "cannot fork: %s",
1122 strerror(errno));
1123 exit(1);
1124 }
1125 if (pid > 0) {
23d97522 1126 pid_t rc;
a74aeac6
PR
1127 /*
1128 * Ignore keyboard signals etc.
1129 * Then wait for child to exit.
1130 */
1131 SETSIG(sa, SIGINT, SIG_IGN, SA_RESTART);
1132 SETSIG(sa, SIGTSTP, SIG_IGN, SA_RESTART);
1133 SETSIG(sa, SIGQUIT, SIG_IGN, SA_RESTART);
1134
1135 while ((rc = waitpid(pid, &st, 0)) != pid)
1136 if (rc < 0 && errno == ECHILD)
1137 break;
1138
1139 /*
1140 * Small optimization. See if stealing
1141 * controlling tty back is needed.
1142 */
1143 pgrp = tcgetpgrp(f);
1144 if (pgrp != getpid())
1145 exit(0);
1146
1147 /*
1148 * Steal controlling tty away. We do
1149 * this with a temporary process.
1150 */
1151 if ((pid = fork()) < 0) {
1152 initlog(L_VB, "cannot fork: %s",
1153 strerror(errno));
1154 exit(1);
1155 }
1156 if (pid == 0) {
1157 setsid();
1158 (void)ioctl(f, TIOCSCTTY, 1);
1159 exit(0);
1160 }
1161 while((rc = waitpid(pid, &st, 0)) != pid)
1162 if (rc < 0 && errno == ECHILD)
1163 break;
1164 exit(0);
1165 }
1166
1167 /* Set ioctl settings to default ones */
1168 console_stty();
1169
1170 } else {
1171 setsid();
1172 if ((f = console_open(O_RDWR|O_NOCTTY)) < 0) {
1173 initlog(L_VB, "open(%s): %s", console_dev,
1174 strerror(errno));
1175 f = open("/dev/null", O_RDWR);
1176 }
1177 dup(f);
1178 dup(f);
1179 }
1180
23d97522
DWF
1181 /*
1182 * Update utmp/wtmp file prior to starting
1183 * any child. This MUST be done right here in
1184 * the child process in order to prevent a race
1185 * condition that occurs when the child
1186 * process' time slice executes before the
1187 * parent (can and does happen in a uniprocessor
1188 * environment). If the child is a getty and
1189 * the race condition happens, then init's utmp
1190 * update will happen AFTER the getty runs
1191 * and expects utmp to be updated already!
1192 *
1193 * Do NOT log if process field starts with '+'
1194 * FIXME: that's for compatibility with *very*
1195 * old getties - probably it can be taken out.
1196 */
1197 if (ch->process[0] != '+')
1198 write_utmp_wtmp("", ch->id, getpid(), INIT_PROCESS, "");
1199
a74aeac6
PR
1200 /* Reset all the signals, set up environment */
1201 for(f = 1; f < NSIG; f++) SETSIG(sa, f, SIG_DFL, SA_RESTART);
1202 environ = init_buildenv(1);
1203
1204 /*
1205 * Execute prog. In case of ENOEXEC try again
1206 * as a shell script.
1207 */
1208 execvp(args[1], args + 1);
1209 if (errno == ENOEXEC) {
1210 args[1] = SHELL;
1211 args[2] = "-c";
1212 strcpy(buf, "exec ");
1213 strncat(buf, proc, sizeof(buf) - strlen(buf) - 1);
1214 args[3] = buf;
1215 args[4] = NULL;
1216 execvp(args[1], args + 1);
1217 }
1218 initlog(L_VB, "cannot execute \"%s\"", args[1]);
23d97522
DWF
1219
1220 if (ch->process[0] != '+')
1221 write_utmp_wtmp("", ch->id, getpid(), DEAD_PROCESS, NULL);
a74aeac6
PR
1222 exit(1);
1223 }
1224 *res = pid;
1225 sigprocmask(SIG_SETMASK, &omask, NULL);
1226
1227 INITDBG(L_VB, "Started id %s (pid %d)", ch->id, pid);
1228
1229 if (pid == -1) {
1230 initlog(L_VB, "cannot fork, retry..");
1231 do_sleep(5);
1232 continue;
1233 }
1234 return(pid);
1235 }
1236}
1237
1238/*
1239 * Start a child running!
1240 */
5f959b29 1241static
a74aeac6
PR
1242void startup(CHILD *ch)
1243{
1244 /*
1245 * See if it's disabled
1246 */
1247 if (ch->flags & FAILING) return;
1248
1249 switch(ch->action) {
1250
1251 case SYSINIT:
1252 case BOOTWAIT:
1253 case WAIT:
1254 case POWERWAIT:
1255 case POWERFAILNOW:
1256 case POWEROKWAIT:
1257 case CTRLALTDEL:
1258 if (!(ch->flags & XECUTED)) ch->flags |= WAITING;
13c69d3f 1259 /* Fall through */
a74aeac6
PR
1260 case KBREQUEST:
1261 case BOOT:
1262 case POWERFAIL:
1263 case ONCE:
1264 if (ch->flags & XECUTED) break;
1265 case ONDEMAND:
1266 case RESPAWN:
1267 ch->flags |= RUNNING;
90c78fcc 1268 (void)spawn(ch, &(ch->pid));
a74aeac6
PR
1269 break;
1270 }
1271}
1272
1273
1274/*
1275 * Read the inittab file.
1276 */
5f959b29 1277static
a74aeac6
PR
1278void read_inittab(void)
1279{
1280 FILE *fp; /* The INITTAB file */
1281 CHILD *ch, *old, *i; /* Pointers to CHILD structure */
1282 CHILD *head = NULL; /* Head of linked list */
1283#ifdef INITLVL
1284 struct stat st; /* To stat INITLVL */
1285#endif
1286 sigset_t nmask, omask; /* For blocking SIGCHLD. */
1287 char buf[256]; /* Line buffer */
1288 char err[64]; /* Error message. */
1289 char *id, *rlevel,
1290 *action, *process; /* Fields of a line */
1291 char *p;
1292 int lineNo = 0; /* Line number in INITTAB file */
1293 int actionNo; /* Decoded action field */
1294 int f; /* Counter */
1295 int round; /* round 0 for SIGTERM, 1 for SIGKILL */
1296 int foundOne = 0; /* No killing no sleep */
1297 int talk; /* Talk to the user */
1298 int done = 0; /* Ready yet? */
1299
1300#if DEBUG
1301 if (newFamily != NULL) {
1302 INITDBG(L_VB, "PANIC newFamily != NULL");
1303 exit(1);
1304 }
1305 INITDBG(L_VB, "Reading inittab");
1306#endif
1307
1308 /*
b55908a4 1309 * Open INITTAB and read line by line.
a74aeac6
PR
1310 */
1311 if ((fp = fopen(INITTAB, "r")) == NULL)
1312 initlog(L_VB, "No inittab file found");
1313
1314 while(!done) {
1315 /*
1316 * Add single user shell entry at the end.
1317 */
1318 if (fp == NULL || fgets(buf, sizeof(buf), fp) == NULL) {
1319 done = 1;
1320 /*
1321 * See if we have a single user entry.
1322 */
1323 for(old = newFamily; old; old = old->next)
1324 if (strpbrk(old->rlevel, "S")) break;
1325 if (old == NULL)
1326 snprintf(buf, sizeof(buf), "~~:S:wait:%s\n", SULOGIN);
1327 else
1328 continue;
1329 }
1330 lineNo++;
1331 /*
1332 * Skip comments and empty lines
1333 */
1334 for(p = buf; *p == ' ' || *p == '\t'; p++)
1335 ;
1336 if (*p == '#' || *p == '\n') continue;
1337
1338 /*
1339 * Decode the fields
1340 */
1341 id = strsep(&p, ":");
1342 rlevel = strsep(&p, ":");
1343 action = strsep(&p, ":");
1344 process = strsep(&p, "\n");
1345
1346 /*
1347 * Check if syntax is OK. Be very verbose here, to
1348 * avoid newbie postings on comp.os.linux.setup :)
1349 */
1350 err[0] = 0;
1351 if (!id || !*id) strcpy(err, "missing id field");
1352 if (!rlevel) strcpy(err, "missing runlevel field");
1353 if (!process) strcpy(err, "missing process field");
1354 if (!action || !*action)
1355 strcpy(err, "missing action field");
1356 if (id && strlen(id) > sizeof(utproto.ut_id))
1357 sprintf(err, "id field too long (max %d characters)",
1358 (int)sizeof(utproto.ut_id));
1359 if (rlevel && strlen(rlevel) > 11)
1360 strcpy(err, "rlevel field too long (max 11 characters)");
1361 if (process && strlen(process) > 127)
1362 strcpy(err, "process field too long");
1363 if (action && strlen(action) > 32)
1364 strcpy(err, "action field too long");
1365 if (err[0] != 0) {
1366 initlog(L_VB, "%s[%d]: %s", INITTAB, lineNo, err);
1367 INITDBG(L_VB, "%s:%s:%s:%s", id, rlevel, action, process);
1368 continue;
1369 }
1370
1371 /*
1372 * Decode the "action" field
1373 */
1374 actionNo = -1;
1375 for(f = 0; actions[f].name; f++)
1376 if (strcasecmp(action, actions[f].name) == 0) {
1377 actionNo = actions[f].act;
1378 break;
1379 }
1380 if (actionNo == -1) {
1381 initlog(L_VB, "%s[%d]: %s: unknown action field",
1382 INITTAB, lineNo, action);
1383 continue;
1384 }
1385
1386 /*
1387 * See if the id field is unique
1388 */
1389 for(old = newFamily; old; old = old->next) {
1390 if(strcmp(old->id, id) == 0 && strcmp(id, "~~")) {
1391 initlog(L_VB, "%s[%d]: duplicate ID field \"%s\"",
1392 INITTAB, lineNo, id);
1393 break;
1394 }
1395 }
1396 if (old) continue;
1397
1398 /*
1399 * Allocate a CHILD structure
1400 */
1401 ch = imalloc(sizeof(CHILD));
1402
1403 /*
1404 * And fill it in.
1405 */
1406 ch->action = actionNo;
1407 strncpy(ch->id, id, sizeof(utproto.ut_id) + 1); /* Hack for different libs. */
1408 strncpy(ch->process, process, sizeof(ch->process) - 1);
1409 if (rlevel[0]) {
192c4567 1410 for(f = 0; f < (int)sizeof(rlevel) - 1 && rlevel[f]; f++) {
a74aeac6
PR
1411 ch->rlevel[f] = rlevel[f];
1412 if (ch->rlevel[f] == 's') ch->rlevel[f] = 'S';
1413 }
1414 strncpy(ch->rlevel, rlevel, sizeof(ch->rlevel) - 1);
1415 } else {
1416 strcpy(ch->rlevel, "0123456789");
1417 if (ISPOWER(ch->action))
1418 strcpy(ch->rlevel, "S0123456789");
1419 }
1420 /*
1421 * We have the fake runlevel '#' for SYSINIT and
1422 * '*' for BOOT and BOOTWAIT.
1423 */
1424 if (ch->action == SYSINIT) strcpy(ch->rlevel, "#");
1425 if (ch->action == BOOT || ch->action == BOOTWAIT)
1426 strcpy(ch->rlevel, "*");
1427
1428 /*
1429 * Now add it to the linked list. Special for powerfail.
1430 */
1431 if (ISPOWER(ch->action)) {
1432
1433 /*
1434 * Disable by default
1435 */
1436 ch->flags |= XECUTED;
1437
1438 /*
1439 * Tricky: insert at the front of the list..
1440 */
1441 old = NULL;
1442 for(i = newFamily; i; i = i->next) {
1443 if (!ISPOWER(i->action)) break;
1444 old = i;
1445 }
1446 /*
1447 * Now add after entry "old"
1448 */
1449 if (old) {
1450 ch->next = i;
1451 old->next = ch;
1452 if (i == NULL) head = ch;
1453 } else {
1454 ch->next = newFamily;
1455 newFamily = ch;
1456 if (ch->next == NULL) head = ch;
1457 }
1458 } else {
1459 /*
1460 * Just add at end of the list
1461 */
1462 if (ch->action == KBREQUEST) ch->flags |= XECUTED;
1463 ch->next = NULL;
1464 if (head)
1465 head->next = ch;
1466 else
1467 newFamily = ch;
1468 head = ch;
1469 }
1470
1471 /*
1472 * Walk through the old list comparing id fields
1473 */
1474 for(old = family; old; old = old->next)
1475 if (strcmp(old->id, ch->id) == 0) {
1476 old->new = ch;
1477 break;
1478 }
1479 }
1480 /*
1481 * We're done.
1482 */
1483 if (fp) fclose(fp);
1484
1485 /*
1486 * Loop through the list of children, and see if they need to
1487 * be killed.
1488 */
1489
1490 INITDBG(L_VB, "Checking for children to kill");
1491 for(round = 0; round < 2; round++) {
1492 talk = 1;
1493 for(ch = family; ch; ch = ch->next) {
1494 ch->flags &= ~KILLME;
1495
1496 /*
1497 * Is this line deleted?
1498 */
1499 if (ch->new == NULL) ch->flags |= KILLME;
1500
1501 /*
1502 * If the entry has changed, kill it anyway. Note that
1503 * we do not check ch->process, only the "action" field.
1504 * This way, you can turn an entry "off" immediately, but
1505 * changes in the command line will only become effective
1506 * after the running version has exited.
1507 */
1508 if (ch->new && ch->action != ch->new->action) ch->flags |= KILLME;
1509
1510 /*
1511 * Only BOOT processes may live in all levels
1512 */
1513 if (ch->action != BOOT &&
1514 strchr(ch->rlevel, runlevel) == NULL) {
1515 /*
1516 * Ondemand procedures live always,
1517 * except in single user
1518 */
1519 if (runlevel == 'S' || !(ch->flags & DEMAND))
1520 ch->flags |= KILLME;
1521 }
1522
1523 /*
1524 * Now, if this process may live note so in the new list
1525 */
1526 if ((ch->flags & KILLME) == 0) {
1527 ch->new->flags = ch->flags;
1528 ch->new->pid = ch->pid;
1529 ch->new->exstat = ch->exstat;
1530 continue;
1531 }
1532
1533
1534 /*
1535 * Is this process still around?
1536 */
1537 if ((ch->flags & RUNNING) == 0) {
1538 ch->flags &= ~KILLME;
1539 continue;
1540 }
1541 INITDBG(L_VB, "Killing \"%s\"", ch->process);
1542 switch(round) {
1543 case 0: /* Send TERM signal */
1544 if (talk)
1545 initlog(L_CO,
82bb2fc6 1546 "Sending processes configured via /etc/inittab the TERM signal");
a74aeac6
PR
1547 kill(-(ch->pid), SIGTERM);
1548 foundOne = 1;
1549 break;
1550 case 1: /* Send KILL signal and collect status */
1551 if (talk)
1552 initlog(L_CO,
82bb2fc6 1553 "Sending processes configured via /etc/inittab the KILL signal");
a74aeac6
PR
1554 kill(-(ch->pid), SIGKILL);
1555 break;
1556 }
1557 talk = 0;
1558
1559 }
1560 /*
1561 * See if we have to wait 5 seconds
1562 */
1563 if (foundOne && round == 0) {
1564 /*
1565 * Yup, but check every second if we still have children.
1566 */
1567 for(f = 0; f < sltime; f++) {
1568 for(ch = family; ch; ch = ch->next) {
1569 if (!(ch->flags & KILLME)) continue;
1570 if ((ch->flags & RUNNING) && !(ch->flags & ZOMBIE))
1571 break;
1572 }
1573 if (ch == NULL) {
1574 /*
1575 * No running children, skip SIGKILL
1576 */
1577 round = 1;
1578 foundOne = 0; /* Skip the sleep below. */
1579 break;
1580 }
1581 do_sleep(1);
1582 }
1583 }
1584 }
1585
1586 /*
1587 * Now give all processes the chance to die and collect exit statuses.
1588 */
1589 if (foundOne) do_sleep(1);
1590 for(ch = family; ch; ch = ch->next)
1591 if (ch->flags & KILLME) {
1592 if (!(ch->flags & ZOMBIE))
1593 initlog(L_CO, "Pid %d [id %s] seems to hang", ch->pid,
1594 ch->id);
1595 else {
1596 INITDBG(L_VB, "Updating utmp for pid %d [id %s]",
1597 ch->pid, ch->id);
1598 ch->flags &= ~RUNNING;
1599 if (ch->process[0] != '+')
1600 write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
1601 }
1602 }
1603
1604 /*
1605 * Both rounds done; clean up the list.
1606 */
1607 sigemptyset(&nmask);
1608 sigaddset(&nmask, SIGCHLD);
1609 sigprocmask(SIG_BLOCK, &nmask, &omask);
1610 for(ch = family; ch; ch = old) {
1611 old = ch->next;
1612 free(ch);
1613 }
1614 family = newFamily;
1615 for(ch = family; ch; ch = ch->next) ch->new = NULL;
1616 newFamily = NULL;
1617 sigprocmask(SIG_SETMASK, &omask, NULL);
1618
1619#ifdef INITLVL
1620 /*
1621 * Dispose of INITLVL file.
1622 */
1623 if (lstat(INITLVL, &st) >= 0 && S_ISLNK(st.st_mode)) {
1624 /*
1625 * INITLVL is a symbolic link, so just truncate the file.
1626 */
1627 close(open(INITLVL, O_WRONLY|O_TRUNC));
1628 } else {
1629 /*
1630 * Delete INITLVL file.
1631 */
1632 unlink(INITLVL);
1633 }
1634#endif
1635#ifdef INITLVL2
1636 /*
1637 * Dispose of INITLVL2 file.
1638 */
1639 if (lstat(INITLVL2, &st) >= 0 && S_ISLNK(st.st_mode)) {
1640 /*
1641 * INITLVL2 is a symbolic link, so just truncate the file.
1642 */
1643 close(open(INITLVL2, O_WRONLY|O_TRUNC));
1644 } else {
1645 /*
1646 * Delete INITLVL2 file.
1647 */
1648 unlink(INITLVL2);
1649 }
1650#endif
1651}
1652
1653/*
1654 * Walk through the family list and start up children.
1655 * The entries that do not belong here at all are removed
1656 * from the list.
1657 */
5f959b29 1658static
a74aeac6
PR
1659void start_if_needed(void)
1660{
1661 CHILD *ch; /* Pointer to child */
1662 int delete; /* Delete this entry from list? */
1663
1664 INITDBG(L_VB, "Checking for children to start");
1665
1666 for(ch = family; ch; ch = ch->next) {
1667
1668#if DEBUG
1669 if (ch->rlevel[0] == 'C') {
1670 INITDBG(L_VB, "%s: flags %d", ch->process, ch->flags);
1671 }
1672#endif
1673
1674 /* Are we waiting for this process? Then quit here. */
1675 if (ch->flags & WAITING) break;
1676
1677 /* Already running? OK, don't touch it */
1678 if (ch->flags & RUNNING) continue;
1679
1680 /* See if we have to start it up */
1681 delete = 1;
1682 if (strchr(ch->rlevel, runlevel) ||
1683 ((ch->flags & DEMAND) && !strchr("#*Ss", runlevel))) {
1684 startup(ch);
1685 delete = 0;
1686 }
1687
1688 if (delete) {
1689 /* FIXME: is this OK? */
1690 ch->flags &= ~(RUNNING|WAITING);
1691 if (!ISPOWER(ch->action) && ch->action != KBREQUEST)
1692 ch->flags &= ~XECUTED;
1693 ch->pid = 0;
1694 } else
1695 /* Do we have to wait for this process? */
1696 if (ch->flags & WAITING) break;
1697 }
1698 /* Done. */
1699}
1700
1701/*
1702 * Ask the user on the console for a runlevel
1703 */
5f959b29 1704static
a74aeac6
PR
1705int ask_runlevel(void)
1706{
1707 const char prompt[] = "\nEnter runlevel: ";
1708 char buf[8];
1709 int lvl = -1;
1710 int fd;
1711
1712 console_stty();
1713 fd = console_open(O_RDWR|O_NOCTTY);
1714
1715 if (fd < 0) return('S');
1716
1717 while(!strchr("0123456789S", lvl)) {
c5ae561e
DWF
1718 safe_write(fd, prompt, sizeof(prompt) - 1);
1719 if (read(fd, buf, sizeof(buf)) <= 0)
1720 buf[0] = 0;
a74aeac6
PR
1721 if (buf[0] != 0 && (buf[1] == '\r' || buf[1] == '\n'))
1722 lvl = buf[0];
1723 if (islower(lvl)) lvl = toupper(lvl);
1724 }
1725 close(fd);
1726 return lvl;
1727}
1728
1729/*
1730 * Search the INITTAB file for the 'initdefault' field, with the default
1731 * runlevel. If this fails, ask the user to supply a runlevel.
1732 */
5f959b29 1733static
a74aeac6
PR
1734int get_init_default(void)
1735{
1736 CHILD *ch;
1737 int lvl = -1;
1738 char *p;
1739
1740 /*
1741 * Look for initdefault.
1742 */
1743 for(ch = family; ch; ch = ch->next)
1744 if (ch->action == INITDEFAULT) {
1745 p = ch->rlevel;
1746 while(*p) {
1747 if (*p > lvl) lvl = *p;
1748 p++;
1749 }
1750 break;
1751 }
1752 /*
1753 * See if level is valid
1754 */
1755 if (lvl > 0) {
1756 if (islower(lvl)) lvl = toupper(lvl);
1757 if (strchr("0123456789S", lvl) == NULL) {
1758 initlog(L_VB,
1759 "Initdefault level '%c' is invalid", lvl);
1760 lvl = 0;
1761 }
1762 }
1763 /*
1764 * Ask for runlevel on console if needed.
1765 */
1766 if (lvl <= 0) lvl = ask_runlevel();
1767
1768 /*
1769 * Log the fact that we have a runlevel now.
1770 */
1771 return lvl;
1772}
1773
1774
1775/*
1776 * We got signaled.
1777 *
1778 * Do actions for the new level. If we are compatible with
1779 * the "old" INITLVL and arg == 0, try to read the new
1780 * runlevel from that file first.
1781 */
5f959b29 1782static
a74aeac6
PR
1783int read_level(int arg)
1784{
1785 CHILD *ch; /* Walk through list */
1786 unsigned char foo = 'X'; /* Contents of INITLVL */
1787 int ok = 1;
1788#ifdef INITLVL
1789 FILE *fp;
1790 struct stat stt;
1791 int st;
1792#endif
1793
1794 if (arg) foo = arg;
1795
1796#ifdef INITLVL
1797 ok = 0;
1798
1799 if (arg == 0) {
1800 fp = NULL;
1801 if (stat(INITLVL, &stt) != 0 || stt.st_size != 0L)
1802 fp = fopen(INITLVL, "r");
1803#ifdef INITLVL2
1804 if (fp == NULL &&
1805 (stat(INITLVL2, &stt) != 0 || stt.st_size != 0L))
1806 fp = fopen(INITLVL2, "r");
1807#endif
1808 if (fp == NULL) {
1809 /* INITLVL file empty or not there - act as 'init q' */
1810 initlog(L_SY, "Re-reading inittab");
1811 return(runlevel);
1812 }
1813 ok = fscanf(fp, "%c %d", &foo, &st);
1814 fclose(fp);
1815 } else {
1816 /* We go to the new runlevel passed as an argument. */
1817 foo = arg;
1818 ok = 1;
1819 }
1820 if (ok == 2) sltime = st;
1821
1822#endif /* INITLVL */
1823
1824 if (islower(foo)) foo = toupper(foo);
1825 if (ok < 1 || ok > 2 || strchr("QS0123456789ABCU", foo) == NULL) {
1826 initlog(L_VB, "bad runlevel: %c", foo);
1827 return runlevel;
1828 }
1829
1830 /* Log this action */
1831 switch(foo) {
1832 case 'S':
1833 initlog(L_VB, "Going single user");
1834 break;
1835 case 'Q':
1836 initlog(L_SY, "Re-reading inittab");
1837 break;
1838 case 'A':
1839 case 'B':
1840 case 'C':
1841 initlog(L_SY,
1842 "Activating demand-procedures for '%c'", foo);
1843 break;
1844 case 'U':
1845 initlog(L_SY, "Trying to re-exec init");
1846 return 'U';
1847 default:
1848 initlog(L_VB, "Switching to runlevel: %c", foo);
1849 }
1850
5f959b29
DWF
1851 if (foo == 'Q') {
1852#if defined(SIGINT_ONLYONCE) && (SIGINT_ONLYONCE == 1)
1853 /* Re-enable signal from keyboard */
1854 struct sigaction sa;
1855 SETSIG(sa, SIGINT, signal_handler, 0);
1856#endif
1857 return runlevel;
1858 }
a74aeac6
PR
1859
1860 /* Check if this is a runlevel a, b or c */
1861 if (strchr("ABC", foo)) {
1862 if (runlevel == 'S') return(runlevel);
1863
1864 /* Read inittab again first! */
1865 read_inittab();
1866
1867 /* Mark those special tasks */
1868 for(ch = family; ch; ch = ch->next)
1869 if (strchr(ch->rlevel, foo) != NULL ||
1870 strchr(ch->rlevel, tolower(foo)) != NULL) {
1871 ch->flags |= DEMAND;
1872 ch->flags &= ~XECUTED;
1873 INITDBG(L_VB,
1874 "Marking (%s) as ondemand, flags %d",
1875 ch->id, ch->flags);
1876 }
1877 return runlevel;
1878 }
1879
1880 /* Store both the old and the new runlevel. */
63a2c981
DWF
1881 wrote_utmp_rlevel = 0;
1882 wrote_wtmp_rlevel = 0;
a74aeac6
PR
1883 write_utmp_wtmp("runlevel", "~~", foo + 256*runlevel, RUN_LVL, "~");
1884 thislevel = foo;
1885 prevlevel = runlevel;
1886 return foo;
1887}
1888
1889
1890/*
1891 * This procedure is called after every signal (SIGHUP, SIGALRM..)
1892 *
1893 * Only clear the 'failing' flag if the process is sleeping
1894 * longer than 5 minutes, or inittab was read again due
1895 * to user interaction.
1896 */
5f959b29 1897static
a74aeac6
PR
1898void fail_check(void)
1899{
1900 CHILD *ch; /* Pointer to child structure */
1901 time_t t; /* System time */
1902 time_t next_alarm = 0; /* When to set next alarm */
1903
1904 time(&t);
1905
1906 for(ch = family; ch; ch = ch->next) {
1907
1908 if (ch->flags & FAILING) {
1909 /* Can we free this sucker? */
1910 if (ch->tm + SLEEPTIME < t) {
1911 ch->flags &= ~FAILING;
1912 ch->count = 0;
1913 ch->tm = 0;
1914 } else {
1915 /* No, we'll look again later */
1916 if (next_alarm == 0 ||
1917 ch->tm + SLEEPTIME > next_alarm)
1918 next_alarm = ch->tm + SLEEPTIME;
1919 }
1920 }
1921 }
1922 if (next_alarm) {
1923 next_alarm -= t;
1924 if (next_alarm < 1) next_alarm = 1;
1925 alarm(next_alarm);
1926 }
1927}
1928
1929/* Set all 'Fail' timers to 0 */
5f959b29 1930static
a74aeac6
PR
1931void fail_cancel(void)
1932{
1933 CHILD *ch;
1934
1935 for(ch = family; ch; ch = ch->next) {
1936 ch->count = 0;
1937 ch->tm = 0;
1938 ch->flags &= ~FAILING;
1939 }
1940}
1941
1942/*
1943 * Start up powerfail entries.
1944 */
5f959b29 1945static
a74aeac6
PR
1946void do_power_fail(int pwrstat)
1947{
1948 CHILD *ch;
1949
1950 /*
1951 * Tell powerwait & powerfail entries to start up
1952 */
1953 for (ch = family; ch; ch = ch->next) {
1954 if (pwrstat == 'O') {
1955 /*
1956 * The power is OK again.
1957 */
1958 if (ch->action == POWEROKWAIT)
1959 ch->flags &= ~XECUTED;
1960 } else if (pwrstat == 'L') {
1961 /*
1962 * Low battery, shut down now.
1963 */
1964 if (ch->action == POWERFAILNOW)
1965 ch->flags &= ~XECUTED;
1966 } else {
1967 /*
1968 * Power is failing, shutdown imminent
1969 */
1970 if (ch->action == POWERFAIL || ch->action == POWERWAIT)
1971 ch->flags &= ~XECUTED;
1972 }
1973 }
1974}
1975
1976/*
1977 * Check for state-pipe presence
1978 */
5f959b29 1979static
a74aeac6
PR
1980int check_pipe(int fd)
1981{
1982 struct timeval t;
1983 fd_set s;
1984 char signature[8];
1985
1986 FD_ZERO(&s);
1987 FD_SET(fd, &s);
1988 t.tv_sec = t.tv_usec = 0;
1989
1990 if (select(fd+1, &s, NULL, NULL, &t) != 1)
1991 return 0;
1992 if (read(fd, signature, 8) != 8)
1993 return 0;
1994 return strncmp(Signature, signature, 8) == 0;
1995}
1996
1997/*
1998 * Make a state-pipe.
1999 */
5f959b29 2000static
a74aeac6
PR
2001int make_pipe(int fd)
2002{
2003 int fds[2];
2004
c5ae561e
DWF
2005 if (pipe(fds)) {
2006 initlog(L_VB, "pipe: %m");
2007 return -1;
2008 }
a74aeac6
PR
2009 dup2(fds[0], fd);
2010 close(fds[0]);
2011 fcntl(fds[1], F_SETFD, 1);
2012 fcntl(fd, F_SETFD, 0);
c5ae561e 2013 safe_write(fds[1], Signature, 8);
a74aeac6
PR
2014
2015 return fds[1];
2016}
2017
2018/*
2019 * Attempt to re-exec.
2020 */
5f959b29 2021static
a74aeac6
PR
2022void re_exec(void)
2023{
2024 CHILD *ch;
2025 sigset_t mask, oldset;
2026 pid_t pid;
2027 char **env;
2028 int fd;
2029
2030 if (strchr("S0123456",runlevel) == NULL)
2031 return;
2032
2033 /*
2034 * Reset the alarm, and block all signals.
2035 */
2036 alarm(0);
2037 sigfillset(&mask);
2038 sigprocmask(SIG_BLOCK, &mask, &oldset);
2039
2040 /*
2041 * construct a pipe fd --> STATE_PIPE and write a signature
2042 */
c5ae561e
DWF
2043 if ((fd = make_pipe(STATE_PIPE)) < 0) {
2044 sigprocmask(SIG_SETMASK, &oldset, NULL);
2045 initlog(L_CO, "Attempt to re-exec failed");
2046 }
a74aeac6
PR
2047
2048 /*
2049 * It's a backup day today, so I'm pissed off. Being a BOFH, however,
2050 * does have it's advantages...
2051 */
2052 fail_cancel();
2053 close(pipe_fd);
2054 pipe_fd = -1;
2055 DELSET(got_signals, SIGCHLD);
2056 DELSET(got_signals, SIGHUP);
2057 DELSET(got_signals, SIGUSR1);
2058
2059 /*
2060 * That should be cleaned.
2061 */
2062 for(ch = family; ch; ch = ch->next)
2063 if (ch->flags & ZOMBIE) {
2064 INITDBG(L_VB, "Child died, PID= %d", ch->pid);
2065 ch->flags &= ~(RUNNING|ZOMBIE|WAITING);
2066 if (ch->process[0] != '+')
2067 write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
2068 }
2069
2070 if ((pid = fork()) == 0) {
2071 /*
2072 * Child sends state information to the parent.
2073 */
2074 send_state(fd);
2075 exit(0);
2076 }
2077
2078 /*
2079 * The existing init process execs a new init binary.
2080 */
2081 env = init_buildenv(0);
2082 execle(myname, myname, "--init", NULL, env);
2083
2084 /*
2085 * We shouldn't be here, something failed.
2086 * Bitch, close the state pipe, unblock signals and return.
2087 */
c5ae561e 2088 init_freeenv(env);
a74aeac6
PR
2089 close(fd);
2090 close(STATE_PIPE);
2091 sigprocmask(SIG_SETMASK, &oldset, NULL);
a74aeac6
PR
2092 initlog(L_CO, "Attempt to re-exec failed");
2093}
2094
63a2c981
DWF
2095/*
2096 * Redo utmp/wtmp entries if required or requested
2097 * Check for written records and size of utmp
2098 */
2099static
2100void redo_utmp_wtmp(void)
2101{
2102 struct stat ustat;
2103 const int ret = stat(UTMP_FILE, &ustat);
2104
2105 if ((ret < 0) || (ustat.st_size == 0))
2106 wrote_utmp_rlevel = wrote_utmp_reboot = 0;
2107
2108 if ((wrote_wtmp_reboot == 0) || (wrote_utmp_reboot == 0))
2109 write_utmp_wtmp("reboot", "~~", 0, BOOT_TIME, "~");
2110
2111 if ((wrote_wtmp_rlevel == 0) || (wrote_wtmp_rlevel == 0))
2112 write_utmp_wtmp("runlevel", "~~", thislevel + 256 * prevlevel, RUN_LVL, "~");
2113}
a74aeac6
PR
2114
2115/*
2116 * We got a change runlevel request through the
2117 * init.fifo. Process it.
2118 */
5f959b29 2119static
a74aeac6
PR
2120void fifo_new_level(int level)
2121{
2122#if CHANGE_WAIT
2123 CHILD *ch;
2124#endif
2125 int oldlevel;
2126
2127 if (level == runlevel) return;
2128
2129#if CHANGE_WAIT
2130 /* Are we waiting for a child? */
2131 for(ch = family; ch; ch = ch->next)
2132 if (ch->flags & WAITING) break;
2133 if (ch == NULL)
2134#endif
2135 {
2136 /* We need to go into a new runlevel */
2137 oldlevel = runlevel;
2138 runlevel = read_level(level);
2139 if (runlevel == 'U') {
2140 runlevel = oldlevel;
2141 re_exec();
2142 } else {
2143 if (oldlevel != 'S' && runlevel == 'S') console_stty();
2144 if (runlevel == '6' || runlevel == '0' ||
2145 runlevel == '1') console_stty();
63a2c981 2146 if (runlevel > '1' && runlevel < '6') redo_utmp_wtmp();
a74aeac6
PR
2147 read_inittab();
2148 fail_cancel();
5f959b29 2149 setproctitle("init [%c]", (int)runlevel);
a74aeac6
PR
2150 }
2151 }
2152}
2153
2154
2155/*
2156 * Set/unset environment variables. The variables are
2157 * encoded as KEY=VAL\0KEY=VAL\0\0. With "=VAL" it means
2158 * setenv, without it means unsetenv.
2159 */
5f959b29 2160static
a74aeac6
PR
2161void initcmd_setenv(char *data, int size)
2162{
23de6d4a
DWF
2163 char *env, *p, *e;
2164 size_t sz;
2165 int i, eq;
a74aeac6
PR
2166
2167 e = data + size;
2168
2169 while (*data && data < e) {
a74aeac6 2170 for (p = data; *p && p < e; p++)
23de6d4a 2171 ;
a74aeac6
PR
2172 if (*p) break;
2173 env = data;
2174 data = ++p;
2175
a74aeac6
PR
2176 /*
2177 * We only allow INIT_* to be set.
2178 */
2179 if (strncmp(env, "INIT_", 5) != 0)
2180 continue;
2181
23de6d4a
DWF
2182 sz = strcspn(env, "=");
2183 eq = (env[sz] == '=');
2184
2185 /*initlog(L_SY, "init_setenv: %s, %d, %d", env, eq, sz);*/
2186
a74aeac6
PR
2187 /* Free existing vars. */
2188 for (i = 0; i < NR_EXTRA_ENV; i++) {
23de6d4a
DWF
2189 if (extra_env[i] == NULL)
2190 continue;
2191 if (sz != strcspn(extra_env[i], "="))
2192 continue;
2193 if (strncmp(extra_env[i], env, sz) == 0) {
a74aeac6
PR
2194 free(extra_env[i]);
2195 extra_env[i] = NULL;
2196 }
2197 }
2198
23de6d4a
DWF
2199 if (eq == 0)
2200 continue;
2201
a74aeac6 2202 /* Set new vars if needed. */
a74aeac6
PR
2203 for (i = 0; i < NR_EXTRA_ENV; i++) {
2204 if (extra_env[i] == NULL) {
2205 extra_env[i] = istrdup(env);
2206 break;
2207 }
2208 }
2209 }
2210}
2211
2212
2213/*
2214 * Read from the init FIFO. Processes like telnetd and rlogind can
2215 * ask us to create login processes on their behalf.
2216 *
2217 * FIXME: this needs to be finished. NOT that it is buggy, but we need
2218 * to add the telnetd/rlogind stuff so people can start using it.
2219 * Maybe move to using an AF_UNIX socket so we can use
2220 * the 2.2 kernel credential stuff to see who we're talking to.
2221 *
2222 */
5f959b29 2223static
a74aeac6
PR
2224void check_init_fifo(void)
2225{
2226 struct init_request request;
2227 struct timeval tv;
2228 struct stat st, st2;
2229 fd_set fds;
2230 int n;
2231 int quit = 0;
2232
2233 /*
2234 * First, try to create /dev/initctl if not present.
2235 */
2236 if (stat(INIT_FIFO, &st2) < 0 && errno == ENOENT)
2237 (void)mkfifo(INIT_FIFO, 0600);
2238
2239 /*
2240 * If /dev/initctl is open, stat the file to see if it
2241 * is still the _same_ inode.
2242 */
2243 if (pipe_fd >= 0) {
2244 fstat(pipe_fd, &st);
2245 if (stat(INIT_FIFO, &st2) < 0 ||
2246 st.st_dev != st2.st_dev ||
2247 st.st_ino != st2.st_ino) {
2248 close(pipe_fd);
2249 pipe_fd = -1;
2250 }
2251 }
2252
2253 /*
2254 * Now finally try to open /dev/initctl
2255 */
2256 if (pipe_fd < 0) {
2257 if ((pipe_fd = open(INIT_FIFO, O_RDWR|O_NONBLOCK)) >= 0) {
2258 fstat(pipe_fd, &st);
2259 if (!S_ISFIFO(st.st_mode)) {
2260 initlog(L_VB, "%s is not a fifo", INIT_FIFO);
2261 close(pipe_fd);
2262 pipe_fd = -1;
2263 }
2264 }
2265 if (pipe_fd >= 0) {
2266 /*
2267 * Don't use fd's 0, 1 or 2.
2268 */
2269 (void) dup2(pipe_fd, PIPE_FD);
2270 close(pipe_fd);
2271 pipe_fd = PIPE_FD;
2272
2273 /*
2274 * Return to caller - we'll be back later.
2275 */
2276 }
2277 }
2278
2279 /* Wait for data to appear, _if_ the pipe was opened. */
2280 if (pipe_fd >= 0) while(!quit) {
2281
2282 /* Do select, return on EINTR. */
2283 FD_ZERO(&fds);
2284 FD_SET(pipe_fd, &fds);
2285 tv.tv_sec = 5;
2286 tv.tv_usec = 0;
2287 n = select(pipe_fd + 1, &fds, NULL, NULL, &tv);
2288 if (n <= 0) {
2289 if (n == 0 || errno == EINTR) return;
2290 continue;
2291 }
2292
2293 /* Read the data, return on EINTR. */
2294 n = read(pipe_fd, &request, sizeof(request));
2295 if (n == 0) {
2296 /*
2297 * End of file. This can't happen under Linux (because
2298 * the pipe is opened O_RDWR - see select() in the
2299 * kernel) but you never know...
2300 */
2301 close(pipe_fd);
2302 pipe_fd = -1;
2303 return;
2304 }
2305 if (n <= 0) {
2306 if (errno == EINTR) return;
2307 initlog(L_VB, "error reading initrequest");
2308 continue;
2309 }
2310
2311 /*
2312 * This is a convenient point to also try to
2313 * find the console device or check if it changed.
2314 */
2315 console_init();
2316
2317 /*
2318 * Process request.
2319 */
2320 if (request.magic != INIT_MAGIC || n != sizeof(request)) {
2321 initlog(L_VB, "got bogus initrequest");
2322 continue;
2323 }
2324 switch(request.cmd) {
2325 case INIT_CMD_RUNLVL:
2326 sltime = request.sleeptime;
2327 fifo_new_level(request.runlevel);
2328 quit = 1;
2329 break;
2330 case INIT_CMD_POWERFAIL:
2331 sltime = request.sleeptime;
2332 do_power_fail('F');
2333 quit = 1;
2334 break;
2335 case INIT_CMD_POWERFAILNOW:
2336 sltime = request.sleeptime;
2337 do_power_fail('L');
2338 quit = 1;
2339 break;
2340 case INIT_CMD_POWEROK:
2341 sltime = request.sleeptime;
2342 do_power_fail('O');
2343 quit = 1;
2344 break;
2345 case INIT_CMD_SETENV:
2346 initcmd_setenv(request.i.data, sizeof(request.i.data));
2347 break;
a74aeac6
PR
2348 default:
2349 initlog(L_VB, "got unimplemented initrequest.");
2350 break;
2351 }
2352 }
2353
2354 /*
2355 * We come here if the pipe couldn't be opened.
2356 */
2357 if (pipe_fd < 0) pause();
2358
2359}
2360
2361
2362/*
2363 * This function is used in the transition
2364 * sysinit (-> single user) boot -> multi-user.
2365 */
5f959b29 2366static
a74aeac6
PR
2367void boot_transitions()
2368{
2369 CHILD *ch;
2370 static int newlevel = 0;
2371 static int warn = 1;
2372 int loglevel;
2373 int oldlevel;
2374
2375 /* Check if there is something to wait for! */
2376 for( ch = family; ch; ch = ch->next )
2377 if ((ch->flags & RUNNING) && ch->action != BOOT) break;
2378
2379 if (ch == NULL) {
2380 /* No processes left in this level, proceed to next level. */
2381 loglevel = -1;
2382 oldlevel = 'N';
2383 switch(runlevel) {
2384 case '#': /* SYSINIT -> BOOT */
2385 INITDBG(L_VB, "SYSINIT -> BOOT");
2386
2387 /* Write a boot record. */
2388 wrote_utmp_reboot = 0;
2389 wrote_wtmp_reboot = 0;
2390 write_utmp_wtmp("reboot", "~~", 0, BOOT_TIME, "~");
2391
2392 /* Get our run level */
2393 newlevel = dfl_level ? dfl_level : get_init_default();
2394 if (newlevel == 'S') {
2395 runlevel = newlevel;
2396 /* Not really 'S' but show anyway. */
2397 setproctitle("init [S]");
2398 } else
2399 runlevel = '*';
2400 break;
2401 case '*': /* BOOT -> NORMAL */
2402 INITDBG(L_VB, "BOOT -> NORMAL");
2403 if (runlevel != newlevel)
2404 loglevel = newlevel;
2405 runlevel = newlevel;
2406 did_boot = 1;
2407 warn = 1;
2408 break;
2409 case 'S': /* Ended SU mode */
2410 case 's':
2411 INITDBG(L_VB, "END SU MODE");
2412 newlevel = get_init_default();
2413 if (!did_boot && newlevel != 'S')
2414 runlevel = '*';
2415 else {
2416 if (runlevel != newlevel)
2417 loglevel = newlevel;
2418 runlevel = newlevel;
2419 oldlevel = 'S';
2420 }
2421 warn = 1;
2422 for(ch = family; ch; ch = ch->next)
2423 if (strcmp(ch->rlevel, "S") == 0)
2424 ch->flags &= ~(FAILING|WAITING|XECUTED);
2425 break;
2426 default:
2427 if (warn)
2428 initlog(L_VB,
2429 "no more processes left in this runlevel");
2430 warn = 0;
2431 loglevel = -1;
2432 if (got_signals == 0)
2433 check_init_fifo();
2434 break;
2435 }
2436 if (loglevel > 0) {
2437 initlog(L_VB, "Entering runlevel: %c", runlevel);
63a2c981
DWF
2438 wrote_utmp_rlevel = 0;
2439 wrote_wtmp_rlevel = 0;
a74aeac6
PR
2440 write_utmp_wtmp("runlevel", "~~", runlevel + 256 * oldlevel, RUN_LVL, "~");
2441 thislevel = runlevel;
2442 prevlevel = oldlevel;
5f959b29 2443 setproctitle("init [%c]", (int)runlevel);
a74aeac6
PR
2444 }
2445 }
2446}
2447
2448/*
2449 * Init got hit by a signal. See which signal it is,
2450 * and act accordingly.
2451 */
5f959b29 2452static
a74aeac6
PR
2453void process_signals()
2454{
2455 CHILD *ch;
2456 int pwrstat;
2457 int oldlevel;
2458 int fd;
2459 char c;
2460
2461 if (ISMEMBER(got_signals, SIGPWR)) {
2462 INITDBG(L_VB, "got SIGPWR");
2463 /* See _what_ kind of SIGPWR this is. */
2464 pwrstat = 0;
2465 if ((fd = open(PWRSTAT, O_RDONLY)) >= 0) {
c5ae561e
DWF
2466 if (read(fd, &c, 1) != 1)
2467 c = 0;
a74aeac6
PR
2468 pwrstat = c;
2469 close(fd);
2470 unlink(PWRSTAT);
364b4b0b
PR
2471 } else if ((fd = open(PWRSTAT_OLD, O_RDONLY)) >= 0) {
2472 /* Path changed 2010-03-20. Look for the old path for a while. */
2473 initlog(L_VB, "warning: found obsolete path %s, use %s instead",
2474 PWRSTAT_OLD, PWRSTAT);
c5ae561e
DWF
2475 if (read(fd, &c, 1) != 1)
2476 c = 0;
364b4b0b
PR
2477 pwrstat = c;
2478 close(fd);
2479 unlink(PWRSTAT_OLD);
2480 }
a74aeac6
PR
2481 do_power_fail(pwrstat);
2482 DELSET(got_signals, SIGPWR);
2483 }
2484
2485 if (ISMEMBER(got_signals, SIGINT)) {
5f959b29
DWF
2486#if defined(SIGINT_ONLYONCE) && (SIGINT_ONLYONCE == 1)
2487 /* Ignore any further signal from keyboard */
2488 struct sigaction sa;
2489 SETSIG(sa, SIGINT, SIG_IGN, SA_RESTART);
2490#endif
a74aeac6
PR
2491 INITDBG(L_VB, "got SIGINT");
2492 /* Tell ctrlaltdel entry to start up */
2493 for(ch = family; ch; ch = ch->next)
2494 if (ch->action == CTRLALTDEL)
2495 ch->flags &= ~XECUTED;
2496 DELSET(got_signals, SIGINT);
2497 }
2498
2499 if (ISMEMBER(got_signals, SIGWINCH)) {
2500 INITDBG(L_VB, "got SIGWINCH");
2501 /* Tell kbrequest entry to start up */
2502 for(ch = family; ch; ch = ch->next)
2503 if (ch->action == KBREQUEST)
2504 ch->flags &= ~XECUTED;
2505 DELSET(got_signals, SIGWINCH);
2506 }
2507
2508 if (ISMEMBER(got_signals, SIGALRM)) {
2509 INITDBG(L_VB, "got SIGALRM");
2510 /* The timer went off: check it out */
2511 DELSET(got_signals, SIGALRM);
2512 }
2513
2514 if (ISMEMBER(got_signals, SIGCHLD)) {
2515 INITDBG(L_VB, "got SIGCHLD");
2516 /* First set flag to 0 */
2517 DELSET(got_signals, SIGCHLD);
2518
2519 /* See which child this was */
2520 for(ch = family; ch; ch = ch->next)
2521 if (ch->flags & ZOMBIE) {
2522 INITDBG(L_VB, "Child died, PID= %d", ch->pid);
2523 ch->flags &= ~(RUNNING|ZOMBIE|WAITING);
2524 if (ch->process[0] != '+')
2525 write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
2526 }
2527
2528 }
2529
2530 if (ISMEMBER(got_signals, SIGHUP)) {
2531 INITDBG(L_VB, "got SIGHUP");
2532#if CHANGE_WAIT
2533 /* Are we waiting for a child? */
2534 for(ch = family; ch; ch = ch->next)
2535 if (ch->flags & WAITING) break;
2536 if (ch == NULL)
2537#endif
2538 {
2539 /* We need to go into a new runlevel */
2540 oldlevel = runlevel;
2541#ifdef INITLVL
2542 runlevel = read_level(0);
2543#endif
2544 if (runlevel == 'U') {
2545 runlevel = oldlevel;
2546 re_exec();
2547 } else {
2548 if (oldlevel != 'S' && runlevel == 'S') console_stty();
2549 if (runlevel == '6' || runlevel == '0' ||
2550 runlevel == '1') console_stty();
2551 read_inittab();
2552 fail_cancel();
5f959b29 2553 setproctitle("init [%c]", (int)runlevel);
a74aeac6
PR
2554 DELSET(got_signals, SIGHUP);
2555 }
2556 }
2557 }
2558 if (ISMEMBER(got_signals, SIGUSR1)) {
2559 /*
2560 * SIGUSR1 means close and reopen /dev/initctl
2561 */
2562 INITDBG(L_VB, "got SIGUSR1");
2563 close(pipe_fd);
2564 pipe_fd = -1;
2565 DELSET(got_signals, SIGUSR1);
2566 }
2567}
2568
2569/*
2570 * The main loop
2571 */
5f959b29
DWF
2572static
2573void init_main(void)
a74aeac6
PR
2574{
2575 CHILD *ch;
2576 struct sigaction sa;
2577 sigset_t sgt;
a74aeac6
PR
2578 int f, st;
2579
2580 if (!reload) {
2581
2582#if INITDEBUG
2583 /*
2584 * Fork so we can debug the init process.
2585 */
2586 if ((f = fork()) > 0) {
2587 static const char killmsg[] = "PRNT: init killed.\r\n";
2588 pid_t rc;
2589
2590 while((rc = wait(&st)) != f)
2591 if (rc < 0 && errno == ECHILD)
2592 break;
c5ae561e 2593 safe_write(1, killmsg, sizeof(killmsg) - 1);
a74aeac6
PR
2594 while(1) pause();
2595 }
2596#endif
2597
2598#ifdef __linux__
2599 /*
2600 * Tell the kernel to send us SIGINT when CTRL-ALT-DEL
2601 * is pressed, and that we want to handle keyboard signals.
2602 */
2603 init_reboot(BMAGIC_SOFT);
2604 if ((f = open(VT_MASTER, O_RDWR | O_NOCTTY)) >= 0) {
2605 (void) ioctl(f, KDSIGACCEPT, SIGWINCH);
2606 close(f);
2607 } else
2608 (void) ioctl(0, KDSIGACCEPT, SIGWINCH);
2609#endif
2610
2611 /*
2612 * Ignore all signals.
2613 */
2614 for(f = 1; f <= NSIG; f++)
2615 SETSIG(sa, f, SIG_IGN, SA_RESTART);
2616 }
2617
2618 SETSIG(sa, SIGALRM, signal_handler, 0);
2619 SETSIG(sa, SIGHUP, signal_handler, 0);
2620 SETSIG(sa, SIGINT, signal_handler, 0);
2621 SETSIG(sa, SIGCHLD, chld_handler, SA_RESTART);
2622 SETSIG(sa, SIGPWR, signal_handler, 0);
2623 SETSIG(sa, SIGWINCH, signal_handler, 0);
2624 SETSIG(sa, SIGUSR1, signal_handler, 0);
2625 SETSIG(sa, SIGSTOP, stop_handler, SA_RESTART);
2626 SETSIG(sa, SIGTSTP, stop_handler, SA_RESTART);
2627 SETSIG(sa, SIGCONT, cont_handler, SA_RESTART);
2628 SETSIG(sa, SIGSEGV, (void (*)(int))segv_handler, SA_RESTART);
2629
2630 console_init();
2631
2632 if (!reload) {
63a2c981 2633 int fd;
a74aeac6
PR
2634
2635 /* Close whatever files are open, and reset the console. */
2636 close(0);
2637 close(1);
2638 close(2);
2639 console_stty();
2640 setsid();
2641
2642 /*
2643 * Set default PATH variable.
2644 */
2645 setenv("PATH", PATH_DEFAULT, 1 /* Overwrite */);
2646
2647 /*
2648 * Initialize /var/run/utmp (only works if /var is on
2649 * root and mounted rw)
2650 */
63a2c981
DWF
2651 if ((fd = open(UTMP_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0)
2652 close(fd);
a74aeac6
PR
2653
2654 /*
2655 * Say hello to the world
2656 */
2657 initlog(L_CO, bootmsg, "booting");
2658
2659 /*
2660 * See if we have to start an emergency shell.
2661 */
2662 if (emerg_shell) {
23d97522 2663 pid_t rc;
a74aeac6
PR
2664 SETSIG(sa, SIGCHLD, SIG_DFL, SA_RESTART);
2665 if (spawn(&ch_emerg, &f) > 0) {
2666 while((rc = wait(&st)) != f)
2667 if (rc < 0 && errno == ECHILD)
2668 break;
2669 }
2670 SETSIG(sa, SIGCHLD, chld_handler, SA_RESTART);
2671 }
2672
2673 /*
2674 * Start normal boot procedure.
2675 */
2676 runlevel = '#';
2677 read_inittab();
2678
2679 } else {
2680 /*
2681 * Restart: unblock signals and let the show go on
2682 */
2683 initlog(L_CO, bootmsg, "reloading");
2684 sigfillset(&sgt);
2685 sigprocmask(SIG_UNBLOCK, &sgt, NULL);
2686
2687 /*
2688 * Set default PATH variable.
2689 */
2690 setenv("PATH", PATH_DEFAULT, 0 /* Don't overwrite */);
2691 }
2692 start_if_needed();
2693
2694 while(1) {
2695
2696 /* See if we need to make the boot transitions. */
2697 boot_transitions();
2698 INITDBG(L_VB, "init_main: waiting..");
2699
2700 /* Check if there are processes to be waited on. */
2701 for(ch = family; ch; ch = ch->next)
2702 if ((ch->flags & RUNNING) && ch->action != BOOT) break;
2703
2704#if CHANGE_WAIT
2705 /* Wait until we get hit by some signal. */
2706 while (ch != NULL && got_signals == 0) {
2707 if (ISMEMBER(got_signals, SIGHUP)) {
2708 /* See if there are processes to be waited on. */
2709 for(ch = family; ch; ch = ch->next)
2710 if (ch->flags & WAITING) break;
2711 }
2712 if (ch != NULL) check_init_fifo();
2713 }
2714#else /* CHANGE_WAIT */
2715 if (ch != NULL && got_signals == 0) check_init_fifo();
2716#endif /* CHANGE_WAIT */
2717
2718 /* Check the 'failing' flags */
2719 fail_check();
2720
2721 /* Process any signals. */
2722 process_signals();
2723
2724 /* See what we need to start up (again) */
2725 start_if_needed();
2726 }
2727 /*NOTREACHED*/
2728}
2729
2730/*
2731 * Tell the user about the syntax we expect.
2732 */
5f959b29 2733static
a74aeac6
PR
2734void usage(char *s)
2735{
2736 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);
2737 exit(1);
2738}
2739
5f959b29 2740static
a74aeac6
PR
2741int telinit(char *progname, int argc, char **argv)
2742{
2743#ifdef TELINIT_USES_INITLVL
2744 FILE *fp;
2745#endif
2746 struct init_request request;
2747 struct sigaction sa;
2748 int f, fd, l;
2749 char *env = NULL;
2750
2751 memset(&request, 0, sizeof(request));
2752 request.magic = INIT_MAGIC;
2753
2754 while ((f = getopt(argc, argv, "t:e:")) != EOF) switch(f) {
2755 case 't':
2756 sltime = atoi(optarg);
2757 break;
2758 case 'e':
2759 if (env == NULL)
2760 env = request.i.data;
2761 l = strlen(optarg);
2762 if (env + l + 2 > request.i.data + sizeof(request.i.data)) {
2763 fprintf(stderr, "%s: -e option data "
2764 "too large\n", progname);
2765 exit(1);
2766 }
2767 memcpy(env, optarg, l);
2768 env += l;
2769 *env++ = 0;
2770 break;
2771 default:
2772 usage(progname);
2773 break;
2774 }
2775
2776 if (env) *env++ = 0;
2777
2778 if (env) {
2779 if (argc != optind)
2780 usage(progname);
2781 request.cmd = INIT_CMD_SETENV;
2782 } else {
2783 if (argc - optind != 1 || strlen(argv[optind]) != 1)
2784 usage(progname);
2785 if (!strchr("0123456789SsQqAaBbCcUu", argv[optind][0]))
2786 usage(progname);
2787 request.cmd = INIT_CMD_RUNLVL;
2788 request.runlevel = env ? 0 : argv[optind][0];
2789 request.sleeptime = sltime;
2790 }
2791
5f959b29
DWF
2792 /* Change to the root directory. */
2793 chdir("/");
2794
a74aeac6
PR
2795 /* Open the fifo and write a command. */
2796 /* Make sure we don't hang on opening /dev/initctl */
2797 SETSIG(sa, SIGALRM, signal_handler, 0);
2798 alarm(3);
5f959b29
DWF
2799 if ((fd = open(INIT_FIFO, O_WRONLY)) >= 0) {
2800 ssize_t p = 0;
2801 size_t s = sizeof(request);
2802 void *ptr = &request;
2803
2804 while (s > 0) {
2805 p = write(fd, ptr, s);
2806 if (p < 0) {
2807 if (errno == EINTR || errno == EAGAIN)
2808 continue;
2809 break;
2810 }
2811 ptr += p;
2812 s -= p;
2813 }
a74aeac6
PR
2814 close(fd);
2815 alarm(0);
2816 return 0;
2817 }
2818
2819#ifdef TELINIT_USES_INITLVL
2820 if (request.cmd == INIT_CMD_RUNLVL) {
2821 /* Fallthrough to the old method. */
2822
2823 /* Now write the new runlevel. */
2824 if ((fp = fopen(INITLVL, "w")) == NULL) {
2825 fprintf(stderr, "%s: cannot create %s\n",
2826 progname, INITLVL);
2827 exit(1);
2828 }
2829 fprintf(fp, "%s %d", argv[optind], sltime);
2830 fclose(fp);
2831
2832 /* And tell init about the pending runlevel change. */
2833 if (kill(INITPID, SIGHUP) < 0) perror(progname);
2834
2835 return 0;
2836 }
2837#endif
2838
2839 fprintf(stderr, "%s: ", progname);
2840 if (ISMEMBER(got_signals, SIGALRM)) {
2841 fprintf(stderr, "timeout opening/writing control channel %s\n",
2842 INIT_FIFO);
2843 } else {
2844 perror(INIT_FIFO);
2845 }
2846 return 1;
2847}
2848
2849/*
2850 * Main entry for init and telinit.
2851 */
2852int main(int argc, char **argv)
2853{
2854 char *p;
2855 int f;
2856 int isinit;
e481cba6 2857#ifdef WITH_SELINUX
a74aeac6 2858 int enforce = 0;
e481cba6 2859#endif
a74aeac6
PR
2860
2861 /* Get my own name */
2862 if ((p = strrchr(argv[0], '/')) != NULL)
2863 p++;
2864 else
2865 p = argv[0];
5f959b29
DWF
2866
2867 /* Common umask */
c5ae561e 2868 umask(umask(077) | 022);
a74aeac6
PR
2869
2870 /* Quick check */
2871 if (geteuid() != 0) {
2872 fprintf(stderr, "%s: must be superuser.\n", p);
2873 exit(1);
2874 }
2875
2876 /*
2877 * Is this telinit or init ?
2878 */
2879 isinit = (getpid() == 1);
2880 for (f = 1; f < argc; f++) {
54cb4b75 2881 if (!strcmp(argv[f], "-i") || !strcmp(argv[f], "--init")) {
a74aeac6
PR
2882 isinit = 1;
2883 break;
54cb4b75 2884 }
a74aeac6
PR
2885 }
2886 if (!isinit) exit(telinit(p, argc, argv));
2887
2888 /*
2889 * Check for re-exec
2890 */
2891 if (check_pipe(STATE_PIPE)) {
2892
2893 receive_state(STATE_PIPE);
2894
2895 myname = istrdup(argv[0]);
2896 argv0 = argv[0];
2897 maxproclen = 0;
2898 for (f = 0; f < argc; f++)
2899 maxproclen += strlen(argv[f]) + 1;
2900 reload = 1;
5f959b29 2901 setproctitle("init [%c]", (int)runlevel);
a74aeac6
PR
2902
2903 init_main();
2904 }
2905
2906 /* Check command line arguments */
2907 maxproclen = strlen(argv[0]) + 1;
2908 for(f = 1; f < argc; f++) {
2909 if (!strcmp(argv[f], "single") || !strcmp(argv[f], "-s"))
2910 dfl_level = 'S';
2911 else if (!strcmp(argv[f], "-a") || !strcmp(argv[f], "auto"))
2912 putenv("AUTOBOOT=YES");
2913 else if (!strcmp(argv[f], "-b") || !strcmp(argv[f],"emergency"))
2914 emerg_shell = 1;
2915 else if (!strcmp(argv[f], "-z")) {
2916 /* Ignore -z xxx */
2917 if (argv[f + 1]) f++;
2918 } else if (strchr("0123456789sS", argv[f][0])
2919 && strlen(argv[f]) == 1)
2920 dfl_level = argv[f][0];
2921 /* "init u" in the very beginning makes no sense */
2922 if (dfl_level == 's') dfl_level = 'S';
2923 maxproclen += strlen(argv[f]) + 1;
2924 }
2925
2926#ifdef WITH_SELINUX
9af2d863 2927 if (getenv("SELINUX_INIT") == NULL) {
be62ca92 2928 if (is_selinux_enabled() != 1) {
9af2d863 2929 if (selinux_init_load_policy(&enforce) == 0) {
be62ca92 2930 putenv("SELINUX_INIT=YES");
9af2d863
DWF
2931 execv(myname, argv);
2932 } else {
2933 if (enforce > 0) {
2934 /* SELinux in enforcing mode but load_policy failed */
2935 /* At this point, we probably can't open /dev/console, so log() won't work */
2936 fprintf(stderr,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.\n");
2937 exit(1);
2938 }
a74aeac6
PR
2939 }
2940 }
2941 }
2942#endif
2943 /* Start booting. */
2944 argv0 = argv[0];
2945 argv[1] = NULL;
2946 setproctitle("init boot");
5f959b29 2947 init_main();
a74aeac6
PR
2948
2949 /*NOTREACHED*/
2950 return 0;
2951}