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