This commit is contained in:
Eric Andersen 1999-10-27 02:31:32 +00:00
parent c8fdb56389
commit a745606df3
2 changed files with 76 additions and 30 deletions

53
init.c
View File

@ -52,6 +52,7 @@
#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
static char *console = CONSOLE; static char *console = CONSOLE;
static char *first_terminal = "/dev/tty1";
static char *second_terminal = "/dev/tty2"; static char *second_terminal = "/dev/tty2";
static char *log = "/dev/tty3"; static char *log = "/dev/tty3";
@ -87,8 +88,13 @@ void message(char *device, char *fmt, ...)
vdprintf(fd, fmt, arguments); vdprintf(fd, fmt, arguments);
va_end(arguments); va_end(arguments);
close(fd); close(fd);
} else } else {
vprintf(fmt, arguments); fprintf(stderr, "Bummer, can't print: ");
va_start(arguments, fmt);
vfprintf(stderr, fmt, arguments);
fflush(stderr);
va_end(arguments);
}
} }
/* Set terminal settings to reasonable defaults */ /* Set terminal settings to reasonable defaults */
@ -164,6 +170,7 @@ static void set_free_pages()
static void console_init() static void console_init()
{ {
int fd; int fd;
struct stat statbuf;
int tried_devcons = 0; int tried_devcons = 0;
int tried_vtmaster = 0; int tried_vtmaster = 0;
char *s; char *s;
@ -174,6 +181,10 @@ static void console_init()
console = CONSOLE; console = CONSOLE;
tried_devcons++; tried_devcons++;
} }
if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
fprintf(stderr, "/dev/console does not exist, or is a symlink.\n");
}
while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
if (!tried_devcons) { if (!tried_devcons) {
tried_devcons++; tried_devcons++;
@ -191,6 +202,7 @@ static void console_init()
console = "/dev/null"; console = "/dev/null";
else else
close(fd); close(fd);
fprintf(stderr, "console=%s\n", console);
} }
static int waitfor(int pid) static int waitfor(int pid)
@ -205,6 +217,7 @@ static int waitfor(int pid)
return wpid; return wpid;
} }
static int run(const char *command, char *terminal, int get_enter) static int run(const char *command, char *terminal, int get_enter)
{ {
int f, pid; int f, pid;
@ -248,13 +261,21 @@ static int run(const char *command, char *terminal, int get_enter)
close(2); close(2);
setsid(); setsid();
if ((f = device_open(terminal, O_RDWR | O_NOCTTY)) < 0) { #if 1
message(log, "open(%s) failed: %s\n", terminal, strerror(errno)); //if ((f = device_open(terminal, O_RDWR | O_NOCTTY)) < 0) {
if ((f = device_open(terminal, O_RDWR )) < 0) {
message(log, "open(%s) failed: %s\n",
terminal, strerror(errno));
return -1; return -1;
} }
dup(f); dup(f);
dup(f); dup(f);
tcsetpgrp(0, getpgrp()); #else
open(terminal, O_RDWR);
dup(0);
dup(0);
//tcsetpgrp(0, getpgrp());
#endif
set_term(); set_term();
if (get_enter) { if (get_enter) {
@ -273,11 +294,11 @@ static int run(const char *command, char *terminal, int get_enter)
} }
/* Log the process name and args */ /* Log the process name and args */
message(console, "Executing "); message(console, "Executing '%s'\r\n", command);
message(console, "'%s'\r\n", command); message(log, "Executing '%s'\r\n", command);
/* Now run it. This should take over the PID, so nothing /* Now run it. This program should take over this PID,
* further in init.c should be run by this PID. */ * so nothing further in init.c should be run. */
execvp(args[1], args + 1); execvp(args[1], args + 1);
message(console, "Hmm. Trying as a script.\r\n"); message(console, "Hmm. Trying as a script.\r\n");
@ -298,6 +319,7 @@ static int run(const char *command, char *terminal, int get_enter)
return pid; return pid;
} }
#ifndef DEBUG_INIT #ifndef DEBUG_INIT
static void shutdown_system(void) static void shutdown_system(void)
{ {
@ -350,8 +372,8 @@ extern int init_main(int argc, char **argv)
int pid1 = 0; int pid1 = 0;
int pid2 = 0; int pid2 = 0;
struct stat statbuf; struct stat statbuf;
const char *init_commands = SHELL "-c exec " INITSCRIPT; const char *init_commands = SHELL " -c exec " INITSCRIPT;
const char *shell_commands = SHELL; const char *shell_commands = SHELL " -";
const char *tty0_commands = init_commands; const char *tty0_commands = init_commands;
const char *tty1_commands = shell_commands; const char *tty1_commands = shell_commands;
char *hello_msg_format = char *hello_msg_format =
@ -397,10 +419,11 @@ extern int init_main(int argc, char **argv)
/* Mount /proc */ /* Mount /proc */
if (mount("/proc", "/proc", "proc", 0, 0)) { if (mount("/proc", "/proc", "proc", 0, 0)) {
message(log, "Mounting /proc: failed!\n");
message(console, "Mounting /proc: failed!\r\n"); message(console, "Mounting /proc: failed!\r\n");
message(log, "Mounting /proc: failed!\n");
} else { } else {
message(console, "Mounting /proc: done.\r\n"); message(console, "Mounting /proc: done.\r\n");
message(log, "Mounting /proc: done.\n");
} }
/* Make sure there is enough memory to do something useful */ /* Make sure there is enough memory to do something useful */
@ -430,7 +453,7 @@ extern int init_main(int argc, char **argv)
!strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
run_rc = FALSE; run_rc = FALSE;
tty0_commands = shell_commands; tty0_commands = shell_commands;
tty1_commands = 0; tty1_commands = shell_commands;
} }
/* Make sure an init script exists before trying to run it */ /* Make sure an init script exists before trying to run it */
@ -447,13 +470,13 @@ extern int init_main(int argc, char **argv)
int status; int status;
if (pid1 == 0 && tty0_commands) { if (pid1 == 0 && tty0_commands) {
pid1 = run(tty0_commands, console, 1); pid1 = run(tty0_commands, first_terminal, 1);
} }
if (pid2 == 0 && tty1_commands) { if (pid2 == 0 && tty1_commands) {
pid2 = run(tty1_commands, second_terminal, 1); pid2 = run(tty1_commands, second_terminal, 1);
} }
wpid = wait(&status); wpid = wait(&status);
if (wpid > 0 && wpid != pid1) { if (wpid > 0 ) {
message(log, "pid %d exited, status=%x.\n", wpid, status); message(log, "pid %d exited, status=%x.\n", wpid, status);
} }
/* Don't respawn an init script if it exits */ /* Don't respawn an init script if it exits */

View File

@ -52,6 +52,7 @@
#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
static char *console = CONSOLE; static char *console = CONSOLE;
static char *first_terminal = "/dev/tty1";
static char *second_terminal = "/dev/tty2"; static char *second_terminal = "/dev/tty2";
static char *log = "/dev/tty3"; static char *log = "/dev/tty3";
@ -87,8 +88,13 @@ void message(char *device, char *fmt, ...)
vdprintf(fd, fmt, arguments); vdprintf(fd, fmt, arguments);
va_end(arguments); va_end(arguments);
close(fd); close(fd);
} else } else {
vprintf(fmt, arguments); fprintf(stderr, "Bummer, can't print: ");
va_start(arguments, fmt);
vfprintf(stderr, fmt, arguments);
fflush(stderr);
va_end(arguments);
}
} }
/* Set terminal settings to reasonable defaults */ /* Set terminal settings to reasonable defaults */
@ -164,6 +170,7 @@ static void set_free_pages()
static void console_init() static void console_init()
{ {
int fd; int fd;
struct stat statbuf;
int tried_devcons = 0; int tried_devcons = 0;
int tried_vtmaster = 0; int tried_vtmaster = 0;
char *s; char *s;
@ -174,6 +181,10 @@ static void console_init()
console = CONSOLE; console = CONSOLE;
tried_devcons++; tried_devcons++;
} }
if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
fprintf(stderr, "/dev/console does not exist, or is a symlink.\n");
}
while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
if (!tried_devcons) { if (!tried_devcons) {
tried_devcons++; tried_devcons++;
@ -191,6 +202,7 @@ static void console_init()
console = "/dev/null"; console = "/dev/null";
else else
close(fd); close(fd);
fprintf(stderr, "console=%s\n", console);
} }
static int waitfor(int pid) static int waitfor(int pid)
@ -205,6 +217,7 @@ static int waitfor(int pid)
return wpid; return wpid;
} }
static int run(const char *command, char *terminal, int get_enter) static int run(const char *command, char *terminal, int get_enter)
{ {
int f, pid; int f, pid;
@ -248,13 +261,21 @@ static int run(const char *command, char *terminal, int get_enter)
close(2); close(2);
setsid(); setsid();
if ((f = device_open(terminal, O_RDWR | O_NOCTTY)) < 0) { #if 1
message(log, "open(%s) failed: %s\n", terminal, strerror(errno)); //if ((f = device_open(terminal, O_RDWR | O_NOCTTY)) < 0) {
if ((f = device_open(terminal, O_RDWR )) < 0) {
message(log, "open(%s) failed: %s\n",
terminal, strerror(errno));
return -1; return -1;
} }
dup(f); dup(f);
dup(f); dup(f);
tcsetpgrp(0, getpgrp()); #else
open(terminal, O_RDWR);
dup(0);
dup(0);
//tcsetpgrp(0, getpgrp());
#endif
set_term(); set_term();
if (get_enter) { if (get_enter) {
@ -273,11 +294,11 @@ static int run(const char *command, char *terminal, int get_enter)
} }
/* Log the process name and args */ /* Log the process name and args */
message(console, "Executing "); message(console, "Executing '%s'\r\n", command);
message(console, "'%s'\r\n", command); message(log, "Executing '%s'\r\n", command);
/* Now run it. This should take over the PID, so nothing /* Now run it. This program should take over this PID,
* further in init.c should be run by this PID. */ * so nothing further in init.c should be run. */
execvp(args[1], args + 1); execvp(args[1], args + 1);
message(console, "Hmm. Trying as a script.\r\n"); message(console, "Hmm. Trying as a script.\r\n");
@ -298,6 +319,7 @@ static int run(const char *command, char *terminal, int get_enter)
return pid; return pid;
} }
#ifndef DEBUG_INIT #ifndef DEBUG_INIT
static void shutdown_system(void) static void shutdown_system(void)
{ {
@ -350,8 +372,8 @@ extern int init_main(int argc, char **argv)
int pid1 = 0; int pid1 = 0;
int pid2 = 0; int pid2 = 0;
struct stat statbuf; struct stat statbuf;
const char *init_commands = SHELL "-c exec " INITSCRIPT; const char *init_commands = SHELL " -c exec " INITSCRIPT;
const char *shell_commands = SHELL; const char *shell_commands = SHELL " -";
const char *tty0_commands = init_commands; const char *tty0_commands = init_commands;
const char *tty1_commands = shell_commands; const char *tty1_commands = shell_commands;
char *hello_msg_format = char *hello_msg_format =
@ -397,10 +419,11 @@ extern int init_main(int argc, char **argv)
/* Mount /proc */ /* Mount /proc */
if (mount("/proc", "/proc", "proc", 0, 0)) { if (mount("/proc", "/proc", "proc", 0, 0)) {
message(log, "Mounting /proc: failed!\n");
message(console, "Mounting /proc: failed!\r\n"); message(console, "Mounting /proc: failed!\r\n");
message(log, "Mounting /proc: failed!\n");
} else { } else {
message(console, "Mounting /proc: done.\r\n"); message(console, "Mounting /proc: done.\r\n");
message(log, "Mounting /proc: done.\n");
} }
/* Make sure there is enough memory to do something useful */ /* Make sure there is enough memory to do something useful */
@ -430,7 +453,7 @@ extern int init_main(int argc, char **argv)
!strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
run_rc = FALSE; run_rc = FALSE;
tty0_commands = shell_commands; tty0_commands = shell_commands;
tty1_commands = 0; tty1_commands = shell_commands;
} }
/* Make sure an init script exists before trying to run it */ /* Make sure an init script exists before trying to run it */
@ -447,13 +470,13 @@ extern int init_main(int argc, char **argv)
int status; int status;
if (pid1 == 0 && tty0_commands) { if (pid1 == 0 && tty0_commands) {
pid1 = run(tty0_commands, console, 1); pid1 = run(tty0_commands, first_terminal, 1);
} }
if (pid2 == 0 && tty1_commands) { if (pid2 == 0 && tty1_commands) {
pid2 = run(tty1_commands, second_terminal, 1); pid2 = run(tty1_commands, second_terminal, 1);
} }
wpid = wait(&status); wpid = wait(&status);
if (wpid > 0 && wpid != pid1) { if (wpid > 0 ) {
message(log, "pid %d exited, status=%x.\n", wpid, status); message(log, "pid %d exited, status=%x.\n", wpid, status);
} }
/* Don't respawn an init script if it exits */ /* Don't respawn an init script if it exits */