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