diff options
| author | Eric Andersen <andersen@codepoet.org> | 1999-10-25 23:32:44 +0000 |
|---|---|---|
| committer | Eric Andersen <andersen@codepoet.org> | 1999-10-25 23:32:44 +0000 |
| commit | 0460ff2e5d443b485c2f6447c8c2ece1b3bec9c3 (patch) | |
| tree | d761a790ee864a0a566d29c9d56173763fa04888 | |
| parent | d143e8f2bd03470e891a5e3ccca5734edd917e73 (diff) | |
| download | busybox-w32-0460ff2e5d443b485c2f6447c8c2ece1b3bec9c3.tar.gz busybox-w32-0460ff2e5d443b485c2f6447c8c2ece1b3bec9c3.tar.bz2 busybox-w32-0460ff2e5d443b485c2f6447c8c2ece1b3bec9c3.zip | |
Stuf
| -rw-r--r-- | Changelog | 3 | ||||
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | applets/busybox.c | 9 | ||||
| -rw-r--r-- | busybox.c | 9 | ||||
| -rw-r--r-- | busybox.def.h | 3 | ||||
| -rw-r--r-- | chvt.c | 36 | ||||
| -rw-r--r-- | console-tools/chvt.c | 36 | ||||
| -rw-r--r-- | console-tools/deallocvt.c | 47 | ||||
| -rw-r--r-- | deallocvt.c | 47 | ||||
| -rw-r--r-- | init.c | 628 | ||||
| -rw-r--r-- | init/init.c | 628 | ||||
| -rw-r--r-- | internal.h | 5 | ||||
| -rw-r--r-- | utility.c | 92 |
13 files changed, 929 insertions, 616 deletions
| @@ -4,6 +4,9 @@ | |||
| 4 | * Fixed mkdir -m option so that it works. | 4 | * Fixed mkdir -m option so that it works. |
| 5 | * kill segfaulted w/o any arguments. Now it doesn't do that. | 5 | * kill segfaulted w/o any arguments. Now it doesn't do that. |
| 6 | * kill wasn't properly accepting signal names. It does now. | 6 | * kill wasn't properly accepting signal names. It does now. |
| 7 | * Added new apps chvt and deallocvt | ||
| 8 | * Major adjustment of init.c. Code is now readable IMHO, | ||
| 9 | and much more solid. | ||
| 7 | 10 | ||
| 8 | -Erik Andersen | 11 | -Erik Andersen |
| 9 | 12 | ||
| @@ -22,7 +22,7 @@ BUILDTIME=$(shell date "+%Y%m%d-%H%M") | |||
| 22 | 22 | ||
| 23 | # Comment out the following to make a debuggable build | 23 | # Comment out the following to make a debuggable build |
| 24 | # Leave this off for production use. | 24 | # Leave this off for production use. |
| 25 | DODEBUG=false | 25 | DODEBUG=true |
| 26 | # If you want a static binary, turn this on. I can't think | 26 | # If you want a static binary, turn this on. I can't think |
| 27 | # of many situations where anybody would ever want it static, | 27 | # of many situations where anybody would ever want it static, |
| 28 | # but... | 28 | # but... |
diff --git a/applets/busybox.c b/applets/busybox.c index d4313ef06..05144c472 100644 --- a/applets/busybox.c +++ b/applets/busybox.c | |||
| @@ -51,6 +51,12 @@ static const struct Applet applets[] = { | |||
| 51 | #ifdef BB_FIND //usr/bin | 51 | #ifdef BB_FIND //usr/bin |
| 52 | {"find", find_main}, | 52 | {"find", find_main}, |
| 53 | #endif | 53 | #endif |
| 54 | #ifdef BB_CHVT //usr/bin | ||
| 55 | {"chvt", chvt_main}, | ||
| 56 | #endif | ||
| 57 | #ifdef BB_DEALLOCVT //usr/bin | ||
| 58 | {"deallocvt", deallocvt_main}, | ||
| 59 | #endif | ||
| 54 | #ifdef BB_FSCK_MINIX //sbin | 60 | #ifdef BB_FSCK_MINIX //sbin |
| 55 | {"fsck.minix", fsck_minix_main}, | 61 | {"fsck.minix", fsck_minix_main}, |
| 56 | #endif | 62 | #endif |
| @@ -156,6 +162,9 @@ static const struct Applet applets[] = { | |||
| 156 | {"true", true_main}, | 162 | {"true", true_main}, |
| 157 | {"false", false_main}, | 163 | {"false", false_main}, |
| 158 | #endif | 164 | #endif |
| 165 | #ifdef BB_UNAME //bin | ||
| 166 | {"uname", uname_main}, | ||
| 167 | #endif | ||
| 159 | #ifdef BB_UMOUNT //bin | 168 | #ifdef BB_UMOUNT //bin |
| 160 | {"umount", umount_main}, | 169 | {"umount", umount_main}, |
| 161 | #endif | 170 | #endif |
| @@ -51,6 +51,12 @@ static const struct Applet applets[] = { | |||
| 51 | #ifdef BB_FIND //usr/bin | 51 | #ifdef BB_FIND //usr/bin |
| 52 | {"find", find_main}, | 52 | {"find", find_main}, |
| 53 | #endif | 53 | #endif |
| 54 | #ifdef BB_CHVT //usr/bin | ||
| 55 | {"chvt", chvt_main}, | ||
| 56 | #endif | ||
| 57 | #ifdef BB_DEALLOCVT //usr/bin | ||
| 58 | {"deallocvt", deallocvt_main}, | ||
| 59 | #endif | ||
| 54 | #ifdef BB_FSCK_MINIX //sbin | 60 | #ifdef BB_FSCK_MINIX //sbin |
| 55 | {"fsck.minix", fsck_minix_main}, | 61 | {"fsck.minix", fsck_minix_main}, |
| 56 | #endif | 62 | #endif |
| @@ -156,6 +162,9 @@ static const struct Applet applets[] = { | |||
| 156 | {"true", true_main}, | 162 | {"true", true_main}, |
| 157 | {"false", false_main}, | 163 | {"false", false_main}, |
| 158 | #endif | 164 | #endif |
| 165 | #ifdef BB_UNAME //bin | ||
| 166 | {"uname", uname_main}, | ||
| 167 | #endif | ||
| 159 | #ifdef BB_UMOUNT //bin | 168 | #ifdef BB_UMOUNT //bin |
| 160 | {"umount", umount_main}, | 169 | {"umount", umount_main}, |
| 161 | #endif | 170 | #endif |
diff --git a/busybox.def.h b/busybox.def.h index 845e46382..a79eaaf4f 100644 --- a/busybox.def.h +++ b/busybox.def.h | |||
| @@ -18,6 +18,8 @@ | |||
| 18 | #define BB_FIND | 18 | #define BB_FIND |
| 19 | //#define BB_FSCK_MINIX | 19 | //#define BB_FSCK_MINIX |
| 20 | //#define BB_MKFS_MINIX | 20 | //#define BB_MKFS_MINIX |
| 21 | #define BB_CHVT | ||
| 22 | #define BB_DEALLOCVT | ||
| 21 | #define BB_GREP | 23 | #define BB_GREP |
| 22 | //#define BB_HALT | 24 | //#define BB_HALT |
| 23 | #define BB_INIT | 25 | #define BB_INIT |
| @@ -53,6 +55,7 @@ | |||
| 53 | //#define BB_TRUE_FALSE // Supplied by ash | 55 | //#define BB_TRUE_FALSE // Supplied by ash |
| 54 | #define BB_UMOUNT | 56 | #define BB_UMOUNT |
| 55 | #define BB_UPDATE | 57 | #define BB_UPDATE |
| 58 | #define BB_UNAME | ||
| 56 | #define BB_ZCAT | 59 | #define BB_ZCAT |
| 57 | //#define BB_GZIP | 60 | //#define BB_GZIP |
| 58 | // Don't turn BB_UTILITY off. It contains support code | 61 | // Don't turn BB_UTILITY off. It contains support code |
| @@ -0,0 +1,36 @@ | |||
| 1 | /* | ||
| 2 | * chvt.c - aeb - 940227 - Change virtual terminal | ||
| 3 | * | ||
| 4 | * busyboxed by Erik Andersen | ||
| 5 | */ | ||
| 6 | #include "internal.h" | ||
| 7 | #include <sys/types.h> | ||
| 8 | #include <sys/ioctl.h> | ||
| 9 | #include <linux/vt.h> | ||
| 10 | #include <stdio.h> | ||
| 11 | #include <stdlib.h> | ||
| 12 | #include <fcntl.h> | ||
| 13 | |||
| 14 | extern int getfd(void); | ||
| 15 | |||
| 16 | int | ||
| 17 | chvt_main(int argc, char** argv) | ||
| 18 | { | ||
| 19 | int fd, num; | ||
| 20 | |||
| 21 | if ( ( argc != 2) || (**(argv+1) == '-' ) ) { | ||
| 22 | usage ("chvt </dev/ttyN>\n"); | ||
| 23 | } | ||
| 24 | fd = get_console_fd("/dev/console"); | ||
| 25 | num = atoi(argv[1]); | ||
| 26 | if (ioctl(fd,VT_ACTIVATE,num)) { | ||
| 27 | perror("VT_ACTIVATE"); | ||
| 28 | exit(FALSE); | ||
| 29 | } | ||
| 30 | if (ioctl(fd,VT_WAITACTIVE,num)) { | ||
| 31 | perror("VT_WAITACTIVE"); | ||
| 32 | exit(FALSE); | ||
| 33 | } | ||
| 34 | exit( TRUE); | ||
| 35 | } | ||
| 36 | |||
diff --git a/console-tools/chvt.c b/console-tools/chvt.c new file mode 100644 index 000000000..81d199527 --- /dev/null +++ b/console-tools/chvt.c | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | /* | ||
| 2 | * chvt.c - aeb - 940227 - Change virtual terminal | ||
| 3 | * | ||
| 4 | * busyboxed by Erik Andersen | ||
| 5 | */ | ||
| 6 | #include "internal.h" | ||
| 7 | #include <sys/types.h> | ||
| 8 | #include <sys/ioctl.h> | ||
| 9 | #include <linux/vt.h> | ||
| 10 | #include <stdio.h> | ||
| 11 | #include <stdlib.h> | ||
| 12 | #include <fcntl.h> | ||
| 13 | |||
| 14 | extern int getfd(void); | ||
| 15 | |||
| 16 | int | ||
| 17 | chvt_main(int argc, char** argv) | ||
| 18 | { | ||
| 19 | int fd, num; | ||
| 20 | |||
| 21 | if ( ( argc != 2) || (**(argv+1) == '-' ) ) { | ||
| 22 | usage ("chvt </dev/ttyN>\n"); | ||
| 23 | } | ||
| 24 | fd = get_console_fd("/dev/console"); | ||
| 25 | num = atoi(argv[1]); | ||
| 26 | if (ioctl(fd,VT_ACTIVATE,num)) { | ||
| 27 | perror("VT_ACTIVATE"); | ||
| 28 | exit(FALSE); | ||
| 29 | } | ||
| 30 | if (ioctl(fd,VT_WAITACTIVE,num)) { | ||
| 31 | perror("VT_WAITACTIVE"); | ||
| 32 | exit(FALSE); | ||
| 33 | } | ||
| 34 | exit( TRUE); | ||
| 35 | } | ||
| 36 | |||
diff --git a/console-tools/deallocvt.c b/console-tools/deallocvt.c new file mode 100644 index 000000000..a8feeb53c --- /dev/null +++ b/console-tools/deallocvt.c | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | /* | ||
| 2 | * disalloc.c - aeb - 940501 - Disallocate virtual terminal(s) | ||
| 3 | * Renamed deallocvt. | ||
| 4 | */ | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <fcntl.h> | ||
| 7 | #include <sys/types.h> | ||
| 8 | #include <sys/ioctl.h> | ||
| 9 | #include <linux/vt.h> | ||
| 10 | #include <stdio.h> | ||
| 11 | |||
| 12 | extern int getfd(void); | ||
| 13 | char *progname; | ||
| 14 | |||
| 15 | int | ||
| 16 | deallocvt_main(int argc, char *argv[]) { | ||
| 17 | int fd, num, i; | ||
| 18 | |||
| 19 | if (argc < 1) /* unlikely */ | ||
| 20 | exit(1); | ||
| 21 | progname = argv[0]; | ||
| 22 | |||
| 23 | fd = get_console_fd("/dev/console"); | ||
| 24 | |||
| 25 | if (argc == 1) { | ||
| 26 | /* deallocate all unused consoles */ | ||
| 27 | if (ioctl(fd,VT_DISALLOCATE,0)) { | ||
| 28 | perror("VT_DISALLOCATE"); | ||
| 29 | exit(1); | ||
| 30 | } | ||
| 31 | } else | ||
| 32 | for (i = 1; i < argc; i++) { | ||
| 33 | num = atoi(argv[i]); | ||
| 34 | if (num == 0) | ||
| 35 | fprintf(stderr, "%s: 0: illegal VT number\n", progname); | ||
| 36 | else if (num == 1) | ||
| 37 | fprintf(stderr, "%s: VT 1 cannot be deallocated\n", progname); | ||
| 38 | else | ||
| 39 | if (ioctl(fd,VT_DISALLOCATE,num)) { | ||
| 40 | perror("VT_DISALLOCATE"); | ||
| 41 | fprintf(stderr, "%s: could not deallocate console %d\n", | ||
| 42 | progname, num); | ||
| 43 | exit(1); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | exit(0); | ||
| 47 | } | ||
diff --git a/deallocvt.c b/deallocvt.c new file mode 100644 index 000000000..a8feeb53c --- /dev/null +++ b/deallocvt.c | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | /* | ||
| 2 | * disalloc.c - aeb - 940501 - Disallocate virtual terminal(s) | ||
| 3 | * Renamed deallocvt. | ||
| 4 | */ | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <fcntl.h> | ||
| 7 | #include <sys/types.h> | ||
| 8 | #include <sys/ioctl.h> | ||
| 9 | #include <linux/vt.h> | ||
| 10 | #include <stdio.h> | ||
| 11 | |||
| 12 | extern int getfd(void); | ||
| 13 | char *progname; | ||
| 14 | |||
| 15 | int | ||
| 16 | deallocvt_main(int argc, char *argv[]) { | ||
| 17 | int fd, num, i; | ||
| 18 | |||
| 19 | if (argc < 1) /* unlikely */ | ||
| 20 | exit(1); | ||
| 21 | progname = argv[0]; | ||
| 22 | |||
| 23 | fd = get_console_fd("/dev/console"); | ||
| 24 | |||
| 25 | if (argc == 1) { | ||
| 26 | /* deallocate all unused consoles */ | ||
| 27 | if (ioctl(fd,VT_DISALLOCATE,0)) { | ||
| 28 | perror("VT_DISALLOCATE"); | ||
| 29 | exit(1); | ||
| 30 | } | ||
| 31 | } else | ||
| 32 | for (i = 1; i < argc; i++) { | ||
| 33 | num = atoi(argv[i]); | ||
| 34 | if (num == 0) | ||
| 35 | fprintf(stderr, "%s: 0: illegal VT number\n", progname); | ||
| 36 | else if (num == 1) | ||
| 37 | fprintf(stderr, "%s: VT 1 cannot be deallocated\n", progname); | ||
| 38 | else | ||
| 39 | if (ioctl(fd,VT_DISALLOCATE,num)) { | ||
| 40 | perror("VT_DISALLOCATE"); | ||
| 41 | fprintf(stderr, "%s: could not deallocate console %d\n", | ||
| 42 | progname, num); | ||
| 43 | exit(1); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | exit(0); | ||
| 47 | } | ||
| @@ -41,144 +41,88 @@ | |||
| 41 | #include <sys/vt.h> /* for vt_stat */ | 41 | #include <sys/vt.h> /* for vt_stat */ |
| 42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
| 43 | 43 | ||
| 44 | static const char init_usage[] = "Used internally by the system."; | 44 | #define DEBUG_INIT |
| 45 | static char console[16] = ""; | ||
| 46 | static const char* default_console = "/dev/tty2"; | ||
| 47 | static char* first_terminal = NULL; | ||
| 48 | static const char* second_terminal = "/dev/tty2"; | ||
| 49 | static const char* log = "/dev/tty3"; | ||
| 50 | static char* term_ptr = NULL; | ||
| 51 | 45 | ||
| 52 | static void | 46 | #define CONSOLE "/dev/console" /* Logical system console */ |
| 53 | message(const char* terminal, const char * pattern, ...) | 47 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
| 54 | { | 48 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
| 55 | int fd; | 49 | #define VT_LOG "/dev/tty2" /* Virtual console master */ |
| 56 | FILE * con = 0; | 50 | #define SHELL "/bin/sh" /* Default shell */ |
| 57 | va_list arguments; | 51 | #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ |
| 58 | 52 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" | |
| 59 | /* | ||
| 60 | * Open the console device each time a message is printed. If the user | ||
| 61 | * has switched consoles, the open will get the new console. If we kept | ||
| 62 | * the console open, we'd always print to the same one. | ||
| 63 | */ | ||
| 64 | if ( !terminal | ||
| 65 | || ((fd = open(terminal, O_WRONLY|O_NOCTTY)) < 0) | ||
| 66 | || ((con = fdopen(fd, "w")) == NULL) ) | ||
| 67 | return; | ||
| 68 | 53 | ||
| 69 | va_start(arguments, pattern); | 54 | static int maxproclen=0; |
| 70 | vfprintf(con, pattern, arguments); | 55 | static char* argv0; |
| 71 | va_end(arguments); | 56 | |
| 72 | fclose(con); | 57 | static char* console = CONSOLE; |
| 58 | static char* second_terminal = "/dev/tty2"; | ||
| 59 | static char* log = "/dev/tty3"; | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | /* try to open up the specified device */ | ||
| 64 | int device_open(char* device, int mode) | ||
| 65 | { | ||
| 66 | int m, f, fd = -1; | ||
| 67 | |||
| 68 | mode = m | O_NONBLOCK; | ||
| 69 | |||
| 70 | /* Retry up to 5 times */ | ||
| 71 | for(f = 0; f < 5; f++) | ||
| 72 | if ((fd = open(device, m)) >= 0) break; | ||
| 73 | if (fd < 0) return fd; | ||
| 74 | /* Set original flags. */ | ||
| 75 | if (m != mode) | ||
| 76 | fcntl(fd, F_SETFL, mode); | ||
| 77 | return fd; | ||
| 73 | } | 78 | } |
| 74 | 79 | ||
| 75 | static int | 80 | /* print a message to the specified device */ |
| 76 | waitfor(int pid) | 81 | void message(char* device, char *fmt, ...) |
| 77 | { | 82 | { |
| 78 | int status; | 83 | int fd; |
| 79 | int wpid; | 84 | va_list arguments; |
| 80 | 85 | if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) { | |
| 81 | message(log, "Waiting for process %d.\n", pid); | 86 | va_start(arguments, fmt); |
| 82 | while ( (wpid = wait(&status)) != pid ) { | 87 | vdprintf(fd, fmt, arguments); |
| 83 | if ( wpid > 0 ) { | 88 | va_end(arguments); |
| 84 | message( | 89 | } |
| 85 | log | 90 | close( fd); |
| 86 | ,"pid %d exited, status=%x.\n" | ||
| 87 | ,wpid | ||
| 88 | ,status); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | return wpid; | ||
| 92 | } | 91 | } |
| 93 | 92 | ||
| 94 | static int | 93 | /* Set terminal settings to reasonable defaults */ |
| 95 | run(const char* program, const char* const* arguments, | 94 | void set_term() |
| 96 | const char* terminal, int get_enter) | ||
| 97 | { | 95 | { |
| 98 | static const char control_characters[] = { | 96 | int fd; |
| 99 | '\003', | 97 | struct termios tty; |
| 100 | '\034', | 98 | |
| 101 | '\177', | 99 | if ((fd = device_open(console, O_RDWR|O_NOCTTY)) < 0) { |
| 102 | '\025', | 100 | message(log, "can't open %s", console); |
| 103 | '\004', | 101 | return; |
| 104 | '\0', | 102 | } |
| 105 | '\1', | 103 | ioctl(fd, TCGETS, &tty); |
| 106 | '\0', | 104 | tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD; |
| 107 | '\021', | 105 | tty.c_cflag |= HUPCL|CLOCAL; |
| 108 | '\023', | 106 | |
| 109 | '\032', | 107 | tty.c_cc[VINTR] = 3; |
| 110 | '\0', | 108 | tty.c_cc[VQUIT] = 28; |
| 111 | '\022', | 109 | tty.c_cc[VERASE] = 127; |
| 112 | '\017', | 110 | tty.c_cc[VKILL] = 24; |
| 113 | '\027', | 111 | tty.c_cc[VEOF] = 4; |
| 114 | '\026', | 112 | tty.c_cc[VTIME] = 0; |
| 115 | '\0' | 113 | tty.c_cc[VMIN] = 1; |
| 116 | }; | 114 | tty.c_cc[VSTART] = 17; |
| 117 | 115 | tty.c_cc[VSTOP] = 19; | |
| 118 | static char * environment[] = { | 116 | tty.c_cc[VSUSP] = 26; |
| 119 | "HOME=/", | 117 | |
| 120 | "PATH=/bin:/sbin:/usr/bin:/usr/sbin", | 118 | /* Set pre and post processing */ |
| 121 | "SHELL=/bin/sh", | 119 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY; |
| 122 | 0, | 120 | tty.c_oflag = OPOST|ONLCR; |
| 123 | "USER=root", | 121 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE; |
| 124 | 0 | 122 | |
| 125 | }; | 123 | /* Now set the terminal line. */ |
| 126 | 124 | ioctl(fd, TCSETS, &tty); | |
| 127 | static const char press_enter[] = | 125 | close( fd); |
| 128 | "\nPlease press Enter to activate this console. "; | ||
| 129 | |||
| 130 | int pid; | ||
| 131 | |||
| 132 | environment[3]=term_ptr; | ||
| 133 | |||
| 134 | pid = fork(); | ||
| 135 | if ( pid == 0 ) { | ||
| 136 | struct termios t; | ||
| 137 | const char * const * arg; | ||
| 138 | |||
| 139 | close(0); | ||
| 140 | close(1); | ||
| 141 | close(2); | ||
| 142 | setsid(); | ||
| 143 | |||
| 144 | open(terminal, O_RDWR); | ||
| 145 | dup(0); | ||
| 146 | dup(0); | ||
| 147 | tcsetpgrp(0, getpgrp()); | ||
| 148 | |||
| 149 | tcgetattr(0, &t); | ||
| 150 | memcpy(t.c_cc, control_characters, sizeof(control_characters)); | ||
| 151 | t.c_line = 0; | ||
| 152 | t.c_iflag = ICRNL|IXON|IXOFF; | ||
| 153 | t.c_oflag = OPOST|ONLCR; | ||
| 154 | t.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN; | ||
| 155 | tcsetattr(0, TCSANOW, &t); | ||
| 156 | |||
| 157 | if ( get_enter ) { | ||
| 158 | /* | ||
| 159 | * Save memory by not exec-ing anything large (like a shell) | ||
| 160 | * before the user wants it. This is critical if swap is not | ||
| 161 | * enabled and the system has low memory. Generally this will | ||
| 162 | * be run on the second virtual console, and the first will | ||
| 163 | * be allowed to start a shell or whatever an init script | ||
| 164 | * specifies. | ||
| 165 | */ | ||
| 166 | char c; | ||
| 167 | write(1, press_enter, sizeof(press_enter) - 1); | ||
| 168 | read(0, &c, 1); | ||
| 169 | } | ||
| 170 | |||
| 171 | message(log, "Executing "); | ||
| 172 | arg = arguments; | ||
| 173 | while ( *arg != 0 ) | ||
| 174 | message(log, "%s ", *arg++); | ||
| 175 | message(log, "\n"); | ||
| 176 | |||
| 177 | execve(program, (char * *)arguments, (char * *)environment); | ||
| 178 | message(log, "%s: could not execute: %s.\r\n", program, strerror(errno)); | ||
| 179 | exit(-1); | ||
| 180 | } | ||
| 181 | return pid; | ||
| 182 | } | 126 | } |
| 183 | 127 | ||
| 184 | static int | 128 | static int |
| @@ -217,33 +161,168 @@ set_free_pages() | |||
| 217 | fclose(f); | 161 | fclose(f); |
| 218 | } | 162 | } |
| 219 | 163 | ||
| 164 | |||
| 220 | static void | 165 | static void |
| 221 | shutdown_system(void) | 166 | console_init() |
| 222 | { | 167 | { |
| 223 | static const char * const umount_args[] = {"/bin/umount", "-a", "-n", 0}; | 168 | int fd; |
| 224 | static const char * const swapoff_args[] = {"/bin/swapoff", "-a", 0}; | 169 | int tried_devcons = 0; |
| 170 | int tried_vtmaster = 0; | ||
| 171 | char *s; | ||
| 172 | |||
| 173 | if ((s = getenv("CONSOLE")) != NULL) | ||
| 174 | console = s; | ||
| 175 | else { | ||
| 176 | console = CONSOLE; | ||
| 177 | tried_devcons++; | ||
| 178 | } | ||
| 179 | while ((fd = open(console, O_RDONLY|O_NONBLOCK)) < 0) { | ||
| 180 | if (!tried_devcons) { | ||
| 181 | tried_devcons++; | ||
| 182 | console = CONSOLE; | ||
| 183 | continue; | ||
| 184 | } | ||
| 185 | if (!tried_vtmaster) { | ||
| 186 | tried_vtmaster++; | ||
| 187 | console = VT_PRIMARY; | ||
| 188 | continue; | ||
| 189 | } | ||
| 190 | break; | ||
| 191 | } | ||
| 192 | if (fd < 0) | ||
| 193 | console = "/dev/null"; | ||
| 194 | else | ||
| 195 | close(fd); | ||
| 196 | } | ||
| 225 | 197 | ||
| 226 | message(console, "The system is going down NOW !!"); | 198 | static int |
| 227 | sync(); | 199 | waitfor(int pid) |
| 228 | /* Allow Ctrl-Alt-Del to reboot system. */ | 200 | { |
| 229 | reboot(RB_ENABLE_CAD); | 201 | int status, wpid; |
| 230 | 202 | ||
| 231 | /* Send signals to every process _except_ pid 1 */ | 203 | message(log, "Waiting for process %d.\n", pid); |
| 232 | message(console, "Sending SIGHUP to all processes.\r\n"); | 204 | while ( (wpid = wait(&status)) != pid ) { |
| 233 | kill(-1, SIGHUP); | 205 | if ( wpid > 0 ) |
| 234 | sleep(2); | 206 | message(log,"pid %d exited, status=%x.\n", wpid, status); |
| 235 | sync(); | 207 | } |
| 236 | message(console, "Sending SIGKILL to all processes.\r\n"); | 208 | return wpid; |
| 237 | kill(-1, SIGKILL); | 209 | } |
| 238 | sleep(1); | 210 | |
| 239 | waitfor(run("/bin/swapoff", swapoff_args, console, 0)); | 211 | static int |
| 240 | waitfor(run("/bin/umount", umount_args, console, 0)); | 212 | run(const char* command, char* terminal, int get_enter) |
| 241 | sync(); | 213 | { |
| 242 | if (get_kernel_revision() <= 2*65536+2*256+11) { | 214 | int f, pid; |
| 243 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ | 215 | char *args[16]; |
| 244 | bdflush(1, 0); | 216 | char buf[256]; |
| 245 | sync(); | 217 | char* ptr; |
| 218 | static const char press_enter[] = | ||
| 219 | "\nPlease press Enter to activate this console. "; | ||
| 220 | |||
| 221 | |||
| 222 | /* Make a proper command from the command string */ | ||
| 223 | strcpy(buf, command); | ||
| 224 | ptr = buf; | ||
| 225 | for(f = 1; f < 15; f++) { | ||
| 226 | /* Skip white space */ | ||
| 227 | while(*ptr == ' ' || *ptr == '\t') ptr++; | ||
| 228 | args[f] = ptr; | ||
| 229 | /* May be trailing space.. */ | ||
| 230 | if (*ptr == 0) break; | ||
| 231 | /* Skip this `word' */ | ||
| 232 | while(*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#') | ||
| 233 | ptr++; | ||
| 234 | /* If end-of-line, break */ | ||
| 235 | if (*ptr == '#' || *ptr == 0) { | ||
| 236 | f++; | ||
| 237 | *ptr = 0; | ||
| 238 | break; | ||
| 239 | } | ||
| 240 | /* End word with \0 and continue */ | ||
| 241 | args[f] = NULL; | ||
| 242 | } | ||
| 243 | args[0] = args[1]; | ||
| 244 | |||
| 245 | |||
| 246 | if ((pid = fork()) == 0) { | ||
| 247 | /* Clean up */ | ||
| 248 | close(0); | ||
| 249 | close(1); | ||
| 250 | close(2); | ||
| 251 | setsid(); | ||
| 252 | |||
| 253 | if ((f = device_open(terminal, O_RDWR|O_NOCTTY)) < 0) { | ||
| 254 | message( log, "open(%s) failed: %s", terminal, strerror(errno)); | ||
| 255 | return -1; | ||
| 256 | } | ||
| 257 | dup(f); | ||
| 258 | dup(f); | ||
| 259 | tcsetpgrp(0, getpgrp()); | ||
| 260 | set_term(); | ||
| 261 | |||
| 262 | if ( get_enter ) { | ||
| 263 | /* | ||
| 264 | * Save memory by not exec-ing anything large (like a shell) | ||
| 265 | * before the user wants it. This is critical if swap is not | ||
| 266 | * enabled and the system has low memory. Generally this will | ||
| 267 | * be run on the second virtual console, and the first will | ||
| 268 | * be allowed to start a shell or whatever an init script | ||
| 269 | * specifies. | ||
| 270 | */ | ||
| 271 | char c; | ||
| 272 | write(1, press_enter, sizeof(press_enter) - 1); | ||
| 273 | read(0, &c, 1); | ||
| 246 | } | 274 | } |
| 275 | |||
| 276 | /* Log the process name and args */ | ||
| 277 | message(log, "Executing '%s'\n", command); | ||
| 278 | |||
| 279 | /* Now run it. This should take over the PID, so nothing | ||
| 280 | * further in init.c should be run by this PID. */ | ||
| 281 | execvp(args[1], args + 1); | ||
| 282 | |||
| 283 | /* If shell scripts are not executed, force the issue */ | ||
| 284 | if (errno == ENOEXEC) { | ||
| 285 | char buf[256]; | ||
| 286 | args[1] = SHELL; | ||
| 287 | args[2] = "-c"; | ||
| 288 | strcpy(buf, "exec "); | ||
| 289 | strcat(buf, command); | ||
| 290 | args[3] = buf; | ||
| 291 | args[4] = NULL; | ||
| 292 | execvp(args[1], args + 1); | ||
| 293 | } | ||
| 294 | message(log, "Could not execute '%s'\n", command, strerror(errno)); | ||
| 295 | exit(-1); | ||
| 296 | } | ||
| 297 | return pid; | ||
| 298 | } | ||
| 299 | |||
| 300 | #ifndef DEBUG_INIT | ||
| 301 | static void | ||
| 302 | shutdown_system(void) | ||
| 303 | { | ||
| 304 | |||
| 305 | message(console, "The system is going down NOW !!"); | ||
| 306 | sync(); | ||
| 307 | /* Allow Ctrl-Alt-Del to reboot system. */ | ||
| 308 | reboot(RB_ENABLE_CAD); | ||
| 309 | |||
| 310 | /* Send signals to every process _except_ pid 1 */ | ||
| 311 | message(console, "Sending SIGHUP to all processes.\r\n"); | ||
| 312 | kill(-1, SIGHUP); | ||
| 313 | sleep(2); | ||
| 314 | sync(); | ||
| 315 | message(console, "Sending SIGKILL to all processes.\r\n"); | ||
| 316 | kill(-1, SIGKILL); | ||
| 317 | sleep(1); | ||
| 318 | waitfor(run("/bin/swapoff -a", console, 0)); | ||
| 319 | waitfor(run("/bin/umount -a -n", console, 0)); | ||
| 320 | sync(); | ||
| 321 | if (get_kernel_revision() <= 2*65536+2*256+11) { | ||
| 322 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ | ||
| 323 | bdflush(1, 0); | ||
| 324 | sync(); | ||
| 325 | } | ||
| 247 | } | 326 | } |
| 248 | 327 | ||
| 249 | static void | 328 | static void |
| @@ -251,7 +330,7 @@ halt_signal(int sig) | |||
| 251 | { | 330 | { |
| 252 | shutdown_system(); | 331 | shutdown_system(); |
| 253 | message(console, "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); | 332 | message(console, "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); |
| 254 | reboot( RB_HALT_SYSTEM); | 333 | reboot( RB_POWER_OFF); |
| 255 | exit(0); | 334 | exit(0); |
| 256 | } | 335 | } |
| 257 | 336 | ||
| @@ -263,202 +342,136 @@ reboot_signal(int sig) | |||
| 263 | reboot( RB_AUTOBOOT); | 342 | reboot( RB_AUTOBOOT); |
| 264 | exit(0); | 343 | exit(0); |
| 265 | } | 344 | } |
| 345 | #endif | ||
| 266 | 346 | ||
| 267 | static void | 347 | int setproctitle(char *fmt, ...) |
| 268 | configure_terminals( int serial_cons, int single_user_mode ) | ||
| 269 | { | 348 | { |
| 270 | char *tty; | 349 | va_list ap; |
| 271 | struct serial_struct sr; | 350 | int len; |
| 272 | struct vt_stat vt; | 351 | char buf[256]; |
| 273 | 352 | ||
| 274 | 353 | buf[0] = 0; | |
| 275 | switch (serial_cons) { | 354 | va_start(ap, fmt); |
| 276 | case -1: | 355 | len = vsprintf(buf, fmt, ap); |
| 277 | /* 2.2 kernels: | 356 | va_end(ap); |
| 278 | * identify the real console backend and try to make use of it */ | 357 | memset(argv0, 0, maxproclen + 1); |
| 279 | if (ioctl(0,TIOCGSERIAL,&sr) == 0) { | 358 | strncpy(argv0, buf, maxproclen); |
| 280 | sprintf( console, "/dev/ttyS%d", sr.line ); | 359 | return len; |
| 281 | serial_cons = sr.line+1; | ||
| 282 | } | ||
| 283 | else if (ioctl(0, VT_GETSTATE, &vt) == 0) { | ||
| 284 | sprintf( console, "/dev/tty%d", vt.v_active ); | ||
| 285 | serial_cons = 0; | ||
| 286 | } | ||
| 287 | else { | ||
| 288 | /* unknown backend: fallback to /dev/console */ | ||
| 289 | strcpy( console, "/dev/console" ); | ||
| 290 | serial_cons = 0; | ||
| 291 | } | ||
| 292 | break; | ||
| 293 | |||
| 294 | case 1: | ||
| 295 | strcpy( console, "/dev/cua0" ); | ||
| 296 | break; | ||
| 297 | case 2: | ||
| 298 | strcpy( console, "/dev/cua1" ); | ||
| 299 | break; | ||
| 300 | default: | ||
| 301 | tty = ttyname(0); | ||
| 302 | if (tty) { | ||
| 303 | strcpy( console, tty ); | ||
| 304 | if (!strncmp( tty, "/dev/cua", 8 )) | ||
| 305 | serial_cons=1; | ||
| 306 | } | ||
| 307 | else | ||
| 308 | /* falls back to /dev/tty1 if an error occurs */ | ||
| 309 | strcpy( console, default_console ); | ||
| 310 | } | ||
| 311 | if (!first_terminal) | ||
| 312 | first_terminal = console; | ||
| 313 | #if defined (__sparc__) | ||
| 314 | if (serial_cons > 0 && !strncmp(term_ptr,"TERM=linux",10)) | ||
| 315 | term_ptr = "TERM=vt100"; | ||
| 316 | #endif | ||
| 317 | if (serial_cons) { | ||
| 318 | /* disable other but the first terminal: | ||
| 319 | * VT is not initialized anymore on 2.2 kernel when booting from | ||
| 320 | * serial console, therefore modprobe is flooding the display with | ||
| 321 | * "can't locate module char-major-4" messages. */ | ||
| 322 | log = 0; | ||
| 323 | second_terminal = 0; | ||
| 324 | } | ||
| 325 | } | 360 | } |
| 326 | 361 | ||
| 327 | extern int | 362 | extern int |
| 328 | init_main(int argc, char * * argv) | 363 | init_main(int argc, char * * argv) |
| 329 | { | 364 | { |
| 330 | const char * rc_arguments[100]; | 365 | int run_rc = TRUE; |
| 331 | const char * arguments[100]; | 366 | int pid1 = 0; |
| 332 | int run_rc = TRUE; | 367 | int pid2 = 0; |
| 333 | int j; | 368 | struct stat statbuf; |
| 334 | int pid1 = 0; | 369 | const char* init_commands = SHELL "-c exec " INITSCRIPT; |
| 335 | int pid2 = 0; | 370 | const char* shell_commands = SHELL; |
| 336 | struct stat statbuf; | 371 | const char* tty0_commands = init_commands; |
| 337 | const char * tty_commands[3] = { "etc/init.d/rcS", "bin/sh"}; | 372 | const char* tty1_commands = shell_commands; |
| 338 | int serial_console = 0; | 373 | const char* no_memory = |
| 339 | int retval; | 374 | "Sorry, your computer does not have enough memory.\n"; |
| 340 | 375 | ||
| 341 | /* | 376 | /* For later use */ |
| 342 | * If I am started as /linuxrc instead of /sbin/init, I don't have the | 377 | argv0 = argv[0]; |
| 343 | * environment that init expects. I can't fix the signal behavior. Try | 378 | maxproclen = strlen(argv[0]); |
| 344 | * to divorce from the controlling terminal with setsid(). This won't work | 379 | setproctitle("init [boot]"); |
| 345 | * if I am the process group leader. | 380 | |
| 346 | */ | 381 | |
| 347 | setsid(); | 382 | #ifndef DEBUG_INIT |
| 348 | 383 | /* Set up sig handlers */ | |
| 349 | signal(SIGUSR1, halt_signal); | 384 | signal(SIGUSR1, halt_signal); |
| 385 | signal(SIGSEGV, halt_signal); | ||
| 386 | signal(SIGPWR, halt_signal); | ||
| 387 | signal(SIGALRM, halt_signal); | ||
| 388 | signal(SIGHUP, halt_signal); | ||
| 350 | signal(SIGUSR2, reboot_signal); | 389 | signal(SIGUSR2, reboot_signal); |
| 351 | signal(SIGINT, reboot_signal); | 390 | signal(SIGINT, reboot_signal); |
| 352 | signal(SIGTERM, reboot_signal); | 391 | signal(SIGTERM, reboot_signal); |
| 392 | #endif | ||
| 393 | /* Figure out where the default console should be */ | ||
| 394 | console_init(); | ||
| 353 | 395 | ||
| 396 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | ||
| 397 | * SIGINT on CAD so we can shut things down gracefully... */ | ||
| 398 | #ifndef DEBUG_INIT | ||
| 354 | reboot(RB_DISABLE_CAD); | 399 | reboot(RB_DISABLE_CAD); |
| 400 | #endif | ||
| 355 | 401 | ||
| 356 | message(log, "%s: started. ", argv[0]); | 402 | /* Close whatever files are open, and reset the console. */ |
| 403 | close(0); | ||
| 404 | close(1); | ||
| 405 | close(2); | ||
| 406 | set_term(); | ||
| 407 | setsid(); | ||
| 357 | 408 | ||
| 358 | for ( j = 1; j < argc; j++ ) { | 409 | /* Make sure PATH is set to something sane */ |
| 359 | if ( strcmp(argv[j], "single") == 0 ) { | 410 | if (getenv("PATH") == NULL) |
| 360 | run_rc = FALSE; | 411 | putenv(PATH_DEFAULT); |
| 361 | tty_commands[0] = "bin/sh"; | ||
| 362 | tty_commands[1] = 0; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | for ( j = 0; __environ[j] != 0; j++ ) { | ||
| 366 | if ( strncmp(__environ[j], "tty", 3) == 0 | ||
| 367 | && __environ[j][3] >= '1' | ||
| 368 | && __environ[j][3] <= '2' | ||
| 369 | && __environ[j][4] == '=' ) { | ||
| 370 | const char * s = &__environ[j][5]; | ||
| 371 | 412 | ||
| 372 | if ( *s == 0 || strcmp(s, "off") == 0 ) | 413 | /* Hello world */ |
| 373 | s = 0; | 414 | message(console, "%s started: BusyBox v%s (%s) multi-call binary", |
| 415 | argv[0], BB_VER, BB_BT); | ||
| 416 | message(log, "%s started: BusyBox v%s (%s) multi-call binary", | ||
| 417 | argv[0], BB_VER, BB_BT); | ||
| 374 | 418 | ||
| 375 | tty_commands[__environ[j][3] - '1'] = s; | ||
| 376 | } | ||
| 377 | /* Should catch the syntax of Sparc kernel console setting. */ | ||
| 378 | /* The kernel does not recognize a serial console when getting*/ | ||
| 379 | /* console=/dev/ttySX !! */ | ||
| 380 | else if ( strcmp(__environ[j], "console=ttya") == 0 ) { | ||
| 381 | serial_console=1; | ||
| 382 | } | ||
| 383 | else if ( strcmp(__environ[j], "console=ttyb") == 0 ) { | ||
| 384 | serial_console=2; | ||
| 385 | } | ||
| 386 | /* standard console settings */ | ||
| 387 | else if ( strncmp(__environ[j], "console=", 8) == 0 ) { | ||
| 388 | first_terminal=&(__environ[j][8]); | ||
| 389 | } | ||
| 390 | else if ( strncmp(__environ[j], "TERM=", 5) == 0) { | ||
| 391 | term_ptr=__environ[j]; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | 419 | ||
| 395 | printf("mounting /proc ...\n"); | 420 | /* Mount /proc */ |
| 421 | message(console, "Mounting /proc: \n"); | ||
| 396 | if (mount("/proc","/proc","proc",0,0)) { | 422 | if (mount("/proc","/proc","proc",0,0)) { |
| 397 | perror("mounting /proc failed\n"); | 423 | message(log, "%s: could not mount /proc!\n", argv[0]); |
| 398 | } | 424 | message(console, "failed!\n"); |
| 399 | printf("\tdone.\n"); | ||
| 400 | |||
| 401 | if (get_kernel_revision() >= 2*65536+1*256+71) { | ||
| 402 | /* if >= 2.1.71 kernel, /dev/console is not a symlink anymore: | ||
| 403 | * use it as primary console */ | ||
| 404 | serial_console=-1; | ||
| 405 | } | 425 | } |
| 426 | message(console, "done.\n"); | ||
| 406 | 427 | ||
| 407 | /* Make sure /etc/init.d/rc exists */ | ||
| 408 | retval= stat(tty_commands[0],&statbuf); | ||
| 409 | if (retval) | ||
| 410 | run_rc = FALSE; | ||
| 411 | |||
| 412 | configure_terminals( serial_console, run_rc); | ||
| 413 | 428 | ||
| 429 | /* Make sure there is enough memory to do something useful*/ | ||
| 414 | set_free_pages(); | 430 | set_free_pages(); |
| 415 | |||
| 416 | /* not enough memory to do anything useful*/ | ||
| 417 | if (mem_total() < 2000) { | 431 | if (mem_total() < 2000) { |
| 432 | int retval; | ||
| 418 | retval= stat("/etc/fstab",&statbuf); | 433 | retval= stat("/etc/fstab",&statbuf); |
| 419 | if (retval) { | 434 | if (retval) { |
| 420 | printf("You do not have enough RAM, sorry.\n"); | 435 | message(console, "%s", no_memory); |
| 421 | while (1) { sleep(1);} | 436 | while (1) { sleep(1);} |
| 422 | } else { | 437 | } else { |
| 423 | /* Try to turn on swap */ | 438 | /* Try to turn on swap */ |
| 424 | static const char * const swapon_args[] = {"/bin/swapon", "-a", 0}; | 439 | waitfor(run("/bin/swapon -a", console, 0)); |
| 425 | waitfor(run("/bin/swapon", swapon_args, console, 0)); | ||
| 426 | if (mem_total() < 2000) { | 440 | if (mem_total() < 2000) { |
| 427 | printf("You do not have enough RAM, sorry.\n"); | 441 | message(console, "%s", no_memory); |
| 428 | while (1) { sleep(1);} | 442 | while (1) { sleep(1);} |
| 429 | } | 443 | } |
| 430 | } | 444 | } |
| 431 | } | 445 | } |
| 432 | 446 | ||
| 433 | /* | 447 | /* Check if we are supposed to be in single user mode */ |
| 434 | * Don't modify **argv directly, it would show up in the "ps" display. | 448 | if (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || !strcmp(argv[1], "1")) { |
| 435 | * I don't want "init" to look like "rc". | 449 | run_rc = FALSE; |
| 436 | */ | 450 | tty0_commands = shell_commands; |
| 437 | rc_arguments[0] = tty_commands[0]; | 451 | tty1_commands = 0; |
| 438 | for ( j = 1; j < argc; j++ ) { | 452 | setproctitle("init [S]"); |
| 439 | rc_arguments[j] = argv[j]; | 453 | } else { |
| 454 | setproctitle("init [1]"); | ||
| 440 | } | 455 | } |
| 441 | rc_arguments[j] = 0; | ||
| 442 | 456 | ||
| 443 | arguments[0] = "-sh"; | 457 | /* Make sure an init script exists before trying to run it */ |
| 444 | arguments[1] = 0; | 458 | if ( run_rc == TRUE && stat( INITSCRIPT, &statbuf)) { |
| 445 | 459 | tty0_commands = shell_commands; | |
| 446 | /* Ok, now launch the rc script /etc/init.d/rcS and prepare to start up | 460 | tty1_commands = shell_commands; |
| 447 | * some VTs on tty1 and tty2 if somebody hits enter | 461 | } |
| 462 | |||
| 463 | /* Ok, now launch the rc script and/or prepare to | ||
| 464 | * start up some VTs if somebody hits enter... | ||
| 448 | */ | 465 | */ |
| 449 | for ( ; ; ) { | 466 | for ( ; ; ) { |
| 450 | int wpid; | 467 | int wpid; |
| 451 | int status; | 468 | int status; |
| 452 | 469 | ||
| 453 | if ( pid1 == 0 && tty_commands[0] ) { | 470 | if ( pid1 == 0 && *tty0_commands ) { |
| 454 | if ( run_rc == TRUE ) { | 471 | pid1 = run(tty0_commands, console, 1); |
| 455 | pid1 = run(tty_commands[0], rc_arguments, first_terminal, 0); | ||
| 456 | } else { | ||
| 457 | pid2 = run(tty_commands[1], arguments, first_terminal, 1); | ||
| 458 | } | ||
| 459 | } | 472 | } |
| 460 | if ( pid2 == 0 && second_terminal && tty_commands[1] ) { | 473 | if ( pid2 == 0 && *tty1_commands ) { |
| 461 | pid2 = run(tty_commands[1], arguments, second_terminal, 1); | 474 | pid2 = run(tty1_commands, second_terminal, 1); |
| 462 | } | 475 | } |
| 463 | wpid = wait(&status); | 476 | wpid = wait(&status); |
| 464 | if ( wpid > 0 && wpid != pid1) { | 477 | if ( wpid > 0 && wpid != pid1) { |
| @@ -467,6 +480,7 @@ init_main(int argc, char * * argv) | |||
| 467 | if ( wpid == pid2 ) { | 480 | if ( wpid == pid2 ) { |
| 468 | pid2 = 0; | 481 | pid2 = 0; |
| 469 | } | 482 | } |
| 483 | sleep(1); | ||
| 470 | } | 484 | } |
| 471 | } | 485 | } |
| 472 | 486 | ||
diff --git a/init/init.c b/init/init.c index c32124dd0..0676bf76d 100644 --- a/init/init.c +++ b/init/init.c | |||
| @@ -41,144 +41,88 @@ | |||
| 41 | #include <sys/vt.h> /* for vt_stat */ | 41 | #include <sys/vt.h> /* for vt_stat */ |
| 42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
| 43 | 43 | ||
| 44 | static const char init_usage[] = "Used internally by the system."; | 44 | #define DEBUG_INIT |
| 45 | static char console[16] = ""; | ||
| 46 | static const char* default_console = "/dev/tty2"; | ||
| 47 | static char* first_terminal = NULL; | ||
| 48 | static const char* second_terminal = "/dev/tty2"; | ||
| 49 | static const char* log = "/dev/tty3"; | ||
| 50 | static char* term_ptr = NULL; | ||
| 51 | 45 | ||
| 52 | static void | 46 | #define CONSOLE "/dev/console" /* Logical system console */ |
| 53 | message(const char* terminal, const char * pattern, ...) | 47 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
| 54 | { | 48 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
| 55 | int fd; | 49 | #define VT_LOG "/dev/tty2" /* Virtual console master */ |
| 56 | FILE * con = 0; | 50 | #define SHELL "/bin/sh" /* Default shell */ |
| 57 | va_list arguments; | 51 | #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ |
| 58 | 52 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" | |
| 59 | /* | ||
| 60 | * Open the console device each time a message is printed. If the user | ||
| 61 | * has switched consoles, the open will get the new console. If we kept | ||
| 62 | * the console open, we'd always print to the same one. | ||
| 63 | */ | ||
| 64 | if ( !terminal | ||
| 65 | || ((fd = open(terminal, O_WRONLY|O_NOCTTY)) < 0) | ||
| 66 | || ((con = fdopen(fd, "w")) == NULL) ) | ||
| 67 | return; | ||
| 68 | 53 | ||
| 69 | va_start(arguments, pattern); | 54 | static int maxproclen=0; |
| 70 | vfprintf(con, pattern, arguments); | 55 | static char* argv0; |
| 71 | va_end(arguments); | 56 | |
| 72 | fclose(con); | 57 | static char* console = CONSOLE; |
| 58 | static char* second_terminal = "/dev/tty2"; | ||
| 59 | static char* log = "/dev/tty3"; | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | /* try to open up the specified device */ | ||
| 64 | int device_open(char* device, int mode) | ||
| 65 | { | ||
| 66 | int m, f, fd = -1; | ||
| 67 | |||
| 68 | mode = m | O_NONBLOCK; | ||
| 69 | |||
| 70 | /* Retry up to 5 times */ | ||
| 71 | for(f = 0; f < 5; f++) | ||
| 72 | if ((fd = open(device, m)) >= 0) break; | ||
| 73 | if (fd < 0) return fd; | ||
| 74 | /* Set original flags. */ | ||
| 75 | if (m != mode) | ||
| 76 | fcntl(fd, F_SETFL, mode); | ||
| 77 | return fd; | ||
| 73 | } | 78 | } |
| 74 | 79 | ||
| 75 | static int | 80 | /* print a message to the specified device */ |
| 76 | waitfor(int pid) | 81 | void message(char* device, char *fmt, ...) |
| 77 | { | 82 | { |
| 78 | int status; | 83 | int fd; |
| 79 | int wpid; | 84 | va_list arguments; |
| 80 | 85 | if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) { | |
| 81 | message(log, "Waiting for process %d.\n", pid); | 86 | va_start(arguments, fmt); |
| 82 | while ( (wpid = wait(&status)) != pid ) { | 87 | vdprintf(fd, fmt, arguments); |
| 83 | if ( wpid > 0 ) { | 88 | va_end(arguments); |
| 84 | message( | 89 | } |
| 85 | log | 90 | close( fd); |
| 86 | ,"pid %d exited, status=%x.\n" | ||
| 87 | ,wpid | ||
| 88 | ,status); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | return wpid; | ||
| 92 | } | 91 | } |
| 93 | 92 | ||
| 94 | static int | 93 | /* Set terminal settings to reasonable defaults */ |
| 95 | run(const char* program, const char* const* arguments, | 94 | void set_term() |
| 96 | const char* terminal, int get_enter) | ||
| 97 | { | 95 | { |
| 98 | static const char control_characters[] = { | 96 | int fd; |
| 99 | '\003', | 97 | struct termios tty; |
| 100 | '\034', | 98 | |
| 101 | '\177', | 99 | if ((fd = device_open(console, O_RDWR|O_NOCTTY)) < 0) { |
| 102 | '\025', | 100 | message(log, "can't open %s", console); |
| 103 | '\004', | 101 | return; |
| 104 | '\0', | 102 | } |
| 105 | '\1', | 103 | ioctl(fd, TCGETS, &tty); |
| 106 | '\0', | 104 | tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD; |
| 107 | '\021', | 105 | tty.c_cflag |= HUPCL|CLOCAL; |
| 108 | '\023', | 106 | |
| 109 | '\032', | 107 | tty.c_cc[VINTR] = 3; |
| 110 | '\0', | 108 | tty.c_cc[VQUIT] = 28; |
| 111 | '\022', | 109 | tty.c_cc[VERASE] = 127; |
| 112 | '\017', | 110 | tty.c_cc[VKILL] = 24; |
| 113 | '\027', | 111 | tty.c_cc[VEOF] = 4; |
| 114 | '\026', | 112 | tty.c_cc[VTIME] = 0; |
| 115 | '\0' | 113 | tty.c_cc[VMIN] = 1; |
| 116 | }; | 114 | tty.c_cc[VSTART] = 17; |
| 117 | 115 | tty.c_cc[VSTOP] = 19; | |
| 118 | static char * environment[] = { | 116 | tty.c_cc[VSUSP] = 26; |
| 119 | "HOME=/", | 117 | |
| 120 | "PATH=/bin:/sbin:/usr/bin:/usr/sbin", | 118 | /* Set pre and post processing */ |
| 121 | "SHELL=/bin/sh", | 119 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY; |
| 122 | 0, | 120 | tty.c_oflag = OPOST|ONLCR; |
| 123 | "USER=root", | 121 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE; |
| 124 | 0 | 122 | |
| 125 | }; | 123 | /* Now set the terminal line. */ |
| 126 | 124 | ioctl(fd, TCSETS, &tty); | |
| 127 | static const char press_enter[] = | 125 | close( fd); |
| 128 | "\nPlease press Enter to activate this console. "; | ||
| 129 | |||
| 130 | int pid; | ||
| 131 | |||
| 132 | environment[3]=term_ptr; | ||
| 133 | |||
| 134 | pid = fork(); | ||
| 135 | if ( pid == 0 ) { | ||
| 136 | struct termios t; | ||
| 137 | const char * const * arg; | ||
| 138 | |||
| 139 | close(0); | ||
| 140 | close(1); | ||
| 141 | close(2); | ||
| 142 | setsid(); | ||
| 143 | |||
| 144 | open(terminal, O_RDWR); | ||
| 145 | dup(0); | ||
| 146 | dup(0); | ||
| 147 | tcsetpgrp(0, getpgrp()); | ||
| 148 | |||
| 149 | tcgetattr(0, &t); | ||
| 150 | memcpy(t.c_cc, control_characters, sizeof(control_characters)); | ||
| 151 | t.c_line = 0; | ||
| 152 | t.c_iflag = ICRNL|IXON|IXOFF; | ||
| 153 | t.c_oflag = OPOST|ONLCR; | ||
| 154 | t.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN; | ||
| 155 | tcsetattr(0, TCSANOW, &t); | ||
| 156 | |||
| 157 | if ( get_enter ) { | ||
| 158 | /* | ||
| 159 | * Save memory by not exec-ing anything large (like a shell) | ||
| 160 | * before the user wants it. This is critical if swap is not | ||
| 161 | * enabled and the system has low memory. Generally this will | ||
| 162 | * be run on the second virtual console, and the first will | ||
| 163 | * be allowed to start a shell or whatever an init script | ||
| 164 | * specifies. | ||
| 165 | */ | ||
| 166 | char c; | ||
| 167 | write(1, press_enter, sizeof(press_enter) - 1); | ||
| 168 | read(0, &c, 1); | ||
| 169 | } | ||
| 170 | |||
| 171 | message(log, "Executing "); | ||
| 172 | arg = arguments; | ||
| 173 | while ( *arg != 0 ) | ||
| 174 | message(log, "%s ", *arg++); | ||
| 175 | message(log, "\n"); | ||
| 176 | |||
| 177 | execve(program, (char * *)arguments, (char * *)environment); | ||
| 178 | message(log, "%s: could not execute: %s.\r\n", program, strerror(errno)); | ||
| 179 | exit(-1); | ||
| 180 | } | ||
| 181 | return pid; | ||
| 182 | } | 126 | } |
| 183 | 127 | ||
| 184 | static int | 128 | static int |
| @@ -217,33 +161,168 @@ set_free_pages() | |||
| 217 | fclose(f); | 161 | fclose(f); |
| 218 | } | 162 | } |
| 219 | 163 | ||
| 164 | |||
| 220 | static void | 165 | static void |
| 221 | shutdown_system(void) | 166 | console_init() |
| 222 | { | 167 | { |
| 223 | static const char * const umount_args[] = {"/bin/umount", "-a", "-n", 0}; | 168 | int fd; |
| 224 | static const char * const swapoff_args[] = {"/bin/swapoff", "-a", 0}; | 169 | int tried_devcons = 0; |
| 170 | int tried_vtmaster = 0; | ||
| 171 | char *s; | ||
| 172 | |||
| 173 | if ((s = getenv("CONSOLE")) != NULL) | ||
| 174 | console = s; | ||
| 175 | else { | ||
| 176 | console = CONSOLE; | ||
| 177 | tried_devcons++; | ||
| 178 | } | ||
| 179 | while ((fd = open(console, O_RDONLY|O_NONBLOCK)) < 0) { | ||
| 180 | if (!tried_devcons) { | ||
| 181 | tried_devcons++; | ||
| 182 | console = CONSOLE; | ||
| 183 | continue; | ||
| 184 | } | ||
| 185 | if (!tried_vtmaster) { | ||
| 186 | tried_vtmaster++; | ||
| 187 | console = VT_PRIMARY; | ||
| 188 | continue; | ||
| 189 | } | ||
| 190 | break; | ||
| 191 | } | ||
| 192 | if (fd < 0) | ||
| 193 | console = "/dev/null"; | ||
| 194 | else | ||
| 195 | close(fd); | ||
| 196 | } | ||
| 225 | 197 | ||
| 226 | message(console, "The system is going down NOW !!"); | 198 | static int |
| 227 | sync(); | 199 | waitfor(int pid) |
| 228 | /* Allow Ctrl-Alt-Del to reboot system. */ | 200 | { |
| 229 | reboot(RB_ENABLE_CAD); | 201 | int status, wpid; |
| 230 | 202 | ||
| 231 | /* Send signals to every process _except_ pid 1 */ | 203 | message(log, "Waiting for process %d.\n", pid); |
| 232 | message(console, "Sending SIGHUP to all processes.\r\n"); | 204 | while ( (wpid = wait(&status)) != pid ) { |
| 233 | kill(-1, SIGHUP); | 205 | if ( wpid > 0 ) |
| 234 | sleep(2); | 206 | message(log,"pid %d exited, status=%x.\n", wpid, status); |
| 235 | sync(); | 207 | } |
| 236 | message(console, "Sending SIGKILL to all processes.\r\n"); | 208 | return wpid; |
| 237 | kill(-1, SIGKILL); | 209 | } |
| 238 | sleep(1); | 210 | |
| 239 | waitfor(run("/bin/swapoff", swapoff_args, console, 0)); | 211 | static int |
| 240 | waitfor(run("/bin/umount", umount_args, console, 0)); | 212 | run(const char* command, char* terminal, int get_enter) |
| 241 | sync(); | 213 | { |
| 242 | if (get_kernel_revision() <= 2*65536+2*256+11) { | 214 | int f, pid; |
| 243 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ | 215 | char *args[16]; |
| 244 | bdflush(1, 0); | 216 | char buf[256]; |
| 245 | sync(); | 217 | char* ptr; |
| 218 | static const char press_enter[] = | ||
| 219 | "\nPlease press Enter to activate this console. "; | ||
| 220 | |||
| 221 | |||
| 222 | /* Make a proper command from the command string */ | ||
| 223 | strcpy(buf, command); | ||
| 224 | ptr = buf; | ||
| 225 | for(f = 1; f < 15; f++) { | ||
| 226 | /* Skip white space */ | ||
| 227 | while(*ptr == ' ' || *ptr == '\t') ptr++; | ||
| 228 | args[f] = ptr; | ||
| 229 | /* May be trailing space.. */ | ||
| 230 | if (*ptr == 0) break; | ||
| 231 | /* Skip this `word' */ | ||
| 232 | while(*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#') | ||
| 233 | ptr++; | ||
| 234 | /* If end-of-line, break */ | ||
| 235 | if (*ptr == '#' || *ptr == 0) { | ||
| 236 | f++; | ||
| 237 | *ptr = 0; | ||
| 238 | break; | ||
| 239 | } | ||
| 240 | /* End word with \0 and continue */ | ||
| 241 | args[f] = NULL; | ||
| 242 | } | ||
| 243 | args[0] = args[1]; | ||
| 244 | |||
| 245 | |||
| 246 | if ((pid = fork()) == 0) { | ||
| 247 | /* Clean up */ | ||
| 248 | close(0); | ||
| 249 | close(1); | ||
| 250 | close(2); | ||
| 251 | setsid(); | ||
| 252 | |||
| 253 | if ((f = device_open(terminal, O_RDWR|O_NOCTTY)) < 0) { | ||
| 254 | message( log, "open(%s) failed: %s", terminal, strerror(errno)); | ||
| 255 | return -1; | ||
| 256 | } | ||
| 257 | dup(f); | ||
| 258 | dup(f); | ||
| 259 | tcsetpgrp(0, getpgrp()); | ||
| 260 | set_term(); | ||
| 261 | |||
| 262 | if ( get_enter ) { | ||
| 263 | /* | ||
| 264 | * Save memory by not exec-ing anything large (like a shell) | ||
| 265 | * before the user wants it. This is critical if swap is not | ||
| 266 | * enabled and the system has low memory. Generally this will | ||
| 267 | * be run on the second virtual console, and the first will | ||
| 268 | * be allowed to start a shell or whatever an init script | ||
| 269 | * specifies. | ||
| 270 | */ | ||
| 271 | char c; | ||
| 272 | write(1, press_enter, sizeof(press_enter) - 1); | ||
| 273 | read(0, &c, 1); | ||
| 246 | } | 274 | } |
| 275 | |||
| 276 | /* Log the process name and args */ | ||
| 277 | message(log, "Executing '%s'\n", command); | ||
| 278 | |||
| 279 | /* Now run it. This should take over the PID, so nothing | ||
| 280 | * further in init.c should be run by this PID. */ | ||
| 281 | execvp(args[1], args + 1); | ||
| 282 | |||
| 283 | /* If shell scripts are not executed, force the issue */ | ||
| 284 | if (errno == ENOEXEC) { | ||
| 285 | char buf[256]; | ||
| 286 | args[1] = SHELL; | ||
| 287 | args[2] = "-c"; | ||
| 288 | strcpy(buf, "exec "); | ||
| 289 | strcat(buf, command); | ||
| 290 | args[3] = buf; | ||
| 291 | args[4] = NULL; | ||
| 292 | execvp(args[1], args + 1); | ||
| 293 | } | ||
| 294 | message(log, "Could not execute '%s'\n", command, strerror(errno)); | ||
| 295 | exit(-1); | ||
| 296 | } | ||
| 297 | return pid; | ||
| 298 | } | ||
| 299 | |||
| 300 | #ifndef DEBUG_INIT | ||
| 301 | static void | ||
| 302 | shutdown_system(void) | ||
| 303 | { | ||
| 304 | |||
| 305 | message(console, "The system is going down NOW !!"); | ||
| 306 | sync(); | ||
| 307 | /* Allow Ctrl-Alt-Del to reboot system. */ | ||
| 308 | reboot(RB_ENABLE_CAD); | ||
| 309 | |||
| 310 | /* Send signals to every process _except_ pid 1 */ | ||
| 311 | message(console, "Sending SIGHUP to all processes.\r\n"); | ||
| 312 | kill(-1, SIGHUP); | ||
| 313 | sleep(2); | ||
| 314 | sync(); | ||
| 315 | message(console, "Sending SIGKILL to all processes.\r\n"); | ||
| 316 | kill(-1, SIGKILL); | ||
| 317 | sleep(1); | ||
| 318 | waitfor(run("/bin/swapoff -a", console, 0)); | ||
| 319 | waitfor(run("/bin/umount -a -n", console, 0)); | ||
| 320 | sync(); | ||
| 321 | if (get_kernel_revision() <= 2*65536+2*256+11) { | ||
| 322 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ | ||
| 323 | bdflush(1, 0); | ||
| 324 | sync(); | ||
| 325 | } | ||
| 247 | } | 326 | } |
| 248 | 327 | ||
| 249 | static void | 328 | static void |
| @@ -251,7 +330,7 @@ halt_signal(int sig) | |||
| 251 | { | 330 | { |
| 252 | shutdown_system(); | 331 | shutdown_system(); |
| 253 | message(console, "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); | 332 | message(console, "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); |
| 254 | reboot( RB_HALT_SYSTEM); | 333 | reboot( RB_POWER_OFF); |
| 255 | exit(0); | 334 | exit(0); |
| 256 | } | 335 | } |
| 257 | 336 | ||
| @@ -263,202 +342,136 @@ reboot_signal(int sig) | |||
| 263 | reboot( RB_AUTOBOOT); | 342 | reboot( RB_AUTOBOOT); |
| 264 | exit(0); | 343 | exit(0); |
| 265 | } | 344 | } |
| 345 | #endif | ||
| 266 | 346 | ||
| 267 | static void | 347 | int setproctitle(char *fmt, ...) |
| 268 | configure_terminals( int serial_cons, int single_user_mode ) | ||
| 269 | { | 348 | { |
| 270 | char *tty; | 349 | va_list ap; |
| 271 | struct serial_struct sr; | 350 | int len; |
| 272 | struct vt_stat vt; | 351 | char buf[256]; |
| 273 | 352 | ||
| 274 | 353 | buf[0] = 0; | |
| 275 | switch (serial_cons) { | 354 | va_start(ap, fmt); |
| 276 | case -1: | 355 | len = vsprintf(buf, fmt, ap); |
| 277 | /* 2.2 kernels: | 356 | va_end(ap); |
| 278 | * identify the real console backend and try to make use of it */ | 357 | memset(argv0, 0, maxproclen + 1); |
| 279 | if (ioctl(0,TIOCGSERIAL,&sr) == 0) { | 358 | strncpy(argv0, buf, maxproclen); |
| 280 | sprintf( console, "/dev/ttyS%d", sr.line ); | 359 | return len; |
| 281 | serial_cons = sr.line+1; | ||
| 282 | } | ||
| 283 | else if (ioctl(0, VT_GETSTATE, &vt) == 0) { | ||
| 284 | sprintf( console, "/dev/tty%d", vt.v_active ); | ||
| 285 | serial_cons = 0; | ||
| 286 | } | ||
| 287 | else { | ||
| 288 | /* unknown backend: fallback to /dev/console */ | ||
| 289 | strcpy( console, "/dev/console" ); | ||
| 290 | serial_cons = 0; | ||
| 291 | } | ||
| 292 | break; | ||
| 293 | |||
| 294 | case 1: | ||
| 295 | strcpy( console, "/dev/cua0" ); | ||
| 296 | break; | ||
| 297 | case 2: | ||
| 298 | strcpy( console, "/dev/cua1" ); | ||
| 299 | break; | ||
| 300 | default: | ||
| 301 | tty = ttyname(0); | ||
| 302 | if (tty) { | ||
| 303 | strcpy( console, tty ); | ||
| 304 | if (!strncmp( tty, "/dev/cua", 8 )) | ||
| 305 | serial_cons=1; | ||
| 306 | } | ||
| 307 | else | ||
| 308 | /* falls back to /dev/tty1 if an error occurs */ | ||
| 309 | strcpy( console, default_console ); | ||
| 310 | } | ||
| 311 | if (!first_terminal) | ||
| 312 | first_terminal = console; | ||
| 313 | #if defined (__sparc__) | ||
| 314 | if (serial_cons > 0 && !strncmp(term_ptr,"TERM=linux",10)) | ||
| 315 | term_ptr = "TERM=vt100"; | ||
| 316 | #endif | ||
| 317 | if (serial_cons) { | ||
| 318 | /* disable other but the first terminal: | ||
| 319 | * VT is not initialized anymore on 2.2 kernel when booting from | ||
| 320 | * serial console, therefore modprobe is flooding the display with | ||
| 321 | * "can't locate module char-major-4" messages. */ | ||
| 322 | log = 0; | ||
| 323 | second_terminal = 0; | ||
| 324 | } | ||
| 325 | } | 360 | } |
| 326 | 361 | ||
| 327 | extern int | 362 | extern int |
| 328 | init_main(int argc, char * * argv) | 363 | init_main(int argc, char * * argv) |
| 329 | { | 364 | { |
| 330 | const char * rc_arguments[100]; | 365 | int run_rc = TRUE; |
| 331 | const char * arguments[100]; | 366 | int pid1 = 0; |
| 332 | int run_rc = TRUE; | 367 | int pid2 = 0; |
| 333 | int j; | 368 | struct stat statbuf; |
| 334 | int pid1 = 0; | 369 | const char* init_commands = SHELL "-c exec " INITSCRIPT; |
| 335 | int pid2 = 0; | 370 | const char* shell_commands = SHELL; |
| 336 | struct stat statbuf; | 371 | const char* tty0_commands = init_commands; |
| 337 | const char * tty_commands[3] = { "etc/init.d/rcS", "bin/sh"}; | 372 | const char* tty1_commands = shell_commands; |
| 338 | int serial_console = 0; | 373 | const char* no_memory = |
| 339 | int retval; | 374 | "Sorry, your computer does not have enough memory.\n"; |
| 340 | 375 | ||
| 341 | /* | 376 | /* For later use */ |
| 342 | * If I am started as /linuxrc instead of /sbin/init, I don't have the | 377 | argv0 = argv[0]; |
| 343 | * environment that init expects. I can't fix the signal behavior. Try | 378 | maxproclen = strlen(argv[0]); |
| 344 | * to divorce from the controlling terminal with setsid(). This won't work | 379 | setproctitle("init [boot]"); |
| 345 | * if I am the process group leader. | 380 | |
| 346 | */ | 381 | |
| 347 | setsid(); | 382 | #ifndef DEBUG_INIT |
| 348 | 383 | /* Set up sig handlers */ | |
| 349 | signal(SIGUSR1, halt_signal); | 384 | signal(SIGUSR1, halt_signal); |
| 385 | signal(SIGSEGV, halt_signal); | ||
| 386 | signal(SIGPWR, halt_signal); | ||
| 387 | signal(SIGALRM, halt_signal); | ||
| 388 | signal(SIGHUP, halt_signal); | ||
| 350 | signal(SIGUSR2, reboot_signal); | 389 | signal(SIGUSR2, reboot_signal); |
| 351 | signal(SIGINT, reboot_signal); | 390 | signal(SIGINT, reboot_signal); |
| 352 | signal(SIGTERM, reboot_signal); | 391 | signal(SIGTERM, reboot_signal); |
| 392 | #endif | ||
| 393 | /* Figure out where the default console should be */ | ||
| 394 | console_init(); | ||
| 353 | 395 | ||
| 396 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | ||
| 397 | * SIGINT on CAD so we can shut things down gracefully... */ | ||
| 398 | #ifndef DEBUG_INIT | ||
| 354 | reboot(RB_DISABLE_CAD); | 399 | reboot(RB_DISABLE_CAD); |
| 400 | #endif | ||
| 355 | 401 | ||
| 356 | message(log, "%s: started. ", argv[0]); | 402 | /* Close whatever files are open, and reset the console. */ |
| 403 | close(0); | ||
| 404 | close(1); | ||
| 405 | close(2); | ||
| 406 | set_term(); | ||
| 407 | setsid(); | ||
| 357 | 408 | ||
| 358 | for ( j = 1; j < argc; j++ ) { | 409 | /* Make sure PATH is set to something sane */ |
| 359 | if ( strcmp(argv[j], "single") == 0 ) { | 410 | if (getenv("PATH") == NULL) |
| 360 | run_rc = FALSE; | 411 | putenv(PATH_DEFAULT); |
| 361 | tty_commands[0] = "bin/sh"; | ||
| 362 | tty_commands[1] = 0; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | for ( j = 0; __environ[j] != 0; j++ ) { | ||
| 366 | if ( strncmp(__environ[j], "tty", 3) == 0 | ||
| 367 | && __environ[j][3] >= '1' | ||
| 368 | && __environ[j][3] <= '2' | ||
| 369 | && __environ[j][4] == '=' ) { | ||
| 370 | const char * s = &__environ[j][5]; | ||
| 371 | 412 | ||
| 372 | if ( *s == 0 || strcmp(s, "off") == 0 ) | 413 | /* Hello world */ |
| 373 | s = 0; | 414 | message(console, "%s started: BusyBox v%s (%s) multi-call binary", |
| 415 | argv[0], BB_VER, BB_BT); | ||
| 416 | message(log, "%s started: BusyBox v%s (%s) multi-call binary", | ||
| 417 | argv[0], BB_VER, BB_BT); | ||
| 374 | 418 | ||
| 375 | tty_commands[__environ[j][3] - '1'] = s; | ||
| 376 | } | ||
| 377 | /* Should catch the syntax of Sparc kernel console setting. */ | ||
| 378 | /* The kernel does not recognize a serial console when getting*/ | ||
| 379 | /* console=/dev/ttySX !! */ | ||
| 380 | else if ( strcmp(__environ[j], "console=ttya") == 0 ) { | ||
| 381 | serial_console=1; | ||
| 382 | } | ||
| 383 | else if ( strcmp(__environ[j], "console=ttyb") == 0 ) { | ||
| 384 | serial_console=2; | ||
| 385 | } | ||
| 386 | /* standard console settings */ | ||
| 387 | else if ( strncmp(__environ[j], "console=", 8) == 0 ) { | ||
| 388 | first_terminal=&(__environ[j][8]); | ||
| 389 | } | ||
| 390 | else if ( strncmp(__environ[j], "TERM=", 5) == 0) { | ||
| 391 | term_ptr=__environ[j]; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | 419 | ||
| 395 | printf("mounting /proc ...\n"); | 420 | /* Mount /proc */ |
| 421 | message(console, "Mounting /proc: \n"); | ||
| 396 | if (mount("/proc","/proc","proc",0,0)) { | 422 | if (mount("/proc","/proc","proc",0,0)) { |
| 397 | perror("mounting /proc failed\n"); | 423 | message(log, "%s: could not mount /proc!\n", argv[0]); |
| 398 | } | 424 | message(console, "failed!\n"); |
| 399 | printf("\tdone.\n"); | ||
| 400 | |||
| 401 | if (get_kernel_revision() >= 2*65536+1*256+71) { | ||
| 402 | /* if >= 2.1.71 kernel, /dev/console is not a symlink anymore: | ||
| 403 | * use it as primary console */ | ||
| 404 | serial_console=-1; | ||
| 405 | } | 425 | } |
| 426 | message(console, "done.\n"); | ||
| 406 | 427 | ||
| 407 | /* Make sure /etc/init.d/rc exists */ | ||
| 408 | retval= stat(tty_commands[0],&statbuf); | ||
| 409 | if (retval) | ||
| 410 | run_rc = FALSE; | ||
| 411 | |||
| 412 | configure_terminals( serial_console, run_rc); | ||
| 413 | 428 | ||
| 429 | /* Make sure there is enough memory to do something useful*/ | ||
| 414 | set_free_pages(); | 430 | set_free_pages(); |
| 415 | |||
| 416 | /* not enough memory to do anything useful*/ | ||
| 417 | if (mem_total() < 2000) { | 431 | if (mem_total() < 2000) { |
| 432 | int retval; | ||
| 418 | retval= stat("/etc/fstab",&statbuf); | 433 | retval= stat("/etc/fstab",&statbuf); |
| 419 | if (retval) { | 434 | if (retval) { |
| 420 | printf("You do not have enough RAM, sorry.\n"); | 435 | message(console, "%s", no_memory); |
| 421 | while (1) { sleep(1);} | 436 | while (1) { sleep(1);} |
| 422 | } else { | 437 | } else { |
| 423 | /* Try to turn on swap */ | 438 | /* Try to turn on swap */ |
| 424 | static const char * const swapon_args[] = {"/bin/swapon", "-a", 0}; | 439 | waitfor(run("/bin/swapon -a", console, 0)); |
| 425 | waitfor(run("/bin/swapon", swapon_args, console, 0)); | ||
| 426 | if (mem_total() < 2000) { | 440 | if (mem_total() < 2000) { |
| 427 | printf("You do not have enough RAM, sorry.\n"); | 441 | message(console, "%s", no_memory); |
| 428 | while (1) { sleep(1);} | 442 | while (1) { sleep(1);} |
| 429 | } | 443 | } |
| 430 | } | 444 | } |
| 431 | } | 445 | } |
| 432 | 446 | ||
| 433 | /* | 447 | /* Check if we are supposed to be in single user mode */ |
| 434 | * Don't modify **argv directly, it would show up in the "ps" display. | 448 | if (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || !strcmp(argv[1], "1")) { |
| 435 | * I don't want "init" to look like "rc". | 449 | run_rc = FALSE; |
| 436 | */ | 450 | tty0_commands = shell_commands; |
| 437 | rc_arguments[0] = tty_commands[0]; | 451 | tty1_commands = 0; |
| 438 | for ( j = 1; j < argc; j++ ) { | 452 | setproctitle("init [S]"); |
| 439 | rc_arguments[j] = argv[j]; | 453 | } else { |
| 454 | setproctitle("init [1]"); | ||
| 440 | } | 455 | } |
| 441 | rc_arguments[j] = 0; | ||
| 442 | 456 | ||
| 443 | arguments[0] = "-sh"; | 457 | /* Make sure an init script exists before trying to run it */ |
| 444 | arguments[1] = 0; | 458 | if ( run_rc == TRUE && stat( INITSCRIPT, &statbuf)) { |
| 445 | 459 | tty0_commands = shell_commands; | |
| 446 | /* Ok, now launch the rc script /etc/init.d/rcS and prepare to start up | 460 | tty1_commands = shell_commands; |
| 447 | * some VTs on tty1 and tty2 if somebody hits enter | 461 | } |
| 462 | |||
| 463 | /* Ok, now launch the rc script and/or prepare to | ||
| 464 | * start up some VTs if somebody hits enter... | ||
| 448 | */ | 465 | */ |
| 449 | for ( ; ; ) { | 466 | for ( ; ; ) { |
| 450 | int wpid; | 467 | int wpid; |
| 451 | int status; | 468 | int status; |
| 452 | 469 | ||
| 453 | if ( pid1 == 0 && tty_commands[0] ) { | 470 | if ( pid1 == 0 && *tty0_commands ) { |
| 454 | if ( run_rc == TRUE ) { | 471 | pid1 = run(tty0_commands, console, 1); |
| 455 | pid1 = run(tty_commands[0], rc_arguments, first_terminal, 0); | ||
| 456 | } else { | ||
| 457 | pid2 = run(tty_commands[1], arguments, first_terminal, 1); | ||
| 458 | } | ||
| 459 | } | 472 | } |
| 460 | if ( pid2 == 0 && second_terminal && tty_commands[1] ) { | 473 | if ( pid2 == 0 && *tty1_commands ) { |
| 461 | pid2 = run(tty_commands[1], arguments, second_terminal, 1); | 474 | pid2 = run(tty1_commands, second_terminal, 1); |
| 462 | } | 475 | } |
| 463 | wpid = wait(&status); | 476 | wpid = wait(&status); |
| 464 | if ( wpid > 0 && wpid != pid1) { | 477 | if ( wpid > 0 && wpid != pid1) { |
| @@ -467,6 +480,7 @@ init_main(int argc, char * * argv) | |||
| 467 | if ( wpid == pid2 ) { | 480 | if ( wpid == pid2 ) { |
| 468 | pid2 = 0; | 481 | pid2 = 0; |
| 469 | } | 482 | } |
| 483 | sleep(1); | ||
| 470 | } | 484 | } |
| 471 | } | 485 | } |
| 472 | 486 | ||
diff --git a/internal.h b/internal.h index 0caaf5cb4..35f990aa7 100644 --- a/internal.h +++ b/internal.h | |||
| @@ -82,6 +82,8 @@ extern int loadfont_main(int argc, char** argv); | |||
| 82 | extern int loadkmap_main(int argc, char** argv); | 82 | extern int loadkmap_main(int argc, char** argv); |
| 83 | extern int losetup_main(int argc, char** argv); | 83 | extern int losetup_main(int argc, char** argv); |
| 84 | extern int ls_main(int argc, char** argv); | 84 | extern int ls_main(int argc, char** argv); |
| 85 | extern int chvt_main(int argc, char** argv); | ||
| 86 | extern int deallocvt_main(int argc, char** argv); | ||
| 85 | extern int makedevs_main(int argc, char** argv); | 87 | extern int makedevs_main(int argc, char** argv); |
| 86 | extern int math_main(int argc, char** argv); | 88 | extern int math_main(int argc, char** argv); |
| 87 | extern int mkdir_main(int argc, char** argv); | 89 | extern int mkdir_main(int argc, char** argv); |
| @@ -110,6 +112,7 @@ extern int true_main(int argc, char** argv); | |||
| 110 | extern int tryopen_main(int argc, char** argv); | 112 | extern int tryopen_main(int argc, char** argv); |
| 111 | extern int umount_main(int argc, char** argv); | 113 | extern int umount_main(int argc, char** argv); |
| 112 | extern int update_main(int argc, char** argv); | 114 | extern int update_main(int argc, char** argv); |
| 115 | extern int uname_main(int argc, char** argv); | ||
| 113 | extern int zcat_main(int argc, char** argv); | 116 | extern int zcat_main(int argc, char** argv); |
| 114 | extern int gzip_main(int argc, char** argv); | 117 | extern int gzip_main(int argc, char** argv); |
| 115 | 118 | ||
| @@ -141,7 +144,7 @@ extern gid_t my_getgrnam(char *name); | |||
| 141 | extern void my_getpwuid(char* name, uid_t uid); | 144 | extern void my_getpwuid(char* name, uid_t uid); |
| 142 | extern void my_getgrgid(char* group, gid_t gid); | 145 | extern void my_getgrgid(char* group, gid_t gid); |
| 143 | extern int get_kernel_revision(); | 146 | extern int get_kernel_revision(); |
| 144 | 147 | extern int get_console_fd(char* tty_name); | |
| 145 | 148 | ||
| 146 | 149 | ||
| 147 | #if defined (BB_FSCK_MINIX) || defined (BB_MKFS_MINIX) | 150 | #if defined (BB_FSCK_MINIX) || defined (BB_MKFS_MINIX) |
| @@ -720,5 +720,97 @@ extern int find_match(char *haystack, char *needle, int ignoreCase) | |||
| 720 | } | 720 | } |
| 721 | #endif | 721 | #endif |
| 722 | 722 | ||
| 723 | |||
| 724 | |||
| 725 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) | ||
| 726 | |||
| 727 | |||
| 728 | #include <linux/kd.h> | ||
| 729 | #include <sys/ioctl.h> | ||
| 730 | |||
| 731 | int is_a_console(int fd) | ||
| 732 | { | ||
| 733 | char arg; | ||
| 734 | |||
| 735 | arg = 0; | ||
| 736 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 | ||
| 737 | && ((arg == KB_101) || (arg == KB_84))); | ||
| 738 | } | ||
| 739 | |||
| 740 | static int open_a_console(char *fnam) | ||
| 741 | { | ||
| 742 | int fd; | ||
| 743 | |||
| 744 | /* try read-only */ | ||
| 745 | fd = open(fnam, O_RDWR); | ||
| 746 | |||
| 747 | /* if failed, try read-only */ | ||
| 748 | if (fd < 0 && errno == EACCES) | ||
| 749 | fd = open(fnam, O_RDONLY); | ||
| 750 | |||
| 751 | /* if failed, try write-only */ | ||
| 752 | if (fd < 0 && errno == EACCES) | ||
| 753 | fd = open(fnam, O_WRONLY); | ||
| 754 | |||
| 755 | /* if failed, fail */ | ||
| 756 | if (fd < 0) | ||
| 757 | return -1; | ||
| 758 | |||
| 759 | /* if not a console, fail */ | ||
| 760 | if (! is_a_console(fd)) | ||
| 761 | { | ||
| 762 | close(fd); | ||
| 763 | return -1; | ||
| 764 | } | ||
| 765 | |||
| 766 | /* success */ | ||
| 767 | return fd; | ||
| 768 | } | ||
| 769 | |||
| 770 | /* | ||
| 771 | * Get an fd for use with kbd/console ioctls. | ||
| 772 | * We try several things because opening /dev/console will fail | ||
| 773 | * if someone else used X (which does a chown on /dev/console). | ||
| 774 | * | ||
| 775 | * if tty_name is non-NULL, try this one instead. | ||
| 776 | */ | ||
| 777 | |||
| 778 | int get_console_fd(char* tty_name) | ||
| 779 | { | ||
| 780 | int fd; | ||
| 781 | |||
| 782 | if (tty_name) | ||
| 783 | { | ||
| 784 | if (-1 == (fd = open_a_console(tty_name))) | ||
| 785 | return -1; | ||
| 786 | else | ||
| 787 | return fd; | ||
| 788 | } | ||
| 789 | |||
| 790 | fd = open_a_console("/dev/tty"); | ||
| 791 | if (fd >= 0) | ||
| 792 | return fd; | ||
| 793 | |||
| 794 | fd = open_a_console("/dev/tty0"); | ||
| 795 | if (fd >= 0) | ||
| 796 | return fd; | ||
| 797 | |||
| 798 | fd = open_a_console("/dev/console"); | ||
| 799 | if (fd >= 0) | ||
| 800 | return fd; | ||
| 801 | |||
| 802 | for (fd = 0; fd < 3; fd++) | ||
| 803 | if (is_a_console(fd)) | ||
| 804 | return fd; | ||
| 805 | |||
| 806 | fprintf(stderr, | ||
| 807 | "Couldnt get a file descriptor referring to the console\n"); | ||
| 808 | return -1; /* total failure */ | ||
| 809 | } | ||
| 810 | |||
| 811 | |||
| 812 | #endif | ||
| 813 | |||
| 814 | |||
| 723 | /* END CODE */ | 815 | /* END CODE */ |
| 724 | 816 | ||
