]>
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 "31-Jul-2004"
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>
63 # define STACK_DEBUG 1
64 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
65 /* Only glibc 2.0 needs this */
66 # include <sigcontext.h>
78 # define SIGPWR SIGUSR2
88 /* Set a signal handler. */
89 #define SETSIG(sa, sig, fun, flags) \
91 sa.sa_handler = fun; \
92 sa.sa_flags = flags; \
93 sigemptyset(&sa.sa_mask); \
94 sigaction(sig, &sa, NULL); \
97 /* Version information */
98 char *Version
= "@(#) init " VERSION
" " DATE
" miquels@cistron.nl";
99 char *bootmsg
= "version " VERSION
" %s";
100 #define E_VERSION "INIT_VERSION=sysvinit-" VERSION
102 CHILD
*family
= NULL
; /* The linked list of all entries */
103 CHILD
*newFamily
= NULL
; /* The list after inittab re-read */
105 CHILD ch_emerg
= { /* Emergency shell */
115 char runlevel
= 'S'; /* The current run level */
116 char thislevel
= 'S'; /* The current runlevel */
117 char prevlevel
= 'N'; /* Previous runlevel */
118 int dfl_level
= 0; /* Default runlevel */
119 sig_atomic_t got_cont
= 0; /* Set if we received the SIGCONT signal */
120 sig_atomic_t got_signals
; /* Set if we received a signal. */
121 int emerg_shell
= 0; /* Start emergency shell? */
122 int wrote_wtmp_reboot
= 1; /* Set when we wrote the reboot record */
123 int wrote_utmp_reboot
= 1; /* Set when we wrote the reboot record */
124 int sltime
= 5; /* Sleep time between TERM and KILL */
125 char *argv0
; /* First arguments; show up in ps listing */
126 int maxproclen
; /* Maximal length of argv[0] with \0 */
127 struct utmp utproto
; /* Only used for sizeof(utproto.ut_id) */
128 char *user_console
= NULL
; /* User console device */
129 char *console_dev
; /* Console device. */
130 int pipe_fd
= -1; /* /dev/initctl */
131 int did_boot
= 0; /* Did we already do BOOT* stuff? */
132 int main(int, char **);
134 /* Used by re-exec part */
135 int reload
= 0; /* Should we do initialization stuff? */
136 char *myname
="/sbin/init"; /* What should we exec */
137 int oops_error
; /* Used by some of the re-exec code. */
138 const char *Signature
= "12567362"; /* Signature for re-exec fd */
140 /* Macro to see if this is a special action */
141 #define ISPOWER(i) ((i) == POWERWAIT || (i) == POWERFAIL || \
142 (i) == POWEROKWAIT || (i) == POWERFAILNOW || \
145 /* ascii values for the `action' field. */
150 { "respawn", RESPAWN
},
154 { "bootwait", BOOTWAIT
},
155 { "powerfail", POWERFAIL
},
156 { "powerfailnow",POWERFAILNOW
},
157 { "powerwait", POWERWAIT
},
158 { "powerokwait", POWEROKWAIT
},
159 { "ctrlaltdel", CTRLALTDEL
},
161 { "ondemand", ONDEMAND
},
162 { "initdefault", INITDEFAULT
},
163 { "sysinit", SYSINIT
},
164 { "kbrequest", KBREQUEST
},
169 * State parser token table (see receive_state)
182 { "CMD", C_PROCESS
},
185 { "-RL", D_RUNLEVEL
},
186 { "-TL", D_THISLEVEL
},
187 { "-PL", D_PREVLEVEL
},
188 { "-SI", D_GOTSIGN
},
189 { "-WR", D_WROTE_WTMP_REBOOT
},
190 { "-WU", D_WROTE_UTMP_REBOOT
},
192 { "-DB", D_DIDBOOT
},
206 #define NR_EXTRA_ENV 16
207 char *extra_env
[NR_EXTRA_ENV
];
211 * Sleep a number of seconds.
213 * This only works correctly because the linux select updates
214 * the elapsed time in the struct timeval passed to select!
216 void do_sleep(int sec
)
223 while(select(0, NULL
, NULL
, NULL
, &tv
) < 0 && errno
== EINTR
)
229 * Non-failing allocation routines (init cannot fail).
231 void *imalloc(size_t size
)
235 while ((m
= malloc(size
)) == NULL
) {
236 initlog(L_VB
, "out of memory");
244 char *istrdup(char *s
)
257 * Send the state info of the previous running init to
258 * the new one, in a version-independant way.
260 void send_state(int fd
)
268 fprintf(fp
, "VER%s\n", Version
);
269 fprintf(fp
, "-RL%c\n", runlevel
);
270 fprintf(fp
, "-TL%c\n", thislevel
);
271 fprintf(fp
, "-PL%c\n", prevlevel
);
272 fprintf(fp
, "-SI%u\n", got_signals
);
273 fprintf(fp
, "-WR%d\n", wrote_wtmp_reboot
);
274 fprintf(fp
, "-WU%d\n", wrote_utmp_reboot
);
275 fprintf(fp
, "-ST%d\n", sltime
);
276 fprintf(fp
, "-DB%d\n", did_boot
);
278 for (p
= family
; p
; p
= p
->next
) {
279 fprintf(fp
, "REC%s\n", p
->id
);
280 fprintf(fp
, "LEV%s\n", p
->rlevel
);
281 for (i
= 0, val
= p
->flags
; flags
[i
].mask
; i
++)
282 if (val
& flags
[i
].mask
) {
283 val
&= ~flags
[i
].mask
;
284 fprintf(fp
, "FL %s\n",flags
[i
].name
);
286 fprintf(fp
, "PID%d\n",p
->pid
);
287 fprintf(fp
, "EXS%u\n",p
->exstat
);
288 for(i
= 0; actions
[i
].act
; i
++)
289 if (actions
[i
].act
== p
->action
) {
290 fprintf(fp
, "AC %s\n", actions
[i
].name
);
293 fprintf(fp
, "CMD%s\n", p
->process
);
294 fprintf(fp
, "EOR\n");
296 fprintf(fp
, "END\n");
301 * Read a string from a file descriptor.
302 * FIXME: why not use fgets() ?
304 static int get_string(char *p
, int size
, FILE *f
)
308 while ((c
= getc(f
)) != EOF
&& c
!= '\n') {
313 return (c
!= EOF
) && (size
> 0);
317 * Read trailing data from the state pipe until we see a newline.
319 static int get_void(FILE *f
)
323 while ((c
= getc(f
)) != EOF
&& c
!= '\n')
330 * Read the next "command" from the state pipe.
332 static int get_cmd(FILE *f
)
337 if (fread(cmd
, 1, sizeof(cmd
) - 1, f
) != sizeof(cmd
) - 1)
340 for(i
= 0; cmds
[i
].cmd
&& strcmp(cmds
[i
].name
, cmd
) != 0; i
++)
346 * Read a CHILD * from the state pipe.
348 static CHILD
*get_record(FILE *f
)
356 switch (cmd
= get_cmd(f
)) {
366 fscanf(f
, "%c\n", &runlevel
);
369 fscanf(f
, "%c\n", &thislevel
);
372 fscanf(f
, "%c\n", &prevlevel
);
375 fscanf(f
, "%u\n", &got_signals
);
377 case D_WROTE_WTMP_REBOOT
:
378 fscanf(f
, "%d\n", &wrote_wtmp_reboot
);
380 case D_WROTE_UTMP_REBOOT
:
381 fscanf(f
, "%d\n", &wrote_utmp_reboot
);
384 fscanf(f
, "%d\n", &sltime
);
387 fscanf(f
, "%d\n", &did_boot
);
390 if (cmd
> 0 || cmd
== C_EOF
) {
395 } while (cmd
!= C_REC
);
397 p
= imalloc(sizeof(CHILD
));
398 get_string(p
->id
, sizeof(p
->id
), f
);
400 do switch(cmd
= get_cmd(f
)) {
406 fscanf(f
, "%d\n", &(p
->pid
));
409 fscanf(f
, "%u\n", &(p
->exstat
));
412 get_string(p
->rlevel
, sizeof(p
->rlevel
), f
);
415 get_string(p
->process
, sizeof(p
->process
), f
);
418 get_string(s
, sizeof(s
), f
);
419 for(i
= 0; flags
[i
].name
; i
++) {
420 if (strcmp(flags
[i
].name
,s
) == 0)
423 p
->flags
|= flags
[i
].mask
;
426 get_string(s
, sizeof(s
), f
);
427 for(i
= 0; actions
[i
].name
; i
++) {
428 if (strcmp(actions
[i
].name
, s
) == 0)
431 p
->action
= actions
[i
].act
? actions
[i
].act
: OFF
;
437 } while( cmd
!= C_EOR
);
443 * Read the complete state info from the state pipe.
444 * Returns 0 on success
446 int receive_state(int fd
)
449 char old_version
[256];
454 if (get_cmd(f
) != C_VER
)
456 get_string(old_version
, sizeof(old_version
), f
);
458 for (pp
= &family
; (*pp
= get_record(f
)) != NULL
; pp
= &((*pp
)->next
))
465 * Set the process title.
468 __attribute__ ((format (printf
, 1, 2)))
470 static int setproctitle(char *fmt
, ...)
479 len
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
482 if (maxproclen
> 1) {
483 memset(argv0
, 0, maxproclen
);
484 strncpy(argv0
, buf
, maxproclen
- 1);
491 * Set console_dev to a working console.
493 void console_init(void)
496 int tried_devcons
= 0;
497 int tried_vtmaster
= 0;
501 console_dev
= user_console
;
502 } else if ((s
= getenv("CONSOLE")) != NULL
)
505 console_dev
= CONSOLE
;
509 while ((fd
= open(console_dev
, O_RDONLY
|O_NONBLOCK
)) < 0) {
510 if (!tried_devcons
) {
512 console_dev
= CONSOLE
;
515 if (!tried_vtmaster
) {
517 console_dev
= VT_MASTER
;
523 console_dev
= "/dev/null";
530 * Open the console with retries.
532 int console_open(int mode
)
538 * Open device in nonblocking mode.
540 m
= mode
| O_NONBLOCK
;
543 * Retry the open five times.
545 for(f
= 0; f
< 5; f
++) {
546 if ((fd
= open(console_dev
, m
)) >= 0) break;
550 if (fd
< 0) return fd
;
553 * Set original flags.
556 fcntl(fd
, F_SETFL
, mode
);
561 * We got a signal (HUP PWR WINCH ALRM INT)
563 void signal_handler(int sig
)
565 ADDSET(got_signals
, sig
);
569 * SIGCHLD: one of our children has died.
575 int saved_errno
= errno
;
578 * Find out which process(es) this was (were)
580 while((pid
= waitpid(-1, &st
, WNOHANG
)) != 0) {
581 if (errno
== ECHILD
) break;
582 for( ch
= family
; ch
; ch
= ch
->next
)
583 if ( ch
->pid
== pid
&& (ch
->flags
& RUNNING
) ) {
585 "chld_handler: marked %d as zombie",
587 ADDSET(got_signals
, SIGCHLD
);
591 ch
->new->exstat
= st
;
592 ch
->new->flags
|= ZOMBIE
;
597 INITDBG(L_VB
, "chld_handler: unknown child %d exited.",
605 * Linux ignores all signals sent to init when the
606 * SIG_DFL handler is installed. Therefore we must catch SIGTSTP
607 * and SIGCONT, or else they won't work....
609 * The SIGCONT handler
617 * Fork and dump core in /.
621 static int dumped
= 0;
628 if (fork() != 0) return;
631 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
633 rlim
.rlim_cur
= RLIM_INFINITY
;
634 rlim
.rlim_max
= RLIM_INFINITY
;
635 setrlimit(RLIMIT_CORE
, &rlim
);
638 signal(SIGSEGV
, SIG_DFL
);
640 sigdelset(&mask
, SIGSEGV
);
641 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
648 * OOPS: segmentation violation!
649 * If we have the info, print where it occured.
650 * Then sleep 30 seconds and try to continue.
652 #if defined(STACK_DEBUG) && defined(__linux__)
653 void segv_handler(int sig
, struct sigcontext ctx
)
656 int saved_errno
= errno
;
658 if ((void *)ctx
.eip
>= (void *)do_sleep
&&
659 (void *)ctx
.eip
< (void *)main
)
661 initlog(L_VB
, "PANIC: segmentation violation at %p%s! "
662 "sleeping for 30 seconds.", (void *)ctx
.eip
, p
);
670 int saved_errno
= errno
;
673 "PANIC: segmentation violation! sleeping for 30 seconds.");
681 * The SIGSTOP & SIGTSTP handler
685 int saved_errno
= errno
;
688 while(!got_cont
) pause();
694 * Set terminal settings to reasonable defaults
696 void console_stty(void)
701 if ((fd
= console_open(O_RDWR
|O_NOCTTY
)) < 0) {
702 initlog(L_VB
, "can't open %s", console_dev
);
706 (void) tcgetattr(fd
, &tty
);
708 tty
.c_cflag
&= CBAUD
|CBAUDEX
|CSIZE
|CSTOPB
|PARENB
|PARODD
;
709 tty
.c_cflag
|= HUPCL
|CLOCAL
|CREAD
;
711 tty
.c_cc
[VINTR
] = 3; /* ctrl('c') */
712 tty
.c_cc
[VQUIT
] = 28; /* ctrl('\\') */
713 tty
.c_cc
[VERASE
] = 127;
714 tty
.c_cc
[VKILL
] = 24; /* ctrl('x') */
715 tty
.c_cc
[VEOF
] = 4; /* ctrl('d') */
718 tty
.c_cc
[VSTART
] = 17; /* ctrl('q') */
719 tty
.c_cc
[VSTOP
] = 19; /* ctrl('s') */
720 tty
.c_cc
[VSUSP
] = 26; /* ctrl('z') */
723 * Set pre and post processing
725 tty
.c_iflag
= IGNPAR
|ICRNL
|IXON
|IXANY
;
726 tty
.c_oflag
= OPOST
|ONLCR
;
727 tty
.c_lflag
= ISIG
|ICANON
|ECHO
|ECHOCTL
|ECHOPRT
|ECHOKE
;
730 * Now set the terminal line.
731 * We don't care about non-transmitted output data
732 * and non-read input data.
734 (void) tcsetattr(fd
, TCSANOW
, &tty
);
735 (void) tcflush(fd
, TCIOFLUSH
);
740 * Print to the system console
746 if ((fd
= console_open(O_WRONLY
|O_NOCTTY
|O_NDELAY
)) >= 0) {
747 write(fd
, s
, strlen(s
));
753 * Log something to a logfile and the console.
756 __attribute__ ((format (printf
, 2, 3)))
758 void initlog(int loglevel
, char *s
, ...)
762 sigset_t nmask
, omask
;
764 va_start(va_alist
, s
);
765 vsnprintf(buf
, sizeof(buf
), s
, va_alist
);
768 if (loglevel
& L_SY
) {
770 * Re-establish connection with syslogd every time.
771 * Block signals while talking to syslog.
774 sigprocmask(SIG_BLOCK
, &nmask
, &omask
);
775 openlog("init", 0, LOG_DAEMON
);
776 syslog(LOG_INFO
, "%s", buf
);
778 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
782 * And log to the console.
784 if (loglevel
& L_CO
) {
793 * Build a new environment for execve().
795 char **init_buildenv(int child
)
797 char i_lvl
[] = "RUNLEVEL=x";
798 char i_prev
[] = "PREVLEVEL=x";
803 for (n
= 0; environ
[n
]; n
++)
805 n
+= NR_EXTRA_ENV
+ 8;
806 e
= calloc(n
, sizeof(char *));
808 for (n
= 0; environ
[n
]; n
++)
809 e
[n
] = istrdup(environ
[n
]);
811 for (i
= 0; i
< NR_EXTRA_ENV
; i
++)
813 e
[n
++] = istrdup(extra_env
[i
]);
816 snprintf(i_cons
, sizeof(i_cons
), "CONSOLE=%s", console_dev
);
817 i_lvl
[9] = thislevel
;
818 i_prev
[10] = prevlevel
;
819 e
[n
++] = istrdup(i_lvl
);
820 e
[n
++] = istrdup(i_prev
);
821 e
[n
++] = istrdup(i_cons
);
822 e
[n
++] = istrdup(E_VERSION
);
831 void init_freeenv(char **e
)
835 for (n
= 0; e
[n
]; n
++)
844 * This function is too long and indents too deep.
847 int spawn(CHILD
*ch
, int *res
)
849 char *args
[16]; /* Argv array */
850 char buf
[136]; /* Line buffer */
851 int f
, st
, rc
; /* Scratch variables */
852 char *ptr
; /* Ditto */
853 time_t t
; /* System time */
854 int oldAlarm
; /* Previous alarm value */
855 char *proc
= ch
->process
; /* Command line */
856 pid_t pid
, pgrp
; /* child, console process group. */
857 sigset_t nmask
, omask
; /* For blocking SIGCHLD */
861 buf
[sizeof(buf
) - 1] = 0;
863 /* Skip '+' if it's there */
864 if (proc
[0] == '+') proc
++;
866 ch
->flags
|= XECUTED
;
868 if (ch
->action
== RESPAWN
|| ch
->action
== ONDEMAND
) {
869 /* Is the date stamp from less than 2 minutes ago? */
871 if (ch
->tm
+ TESTTIME
> t
) {
878 /* Do we try to respawn too fast? */
879 if (ch
->count
>= MAXSPAWN
) {
882 "Id \"%s\" respawning too fast: disabled for %d minutes",
883 ch
->id
, SLEEPTIME
/ 60);
884 ch
->flags
&= ~RUNNING
;
885 ch
->flags
|= FAILING
;
887 /* Remember the time we stopped */
890 /* Try again in 5 minutes */
892 if (oldAlarm
> SLEEPTIME
|| oldAlarm
<= 0) oldAlarm
= SLEEPTIME
;
898 /* See if there is an "initscript" (except in single user mode). */
899 if (access(INITSCRIPT
, R_OK
) == 0 && runlevel
!= 'S') {
900 /* Build command line using "initscript" */
902 args
[2] = INITSCRIPT
;
904 args
[4] = ch
->rlevel
;
906 for(f
= 0; actions
[f
].name
; f
++) {
907 if (ch
->action
== actions
[f
].act
) {
908 args
[5] = actions
[f
].name
;
914 } else if (strpbrk(proc
, "~`!$^&*()=|\\{}[];\"'<>?")) {
915 /* See if we need to fire off a shell for this command */
916 /* Give command line to shell */
919 strcpy(buf
, "exec ");
920 strncat(buf
, proc
, sizeof(buf
) - strlen(buf
) - 1);
924 /* Split up command line arguments */
926 strncat(buf
, proc
, sizeof(buf
) - 1);
928 for(f
= 1; f
< 15; f
++) {
929 /* Skip white space */
930 while(*ptr
== ' ' || *ptr
== '\t') ptr
++;
933 /* May be trailing space.. */
934 if (*ptr
== 0) break;
936 /* Skip this `word' */
937 while(*ptr
&& *ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '#')
940 /* If end-of-line, break */
941 if (*ptr
== '#' || *ptr
== 0) {
946 /* End word with \0 and continue */
954 * Block sigchild while forking.
957 sigaddset(&nmask
, SIGCHLD
);
958 sigprocmask(SIG_BLOCK
, &nmask
, &omask
);
960 if ((pid
= fork()) == 0) {
965 if (pipe_fd
>= 0) close(pipe_fd
);
967 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
970 * In sysinit, boot, bootwait or single user mode:
971 * for any wait-type subprocess we _force_ the console
972 * to be its controlling tty.
974 if (strchr("*#sS", runlevel
) && ch
->flags
& WAITING
) {
976 * We fork once extra. This is so that we can
977 * wait and change the process group and session
978 * of the console after exit of the leader.
981 if ((f
= console_open(O_RDWR
|O_NOCTTY
)) >= 0) {
982 /* Take over controlling tty by force */
983 (void)ioctl(f
, TIOCSCTTY
, 1);
987 SETSIG(sa
, SIGCHLD
, SIG_DFL
, SA_RESTART
);
988 if ((pid
= fork()) < 0) {
989 initlog(L_VB
, "cannot fork: %s",
995 * Ignore keyboard signals etc.
996 * Then wait for child to exit.
998 SETSIG(sa
, SIGINT
, SIG_IGN
, SA_RESTART
);
999 SETSIG(sa
, SIGTSTP
, SIG_IGN
, SA_RESTART
);
1000 SETSIG(sa
, SIGQUIT
, SIG_IGN
, SA_RESTART
);
1002 while ((rc
= waitpid(pid
, &st
, 0)) != pid
)
1003 if (rc
< 0 && errno
== ECHILD
)
1007 * Small optimization. See if stealing
1008 * controlling tty back is needed.
1010 pgrp
= tcgetpgrp(f
);
1011 if (pgrp
!= getpid())
1015 * Steal controlling tty away. We do
1016 * this with a temporary process.
1018 if ((pid
= fork()) < 0) {
1019 initlog(L_VB
, "cannot fork: %s",
1025 (void)ioctl(f
, TIOCSCTTY
, 1);
1028 while((rc
= waitpid(pid
, &st
, 0)) != pid
)
1029 if (rc
< 0 && errno
== ECHILD
)
1034 /* Set ioctl settings to default ones */
1039 if ((f
= console_open(O_RDWR
|O_NOCTTY
)) < 0) {
1040 initlog(L_VB
, "open(%s): %s", console_dev
,
1042 f
= open("/dev/null", O_RDWR
);
1048 /* Reset all the signals, set up environment */
1049 for(f
= 1; f
< NSIG
; f
++) SETSIG(sa
, f
, SIG_DFL
, SA_RESTART
);
1050 environ
= init_buildenv(1);
1053 * Execute prog. In case of ENOEXEC try again
1054 * as a shell script.
1056 execvp(args
[1], args
+ 1);
1057 if (errno
== ENOEXEC
) {
1060 strcpy(buf
, "exec ");
1061 strncat(buf
, proc
, sizeof(buf
) - strlen(buf
) - 1);
1064 execvp(args
[1], args
+ 1);
1066 initlog(L_VB
, "cannot execute \"%s\"", args
[1]);
1070 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
1072 INITDBG(L_VB
, "Started id %s (pid %d)", ch
->id
, pid
);
1075 initlog(L_VB
, "cannot fork, retry..");
1084 * Start a child running!
1086 void startup(CHILD
*ch
)
1089 * See if it's disabled
1091 if (ch
->flags
& FAILING
) return;
1093 switch(ch
->action
) {
1102 if (!(ch
->flags
& XECUTED
)) ch
->flags
|= WAITING
;
1107 if (ch
->flags
& XECUTED
) break;
1110 ch
->flags
|= RUNNING
;
1111 if (spawn(ch
, &(ch
->pid
)) < 0) break;
1113 * Do NOT log if process field starts with '+'
1114 * FIXME: that's for compatibility with *very*
1115 * old getties - probably it can be taken out.
1117 if (ch
->process
[0] != '+')
1118 write_utmp_wtmp("", ch
->id
, ch
->pid
,
1126 * Read the inittab file.
1128 void read_inittab(void)
1130 FILE *fp
; /* The INITTAB file */
1131 CHILD
*ch
, *old
, *i
; /* Pointers to CHILD structure */
1132 CHILD
*head
= NULL
; /* Head of linked list */
1134 struct stat st
; /* To stat INITLVL */
1136 sigset_t nmask
, omask
; /* For blocking SIGCHLD. */
1137 char buf
[256]; /* Line buffer */
1138 char err
[64]; /* Error message. */
1140 *action
, *process
; /* Fields of a line */
1142 int lineNo
= 0; /* Line number in INITTAB file */
1143 int actionNo
; /* Decoded action field */
1144 int f
; /* Counter */
1145 int round
; /* round 0 for SIGTERM, 1 for SIGKILL */
1146 int foundOne
= 0; /* No killing no sleep */
1147 int talk
; /* Talk to the user */
1148 int done
= 0; /* Ready yet? */
1151 if (newFamily
!= NULL
) {
1152 INITDBG(L_VB
, "PANIC newFamily != NULL");
1155 INITDBG(L_VB
, "Reading inittab");
1159 * Open INITTAB and real line by line.
1161 if ((fp
= fopen(INITTAB
, "r")) == NULL
)
1162 initlog(L_VB
, "No inittab file found");
1166 * Add single user shell entry at the end.
1168 if (fp
== NULL
|| fgets(buf
, sizeof(buf
), fp
) == NULL
) {
1171 * See if we have a single user entry.
1173 for(old
= newFamily
; old
; old
= old
->next
)
1174 if (strpbrk(old
->rlevel
, "S")) break;
1176 snprintf(buf
, sizeof(buf
), "~~:S:wait:%s\n", SULOGIN
);
1182 * Skip comments and empty lines
1184 for(p
= buf
; *p
== ' ' || *p
== '\t'; p
++)
1186 if (*p
== '#' || *p
== '\n') continue;
1191 id
= strsep(&p
, ":");
1192 rlevel
= strsep(&p
, ":");
1193 action
= strsep(&p
, ":");
1194 process
= strsep(&p
, "\n");
1197 * Check if syntax is OK. Be very verbose here, to
1198 * avoid newbie postings on comp.os.linux.setup :)
1201 if (!id
|| !*id
) strcpy(err
, "missing id field");
1202 if (!rlevel
) strcpy(err
, "missing runlevel field");
1203 if (!process
) strcpy(err
, "missing process field");
1204 if (!action
|| !*action
)
1205 strcpy(err
, "missing action field");
1206 if (id
&& strlen(id
) > sizeof(utproto
.ut_id
))
1207 sprintf(err
, "id field too long (max %d characters)",
1208 (int)sizeof(utproto
.ut_id
));
1209 if (rlevel
&& strlen(rlevel
) > 11)
1210 strcpy(err
, "rlevel field too long (max 11 characters)");
1211 if (process
&& strlen(process
) > 127)
1212 strcpy(err
, "process field too long");
1213 if (action
&& strlen(action
) > 32)
1214 strcpy(err
, "action field too long");
1216 initlog(L_VB
, "%s[%d]: %s", INITTAB
, lineNo
, err
);
1217 INITDBG(L_VB
, "%s:%s:%s:%s", id
, rlevel
, action
, process
);
1222 * Decode the "action" field
1225 for(f
= 0; actions
[f
].name
; f
++)
1226 if (strcasecmp(action
, actions
[f
].name
) == 0) {
1227 actionNo
= actions
[f
].act
;
1230 if (actionNo
== -1) {
1231 initlog(L_VB
, "%s[%d]: %s: unknown action field",
1232 INITTAB
, lineNo
, action
);
1237 * See if the id field is unique
1239 for(old
= newFamily
; old
; old
= old
->next
) {
1240 if(strcmp(old
->id
, id
) == 0 && strcmp(id
, "~~")) {
1241 initlog(L_VB
, "%s[%d]: duplicate ID field \"%s\"",
1242 INITTAB
, lineNo
, id
);
1249 * Allocate a CHILD structure
1251 ch
= imalloc(sizeof(CHILD
));
1256 ch
->action
= actionNo
;
1257 strncpy(ch
->id
, id
, sizeof(utproto
.ut_id
) + 1); /* Hack for different libs. */
1258 strncpy(ch
->process
, process
, sizeof(ch
->process
) - 1);
1260 for(f
= 0; f
< sizeof(rlevel
) - 1 && rlevel
[f
]; f
++) {
1261 ch
->rlevel
[f
] = rlevel
[f
];
1262 if (ch
->rlevel
[f
] == 's') ch
->rlevel
[f
] = 'S';
1264 strncpy(ch
->rlevel
, rlevel
, sizeof(ch
->rlevel
) - 1);
1266 strcpy(ch
->rlevel
, "0123456789");
1267 if (ISPOWER(ch
->action
))
1268 strcpy(ch
->rlevel
, "S0123456789");
1271 * We have the fake runlevel '#' for SYSINIT and
1272 * '*' for BOOT and BOOTWAIT.
1274 if (ch
->action
== SYSINIT
) strcpy(ch
->rlevel
, "#");
1275 if (ch
->action
== BOOT
|| ch
->action
== BOOTWAIT
)
1276 strcpy(ch
->rlevel
, "*");
1279 * Now add it to the linked list. Special for powerfail.
1281 if (ISPOWER(ch
->action
)) {
1284 * Disable by default
1286 ch
->flags
|= XECUTED
;
1289 * Tricky: insert at the front of the list..
1292 for(i
= newFamily
; i
; i
= i
->next
) {
1293 if (!ISPOWER(i
->action
)) break;
1297 * Now add after entry "old"
1302 if (i
== NULL
) head
= ch
;
1304 ch
->next
= newFamily
;
1306 if (ch
->next
== NULL
) head
= ch
;
1310 * Just add at end of the list
1312 if (ch
->action
== KBREQUEST
) ch
->flags
|= XECUTED
;
1322 * Walk through the old list comparing id fields
1324 for(old
= family
; old
; old
= old
->next
)
1325 if (strcmp(old
->id
, ch
->id
) == 0) {
1336 * Loop through the list of children, and see if they need to
1340 INITDBG(L_VB
, "Checking for children to kill");
1341 for(round
= 0; round
< 2; round
++) {
1343 for(ch
= family
; ch
; ch
= ch
->next
) {
1344 ch
->flags
&= ~KILLME
;
1347 * Is this line deleted?
1349 if (ch
->new == NULL
) ch
->flags
|= KILLME
;
1352 * If the entry has changed, kill it anyway. Note that
1353 * we do not check ch->process, only the "action" field.
1354 * This way, you can turn an entry "off" immediately, but
1355 * changes in the command line will only become effective
1356 * after the running version has exited.
1358 if (ch
->new && ch
->action
!= ch
->new->action
) ch
->flags
|= KILLME
;
1361 * Only BOOT processes may live in all levels
1363 if (ch
->action
!= BOOT
&&
1364 strchr(ch
->rlevel
, runlevel
) == NULL
) {
1366 * Ondemand procedures live always,
1367 * except in single user
1369 if (runlevel
== 'S' || !(ch
->flags
& DEMAND
))
1370 ch
->flags
|= KILLME
;
1374 * Now, if this process may live note so in the new list
1376 if ((ch
->flags
& KILLME
) == 0) {
1377 ch
->new->flags
= ch
->flags
;
1378 ch
->new->pid
= ch
->pid
;
1379 ch
->new->exstat
= ch
->exstat
;
1385 * Is this process still around?
1387 if ((ch
->flags
& RUNNING
) == 0) {
1388 ch
->flags
&= ~KILLME
;
1391 INITDBG(L_VB
, "Killing \"%s\"", ch
->process
);
1393 case 0: /* Send TERM signal */
1396 "Sending processes the TERM signal");
1397 kill(-(ch
->pid
), SIGTERM
);
1400 case 1: /* Send KILL signal and collect status */
1403 "Sending processes the KILL signal");
1404 kill(-(ch
->pid
), SIGKILL
);
1411 * See if we have to wait 5 seconds
1413 if (foundOne
&& round
== 0) {
1415 * Yup, but check every second if we still have children.
1417 for(f
= 0; f
< sltime
; f
++) {
1418 for(ch
= family
; ch
; ch
= ch
->next
) {
1419 if (!(ch
->flags
& KILLME
)) continue;
1420 if ((ch
->flags
& RUNNING
) && !(ch
->flags
& ZOMBIE
))
1425 * No running children, skip SIGKILL
1428 foundOne
= 0; /* Skip the sleep below. */
1437 * Now give all processes the chance to die and collect exit statuses.
1439 if (foundOne
) do_sleep(1);
1440 for(ch
= family
; ch
; ch
= ch
->next
)
1441 if (ch
->flags
& KILLME
) {
1442 if (!(ch
->flags
& ZOMBIE
))
1443 initlog(L_CO
, "Pid %d [id %s] seems to hang", ch
->pid
,
1446 INITDBG(L_VB
, "Updating utmp for pid %d [id %s]",
1448 ch
->flags
&= ~RUNNING
;
1449 if (ch
->process
[0] != '+')
1450 write_utmp_wtmp("", ch
->id
, ch
->pid
, DEAD_PROCESS
, NULL
);
1455 * Both rounds done; clean up the list.
1457 sigemptyset(&nmask
);
1458 sigaddset(&nmask
, SIGCHLD
);
1459 sigprocmask(SIG_BLOCK
, &nmask
, &omask
);
1460 for(ch
= family
; ch
; ch
= old
) {
1465 for(ch
= family
; ch
; ch
= ch
->next
) ch
->new = NULL
;
1467 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
1471 * Dispose of INITLVL file.
1473 if (lstat(INITLVL
, &st
) >= 0 && S_ISLNK(st
.st_mode
)) {
1475 * INITLVL is a symbolic link, so just truncate the file.
1477 close(open(INITLVL
, O_WRONLY
|O_TRUNC
));
1480 * Delete INITLVL file.
1487 * Dispose of INITLVL2 file.
1489 if (lstat(INITLVL2
, &st
) >= 0 && S_ISLNK(st
.st_mode
)) {
1491 * INITLVL2 is a symbolic link, so just truncate the file.
1493 close(open(INITLVL2
, O_WRONLY
|O_TRUNC
));
1496 * Delete INITLVL2 file.
1504 * Walk through the family list and start up children.
1505 * The entries that do not belong here at all are removed
1508 void start_if_needed(void)
1510 CHILD
*ch
; /* Pointer to child */
1511 int delete; /* Delete this entry from list? */
1513 INITDBG(L_VB
, "Checking for children to start");
1515 for(ch
= family
; ch
; ch
= ch
->next
) {
1518 if (ch
->rlevel
[0] == 'C') {
1519 INITDBG(L_VB
, "%s: flags %d", ch
->process
, ch
->flags
);
1523 /* Are we waiting for this process? Then quit here. */
1524 if (ch
->flags
& WAITING
) break;
1526 /* Already running? OK, don't touch it */
1527 if (ch
->flags
& RUNNING
) continue;
1529 /* See if we have to start it up */
1531 if (strchr(ch
->rlevel
, runlevel
) ||
1532 ((ch
->flags
& DEMAND
) && !strchr("#*Ss", runlevel
))) {
1538 /* FIXME: is this OK? */
1539 ch
->flags
&= ~(RUNNING
|WAITING
);
1540 if (!ISPOWER(ch
->action
) && ch
->action
!= KBREQUEST
)
1541 ch
->flags
&= ~XECUTED
;
1544 /* Do we have to wait for this process? */
1545 if (ch
->flags
& WAITING
) break;
1551 * Ask the user on the console for a runlevel
1553 int ask_runlevel(void)
1555 const char prompt
[] = "\nEnter runlevel: ";
1561 fd
= console_open(O_RDWR
|O_NOCTTY
);
1563 if (fd
< 0) return('S');
1565 while(!strchr("0123456789S", lvl
)) {
1566 write(fd
, prompt
, sizeof(prompt
) - 1);
1568 read(fd
, buf
, sizeof(buf
));
1569 if (buf
[0] != 0 && (buf
[1] == '\r' || buf
[1] == '\n'))
1571 if (islower(lvl
)) lvl
= toupper(lvl
);
1578 * Search the INITTAB file for the 'initdefault' field, with the default
1579 * runlevel. If this fails, ask the user to supply a runlevel.
1581 int get_init_default(void)
1588 * Look for initdefault.
1590 for(ch
= family
; ch
; ch
= ch
->next
)
1591 if (ch
->action
== INITDEFAULT
) {
1594 if (*p
> lvl
) lvl
= *p
;
1600 * See if level is valid
1603 if (islower(lvl
)) lvl
= toupper(lvl
);
1604 if (strchr("0123456789S", lvl
) == NULL
) {
1606 "Initdefault level '%c' is invalid", lvl
);
1611 * Ask for runlevel on console if needed.
1613 if (lvl
<= 0) lvl
= ask_runlevel();
1616 * Log the fact that we have a runlevel now.
1625 * Do actions for the new level. If we are compatible with
1626 * the "old" INITLVL and arg == 0, try to read the new
1627 * runlevel from that file first.
1629 int read_level(int arg
)
1631 CHILD
*ch
; /* Walk through list */
1632 unsigned char foo
= 'X'; /* Contents of INITLVL */
1647 if (stat(INITLVL
, &stt
) != 0 || stt
.st_size
!= 0L)
1648 fp
= fopen(INITLVL
, "r");
1651 (stat(INITLVL2
, &stt
) != 0 || stt
.st_size
!= 0L))
1652 fp
= fopen(INITLVL2
, "r");
1655 /* INITLVL file empty or not there - act as 'init q' */
1656 initlog(L_SY
, "Re-reading inittab");
1659 ok
= fscanf(fp
, "%c %d", &foo
, &st
);
1662 /* We go to the new runlevel passed as an argument. */
1666 if (ok
== 2) sltime
= st
;
1668 #endif /* INITLVL */
1670 if (islower(foo
)) foo
= toupper(foo
);
1671 if (ok
< 1 || ok
> 2 || strchr("QS0123456789ABCU", foo
) == NULL
) {
1672 initlog(L_VB
, "bad runlevel: %c", foo
);
1676 /* Log this action */
1679 initlog(L_VB
, "Going single user");
1682 initlog(L_SY
, "Re-reading inittab");
1688 "Activating demand-procedures for '%c'", foo
);
1691 initlog(L_SY
, "Trying to re-exec init");
1694 initlog(L_VB
, "Switching to runlevel: %c", foo
);
1697 if (foo
== 'Q') return runlevel
;
1699 /* Check if this is a runlevel a, b or c */
1700 if (strchr("ABC", foo
)) {
1701 if (runlevel
== 'S') return(runlevel
);
1703 /* Read inittab again first! */
1706 /* Mark those special tasks */
1707 for(ch
= family
; ch
; ch
= ch
->next
)
1708 if (strchr(ch
->rlevel
, foo
) != NULL
||
1709 strchr(ch
->rlevel
, tolower(foo
)) != NULL
) {
1710 ch
->flags
|= DEMAND
;
1711 ch
->flags
&= ~XECUTED
;
1713 "Marking (%s) as ondemand, flags %d",
1719 /* Store both the old and the new runlevel. */
1720 write_utmp_wtmp("runlevel", "~~", foo
+ 256*runlevel
, RUN_LVL
, "~");
1722 prevlevel
= runlevel
;
1728 * This procedure is called after every signal (SIGHUP, SIGALRM..)
1730 * Only clear the 'failing' flag if the process is sleeping
1731 * longer than 5 minutes, or inittab was read again due
1732 * to user interaction.
1734 void fail_check(void)
1736 CHILD
*ch
; /* Pointer to child structure */
1737 time_t t
; /* System time */
1738 time_t next_alarm
= 0; /* When to set next alarm */
1742 for(ch
= family
; ch
; ch
= ch
->next
) {
1744 if (ch
->flags
& FAILING
) {
1745 /* Can we free this sucker? */
1746 if (ch
->tm
+ SLEEPTIME
< t
) {
1747 ch
->flags
&= ~FAILING
;
1751 /* No, we'll look again later */
1752 if (next_alarm
== 0 ||
1753 ch
->tm
+ SLEEPTIME
> next_alarm
)
1754 next_alarm
= ch
->tm
+ SLEEPTIME
;
1760 if (next_alarm
< 1) next_alarm
= 1;
1765 /* Set all 'Fail' timers to 0 */
1766 void fail_cancel(void)
1770 for(ch
= family
; ch
; ch
= ch
->next
) {
1773 ch
->flags
&= ~FAILING
;
1778 * Start up powerfail entries.
1780 void do_power_fail(int pwrstat
)
1785 * Tell powerwait & powerfail entries to start up
1787 for (ch
= family
; ch
; ch
= ch
->next
) {
1788 if (pwrstat
== 'O') {
1790 * The power is OK again.
1792 if (ch
->action
== POWEROKWAIT
)
1793 ch
->flags
&= ~XECUTED
;
1794 } else if (pwrstat
== 'L') {
1796 * Low battery, shut down now.
1798 if (ch
->action
== POWERFAILNOW
)
1799 ch
->flags
&= ~XECUTED
;
1802 * Power is failing, shutdown imminent
1804 if (ch
->action
== POWERFAIL
|| ch
->action
== POWERWAIT
)
1805 ch
->flags
&= ~XECUTED
;
1811 * Check for state-pipe presence
1813 int check_pipe(int fd
)
1821 t
.tv_sec
= t
.tv_usec
= 0;
1823 if (select(fd
+1, &s
, NULL
, NULL
, &t
) != 1)
1825 if (read(fd
, signature
, 8) != 8)
1827 return strncmp(Signature
, signature
, 8) == 0;
1831 * Make a state-pipe.
1833 int make_pipe(int fd
)
1840 fcntl(fds
[1], F_SETFD
, 1);
1841 fcntl(fd
, F_SETFD
, 0);
1842 write(fds
[1], Signature
, 8);
1848 * Attempt to re-exec.
1853 sigset_t mask
, oldset
;
1858 if (strchr("S0123456",runlevel
) == NULL
)
1862 * Reset the alarm, and block all signals.
1866 sigprocmask(SIG_BLOCK
, &mask
, &oldset
);
1869 * construct a pipe fd --> STATE_PIPE and write a signature
1871 fd
= make_pipe(STATE_PIPE
);
1874 * It's a backup day today, so I'm pissed off. Being a BOFH, however,
1875 * does have it's advantages...
1880 DELSET(got_signals
, SIGCHLD
);
1881 DELSET(got_signals
, SIGHUP
);
1882 DELSET(got_signals
, SIGUSR1
);
1885 * That should be cleaned.
1887 for(ch
= family
; ch
; ch
= ch
->next
)
1888 if (ch
->flags
& ZOMBIE
) {
1889 INITDBG(L_VB
, "Child died, PID= %d", ch
->pid
);
1890 ch
->flags
&= ~(RUNNING
|ZOMBIE
|WAITING
);
1891 if (ch
->process
[0] != '+')
1892 write_utmp_wtmp("", ch
->id
, ch
->pid
, DEAD_PROCESS
, NULL
);
1895 if ((pid
= fork()) == 0) {
1897 * Child sends state information to the parent.
1904 * The existing init process execs a new init binary.
1906 env
= init_buildenv(0);
1907 execle(myname
, myname
, "--init", NULL
, env
);
1910 * We shouldn't be here, something failed.
1911 * Bitch, close the state pipe, unblock signals and return.
1915 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
1917 initlog(L_CO
, "Attempt to re-exec failed");
1922 * We got a change runlevel request through the
1923 * init.fifo. Process it.
1925 void fifo_new_level(int level
)
1932 if (level
== runlevel
) return;
1935 /* Are we waiting for a child? */
1936 for(ch
= family
; ch
; ch
= ch
->next
)
1937 if (ch
->flags
& WAITING
) break;
1941 /* We need to go into a new runlevel */
1942 oldlevel
= runlevel
;
1943 runlevel
= read_level(level
);
1944 if (runlevel
== 'U') {
1945 runlevel
= oldlevel
;
1948 if (oldlevel
!= 'S' && runlevel
== 'S') console_stty();
1949 if (runlevel
== '6' || runlevel
== '0' ||
1950 runlevel
== '1') console_stty();
1953 setproctitle("init [%c]", runlevel
);
1960 * Set/unset environment variables. The variables are
1961 * encoded as KEY=VAL\0KEY=VAL\0\0. With "=VAL" it means
1962 * setenv, without it means unsetenv.
1964 void initcmd_setenv(char *data
, int size
)
1966 char *env
, *p
, *e
, *eq
;
1971 while (*data
&& data
< e
) {
1973 for (p
= data
; *p
&& p
< e
; p
++)
1974 if (*p
== '=') eq
= p
;
1979 sz
= eq
? (eq
- env
) : (p
- env
);
1981 /*initlog(L_SY, "init_setenv: %s, %s, %d", env, eq, sz);*/
1984 * We only allow INIT_* to be set.
1986 if (strncmp(env
, "INIT_", 5) != 0)
1989 /* Free existing vars. */
1990 for (i
= 0; i
< NR_EXTRA_ENV
; i
++) {
1991 if (extra_env
[i
] == NULL
) continue;
1992 if (!strncmp(extra_env
[i
], env
, sz
) &&
1993 extra_env
[i
][sz
] == '=') {
1995 extra_env
[i
] = NULL
;
1999 /* Set new vars if needed. */
2000 if (eq
== NULL
) continue;
2001 for (i
= 0; i
< NR_EXTRA_ENV
; i
++) {
2002 if (extra_env
[i
] == NULL
) {
2003 extra_env
[i
] = istrdup(env
);
2012 * Read from the init FIFO. Processes like telnetd and rlogind can
2013 * ask us to create login processes on their behalf.
2015 * FIXME: this needs to be finished. NOT that it is buggy, but we need
2016 * to add the telnetd/rlogind stuff so people can start using it.
2017 * Maybe move to using an AF_UNIX socket so we can use
2018 * the 2.2 kernel credential stuff to see who we're talking to.
2021 void check_init_fifo(void)
2023 struct init_request request
;
2025 struct stat st
, st2
;
2031 * First, try to create /dev/initctl if not present.
2033 if (stat(INIT_FIFO
, &st2
) < 0 && errno
== ENOENT
)
2034 (void)mkfifo(INIT_FIFO
, 0600);
2037 * If /dev/initctl is open, stat the file to see if it
2038 * is still the _same_ inode.
2041 fstat(pipe_fd
, &st
);
2042 if (stat(INIT_FIFO
, &st2
) < 0 ||
2043 st
.st_dev
!= st2
.st_dev
||
2044 st
.st_ino
!= st2
.st_ino
) {
2051 * Now finally try to open /dev/initctl
2054 if ((pipe_fd
= open(INIT_FIFO
, O_RDWR
|O_NONBLOCK
)) >= 0) {
2055 fstat(pipe_fd
, &st
);
2056 if (!S_ISFIFO(st
.st_mode
)) {
2057 initlog(L_VB
, "%s is not a fifo", INIT_FIFO
);
2064 * Don't use fd's 0, 1 or 2.
2066 (void) dup2(pipe_fd
, PIPE_FD
);
2071 * Return to caller - we'll be back later.
2076 /* Wait for data to appear, _if_ the pipe was opened. */
2077 if (pipe_fd
>= 0) while(!quit
) {
2079 /* Do select, return on EINTR. */
2081 FD_SET(pipe_fd
, &fds
);
2084 n
= select(pipe_fd
+ 1, &fds
, NULL
, NULL
, &tv
);
2086 if (n
== 0 || errno
== EINTR
) return;
2090 /* Read the data, return on EINTR. */
2091 n
= read(pipe_fd
, &request
, sizeof(request
));
2094 * End of file. This can't happen under Linux (because
2095 * the pipe is opened O_RDWR - see select() in the
2096 * kernel) but you never know...
2103 if (errno
== EINTR
) return;
2104 initlog(L_VB
, "error reading initrequest");
2109 * This is a convenient point to also try to
2110 * find the console device or check if it changed.
2117 if (request
.magic
!= INIT_MAGIC
|| n
!= sizeof(request
)) {
2118 initlog(L_VB
, "got bogus initrequest");
2121 switch(request
.cmd
) {
2122 case INIT_CMD_RUNLVL
:
2123 sltime
= request
.sleeptime
;
2124 fifo_new_level(request
.runlevel
);
2127 case INIT_CMD_POWERFAIL
:
2128 sltime
= request
.sleeptime
;
2132 case INIT_CMD_POWERFAILNOW
:
2133 sltime
= request
.sleeptime
;
2137 case INIT_CMD_POWEROK
:
2138 sltime
= request
.sleeptime
;
2142 case INIT_CMD_SETENV
:
2143 initcmd_setenv(request
.i
.data
, sizeof(request
.i
.data
));
2145 case INIT_CMD_CHANGECONS
:
2148 user_console
= NULL
;
2150 if (!request
.i
.bsd
.reserved
[0])
2151 user_console
= NULL
;
2153 user_console
= strdup(request
.i
.bsd
.reserved
);
2158 initlog(L_VB
, "got unimplemented initrequest.");
2164 * We come here if the pipe couldn't be opened.
2166 if (pipe_fd
< 0) pause();
2172 * This function is used in the transition
2173 * sysinit (-> single user) boot -> multi-user.
2175 void boot_transitions()
2178 static int newlevel
= 0;
2179 static int warn
= 1;
2183 /* Check if there is something to wait for! */
2184 for( ch
= family
; ch
; ch
= ch
->next
)
2185 if ((ch
->flags
& RUNNING
) && ch
->action
!= BOOT
) break;
2188 /* No processes left in this level, proceed to next level. */
2192 case '#': /* SYSINIT -> BOOT */
2193 INITDBG(L_VB
, "SYSINIT -> BOOT");
2195 /* Write a boot record. */
2196 wrote_utmp_reboot
= 0;
2197 wrote_wtmp_reboot
= 0;
2198 write_utmp_wtmp("reboot", "~~", 0, BOOT_TIME
, "~");
2200 /* Get our run level */
2201 newlevel
= dfl_level
? dfl_level
: get_init_default();
2202 if (newlevel
== 'S') {
2203 runlevel
= newlevel
;
2204 /* Not really 'S' but show anyway. */
2205 setproctitle("init [S]");
2209 case '*': /* BOOT -> NORMAL */
2210 INITDBG(L_VB
, "BOOT -> NORMAL");
2211 if (runlevel
!= newlevel
)
2212 loglevel
= newlevel
;
2213 runlevel
= newlevel
;
2217 case 'S': /* Ended SU mode */
2219 INITDBG(L_VB
, "END SU MODE");
2220 newlevel
= get_init_default();
2221 if (!did_boot
&& newlevel
!= 'S')
2224 if (runlevel
!= newlevel
)
2225 loglevel
= newlevel
;
2226 runlevel
= newlevel
;
2230 for(ch
= family
; ch
; ch
= ch
->next
)
2231 if (strcmp(ch
->rlevel
, "S") == 0)
2232 ch
->flags
&= ~(FAILING
|WAITING
|XECUTED
);
2237 "no more processes left in this runlevel");
2240 if (got_signals
== 0)
2245 initlog(L_VB
, "Entering runlevel: %c", runlevel
);
2246 write_utmp_wtmp("runlevel", "~~", runlevel
+ 256 * oldlevel
, RUN_LVL
, "~");
2247 thislevel
= runlevel
;
2248 prevlevel
= oldlevel
;
2249 setproctitle("init [%c]", runlevel
);
2255 * Init got hit by a signal. See which signal it is,
2256 * and act accordingly.
2258 void process_signals()
2266 if (ISMEMBER(got_signals
, SIGPWR
)) {
2267 INITDBG(L_VB
, "got SIGPWR");
2268 /* See _what_ kind of SIGPWR this is. */
2270 if ((fd
= open(PWRSTAT
, O_RDONLY
)) >= 0) {
2277 do_power_fail(pwrstat
);
2278 DELSET(got_signals
, SIGPWR
);
2281 if (ISMEMBER(got_signals
, SIGINT
)) {
2282 INITDBG(L_VB
, "got SIGINT");
2283 /* Tell ctrlaltdel entry to start up */
2284 for(ch
= family
; ch
; ch
= ch
->next
)
2285 if (ch
->action
== CTRLALTDEL
)
2286 ch
->flags
&= ~XECUTED
;
2287 DELSET(got_signals
, SIGINT
);
2290 if (ISMEMBER(got_signals
, SIGWINCH
)) {
2291 INITDBG(L_VB
, "got SIGWINCH");
2292 /* Tell kbrequest entry to start up */
2293 for(ch
= family
; ch
; ch
= ch
->next
)
2294 if (ch
->action
== KBREQUEST
)
2295 ch
->flags
&= ~XECUTED
;
2296 DELSET(got_signals
, SIGWINCH
);
2299 if (ISMEMBER(got_signals
, SIGALRM
)) {
2300 INITDBG(L_VB
, "got SIGALRM");
2301 /* The timer went off: check it out */
2302 DELSET(got_signals
, SIGALRM
);
2305 if (ISMEMBER(got_signals
, SIGCHLD
)) {
2306 INITDBG(L_VB
, "got SIGCHLD");
2307 /* First set flag to 0 */
2308 DELSET(got_signals
, SIGCHLD
);
2310 /* See which child this was */
2311 for(ch
= family
; ch
; ch
= ch
->next
)
2312 if (ch
->flags
& ZOMBIE
) {
2313 INITDBG(L_VB
, "Child died, PID= %d", ch
->pid
);
2314 ch
->flags
&= ~(RUNNING
|ZOMBIE
|WAITING
);
2315 if (ch
->process
[0] != '+')
2316 write_utmp_wtmp("", ch
->id
, ch
->pid
, DEAD_PROCESS
, NULL
);
2321 if (ISMEMBER(got_signals
, SIGHUP
)) {
2322 INITDBG(L_VB
, "got SIGHUP");
2324 /* Are we waiting for a child? */
2325 for(ch
= family
; ch
; ch
= ch
->next
)
2326 if (ch
->flags
& WAITING
) break;
2330 /* We need to go into a new runlevel */
2331 oldlevel
= runlevel
;
2333 runlevel
= read_level(0);
2335 if (runlevel
== 'U') {
2336 runlevel
= oldlevel
;
2339 if (oldlevel
!= 'S' && runlevel
== 'S') console_stty();
2340 if (runlevel
== '6' || runlevel
== '0' ||
2341 runlevel
== '1') console_stty();
2344 setproctitle("init [%c]", runlevel
);
2345 DELSET(got_signals
, SIGHUP
);
2349 if (ISMEMBER(got_signals
, SIGUSR1
)) {
2351 * SIGUSR1 means close and reopen /dev/initctl
2353 INITDBG(L_VB
, "got SIGUSR1");
2356 DELSET(got_signals
, SIGUSR1
);
2366 struct sigaction sa
;
2375 * Fork so we can debug the init process.
2377 if ((f
= fork()) > 0) {
2378 static const char killmsg
[] = "PRNT: init killed.\r\n";
2381 while((rc
= wait(&st
)) != f
)
2382 if (rc
< 0 && errno
== ECHILD
)
2384 write(1, killmsg
, sizeof(killmsg
) - 1);
2391 * Tell the kernel to send us SIGINT when CTRL-ALT-DEL
2392 * is pressed, and that we want to handle keyboard signals.
2394 init_reboot(BMAGIC_SOFT
);
2395 if ((f
= open(VT_MASTER
, O_RDWR
| O_NOCTTY
)) >= 0) {
2396 (void) ioctl(f
, KDSIGACCEPT
, SIGWINCH
);
2399 (void) ioctl(0, KDSIGACCEPT
, SIGWINCH
);
2403 * Ignore all signals.
2405 for(f
= 1; f
<= NSIG
; f
++)
2406 SETSIG(sa
, f
, SIG_IGN
, SA_RESTART
);
2409 SETSIG(sa
, SIGALRM
, signal_handler
, 0);
2410 SETSIG(sa
, SIGHUP
, signal_handler
, 0);
2411 SETSIG(sa
, SIGINT
, signal_handler
, 0);
2412 SETSIG(sa
, SIGCHLD
, chld_handler
, SA_RESTART
);
2413 SETSIG(sa
, SIGPWR
, signal_handler
, 0);
2414 SETSIG(sa
, SIGWINCH
, signal_handler
, 0);
2415 SETSIG(sa
, SIGUSR1
, signal_handler
, 0);
2416 SETSIG(sa
, SIGSTOP
, stop_handler
, SA_RESTART
);
2417 SETSIG(sa
, SIGTSTP
, stop_handler
, SA_RESTART
);
2418 SETSIG(sa
, SIGCONT
, cont_handler
, SA_RESTART
);
2419 SETSIG(sa
, SIGSEGV
, (void (*)(int))segv_handler
, SA_RESTART
);
2425 /* Close whatever files are open, and reset the console. */
2433 * Set default PATH variable.
2435 setenv("PATH", PATH_DEFAULT
, 1 /* Overwrite */);
2438 * Initialize /var/run/utmp (only works if /var is on
2439 * root and mounted rw)
2441 (void) close(open(UTMP_FILE
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644));
2444 * Say hello to the world
2446 initlog(L_CO
, bootmsg
, "booting");
2449 * See if we have to start an emergency shell.
2452 SETSIG(sa
, SIGCHLD
, SIG_DFL
, SA_RESTART
);
2453 if (spawn(&ch_emerg
, &f
) > 0) {
2454 while((rc
= wait(&st
)) != f
)
2455 if (rc
< 0 && errno
== ECHILD
)
2458 SETSIG(sa
, SIGCHLD
, chld_handler
, SA_RESTART
);
2462 * Start normal boot procedure.
2469 * Restart: unblock signals and let the show go on
2471 initlog(L_CO
, bootmsg
, "reloading");
2473 sigprocmask(SIG_UNBLOCK
, &sgt
, NULL
);
2476 * Set default PATH variable.
2478 setenv("PATH", PATH_DEFAULT
, 0 /* Don't overwrite */);
2484 /* See if we need to make the boot transitions. */
2486 INITDBG(L_VB
, "init_main: waiting..");
2488 /* Check if there are processes to be waited on. */
2489 for(ch
= family
; ch
; ch
= ch
->next
)
2490 if ((ch
->flags
& RUNNING
) && ch
->action
!= BOOT
) break;
2493 /* Wait until we get hit by some signal. */
2494 while (ch
!= NULL
&& got_signals
== 0) {
2495 if (ISMEMBER(got_signals
, SIGHUP
)) {
2496 /* See if there are processes to be waited on. */
2497 for(ch
= family
; ch
; ch
= ch
->next
)
2498 if (ch
->flags
& WAITING
) break;
2500 if (ch
!= NULL
) check_init_fifo();
2502 #else /* CHANGE_WAIT */
2503 if (ch
!= NULL
&& got_signals
== 0) check_init_fifo();
2504 #endif /* CHANGE_WAIT */
2506 /* Check the 'failing' flags */
2509 /* Process any signals. */
2512 /* See what we need to start up (again) */
2519 * Tell the user about the syntax we expect.
2523 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
);
2527 int telinit(char *progname
, int argc
, char **argv
)
2529 #ifdef TELINIT_USES_INITLVL
2532 struct init_request request
;
2533 struct sigaction sa
;
2537 memset(&request
, 0, sizeof(request
));
2538 request
.magic
= INIT_MAGIC
;
2540 while ((f
= getopt(argc
, argv
, "t:e:")) != EOF
) switch(f
) {
2542 sltime
= atoi(optarg
);
2546 env
= request
.i
.data
;
2548 if (env
+ l
+ 2 > request
.i
.data
+ sizeof(request
.i
.data
)) {
2549 fprintf(stderr
, "%s: -e option data "
2550 "too large\n", progname
);
2553 memcpy(env
, optarg
, l
);
2562 if (env
) *env
++ = 0;
2567 request
.cmd
= INIT_CMD_SETENV
;
2569 if (argc
- optind
!= 1 || strlen(argv
[optind
]) != 1)
2571 if (!strchr("0123456789SsQqAaBbCcUu", argv
[optind
][0]))
2573 request
.cmd
= INIT_CMD_RUNLVL
;
2574 request
.runlevel
= env
? 0 : argv
[optind
][0];
2575 request
.sleeptime
= sltime
;
2578 /* Open the fifo and write a command. */
2579 /* Make sure we don't hang on opening /dev/initctl */
2580 SETSIG(sa
, SIGALRM
, signal_handler
, 0);
2582 if ((fd
= open(INIT_FIFO
, O_WRONLY
)) >= 0 &&
2583 write(fd
, &request
, sizeof(request
)) == sizeof(request
)) {
2589 #ifdef TELINIT_USES_INITLVL
2590 if (request
.cmd
== INIT_CMD_RUNLVL
) {
2591 /* Fallthrough to the old method. */
2593 /* Now write the new runlevel. */
2594 if ((fp
= fopen(INITLVL
, "w")) == NULL
) {
2595 fprintf(stderr
, "%s: cannot create %s\n",
2599 fprintf(fp
, "%s %d", argv
[optind
], sltime
);
2602 /* And tell init about the pending runlevel change. */
2603 if (kill(INITPID
, SIGHUP
) < 0) perror(progname
);
2609 fprintf(stderr
, "%s: ", progname
);
2610 if (ISMEMBER(got_signals
, SIGALRM
)) {
2611 fprintf(stderr
, "timeout opening/writing control channel %s\n",
2620 * Main entry for init and telinit.
2622 int main(int argc
, char **argv
)
2629 /* Get my own name */
2630 if ((p
= strrchr(argv
[0], '/')) != NULL
)
2637 if (geteuid() != 0) {
2638 fprintf(stderr
, "%s: must be superuser.\n", p
);
2643 * Is this telinit or init ?
2645 isinit
= (getpid() == 1);
2646 for (f
= 1; f
< argc
; f
++) {
2647 if (!strcmp(argv
[f
], "-i") || !strcmp(argv
[f
], "--init"))
2651 if (!isinit
) exit(telinit(p
, argc
, argv
));
2656 if (check_pipe(STATE_PIPE
)) {
2658 receive_state(STATE_PIPE
);
2660 myname
= istrdup(argv
[0]);
2663 for (f
= 0; f
< argc
; f
++)
2664 maxproclen
+= strlen(argv
[f
]) + 1;
2666 setproctitle("init [%c]",runlevel
);
2671 /* Check command line arguments */
2672 maxproclen
= strlen(argv
[0]) + 1;
2673 for(f
= 1; f
< argc
; f
++) {
2674 if (!strcmp(argv
[f
], "single") || !strcmp(argv
[f
], "-s"))
2676 else if (!strcmp(argv
[f
], "-a") || !strcmp(argv
[f
], "auto"))
2677 putenv("AUTOBOOT=YES");
2678 else if (!strcmp(argv
[f
], "-b") || !strcmp(argv
[f
],"emergency"))
2680 else if (!strcmp(argv
[f
], "-z")) {
2682 if (argv
[f
+ 1]) f
++;
2683 } else if (strchr("0123456789sS", argv
[f
][0])
2684 && strlen(argv
[f
]) == 1)
2685 dfl_level
= argv
[f
][0];
2686 /* "init u" in the very beginning makes no sense */
2687 if (dfl_level
== 's') dfl_level
= 'S';
2688 maxproclen
+= strlen(argv
[f
]) + 1;
2692 if (getenv("SELINUX_INIT") == NULL
&& !is_selinux_enabled()) {
2693 putenv("SELINUX_INIT=YES");
2694 if (selinux_init_load_policy(&enforce
) == 0 ) {
2695 execv(myname
, argv
);
2698 /* SELinux in enforcing mode but load_policy failed */
2699 /* At this point, we probably can't open /dev/console, so log() won't work */
2700 fprintf(stderr
,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.\n");
2706 /* Start booting. */
2709 setproctitle("init boot");
2710 init_main(dfl_level
);