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