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