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 --- 27 unchanged lines hidden (view full) --- 36 The Regents of the University of California. All rights reserved.\n"; 37#endif /* not lint */ 38 39#ifndef lint 40#if 0 41static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 7/15/93"; 42#endif 43static const char rcsid[] = |
44 "$FreeBSD: head/sbin/init/init.c 166484 2007-02-04 06:33:13Z imp $"; |
45#endif /* not lint */ 46 47#include <sys/param.h> 48#include <sys/ioctl.h> 49#include <sys/mount.h> 50#include <sys/sysctl.h> 51#include <sys/wait.h> 52#include <sys/stat.h> 53#include <sys/uio.h> 54 55#include <db.h> 56#include <errno.h> 57#include <fcntl.h> |
58#include <kenv.h> |
59#include <libutil.h> 60#include <paths.h> 61#include <signal.h> 62#include <stdio.h> 63#include <stdlib.h> 64#include <string.h> 65#include <syslog.h> 66#include <time.h> --- 50 unchanged lines hidden (view full) --- 117state_func_t single_user(void); 118state_func_t runcom(void); 119state_func_t read_ttys(void); 120state_func_t multi_user(void); 121state_func_t clean_ttys(void); 122state_func_t catatonia(void); 123state_func_t death(void); 124 |
125state_func_t run_script(const char *); 126 |
127enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT; 128#define FALSE 0 129#define TRUE 1 130 131int Reboot = FALSE; 132int howto = RB_AUTOBOOT; 133 134int devfs; 135 136void transition(state_t); |
137state_t requested_transition; |
138 139void setctty(const char *); |
140const char *get_shell(void); 141void write_stderr(const char *message); |
142 143typedef struct init_session { 144 int se_index; /* index of entry in ttys file */ 145 pid_t se_process; /* controlling process */ 146 time_t se_started; /* used to avoid thrashing */ 147 int se_flags; /* status of session */ 148#define SE_SHUTDOWN 0x1 /* session won't be restarted */ 149#define SE_PRESENT 0x2 /* session is in /etc/ttys */ --- 37 unchanged lines hidden (view full) --- 187DB *session_db; 188 189/* 190 * The mother of all processes. 191 */ 192int 193main(int argc, char *argv[]) 194{ |
195 state_t initial_transition = runcom; 196 char kenv_value[PATH_MAX]; |
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)); --- 59 unchanged lines hidden (view full) --- 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 initial_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 /* 286 * We catch or block signals rather than ignore them, 287 * so that they get reset on exec. 288 */ 289 handle(badsys, SIGSYS, 0); 290 handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV, 291 SIGBUS, SIGXCPU, SIGXFSZ, 0); 292 handle(transition_handler, SIGHUP, SIGINT, SIGTERM, SIGTSTP, 293 SIGUSR1, SIGUSR2, 0); 294 handle(alrm_handler, SIGALRM, 0); 295 sigfillset(&mask); 296 delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS, 297 SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGTERM, SIGTSTP, SIGALRM, 298 SIGUSR1, SIGUSR2, 0); 299 sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); 300 sigemptyset(&sa.sa_mask); 301 sa.sa_flags = 0; 302 sa.sa_handler = SIG_IGN; 303 (void) sigaction(SIGTTIN, &sa, (struct sigaction *)0); 304 (void) sigaction(SIGTTOU, &sa, (struct sigaction *)0); 305 306 /* 307 * Paranoia. 308 */ 309 close(0); 310 close(1); 311 close(2); 312 313 if (kenv(KENV_GET, "init_script", kenv_value, sizeof(kenv_value)) > 0) { 314 state_func_t next_transition; 315 316 if ((next_transition = run_script(kenv_value)) != 0) 317 initial_transition = (state_t) next_transition; 318 } 319 320 if (kenv(KENV_GET, "init_chroot", kenv_value, sizeof(kenv_value)) > 0) { 321 if (chdir(kenv_value) != 0 || chroot(".") != 0) 322 warning("Can't chroot to %s: %m", kenv_value); 323 } 324 325 /* 326 * Additional check if devfs needs to be mounted: 327 * If "/" and "/dev" have the same device number, 328 * then it hasn't been mounted yet. 329 */ 330 if (!devfs) { 331 struct stat stst; 332 dev_t root_devno; 333 334 stat("/", &stst); 335 root_devno = stst.st_dev; 336 if (stat("/dev", &stst) != 0) 337 warning("Can't stat /dev: %m"); 338 else if (stst.st_dev == root_devno) 339 devfs++; 340 } 341 |
342 if (devfs) { 343 struct iovec iov[4]; 344 char *s; 345 int i; 346 347 char _fstype[] = "fstype"; 348 char _devfs[] = "devfs"; 349 char _fspath[] = "fspath"; --- 21 unchanged lines hidden (view full) --- 371 iov[3].iov_len = sizeof(_path_dev); 372 } 373 nmount(iov, 4, 0); 374 if (s != NULL) 375 free(s); 376 } 377 378 /* |
379 * Start the state machine. 380 */ |
381 transition(initial_transition); |
382 383 /* 384 * Should never reach here. 385 */ 386 return 1; 387} 388 389/* --- 199 unchanged lines hidden (view full) --- 589 _exit(1); 590 } 591 if (login_tty(fd) == -1) { 592 stall("can't get %s for controlling terminal: %m", name); 593 _exit(1); 594 } 595} 596 |
597const char * 598get_shell(void) 599{ 600 static char kenv_value[PATH_MAX]; 601 602 if (kenv(KENV_GET, "init_shell", kenv_value, sizeof(kenv_value)) > 0) 603 return kenv_value; 604 else 605 return _PATH_BSHELL; 606} 607 608void 609write_stderr(const char *message) 610{ 611 write(STDERR_FILENO, message, strlen(message)); 612} 613 |
614/* 615 * Bring the system up single user. 616 */ 617state_func_t 618single_user(void) 619{ 620 pid_t pid, wpid; 621 int status; 622 sigset_t mask; |
623 const char *shell; |
624 char *argv[2]; 625#ifdef SECURE 626 struct ttyent *typ; 627 struct passwd *pp; 628 static const char banner[] = 629 "Enter root password, or ^D to go multi-user\n"; 630 char *clear, *password; 631#endif --- 5 unchanged lines hidden (view full) --- 637 /* Instead of going single user, let's reboot the machine */ 638 sync(); 639 alarm(2); 640 pause(); 641 reboot(howto); 642 _exit(0); 643 } 644 |
645 shell = get_shell(); 646 |
647 if ((pid = fork()) == 0) { 648 /* 649 * Start the single user session. 650 */ 651 setctty(_PATH_CONSOLE); 652 653#ifdef SECURE 654 /* 655 * Check the root password. 656 * We don't care if the console is 'on' by default; 657 * it's the only tty that can be 'off' and 'secure'. 658 */ 659 typ = getttynam("console"); 660 pp = getpwnam("root"); 661 if (typ && (typ->ty_status & TTY_SECURE) == 0 && 662 pp && *pp->pw_passwd) { |
663 write_stderr(banner); |
664 for (;;) { 665 clear = getpass("Password:"); 666 if (clear == 0 || *clear == '\0') 667 _exit(0); 668 password = crypt(clear, pp->pw_passwd); 669 bzero(clear, _PASSWORD_LEN); 670 if (strcmp(password, pp->pw_passwd) == 0) 671 break; --- 4 unchanged lines hidden (view full) --- 676 endpwent(); 677#endif /* SECURE */ 678 679#ifdef DEBUGSHELL 680 { 681 char *cp = altshell; 682 int num; 683 |
684#define SHREQUEST "Enter full pathname of shell or RETURN for " 685 write_stderr(SHREQUEST); 686 write_stderr(shell); 687 write_stderr(": "); |
688 while ((num = read(STDIN_FILENO, cp, 1)) != -1 && 689 num != 0 && *cp != '\n' && cp < &altshell[127]) 690 cp++; 691 *cp = '\0'; 692 if (altshell[0] != '\0') 693 shell = altshell; 694 } 695#endif /* DEBUGSHELL */ --- 72 unchanged lines hidden (view full) --- 768} 769 770/* 771 * Run the system startup script. 772 */ 773state_func_t 774runcom(void) 775{ |
776 state_func_t next_transition; 777 778 if ((next_transition = run_script(_PATH_RUNCOM)) != 0) 779 return next_transition; 780 781 runcom_mode = AUTOBOOT; /* the default */ 782 /* NB: should send a message to the session logger to avoid blocking. */ 783 logwtmp("~", "reboot", ""); 784 return (state_func_t) read_ttys; 785} 786 787/* 788 * Run a shell script. 789 * Returns 0 on success, otherwise the next transition to enter: 790 * - single_user if fork/execv/waitpid failed, or if the script 791 * terminated with a signal or exit code != 0. 792 * - death if a SIGTERM was delivered to init(8). 793 */ 794state_func_t 795run_script(const char *script) 796{ |
797 pid_t pid, wpid; 798 int status; 799 char *argv[4]; |
800 const char *shell; |
801 struct sigaction sa; 802 |
803 shell = get_shell(); 804 |
805 if ((pid = fork()) == 0) { 806 sigemptyset(&sa.sa_mask); 807 sa.sa_flags = 0; 808 sa.sa_handler = SIG_IGN; 809 (void) sigaction(SIGTSTP, &sa, (struct sigaction *)0); 810 (void) sigaction(SIGHUP, &sa, (struct sigaction *)0); 811 812 setctty(_PATH_CONSOLE); 813 814 char _sh[] = "sh"; |
815 char _autoboot[] = "autoboot"; 816 817 argv[0] = _sh; |
818 argv[1] = __DECONST(char *, script); |
819 argv[2] = runcom_mode == AUTOBOOT ? _autoboot : 0; 820 argv[3] = 0; 821 822 sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0); 823 824#ifdef LOGIN_CAP 825 setprocresources(RESOURCE_RC); 826#endif |
827 execv(shell, argv); 828 stall("can't exec %s for %s: %m", shell, script); |
829 _exit(1); /* force single user mode */ 830 } 831 832 if (pid == -1) { |
833 emergency("can't fork for %s on %s: %m", shell, script); |
834 while (waitpid(-1, (int *) 0, WNOHANG) > 0) 835 continue; 836 sleep(STALL_TIMEOUT); 837 return (state_func_t) single_user; 838 } 839 840 /* 841 * Copied from single_user(). This is a bit paranoid. 842 */ 843 requested_transition = 0; 844 do { 845 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 846 collect_child(wpid); 847 if (wpid == -1) { 848 if (requested_transition == death) 849 return (state_func_t) death; 850 if (errno == EINTR) 851 continue; |
852 warning("wait for %s on %s failed: %m; going to " 853 "single user mode", shell, script); |
854 return (state_func_t) single_user; 855 } 856 if (wpid == pid && WIFSTOPPED(status)) { 857 warning("init: %s on %s stopped, restarting\n", |
858 shell, script); |
859 kill(pid, SIGCONT); 860 wpid = -1; 861 } 862 } while (wpid != pid); 863 864 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM && 865 requested_transition == catatonia) { 866 /* /etc/rc executed /sbin/reboot; wait for the end quietly */ 867 sigset_t s; 868 869 sigfillset(&s); 870 for (;;) 871 sigsuspend(&s); 872 } 873 874 if (!WIFEXITED(status)) { |
875 warning("%s on %s terminated abnormally, going to single " 876 "user mode", shell, script); |
877 return (state_func_t) single_user; 878 } 879 880 if (WEXITSTATUS(status)) 881 return (state_func_t) single_user; 882 |
883 return (state_func_t) 0; |
884} 885 886/* 887 * Open the session database. 888 * 889 * NB: We could pass in the size here; is it necessary? 890 */ 891int --- 642 unchanged lines hidden (view full) --- 1534int 1535runshutdown(void) 1536{ 1537 pid_t pid, wpid; 1538 int status; 1539 int shutdowntimeout; 1540 size_t len; 1541 char *argv[4]; |
1542 const char *shell; |
1543 struct sigaction sa; 1544 struct stat sb; 1545 1546 /* 1547 * rc.shutdown is optional, so to prevent any unnecessary 1548 * complaints from the shell we simply don't run it if the 1549 * file does not exist. If the stat() here fails for other 1550 * reasons, we'll let the shell complain. 1551 */ 1552 if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT) 1553 return 0; 1554 |
1555 shell = get_shell(); 1556 |
1557 if ((pid = fork()) == 0) { 1558 int fd; 1559 1560 /* Assume that init already grab console as ctty before */ 1561 1562 sigemptyset(&sa.sa_mask); 1563 sa.sa_flags = 0; 1564 sa.sa_handler = SIG_IGN; --- 24 unchanged lines hidden (view full) --- 1589 argv[2] = Reboot ? _reboot : _single; 1590 argv[3] = 0; 1591 1592 sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0); 1593 1594#ifdef LOGIN_CAP 1595 setprocresources(RESOURCE_RC); 1596#endif |
1597 execv(shell, argv); 1598 warning("can't exec %s for %s: %m", shell, _PATH_RUNDOWN); |
1599 _exit(1); /* force single user mode */ 1600 } 1601 1602 if (pid == -1) { |
1603 emergency("can't fork for %s on %s: %m", shell, _PATH_RUNDOWN); |
1604 while (waitpid(-1, (int *) 0, WNOHANG) > 0) 1605 continue; 1606 sleep(STALL_TIMEOUT); 1607 return -1; 1608 } 1609 1610 len = sizeof(shutdowntimeout); 1611 if (sysctlbyname("kern.init_shutdown_timeout", --- 7 unchanged lines hidden (view full) --- 1619 * Use the same ALRM handler. 1620 */ 1621 do { 1622 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 1623 collect_child(wpid); 1624 if (clang == 1) { 1625 /* we were waiting for the sub-shell */ 1626 kill(wpid, SIGTERM); |
1627 warning("timeout expired for %s on %s: %m; going to " 1628 "single user mode", shell, _PATH_RUNDOWN); |
1629 return -1; 1630 } 1631 if (wpid == -1) { 1632 if (errno == EINTR) 1633 continue; |
1634 warning("wait for %s on %s failed: %m; going to " 1635 "single user mode", shell, _PATH_RUNDOWN); |
1636 return -1; 1637 } 1638 if (wpid == pid && WIFSTOPPED(status)) { 1639 warning("init: %s on %s stopped, restarting\n", |
1640 shell, _PATH_RUNDOWN); |
1641 kill(pid, SIGCONT); 1642 wpid = -1; 1643 } 1644 } while (wpid != pid && !clang); 1645 1646 /* Turn off the alarm */ 1647 alarm(0); 1648 --- 6 unchanged lines hidden (view full) --- 1655 sigset_t s; 1656 1657 sigfillset(&s); 1658 for (;;) 1659 sigsuspend(&s); 1660 } 1661 1662 if (!WIFEXITED(status)) { |
1663 warning("%s on %s terminated abnormally, going to " 1664 "single user mode", shell, _PATH_RUNDOWN); |
1665 return -2; 1666 } 1667 1668 if ((status = WEXITSTATUS(status)) != 0) 1669 warning("%s returned status %d", _PATH_RUNDOWN, status); 1670 1671 return status; 1672} --- 51 unchanged lines hidden --- |