1 /*	$OpenBSD: init.c,v 1.36 2005/03/13 13:53:23 markus Exp $	*/
2 /*	$NetBSD: init.c,v 1.22 1996/05/15 23:29:33 jtc Exp $	*/
3 
4 /*-
5  * Copyright © 2013, 2014
6  *	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
7  * Copyright (c) 1991, 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Donn Seeley at Berkeley Software Design, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/param.h>
39 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n");
41 __SCCSID("@(#)init.c	8.2 (Berkeley) 4/28/95");
42 __RCSID("$MirOS: src/sbin/init/init.c,v 1.10 2014/03/13 05:48:23 tg Exp $");
43 
44 #include <sys/sysctl.h>
45 #include <sys/ioctl.h>
46 #include <sys/wait.h>
47 #include <sys/reboot.h>
48 
49 #include <dev/rndioctl.h>
50 
51 #include <db.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <signal.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <syslog.h>
60 #include <time.h>
61 #include <ttyent.h>
62 #include <unistd.h>
63 #include <util.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	WINDOW_WAIT		 3	/* wait N secs after starting window */
81 #define	STALL_TIMEOUT		30	/* wait N secs after warning */
82 #define	DEATH_WATCH		10	/* wait N secs for procs to die */
83 
84 /*
85  * User-based resource limits.
86  */
87 #define RESOURCE_RC		"daemon"
88 #define RESOURCE_WINDOW		"default"
89 #define RESOURCE_GETTY		"default"
90 
91 #ifndef DEFAULT_STATE
92 #define DEFAULT_STATE		runcom
93 #endif
94 
95 void handle(sig_t, ...);
96 void delset(sigset_t *, ...);
97 
98 void stall(const char *, ...)
99     __attribute__((__format__(__syslog__, 1, 2)));
100 void warning(const char *, ...)
101     __attribute__((__format__(__syslog__, 1, 2)));
102 void emergency(const char *, ...)
103     __attribute__((__format__(__syslog__, 1, 2)));
104 void disaster(int) __dead;
105 void badsys(int);
106 
107 /*
108  * We really need a recursive typedef...
109  * The following at least guarantees that the return type of (*state_t)()
110  * is sufficiently wide to hold a function pointer.
111  */
112 typedef long (*state_func_t)(void);
113 typedef state_func_t (*state_t)(void);
114 
115 state_func_t single_user(void);
116 state_func_t runcom(void);
117 state_func_t read_ttys(void);
118 state_func_t multi_user(void);
119 state_func_t clean_ttys(void);
120 state_func_t catatonia(void);
121 state_func_t death(void);
122 state_func_t nice_death(void);
123 
124 enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT;
125 
126 void transition(state_t) __dead;
127 state_t requested_transition = DEFAULT_STATE;
128 
129 void setctty(const char *);
130 
131 typedef struct init_session {
132 	int	se_index;		/* index of entry in ttys file */
133 	pid_t	se_process;		/* controlling process */
134 	time_t	se_started;		/* used to avoid thrashing */
135 	int	se_flags;		/* status of session */
136 #define	SE_SHUTDOWN	0x1		/* session won't be restarted */
137 #define	SE_PRESENT	0x2		/* session is in /etc/ttys */
138 #define	SE_DEVEXISTS	0x4		/* open does not result in ENODEV */
139 	char	*se_device;		/* filename of port */
140 	char	*se_getty;		/* what to run on that port */
141 	char	**se_getty_argv;	/* pre-parsed argument array */
142 	char	*se_window;		/* window system (started only once) */
143 	char	**se_window_argv;	/* pre-parsed argument array */
144 	struct	init_session *se_prev;
145 	struct	init_session *se_next;
146 } session_t;
147 
148 void free_session(session_t *);
149 session_t *new_session(session_t *, int, struct ttyent *);
150 session_t *sessions;
151 
152 char **construct_argv(char *);
153 void start_window_system(session_t *);
154 void collect_child(pid_t);
155 pid_t start_getty(session_t *);
156 void transition_handler(int);
157 void alrm_handler(int);
158 void setsecuritylevel(int);
159 int getsecuritylevel(void);
160 int setupargv(session_t *, struct ttyent *);
161 int clang;
162 
163 #ifdef LOGIN_CAP
164 void setprocresources(const char *);
165 #else
166 #define setprocresources(p)
167 #endif
168 
169 void clear_session_logs(session_t *);
170 
171 int start_session_db(void);
172 void add_session(session_t *);
173 void del_session(session_t *);
174 session_t *find_session(pid_t);
175 DB *session_db;
176 
177 extern void arc4random_ctl(unsigned int);
178 
179 /*
180  * The mother of all processes.
181  */
182 int
main(int argc,char * argv[])183 main(int argc, char *argv[])
184 {
185 	int c;
186 	struct sigaction sa;
187 	sigset_t mask;
188 
189 	/* Dispose of random users. */
190 	if (getuid() != 0) {
191 		(void)fprintf(stderr, "init: %s\n", strerror(EPERM));
192 		exit (1);
193 	}
194 
195 	/* System V users like to reexec init. */
196 	if (getpid() != 1) {
197 		(void)fprintf(stderr, "init: already running\n");
198 		exit (1);
199 	}
200 
201 	/*
202 	 * Note that this does NOT open a file...
203 	 * Does 'init' deserve its own facility number?
204 	 */
205 	openlog("init", LOG_CONS|LOG_ODELAY, LOG_AUTH);
206 
207 	/*
208 	 * Create an initial session.
209 	 */
210 	if (setsid() < 0)
211 		warning("initial setsid() failed: %m");
212 
213 	/*
214 	 * Establish an initial user so that programs running
215 	 * single user do not freak out and die (like passwd).
216 	 */
217 	if (setlogin("root") < 0)
218 		warning("setlogin() failed: %m");
219 
220 	/*
221 	 * This code assumes that we always get arguments through flags,
222 	 * never through bits set in some random machine register.
223 	 */
224 	while ((c = getopt(argc, argv, "sf")) != -1)
225 		switch (c) {
226 		case 's':
227 			requested_transition = single_user;
228 			break;
229 		case 'f':
230 			runcom_mode = FASTBOOT;
231 			break;
232 		default:
233 			warning("unrecognized flag '-%c'", c);
234 			break;
235 		}
236 
237 	if (optind != argc)
238 		warning("ignoring excess arguments");
239 
240 	/*
241 	 * We catch or block signals rather than ignore them,
242 	 * so that they get reset on exec.
243 	 */
244 	handle(badsys, SIGSYS, 0);
245 	handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV,
246 	    SIGBUS, SIGXCPU, SIGXFSZ, 0);
247 	handle(transition_handler, SIGHUP, SIGTERM, SIGTSTP, SIGUSR1, 0);
248 	handle(alrm_handler, SIGALRM, 0);
249 	sigfillset(&mask);
250 	delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
251 	    SIGXCPU, SIGXFSZ, SIGHUP, SIGTERM, SIGUSR1, SIGTSTP, SIGALRM, 0);
252 	sigprocmask(SIG_SETMASK, &mask, NULL);
253 	memset(&sa, 0, sizeof sa);
254 	sigemptyset(&sa.sa_mask);
255 	sa.sa_flags = 0;
256 	sa.sa_handler = SIG_IGN;
257 	(void) sigaction(SIGTTIN, &sa, NULL);
258 	(void) sigaction(SIGTTOU, &sa, NULL);
259 
260 	/*
261 	 * Paranoia.
262 	 */
263 	close(STDIN_FILENO);
264 	close(STDOUT_FILENO);
265 	close(STDERR_FILENO);
266 
267 	/*
268 	 * Start the state machine.
269 	 */
270 	transition(requested_transition);
271 
272 	/*
273 	 * Should never reach here.
274 	 */
275 	exit(1);
276 }
277 
278 /*
279  * Associate a function with a signal handler.
280  */
281 void
handle(sig_t handler,...)282 handle(sig_t handler, ...)
283 {
284 	int sig;
285 	struct sigaction sa;
286 	sigset_t mask_everything;
287 	va_list ap;
288 
289 	va_start(ap, handler);
290 
291 	memset(&sa, 0, sizeof sa);
292 	sa.sa_handler = handler;
293 	sigfillset(&mask_everything);
294 
295 	while ((sig = va_arg(ap, int))) {
296 		sa.sa_mask = mask_everything;
297 		/* XXX SA_RESTART? */
298 		sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0;
299 		sigaction(sig, &sa, NULL);
300 	}
301 	va_end(ap);
302 }
303 
304 /*
305  * Delete a set of signals from a mask.
306  */
307 void
delset(sigset_t * maskp,...)308 delset(sigset_t *maskp, ...)
309 {
310 	int sig;
311 	va_list ap;
312 
313 	va_start(ap, maskp);
314 	while ((sig = va_arg(ap, int)))
315 		sigdelset(maskp, sig);
316 	va_end(ap);
317 }
318 
319 /*
320  * Log a message and sleep for a while (to give someone an opportunity
321  * to read it and to save log or hardcopy output if the problem is chronic).
322  * NB: should send a message to the session logger to avoid blocking.
323  */
324 void
stall(const char * message,...)325 stall(const char *message, ...)
326 {
327 	va_list ap;
328 
329 	va_start(ap, message);
330 	vsyslog(LOG_ALERT, message, ap);
331 	va_end(ap);
332 	closelog();
333 	sleep(STALL_TIMEOUT);
334 }
335 
336 /*
337  * Like stall(), but doesn't sleep.
338  * If cpp had variadic macros, the two functions could be #defines for another.
339  * NB: should send a message to the session logger to avoid blocking.
340  */
341 void
warning(const char * message,...)342 warning(const char *message, ...)
343 {
344 	va_list ap;
345 
346 	va_start(ap, message);
347 	vsyslog(LOG_ALERT, message, ap);
348 	va_end(ap);
349 	closelog();
350 }
351 
352 /*
353  * Log an emergency message.
354  * NB: should send a message to the session logger to avoid blocking.
355  */
356 void
emergency(const char * message,...)357 emergency(const char *message, ...)
358 {
359 	struct syslog_data sdata = SYSLOG_DATA_INIT;
360 	va_list ap;
361 
362 	va_start(ap, message);
363 	vsyslog_r(LOG_EMERG, &sdata, message, ap);
364 	va_end(ap);
365 }
366 
367 /*
368  * Catch a SIGSYS signal.
369  *
370  * These may arise if a system does not support sysctl.
371  * We tolerate up to 25 of these, then throw in the towel.
372  */
373 void
badsys(int sig)374 badsys(int sig)
375 {
376 	static int badcount = 0;
377 
378 	if (badcount++ < 25)
379 		return;
380 	disaster(sig);
381 }
382 
383 /*
384  * Catch an unexpected signal.
385  */
386 void
disaster(int sig)387 disaster(int sig)
388 {
389 	emergency("fatal signal: %s", strsignal(sig));
390 
391 	sleep(STALL_TIMEOUT);
392 	_exit(sig);		/* reboot */
393 }
394 
395 /*
396  * Get the security level of the kernel.
397  */
398 int
getsecuritylevel(void)399 getsecuritylevel(void)
400 {
401 #ifdef KERN_SECURELVL
402 	int name[2], curlevel;
403 	size_t len;
404 
405 	name[0] = CTL_KERN;
406 	name[1] = KERN_SECURELVL;
407 	len = sizeof curlevel;
408 	if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
409 		emergency("cannot get kernel security level: %s",
410 		    strerror(errno));
411 		return (-1);
412 	}
413 	return (curlevel);
414 #else
415 	return (-1);
416 #endif
417 }
418 
419 /*
420  * Set the security level of the kernel.
421  */
422 void
setsecuritylevel(int newlevel)423 setsecuritylevel(int newlevel)
424 {
425 #ifdef KERN_SECURELVL
426 	int name[2], curlevel;
427 
428 	curlevel = getsecuritylevel();
429 	if (newlevel == curlevel)
430 		return;
431 	name[0] = CTL_KERN;
432 	name[1] = KERN_SECURELVL;
433 	if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
434 		emergency(
435 		    "cannot change kernel security level from %d to %d: %s",
436 		    curlevel, newlevel, strerror(errno));
437 		return;
438 	}
439 #ifdef SECURE
440 	warning("kernel security level changed from %d to %d",
441 	    curlevel, newlevel);
442 #endif
443 #endif
444 }
445 
446 /*
447  * Change states in the finite state machine.
448  * The initial state is passed as an argument.
449  */
450 void
transition(state_t s)451 transition(state_t s)
452 {
453 	for (;;)
454 		s = (state_t) (*s)();
455 }
456 
457 /*
458  * Close out the accounting files for a login session.
459  * NB: should send a message to the session logger to avoid blocking.
460  */
461 void
clear_session_logs(session_t * sp)462 clear_session_logs(session_t *sp)
463 {
464 	char *line = sp->se_device + sizeof(_PATH_DEV) - 1;
465 
466 	if (logout(line))
467 		logwtmp(line, "", "");
468 }
469 
470 /*
471  * Start a session and allocate a controlling terminal.
472  * Only called by children of init after forking.
473  */
474 void
setctty(const char * name)475 setctty(const char *name)
476 {
477 	int fd;
478 
479 	(void) revoke(name);
480 	sleep(2);			/* leave DTR low */
481 	if ((fd = open(name, O_RDWR)) == -1) {
482 		stall("can't open %s: %m", name);
483 		_exit(1);
484 	}
485 	if (login_tty(fd) == -1) {
486 		stall("can't get %s for controlling terminal: %m", name);
487 		_exit(1);
488 	}
489 }
490 
491 /*
492  * Bring the system up single user.
493  */
494 state_func_t
single_user(void)495 single_user(void)
496 {
497 	pid_t pid, wpid;
498 	int status;
499 	sigset_t mask;
500 	char shell[MAXPATHLEN];		/* Allocate space here */
501 	char name[MAXPATHLEN];		/* Name (argv[0]) of shell */
502 	char *argv[2];
503 #ifdef SECURE
504 	struct ttyent *typ;
505 	struct passwd *pp;
506 	static const char banner[] =
507 		"Enter root password, or ^D to go multi-user\n";
508 	char *clear, *password;
509 #endif
510 
511 	/* Init shell and name */
512 	strlcpy(shell, _PATH_BSHELL, sizeof shell);
513 	strlcpy(name, "-sh", sizeof name);
514 
515 	/*
516 	 * If the kernel is in secure mode, downgrade it to insecure mode.
517 	 */
518 	if (getsecuritylevel() > 0)
519 		setsecuritylevel(0);
520 
521 	if ((pid = fork()) == 0) {
522 		char dashsh[] = "-sh";
523 
524 		/*
525 		 * Start the single user session.
526 		 */
527 		setctty(_PATH_CONSOLE);
528 
529 #ifdef SECURE
530 		/*
531 		 * Check the root password.
532 		 * We don't care if the console is 'on' by default;
533 		 * it's the only tty that can be 'off' and 'secure'.
534 		 */
535 		typ = getttynam("console");
536 		pp = getpwnam("root");
537 		if (typ && (typ->ty_status & TTY_SECURE) == 0 && pp &&
538 		    *pp->pw_passwd) {
539 			write(STDERR_FILENO, banner, sizeof banner - 1);
540 			for (;;) {
541 				clear = getpass("Password:");
542 				if (clear == 0 || *clear == '\0')
543 					_exit(0);
544 				password = crypt(clear, pp->pw_passwd);
545 				memset(clear, 0, _PASSWORD_LEN);
546 				if (strcmp(password, pp->pw_passwd) == 0)
547 					break;
548 				warning("single-user login failed\n");
549 			}
550 		}
551 		endttyent();
552 		endpwent();
553 #endif /* SECURE */
554 
555 #ifdef DEBUGSHELL
556 		{
557 			char altshell[128], *cp = altshell;
558 			int num;
559 
560 #define	SHREQUEST \
561 	"Enter pathname of shell or RETURN for sh: "
562 
563 			(void)write(STDERR_FILENO,
564 			    SHREQUEST, sizeof(SHREQUEST) - 1);
565 			while ((num = read(STDIN_FILENO, cp, 1)) != -1 &&
566 			    num != 0 && *cp != '\n' && cp < &altshell[127])
567 				cp++;
568 			*cp = '\0';
569 
570 			/* Copy in alternate shell */
571 			if (altshell[0] != '\0'){
572 				char *p;
573 
574 				/* Binary to exec */
575 				strlcpy(shell, altshell, sizeof shell);
576 
577 				/* argv[0] */
578 				p = strrchr(altshell, '/');
579 				if(p == NULL) p = altshell;
580 				else p++;
581 
582 				name[0] = '-';
583 				strlcpy(&name[1], p, sizeof name -1);
584 			}
585 		}
586 #endif /* DEBUGSHELL */
587 
588 		/*
589 		 * Unblock signals.
590 		 * We catch all the interesting ones,
591 		 * and those are reset to SIG_DFL on exec.
592 		 */
593 		sigemptyset(&mask);
594 		sigprocmask(SIG_SETMASK, &mask, NULL);
595 
596 		/*
597 		 * Fire off a shell.
598 		 * If the default one doesn't work, try the Bourne shell.
599 		 */
600 		argv[0] = name;
601 		argv[1] = NULL;
602 		setenv("PATH", _PATH_STDPATH, 1);
603 		execv(shell, argv);
604 		emergency("can't exec %s for single user: %m", shell);
605 
606 		argv[0] = dashsh;
607 		argv[1] = NULL;
608 		execv(_PATH_BSHELL, argv);
609 		emergency("can't exec %s for single user: %m", _PATH_BSHELL);
610 		sleep(STALL_TIMEOUT);
611 		_exit(1);
612 	}
613 
614 	if (pid == -1) {
615 		/*
616 		 * We are seriously hosed.  Do our best.
617 		 */
618 		emergency("can't fork single-user shell, trying again");
619 		while (waitpid(-1, NULL, WNOHANG) > 0)
620 			continue;
621 		return (state_func_t) single_user;
622 	}
623 
624 	requested_transition = 0;
625 	do {
626 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
627 			collect_child(wpid);
628 		if (wpid == -1) {
629 			if (errno == EINTR)
630 				continue;
631 			warning("wait for single-user shell failed: %m; restarting");
632 			return (state_func_t) single_user;
633 		}
634 		if (wpid == pid && WIFSTOPPED(status)) {
635 			warning("init: shell stopped, restarting\n");
636 			kill(pid, SIGCONT);
637 			wpid = -1;
638 		}
639 	} while (wpid != pid && !requested_transition);
640 
641 	arc4random_ctl(1);
642 
643 	if (requested_transition)
644 		return (state_func_t) requested_transition;
645 
646 	if (!WIFEXITED(status)) {
647 		if (WTERMSIG(status) == SIGKILL) {
648 			/*
649 			 *  reboot(8) killed shell?
650 			 */
651 			warning("single user shell terminated.");
652 			sleep(STALL_TIMEOUT);
653 			_exit(0);
654 		} else {
655 			warning("single user shell terminated, restarting");
656 			return (state_func_t) single_user;
657 		}
658 	}
659 
660 	runcom_mode = FASTBOOT;
661 	return (state_func_t) runcom;
662 }
663 
664 /*
665  * Run the system startup script.
666  */
667 state_func_t
runcom(void)668 runcom(void)
669 {
670 	pid_t pid, wpid;
671 	int status;
672 	char *argv[4];
673 	struct sigaction sa;
674 
675 	if ((pid = fork()) == 0) {
676 		char esha[] = "sh", autoboot[] = "autoboot";;
677 
678 		memset(&sa, 0, sizeof sa);
679 		sigemptyset(&sa.sa_mask);
680 		sa.sa_flags = 0;
681 		sa.sa_handler = SIG_IGN;
682 		(void) sigaction(SIGTSTP, &sa, NULL);
683 		(void) sigaction(SIGHUP, &sa, NULL);
684 
685 		setctty(_PATH_CONSOLE);
686 
687 		argv[0] = esha;
688 		argv[1] = strdup(_PATH_RUNCOM);
689 		argv[2] = runcom_mode == AUTOBOOT ? autoboot : NULL;
690 		argv[3] = NULL;
691 
692 		sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
693 
694 		setprocresources(RESOURCE_RC);
695 
696 		execv(_PATH_BSHELL, argv);
697 		stall("can't exec %s for %s: %m", _PATH_BSHELL, _PATH_RUNCOM);
698 		_exit(1);	/* force single user mode */
699 	}
700 
701 	if (pid == -1) {
702 		emergency("can't fork for %s on %s: %m",
703 			_PATH_BSHELL, _PATH_RUNCOM);
704 		while (waitpid(-1, NULL, WNOHANG) > 0)
705 			continue;
706 		sleep(STALL_TIMEOUT);
707 		return (state_func_t) single_user;
708 	}
709 
710 	/*
711 	 * Copied from single_user().  This is a bit paranoid.
712 	 */
713 	do {
714 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
715 			collect_child(wpid);
716 		if (wpid == -1) {
717 			if (errno == EINTR)
718 				continue;
719 			warning("wait for %s on %s failed: %m; going to single user mode",
720 			    _PATH_BSHELL, _PATH_RUNCOM);
721 			return (state_func_t) single_user;
722 		}
723 		if (wpid == pid && WIFSTOPPED(status)) {
724 			warning("init: %s on %s stopped, restarting\n",
725 			    _PATH_BSHELL, _PATH_RUNCOM);
726 			kill(pid, SIGCONT);
727 			wpid = -1;
728 		}
729 	} while (wpid != pid);
730 
731 	arc4random_ctl(1);
732 
733 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
734 	    requested_transition == catatonia) {
735 		/* /etc/rc executed /sbin/reboot; wait for the end quietly */
736 		sigset_t s;
737 
738 		sigfillset(&s);
739 		for (;;)
740 			sigsuspend(&s);
741 	}
742 
743 	if (!WIFEXITED(status)) {
744 		warning("%s on %s terminated abnormally, going to single user mode",
745 		    _PATH_BSHELL, _PATH_RUNCOM);
746 		return (state_func_t) single_user;
747 	}
748 
749 	if (WEXITSTATUS(status))
750 		return (state_func_t) single_user;
751 
752 	runcom_mode = AUTOBOOT;		/* the default */
753 	/* NB: should send a message to the session logger to avoid blocking. */
754 	logwtmp("~", "reboot", "");
755 	return (state_func_t) read_ttys;
756 }
757 
758 /*
759  * Open the session database.
760  *
761  * NB: We could pass in the size here; is it necessary?
762  */
763 int
start_session_db(void)764 start_session_db(void)
765 {
766 	if (session_db && (*session_db->close)(session_db))
767 		emergency("session database close: %s", strerror(errno));
768 	if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) {
769 		emergency("session database open: %s", strerror(errno));
770 		return (1);
771 	}
772 	return (0);
773 }
774 
775 /*
776  * Add a new login session.
777  */
778 void
add_session(session_t * sp)779 add_session(session_t *sp)
780 {
781 	DBT key;
782 	DBT data;
783 
784 	key.data = &sp->se_process;
785 	key.size = sizeof sp->se_process;
786 	data.data = &sp;
787 	data.size = sizeof sp;
788 
789 	if ((*session_db->put)(session_db, &key, &data, 0))
790 		emergency("insert %d: %s", sp->se_process, strerror(errno));
791 }
792 
793 /*
794  * Delete an old login session.
795  */
796 void
del_session(session_t * sp)797 del_session(session_t *sp)
798 {
799 	DBT key;
800 
801 	key.data = &sp->se_process;
802 	key.size = sizeof sp->se_process;
803 
804 	if ((*session_db->del)(session_db, &key, 0))
805 		emergency("delete %d: %s", sp->se_process, strerror(errno));
806 }
807 
808 /*
809  * Look up a login session by pid.
810  */
811 session_t *
find_session(pid_t pid)812 find_session(pid_t pid)
813 {
814 	DBT key;
815 	DBT data;
816 	session_t *ret;
817 
818 	key.data = &pid;
819 	key.size = sizeof pid;
820 	if ((*session_db->get)(session_db, &key, &data, 0) != 0)
821 		return (0);
822 	memcpy(&ret, data.data, sizeof(ret));
823 	return (ret);
824 }
825 
826 /*
827  * Construct an argument vector from a command line.
828  */
829 char **
construct_argv(char * command)830 construct_argv(char *command)
831 {
832 	int argc = 0;
833 	char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1) *
834 	    sizeof (char *));
835 	static const char separators[] = " \t";
836 
837 	if ((argv[argc++] = strtok(command, separators)) == 0)
838 		return (0);
839 	while ((argv[argc++] = strtok(NULL, separators)))
840 		continue;
841 	return (argv);
842 }
843 
844 /*
845  * Deallocate a session descriptor.
846  */
847 void
free_session(session_t * sp)848 free_session(session_t *sp)
849 {
850 	free(sp->se_device);
851 	if (sp->se_getty) {
852 		free(sp->se_getty);
853 		free(sp->se_getty_argv);
854 	}
855 	if (sp->se_window) {
856 		free(sp->se_window);
857 		free(sp->se_window_argv);
858 	}
859 	free(sp);
860 }
861 
862 /*
863  * Allocate a new session descriptor.
864  */
865 session_t *
new_session(session_t * sprev,int session_index,struct ttyent * typ)866 new_session(session_t *sprev, int session_index, struct ttyent *typ)
867 {
868 	session_t *sp;
869 	size_t len;
870 
871 	if ((typ->ty_status & TTY_ON) == 0 ||
872 	    typ->ty_name == 0 ||
873 	    typ->ty_getty == 0)
874 		return (0);
875 
876 	sp = (session_t *) malloc(sizeof (session_t));
877 	memset(sp, 0, sizeof *sp);
878 
879 	sp->se_flags = SE_PRESENT;
880 	sp->se_index = session_index;
881 
882 	len = sizeof(_PATH_DEV) + strlen(typ->ty_name);
883 	sp->se_device = malloc(len);
884 	(void) snprintf(sp->se_device, len, "%s%s", _PATH_DEV, typ->ty_name);
885 
886 	if (setupargv(sp, typ) == 0) {
887 		free_session(sp);
888 		return (0);
889 	}
890 
891 	sp->se_next = 0;
892 	if (sprev == 0) {
893 		sessions = sp;
894 		sp->se_prev = 0;
895 	} else {
896 		sprev->se_next = sp;
897 		sp->se_prev = sprev;
898 	}
899 
900 	return (sp);
901 }
902 
903 /*
904  * Calculate getty and if useful window argv vectors.
905  */
906 int
setupargv(session_t * sp,struct ttyent * typ)907 setupargv(session_t *sp, struct ttyent *typ)
908 {
909 	size_t len;
910 
911 	if (sp->se_getty) {
912 		free(sp->se_getty);
913 		free(sp->se_getty_argv);
914 	}
915 	len = strlen(typ->ty_getty) + strlen(typ->ty_name) + 2;
916 	sp->se_getty = malloc(len);
917 	(void) snprintf(sp->se_getty, len, "%s %s", typ->ty_getty, typ->ty_name);
918 	sp->se_getty_argv = construct_argv(sp->se_getty);
919 	if (sp->se_getty_argv == 0) {
920 		warning("can't parse getty for port %s", sp->se_device);
921 		free(sp->se_getty);
922 		sp->se_getty = 0;
923 		return (0);
924 	}
925 	if (typ->ty_window) {
926 		if (sp->se_window)
927 			free(sp->se_window);
928 		sp->se_window = strdup(typ->ty_window);
929 		if (sp->se_window == NULL) {
930 			warning("can't allocate window");
931 			return (0);
932 		}
933 		sp->se_window_argv = construct_argv(sp->se_window);
934 		if (sp->se_window_argv == NULL) {
935 			warning("can't parse window for port %s",
936 			    sp->se_device);
937 			free(sp->se_window);
938 			sp->se_window = NULL;
939 			return (0);
940 		}
941 	}
942 	return (1);
943 }
944 
945 /*
946  * Walk the list of ttys and create sessions for each active line.
947  */
948 state_func_t
read_ttys(void)949 read_ttys(void)
950 {
951 	int session_index = 0;
952 	session_t *sp, *snext;
953 	struct ttyent *typ;
954 
955 	/*
956 	 * Destroy any previous session state.
957 	 * There shouldn't be any, but just in case...
958 	 */
959 	for (sp = sessions; sp; sp = snext) {
960 		if (sp->se_process)
961 			clear_session_logs(sp);
962 		snext = sp->se_next;
963 		free_session(sp);
964 	}
965 	sessions = 0;
966 	if (start_session_db())
967 		return (state_func_t) single_user;
968 
969 	/*
970 	 * Allocate a session entry for each active port.
971 	 * Note that sp starts at 0.
972 	 */
973 	while ((typ = getttyent()))
974 		if ((snext = new_session(sp, ++session_index, typ)))
975 			sp = snext;
976 
977 	endttyent();
978 
979 	return (state_func_t) multi_user;
980 }
981 
982 /*
983  * Start a window system running.
984  */
985 void
start_window_system(session_t * sp)986 start_window_system(session_t *sp)
987 {
988 	pid_t pid;
989 	sigset_t mask;
990 
991 	if ((pid = fork()) == -1) {
992 		emergency("can't fork for window system on port %s: %m",
993 		    sp->se_device);
994 		/* hope that getty fails and we can try again */
995 		return;
996 	}
997 
998 	if (pid)
999 		return;
1000 
1001 	sigemptyset(&mask);
1002 	sigprocmask(SIG_SETMASK, &mask, NULL);
1003 
1004 	if (setsid() < 0)
1005 		emergency("setsid failed (window) %m");
1006 
1007 	setprocresources(RESOURCE_WINDOW);
1008 
1009 	execv(sp->se_window_argv[0], sp->se_window_argv);
1010 	stall("can't exec window system '%s' for port %s: %m",
1011 	    sp->se_window_argv[0], sp->se_device);
1012 	_exit(1);
1013 }
1014 
1015 /*
1016  * Start a login session running.
1017  * For first open, man-handle tty directly to determine if it
1018  * really exists. It is not efficient to spawn gettys on devices
1019  * that do not exist.
1020  */
1021 pid_t
start_getty(session_t * sp)1022 start_getty(session_t *sp)
1023 {
1024 	pid_t pid;
1025 	sigset_t mask;
1026 	time_t current_time = time(NULL);
1027 	int p[2], new = 1;
1028 
1029 	if (sp->se_flags & SE_DEVEXISTS)
1030 		new = 0;
1031 
1032 	if (new) {
1033 		if (pipe(p) == -1)
1034 			return (-1);
1035 	}
1036 
1037 	/*
1038 	 * fork(), not vfork() -- we can't afford to block.
1039 	 */
1040 	if ((pid = fork()) == -1) {
1041 		emergency("can't fork for getty on port %s: %m", sp->se_device);
1042 		return (-1);
1043 	}
1044 
1045 	if (pid) {
1046 		if (new) {
1047 			char c;
1048 
1049 			close(p[1]);
1050 			if (read(p[0], &c, 1) != 1) {
1051 				close(p[0]);
1052 				return (-1);
1053 			}
1054 			close(p[0]);
1055 			if (c == '1')
1056 				sp->se_flags |= SE_DEVEXISTS;
1057 			else
1058 				sp->se_flags |= SE_SHUTDOWN;
1059 		}
1060 		return (pid);
1061 	}
1062 	if (new) {
1063 		int fd;
1064 
1065 		close(p[0]);
1066 		fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0666);
1067 		if (fd == -1 && (errno == ENXIO || errno == ENOENT ||
1068 		    errno == EISDIR)) {
1069 			(void)write(p[1], "0", 1);
1070 			close(p[1]);
1071 			_exit(1);
1072 		}
1073 		(void)write(p[1], "1", 1);
1074 		close(p[1]);
1075 		close(fd);
1076 		sleep(1);
1077 	}
1078 
1079 	if (current_time > sp->se_started &&
1080 	    current_time - sp->se_started < GETTY_SPACING) {
1081 		warning("getty repeating too quickly on port %s, sleeping",
1082 		    sp->se_device);
1083 		sleep((unsigned) GETTY_SLEEP);
1084 	}
1085 
1086 	if (sp->se_window) {
1087 		start_window_system(sp);
1088 		sleep(WINDOW_WAIT);
1089 	}
1090 
1091 	sigemptyset(&mask);
1092 	sigprocmask(SIG_SETMASK, &mask, NULL);
1093 
1094 	setprocresources(RESOURCE_GETTY);
1095 
1096 	execv(sp->se_getty_argv[0], sp->se_getty_argv);
1097 	stall("can't exec getty '%s' for port %s: %m",
1098 	    sp->se_getty_argv[0], sp->se_device);
1099 	_exit(1);
1100 }
1101 
1102 /*
1103  * Collect exit status for a child.
1104  * If an exiting login, start a new login running.
1105  */
1106 void
collect_child(pid_t pid)1107 collect_child(pid_t pid)
1108 {
1109 	session_t *sp, *sprev, *snext;
1110 
1111 	if (sessions == NULL)
1112 		return;
1113 
1114 	if ((sp = find_session(pid)) == NULL)
1115 		return;
1116 
1117 	clear_session_logs(sp);
1118 	login_fbtab(sp->se_device + sizeof(_PATH_DEV) - 1, 0, 0);
1119 	del_session(sp);
1120 	sp->se_process = 0;
1121 
1122 	if (sp->se_flags & SE_SHUTDOWN) {
1123 		if ((sprev = sp->se_prev))
1124 			sprev->se_next = sp->se_next;
1125 		else
1126 			sessions = sp->se_next;
1127 		if ((snext = sp->se_next))
1128 			snext->se_prev = sp->se_prev;
1129 		free_session(sp);
1130 		return;
1131 	}
1132 
1133 	if ((pid = start_getty(sp)) == -1) {
1134 		/* serious trouble */
1135 		requested_transition = clean_ttys;
1136 		return;
1137 	}
1138 
1139 	sp->se_process = pid;
1140 	sp->se_started = time(NULL);
1141 	add_session(sp);
1142 }
1143 
1144 /*
1145  * Catch a signal and request a state transition.
1146  */
1147 void
transition_handler(int sig)1148 transition_handler(int sig)
1149 {
1150 
1151 	switch (sig) {
1152 	case SIGHUP:
1153 		requested_transition = clean_ttys;
1154 		break;
1155 	case SIGTERM:
1156 		requested_transition = death;
1157 		break;
1158 	case SIGUSR1:
1159 		requested_transition = nice_death;
1160 		break;
1161 	case SIGTSTP:
1162 		requested_transition = catatonia;
1163 		break;
1164 	default:
1165 		requested_transition = 0;
1166 		break;
1167 	}
1168 }
1169 
1170 /*
1171  * Take the system multiuser.
1172  */
1173 state_func_t
multi_user(void)1174 multi_user(void)
1175 {
1176 	pid_t pid;
1177 	session_t *sp;
1178 
1179 	requested_transition = 0;
1180 
1181 	/*
1182 	 * If the administrator has not set the security level to -1
1183 	 * to indicate that the kernel should not run multiuser in secure
1184 	 * mode, and the run script has not set a higher level of security
1185 	 * than level 1, then put the kernel into secure mode.
1186 	 */
1187 	if (getsecuritylevel() == 0)
1188 		setsecuritylevel(1);
1189 
1190 	for (sp = sessions; sp; sp = sp->se_next) {
1191 		if (sp->se_process)
1192 			continue;
1193 		if ((pid = start_getty(sp)) == -1) {
1194 			/* serious trouble */
1195 			requested_transition = clean_ttys;
1196 			break;
1197 		}
1198 		sp->se_process = pid;
1199 		sp->se_started = time(NULL);
1200 		add_session(sp);
1201 	}
1202 
1203 	while (!requested_transition)
1204 		if ((pid = waitpid(-1, NULL, 0)) != -1)
1205 			collect_child(pid);
1206 
1207 	return (state_func_t) requested_transition;
1208 }
1209 
1210 /*
1211  * This is an n-squared algorithm.  We hope it isn't run often...
1212  */
1213 state_func_t
clean_ttys(void)1214 clean_ttys(void)
1215 {
1216 	session_t *sp, *sprev;
1217 	struct ttyent *typ;
1218 	int session_index = 0;
1219 	int devlen;
1220 
1221 	for (sp = sessions; sp; sp = sp->se_next)
1222 		sp->se_flags &= ~SE_PRESENT;
1223 
1224 	devlen = sizeof(_PATH_DEV) - 1;
1225 	while ((typ = getttyent())) {
1226 		++session_index;
1227 
1228 		for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
1229 			if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
1230 				break;
1231 
1232 		if (sp) {
1233 			sp->se_flags |= SE_PRESENT;
1234 			if (sp->se_index != session_index) {
1235 				warning("port %s changed utmp index from %d to %d",
1236 				    sp->se_device, sp->se_index,
1237 				    session_index);
1238 				sp->se_index = session_index;
1239 			}
1240 			if ((typ->ty_status & TTY_ON) == 0 ||
1241 			    typ->ty_getty == 0) {
1242 				sp->se_flags |= SE_SHUTDOWN;
1243 				kill(sp->se_process, SIGHUP);
1244 				continue;
1245 			}
1246 			sp->se_flags &= ~SE_SHUTDOWN;
1247 			if (setupargv(sp, typ) == 0) {
1248 				warning("can't parse getty for port %s",
1249 				    sp->se_device);
1250 				sp->se_flags |= SE_SHUTDOWN;
1251 				kill(sp->se_process, SIGHUP);
1252 			}
1253 			continue;
1254 		}
1255 
1256 		new_session(sprev, session_index, typ);
1257 	}
1258 
1259 	endttyent();
1260 
1261 	for (sp = sessions; sp; sp = sp->se_next)
1262 		if ((sp->se_flags & SE_PRESENT) == 0) {
1263 			sp->se_flags |= SE_SHUTDOWN;
1264 			kill(sp->se_process, SIGHUP);
1265 		}
1266 
1267 	return (state_func_t) multi_user;
1268 }
1269 
1270 /*
1271  * Block further logins.
1272  */
1273 state_func_t
catatonia(void)1274 catatonia(void)
1275 {
1276 #ifndef NORNDSHUF
1277 	int arnd_fd;
1278 #endif
1279 	session_t *sp;
1280 
1281 	arc4random_ctl(1);
1282 #ifndef NORNDSHUF
1283 	arnd_fd = open(_PATH_ARANDOMDEV, O_RDWR);
1284 	if (arnd_fd != -1) {
1285 		/* trigger a reset of all kernel entropy pools */
1286 		ioctl(arnd_fd, RNDSTIRARC4);
1287 		close(arnd_fd);
1288 	}
1289 #endif
1290 
1291 	for (sp = sessions; sp; sp = sp->se_next)
1292 		sp->se_flags |= SE_SHUTDOWN;
1293 
1294 	return (state_func_t) multi_user;
1295 }
1296 
1297 /*
1298  * Note SIGALRM.
1299  */
1300 void
alrm_handler(int sig __unused)1301 alrm_handler(int sig __unused)
1302 {
1303 	clang = 1;
1304 }
1305 
1306 /*
1307  * Bring the system down to single user nicely, after run the shutdown script.
1308  */
1309 state_func_t
nice_death(void)1310 nice_death(void)
1311 {
1312 	session_t *sp;
1313 	int i;
1314 #ifndef NORNDSHUF
1315 	int rnd_fd, arnd_fd;
1316 #endif
1317 	pid_t pid;
1318 	static const int death_sigs[3] = { SIGHUP, SIGTERM, SIGKILL };
1319 	int howto = RB_HALT;
1320 	int status, needwrites = 4;
1321 #ifndef NORNDSHUF
1322 	char rnd_buf[512];
1323 
1324 	arnd_fd = open(_PATH_ARANDOMDEV, O_RDWR);
1325 	if (arnd_fd != -1) {
1326 		/* also shove some of our state into the kernel */
1327 		arc4random_buf(rnd_buf, 16);
1328 		/* trigger a reset of arandom(4), arc4random(9) */
1329 		write(arnd_fd, rnd_buf, 16);
1330 	}
1331 #endif
1332 
1333 	for (sp = sessions; sp; sp = sp->se_next) {
1334 		sp->se_flags &= ~SE_PRESENT;
1335 		sp->se_flags |= SE_SHUTDOWN;
1336 		kill(sp->se_process, SIGHUP);
1337 	}
1338 
1339 	/* terminate the accounting process */
1340 	acct(NULL);
1341 
1342 	/* NB: should send a message to the session logger to avoid blocking. */
1343 	logwtmp("~", "shutdown", "");
1344 
1345 	if (access(_PATH_RUNCOM, R_OK) != -1) {
1346 		pid_t pid_;
1347 		struct sigaction sa;
1348 
1349 		switch ((pid_ = fork())) {
1350 		case -1:
1351 			break;
1352 		case 0:
1353 
1354 			memset(&sa, 0, sizeof sa);
1355 			sigemptyset(&sa.sa_mask);
1356 			sa.sa_flags = 0;
1357 			sa.sa_handler = SIG_IGN;
1358 			(void) sigaction(SIGTSTP, &sa, NULL);
1359 			(void) sigaction(SIGHUP, &sa, NULL);
1360 
1361 			setctty(_PATH_CONSOLE);
1362 
1363 			sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
1364 
1365 			execl(_PATH_BSHELL, "sh", _PATH_RUNCOM, "shutdown",
1366 			    (char *)NULL);
1367 			stall("can't exec %s for %s %s: %m", _PATH_BSHELL,
1368 			    _PATH_RUNCOM, "shutdown");
1369 			_exit(1);
1370 		default:
1371 			waitpid(pid_, &status, 0);
1372 			if (WIFEXITED(status) && WEXITSTATUS(status) == 2)
1373 				howto |= RB_POWERDOWN;
1374 		}
1375 	}
1376 
1377 	arc4random_ctl(1);
1378 #ifndef NORNDSHUF
1379 	rnd_fd = open(_PATH_HOSTRANDOM, O_WRONLY | O_APPEND | O_SYNC);
1380 #endif
1381 
1382 	for (i = 0; i < 3; ++i) {
1383 		warning("Sending SIG%s to all processes...",
1384 		    sys_signame[death_sigs[i]]);
1385 
1386 		if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
1387 			goto die;
1388 
1389 		clang = 0;
1390 		alarm(DEATH_WATCH);
1391 		do {
1392 			if ((pid = waitpid(-1, NULL, 0)) != -1)
1393 				collect_child(pid);
1394 		} while (clang == 0 && errno != ECHILD);
1395 		status = errno;
1396 
1397 #ifndef NORNDSHUF
1398 		if (arnd_fd != -1)
1399 			/* reset lopool, arandom */
1400 			ioctl(arnd_fd, RNDSTIRARC4);
1401 		if (rnd_fd != -1) {
1402 			if (arnd_fd != -1)
1403 				read(arnd_fd, rnd_buf, sizeof(rnd_buf));
1404 			else
1405 				arc4random_buf(rnd_buf, sizeof(rnd_buf));
1406 			write(rnd_fd, rnd_buf, sizeof(rnd_buf));
1407 			--needwrites;
1408 		}
1409 #endif
1410 
1411 		if (status == ECHILD)
1412 			goto die;
1413 	}
1414 
1415 	warning("some processes would not die; ps axl advised");
1416 
1417  die:
1418 	arc4random_ctl(1);
1419 #ifndef NORNDSHUF
1420 	if (rnd_fd != -1) {
1421 		while (needwrites--) {
1422 			arc4random_buf(rnd_buf, sizeof(rnd_buf));
1423 			write(rnd_fd, rnd_buf, sizeof(rnd_buf));
1424 		}
1425 		close(rnd_fd);
1426 	}
1427 	if (arnd_fd != -1) {
1428 		ioctl(arnd_fd, RNDSTIRARC4);
1429 		close(arnd_fd);
1430 	}
1431 #endif
1432 	reboot(howto);
1433 
1434 	/* ... and if that fails.. oh well */
1435 	return (state_func_t) single_user;
1436 }
1437 
1438 /*
1439  * Bring the system down to single user.
1440  */
1441 state_func_t
death(void)1442 death(void)
1443 {
1444 	session_t *sp;
1445 	int i;
1446 	pid_t pid;
1447 	static const int death_sigs[3] = { SIGHUP, SIGTERM, SIGKILL };
1448 
1449 	/* terminate the accounting process */
1450 	acct(NULL);
1451 
1452 	for (sp = sessions; sp; sp = sp->se_next)
1453 		sp->se_flags |= SE_SHUTDOWN;
1454 
1455 	/* NB: should send a message to the session logger to avoid blocking. */
1456 	logwtmp("~", "shutdown", "");
1457 
1458 	for (i = 0; i < 3; ++i) {
1459 		if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
1460 			return (state_func_t) single_user;
1461 
1462 		clang = 0;
1463 		alarm(DEATH_WATCH);
1464 		do {
1465 			if ((pid = waitpid(-1, NULL, 0)) != -1)
1466 				collect_child(pid);
1467 		} while (clang == 0 && errno != ECHILD);
1468 
1469 		if (errno == ECHILD)
1470 			return (state_func_t) single_user;
1471 	}
1472 
1473 	warning("some processes would not die; ps axl advised");
1474 
1475 	return (state_func_t) single_user;
1476 }
1477 
1478 #ifdef LOGIN_CAP
1479 void
setprocresources(const char * class)1480 setprocresources(const char *class)
1481 {
1482 	login_cap_t *lc;
1483 	char *cp;
1484 
1485 	cp = strdup(class);
1486 	if ((lc = login_getclass(cp)) != NULL) {
1487 		setusercontext(lc, NULL, 0,
1488 		    LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK);
1489 		login_close(lc);
1490 	}
1491 	free(cp);
1492 }
1493 #endif
1494