First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
diff --git a/toys/pending/init.c b/toys/pending/init.c
index 8199e74..ca3c6cc 100644
--- a/toys/pending/init.c
+++ b/toys/pending/init.c
@@ -13,7 +13,10 @@
help
usage: init
- init the system.
+ System V style init.
+
+ First program to run (as PID 1) when the system comes up, reading
+ /etc/inittab to determine actions.
*/
#include "toys.h"
@@ -41,8 +44,9 @@
static void initialize_console(void)
{
int fd;
- char *p = (p = getenv("CONSOLE")) ? p : getenv("console");
+ char *p = getenv("CONSOLE");
+ if (!p) p = getenv("console");
if (!p) {
fd = open("/dev/null", O_RDWR);
if (fd >= 0) {
@@ -62,11 +66,11 @@
if (!getenv("TERM")) putenv("TERM=linux");
}
-static void set_sane_term(void)
+static void reset_term(int fd)
{
struct termios terminal;
- tcgetattr(0, &terminal);
+ tcgetattr(fd, &terminal);
terminal.c_cc[VINTR] = 3; //ctrl-c
terminal.c_cc[VQUIT] = 28; /*ctrl-\*/
terminal.c_cc[VERASE] = 127; //ctrl-?
@@ -77,12 +81,16 @@
terminal.c_cc[VSUSP] = 26; //ctrl-z
terminal.c_line = 0;
- terminal.c_cflag = terminal.c_cflag&(CRTSCTS|PARODD|PARENB|CSTOPB|CSIZE|CBAUDEX|CBAUD);
- terminal.c_cflag = terminal.c_cflag|(CLOCAL|HUPCL|CREAD);
- terminal.c_iflag = IXON|IXOFF|ICRNL;//enable start/stop input and output control + map CR to NL on input
- terminal.c_oflag = ONLCR|OPOST;//Map NL to CR-NL on output
+ terminal.c_cflag &= CRTSCTS|PARODD|PARENB|CSTOPB|CSIZE|CBAUDEX|CBAUD;
+ terminal.c_cflag |= CLOCAL|HUPCL|CREAD;
+
+ //enable start/stop input and output control + map CR to NL on input
+ terminal.c_iflag = IXON|IXOFF|ICRNL;
+
+ //Map NL to CR-NL on output
+ terminal.c_oflag = ONLCR|OPOST;
terminal.c_lflag = IEXTEN|ECHOKE|ECHOCTL|ECHOK|ECHOE|ECHO|ICANON|ISIG;
- tcsetattr(0, TCSANOW, &terminal);
+ tcsetattr(fd, TCSANOW, &terminal);
}
static void add_new_action(uint8_t action,char *command,char *term)
@@ -250,7 +258,7 @@
dup2(0, 2);
}
}
- set_sane_term();
+ reset_term(0);
run_command(x->command);
_exit(-1);
}
@@ -281,6 +289,7 @@
if (kill(y, 0)) break;
}
}
+
static void run_action_from_list(int action)
{
pid_t pid;
@@ -302,14 +311,7 @@
{
sigset_t signal_set_c;
- signal(SIGUSR1,SIG_DFL);
- signal(SIGUSR2,SIG_DFL);
- signal(SIGTERM,SIG_DFL);
- signal(SIGQUIT,SIG_DFL);
- signal(SIGINT,SIG_DFL);
- signal(SIGHUP,SIG_DFL);
- signal(SIGTSTP,SIG_DFL);
- signal(SIGSTOP,SIG_DFL);
+ sigatexit(SIG_DFL);
sigfillset(&signal_set_c);
sigprocmask(SIG_UNBLOCK,&signal_set_c, NULL);
@@ -357,6 +359,7 @@
while(1) sleep(1);
}
+
static void restart_init_handler(int sig_no)
{
struct action_list_seed *x;
@@ -386,7 +389,7 @@
} else {
dup2(0, 1);
dup2(0, 2);
- set_sane_term();
+ reset_term(0);
run_command(x->command);
}
}
@@ -440,7 +443,7 @@
if (getpid() != 1) error_exit("Already running");
printf("Started init\n");
initialize_console();
- set_sane_term();
+ reset_term(0);
if (chdir("/")) perror_exit("Can't cd to /");
setsid();
@@ -449,6 +452,7 @@
putenv("PATH=/sbin:/usr/sbin:/bin:/usr/bin");
putenv("SHELL=/bin/sh");
putenv("USER=root");
+
inittab_parsing();
signal(SIGUSR1, halt_poweroff_reboot_handler);//halt
signal(SIGUSR2, halt_poweroff_reboot_handler);//poweroff