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