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