]>
git.wh0rd.org - sysvinit.git/blob - src/init.c
2 * Init A System-V Init Clone.
5 * init [0123456SsQqAaBbCc]
6 * telinit [0123456SsQqAaBbCc]
8 * Version: @(#)init.c 2.86 30-Jul-2004 miquels@cistron.nl
10 #define VERSION "2.88"
11 #define DATE "26-Mar-2010"
13 * This file is part of the sysvinit suite,
14 * Copyright (C) 1991-2004 Miquel van Smoorenburg.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/types.h>
34 #include <sys/ioctl.h>
39 #include <sys/resource.h>
52 #include <sys/syslog.h>
56 # include <selinux/selinux.h>
57 # include <sys/mount.h>
58 # ifndef MNT_DETACH /* present in glibc 2.10, missing in 2.7 */
66 # define STACK_DEBUG 1
67 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
68 /* Only glibc 2.0 needs this */
69 # include <sigcontext.h>
70 # elif ( __GLIBC__ > 2) && ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1))
71 # include <bits/sigcontext.h>
83 # define SIGPWR SIGUSR2
93 /* Set a signal handler. */
94 #define SETSIG(sa, sig, fun, flags) \
96 sa.sa_handler = fun; \
97 sa.sa_flags = flags; \
98 sigemptyset(&sa.sa_mask); \
99 sigaction(sig, &sa, NULL); \
102 /* Version information */
103 char *Version
= "@(#) init " VERSION
" " DATE
" miquels@cistron.nl";
104 char *bootmsg
= "version " VERSION
" %s";
105 #define E_VERSION "INIT_VERSION=sysvinit-" VERSION
107 CHILD
*family
= NULL
; /* The linked list of all entries */
108 CHILD
*newFamily
= NULL
; /* The list after inittab re-read */
110 CHILD ch_emerg
= { /* Emergency shell */
120 char runlevel
= 'S'; /* The current run level */
121 char thislevel
= 'S'; /* The current runlevel */
122 char prevlevel
= 'N'; /* Previous runlevel */
123 int dfl_level
= 0; /* Default runlevel */
124 sig_atomic_t got_cont
= 0; /* Set if we received the SIGCONT signal */
125 sig_atomic_t got_signals
; /* Set if we received a signal. */
126 int emerg_shell
= 0; /* Start emergency shell? */
127 int wrote_wtmp_reboot
= 1; /* Set when we wrote the reboot record */
128 int wrote_utmp_reboot
= 1; /* Set when we wrote the reboot record */
129 int wrote_wtmp_rlevel
= 1; /* Set when we wrote the runlevel record */
130 int wrote_utmp_rlevel
= 1; /* Set when we wrote the runlevel record */
131 int sltime
= 5; /* Sleep time between TERM and KILL */
132 char *argv0
; /* First arguments; show up in ps listing */
133 int maxproclen
; /* Maximal length of argv[0] with \0 */
134 struct utmp utproto
; /* Only used for sizeof(utproto.ut_id) */
135 char *console_dev
; /* Console device. */
136 int pipe_fd
= -1; /* /dev/initctl */
137 int did_boot
= 0; /* Did we already do BOOT* stuff? */
138 int main(int, char **);
140 /* Used by re-exec part */
141 int reload
= 0; /* Should we do initialization stuff? */
142 char *myname
="/sbin/init"; /* What should we exec */
143 int oops_error
; /* Used by some of the re-exec code. */
144 const char *Signature
= "12567362"; /* Signature for re-exec fd */
146 /* Macro to see if this is a special action */
147 #define ISPOWER(i) ((i) == POWERWAIT || (i) == POWERFAIL || \
148 (i) == POWEROKWAIT || (i) == POWERFAILNOW || \
151 /* ascii values for the `action' field. */
156 { "respawn", RESPAWN
},
160 { "bootwait", BOOTWAIT
},
161 { "powerfail", POWERFAIL
},
162 { "powerfailnow",POWERFAILNOW
},
163 { "powerwait", POWERWAIT
},
164 { "powerokwait", POWEROKWAIT
},
165 { "ctrlaltdel", CTRLALTDEL
},
167 { "ondemand", ONDEMAND
},
168 { "initdefault", INITDEFAULT
},
169 { "sysinit", SYSINIT
},
170 { "kbrequest", KBREQUEST
},
175 * State parser token table (see receive_state)
188 { "CMD", C_PROCESS
},
191 { "-RL", D_RUNLEVEL
},
192 { "-TL", D_THISLEVEL
},
193 { "-PL", D_PREVLEVEL
},
194 { "-SI", D_GOTSIGN
},
195 { "-WR", D_WROTE_WTMP_REBOOT
},
196 { "-WU", D_WROTE_UTMP_REBOOT
},
198 { "-DB", D_DIDBOOT
},
199 { "-LW", D_WROTE_WTMP_RLEVEL
},
200 { "-LU", D_WROTE_UTMP_RLEVEL
},
214 #define NR_EXTRA_ENV 16
215 char *extra_env
[NR_EXTRA_ENV
];
219 * Sleep a number of seconds.
221 * This only works correctly because the linux select updates
222 * the elapsed time in the struct timeval passed to select!
225 void do_sleep(int sec
)
232 while(select(0, NULL
, NULL
, NULL
, &tv
) < 0 && errno
== EINTR
)
238 * Non-failing allocation routines (init cannot fail).
241 void *imalloc(size_t size
)
245 while ((m
= malloc(size
)) == NULL
) {
246 initlog(L_VB
, "out of memory");
254 char *istrdup(char *s
)
267 * Send the state info of the previous running init to
268 * the new one, in a version-independant way.
271 void send_state(int fd
)
279 fprintf(fp
, "VER%s\n", Version
);
280 fprintf(fp
, "-RL%c\n", runlevel
);
281 fprintf(fp
, "-TL%c\n", thislevel
);
282 fprintf(fp
, "-PL%c\n", prevlevel
);
283 fprintf(fp
, "-SI%u\n", got_signals
);
284 fprintf(fp
, "-WR%d\n", wrote_wtmp_reboot
);
285 fprintf(fp
, "-WU%d\n", wrote_utmp_reboot
);
286 fprintf(fp
, "-ST%d\n", sltime
);
287 fprintf(fp
, "-DB%d\n", did_boot
);
289 for (p
= family
; p
; p
= p
->next
) {
290 fprintf(fp
, "REC%s\n", p
->id
);
291 fprintf(fp
, "LEV%s\n", p
->rlevel
);
292 for (i
= 0, val
= p
->flags
; flags
[i
].mask
; i
++)
293 if (val
& flags
[i
].mask
) {
294 val
&= ~flags
[i
].mask
;
295 fprintf(fp
, "FL %s\n",flags
[i
].name
);
297 fprintf(fp
, "PID%d\n",p
->pid
);
298 fprintf(fp
, "EXS%u\n",p
->exstat
);
299 for(i
= 0; actions
[i
].act
; i
++)
300 if (actions
[i
].act
== p
->action
) {
301 fprintf(fp
, "AC %s\n", actions
[i
].name
);
304 fprintf(fp
, "CMD%s\n", p
->process
);
305 fprintf(fp
, "EOR\n");
307 fprintf(fp
, "END\n");
312 * Read a string from a file descriptor.
313 * FIXME: why not use fgets() ?
315 static int get_string(char *p
, int size
, FILE *f
)
319 while ((c
= getc(f
)) != EOF
&& c
!= '\n') {
324 return (c
!= EOF
) && (size
> 0);
328 * Read trailing data from the state pipe until we see a newline.
330 static int get_void(FILE *f
)
334 while ((c
= getc(f
)) != EOF
&& c
!= '\n')
341 * Read the next "command" from the state pipe.
343 static int get_cmd(FILE *f
)
348 if (fread(cmd
, 1, sizeof(cmd
) - 1, f
) != sizeof(cmd
) - 1)
351 for(i
= 0; cmds
[i
].cmd
&& strcmp(cmds
[i
].name
, cmd
) != 0; i
++)
357 * Read a CHILD * from the state pipe.
359 static CHILD
*get_record(FILE *f
)
367 switch (cmd
= get_cmd(f
)) {
377 fscanf(f
, "%c\n", &runlevel
);
380 fscanf(f
, "%c\n", &thislevel
);
383 fscanf(f
, "%c\n", &prevlevel
);
386 fscanf(f
, "%u\n", &got_signals
);
388 case D_WROTE_WTMP_REBOOT
:
389 fscanf(f
, "%d\n", &wrote_wtmp_reboot
);
391 case D_WROTE_UTMP_REBOOT
:
392 fscanf(f
, "%d\n", &wrote_utmp_reboot
);
395 fscanf(f
, "%d\n", &sltime
);
398 fscanf(f
, "%d\n", &did_boot
);
400 case D_WROTE_WTMP_RLEVEL
:
401 fscanf(f
, "%d\n", &wrote_wtmp_rlevel
);
403 case D_WROTE_UTMP_RLEVEL
:
404 fscanf(f
, "%d\n", &wrote_utmp_rlevel
);
407 if (cmd
> 0 || cmd
== C_EOF
) {
412 } while (cmd
!= C_REC
);
414 p
= imalloc(sizeof(CHILD
));
415 get_string(p
->id
, sizeof(p
->id
), f
);
417 do switch(cmd
= get_cmd(f
)) {
423 fscanf(f
, "%d\n", &(p
->pid
));
426 fscanf(f
, "%u\n", &(p
->exstat
));
429 get_string(p
->rlevel
, sizeof(p
->rlevel
), f
);
432 get_string(p
->process
, sizeof(p
->process
), f
);
435 get_string(s
, sizeof(s
), f
);
436 for(i
= 0; flags
[i
].name
; i
++) {
437 if (strcmp(flags
[i
].name
,s
) == 0)
440 p
->flags
|= flags
[i
].mask
;
443 get_string(s
, sizeof(s
), f
);
444 for(i
= 0; actions
[i
].name
; i
++) {
445 if (strcmp(actions
[i
].name
, s
) == 0)
448 p
->action
= actions
[i
].act
? actions
[i
].act
: OFF
;
454 } while( cmd
!= C_EOR
);
460 * Read the complete state info from the state pipe.
461 * Returns 0 on success
464 int receive_state(int fd
)
467 char old_version
[256];
472 if (get_cmd(f
) != C_VER
)
474 get_string(old_version
, sizeof(old_version
), f
);
476 for (pp
= &family
; (*pp
= get_record(f
)) != NULL
; pp
= &((*pp
)->next
))
483 * Set the process title.
486 __attribute__ ((format (printf
, 1, 2)))
488 static int setproctitle(char *fmt
, ...)
497 len
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
500 if (maxproclen
> 1) {
501 memset(argv0
, 0, maxproclen
);
502 strncpy(argv0
, buf
, maxproclen
- 1);
509 * Set console_dev to a working console.
512 void console_init(void)
515 int tried_devcons
= 0;
516 int tried_vtmaster
= 0;
519 if ((s
= getenv("CONSOLE")) != NULL
)
522 console_dev
= CONSOLE
;
526 while ((fd
= open(console_dev
, O_RDONLY
|O_NONBLOCK
)) < 0) {
527 if (!tried_devcons
) {
529 console_dev
= CONSOLE
;
532 if (!tried_vtmaster
) {
534 console_dev
= VT_MASTER
;
540 console_dev
= "/dev/null";
547 * Open the console with retries.
550 int console_open(int mode
)
556 * Open device in nonblocking mode.
558 m
= mode
| O_NONBLOCK
;
561 * Retry the open five times.
563 for(f
= 0; f
< 5; f
++) {
564 if ((fd
= open(console_dev
, m
)) >= 0) break;
568 if (fd
< 0) return fd
;
571 * Set original flags.
574 fcntl(fd
, F_SETFL
, mode
);
579 * We got a signal (HUP PWR WINCH ALRM INT)
582 void signal_handler(int sig
)
584 ADDSET(got_signals
, sig
);
588 * SIGCHLD: one of our children has died.
592 void chld_handler(int sig
__attribute__((unused
)))
594 void chld_handler(int sig
)
599 int saved_errno
= errno
;
602 * Find out which process(es) this was (were)
604 while((pid
= waitpid(-1, &st
, WNOHANG
)) != 0) {
605 if (errno
== ECHILD
) break;
606 for( ch
= family
; ch
; ch
= ch
->next
)
607 if ( ch
->pid
== pid
&& (ch
->flags
& RUNNING
) ) {
609 "chld_handler: marked %d as zombie",
611 ADDSET(got_signals
, SIGCHLD
);
615 ch
->new->exstat
= st
;
616 ch
->new->flags
|= ZOMBIE
;
621 INITDBG(L_VB
, "chld_handler: unknown child %d exited.",
629 * Linux ignores all signals sent to init when the
630 * SIG_DFL handler is installed. Therefore we must catch SIGTSTP
631 * and SIGCONT, or else they won't work....
633 * The SIGCONT handler
637 void cont_handler(int sig
__attribute__((unused
)))
639 void cont_handler(int sig
)
646 * Fork and dump core in /.
651 static int dumped
= 0;
658 if (fork() != 0) return;
661 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
663 rlim
.rlim_cur
= RLIM_INFINITY
;
664 rlim
.rlim_max
= RLIM_INFINITY
;
665 setrlimit(RLIMIT_CORE
, &rlim
);
668 signal(SIGSEGV
, SIG_DFL
);
670 sigdelset(&mask
, SIGSEGV
);
671 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
678 * OOPS: segmentation violation!
679 * If we have the info, print where it occured.
680 * Then sleep 30 seconds and try to continue.
683 #if defined(STACK_DEBUG) && defined(__linux__)
685 void segv_handler(int sig
__attribute__((unused
)), struct sigcontext ctx
)
687 void segv_handler(int sig
, struct sigcontext ctx
)
691 int saved_errno
= errno
;
693 if ((void *)ctx
.eip
>= (void *)do_sleep
&&
694 (void *)ctx
.eip
< (void *)main
)
696 initlog(L_VB
, "PANIC: segmentation violation at %p%s! "
697 "sleeping for 30 seconds.", (void *)ctx
.eip
, p
);
704 void segv_handler(int sig
__attribute__((unused
)))
706 void segv_handler(int sig
)
709 int saved_errno
= errno
;
712 "PANIC: segmentation violation! sleeping for 30 seconds.");
720 * The SIGSTOP & SIGTSTP handler
724 void stop_handler(int sig
__attribute__((unused
)))
726 void stop_handler(int sig
)
729 int saved_errno
= errno
;
732 while(!got_cont
) pause();
738 * Set terminal settings to reasonable defaults
741 void console_stty(void)
746 if ((fd
= console_open(O_RDWR
|O_NOCTTY
)) < 0) {
747 initlog(L_VB
, "can't open %s", console_dev
);
751 #ifdef __FreeBSD_kernel__
753 * The kernel of FreeBSD expects userland to set TERM. Usually, we want
754 * "cons25". Later, gettys might disagree on this (i.e. we're not using
755 * syscons) but some boot scripts, like /etc/init.d/xserver-xorg, still
756 * need a non-dumb terminal.
758 putenv ("TERM=cons25");
761 (void) tcgetattr(fd
, &tty
);
763 tty
.c_cflag
&= CBAUD
|CBAUDEX
|CSIZE
|CSTOPB
|PARENB
|PARODD
;
764 tty
.c_cflag
|= HUPCL
|CLOCAL
|CREAD
;
766 tty
.c_cc
[VINTR
] = CINTR
;
767 tty
.c_cc
[VQUIT
] = CQUIT
;
768 tty
.c_cc
[VERASE
] = CERASE
; /* ASCII DEL (0177) */
769 tty
.c_cc
[VKILL
] = CKILL
;
770 tty
.c_cc
[VEOF
] = CEOF
;
773 tty
.c_cc
[VSWTC
] = _POSIX_VDISABLE
;
774 tty
.c_cc
[VSTART
] = CSTART
;
775 tty
.c_cc
[VSTOP
] = CSTOP
;
776 tty
.c_cc
[VSUSP
] = CSUSP
;
777 tty
.c_cc
[VEOL
] = _POSIX_VDISABLE
;
778 tty
.c_cc
[VREPRINT
] = CREPRINT
;
779 tty
.c_cc
[VDISCARD
] = CDISCARD
;
780 tty
.c_cc
[VWERASE
] = CWERASE
;
781 tty
.c_cc
[VLNEXT
] = CLNEXT
;
782 tty
.c_cc
[VEOL2
] = _POSIX_VDISABLE
;
785 * Set pre and post processing
787 tty
.c_iflag
= IGNPAR
|ICRNL
|IXON
|IXANY
;
788 #ifdef IUTF8 /* Not defined on FreeBSD */
789 tty
.c_iflag
|= IUTF8
;
791 tty
.c_oflag
= OPOST
|ONLCR
;
792 tty
.c_lflag
= ISIG
|ICANON
|ECHO
|ECHOCTL
|ECHOPRT
|ECHOKE
;
794 #if defined(SANE_TIO) && (SANE_TIO == 1)
796 * Disable flow control (-ixon), ignore break (ignbrk),
797 * and make nl/cr more usable (sane).
799 tty
.c_iflag
|= IGNBRK
;
800 tty
.c_iflag
&= ~(BRKINT
|INLCR
|IGNCR
|IXON
);
801 tty
.c_oflag
&= ~(OCRNL
|ONLRET
);
804 * Now set the terminal line.
805 * We don't care about non-transmitted output data
806 * and non-read input data.
808 (void) tcsetattr(fd
, TCSANOW
, &tty
);
809 (void) tcflush(fd
, TCIOFLUSH
);
814 * Print to the system console
820 if ((fd
= console_open(O_WRONLY
|O_NOCTTY
|O_NDELAY
)) >= 0) {
821 write(fd
, s
, strlen(s
));
827 * Log something to a logfile and the console.
830 __attribute__ ((format (printf
, 2, 3)))
832 void initlog(int loglevel
, char *s
, ...)
836 sigset_t nmask
, omask
;
838 va_start(va_alist
, s
);
839 vsnprintf(buf
, sizeof(buf
), s
, va_alist
);
842 if (loglevel
& L_SY
) {
844 * Re-establish connection with syslogd every time.
845 * Block signals while talking to syslog.
848 sigprocmask(SIG_BLOCK
, &nmask
, &omask
);
849 openlog("init", 0, LOG_DAEMON
);
850 syslog(LOG_INFO
, "%s", buf
);
852 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
856 * And log to the console.
858 if (loglevel
& L_CO
) {
867 * Build a new environment for execve().
869 char **init_buildenv(int child
)
871 char i_lvl
[] = "RUNLEVEL=x";
872 char i_prev
[] = "PREVLEVEL=x";
874 char i_shell
[] = "SHELL=" SHELL
;
878 for (n
= 0; environ
[n
]; n
++)
883 e
= calloc(n
, sizeof(char *));
885 for (n
= 0; environ
[n
]; n
++)
886 e
[n
] = istrdup(environ
[n
]);
888 for (i
= 0; i
< NR_EXTRA_ENV
; i
++) {
890 e
[n
++] = istrdup(extra_env
[i
]);
894 snprintf(i_cons
, sizeof(i_cons
), "CONSOLE=%s", console_dev
);
895 i_lvl
[9] = thislevel
;
896 i_prev
[10] = prevlevel
;
897 e
[n
++] = istrdup(i_shell
);
898 e
[n
++] = istrdup(i_lvl
);
899 e
[n
++] = istrdup(i_prev
);
900 e
[n
++] = istrdup(i_cons
);
901 e
[n
++] = istrdup(E_VERSION
);
910 void init_freeenv(char **e
)
914 for (n
= 0; e
[n
]; n
++)
923 * This function is too long and indents too deep.
927 pid_t
spawn(CHILD
*ch
, int *res
)
929 char *args
[16]; /* Argv array */
930 char buf
[136]; /* Line buffer */
931 int f
, st
; /* Scratch variables */
932 char *ptr
; /* Ditto */
933 time_t t
; /* System time */
934 int oldAlarm
; /* Previous alarm value */
935 char *proc
= ch
->process
; /* Command line */
936 pid_t pid
, pgrp
; /* child, console process group. */
937 sigset_t nmask
, omask
; /* For blocking SIGCHLD */
941 buf
[sizeof(buf
) - 1] = 0;
943 /* Skip '+' if it's there */
944 if (proc
[0] == '+') proc
++;
946 ch
->flags
|= XECUTED
;
948 if (ch
->action
== RESPAWN
|| ch
->action
== ONDEMAND
) {
949 /* Is the date stamp from less than 2 minutes ago? */
951 if (ch
->tm
+ TESTTIME
> t
) {
958 /* Do we try to respawn too fast? */
959 if (ch
->count
>= MAXSPAWN
) {
962 "Id \"%s\" respawning too fast: disabled for %d minutes",
963 ch
->id
, SLEEPTIME
/ 60);
964 ch
->flags
&= ~RUNNING
;
965 ch
->flags
|= FAILING
;
967 /* Remember the time we stopped */
970 /* Try again in 5 minutes */
972 if (oldAlarm
> SLEEPTIME
|| oldAlarm
<= 0) oldAlarm
= SLEEPTIME
;
978 /* See if there is an "initscript" (except in single user mode). */
979 if (access(INITSCRIPT
, R_OK
) == 0 && runlevel
!= 'S') {
980 /* Build command line using "initscript" */
982 args
[2] = INITSCRIPT
;
984 args
[4] = ch
->rlevel
;
986 for(f
= 0; actions
[f
].name
; f
++) {
987 if (ch
->action
== actions
[f
].act
) {
988 args
[5] = actions
[f
].name
;
994 } else if (strpbrk(proc
, "~`!$^&*()=|\\{}[];\"'<>?")) {
995 /* See if we need to fire off a shell for this command */
996 /* Give command line to shell */
999 strcpy(buf
, "exec ");
1000 strncat(buf
, proc
, sizeof(buf
) - strlen(buf
) - 1);
1004 /* Split up command line arguments */
1006 strncat(buf
, proc
, sizeof(buf
) - 1);
1008 for(f
= 1; f
< 15; f
++) {
1009 /* Skip white space */
1010 while(*ptr
== ' ' || *ptr
== '\t') ptr
++;
1013 /* May be trailing space.. */
1014 if (*ptr
== 0) break;
1016 /* Skip this `word' */
1017 while(*ptr
&& *ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '#')
1020 /* If end-of-line, break */
1021 if (*ptr
== '#' || *ptr
== 0) {
1026 /* End word with \0 and continue */
1034 * Block sigchild while forking.
1036 sigemptyset(&nmask
);
1037 sigaddset(&nmask
, SIGCHLD
);
1038 sigprocmask(SIG_BLOCK
, &nmask
, &omask
);
1040 if ((pid
= fork()) == 0) {
1045 if (pipe_fd
>= 0) close(pipe_fd
);
1047 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
1050 * In sysinit, boot, bootwait or single user mode:
1051 * for any wait-type subprocess we _force_ the console
1052 * to be its controlling tty.
1054 if (strchr("*#sS", runlevel
) && ch
->flags
& WAITING
) {
1056 * We fork once extra. This is so that we can
1057 * wait and change the process group and session
1058 * of the console after exit of the leader.
1061 if ((f
= console_open(O_RDWR
|O_NOCTTY
)) >= 0) {
1062 /* Take over controlling tty by force */
1063 (void)ioctl(f
, TIOCSCTTY
, 1);
1069 * 4 Sep 2001, Andrea Arcangeli:
1070 * Fix a race in spawn() that is used to deadlock init in a
1071 * waitpid() loop: must set the childhandler as default before forking
1072 * off the child or the chld_handler could run before the waitpid loop
1073 * has a chance to find its zombie-child.
1075 SETSIG(sa
, SIGCHLD
, SIG_DFL
, SA_RESTART
);
1076 if ((pid
= fork()) < 0) {
1077 initlog(L_VB
, "cannot fork: %s",
1084 * Ignore keyboard signals etc.
1085 * Then wait for child to exit.
1087 SETSIG(sa
, SIGINT
, SIG_IGN
, SA_RESTART
);
1088 SETSIG(sa
, SIGTSTP
, SIG_IGN
, SA_RESTART
);
1089 SETSIG(sa
, SIGQUIT
, SIG_IGN
, SA_RESTART
);
1091 while ((rc
= waitpid(pid
, &st
, 0)) != pid
)
1092 if (rc
< 0 && errno
== ECHILD
)
1096 * Small optimization. See if stealing
1097 * controlling tty back is needed.
1099 pgrp
= tcgetpgrp(f
);
1100 if (pgrp
!= getpid())
1104 * Steal controlling tty away. We do
1105 * this with a temporary process.
1107 if ((pid
= fork()) < 0) {
1108 initlog(L_VB
, "cannot fork: %s",
1114 (void)ioctl(f
, TIOCSCTTY
, 1);
1117 while((rc
= waitpid(pid
, &st
, 0)) != pid
)
1118 if (rc
< 0 && errno
== ECHILD
)
1123 /* Set ioctl settings to default ones */
1128 if ((f
= console_open(O_RDWR
|O_NOCTTY
)) < 0) {
1129 initlog(L_VB
, "open(%s): %s", console_dev
,
1131 f
= open("/dev/null", O_RDWR
);
1138 * Update utmp/wtmp file prior to starting
1139 * any child. This MUST be done right here in
1140 * the child process in order to prevent a race
1141 * condition that occurs when the child
1142 * process' time slice executes before the
1143 * parent (can and does happen in a uniprocessor
1144 * environment). If the child is a getty and
1145 * the race condition happens, then init's utmp
1146 * update will happen AFTER the getty runs
1147 * and expects utmp to be updated already!
1149 * Do NOT log if process field starts with '+'
1150 * FIXME: that's for compatibility with *very*
1151 * old getties - probably it can be taken out.
1153 if (ch
->process
[0] != '+')
1154 write_utmp_wtmp("", ch
->id
, getpid(), INIT_PROCESS
, "");
1156 /* Reset all the signals, set up environment */
1157 for(f
= 1; f
< NSIG
; f
++) SETSIG(sa
, f
, SIG_DFL
, SA_RESTART
);
1158 environ
= init_buildenv(1);
1161 * Execute prog. In case of ENOEXEC try again
1162 * as a shell script.
1164 execvp(args
[1], args
+ 1);
1165 if (errno
== ENOEXEC
) {
1168 strcpy(buf
, "exec ");
1169 strncat(buf
, proc
, sizeof(buf
) - strlen(buf
) - 1);
1172 execvp(args
[1], args
+ 1);
1174 initlog(L_VB
, "cannot execute \"%s\"", args
[1]);
1176 if (ch
->process
[0] != '+')
1177 write_utmp_wtmp("", ch
->id
, getpid(), DEAD_PROCESS
, NULL
);
1181 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
1183 INITDBG(L_VB
, "Started id %s (pid %d)", ch
->id
, pid
);
1186 initlog(L_VB
, "cannot fork, retry..");
1195 * Start a child running!
1198 void startup(CHILD
*ch
)
1201 * See if it's disabled
1203 if (ch
->flags
& FAILING
) return;
1205 switch(ch
->action
) {
1214 if (!(ch
->flags
& XECUTED
)) ch
->flags
|= WAITING
;
1219 if (ch
->flags
& XECUTED
) break;
1222 ch
->flags
|= RUNNING
;
1223 (void)spawn(ch
, &(ch
->pid
));
1230 * Read the inittab file.
1233 void read_inittab(void)
1235 FILE *fp
; /* The INITTAB file */
1236 CHILD
*ch
, *old
, *i
; /* Pointers to CHILD structure */
1237 CHILD
*head
= NULL
; /* Head of linked list */
1239 struct stat st
; /* To stat INITLVL */
1241 sigset_t nmask
, omask
; /* For blocking SIGCHLD. */
1242 char buf
[256]; /* Line buffer */
1243 char err
[64]; /* Error message. */
1245 *action
, *process
; /* Fields of a line */
1247 int lineNo
= 0; /* Line number in INITTAB file */
1248 int actionNo
; /* Decoded action field */
1249 int f
; /* Counter */
1250 int round
; /* round 0 for SIGTERM, 1 for SIGKILL */
1251 int foundOne
= 0; /* No killing no sleep */
1252 int talk
; /* Talk to the user */
1253 int done
= 0; /* Ready yet? */
1256 if (newFamily
!= NULL
) {
1257 INITDBG(L_VB
, "PANIC newFamily != NULL");
1260 INITDBG(L_VB
, "Reading inittab");
1264 * Open INITTAB and real line by line.
1266 if ((fp
= fopen(INITTAB
, "r")) == NULL
)
1267 initlog(L_VB
, "No inittab file found");
1271 * Add single user shell entry at the end.
1273 if (fp
== NULL
|| fgets(buf
, sizeof(buf
), fp
) == NULL
) {
1276 * See if we have a single user entry.
1278 for(old
= newFamily
; old
; old
= old
->next
)
1279 if (strpbrk(old
->rlevel
, "S")) break;
1281 snprintf(buf
, sizeof(buf
), "~~:S:wait:%s\n", SULOGIN
);
1287 * Skip comments and empty lines
1289 for(p
= buf
; *p
== ' ' || *p
== '\t'; p
++)
1291 if (*p
== '#' || *p
== '\n') continue;
1296 id
= strsep(&p
, ":");
1297 rlevel
= strsep(&p
, ":");
1298 action
= strsep(&p
, ":");
1299 process
= strsep(&p
, "\n");
1302 * Check if syntax is OK. Be very verbose here, to
1303 * avoid newbie postings on comp.os.linux.setup :)
1306 if (!id
|| !*id
) strcpy(err
, "missing id field");
1307 if (!rlevel
) strcpy(err
, "missing runlevel field");
1308 if (!process
) strcpy(err
, "missing process field");
1309 if (!action
|| !*action
)
1310 strcpy(err
, "missing action field");
1311 if (id
&& strlen(id
) > sizeof(utproto
.ut_id
))
1312 sprintf(err
, "id field too long (max %d characters)",
1313 (int)sizeof(utproto
.ut_id
));
1314 if (rlevel
&& strlen(rlevel
) > 11)
1315 strcpy(err
, "rlevel field too long (max 11 characters)");
1316 if (process
&& strlen(process
) > 127)
1317 strcpy(err
, "process field too long");
1318 if (action
&& strlen(action
) > 32)
1319 strcpy(err
, "action field too long");
1321 initlog(L_VB
, "%s[%d]: %s", INITTAB
, lineNo
, err
);
1322 INITDBG(L_VB
, "%s:%s:%s:%s", id
, rlevel
, action
, process
);
1327 * Decode the "action" field
1330 for(f
= 0; actions
[f
].name
; f
++)
1331 if (strcasecmp(action
, actions
[f
].name
) == 0) {
1332 actionNo
= actions
[f
].act
;
1335 if (actionNo
== -1) {
1336 initlog(L_VB
, "%s[%d]: %s: unknown action field",
1337 INITTAB
, lineNo
, action
);
1342 * See if the id field is unique
1344 for(old
= newFamily
; old
; old
= old
->next
) {
1345 if(strcmp(old
->id
, id
) == 0 && strcmp(id
, "~~")) {
1346 initlog(L_VB
, "%s[%d]: duplicate ID field \"%s\"",
1347 INITTAB
, lineNo
, id
);
1354 * Allocate a CHILD structure
1356 ch
= imalloc(sizeof(CHILD
));
1361 ch
->action
= actionNo
;
1362 strncpy(ch
->id
, id
, sizeof(utproto
.ut_id
) + 1); /* Hack for different libs. */
1363 strncpy(ch
->process
, process
, sizeof(ch
->process
) - 1);
1365 for(f
= 0; f
< (int)sizeof(rlevel
) - 1 && rlevel
[f
]; f
++) {
1366 ch
->rlevel
[f
] = rlevel
[f
];
1367 if (ch
->rlevel
[f
] == 's') ch
->rlevel
[f
] = 'S';
1369 strncpy(ch
->rlevel
, rlevel
, sizeof(ch
->rlevel
) - 1);
1371 strcpy(ch
->rlevel
, "0123456789");
1372 if (ISPOWER(ch
->action
))
1373 strcpy(ch
->rlevel
, "S0123456789");
1376 * We have the fake runlevel '#' for SYSINIT and
1377 * '*' for BOOT and BOOTWAIT.
1379 if (ch
->action
== SYSINIT
) strcpy(ch
->rlevel
, "#");
1380 if (ch
->action
== BOOT
|| ch
->action
== BOOTWAIT
)
1381 strcpy(ch
->rlevel
, "*");
1384 * Now add it to the linked list. Special for powerfail.
1386 if (ISPOWER(ch
->action
)) {
1389 * Disable by default
1391 ch
->flags
|= XECUTED
;
1394 * Tricky: insert at the front of the list..
1397 for(i
= newFamily
; i
; i
= i
->next
) {
1398 if (!ISPOWER(i
->action
)) break;
1402 * Now add after entry "old"
1407 if (i
== NULL
) head
= ch
;
1409 ch
->next
= newFamily
;
1411 if (ch
->next
== NULL
) head
= ch
;
1415 * Just add at end of the list
1417 if (ch
->action
== KBREQUEST
) ch
->flags
|= XECUTED
;
1427 * Walk through the old list comparing id fields
1429 for(old
= family
; old
; old
= old
->next
)
1430 if (strcmp(old
->id
, ch
->id
) == 0) {
1441 * Loop through the list of children, and see if they need to
1445 INITDBG(L_VB
, "Checking for children to kill");
1446 for(round
= 0; round
< 2; round
++) {
1448 for(ch
= family
; ch
; ch
= ch
->next
) {
1449 ch
->flags
&= ~KILLME
;
1452 * Is this line deleted?
1454 if (ch
->new == NULL
) ch
->flags
|= KILLME
;
1457 * If the entry has changed, kill it anyway. Note that
1458 * we do not check ch->process, only the "action" field.
1459 * This way, you can turn an entry "off" immediately, but
1460 * changes in the command line will only become effective
1461 * after the running version has exited.
1463 if (ch
->new && ch
->action
!= ch
->new->action
) ch
->flags
|= KILLME
;
1466 * Only BOOT processes may live in all levels
1468 if (ch
->action
!= BOOT
&&
1469 strchr(ch
->rlevel
, runlevel
) == NULL
) {
1471 * Ondemand procedures live always,
1472 * except in single user
1474 if (runlevel
== 'S' || !(ch
->flags
& DEMAND
))
1475 ch
->flags
|= KILLME
;
1479 * Now, if this process may live note so in the new list
1481 if ((ch
->flags
& KILLME
) == 0) {
1482 ch
->new->flags
= ch
->flags
;
1483 ch
->new->pid
= ch
->pid
;
1484 ch
->new->exstat
= ch
->exstat
;
1490 * Is this process still around?
1492 if ((ch
->flags
& RUNNING
) == 0) {
1493 ch
->flags
&= ~KILLME
;
1496 INITDBG(L_VB
, "Killing \"%s\"", ch
->process
);
1498 case 0: /* Send TERM signal */
1501 "Sending processes the TERM signal");
1502 kill(-(ch
->pid
), SIGTERM
);
1505 case 1: /* Send KILL signal and collect status */
1508 "Sending processes the KILL signal");
1509 kill(-(ch
->pid
), SIGKILL
);
1516 * See if we have to wait 5 seconds
1518 if (foundOne
&& round
== 0) {
1520 * Yup, but check every second if we still have children.
1522 for(f
= 0; f
< sltime
; f
++) {
1523 for(ch
= family
; ch
; ch
= ch
->next
) {
1524 if (!(ch
->flags
& KILLME
)) continue;
1525 if ((ch
->flags
& RUNNING
) && !(ch
->flags
& ZOMBIE
))
1530 * No running children, skip SIGKILL
1533 foundOne
= 0; /* Skip the sleep below. */
1542 * Now give all processes the chance to die and collect exit statuses.
1544 if (foundOne
) do_sleep(1);
1545 for(ch
= family
; ch
; ch
= ch
->next
)
1546 if (ch
->flags
& KILLME
) {
1547 if (!(ch
->flags
& ZOMBIE
))
1548 initlog(L_CO
, "Pid %d [id %s] seems to hang", ch
->pid
,
1551 INITDBG(L_VB
, "Updating utmp for pid %d [id %s]",
1553 ch
->flags
&= ~RUNNING
;
1554 if (ch
->process
[0] != '+')
1555 write_utmp_wtmp("", ch
->id
, ch
->pid
, DEAD_PROCESS
, NULL
);
1560 * Both rounds done; clean up the list.
1562 sigemptyset(&nmask
);
1563 sigaddset(&nmask
, SIGCHLD
);
1564 sigprocmask(SIG_BLOCK
, &nmask
, &omask
);
1565 for(ch
= family
; ch
; ch
= old
) {
1570 for(ch
= family
; ch
; ch
= ch
->next
) ch
->new = NULL
;
1572 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
1576 * Dispose of INITLVL file.
1578 if (lstat(INITLVL
, &st
) >= 0 && S_ISLNK(st
.st_mode
)) {
1580 * INITLVL is a symbolic link, so just truncate the file.
1582 close(open(INITLVL
, O_WRONLY
|O_TRUNC
));
1585 * Delete INITLVL file.
1592 * Dispose of INITLVL2 file.
1594 if (lstat(INITLVL2
, &st
) >= 0 && S_ISLNK(st
.st_mode
)) {
1596 * INITLVL2 is a symbolic link, so just truncate the file.
1598 close(open(INITLVL2
, O_WRONLY
|O_TRUNC
));
1601 * Delete INITLVL2 file.
1609 * Walk through the family list and start up children.
1610 * The entries that do not belong here at all are removed
1614 void start_if_needed(void)
1616 CHILD
*ch
; /* Pointer to child */
1617 int delete; /* Delete this entry from list? */
1619 INITDBG(L_VB
, "Checking for children to start");
1621 for(ch
= family
; ch
; ch
= ch
->next
) {
1624 if (ch
->rlevel
[0] == 'C') {
1625 INITDBG(L_VB
, "%s: flags %d", ch
->process
, ch
->flags
);
1629 /* Are we waiting for this process? Then quit here. */
1630 if (ch
->flags
& WAITING
) break;
1632 /* Already running? OK, don't touch it */
1633 if (ch
->flags
& RUNNING
) continue;
1635 /* See if we have to start it up */
1637 if (strchr(ch
->rlevel
, runlevel
) ||
1638 ((ch
->flags
& DEMAND
) && !strchr("#*Ss", runlevel
))) {
1644 /* FIXME: is this OK? */
1645 ch
->flags
&= ~(RUNNING
|WAITING
);
1646 if (!ISPOWER(ch
->action
) && ch
->action
!= KBREQUEST
)
1647 ch
->flags
&= ~XECUTED
;
1650 /* Do we have to wait for this process? */
1651 if (ch
->flags
& WAITING
) break;
1657 * Ask the user on the console for a runlevel
1660 int ask_runlevel(void)
1662 const char prompt
[] = "\nEnter runlevel: ";
1668 fd
= console_open(O_RDWR
|O_NOCTTY
);
1670 if (fd
< 0) return('S');
1672 while(!strchr("0123456789S", lvl
)) {
1673 write(fd
, prompt
, sizeof(prompt
) - 1);
1675 read(fd
, buf
, sizeof(buf
));
1676 if (buf
[0] != 0 && (buf
[1] == '\r' || buf
[1] == '\n'))
1678 if (islower(lvl
)) lvl
= toupper(lvl
);
1685 * Search the INITTAB file for the 'initdefault' field, with the default
1686 * runlevel. If this fails, ask the user to supply a runlevel.
1689 int get_init_default(void)
1696 * Look for initdefault.
1698 for(ch
= family
; ch
; ch
= ch
->next
)
1699 if (ch
->action
== INITDEFAULT
) {
1702 if (*p
> lvl
) lvl
= *p
;
1708 * See if level is valid
1711 if (islower(lvl
)) lvl
= toupper(lvl
);
1712 if (strchr("0123456789S", lvl
) == NULL
) {
1714 "Initdefault level '%c' is invalid", lvl
);
1719 * Ask for runlevel on console if needed.
1721 if (lvl
<= 0) lvl
= ask_runlevel();
1724 * Log the fact that we have a runlevel now.
1733 * Do actions for the new level. If we are compatible with
1734 * the "old" INITLVL and arg == 0, try to read the new
1735 * runlevel from that file first.
1738 int read_level(int arg
)
1740 CHILD
*ch
; /* Walk through list */
1741 unsigned char foo
= 'X'; /* Contents of INITLVL */
1756 if (stat(INITLVL
, &stt
) != 0 || stt
.st_size
!= 0L)
1757 fp
= fopen(INITLVL
, "r");
1760 (stat(INITLVL2
, &stt
) != 0 || stt
.st_size
!= 0L))
1761 fp
= fopen(INITLVL2
, "r");
1764 /* INITLVL file empty or not there - act as 'init q' */
1765 initlog(L_SY
, "Re-reading inittab");
1768 ok
= fscanf(fp
, "%c %d", &foo
, &st
);
1771 /* We go to the new runlevel passed as an argument. */
1775 if (ok
== 2) sltime
= st
;
1777 #endif /* INITLVL */
1779 if (islower(foo
)) foo
= toupper(foo
);
1780 if (ok
< 1 || ok
> 2 || strchr("QS0123456789ABCU", foo
) == NULL
) {
1781 initlog(L_VB
, "bad runlevel: %c", foo
);
1785 /* Log this action */
1788 initlog(L_VB
, "Going single user");
1791 initlog(L_SY
, "Re-reading inittab");
1797 "Activating demand-procedures for '%c'", foo
);
1800 initlog(L_SY
, "Trying to re-exec init");
1803 initlog(L_VB
, "Switching to runlevel: %c", foo
);
1807 #if defined(SIGINT_ONLYONCE) && (SIGINT_ONLYONCE == 1)
1808 /* Re-enable signal from keyboard */
1809 struct sigaction sa
;
1810 SETSIG(sa
, SIGINT
, signal_handler
, 0);
1815 /* Check if this is a runlevel a, b or c */
1816 if (strchr("ABC", foo
)) {
1817 if (runlevel
== 'S') return(runlevel
);
1819 /* Read inittab again first! */
1822 /* Mark those special tasks */
1823 for(ch
= family
; ch
; ch
= ch
->next
)
1824 if (strchr(ch
->rlevel
, foo
) != NULL
||
1825 strchr(ch
->rlevel
, tolower(foo
)) != NULL
) {
1826 ch
->flags
|= DEMAND
;
1827 ch
->flags
&= ~XECUTED
;
1829 "Marking (%s) as ondemand, flags %d",
1835 /* Store both the old and the new runlevel. */
1836 wrote_utmp_rlevel
= 0;
1837 wrote_wtmp_rlevel
= 0;
1838 write_utmp_wtmp("runlevel", "~~", foo
+ 256*runlevel
, RUN_LVL
, "~");
1840 prevlevel
= runlevel
;
1846 * This procedure is called after every signal (SIGHUP, SIGALRM..)
1848 * Only clear the 'failing' flag if the process is sleeping
1849 * longer than 5 minutes, or inittab was read again due
1850 * to user interaction.
1853 void fail_check(void)
1855 CHILD
*ch
; /* Pointer to child structure */
1856 time_t t
; /* System time */
1857 time_t next_alarm
= 0; /* When to set next alarm */
1861 for(ch
= family
; ch
; ch
= ch
->next
) {
1863 if (ch
->flags
& FAILING
) {
1864 /* Can we free this sucker? */
1865 if (ch
->tm
+ SLEEPTIME
< t
) {
1866 ch
->flags
&= ~FAILING
;
1870 /* No, we'll look again later */
1871 if (next_alarm
== 0 ||
1872 ch
->tm
+ SLEEPTIME
> next_alarm
)
1873 next_alarm
= ch
->tm
+ SLEEPTIME
;
1879 if (next_alarm
< 1) next_alarm
= 1;
1884 /* Set all 'Fail' timers to 0 */
1886 void fail_cancel(void)
1890 for(ch
= family
; ch
; ch
= ch
->next
) {
1893 ch
->flags
&= ~FAILING
;
1898 * Start up powerfail entries.
1901 void do_power_fail(int pwrstat
)
1906 * Tell powerwait & powerfail entries to start up
1908 for (ch
= family
; ch
; ch
= ch
->next
) {
1909 if (pwrstat
== 'O') {
1911 * The power is OK again.
1913 if (ch
->action
== POWEROKWAIT
)
1914 ch
->flags
&= ~XECUTED
;
1915 } else if (pwrstat
== 'L') {
1917 * Low battery, shut down now.
1919 if (ch
->action
== POWERFAILNOW
)
1920 ch
->flags
&= ~XECUTED
;
1923 * Power is failing, shutdown imminent
1925 if (ch
->action
== POWERFAIL
|| ch
->action
== POWERWAIT
)
1926 ch
->flags
&= ~XECUTED
;
1932 * Check for state-pipe presence
1935 int check_pipe(int fd
)
1943 t
.tv_sec
= t
.tv_usec
= 0;
1945 if (select(fd
+1, &s
, NULL
, NULL
, &t
) != 1)
1947 if (read(fd
, signature
, 8) != 8)
1949 return strncmp(Signature
, signature
, 8) == 0;
1953 * Make a state-pipe.
1956 int make_pipe(int fd
)
1963 fcntl(fds
[1], F_SETFD
, 1);
1964 fcntl(fd
, F_SETFD
, 0);
1965 write(fds
[1], Signature
, 8);
1971 * Attempt to re-exec.
1977 sigset_t mask
, oldset
;
1982 if (strchr("S0123456",runlevel
) == NULL
)
1986 * Reset the alarm, and block all signals.
1990 sigprocmask(SIG_BLOCK
, &mask
, &oldset
);
1993 * construct a pipe fd --> STATE_PIPE and write a signature
1995 fd
= make_pipe(STATE_PIPE
);
1998 * It's a backup day today, so I'm pissed off. Being a BOFH, however,
1999 * does have it's advantages...
2004 DELSET(got_signals
, SIGCHLD
);
2005 DELSET(got_signals
, SIGHUP
);
2006 DELSET(got_signals
, SIGUSR1
);
2009 * That should be cleaned.
2011 for(ch
= family
; ch
; ch
= ch
->next
)
2012 if (ch
->flags
& ZOMBIE
) {
2013 INITDBG(L_VB
, "Child died, PID= %d", ch
->pid
);
2014 ch
->flags
&= ~(RUNNING
|ZOMBIE
|WAITING
);
2015 if (ch
->process
[0] != '+')
2016 write_utmp_wtmp("", ch
->id
, ch
->pid
, DEAD_PROCESS
, NULL
);
2019 if ((pid
= fork()) == 0) {
2021 * Child sends state information to the parent.
2028 * The existing init process execs a new init binary.
2030 env
= init_buildenv(0);
2031 execle(myname
, myname
, "--init", NULL
, env
);
2034 * We shouldn't be here, something failed.
2035 * Bitch, close the state pipe, unblock signals and return.
2039 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
2041 initlog(L_CO
, "Attempt to re-exec failed");
2045 * Redo utmp/wtmp entries if required or requested
2046 * Check for written records and size of utmp
2049 void redo_utmp_wtmp(void)
2052 const int ret
= stat(UTMP_FILE
, &ustat
);
2054 if ((ret
< 0) || (ustat
.st_size
== 0))
2055 wrote_utmp_rlevel
= wrote_utmp_reboot
= 0;
2057 if ((wrote_wtmp_reboot
== 0) || (wrote_utmp_reboot
== 0))
2058 write_utmp_wtmp("reboot", "~~", 0, BOOT_TIME
, "~");
2060 if ((wrote_wtmp_rlevel
== 0) || (wrote_wtmp_rlevel
== 0))
2061 write_utmp_wtmp("runlevel", "~~", thislevel
+ 256 * prevlevel
, RUN_LVL
, "~");
2065 * We got a change runlevel request through the
2066 * init.fifo. Process it.
2069 void fifo_new_level(int level
)
2076 if (level
== runlevel
) return;
2079 /* Are we waiting for a child? */
2080 for(ch
= family
; ch
; ch
= ch
->next
)
2081 if (ch
->flags
& WAITING
) break;
2085 /* We need to go into a new runlevel */
2086 oldlevel
= runlevel
;
2087 runlevel
= read_level(level
);
2088 if (runlevel
== 'U') {
2089 runlevel
= oldlevel
;
2092 if (oldlevel
!= 'S' && runlevel
== 'S') console_stty();
2093 if (runlevel
== '6' || runlevel
== '0' ||
2094 runlevel
== '1') console_stty();
2095 if (runlevel
> '1' && runlevel
< '6') redo_utmp_wtmp();
2098 setproctitle("init [%c]", (int)runlevel
);
2105 * Set/unset environment variables. The variables are
2106 * encoded as KEY=VAL\0KEY=VAL\0\0. With "=VAL" it means
2107 * setenv, without it means unsetenv.
2110 void initcmd_setenv(char *data
, int size
)
2112 char *env
, *p
, *e
, *eq
;
2117 while (*data
&& data
< e
) {
2119 for (p
= data
; *p
&& p
< e
; p
++)
2120 if (*p
== '=') eq
= p
;
2125 sz
= eq
? (eq
- env
) : (p
- env
);
2127 /*initlog(L_SY, "init_setenv: %s, %s, %d", env, eq, sz);*/
2130 * We only allow INIT_* to be set.
2132 if (strncmp(env
, "INIT_", 5) != 0)
2135 /* Free existing vars. */
2136 for (i
= 0; i
< NR_EXTRA_ENV
; i
++) {
2137 if (extra_env
[i
] == NULL
) continue;
2138 if (!strncmp(extra_env
[i
], env
, sz
) &&
2139 extra_env
[i
][sz
] == '=') {
2141 extra_env
[i
] = NULL
;
2145 /* Set new vars if needed. */
2146 if (eq
== NULL
) continue;
2147 for (i
= 0; i
< NR_EXTRA_ENV
; i
++) {
2148 if (extra_env
[i
] == NULL
) {
2149 extra_env
[i
] = istrdup(env
);
2158 * Read from the init FIFO. Processes like telnetd and rlogind can
2159 * ask us to create login processes on their behalf.
2161 * FIXME: this needs to be finished. NOT that it is buggy, but we need
2162 * to add the telnetd/rlogind stuff so people can start using it.
2163 * Maybe move to using an AF_UNIX socket so we can use
2164 * the 2.2 kernel credential stuff to see who we're talking to.
2168 void check_init_fifo(void)
2170 struct init_request request
;
2172 struct stat st
, st2
;
2178 * First, try to create /dev/initctl if not present.
2180 if (stat(INIT_FIFO
, &st2
) < 0 && errno
== ENOENT
)
2181 (void)mkfifo(INIT_FIFO
, 0600);
2184 * If /dev/initctl is open, stat the file to see if it
2185 * is still the _same_ inode.
2188 fstat(pipe_fd
, &st
);
2189 if (stat(INIT_FIFO
, &st2
) < 0 ||
2190 st
.st_dev
!= st2
.st_dev
||
2191 st
.st_ino
!= st2
.st_ino
) {
2198 * Now finally try to open /dev/initctl
2201 if ((pipe_fd
= open(INIT_FIFO
, O_RDWR
|O_NONBLOCK
)) >= 0) {
2202 fstat(pipe_fd
, &st
);
2203 if (!S_ISFIFO(st
.st_mode
)) {
2204 initlog(L_VB
, "%s is not a fifo", INIT_FIFO
);
2211 * Don't use fd's 0, 1 or 2.
2213 (void) dup2(pipe_fd
, PIPE_FD
);
2218 * Return to caller - we'll be back later.
2223 /* Wait for data to appear, _if_ the pipe was opened. */
2224 if (pipe_fd
>= 0) while(!quit
) {
2226 /* Do select, return on EINTR. */
2228 FD_SET(pipe_fd
, &fds
);
2231 n
= select(pipe_fd
+ 1, &fds
, NULL
, NULL
, &tv
);
2233 if (n
== 0 || errno
== EINTR
) return;
2237 /* Read the data, return on EINTR. */
2238 n
= read(pipe_fd
, &request
, sizeof(request
));
2241 * End of file. This can't happen under Linux (because
2242 * the pipe is opened O_RDWR - see select() in the
2243 * kernel) but you never know...
2250 if (errno
== EINTR
) return;
2251 initlog(L_VB
, "error reading initrequest");
2256 * This is a convenient point to also try to
2257 * find the console device or check if it changed.
2264 if (request
.magic
!= INIT_MAGIC
|| n
!= sizeof(request
)) {
2265 initlog(L_VB
, "got bogus initrequest");
2268 switch(request
.cmd
) {
2269 case INIT_CMD_RUNLVL
:
2270 sltime
= request
.sleeptime
;
2271 fifo_new_level(request
.runlevel
);
2274 case INIT_CMD_POWERFAIL
:
2275 sltime
= request
.sleeptime
;
2279 case INIT_CMD_POWERFAILNOW
:
2280 sltime
= request
.sleeptime
;
2284 case INIT_CMD_POWEROK
:
2285 sltime
= request
.sleeptime
;
2289 case INIT_CMD_SETENV
:
2290 initcmd_setenv(request
.i
.data
, sizeof(request
.i
.data
));
2293 initlog(L_VB
, "got unimplemented initrequest.");
2299 * We come here if the pipe couldn't be opened.
2301 if (pipe_fd
< 0) pause();
2307 * This function is used in the transition
2308 * sysinit (-> single user) boot -> multi-user.
2311 void boot_transitions()
2314 static int newlevel
= 0;
2315 static int warn
= 1;
2319 /* Check if there is something to wait for! */
2320 for( ch
= family
; ch
; ch
= ch
->next
)
2321 if ((ch
->flags
& RUNNING
) && ch
->action
!= BOOT
) break;
2324 /* No processes left in this level, proceed to next level. */
2328 case '#': /* SYSINIT -> BOOT */
2329 INITDBG(L_VB
, "SYSINIT -> BOOT");
2331 /* Write a boot record. */
2332 wrote_utmp_reboot
= 0;
2333 wrote_wtmp_reboot
= 0;
2334 write_utmp_wtmp("reboot", "~~", 0, BOOT_TIME
, "~");
2336 /* Get our run level */
2337 newlevel
= dfl_level
? dfl_level
: get_init_default();
2338 if (newlevel
== 'S') {
2339 runlevel
= newlevel
;
2340 /* Not really 'S' but show anyway. */
2341 setproctitle("init [S]");
2345 case '*': /* BOOT -> NORMAL */
2346 INITDBG(L_VB
, "BOOT -> NORMAL");
2347 if (runlevel
!= newlevel
)
2348 loglevel
= newlevel
;
2349 runlevel
= newlevel
;
2353 case 'S': /* Ended SU mode */
2355 INITDBG(L_VB
, "END SU MODE");
2356 newlevel
= get_init_default();
2357 if (!did_boot
&& newlevel
!= 'S')
2360 if (runlevel
!= newlevel
)
2361 loglevel
= newlevel
;
2362 runlevel
= newlevel
;
2366 for(ch
= family
; ch
; ch
= ch
->next
)
2367 if (strcmp(ch
->rlevel
, "S") == 0)
2368 ch
->flags
&= ~(FAILING
|WAITING
|XECUTED
);
2373 "no more processes left in this runlevel");
2376 if (got_signals
== 0)
2381 initlog(L_VB
, "Entering runlevel: %c", runlevel
);
2382 wrote_utmp_rlevel
= 0;
2383 wrote_wtmp_rlevel
= 0;
2384 write_utmp_wtmp("runlevel", "~~", runlevel
+ 256 * oldlevel
, RUN_LVL
, "~");
2385 thislevel
= runlevel
;
2386 prevlevel
= oldlevel
;
2387 setproctitle("init [%c]", (int)runlevel
);
2393 * Init got hit by a signal. See which signal it is,
2394 * and act accordingly.
2397 void process_signals()
2405 if (ISMEMBER(got_signals
, SIGPWR
)) {
2406 INITDBG(L_VB
, "got SIGPWR");
2407 /* See _what_ kind of SIGPWR this is. */
2409 if ((fd
= open(PWRSTAT
, O_RDONLY
)) >= 0) {
2415 } else if ((fd
= open(PWRSTAT_OLD
, O_RDONLY
)) >= 0) {
2416 /* Path changed 2010-03-20. Look for the old path for a while. */
2417 initlog(L_VB
, "warning: found obsolete path %s, use %s instead",
2418 PWRSTAT_OLD
, PWRSTAT
);
2423 unlink(PWRSTAT_OLD
);
2425 do_power_fail(pwrstat
);
2426 DELSET(got_signals
, SIGPWR
);
2429 if (ISMEMBER(got_signals
, SIGINT
)) {
2430 #if defined(SIGINT_ONLYONCE) && (SIGINT_ONLYONCE == 1)
2431 /* Ignore any further signal from keyboard */
2432 struct sigaction sa
;
2433 SETSIG(sa
, SIGINT
, SIG_IGN
, SA_RESTART
);
2435 INITDBG(L_VB
, "got SIGINT");
2436 /* Tell ctrlaltdel entry to start up */
2437 for(ch
= family
; ch
; ch
= ch
->next
)
2438 if (ch
->action
== CTRLALTDEL
)
2439 ch
->flags
&= ~XECUTED
;
2440 DELSET(got_signals
, SIGINT
);
2443 if (ISMEMBER(got_signals
, SIGWINCH
)) {
2444 INITDBG(L_VB
, "got SIGWINCH");
2445 /* Tell kbrequest entry to start up */
2446 for(ch
= family
; ch
; ch
= ch
->next
)
2447 if (ch
->action
== KBREQUEST
)
2448 ch
->flags
&= ~XECUTED
;
2449 DELSET(got_signals
, SIGWINCH
);
2452 if (ISMEMBER(got_signals
, SIGALRM
)) {
2453 INITDBG(L_VB
, "got SIGALRM");
2454 /* The timer went off: check it out */
2455 DELSET(got_signals
, SIGALRM
);
2458 if (ISMEMBER(got_signals
, SIGCHLD
)) {
2459 INITDBG(L_VB
, "got SIGCHLD");
2460 /* First set flag to 0 */
2461 DELSET(got_signals
, SIGCHLD
);
2463 /* See which child this was */
2464 for(ch
= family
; ch
; ch
= ch
->next
)
2465 if (ch
->flags
& ZOMBIE
) {
2466 INITDBG(L_VB
, "Child died, PID= %d", ch
->pid
);
2467 ch
->flags
&= ~(RUNNING
|ZOMBIE
|WAITING
);
2468 if (ch
->process
[0] != '+')
2469 write_utmp_wtmp("", ch
->id
, ch
->pid
, DEAD_PROCESS
, NULL
);
2474 if (ISMEMBER(got_signals
, SIGHUP
)) {
2475 INITDBG(L_VB
, "got SIGHUP");
2477 /* Are we waiting for a child? */
2478 for(ch
= family
; ch
; ch
= ch
->next
)
2479 if (ch
->flags
& WAITING
) break;
2483 /* We need to go into a new runlevel */
2484 oldlevel
= runlevel
;
2486 runlevel
= read_level(0);
2488 if (runlevel
== 'U') {
2489 runlevel
= oldlevel
;
2492 if (oldlevel
!= 'S' && runlevel
== 'S') console_stty();
2493 if (runlevel
== '6' || runlevel
== '0' ||
2494 runlevel
== '1') console_stty();
2497 setproctitle("init [%c]", (int)runlevel
);
2498 DELSET(got_signals
, SIGHUP
);
2502 if (ISMEMBER(got_signals
, SIGUSR1
)) {
2504 * SIGUSR1 means close and reopen /dev/initctl
2506 INITDBG(L_VB
, "got SIGUSR1");
2509 DELSET(got_signals
, SIGUSR1
);
2517 void init_main(void)
2520 struct sigaction sa
;
2528 * Fork so we can debug the init process.
2530 if ((f
= fork()) > 0) {
2531 static const char killmsg
[] = "PRNT: init killed.\r\n";
2534 while((rc
= wait(&st
)) != f
)
2535 if (rc
< 0 && errno
== ECHILD
)
2537 write(1, killmsg
, sizeof(killmsg
) - 1);
2544 * Tell the kernel to send us SIGINT when CTRL-ALT-DEL
2545 * is pressed, and that we want to handle keyboard signals.
2547 init_reboot(BMAGIC_SOFT
);
2548 if ((f
= open(VT_MASTER
, O_RDWR
| O_NOCTTY
)) >= 0) {
2549 (void) ioctl(f
, KDSIGACCEPT
, SIGWINCH
);
2552 (void) ioctl(0, KDSIGACCEPT
, SIGWINCH
);
2556 * Ignore all signals.
2558 for(f
= 1; f
<= NSIG
; f
++)
2559 SETSIG(sa
, f
, SIG_IGN
, SA_RESTART
);
2562 SETSIG(sa
, SIGALRM
, signal_handler
, 0);
2563 SETSIG(sa
, SIGHUP
, signal_handler
, 0);
2564 SETSIG(sa
, SIGINT
, signal_handler
, 0);
2565 SETSIG(sa
, SIGCHLD
, chld_handler
, SA_RESTART
);
2566 SETSIG(sa
, SIGPWR
, signal_handler
, 0);
2567 SETSIG(sa
, SIGWINCH
, signal_handler
, 0);
2568 SETSIG(sa
, SIGUSR1
, signal_handler
, 0);
2569 SETSIG(sa
, SIGSTOP
, stop_handler
, SA_RESTART
);
2570 SETSIG(sa
, SIGTSTP
, stop_handler
, SA_RESTART
);
2571 SETSIG(sa
, SIGCONT
, cont_handler
, SA_RESTART
);
2572 SETSIG(sa
, SIGSEGV
, (void (*)(int))segv_handler
, SA_RESTART
);
2579 /* Close whatever files are open, and reset the console. */
2587 * Set default PATH variable.
2589 setenv("PATH", PATH_DEFAULT
, 1 /* Overwrite */);
2592 * Initialize /var/run/utmp (only works if /var is on
2593 * root and mounted rw)
2595 if ((fd
= open(UTMP_FILE
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) >= 0)
2599 * Say hello to the world
2601 initlog(L_CO
, bootmsg
, "booting");
2604 * See if we have to start an emergency shell.
2608 SETSIG(sa
, SIGCHLD
, SIG_DFL
, SA_RESTART
);
2609 if (spawn(&ch_emerg
, &f
) > 0) {
2610 while((rc
= wait(&st
)) != f
)
2611 if (rc
< 0 && errno
== ECHILD
)
2614 SETSIG(sa
, SIGCHLD
, chld_handler
, SA_RESTART
);
2618 * Start normal boot procedure.
2625 * Restart: unblock signals and let the show go on
2627 initlog(L_CO
, bootmsg
, "reloading");
2629 sigprocmask(SIG_UNBLOCK
, &sgt
, NULL
);
2632 * Set default PATH variable.
2634 setenv("PATH", PATH_DEFAULT
, 0 /* Don't overwrite */);
2640 /* See if we need to make the boot transitions. */
2642 INITDBG(L_VB
, "init_main: waiting..");
2644 /* Check if there are processes to be waited on. */
2645 for(ch
= family
; ch
; ch
= ch
->next
)
2646 if ((ch
->flags
& RUNNING
) && ch
->action
!= BOOT
) break;
2649 /* Wait until we get hit by some signal. */
2650 while (ch
!= NULL
&& got_signals
== 0) {
2651 if (ISMEMBER(got_signals
, SIGHUP
)) {
2652 /* See if there are processes to be waited on. */
2653 for(ch
= family
; ch
; ch
= ch
->next
)
2654 if (ch
->flags
& WAITING
) break;
2656 if (ch
!= NULL
) check_init_fifo();
2658 #else /* CHANGE_WAIT */
2659 if (ch
!= NULL
&& got_signals
== 0) check_init_fifo();
2660 #endif /* CHANGE_WAIT */
2662 /* Check the 'failing' flags */
2665 /* Process any signals. */
2668 /* See what we need to start up (again) */
2675 * Tell the user about the syntax we expect.
2680 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
);
2685 int telinit(char *progname
, int argc
, char **argv
)
2687 #ifdef TELINIT_USES_INITLVL
2690 struct init_request request
;
2691 struct sigaction sa
;
2695 memset(&request
, 0, sizeof(request
));
2696 request
.magic
= INIT_MAGIC
;
2698 while ((f
= getopt(argc
, argv
, "t:e:")) != EOF
) switch(f
) {
2700 sltime
= atoi(optarg
);
2704 env
= request
.i
.data
;
2706 if (env
+ l
+ 2 > request
.i
.data
+ sizeof(request
.i
.data
)) {
2707 fprintf(stderr
, "%s: -e option data "
2708 "too large\n", progname
);
2711 memcpy(env
, optarg
, l
);
2720 if (env
) *env
++ = 0;
2725 request
.cmd
= INIT_CMD_SETENV
;
2727 if (argc
- optind
!= 1 || strlen(argv
[optind
]) != 1)
2729 if (!strchr("0123456789SsQqAaBbCcUu", argv
[optind
][0]))
2731 request
.cmd
= INIT_CMD_RUNLVL
;
2732 request
.runlevel
= env
? 0 : argv
[optind
][0];
2733 request
.sleeptime
= sltime
;
2736 /* Change to the root directory. */
2739 /* Open the fifo and write a command. */
2740 /* Make sure we don't hang on opening /dev/initctl */
2741 SETSIG(sa
, SIGALRM
, signal_handler
, 0);
2743 if ((fd
= open(INIT_FIFO
, O_WRONLY
)) >= 0) {
2745 size_t s
= sizeof(request
);
2746 void *ptr
= &request
;
2749 p
= write(fd
, ptr
, s
);
2751 if (errno
== EINTR
|| errno
== EAGAIN
)
2763 #ifdef TELINIT_USES_INITLVL
2764 if (request
.cmd
== INIT_CMD_RUNLVL
) {
2765 /* Fallthrough to the old method. */
2767 /* Now write the new runlevel. */
2768 if ((fp
= fopen(INITLVL
, "w")) == NULL
) {
2769 fprintf(stderr
, "%s: cannot create %s\n",
2773 fprintf(fp
, "%s %d", argv
[optind
], sltime
);
2776 /* And tell init about the pending runlevel change. */
2777 if (kill(INITPID
, SIGHUP
) < 0) perror(progname
);
2783 fprintf(stderr
, "%s: ", progname
);
2784 if (ISMEMBER(got_signals
, SIGALRM
)) {
2785 fprintf(stderr
, "timeout opening/writing control channel %s\n",
2794 * Main entry for init and telinit.
2796 int main(int argc
, char **argv
)
2805 /* Get my own name */
2806 if ((p
= strrchr(argv
[0], '/')) != NULL
)
2815 if (geteuid() != 0) {
2816 fprintf(stderr
, "%s: must be superuser.\n", p
);
2821 * Is this telinit or init ?
2823 isinit
= (getpid() == 1);
2824 for (f
= 1; f
< argc
; f
++) {
2825 if (!strcmp(argv
[f
], "-i") || !strcmp(argv
[f
], "--init")) {
2830 if (!isinit
) exit(telinit(p
, argc
, argv
));
2835 if (check_pipe(STATE_PIPE
)) {
2837 receive_state(STATE_PIPE
);
2839 myname
= istrdup(argv
[0]);
2842 for (f
= 0; f
< argc
; f
++)
2843 maxproclen
+= strlen(argv
[f
]) + 1;
2845 setproctitle("init [%c]", (int)runlevel
);
2850 /* Check command line arguments */
2851 maxproclen
= strlen(argv
[0]) + 1;
2852 for(f
= 1; f
< argc
; f
++) {
2853 if (!strcmp(argv
[f
], "single") || !strcmp(argv
[f
], "-s"))
2855 else if (!strcmp(argv
[f
], "-a") || !strcmp(argv
[f
], "auto"))
2856 putenv("AUTOBOOT=YES");
2857 else if (!strcmp(argv
[f
], "-b") || !strcmp(argv
[f
],"emergency"))
2859 else if (!strcmp(argv
[f
], "-z")) {
2861 if (argv
[f
+ 1]) f
++;
2862 } else if (strchr("0123456789sS", argv
[f
][0])
2863 && strlen(argv
[f
]) == 1)
2864 dfl_level
= argv
[f
][0];
2865 /* "init u" in the very beginning makes no sense */
2866 if (dfl_level
== 's') dfl_level
= 'S';
2867 maxproclen
+= strlen(argv
[f
]) + 1;
2871 if (getenv("SELINUX_INIT") == NULL
) {
2872 const int rc
= mount("proc", "/proc", "proc", 0, 0);
2873 if (is_selinux_enabled() > 0) {
2874 putenv("SELINUX_INIT=YES");
2875 if (rc
== 0) umount2("/proc", MNT_DETACH
);
2876 if (selinux_init_load_policy(&enforce
) == 0) {
2877 execv(myname
, argv
);
2880 /* SELinux in enforcing mode but load_policy failed */
2881 /* At this point, we probably can't open /dev/console, so log() won't work */
2882 fprintf(stderr
,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.\n");
2887 if (rc
== 0) umount2("/proc", MNT_DETACH
);
2890 /* Start booting. */
2893 setproctitle("init boot");