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