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