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