]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/init/init.c
MFV r298691:
[FreeBSD/FreeBSD.git] / sbin / init / init.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Donn Seeley at Berkeley Software Design, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1991, 1993\n\
36         The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)init.c      8.1 (Berkeley) 7/15/93";
42 #endif
43 static const char rcsid[] =
44   "$FreeBSD$";
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/ioctl.h>
49 #include <sys/mman.h>
50 #include <sys/mount.h>
51 #include <sys/sysctl.h>
52 #include <sys/wait.h>
53 #include <sys/stat.h>
54 #include <sys/uio.h>
55
56 #include <db.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <kenv.h>
60 #include <libutil.h>
61 #include <paths.h>
62 #include <signal.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <syslog.h>
67 #include <time.h>
68 #include <ttyent.h>
69 #include <unistd.h>
70 #include <sys/reboot.h>
71 #include <err.h>
72
73 #include <stdarg.h>
74
75 #ifdef SECURE
76 #include <pwd.h>
77 #endif
78
79 #ifdef LOGIN_CAP
80 #include <login_cap.h>
81 #endif
82
83 #include "mntopts.h"
84 #include "pathnames.h"
85
86 /*
87  * Sleep times; used to prevent thrashing.
88  */
89 #define GETTY_SPACING            5      /* N secs minimum getty spacing */
90 #define GETTY_SLEEP             30      /* sleep N secs after spacing problem */
91 #define GETTY_NSPACE             3      /* max. spacing count to bring reaction */
92 #define WINDOW_WAIT              3      /* wait N secs after starting window */
93 #define STALL_TIMEOUT           30      /* wait N secs after warning */
94 #define DEATH_WATCH             10      /* wait N secs for procs to die */
95 #define DEATH_SCRIPT            120     /* wait for 2min for /etc/rc.shutdown */
96 #define RESOURCE_RC             "daemon"
97 #define RESOURCE_WINDOW         "default"
98 #define RESOURCE_GETTY          "default"
99
100 static void handle(sig_t, ...);
101 static void delset(sigset_t *, ...);
102
103 static void stall(const char *, ...) __printflike(1, 2);
104 static void warning(const char *, ...) __printflike(1, 2);
105 static void emergency(const char *, ...) __printflike(1, 2);
106 static void disaster(int);
107 static void badsys(int);
108 static void revoke_ttys(void);
109 static int  runshutdown(void);
110 static char *strk(char *);
111
112 /*
113  * We really need a recursive typedef...
114  * The following at least guarantees that the return type of (*state_t)()
115  * is sufficiently wide to hold a function pointer.
116  */
117 typedef long (*state_func_t)(void);
118 typedef state_func_t (*state_t)(void);
119
120 static state_func_t single_user(void);
121 static state_func_t runcom(void);
122 static state_func_t read_ttys(void);
123 static state_func_t multi_user(void);
124 static state_func_t clean_ttys(void);
125 static state_func_t catatonia(void);
126 static state_func_t death(void);
127 static state_func_t death_single(void);
128 static state_func_t reroot(void);
129 static state_func_t reroot_phase_two(void);
130
131 static state_func_t run_script(const char *);
132
133 static enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT;
134 #define FALSE   0
135 #define TRUE    1
136
137 static int Reboot = FALSE;
138 static int howto = RB_AUTOBOOT;
139
140 static int devfs;
141
142 static void transition(state_t);
143 static state_t requested_transition;
144 static state_t current_state = death_single;
145
146 static void open_console(void);
147 static const char *get_shell(void);
148 static void write_stderr(const char *message);
149
150 typedef struct init_session {
151         pid_t   se_process;             /* controlling process */
152         time_t  se_started;             /* used to avoid thrashing */
153         int     se_flags;               /* status of session */
154 #define SE_SHUTDOWN     0x1             /* session won't be restarted */
155 #define SE_PRESENT      0x2             /* session is in /etc/ttys */
156         int     se_nspace;              /* spacing count */
157         char    *se_device;             /* filename of port */
158         char    *se_getty;              /* what to run on that port */
159         char    *se_getty_argv_space;   /* pre-parsed argument array space */
160         char    **se_getty_argv;        /* pre-parsed argument array */
161         char    *se_window;             /* window system (started only once) */
162         char    *se_window_argv_space;  /* pre-parsed argument array space */
163         char    **se_window_argv;       /* pre-parsed argument array */
164         char    *se_type;               /* default terminal type */
165         struct  init_session *se_prev;
166         struct  init_session *se_next;
167 } session_t;
168
169 static void free_session(session_t *);
170 static session_t *new_session(session_t *, struct ttyent *);
171 static session_t *sessions;
172
173 static char **construct_argv(char *);
174 static void start_window_system(session_t *);
175 static void collect_child(pid_t);
176 static pid_t start_getty(session_t *);
177 static void transition_handler(int);
178 static void alrm_handler(int);
179 static void setsecuritylevel(int);
180 static int getsecuritylevel(void);
181 static int setupargv(session_t *, struct ttyent *);
182 #ifdef LOGIN_CAP
183 static void setprocresources(const char *);
184 #endif
185 static int clang;
186
187 static int start_session_db(void);
188 static void add_session(session_t *);
189 static void del_session(session_t *);
190 static session_t *find_session(pid_t);
191 static DB *session_db;
192
193 /*
194  * The mother of all processes.
195  */
196 int
197 main(int argc, char *argv[])
198 {
199         state_t initial_transition = runcom;
200         char kenv_value[PATH_MAX];
201         int c, error;
202         struct sigaction sa;
203         sigset_t mask;
204
205         /* Dispose of random users. */
206         if (getuid() != 0)
207                 errx(1, "%s", strerror(EPERM));
208
209         /* System V users like to reexec init. */
210         if (getpid() != 1) {
211 #ifdef COMPAT_SYSV_INIT
212                 /* So give them what they want */
213                 if (argc > 1) {
214                         if (strlen(argv[1]) == 1) {
215                                 char runlevel = *argv[1];
216                                 int sig;
217
218                                 switch (runlevel) {
219                                 case '0': /* halt + poweroff */
220                                         sig = SIGUSR2;
221                                         break;
222                                 case '1': /* single-user */
223                                         sig = SIGTERM;
224                                         break;
225                                 case '6': /* reboot */
226                                         sig = SIGINT;
227                                         break;
228                                 case 'c': /* block further logins */
229                                         sig = SIGTSTP;
230                                         break;
231                                 case 'q': /* rescan /etc/ttys */
232                                         sig = SIGHUP;
233                                         break;
234                                 case 'r': /* remount root */
235                                         sig = SIGEMT;
236                                         break;
237                                 default:
238                                         goto invalid;
239                                 }
240                                 kill(1, sig);
241                                 _exit(0);
242                         } else
243 invalid:
244                                 errx(1, "invalid run-level ``%s''", argv[1]);
245                 } else
246 #endif
247                         errx(1, "already running");
248         }
249         /*
250          * Note that this does NOT open a file...
251          * Does 'init' deserve its own facility number?
252          */
253         openlog("init", LOG_CONS, LOG_AUTH);
254
255         /*
256          * Create an initial session.
257          */
258         if (setsid() < 0 && (errno != EPERM || getsid(0) != 1))
259                 warning("initial setsid() failed: %m");
260
261         /*
262          * Establish an initial user so that programs running
263          * single user do not freak out and die (like passwd).
264          */
265         if (setlogin("root") < 0)
266                 warning("setlogin() failed: %m");
267
268         /*
269          * This code assumes that we always get arguments through flags,
270          * never through bits set in some random machine register.
271          */
272         while ((c = getopt(argc, argv, "dsfr")) != -1)
273                 switch (c) {
274                 case 'd':
275                         devfs = 1;
276                         break;
277                 case 's':
278                         initial_transition = single_user;
279                         break;
280                 case 'f':
281                         runcom_mode = FASTBOOT;
282                         break;
283                 case 'r':
284                         initial_transition = reroot_phase_two;
285                         break;
286                 default:
287                         warning("unrecognized flag '-%c'", c);
288                         break;
289                 }
290
291         if (optind != argc)
292                 warning("ignoring excess arguments");
293
294         /*
295          * We catch or block signals rather than ignore them,
296          * so that they get reset on exec.
297          */
298         handle(badsys, SIGSYS, 0);
299         handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGXCPU,
300             SIGXFSZ, 0);
301         handle(transition_handler, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP,
302             SIGUSR1, SIGUSR2, 0);
303         handle(alrm_handler, SIGALRM, 0);
304         sigfillset(&mask);
305         delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
306             SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP,
307             SIGALRM, SIGUSR1, SIGUSR2, 0);
308         sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
309         sigemptyset(&sa.sa_mask);
310         sa.sa_flags = 0;
311         sa.sa_handler = SIG_IGN;
312         sigaction(SIGTTIN, &sa, (struct sigaction *)0);
313         sigaction(SIGTTOU, &sa, (struct sigaction *)0);
314
315         /*
316          * Paranoia.
317          */
318         close(0);
319         close(1);
320         close(2);
321
322         if (kenv(KENV_GET, "init_script", kenv_value, sizeof(kenv_value)) > 0) {
323                 state_func_t next_transition;
324
325                 if ((next_transition = run_script(kenv_value)) != 0)
326                         initial_transition = (state_t) next_transition;
327         }
328
329         if (kenv(KENV_GET, "init_chroot", kenv_value, sizeof(kenv_value)) > 0) {
330                 if (chdir(kenv_value) != 0 || chroot(".") != 0)
331                         warning("Can't chroot to %s: %m", kenv_value);
332         }
333
334         /*
335          * Additional check if devfs needs to be mounted:
336          * If "/" and "/dev" have the same device number,
337          * then it hasn't been mounted yet.
338          */
339         if (!devfs) {
340                 struct stat stst;
341                 dev_t root_devno;
342
343                 stat("/", &stst);
344                 root_devno = stst.st_dev;
345                 if (stat("/dev", &stst) != 0)
346                         warning("Can't stat /dev: %m");
347                 else if (stst.st_dev == root_devno)
348                         devfs++;
349         }
350
351         if (devfs) {
352                 struct iovec iov[4];
353                 char *s;
354                 int i;
355
356                 char _fstype[]  = "fstype";
357                 char _devfs[]   = "devfs";
358                 char _fspath[]  = "fspath";
359                 char _path_dev[]= _PATH_DEV;
360
361                 iov[0].iov_base = _fstype;
362                 iov[0].iov_len = sizeof(_fstype);
363                 iov[1].iov_base = _devfs;
364                 iov[1].iov_len = sizeof(_devfs);
365                 iov[2].iov_base = _fspath;
366                 iov[2].iov_len = sizeof(_fspath);
367                 /*
368                  * Try to avoid the trailing slash in _PATH_DEV.
369                  * Be *very* defensive.
370                  */
371                 s = strdup(_PATH_DEV);
372                 if (s != NULL) {
373                         i = strlen(s);
374                         if (i > 0 && s[i - 1] == '/')
375                                 s[i - 1] = '\0';
376                         iov[3].iov_base = s;
377                         iov[3].iov_len = strlen(s) + 1;
378                 } else {
379                         iov[3].iov_base = _path_dev;
380                         iov[3].iov_len = sizeof(_path_dev);
381                 }
382                 nmount(iov, 4, 0);
383                 if (s != NULL)
384                         free(s);
385         }
386
387         if (initial_transition != reroot_phase_two) {
388                 /*
389                  * Unmount reroot leftovers.  This runs after init(8)
390                  * gets reexecuted after reroot_phase_two() is done.
391                  */
392                 error = unmount(_PATH_REROOT, MNT_FORCE);
393                 if (error != 0 && errno != EINVAL)
394                         warning("Cannot unmount %s: %m", _PATH_REROOT);
395         }
396
397         /*
398          * Start the state machine.
399          */
400         transition(initial_transition);
401
402         /*
403          * Should never reach here.
404          */
405         return 1;
406 }
407
408 /*
409  * Associate a function with a signal handler.
410  */
411 static void
412 handle(sig_t handler, ...)
413 {
414         int sig;
415         struct sigaction sa;
416         sigset_t mask_everything;
417         va_list ap;
418         va_start(ap, handler);
419
420         sa.sa_handler = handler;
421         sigfillset(&mask_everything);
422
423         while ((sig = va_arg(ap, int)) != 0) {
424                 sa.sa_mask = mask_everything;
425                 /* XXX SA_RESTART? */
426                 sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0;
427                 sigaction(sig, &sa, (struct sigaction *) 0);
428         }
429         va_end(ap);
430 }
431
432 /*
433  * Delete a set of signals from a mask.
434  */
435 static void
436 delset(sigset_t *maskp, ...)
437 {
438         int sig;
439         va_list ap;
440         va_start(ap, maskp);
441
442         while ((sig = va_arg(ap, int)) != 0)
443                 sigdelset(maskp, sig);
444         va_end(ap);
445 }
446
447 /*
448  * Log a message and sleep for a while (to give someone an opportunity
449  * to read it and to save log or hardcopy output if the problem is chronic).
450  * NB: should send a message to the session logger to avoid blocking.
451  */
452 static void
453 stall(const char *message, ...)
454 {
455         va_list ap;
456         va_start(ap, message);
457
458         vsyslog(LOG_ALERT, message, ap);
459         va_end(ap);
460         sleep(STALL_TIMEOUT);
461 }
462
463 /*
464  * Like stall(), but doesn't sleep.
465  * If cpp had variadic macros, the two functions could be #defines for another.
466  * NB: should send a message to the session logger to avoid blocking.
467  */
468 static void
469 warning(const char *message, ...)
470 {
471         va_list ap;
472         va_start(ap, message);
473
474         vsyslog(LOG_ALERT, message, ap);
475         va_end(ap);
476 }
477
478 /*
479  * Log an emergency message.
480  * NB: should send a message to the session logger to avoid blocking.
481  */
482 static void
483 emergency(const char *message, ...)
484 {
485         va_list ap;
486         va_start(ap, message);
487
488         vsyslog(LOG_EMERG, message, ap);
489         va_end(ap);
490 }
491
492 /*
493  * Catch a SIGSYS signal.
494  *
495  * These may arise if a system does not support sysctl.
496  * We tolerate up to 25 of these, then throw in the towel.
497  */
498 static void
499 badsys(int sig)
500 {
501         static int badcount = 0;
502
503         if (badcount++ < 25)
504                 return;
505         disaster(sig);
506 }
507
508 /*
509  * Catch an unexpected signal.
510  */
511 static void
512 disaster(int sig)
513 {
514
515         emergency("fatal signal: %s",
516             (unsigned)sig < NSIG ? sys_siglist[sig] : "unknown signal");
517
518         sleep(STALL_TIMEOUT);
519         _exit(sig);             /* reboot */
520 }
521
522 /*
523  * Get the security level of the kernel.
524  */
525 static int
526 getsecuritylevel(void)
527 {
528 #ifdef KERN_SECURELVL
529         int name[2], curlevel;
530         size_t len;
531
532         name[0] = CTL_KERN;
533         name[1] = KERN_SECURELVL;
534         len = sizeof curlevel;
535         if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
536                 emergency("cannot get kernel security level: %s",
537                     strerror(errno));
538                 return (-1);
539         }
540         return (curlevel);
541 #else
542         return (-1);
543 #endif
544 }
545
546 /*
547  * Set the security level of the kernel.
548  */
549 static void
550 setsecuritylevel(int newlevel)
551 {
552 #ifdef KERN_SECURELVL
553         int name[2], curlevel;
554
555         curlevel = getsecuritylevel();
556         if (newlevel == curlevel)
557                 return;
558         name[0] = CTL_KERN;
559         name[1] = KERN_SECURELVL;
560         if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
561                 emergency(
562                     "cannot change kernel security level from %d to %d: %s",
563                     curlevel, newlevel, strerror(errno));
564                 return;
565         }
566 #ifdef SECURE
567         warning("kernel security level changed from %d to %d",
568             curlevel, newlevel);
569 #endif
570 #endif
571 }
572
573 /*
574  * Change states in the finite state machine.
575  * The initial state is passed as an argument.
576  */
577 static void
578 transition(state_t s)
579 {
580
581         current_state = s;
582         for (;;)
583                 current_state = (state_t) (*current_state)();
584 }
585
586 /*
587  * Start a session and allocate a controlling terminal.
588  * Only called by children of init after forking.
589  */
590 static void
591 open_console(void)
592 {
593         int fd;
594
595         /*
596          * Try to open /dev/console.  Open the device with O_NONBLOCK to
597          * prevent potential blocking on a carrier.
598          */
599         revoke(_PATH_CONSOLE);
600         if ((fd = open(_PATH_CONSOLE, O_RDWR | O_NONBLOCK)) != -1) {
601                 (void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
602                 if (login_tty(fd) == 0)
603                         return;
604                 close(fd);
605         }
606
607         /* No luck.  Log output to file if possible. */
608         if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
609                 stall("cannot open null device.");
610                 _exit(1);
611         }
612         if (fd != STDIN_FILENO) {
613                 dup2(fd, STDIN_FILENO);
614                 close(fd);
615         }
616         fd = open(_PATH_INITLOG, O_WRONLY | O_APPEND | O_CREAT, 0644);
617         if (fd == -1)
618                 dup2(STDIN_FILENO, STDOUT_FILENO);
619         else if (fd != STDOUT_FILENO) {
620                 dup2(fd, STDOUT_FILENO);
621                 close(fd);
622         }
623         dup2(STDOUT_FILENO, STDERR_FILENO);
624 }
625
626 static const char *
627 get_shell(void)
628 {
629         static char kenv_value[PATH_MAX];
630
631         if (kenv(KENV_GET, "init_shell", kenv_value, sizeof(kenv_value)) > 0)
632                 return kenv_value;
633         else
634                 return _PATH_BSHELL;
635 }
636
637 static void
638 write_stderr(const char *message)
639 {
640
641         write(STDERR_FILENO, message, strlen(message));
642 }
643
644 static int
645 read_file(const char *path, void **bufp, size_t *bufsizep)
646 {
647         struct stat sb;
648         size_t bufsize;
649         void *buf;
650         ssize_t nbytes;
651         int error, fd;
652
653         fd = open(path, O_RDONLY);
654         if (fd < 0) {
655                 emergency("%s: %s", path, strerror(errno));
656                 return (-1);
657         }
658
659         error = fstat(fd, &sb);
660         if (error != 0) {
661                 emergency("fstat: %s", strerror(errno));
662                 close(fd);
663                 return (error);
664         }
665
666         bufsize = sb.st_size;
667         buf = malloc(bufsize);
668         if (buf == NULL) {
669                 emergency("malloc: %s", strerror(errno));
670                 close(fd);
671                 return (error);
672         }
673
674         nbytes = read(fd, buf, bufsize);
675         if (nbytes != (ssize_t)bufsize) {
676                 emergency("read: %s", strerror(errno));
677                 close(fd);
678                 free(buf);
679                 return (error);
680         }
681
682         error = close(fd);
683         if (error != 0) {
684                 emergency("close: %s", strerror(errno));
685                 free(buf);
686                 return (error);
687         }
688
689         *bufp = buf;
690         *bufsizep = bufsize;
691
692         return (0);
693 }
694
695 static int
696 create_file(const char *path, const void *buf, size_t bufsize)
697 {
698         ssize_t nbytes;
699         int error, fd;
700
701         fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0700);
702         if (fd < 0) {
703                 emergency("%s: %s", path, strerror(errno));
704                 return (-1);
705         }
706
707         nbytes = write(fd, buf, bufsize);
708         if (nbytes != (ssize_t)bufsize) {
709                 emergency("write: %s", strerror(errno));
710                 close(fd);
711                 return (-1);
712         }
713
714         error = close(fd);
715         if (error != 0) {
716                 emergency("close: %s", strerror(errno));
717                 return (-1);
718         }
719
720         return (0);
721 }
722
723 static int
724 mount_tmpfs(const char *fspath)
725 {
726         struct iovec *iov;
727         char errmsg[255];
728         int error, iovlen;
729
730         iov = NULL;
731         iovlen = 0;
732         memset(errmsg, 0, sizeof(errmsg));
733         build_iovec(&iov, &iovlen, "fstype",
734             __DECONST(void *, "tmpfs"), (size_t)-1);
735         build_iovec(&iov, &iovlen, "fspath",
736             __DECONST(void *, fspath), (size_t)-1);
737         build_iovec(&iov, &iovlen, "errmsg",
738             errmsg, sizeof(errmsg));
739
740         error = nmount(iov, iovlen, 0);
741         if (error != 0) {
742                 if (*errmsg != '\0') {
743                         emergency("cannot mount tmpfs on %s: %s: %s",
744                             fspath, errmsg, strerror(errno));
745                 } else {
746                         emergency("cannot mount tmpfs on %s: %s",
747                             fspath, strerror(errno));
748                 }
749                 return (error);
750         }
751         return (0);
752 }
753
754 static state_func_t
755 reroot(void)
756 {
757         void *buf;
758         char init_path[PATH_MAX];
759         size_t bufsize, init_path_len;
760         int error, name[4];
761
762         buf = NULL;
763         bufsize = 0;
764
765         name[0] = CTL_KERN;
766         name[1] = KERN_PROC;
767         name[2] = KERN_PROC_PATHNAME;
768         name[3] = -1;
769         init_path_len = sizeof(init_path);
770         error = sysctl(name, 4, init_path, &init_path_len, NULL, 0);
771         if (error != 0) {
772                 emergency("failed to get kern.proc.pathname: %s",
773                     strerror(errno));
774                 goto out;
775         }
776
777         revoke_ttys();
778         runshutdown();
779
780         /*
781          * Make sure nobody can interfere with our scheme.
782          */
783         error = kill(-1, SIGKILL);
784         if (error != 0) {
785                 emergency("kill(2) failed: %s", strerror(errno));
786                 goto out;
787         }
788
789         /*
790          * Copy the init binary into tmpfs, so that we can unmount
791          * the old rootfs without committing suicide.
792          */
793         error = read_file(init_path, &buf, &bufsize);
794         if (error != 0)
795                 goto out;
796         error = mount_tmpfs(_PATH_REROOT);
797         if (error != 0)
798                 goto out;
799         error = create_file(_PATH_REROOT_INIT, buf, bufsize);
800         if (error != 0)
801                 goto out;
802
803         /*
804          * Execute the temporary init.
805          */
806         execl(_PATH_REROOT_INIT, _PATH_REROOT_INIT, "-r", NULL);
807         emergency("cannot exec %s: %s", _PATH_REROOT_INIT, strerror(errno));
808
809 out:
810         emergency("reroot failed; going to single user mode");
811         free(buf);
812         return (state_func_t) single_user;
813 }
814
815 static state_func_t
816 reroot_phase_two(void)
817 {
818         char init_path[PATH_MAX], *path, *path_component;
819         size_t init_path_len;
820         int nbytes, error;
821
822         /*
823          * Ask the kernel to mount the new rootfs.
824          */
825         error = reboot(RB_REROOT);
826         if (error != 0) {
827                 emergency("RB_REBOOT failed: %s", strerror(errno));
828                 goto out;
829         }
830
831         /*
832          * Figure out where the destination init(8) binary is.  Note that
833          * the path could be different than what we've started with.  Use
834          * the value from kenv, if set, or the one from sysctl otherwise.
835          * The latter defaults to a hardcoded value, but can be overridden
836          * by a build time option.
837          */
838         nbytes = kenv(KENV_GET, "init_path", init_path, sizeof(init_path));
839         if (nbytes <= 0) {
840                 init_path_len = sizeof(init_path);
841                 error = sysctlbyname("kern.init_path",
842                     init_path, &init_path_len, NULL, 0);
843                 if (error != 0) {
844                         emergency("failed to retrieve kern.init_path: %s",
845                             strerror(errno));
846                         goto out;
847                 }
848         }
849
850         /*
851          * Repeat the init search logic from sys/kern/init_path.c
852          */
853         path_component = init_path;
854         while ((path = strsep(&path_component, ":")) != NULL) {
855                 /*
856                  * Execute init(8) from the new rootfs.
857                  */
858                 execl(path, path, NULL);
859         }
860         emergency("cannot exec init from %s: %s", init_path, strerror(errno));
861
862 out:
863         emergency("reroot failed; going to single user mode");
864         return (state_func_t) single_user;
865 }
866
867 /*
868  * Bring the system up single user.
869  */
870 static state_func_t
871 single_user(void)
872 {
873         pid_t pid, wpid;
874         int status;
875         sigset_t mask;
876         const char *shell;
877         char *argv[2];
878 #ifdef SECURE
879         struct ttyent *typ;
880         struct passwd *pp;
881         static const char banner[] =
882                 "Enter root password, or ^D to go multi-user\n";
883         char *clear, *password;
884 #endif
885 #ifdef DEBUGSHELL
886         char altshell[128];
887 #endif
888
889         if (Reboot) {
890                 /* Instead of going single user, let's reboot the machine */
891                 sync();
892                 reboot(howto);
893                 _exit(0);
894         }
895
896         shell = get_shell();
897
898         if ((pid = fork()) == 0) {
899                 /*
900                  * Start the single user session.
901                  */
902                 open_console();
903
904 #ifdef SECURE
905                 /*
906                  * Check the root password.
907                  * We don't care if the console is 'on' by default;
908                  * it's the only tty that can be 'off' and 'secure'.
909                  */
910                 typ = getttynam("console");
911                 pp = getpwnam("root");
912                 if (typ && (typ->ty_status & TTY_SECURE) == 0 &&
913                     pp && *pp->pw_passwd) {
914                         write_stderr(banner);
915                         for (;;) {
916                                 clear = getpass("Password:");
917                                 if (clear == 0 || *clear == '\0')
918                                         _exit(0);
919                                 password = crypt(clear, pp->pw_passwd);
920                                 bzero(clear, _PASSWORD_LEN);
921                                 if (password == NULL ||
922                                     strcmp(password, pp->pw_passwd) == 0)
923                                         break;
924                                 warning("single-user login failed\n");
925                         }
926                 }
927                 endttyent();
928                 endpwent();
929 #endif /* SECURE */
930
931 #ifdef DEBUGSHELL
932                 {
933                         char *cp = altshell;
934                         int num;
935
936 #define SHREQUEST "Enter full pathname of shell or RETURN for "
937                         write_stderr(SHREQUEST);
938                         write_stderr(shell);
939                         write_stderr(": ");
940                         while ((num = read(STDIN_FILENO, cp, 1)) != -1 &&
941                             num != 0 && *cp != '\n' && cp < &altshell[127])
942                                 cp++;
943                         *cp = '\0';
944                         if (altshell[0] != '\0')
945                                 shell = altshell;
946                 }
947 #endif /* DEBUGSHELL */
948
949                 /*
950                  * Unblock signals.
951                  * We catch all the interesting ones,
952                  * and those are reset to SIG_DFL on exec.
953                  */
954                 sigemptyset(&mask);
955                 sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
956
957                 /*
958                  * Fire off a shell.
959                  * If the default one doesn't work, try the Bourne shell.
960                  */
961
962                 char name[] = "-sh";
963
964                 argv[0] = name;
965                 argv[1] = 0;
966                 execv(shell, argv);
967                 emergency("can't exec %s for single user: %m", shell);
968                 execv(_PATH_BSHELL, argv);
969                 emergency("can't exec %s for single user: %m", _PATH_BSHELL);
970                 sleep(STALL_TIMEOUT);
971                 _exit(1);
972         }
973
974         if (pid == -1) {
975                 /*
976                  * We are seriously hosed.  Do our best.
977                  */
978                 emergency("can't fork single-user shell, trying again");
979                 while (waitpid(-1, (int *) 0, WNOHANG) > 0)
980                         continue;
981                 return (state_func_t) single_user;
982         }
983
984         requested_transition = 0;
985         do {
986                 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
987                         collect_child(wpid);
988                 if (wpid == -1) {
989                         if (errno == EINTR)
990                                 continue;
991                         warning("wait for single-user shell failed: %m; restarting");
992                         return (state_func_t) single_user;
993                 }
994                 if (wpid == pid && WIFSTOPPED(status)) {
995                         warning("init: shell stopped, restarting\n");
996                         kill(pid, SIGCONT);
997                         wpid = -1;
998                 }
999         } while (wpid != pid && !requested_transition);
1000
1001         if (requested_transition)
1002                 return (state_func_t) requested_transition;
1003
1004         if (!WIFEXITED(status)) {
1005                 if (WTERMSIG(status) == SIGKILL) {
1006                         /*
1007                          *  reboot(8) killed shell?
1008                          */
1009                         warning("single user shell terminated.");
1010                         sleep(STALL_TIMEOUT);
1011                         _exit(0);
1012                 } else {
1013                         warning("single user shell terminated, restarting");
1014                         return (state_func_t) single_user;
1015                 }
1016         }
1017
1018         runcom_mode = FASTBOOT;
1019         return (state_func_t) runcom;
1020 }
1021
1022 /*
1023  * Run the system startup script.
1024  */
1025 static state_func_t
1026 runcom(void)
1027 {
1028         state_func_t next_transition;
1029
1030         if ((next_transition = run_script(_PATH_RUNCOM)) != 0)
1031                 return next_transition;
1032
1033         runcom_mode = AUTOBOOT;         /* the default */
1034         return (state_func_t) read_ttys;
1035 }
1036
1037 /*
1038  * Run a shell script.
1039  * Returns 0 on success, otherwise the next transition to enter:
1040  *  - single_user if fork/execv/waitpid failed, or if the script
1041  *    terminated with a signal or exit code != 0.
1042  *  - death_single if a SIGTERM was delivered to init(8).
1043  */
1044 static state_func_t
1045 run_script(const char *script)
1046 {
1047         pid_t pid, wpid;
1048         int status;
1049         char *argv[4];
1050         const char *shell;
1051         struct sigaction sa;
1052
1053         shell = get_shell();
1054
1055         if ((pid = fork()) == 0) {
1056                 sigemptyset(&sa.sa_mask);
1057                 sa.sa_flags = 0;
1058                 sa.sa_handler = SIG_IGN;
1059                 sigaction(SIGTSTP, &sa, (struct sigaction *)0);
1060                 sigaction(SIGHUP, &sa, (struct sigaction *)0);
1061
1062                 open_console();
1063
1064                 char _sh[]              = "sh";
1065                 char _autoboot[]        = "autoboot";
1066
1067                 argv[0] = _sh;
1068                 argv[1] = __DECONST(char *, script);
1069                 argv[2] = runcom_mode == AUTOBOOT ? _autoboot : 0;
1070                 argv[3] = 0;
1071
1072                 sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0);
1073
1074 #ifdef LOGIN_CAP
1075                 setprocresources(RESOURCE_RC);
1076 #endif
1077                 execv(shell, argv);
1078                 stall("can't exec %s for %s: %m", shell, script);
1079                 _exit(1);       /* force single user mode */
1080         }
1081
1082         if (pid == -1) {
1083                 emergency("can't fork for %s on %s: %m", shell, script);
1084                 while (waitpid(-1, (int *) 0, WNOHANG) > 0)
1085                         continue;
1086                 sleep(STALL_TIMEOUT);
1087                 return (state_func_t) single_user;
1088         }
1089
1090         /*
1091          * Copied from single_user().  This is a bit paranoid.
1092          */
1093         requested_transition = 0;
1094         do {
1095                 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
1096                         collect_child(wpid);
1097                 if (wpid == -1) {
1098                         if (requested_transition == death_single ||
1099                             requested_transition == reroot)
1100                                 return (state_func_t) requested_transition;
1101                         if (errno == EINTR)
1102                                 continue;
1103                         warning("wait for %s on %s failed: %m; going to "
1104                             "single user mode", shell, script);
1105                         return (state_func_t) single_user;
1106                 }
1107                 if (wpid == pid && WIFSTOPPED(status)) {
1108                         warning("init: %s on %s stopped, restarting\n",
1109                             shell, script);
1110                         kill(pid, SIGCONT);
1111                         wpid = -1;
1112                 }
1113         } while (wpid != pid);
1114
1115         if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
1116             requested_transition == catatonia) {
1117                 /* /etc/rc executed /sbin/reboot; wait for the end quietly */
1118                 sigset_t s;
1119
1120                 sigfillset(&s);
1121                 for (;;)
1122                         sigsuspend(&s);
1123         }
1124
1125         if (!WIFEXITED(status)) {
1126                 warning("%s on %s terminated abnormally, going to single "
1127                     "user mode", shell, script);
1128                 return (state_func_t) single_user;
1129         }
1130
1131         if (WEXITSTATUS(status))
1132                 return (state_func_t) single_user;
1133
1134         return (state_func_t) 0;
1135 }
1136
1137 /*
1138  * Open the session database.
1139  *
1140  * NB: We could pass in the size here; is it necessary?
1141  */
1142 static int
1143 start_session_db(void)
1144 {
1145         if (session_db && (*session_db->close)(session_db))
1146                 emergency("session database close: %s", strerror(errno));
1147         if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == NULL) {
1148                 emergency("session database open: %s", strerror(errno));
1149                 return (1);
1150         }
1151         return (0);
1152
1153 }
1154
1155 /*
1156  * Add a new login session.
1157  */
1158 static void
1159 add_session(session_t *sp)
1160 {
1161         DBT key;
1162         DBT data;
1163
1164         key.data = &sp->se_process;
1165         key.size = sizeof sp->se_process;
1166         data.data = &sp;
1167         data.size = sizeof sp;
1168
1169         if ((*session_db->put)(session_db, &key, &data, 0))
1170                 emergency("insert %d: %s", sp->se_process, strerror(errno));
1171 }
1172
1173 /*
1174  * Delete an old login session.
1175  */
1176 static void
1177 del_session(session_t *sp)
1178 {
1179         DBT key;
1180
1181         key.data = &sp->se_process;
1182         key.size = sizeof sp->se_process;
1183
1184         if ((*session_db->del)(session_db, &key, 0))
1185                 emergency("delete %d: %s", sp->se_process, strerror(errno));
1186 }
1187
1188 /*
1189  * Look up a login session by pid.
1190  */
1191 static session_t *
1192 find_session(pid_t pid)
1193 {
1194         DBT key;
1195         DBT data;
1196         session_t *ret;
1197
1198         key.data = &pid;
1199         key.size = sizeof pid;
1200         if ((*session_db->get)(session_db, &key, &data, 0) != 0)
1201                 return 0;
1202         bcopy(data.data, (char *)&ret, sizeof(ret));
1203         return ret;
1204 }
1205
1206 /*
1207  * Construct an argument vector from a command line.
1208  */
1209 static char **
1210 construct_argv(char *command)
1211 {
1212         int argc = 0;
1213         char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1)
1214                                                 * sizeof (char *));
1215
1216         if ((argv[argc++] = strk(command)) == 0) {
1217                 free(argv);
1218                 return (NULL);
1219         }
1220         while ((argv[argc++] = strk((char *) 0)) != NULL)
1221                 continue;
1222         return argv;
1223 }
1224
1225 /*
1226  * Deallocate a session descriptor.
1227  */
1228 static void
1229 free_session(session_t *sp)
1230 {
1231         free(sp->se_device);
1232         if (sp->se_getty) {
1233                 free(sp->se_getty);
1234                 free(sp->se_getty_argv_space);
1235                 free(sp->se_getty_argv);
1236         }
1237         if (sp->se_window) {
1238                 free(sp->se_window);
1239                 free(sp->se_window_argv_space);
1240                 free(sp->se_window_argv);
1241         }
1242         if (sp->se_type)
1243                 free(sp->se_type);
1244         free(sp);
1245 }
1246
1247 /*
1248  * Allocate a new session descriptor.
1249  * Mark it SE_PRESENT.
1250  */
1251 static session_t *
1252 new_session(session_t *sprev, struct ttyent *typ)
1253 {
1254         session_t *sp;
1255         int fd;
1256
1257         if ((typ->ty_status & TTY_ON) == 0 ||
1258             typ->ty_name == 0 ||
1259             typ->ty_getty == 0)
1260                 return 0;
1261
1262         sp = (session_t *) calloc(1, sizeof (session_t));
1263
1264         sp->se_flags |= SE_PRESENT;
1265
1266         sp->se_device = malloc(sizeof(_PATH_DEV) + strlen(typ->ty_name));
1267         sprintf(sp->se_device, "%s%s", _PATH_DEV, typ->ty_name);
1268
1269         /*
1270          * Attempt to open the device, if we get "device not configured"
1271          * then don't add the device to the session list.
1272          */
1273         if ((fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0)) < 0) {
1274                 if (errno == ENXIO) {
1275                         free_session(sp);
1276                         return (0);
1277                 }
1278         } else
1279                 close(fd);
1280
1281         if (setupargv(sp, typ) == 0) {
1282                 free_session(sp);
1283                 return (0);
1284         }
1285
1286         sp->se_next = 0;
1287         if (sprev == NULL) {
1288                 sessions = sp;
1289                 sp->se_prev = 0;
1290         } else {
1291                 sprev->se_next = sp;
1292                 sp->se_prev = sprev;
1293         }
1294
1295         return sp;
1296 }
1297
1298 /*
1299  * Calculate getty and if useful window argv vectors.
1300  */
1301 static int
1302 setupargv(session_t *sp, struct ttyent *typ)
1303 {
1304
1305         if (sp->se_getty) {
1306                 free(sp->se_getty);
1307                 free(sp->se_getty_argv_space);
1308                 free(sp->se_getty_argv);
1309         }
1310         sp->se_getty = malloc(strlen(typ->ty_getty) + strlen(typ->ty_name) + 2);
1311         sprintf(sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name);
1312         sp->se_getty_argv_space = strdup(sp->se_getty);
1313         sp->se_getty_argv = construct_argv(sp->se_getty_argv_space);
1314         if (sp->se_getty_argv == NULL) {
1315                 warning("can't parse getty for port %s", sp->se_device);
1316                 free(sp->se_getty);
1317                 free(sp->se_getty_argv_space);
1318                 sp->se_getty = sp->se_getty_argv_space = 0;
1319                 return (0);
1320         }
1321         if (sp->se_window) {
1322                 free(sp->se_window);
1323                 free(sp->se_window_argv_space);
1324                 free(sp->se_window_argv);
1325         }
1326         sp->se_window = sp->se_window_argv_space = 0;
1327         sp->se_window_argv = 0;
1328         if (typ->ty_window) {
1329                 sp->se_window = strdup(typ->ty_window);
1330                 sp->se_window_argv_space = strdup(sp->se_window);
1331                 sp->se_window_argv = construct_argv(sp->se_window_argv_space);
1332                 if (sp->se_window_argv == NULL) {
1333                         warning("can't parse window for port %s",
1334                             sp->se_device);
1335                         free(sp->se_window_argv_space);
1336                         free(sp->se_window);
1337                         sp->se_window = sp->se_window_argv_space = 0;
1338                         return (0);
1339                 }
1340         }
1341         if (sp->se_type)
1342                 free(sp->se_type);
1343         sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0;
1344         return (1);
1345 }
1346
1347 /*
1348  * Walk the list of ttys and create sessions for each active line.
1349  */
1350 static state_func_t
1351 read_ttys(void)
1352 {
1353         session_t *sp, *snext;
1354         struct ttyent *typ;
1355
1356         /*
1357          * Destroy any previous session state.
1358          * There shouldn't be any, but just in case...
1359          */
1360         for (sp = sessions; sp; sp = snext) {
1361                 snext = sp->se_next;
1362                 free_session(sp);
1363         }
1364         sessions = 0;
1365         if (start_session_db())
1366                 return (state_func_t) single_user;
1367
1368         /*
1369          * Allocate a session entry for each active port.
1370          * Note that sp starts at 0.
1371          */
1372         while ((typ = getttyent()) != NULL)
1373                 if ((snext = new_session(sp, typ)) != NULL)
1374                         sp = snext;
1375
1376         endttyent();
1377
1378         return (state_func_t) multi_user;
1379 }
1380
1381 /*
1382  * Start a window system running.
1383  */
1384 static void
1385 start_window_system(session_t *sp)
1386 {
1387         pid_t pid;
1388         sigset_t mask;
1389         char term[64], *env[2];
1390         int status;
1391
1392         if ((pid = fork()) == -1) {
1393                 emergency("can't fork for window system on port %s: %m",
1394                     sp->se_device);
1395                 /* hope that getty fails and we can try again */
1396                 return;
1397         }
1398         if (pid) {
1399                 waitpid(-1, &status, 0);
1400                 return;
1401         }
1402
1403         /* reparent window process to the init to not make a zombie on exit */
1404         if ((pid = fork()) == -1) {
1405                 emergency("can't fork for window system on port %s: %m",
1406                     sp->se_device);
1407                 _exit(1);
1408         }
1409         if (pid)
1410                 _exit(0);
1411
1412         sigemptyset(&mask);
1413         sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
1414
1415         if (setsid() < 0)
1416                 emergency("setsid failed (window) %m");
1417
1418 #ifdef LOGIN_CAP
1419         setprocresources(RESOURCE_WINDOW);
1420 #endif
1421         if (sp->se_type) {
1422                 /* Don't use malloc after fork */
1423                 strcpy(term, "TERM=");
1424                 strncat(term, sp->se_type, sizeof(term) - 6);
1425                 env[0] = term;
1426                 env[1] = 0;
1427         }
1428         else
1429                 env[0] = 0;
1430         execve(sp->se_window_argv[0], sp->se_window_argv, env);
1431         stall("can't exec window system '%s' for port %s: %m",
1432                 sp->se_window_argv[0], sp->se_device);
1433         _exit(1);
1434 }
1435
1436 /*
1437  * Start a login session running.
1438  */
1439 static pid_t
1440 start_getty(session_t *sp)
1441 {
1442         pid_t pid;
1443         sigset_t mask;
1444         time_t current_time = time((time_t *) 0);
1445         int too_quick = 0;
1446         char term[64], *env[2];
1447
1448         if (current_time >= sp->se_started &&
1449             current_time - sp->se_started < GETTY_SPACING) {
1450                 if (++sp->se_nspace > GETTY_NSPACE) {
1451                         sp->se_nspace = 0;
1452                         too_quick = 1;
1453                 }
1454         } else
1455                 sp->se_nspace = 0;
1456
1457         /*
1458          * fork(), not vfork() -- we can't afford to block.
1459          */
1460         if ((pid = fork()) == -1) {
1461                 emergency("can't fork for getty on port %s: %m", sp->se_device);
1462                 return -1;
1463         }
1464
1465         if (pid)
1466                 return pid;
1467
1468         if (too_quick) {
1469                 warning("getty repeating too quickly on port %s, sleeping %d secs",
1470                     sp->se_device, GETTY_SLEEP);
1471                 sleep((unsigned) GETTY_SLEEP);
1472         }
1473
1474         if (sp->se_window) {
1475                 start_window_system(sp);
1476                 sleep(WINDOW_WAIT);
1477         }
1478
1479         sigemptyset(&mask);
1480         sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
1481
1482 #ifdef LOGIN_CAP
1483         setprocresources(RESOURCE_GETTY);
1484 #endif
1485         if (sp->se_type) {
1486                 /* Don't use malloc after fork */
1487                 strcpy(term, "TERM=");
1488                 strncat(term, sp->se_type, sizeof(term) - 6);
1489                 env[0] = term;
1490                 env[1] = 0;
1491         } else
1492                 env[0] = 0;
1493         execve(sp->se_getty_argv[0], sp->se_getty_argv, env);
1494         stall("can't exec getty '%s' for port %s: %m",
1495                 sp->se_getty_argv[0], sp->se_device);
1496         _exit(1);
1497 }
1498
1499 /*
1500  * Collect exit status for a child.
1501  * If an exiting login, start a new login running.
1502  */
1503 static void
1504 collect_child(pid_t pid)
1505 {
1506         session_t *sp, *sprev, *snext;
1507
1508         if (! sessions)
1509                 return;
1510
1511         if (! (sp = find_session(pid)))
1512                 return;
1513
1514         del_session(sp);
1515         sp->se_process = 0;
1516
1517         if (sp->se_flags & SE_SHUTDOWN) {
1518                 if ((sprev = sp->se_prev) != NULL)
1519                         sprev->se_next = sp->se_next;
1520                 else
1521                         sessions = sp->se_next;
1522                 if ((snext = sp->se_next) != NULL)
1523                         snext->se_prev = sp->se_prev;
1524                 free_session(sp);
1525                 return;
1526         }
1527
1528         if ((pid = start_getty(sp)) == -1) {
1529                 /* serious trouble */
1530                 requested_transition = clean_ttys;
1531                 return;
1532         }
1533
1534         sp->se_process = pid;
1535         sp->se_started = time((time_t *) 0);
1536         add_session(sp);
1537 }
1538
1539 /*
1540  * Catch a signal and request a state transition.
1541  */
1542 static void
1543 transition_handler(int sig)
1544 {
1545
1546         switch (sig) {
1547         case SIGHUP:
1548                 if (current_state == read_ttys || current_state == multi_user ||
1549                     current_state == clean_ttys || current_state == catatonia)
1550                         requested_transition = clean_ttys;
1551                 break;
1552         case SIGUSR2:
1553                 howto = RB_POWEROFF;
1554         case SIGUSR1:
1555                 howto |= RB_HALT;
1556         case SIGINT:
1557                 Reboot = TRUE;
1558         case SIGTERM:
1559                 if (current_state == read_ttys || current_state == multi_user ||
1560                     current_state == clean_ttys || current_state == catatonia)
1561                         requested_transition = death;
1562                 else
1563                         requested_transition = death_single;
1564                 break;
1565         case SIGTSTP:
1566                 if (current_state == runcom || current_state == read_ttys ||
1567                     current_state == clean_ttys ||
1568                     current_state == multi_user || current_state == catatonia)
1569                         requested_transition = catatonia;
1570                 break;
1571         case SIGEMT:
1572                 requested_transition = reroot;
1573                 break;
1574         default:
1575                 requested_transition = 0;
1576                 break;
1577         }
1578 }
1579
1580 /*
1581  * Take the system multiuser.
1582  */
1583 static state_func_t
1584 multi_user(void)
1585 {
1586         pid_t pid;
1587         session_t *sp;
1588
1589         requested_transition = 0;
1590
1591         /*
1592          * If the administrator has not set the security level to -1
1593          * to indicate that the kernel should not run multiuser in secure
1594          * mode, and the run script has not set a higher level of security
1595          * than level 1, then put the kernel into secure mode.
1596          */
1597         if (getsecuritylevel() == 0)
1598                 setsecuritylevel(1);
1599
1600         for (sp = sessions; sp; sp = sp->se_next) {
1601                 if (sp->se_process)
1602                         continue;
1603                 if ((pid = start_getty(sp)) == -1) {
1604                         /* serious trouble */
1605                         requested_transition = clean_ttys;
1606                         break;
1607                 }
1608                 sp->se_process = pid;
1609                 sp->se_started = time((time_t *) 0);
1610                 add_session(sp);
1611         }
1612
1613         while (!requested_transition)
1614                 if ((pid = waitpid(-1, (int *) 0, 0)) != -1)
1615                         collect_child(pid);
1616
1617         return (state_func_t) requested_transition;
1618 }
1619
1620 /*
1621  * This is an (n*2)+(n^2) algorithm.  We hope it isn't run often...
1622  */
1623 static state_func_t
1624 clean_ttys(void)
1625 {
1626         session_t *sp, *sprev;
1627         struct ttyent *typ;
1628         int devlen;
1629         char *old_getty, *old_window, *old_type;
1630
1631         /*
1632          * mark all sessions for death, (!SE_PRESENT)
1633          * as we find or create new ones they'll be marked as keepers,
1634          * we'll later nuke all the ones not found in /etc/ttys
1635          */
1636         for (sp = sessions; sp != NULL; sp = sp->se_next)
1637                 sp->se_flags &= ~SE_PRESENT;
1638
1639         devlen = sizeof(_PATH_DEV) - 1;
1640         while ((typ = getttyent()) != NULL) {
1641                 for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
1642                         if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
1643                                 break;
1644
1645                 if (sp) {
1646                         /* we want this one to live */
1647                         sp->se_flags |= SE_PRESENT;
1648                         if ((typ->ty_status & TTY_ON) == 0 ||
1649                             typ->ty_getty == 0) {
1650                                 sp->se_flags |= SE_SHUTDOWN;
1651                                 kill(sp->se_process, SIGHUP);
1652                                 continue;
1653                         }
1654                         sp->se_flags &= ~SE_SHUTDOWN;
1655                         old_getty = sp->se_getty ? strdup(sp->se_getty) : 0;
1656                         old_window = sp->se_window ? strdup(sp->se_window) : 0;
1657                         old_type = sp->se_type ? strdup(sp->se_type) : 0;
1658                         if (setupargv(sp, typ) == 0) {
1659                                 warning("can't parse getty for port %s",
1660                                         sp->se_device);
1661                                 sp->se_flags |= SE_SHUTDOWN;
1662                                 kill(sp->se_process, SIGHUP);
1663                         }
1664                         else if (   !old_getty
1665                                  || (!old_type && sp->se_type)
1666                                  || (old_type && !sp->se_type)
1667                                  || (!old_window && sp->se_window)
1668                                  || (old_window && !sp->se_window)
1669                                  || (strcmp(old_getty, sp->se_getty) != 0)
1670                                  || (old_window && strcmp(old_window, sp->se_window) != 0)
1671                                  || (old_type && strcmp(old_type, sp->se_type) != 0)
1672                                 ) {
1673                                 /* Don't set SE_SHUTDOWN here */
1674                                 sp->se_nspace = 0;
1675                                 sp->se_started = 0;
1676                                 kill(sp->se_process, SIGHUP);
1677                         }
1678                         if (old_getty)
1679                                 free(old_getty);
1680                         if (old_window)
1681                                 free(old_window);
1682                         if (old_type)
1683                                 free(old_type);
1684                         continue;
1685                 }
1686
1687                 new_session(sprev, typ);
1688         }
1689
1690         endttyent();
1691
1692         /*
1693          * sweep through and kill all deleted sessions
1694          * ones who's /etc/ttys line was deleted (SE_PRESENT unset)
1695          */
1696         for (sp = sessions; sp != NULL; sp = sp->se_next) {
1697                 if ((sp->se_flags & SE_PRESENT) == 0) {
1698                         sp->se_flags |= SE_SHUTDOWN;
1699                         kill(sp->se_process, SIGHUP);
1700                 }
1701         }
1702
1703         return (state_func_t) multi_user;
1704 }
1705
1706 /*
1707  * Block further logins.
1708  */
1709 static state_func_t
1710 catatonia(void)
1711 {
1712         session_t *sp;
1713
1714         for (sp = sessions; sp; sp = sp->se_next)
1715                 sp->se_flags |= SE_SHUTDOWN;
1716
1717         return (state_func_t) multi_user;
1718 }
1719
1720 /*
1721  * Note SIGALRM.
1722  */
1723 static void
1724 alrm_handler(int sig)
1725 {
1726
1727         (void)sig;
1728         clang = 1;
1729 }
1730
1731 /*
1732  * Bring the system down to single user.
1733  */
1734 static state_func_t
1735 death(void)
1736 {
1737         int block, blocked;
1738         size_t len;
1739
1740         /* Temporarily block suspend. */
1741         len = sizeof(blocked);
1742         block = 1;
1743         if (sysctlbyname("kern.suspend_blocked", &blocked, &len,
1744             &block, sizeof(block)) == -1)
1745                 blocked = 0;
1746
1747         /*
1748          * Also revoke the TTY here.  Because runshutdown() may reopen
1749          * the TTY whose getty we're killing here, there is no guarantee
1750          * runshutdown() will perform the initial open() call, causing
1751          * the terminal attributes to be misconfigured.
1752          */
1753         revoke_ttys();
1754
1755         /* Try to run the rc.shutdown script within a period of time */
1756         runshutdown();
1757
1758         /* Unblock suspend if we blocked it. */
1759         if (!blocked)
1760                 sysctlbyname("kern.suspend_blocked", NULL, NULL,
1761                     &blocked, sizeof(blocked));
1762
1763         return (state_func_t) death_single;
1764 }
1765
1766 /*
1767  * Do what is necessary to reinitialize single user mode or reboot
1768  * from an incomplete state.
1769  */
1770 static state_func_t
1771 death_single(void)
1772 {
1773         int i;
1774         pid_t pid;
1775         static const int death_sigs[2] = { SIGTERM, SIGKILL };
1776
1777         revoke(_PATH_CONSOLE);
1778
1779         for (i = 0; i < 2; ++i) {
1780                 if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
1781                         return (state_func_t) single_user;
1782
1783                 clang = 0;
1784                 alarm(DEATH_WATCH);
1785                 do
1786                         if ((pid = waitpid(-1, (int *)0, 0)) != -1)
1787                                 collect_child(pid);
1788                 while (clang == 0 && errno != ECHILD);
1789
1790                 if (errno == ECHILD)
1791                         return (state_func_t) single_user;
1792         }
1793
1794         warning("some processes would not die; ps axl advised");
1795
1796         return (state_func_t) single_user;
1797 }
1798
1799 static void
1800 revoke_ttys(void)
1801 {
1802         session_t *sp;
1803
1804         for (sp = sessions; sp; sp = sp->se_next) {
1805                 sp->se_flags |= SE_SHUTDOWN;
1806                 kill(sp->se_process, SIGHUP);
1807                 revoke(sp->se_device);
1808         }
1809 }
1810
1811 /*
1812  * Run the system shutdown script.
1813  *
1814  * Exit codes:      XXX I should document more
1815  * -2       shutdown script terminated abnormally
1816  * -1       fatal error - can't run script
1817  * 0        good.
1818  * >0       some error (exit code)
1819  */
1820 static int
1821 runshutdown(void)
1822 {
1823         pid_t pid, wpid;
1824         int status;
1825         int shutdowntimeout;
1826         size_t len;
1827         char *argv[4];
1828         const char *shell;
1829         struct sigaction sa;
1830         struct stat sb;
1831
1832         /*
1833          * rc.shutdown is optional, so to prevent any unnecessary
1834          * complaints from the shell we simply don't run it if the
1835          * file does not exist. If the stat() here fails for other
1836          * reasons, we'll let the shell complain.
1837          */
1838         if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT)
1839                 return 0;
1840
1841         shell = get_shell();
1842
1843         if ((pid = fork()) == 0) {
1844                 sigemptyset(&sa.sa_mask);
1845                 sa.sa_flags = 0;
1846                 sa.sa_handler = SIG_IGN;
1847                 sigaction(SIGTSTP, &sa, (struct sigaction *)0);
1848                 sigaction(SIGHUP, &sa, (struct sigaction *)0);
1849
1850                 open_console();
1851
1852                 char _sh[]      = "sh";
1853                 char _reboot[]  = "reboot";
1854                 char _single[]  = "single";
1855                 char _path_rundown[] = _PATH_RUNDOWN;
1856
1857                 argv[0] = _sh;
1858                 argv[1] = _path_rundown;
1859                 argv[2] = Reboot ? _reboot : _single;
1860                 argv[3] = 0;
1861
1862                 sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0);
1863
1864 #ifdef LOGIN_CAP
1865                 setprocresources(RESOURCE_RC);
1866 #endif
1867                 execv(shell, argv);
1868                 warning("can't exec %s for %s: %m", shell, _PATH_RUNDOWN);
1869                 _exit(1);       /* force single user mode */
1870         }
1871
1872         if (pid == -1) {
1873                 emergency("can't fork for %s on %s: %m", shell, _PATH_RUNDOWN);
1874                 while (waitpid(-1, (int *) 0, WNOHANG) > 0)
1875                         continue;
1876                 sleep(STALL_TIMEOUT);
1877                 return -1;
1878         }
1879
1880         len = sizeof(shutdowntimeout);
1881         if (sysctlbyname("kern.init_shutdown_timeout", &shutdowntimeout, &len,
1882             NULL, 0) == -1 || shutdowntimeout < 2)
1883                 shutdowntimeout = DEATH_SCRIPT;
1884         alarm(shutdowntimeout);
1885         clang = 0;
1886         /*
1887          * Copied from single_user().  This is a bit paranoid.
1888          * Use the same ALRM handler.
1889          */
1890         do {
1891                 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
1892                         collect_child(wpid);
1893                 if (clang == 1) {
1894                         /* we were waiting for the sub-shell */
1895                         kill(wpid, SIGTERM);
1896                         warning("timeout expired for %s on %s: %m; going to "
1897                             "single user mode", shell, _PATH_RUNDOWN);
1898                         return -1;
1899                 }
1900                 if (wpid == -1) {
1901                         if (errno == EINTR)
1902                                 continue;
1903                         warning("wait for %s on %s failed: %m; going to "
1904                             "single user mode", shell, _PATH_RUNDOWN);
1905                         return -1;
1906                 }
1907                 if (wpid == pid && WIFSTOPPED(status)) {
1908                         warning("init: %s on %s stopped, restarting\n",
1909                                 shell, _PATH_RUNDOWN);
1910                         kill(pid, SIGCONT);
1911                         wpid = -1;
1912                 }
1913         } while (wpid != pid && !clang);
1914
1915         /* Turn off the alarm */
1916         alarm(0);
1917
1918         if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
1919             requested_transition == catatonia) {
1920                 /*
1921                  * /etc/rc.shutdown executed /sbin/reboot;
1922                  * wait for the end quietly
1923                  */
1924                 sigset_t s;
1925
1926                 sigfillset(&s);
1927                 for (;;)
1928                         sigsuspend(&s);
1929         }
1930
1931         if (!WIFEXITED(status)) {
1932                 warning("%s on %s terminated abnormally, going to "
1933                     "single user mode", shell, _PATH_RUNDOWN);
1934                 return -2;
1935         }
1936
1937         if ((status = WEXITSTATUS(status)) != 0)
1938                 warning("%s returned status %d", _PATH_RUNDOWN, status);
1939
1940         return status;
1941 }
1942
1943 static char *
1944 strk(char *p)
1945 {
1946         static char *t;
1947         char *q;
1948         int c;
1949
1950         if (p)
1951                 t = p;
1952         if (!t)
1953                 return 0;
1954
1955         c = *t;
1956         while (c == ' ' || c == '\t' )
1957                 c = *++t;
1958         if (!c) {
1959                 t = 0;
1960                 return 0;
1961         }
1962         q = t;
1963         if (c == '\'') {
1964                 c = *++t;
1965                 q = t;
1966                 while (c && c != '\'')
1967                         c = *++t;
1968                 if (!c)  /* unterminated string */
1969                         q = t = 0;
1970                 else
1971                         *t++ = 0;
1972         } else {
1973                 while (c && c != ' ' && c != '\t' )
1974                         c = *++t;
1975                 *t++ = 0;
1976                 if (!c)
1977                         t = 0;
1978         }
1979         return q;
1980 }
1981
1982 #ifdef LOGIN_CAP
1983 static void
1984 setprocresources(const char *cname)
1985 {
1986         login_cap_t *lc;
1987         if ((lc = login_getclassbyname(cname, NULL)) != NULL) {
1988                 setusercontext(lc, (struct passwd*)NULL, 0,
1989                     LOGIN_SETPRIORITY | LOGIN_SETRESOURCES |
1990                     LOGIN_SETLOGINCLASS | LOGIN_SETCPUMASK);
1991                 login_close(lc);
1992         }
1993 }
1994 #endif