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