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