Mercurial > hg > toybox
changeset 1185:2a4f1dc494d0 draft
-Eradicate (char*) casts for strings.
-Don't panic on failure to write messages.
-Don't panic on failure to fork; sleep an extra second instead.
-s/defualt/default/g
-Inline a couple functions called once.
(Don't inline inittab_parsing because it is too large).
author | Isaac Dunham <ibid.ag@gmail.com> |
---|---|
date | Wed, 22 Jan 2014 23:29:50 -0600 |
parents | 48334d7a2b55 |
children | 8b62561ce52a |
files | toys/pending/init.c |
diffstat | 1 files changed, 35 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/toys/pending/init.c Mon Jan 20 17:26:50 2014 -0600 +++ b/toys/pending/init.c Wed Jan 22 23:29:50 2014 -0600 @@ -52,7 +52,7 @@ } } else { fd = open(p, O_RDWR | O_NONBLOCK | O_NOCTTY); - if (fd < 0) xprintf("Unable to open console %s\n",p); + if (fd < 0) printf("Unable to open console %s\n",p); else { dup2(fd,0); dup2(fd,1); @@ -64,10 +64,10 @@ #ifdef VT_OPENQRY int terminal_no; if (ioctl(0, VT_OPENQRY, &terminal_no)) { - if (!p || !strcmp(p,"linux")) putenv((char*)"TERM=vt102"); + if (!p || !strcmp(p,"linux")) putenv("TERM=vt102"); } else #endif - if (!p) putenv((char*)"TERM=linux"); + if (!p) putenv("TERM=linux"); } static void set_sane_term(void) @@ -93,14 +93,6 @@ tcsetattr(0, TCSANOW, &terminal); } -static void set_enviornment(void) -{ - putenv((char*)"HOME=/"); - putenv((char*)"PATH=/sbin:/usr/sbin:/bin:/usr/bin"); - putenv((char*)"SHELL=/bin/sh"); - putenv((char*)"USER=root"); -} - static void add_new_action(uint8_t action,char *command,char *term) { struct action_list_seed *x,**y; @@ -214,8 +206,8 @@ } else { snprintf(toybuf, sizeof(toybuf), "exec %s", command); command = "-/bin/sh"+1; - final_command[0] = (char*)("-/bin/sh"+!hyphen); - final_command[1] = (char*)"-c"; + final_command[0] = ("-/bin/sh"+!hyphen); + final_command[1] = "-c"; final_command[2] = toybuf; final_command[3] = NULL; } @@ -243,7 +235,11 @@ sigprocmask(SIG_UNBLOCK, &signal_set, NULL); return pid; - } else if (pid < 0) perror_exit("fork fail"); + } else if (pid < 0) { + perror_msg("fork fail"); + sleep(1); + return 0; + } //new born child process sigset_t signal_set_c; @@ -302,14 +298,15 @@ if (!(x->action & action)) continue; if (x->action & (SHUTDOWN|ONCE|SYSINIT|CTRLALTDEL|WAIT)) { pid = final_run(x); + if (!pid) return; if (x->action & (SHUTDOWN|SYSINIT|CTRLALTDEL|WAIT)) waitforpid(pid); } if (x->action & (ASKFIRST|RESPAWN)) if (!(x->pid)) x->pid = final_run(x); } -} + } -static void set_defualt(void) +static void set_default(void) { sigset_t signal_set_c; @@ -339,7 +336,7 @@ unsigned int reboot_magic_no = 0; pid_t pid; - set_defualt(); + set_default(); switch (sig_no) { case SIGUSR1: @@ -377,7 +374,7 @@ for (x = action_list_pointer; x; x = x->next) { if (!(x->action & RESTART)) continue; - set_defualt(); + set_default(); if (x->terminal_name[0]) { close(0); @@ -431,26 +428,6 @@ caught_signal = signal_backup; } -static void assign_signal_handler(void) -{ - struct sigaction sig_act; - - signal(SIGUSR1, halt_poweroff_reboot_handler); //halt - signal(SIGUSR2, halt_poweroff_reboot_handler); //poweroff - signal(SIGTERM, halt_poweroff_reboot_handler); //reboot - signal(SIGQUIT, restart_init_handler); //restart init - memset(&sig_act, 0, sizeof(sig_act)); - sigfillset(&sig_act.sa_mask); - sigdelset(&sig_act.sa_mask, SIGCONT); - sig_act.sa_handler = pause_handler; - sigaction(SIGTSTP, &sig_act, NULL); - - memset(&sig_act, 0, sizeof(sig_act)); - sig_act.sa_handler = catch_signal; - sigaction(SIGINT, &sig_act, NULL); - sigaction(SIGHUP, &sig_act, NULL); -} - static int check_if_pending_signals(void) { int signal_caught = 0; @@ -466,17 +443,34 @@ void init_main(void) { + struct sigaction sig_act; + if (getpid() != 1) error_exit("Already running"); - xprintf("Started init\n"); + printf("Started init\n"); initialize_console(); set_sane_term(); if (chdir("/")) perror_exit("Can't cd to /"); setsid(); - set_enviornment(); + putenv("HOME=/"); + putenv("PATH=/sbin:/usr/sbin:/bin:/usr/bin"); + putenv("SHELL=/bin/sh"); + putenv("USER=root"); inittab_parsing(); - assign_signal_handler(); + signal(SIGUSR1, halt_poweroff_reboot_handler);//halt + signal(SIGUSR2, halt_poweroff_reboot_handler);//poweroff + signal(SIGTERM, halt_poweroff_reboot_handler);//reboot + signal(SIGQUIT, restart_init_handler);//restart init + memset(&sig_act, 0, sizeof(sig_act)); + sigfillset(&sig_act.sa_mask); + sigdelset(&sig_act.sa_mask, SIGCONT); + sig_act.sa_handler = pause_handler; + sigaction(SIGTSTP, &sig_act, NULL); + memset(&sig_act, 0, sizeof(sig_act)); + sig_act.sa_handler = catch_signal; + sigaction(SIGINT, &sig_act, NULL); + sigaction(SIGHUP, &sig_act, NULL); run_action_from_list(SYSINIT); check_if_pending_signals(); run_action_from_list(WAIT);