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 #define _ULOG_POSIX_NAMES 69 #include <ulog.h> 70 #include <unistd.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) 573 { 574 575 ulog_logout(sp->se_device); 576 } 577 578 /* 579 * Start a session and allocate a controlling terminal. 580 * Only called by children of init after forking. 581 */ 582 static void 583 setctty(const char *name) 584 { 585 int fd; 586 587 revoke(name); 588 if ((fd = open(name, O_RDWR)) == -1) { 589 stall("can't open %s: %m", name); 590 _exit(1); 591 } 592 if (login_tty(fd) == -1) { 593 stall("can't get %s for controlling terminal: %m", name); 594 _exit(1); 595 } 596 } 597 598 static const char * 599 get_shell(void) 600 { 601 static char kenv_value[PATH_MAX]; 602 603 if (kenv(KENV_GET, "init_shell", kenv_value, sizeof(kenv_value)) > 0) 604 return kenv_value; 605 else 606 return _PATH_BSHELL; 607 } 608 609 static void 610 write_stderr(const char *message) 611 { 612 613 write(STDERR_FILENO, message, strlen(message)); 614 } 615 616 /* 617 * Bring the system up single user. 618 */ 619 static state_func_t 620 single_user(void) 621 { 622 pid_t pid, wpid; 623 int status; 624 sigset_t mask; 625 const char *shell; 626 char *argv[2]; 627 #ifdef SECURE 628 struct ttyent *typ; 629 struct passwd *pp; 630 static const char banner[] = 631 "Enter root password, or ^D to go multi-user\n"; 632 char *clear, *password; 633 #endif 634 #ifdef DEBUGSHELL 635 char altshell[128]; 636 #endif 637 638 if (Reboot) { 639 /* Instead of going single user, let's reboot the machine */ 640 sync(); 641 alarm(2); 642 pause(); 643 reboot(howto); 644 _exit(0); 645 } 646 647 shell = get_shell(); 648 649 if ((pid = fork()) == 0) { 650 /* 651 * Start the single user session. 652 */ 653 setctty(_PATH_CONSOLE); 654 655 #ifdef SECURE 656 /* 657 * Check the root password. 658 * We don't care if the console is 'on' by default; 659 * it's the only tty that can be 'off' and 'secure'. 660 */ 661 typ = getttynam("console"); 662 pp = getpwnam("root"); 663 if (typ && (typ->ty_status & TTY_SECURE) == 0 && 664 pp && *pp->pw_passwd) { 665 write_stderr(banner); 666 for (;;) { 667 clear = getpass("Password:"); 668 if (clear == 0 || *clear == '\0') 669 _exit(0); 670 password = crypt(clear, pp->pw_passwd); 671 bzero(clear, _PASSWORD_LEN); 672 if (strcmp(password, pp->pw_passwd) == 0) 673 break; 674 warning("single-user login failed\n"); 675 } 676 } 677 endttyent(); 678 endpwent(); 679 #endif /* SECURE */ 680 681 #ifdef DEBUGSHELL 682 { 683 char *cp = altshell; 684 int num; 685 686 #define SHREQUEST "Enter full pathname of shell or RETURN for " 687 write_stderr(SHREQUEST); 688 write_stderr(shell); 689 write_stderr(": "); 690 while ((num = read(STDIN_FILENO, cp, 1)) != -1 && 691 num != 0 && *cp != '\n' && cp < &altshell[127]) 692 cp++; 693 *cp = '\0'; 694 if (altshell[0] != '\0') 695 shell = altshell; 696 } 697 #endif /* DEBUGSHELL */ 698 699 /* 700 * Unblock signals. 701 * We catch all the interesting ones, 702 * and those are reset to SIG_DFL on exec. 703 */ 704 sigemptyset(&mask); 705 sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); 706 707 /* 708 * Fire off a shell. 709 * If the default one doesn't work, try the Bourne shell. 710 */ 711 712 char name[] = "-sh"; 713 714 argv[0] = name; 715 argv[1] = 0; 716 execv(shell, argv); 717 emergency("can't exec %s for single user: %m", shell); 718 execv(_PATH_BSHELL, argv); 719 emergency("can't exec %s for single user: %m", _PATH_BSHELL); 720 sleep(STALL_TIMEOUT); 721 _exit(1); 722 } 723 724 if (pid == -1) { 725 /* 726 * We are seriously hosed. Do our best. 727 */ 728 emergency("can't fork single-user shell, trying again"); 729 while (waitpid(-1, (int *) 0, WNOHANG) > 0) 730 continue; 731 return (state_func_t) single_user; 732 } 733 734 requested_transition = 0; 735 do { 736 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 737 collect_child(wpid); 738 if (wpid == -1) { 739 if (errno == EINTR) 740 continue; 741 warning("wait for single-user shell failed: %m; restarting"); 742 return (state_func_t) single_user; 743 } 744 if (wpid == pid && WIFSTOPPED(status)) { 745 warning("init: shell stopped, restarting\n"); 746 kill(pid, SIGCONT); 747 wpid = -1; 748 } 749 } while (wpid != pid && !requested_transition); 750 751 if (requested_transition) 752 return (state_func_t) requested_transition; 753 754 if (!WIFEXITED(status)) { 755 if (WTERMSIG(status) == SIGKILL) { 756 /* 757 * reboot(8) killed shell? 758 */ 759 warning("single user shell terminated."); 760 sleep(STALL_TIMEOUT); 761 _exit(0); 762 } else { 763 warning("single user shell terminated, restarting"); 764 return (state_func_t) single_user; 765 } 766 } 767 768 runcom_mode = FASTBOOT; 769 return (state_func_t) runcom; 770 } 771 772 /* 773 * Run the system startup script. 774 */ 775 static state_func_t 776 runcom(void) 777 { 778 struct utmpx utx; 779 state_func_t next_transition; 780 781 if ((next_transition = run_script(_PATH_RUNCOM)) != 0) 782 return next_transition; 783 784 runcom_mode = AUTOBOOT; /* the default */ 785 /* NB: should send a message to the session logger to avoid blocking. */ 786 utx.ut_type = BOOT_TIME; 787 gettimeofday(&utx.ut_tv, NULL); 788 pututxline(&utx); 789 return (state_func_t) read_ttys; 790 } 791 792 /* 793 * Run a shell script. 794 * Returns 0 on success, otherwise the next transition to enter: 795 * - single_user if fork/execv/waitpid failed, or if the script 796 * terminated with a signal or exit code != 0. 797 * - death if a SIGTERM was delivered to init(8). 798 */ 799 static state_func_t 800 run_script(const char *script) 801 { 802 pid_t pid, wpid; 803 int status; 804 char *argv[4]; 805 const char *shell; 806 struct sigaction sa; 807 808 shell = get_shell(); 809 810 if ((pid = fork()) == 0) { 811 sigemptyset(&sa.sa_mask); 812 sa.sa_flags = 0; 813 sa.sa_handler = SIG_IGN; 814 sigaction(SIGTSTP, &sa, (struct sigaction *)0); 815 sigaction(SIGHUP, &sa, (struct sigaction *)0); 816 817 setctty(_PATH_CONSOLE); 818 819 char _sh[] = "sh"; 820 char _autoboot[] = "autoboot"; 821 822 argv[0] = _sh; 823 argv[1] = __DECONST(char *, script); 824 argv[2] = runcom_mode == AUTOBOOT ? _autoboot : 0; 825 argv[3] = 0; 826 827 sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0); 828 829 #ifdef LOGIN_CAP 830 setprocresources(RESOURCE_RC); 831 #endif 832 execv(shell, argv); 833 stall("can't exec %s for %s: %m", shell, script); 834 _exit(1); /* force single user mode */ 835 } 836 837 if (pid == -1) { 838 emergency("can't fork for %s on %s: %m", shell, script); 839 while (waitpid(-1, (int *) 0, WNOHANG) > 0) 840 continue; 841 sleep(STALL_TIMEOUT); 842 return (state_func_t) single_user; 843 } 844 845 /* 846 * Copied from single_user(). This is a bit paranoid. 847 */ 848 requested_transition = 0; 849 do { 850 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 851 collect_child(wpid); 852 if (wpid == -1) { 853 if (requested_transition == death) 854 return (state_func_t) death; 855 if (errno == EINTR) 856 continue; 857 warning("wait for %s on %s failed: %m; going to " 858 "single user mode", shell, script); 859 return (state_func_t) single_user; 860 } 861 if (wpid == pid && WIFSTOPPED(status)) { 862 warning("init: %s on %s stopped, restarting\n", 863 shell, script); 864 kill(pid, SIGCONT); 865 wpid = -1; 866 } 867 } while (wpid != pid); 868 869 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM && 870 requested_transition == catatonia) { 871 /* /etc/rc executed /sbin/reboot; wait for the end quietly */ 872 sigset_t s; 873 874 sigfillset(&s); 875 for (;;) 876 sigsuspend(&s); 877 } 878 879 if (!WIFEXITED(status)) { 880 warning("%s on %s terminated abnormally, going to single " 881 "user mode", shell, script); 882 return (state_func_t) single_user; 883 } 884 885 if (WEXITSTATUS(status)) 886 return (state_func_t) single_user; 887 888 return (state_func_t) 0; 889 } 890 891 /* 892 * Open the session database. 893 * 894 * NB: We could pass in the size here; is it necessary? 895 */ 896 static int 897 start_session_db(void) 898 { 899 if (session_db && (*session_db->close)(session_db)) 900 emergency("session database close: %s", strerror(errno)); 901 if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) { 902 emergency("session database open: %s", strerror(errno)); 903 return (1); 904 } 905 return (0); 906 907 } 908 909 /* 910 * Add a new login session. 911 */ 912 static void 913 add_session(session_t *sp) 914 { 915 DBT key; 916 DBT data; 917 918 key.data = &sp->se_process; 919 key.size = sizeof sp->se_process; 920 data.data = &sp; 921 data.size = sizeof sp; 922 923 if ((*session_db->put)(session_db, &key, &data, 0)) 924 emergency("insert %d: %s", sp->se_process, strerror(errno)); 925 } 926 927 /* 928 * Delete an old login session. 929 */ 930 static void 931 del_session(session_t *sp) 932 { 933 DBT key; 934 935 key.data = &sp->se_process; 936 key.size = sizeof sp->se_process; 937 938 if ((*session_db->del)(session_db, &key, 0)) 939 emergency("delete %d: %s", sp->se_process, strerror(errno)); 940 } 941 942 /* 943 * Look up a login session by pid. 944 */ 945 static session_t * 946 find_session(pid_t pid) 947 { 948 DBT key; 949 DBT data; 950 session_t *ret; 951 952 key.data = &pid; 953 key.size = sizeof pid; 954 if ((*session_db->get)(session_db, &key, &data, 0) != 0) 955 return 0; 956 bcopy(data.data, (char *)&ret, sizeof(ret)); 957 return ret; 958 } 959 960 /* 961 * Construct an argument vector from a command line. 962 */ 963 static char ** 964 construct_argv(char *command) 965 { 966 int argc = 0; 967 char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1) 968 * sizeof (char *)); 969 970 if ((argv[argc++] = strk(command)) == 0) { 971 free(argv); 972 return (NULL); 973 } 974 while ((argv[argc++] = strk((char *) 0)) != NULL) 975 continue; 976 return argv; 977 } 978 979 /* 980 * Deallocate a session descriptor. 981 */ 982 static void 983 free_session(session_t *sp) 984 { 985 free(sp->se_device); 986 if (sp->se_getty) { 987 free(sp->se_getty); 988 free(sp->se_getty_argv_space); 989 free(sp->se_getty_argv); 990 } 991 if (sp->se_window) { 992 free(sp->se_window); 993 free(sp->se_window_argv_space); 994 free(sp->se_window_argv); 995 } 996 if (sp->se_type) 997 free(sp->se_type); 998 free(sp); 999 } 1000 1001 /* 1002 * Allocate a new session descriptor. 1003 * Mark it SE_PRESENT. 1004 */ 1005 static session_t * 1006 new_session(session_t *sprev, int session_index, struct ttyent *typ) 1007 { 1008 session_t *sp; 1009 int fd; 1010 1011 if ((typ->ty_status & TTY_ON) == 0 || 1012 typ->ty_name == 0 || 1013 typ->ty_getty == 0) 1014 return 0; 1015 1016 sp = (session_t *) calloc(1, sizeof (session_t)); 1017 1018 sp->se_index = session_index; 1019 sp->se_flags |= SE_PRESENT; 1020 1021 sp->se_device = malloc(sizeof(_PATH_DEV) + strlen(typ->ty_name)); 1022 sprintf(sp->se_device, "%s%s", _PATH_DEV, typ->ty_name); 1023 1024 /* 1025 * Attempt to open the device, if we get "device not configured" 1026 * then don't add the device to the session list. 1027 */ 1028 if ((fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0)) < 0) { 1029 if (errno == ENXIO) { 1030 free_session(sp); 1031 return (0); 1032 } 1033 } else 1034 close(fd); 1035 1036 if (setupargv(sp, typ) == 0) { 1037 free_session(sp); 1038 return (0); 1039 } 1040 1041 sp->se_next = 0; 1042 if (sprev == 0) { 1043 sessions = sp; 1044 sp->se_prev = 0; 1045 } else { 1046 sprev->se_next = sp; 1047 sp->se_prev = sprev; 1048 } 1049 1050 return sp; 1051 } 1052 1053 /* 1054 * Calculate getty and if useful window argv vectors. 1055 */ 1056 static int 1057 setupargv(session_t *sp, struct ttyent *typ) 1058 { 1059 1060 if (sp->se_getty) { 1061 free(sp->se_getty); 1062 free(sp->se_getty_argv_space); 1063 free(sp->se_getty_argv); 1064 } 1065 sp->se_getty = malloc(strlen(typ->ty_getty) + strlen(typ->ty_name) + 2); 1066 sprintf(sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name); 1067 sp->se_getty_argv_space = strdup(sp->se_getty); 1068 sp->se_getty_argv = construct_argv(sp->se_getty_argv_space); 1069 if (sp->se_getty_argv == 0) { 1070 warning("can't parse getty for port %s", sp->se_device); 1071 free(sp->se_getty); 1072 free(sp->se_getty_argv_space); 1073 sp->se_getty = sp->se_getty_argv_space = 0; 1074 return (0); 1075 } 1076 if (sp->se_window) { 1077 free(sp->se_window); 1078 free(sp->se_window_argv_space); 1079 free(sp->se_window_argv); 1080 } 1081 sp->se_window = sp->se_window_argv_space = 0; 1082 sp->se_window_argv = 0; 1083 if (typ->ty_window) { 1084 sp->se_window = strdup(typ->ty_window); 1085 sp->se_window_argv_space = strdup(sp->se_window); 1086 sp->se_window_argv = construct_argv(sp->se_window_argv_space); 1087 if (sp->se_window_argv == 0) { 1088 warning("can't parse window for port %s", 1089 sp->se_device); 1090 free(sp->se_window_argv_space); 1091 free(sp->se_window); 1092 sp->se_window = sp->se_window_argv_space = 0; 1093 return (0); 1094 } 1095 } 1096 if (sp->se_type) 1097 free(sp->se_type); 1098 sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0; 1099 return (1); 1100 } 1101 1102 /* 1103 * Walk the list of ttys and create sessions for each active line. 1104 */ 1105 static state_func_t 1106 read_ttys(void) 1107 { 1108 int session_index = 0; 1109 session_t *sp, *snext; 1110 struct ttyent *typ; 1111 1112 /* 1113 * Destroy any previous session state. 1114 * There shouldn't be any, but just in case... 1115 */ 1116 for (sp = sessions; sp; sp = snext) { 1117 if (sp->se_process) 1118 clear_session_logs(sp); 1119 snext = sp->se_next; 1120 free_session(sp); 1121 } 1122 sessions = 0; 1123 if (start_session_db()) 1124 return (state_func_t) single_user; 1125 1126 /* 1127 * Allocate a session entry for each active port. 1128 * Note that sp starts at 0. 1129 */ 1130 while ((typ = getttyent()) != NULL) 1131 if ((snext = new_session(sp, ++session_index, typ)) != NULL) 1132 sp = snext; 1133 1134 endttyent(); 1135 1136 return (state_func_t) multi_user; 1137 } 1138 1139 /* 1140 * Start a window system running. 1141 */ 1142 static void 1143 start_window_system(session_t *sp) 1144 { 1145 pid_t pid; 1146 sigset_t mask; 1147 char term[64], *env[2]; 1148 int status; 1149 1150 if ((pid = fork()) == -1) { 1151 emergency("can't fork for window system on port %s: %m", 1152 sp->se_device); 1153 /* hope that getty fails and we can try again */ 1154 return; 1155 } 1156 if (pid) { 1157 waitpid(-1, &status, 0); 1158 return; 1159 } 1160 1161 /* reparent window process to the init to not make a zombie on exit */ 1162 if ((pid = fork()) == -1) { 1163 emergency("can't fork for window system on port %s: %m", 1164 sp->se_device); 1165 _exit(1); 1166 } 1167 if (pid) 1168 _exit(0); 1169 1170 sigemptyset(&mask); 1171 sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); 1172 1173 if (setsid() < 0) 1174 emergency("setsid failed (window) %m"); 1175 1176 #ifdef LOGIN_CAP 1177 setprocresources(RESOURCE_WINDOW); 1178 #endif 1179 if (sp->se_type) { 1180 /* Don't use malloc after fork */ 1181 strcpy(term, "TERM="); 1182 strncat(term, sp->se_type, sizeof(term) - 6); 1183 env[0] = term; 1184 env[1] = 0; 1185 } 1186 else 1187 env[0] = 0; 1188 execve(sp->se_window_argv[0], sp->se_window_argv, env); 1189 stall("can't exec window system '%s' for port %s: %m", 1190 sp->se_window_argv[0], sp->se_device); 1191 _exit(1); 1192 } 1193 1194 /* 1195 * Start a login session running. 1196 */ 1197 static pid_t 1198 start_getty(session_t *sp) 1199 { 1200 pid_t pid; 1201 sigset_t mask; 1202 time_t current_time = time((time_t *) 0); 1203 int too_quick = 0; 1204 char term[64], *env[2]; 1205 1206 if (current_time >= sp->se_started && 1207 current_time - sp->se_started < GETTY_SPACING) { 1208 if (++sp->se_nspace > GETTY_NSPACE) { 1209 sp->se_nspace = 0; 1210 too_quick = 1; 1211 } 1212 } else 1213 sp->se_nspace = 0; 1214 1215 /* 1216 * fork(), not vfork() -- we can't afford to block. 1217 */ 1218 if ((pid = fork()) == -1) { 1219 emergency("can't fork for getty on port %s: %m", sp->se_device); 1220 return -1; 1221 } 1222 1223 if (pid) 1224 return pid; 1225 1226 if (too_quick) { 1227 warning("getty repeating too quickly on port %s, sleeping %d secs", 1228 sp->se_device, GETTY_SLEEP); 1229 sleep((unsigned) GETTY_SLEEP); 1230 } 1231 1232 if (sp->se_window) { 1233 start_window_system(sp); 1234 sleep(WINDOW_WAIT); 1235 } 1236 1237 sigemptyset(&mask); 1238 sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); 1239 1240 #ifdef LOGIN_CAP 1241 setprocresources(RESOURCE_GETTY); 1242 #endif 1243 if (sp->se_type) { 1244 /* Don't use malloc after fork */ 1245 strcpy(term, "TERM="); 1246 strncat(term, sp->se_type, sizeof(term) - 6); 1247 env[0] = term; 1248 env[1] = 0; 1249 } else 1250 env[0] = 0; 1251 execve(sp->se_getty_argv[0], sp->se_getty_argv, env); 1252 stall("can't exec getty '%s' for port %s: %m", 1253 sp->se_getty_argv[0], sp->se_device); 1254 _exit(1); 1255 } 1256 1257 /* 1258 * Collect exit status for a child. 1259 * If an exiting login, start a new login running. 1260 */ 1261 static void 1262 collect_child(pid_t pid) 1263 { 1264 session_t *sp, *sprev, *snext; 1265 1266 if (! sessions) 1267 return; 1268 1269 if (! (sp = find_session(pid))) 1270 return; 1271 1272 clear_session_logs(sp); 1273 del_session(sp); 1274 sp->se_process = 0; 1275 1276 if (sp->se_flags & SE_SHUTDOWN) { 1277 if ((sprev = sp->se_prev) != NULL) 1278 sprev->se_next = sp->se_next; 1279 else 1280 sessions = sp->se_next; 1281 if ((snext = sp->se_next) != NULL) 1282 snext->se_prev = sp->se_prev; 1283 free_session(sp); 1284 return; 1285 } 1286 1287 if ((pid = start_getty(sp)) == -1) { 1288 /* serious trouble */ 1289 requested_transition = clean_ttys; 1290 return; 1291 } 1292 1293 sp->se_process = pid; 1294 sp->se_started = time((time_t *) 0); 1295 add_session(sp); 1296 } 1297 1298 /* 1299 * Catch a signal and request a state transition. 1300 */ 1301 static void 1302 transition_handler(int sig) 1303 { 1304 1305 switch (sig) { 1306 case SIGHUP: 1307 requested_transition = clean_ttys; 1308 break; 1309 case SIGUSR2: 1310 howto = RB_POWEROFF; 1311 case SIGUSR1: 1312 howto |= RB_HALT; 1313 case SIGINT: 1314 Reboot = TRUE; 1315 case SIGTERM: 1316 requested_transition = death; 1317 break; 1318 case SIGTSTP: 1319 requested_transition = catatonia; 1320 break; 1321 default: 1322 requested_transition = 0; 1323 break; 1324 } 1325 } 1326 1327 /* 1328 * Take the system multiuser. 1329 */ 1330 static state_func_t 1331 multi_user(void) 1332 { 1333 pid_t pid; 1334 session_t *sp; 1335 1336 requested_transition = 0; 1337 1338 /* 1339 * If the administrator has not set the security level to -1 1340 * to indicate that the kernel should not run multiuser in secure 1341 * mode, and the run script has not set a higher level of security 1342 * than level 1, then put the kernel into secure mode. 1343 */ 1344 if (getsecuritylevel() == 0) 1345 setsecuritylevel(1); 1346 1347 for (sp = sessions; sp; sp = sp->se_next) { 1348 if (sp->se_process) 1349 continue; 1350 if ((pid = start_getty(sp)) == -1) { 1351 /* serious trouble */ 1352 requested_transition = clean_ttys; 1353 break; 1354 } 1355 sp->se_process = pid; 1356 sp->se_started = time((time_t *) 0); 1357 add_session(sp); 1358 } 1359 1360 while (!requested_transition) 1361 if ((pid = waitpid(-1, (int *) 0, 0)) != -1) 1362 collect_child(pid); 1363 1364 return (state_func_t) requested_transition; 1365 } 1366 1367 /* 1368 * This is an (n*2)+(n^2) algorithm. We hope it isn't run often... 1369 */ 1370 static state_func_t 1371 clean_ttys(void) 1372 { 1373 session_t *sp, *sprev; 1374 struct ttyent *typ; 1375 int session_index = 0; 1376 int devlen; 1377 char *old_getty, *old_window, *old_type; 1378 1379 /* 1380 * mark all sessions for death, (!SE_PRESENT) 1381 * as we find or create new ones they'll be marked as keepers, 1382 * we'll later nuke all the ones not found in /etc/ttys 1383 */ 1384 for (sp = sessions; sp != NULL; sp = sp->se_next) 1385 sp->se_flags &= ~SE_PRESENT; 1386 1387 devlen = sizeof(_PATH_DEV) - 1; 1388 while ((typ = getttyent()) != NULL) { 1389 ++session_index; 1390 1391 for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next) 1392 if (strcmp(typ->ty_name, sp->se_device + devlen) == 0) 1393 break; 1394 1395 if (sp) { 1396 /* we want this one to live */ 1397 sp->se_flags |= SE_PRESENT; 1398 if (sp->se_index != session_index) { 1399 warning("port %s changed utmp index from %d to %d", 1400 sp->se_device, sp->se_index, 1401 session_index); 1402 sp->se_index = session_index; 1403 } 1404 if ((typ->ty_status & TTY_ON) == 0 || 1405 typ->ty_getty == 0) { 1406 sp->se_flags |= SE_SHUTDOWN; 1407 kill(sp->se_process, SIGHUP); 1408 continue; 1409 } 1410 sp->se_flags &= ~SE_SHUTDOWN; 1411 old_getty = sp->se_getty ? strdup(sp->se_getty) : 0; 1412 old_window = sp->se_window ? strdup(sp->se_window) : 0; 1413 old_type = sp->se_type ? strdup(sp->se_type) : 0; 1414 if (setupargv(sp, typ) == 0) { 1415 warning("can't parse getty for port %s", 1416 sp->se_device); 1417 sp->se_flags |= SE_SHUTDOWN; 1418 kill(sp->se_process, SIGHUP); 1419 } 1420 else if ( !old_getty 1421 || (!old_type && sp->se_type) 1422 || (old_type && !sp->se_type) 1423 || (!old_window && sp->se_window) 1424 || (old_window && !sp->se_window) 1425 || (strcmp(old_getty, sp->se_getty) != 0) 1426 || (old_window && strcmp(old_window, sp->se_window) != 0) 1427 || (old_type && strcmp(old_type, sp->se_type) != 0) 1428 ) { 1429 /* Don't set SE_SHUTDOWN here */ 1430 sp->se_nspace = 0; 1431 sp->se_started = 0; 1432 kill(sp->se_process, SIGHUP); 1433 } 1434 if (old_getty) 1435 free(old_getty); 1436 if (old_window) 1437 free(old_window); 1438 if (old_type) 1439 free(old_type); 1440 continue; 1441 } 1442 1443 new_session(sprev, session_index, typ); 1444 } 1445 1446 endttyent(); 1447 1448 /* 1449 * sweep through and kill all deleted sessions 1450 * ones who's /etc/ttys line was deleted (SE_PRESENT unset) 1451 */ 1452 for (sp = sessions; sp != NULL; sp = sp->se_next) { 1453 if ((sp->se_flags & SE_PRESENT) == 0) { 1454 sp->se_flags |= SE_SHUTDOWN; 1455 kill(sp->se_process, SIGHUP); 1456 } 1457 } 1458 1459 return (state_func_t) multi_user; 1460 } 1461 1462 /* 1463 * Block further logins. 1464 */ 1465 static state_func_t 1466 catatonia(void) 1467 { 1468 session_t *sp; 1469 1470 for (sp = sessions; sp; sp = sp->se_next) 1471 sp->se_flags |= SE_SHUTDOWN; 1472 1473 return (state_func_t) multi_user; 1474 } 1475 1476 /* 1477 * Note SIGALRM. 1478 */ 1479 static void 1480 alrm_handler(int sig) 1481 { 1482 1483 (void)sig; 1484 clang = 1; 1485 } 1486 1487 /* 1488 * Bring the system down to single user. 1489 */ 1490 static state_func_t 1491 death(void) 1492 { 1493 struct utmpx utx; 1494 session_t *sp; 1495 int i; 1496 pid_t pid; 1497 static const int death_sigs[2] = { SIGTERM, SIGKILL }; 1498 1499 /* NB: should send a message to the session logger to avoid blocking. */ 1500 utx.ut_type = SHUTDOWN_TIME; 1501 gettimeofday(&utx.ut_tv, NULL); 1502 pututxline(&utx); 1503 1504 /* 1505 * Also revoke the TTY here. Because runshutdown() may reopen 1506 * the TTY whose getty we're killing here, there is no guarantee 1507 * runshutdown() will perform the initial open() call, causing 1508 * the terminal attributes to be misconfigured. 1509 */ 1510 for (sp = sessions; sp; sp = sp->se_next) { 1511 sp->se_flags |= SE_SHUTDOWN; 1512 kill(sp->se_process, SIGHUP); 1513 revoke(sp->se_device); 1514 } 1515 1516 /* Try to run the rc.shutdown script within a period of time */ 1517 runshutdown(); 1518 1519 for (i = 0; i < 2; ++i) { 1520 if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH) 1521 return (state_func_t) single_user; 1522 1523 clang = 0; 1524 alarm(DEATH_WATCH); 1525 do 1526 if ((pid = waitpid(-1, (int *)0, 0)) != -1) 1527 collect_child(pid); 1528 while (clang == 0 && errno != ECHILD); 1529 1530 if (errno == ECHILD) 1531 return (state_func_t) single_user; 1532 } 1533 1534 warning("some processes would not die; ps axl advised"); 1535 1536 return (state_func_t) single_user; 1537 } 1538 1539 /* 1540 * Run the system shutdown script. 1541 * 1542 * Exit codes: XXX I should document more 1543 * -2 shutdown script terminated abnormally 1544 * -1 fatal error - can't run script 1545 * 0 good. 1546 * >0 some error (exit code) 1547 */ 1548 static int 1549 runshutdown(void) 1550 { 1551 pid_t pid, wpid; 1552 int status; 1553 int shutdowntimeout; 1554 size_t len; 1555 char *argv[4]; 1556 const char *shell; 1557 struct sigaction sa; 1558 struct stat sb; 1559 1560 /* 1561 * rc.shutdown is optional, so to prevent any unnecessary 1562 * complaints from the shell we simply don't run it if the 1563 * file does not exist. If the stat() here fails for other 1564 * reasons, we'll let the shell complain. 1565 */ 1566 if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT) 1567 return 0; 1568 1569 shell = get_shell(); 1570 1571 if ((pid = fork()) == 0) { 1572 sigemptyset(&sa.sa_mask); 1573 sa.sa_flags = 0; 1574 sa.sa_handler = SIG_IGN; 1575 sigaction(SIGTSTP, &sa, (struct sigaction *)0); 1576 sigaction(SIGHUP, &sa, (struct sigaction *)0); 1577 1578 setctty(_PATH_CONSOLE); 1579 1580 char _sh[] = "sh"; 1581 char _reboot[] = "reboot"; 1582 char _single[] = "single"; 1583 char _path_rundown[] = _PATH_RUNDOWN; 1584 1585 argv[0] = _sh; 1586 argv[1] = _path_rundown; 1587 argv[2] = Reboot ? _reboot : _single; 1588 argv[3] = 0; 1589 1590 sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0); 1591 1592 #ifdef LOGIN_CAP 1593 setprocresources(RESOURCE_RC); 1594 #endif 1595 execv(shell, argv); 1596 warning("can't exec %s for %s: %m", shell, _PATH_RUNDOWN); 1597 _exit(1); /* force single user mode */ 1598 } 1599 1600 if (pid == -1) { 1601 emergency("can't fork for %s on %s: %m", shell, _PATH_RUNDOWN); 1602 while (waitpid(-1, (int *) 0, WNOHANG) > 0) 1603 continue; 1604 sleep(STALL_TIMEOUT); 1605 return -1; 1606 } 1607 1608 len = sizeof(shutdowntimeout); 1609 if (sysctlbyname("kern.init_shutdown_timeout", &shutdowntimeout, &len, 1610 NULL, 0) == -1 || shutdowntimeout < 2) 1611 shutdowntimeout = DEATH_SCRIPT; 1612 alarm(shutdowntimeout); 1613 clang = 0; 1614 /* 1615 * Copied from single_user(). This is a bit paranoid. 1616 * Use the same ALRM handler. 1617 */ 1618 do { 1619 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 1620 collect_child(wpid); 1621 if (clang == 1) { 1622 /* we were waiting for the sub-shell */ 1623 kill(wpid, SIGTERM); 1624 warning("timeout expired for %s on %s: %m; going to " 1625 "single user mode", shell, _PATH_RUNDOWN); 1626 return -1; 1627 } 1628 if (wpid == -1) { 1629 if (errno == EINTR) 1630 continue; 1631 warning("wait for %s on %s failed: %m; going to " 1632 "single user mode", shell, _PATH_RUNDOWN); 1633 return -1; 1634 } 1635 if (wpid == pid && WIFSTOPPED(status)) { 1636 warning("init: %s on %s stopped, restarting\n", 1637 shell, _PATH_RUNDOWN); 1638 kill(pid, SIGCONT); 1639 wpid = -1; 1640 } 1641 } while (wpid != pid && !clang); 1642 1643 /* Turn off the alarm */ 1644 alarm(0); 1645 1646 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM && 1647 requested_transition == catatonia) { 1648 /* 1649 * /etc/rc.shutdown executed /sbin/reboot; 1650 * wait for the end quietly 1651 */ 1652 sigset_t s; 1653 1654 sigfillset(&s); 1655 for (;;) 1656 sigsuspend(&s); 1657 } 1658 1659 if (!WIFEXITED(status)) { 1660 warning("%s on %s terminated abnormally, going to " 1661 "single user mode", shell, _PATH_RUNDOWN); 1662 return -2; 1663 } 1664 1665 if ((status = WEXITSTATUS(status)) != 0) 1666 warning("%s returned status %d", _PATH_RUNDOWN, status); 1667 1668 return status; 1669 } 1670 1671 static char * 1672 strk(char *p) 1673 { 1674 static char *t; 1675 char *q; 1676 int c; 1677 1678 if (p) 1679 t = p; 1680 if (!t) 1681 return 0; 1682 1683 c = *t; 1684 while (c == ' ' || c == '\t' ) 1685 c = *++t; 1686 if (!c) { 1687 t = 0; 1688 return 0; 1689 } 1690 q = t; 1691 if (c == '\'') { 1692 c = *++t; 1693 q = t; 1694 while (c && c != '\'') 1695 c = *++t; 1696 if (!c) /* unterminated string */ 1697 q = t = 0; 1698 else 1699 *t++ = 0; 1700 } else { 1701 while (c && c != ' ' && c != '\t' ) 1702 c = *++t; 1703 *t++ = 0; 1704 if (!c) 1705 t = 0; 1706 } 1707 return q; 1708 } 1709 1710 #ifdef LOGIN_CAP 1711 static void 1712 setprocresources(const char *cname) 1713 { 1714 login_cap_t *lc; 1715 if ((lc = login_getclassbyname(cname, NULL)) != NULL) { 1716 setusercontext(lc, (struct passwd*)NULL, 0, 1717 LOGIN_SETPRIORITY | LOGIN_SETRESOURCES); 1718 login_close(lc); 1719 } 1720 } 1721 #endif 1722