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