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