annotate toys/pending/init.c @ 986:69adf14d70e1

System V style init, submitted by Kyungwan Han.
author Rob Landley <rob@landley.net>
date Sun, 04 Aug 2013 00:31:27 -0500
parents
children a2f80613be36
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
986
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* init.c - init program.
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Harvind Singh <harvindsingh1981@gmail.com>
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 *
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 * No Standard
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 USE_INIT(NEWTOY(init, "", TOYFLAG_SBIN))
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
9
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 config INIT
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 bool "init"
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 default n
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 help
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 usage: init
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 init the system.
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 */
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 #include "toys.h"
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 #include<linux/vt.h>
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 #include<sys/reboot.h>
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
22
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 struct action_list_seed {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 struct action_list_seed *next;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 uint8_t action;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 char *terminal_name;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 char *command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 };
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 struct action_list_seed *action_list_pointer = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 int caught_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
32
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 //INITTAB action defination
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 #define SYSINIT 0x01
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 #define WAIT 0x02
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 #define ONCE 0x04
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 #define RESPAWN 0x08
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 #define ASKFIRST 0x10
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 #define CTRLALTDEL 0x20
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 #define SHUTDOWN 0x40
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 #define RESTART 0x80
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
42
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 static void initialize_console(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 int fd;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 char *p = (p = getenv("CONSOLE")) ? p : getenv("console");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 if (!p) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 fd = open("/dev/null", O_RDWR);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 if (fd >= 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 while(fd < 2) fd = dup(fd);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 while(fd > 2) close(fd--);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 fd = open(p, O_RDWR | O_NONBLOCK | O_NOCTTY);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 if (fd < 0) xprintf("Unable to open console %s\n",p);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 dup2(fd,0);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 dup2(fd,1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 dup2(fd,2);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 p = getenv("TERM");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 #ifdef VT_OPENQRY
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 int terminal_no;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 if (ioctl(0, VT_OPENQRY, &terminal_no)) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 if (!p || !strcmp(p,"linux")) putenv((char*)"TERM=vt102");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 } else
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 #endif
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 if (!p) putenv((char*)"TERM=linux");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
71
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 static void set_sane_term(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 struct termios terminal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 tcgetattr(0, &terminal);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 terminal.c_cc[VINTR] = 3;//ctrl-c
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 terminal.c_cc[VQUIT] = 28;/*ctrl-\*/
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 terminal.c_cc[VERASE] = 127;//ctrl-?
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 terminal.c_cc[VKILL] = 21;//ctrl-u
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 terminal.c_cc[VEOF] = 4;//ctrl-d
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 terminal.c_cc[VSTART] = 17;//ctrl-q
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 terminal.c_cc[VSTOP] = 19;//ctrl-s
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 terminal.c_cc[VSUSP] = 26;//ctrl-z
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
84
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 terminal.c_line = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 terminal.c_cflag = terminal.c_cflag&(CRTSCTS|PARODD|PARENB|CSTOPB|CSIZE|CBAUDEX|CBAUD);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 terminal.c_cflag = terminal.c_cflag|(CLOCAL|HUPCL|CREAD);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 terminal.c_iflag = IXON|IXOFF|ICRNL;//enable start/stop input and output control + map CR to NL on input
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 terminal.c_oflag = ONLCR|OPOST;//Map NL to CR-NL on output
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 terminal.c_lflag = IEXTEN|ECHOKE|ECHOCTL|ECHOK|ECHOE|ECHO|ICANON|ISIG;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 tcsetattr(0, TCSANOW, &terminal);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
93
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 static void set_enviornment(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 putenv((char*)"HOME=/");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 putenv((char*)"PATH=/sbin:/usr/sbin:/bin:/usr/bin");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 putenv((char*)"SHELL=/bin/sh");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 putenv((char*)"USER=root");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
101
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 static void add_new_action(uint8_t action,char *command,char *term)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 struct action_list_seed *x,**y;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 y = &action_list_pointer;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 x = *y;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 while(x) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 if (!(strcmp(x->command,command)) && !(strcmp(x->terminal_name,term))) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 *y = x->next;//remove from the list
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 while(*y) y = &(*y)->next;//traverse through list till end
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 x->next = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 y = &(x)->next;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 x = *y;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 //create a new node
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 if (!x) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 x = xzalloc(sizeof(*x));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 x->command = xstrdup(command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 x->terminal_name = xstrdup(term);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 x->action = action;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 *y = x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
126
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 static void inittab_parsing(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 int i, fd, line_number = 0, token_count = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 char *p, *q, *extracted_token, *tty_name = NULL, *command = NULL, *tmp;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 uint8_t action = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 char *act_name = "sysinit\0wait\0once\0respawn\0askfirst\0ctrlaltdel\0"
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 "shutdown\0restart\0";
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
134
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 fd = open("/etc/inittab", O_RDONLY);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 if (fd < 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 error_msg("Unable to open /etc/inittab. Using Default inittab");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 add_new_action(SYSINIT, "/etc/init.d/rcS", "");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 add_new_action(RESPAWN, "/sbin/getty -n -l /bin/sh -L 115200 tty1 vt100", "");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 while((q = p = get_line(fd))) { //read single line from /etc/inittab
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 char *x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 if((x = strchr(p, '#'))) *x = '\0';
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 line_number++;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 token_count = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
146 action = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 while((extracted_token = strsep(&p,":"))) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 token_count++;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
149 switch (token_count) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 case 1:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 if (*extracted_token) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 if(!strncmp(extracted_token, "/dev/", 5))
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 tty_name = xmsprintf("%s",extracted_token);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 else tty_name = xmsprintf("/dev/%s",extracted_token);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 } else tty_name = xstrdup("");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
156 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 case 2:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
159 case 3:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 for(tmp = act_name, i = 0; *tmp; i++, tmp += strlen(tmp) +1) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 if ( !strcmp(tmp, extracted_token)) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 action = 1 << i;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 if (!*tmp) error_msg("Invalid action at line number %d ---- ignoring",line_number);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 case 4:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 command = xstrdup(extracted_token);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 default:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 error_msg("Bad inittab entry at line %d", line_number);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 } //while token
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
176 if (q) free(q);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 if (token_count != 4) continue;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 if (action) add_new_action(action, command, tty_name);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
179 free(tty_name);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 free(command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
181 }//while line
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
182 close(fd);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
184 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
185
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 static void run_command(char *command)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 char *final_command[128];
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 int hyphen = (command[0]=='-');
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
190
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 command = command + hyphen;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 if (!strpbrk(command, "?<>'\";[]{}\\|=()*&^$!`~")) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 char *next_command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 char *extracted_command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
195 int x = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 next_command = strncpy(toybuf, command - hyphen, sizeof(toybuf));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
197 next_command[sizeof(toybuf) - 1] = toybuf[sizeof(toybuf) - 1 ] = '\0';
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 command = next_command + hyphen;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 while((extracted_command = strsep(&next_command," \t"))) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 if (*extracted_command) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 final_command[x] = extracted_command;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
202 x++;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
205 final_command[x] = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 snprintf(toybuf, sizeof(toybuf), "exec %s", command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 command = "-/bin/sh"+1;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
209 final_command[0] = (char*)("-/bin/sh"+!hyphen);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 final_command[1] = (char*)"-c";
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
211 final_command[2] = toybuf;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 final_command[3] = NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 if (hyphen) ioctl(0, TIOCSCTTY, 0);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
215 execvp(command, final_command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 error_msg("unable to run %s",command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
217 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
218
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 //runs all same type of actions
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
220 static pid_t final_run(struct action_list_seed *x)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
223 int fd;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 sigset_t signal_set;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
225
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 sigfillset(&signal_set);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 sigprocmask(SIG_BLOCK, &signal_set, NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 if (x->action & ASKFIRST) pid = fork();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 else pid = vfork();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
230 if (pid > 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
231 //parent process or error
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 //unblock the signals
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 sigfillset(&signal_set);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
234 sigprocmask(SIG_UNBLOCK, &signal_set, NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
235 return pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 } else if (pid < 0) perror_exit("fork fail");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 //new born child process
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
238 sigset_t signal_set_c;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 sigfillset(&signal_set_c);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 sigprocmask(SIG_UNBLOCK, &signal_set_c, NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
241 setsid();//new session
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
242 if (x->terminal_name[0]) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 close(0);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
244 fd = open(x->terminal_name,(O_RDWR|O_NONBLOCK),0600);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
245 if (fd != 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
246 error_msg("Unable to open %s,%s\n",x->terminal_name,strerror(errno));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
247 _exit(EXIT_FAILURE);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
248 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
249 dup2(0,1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
250 dup2(0,2);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
252 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
253 set_sane_term();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
254 run_command(x->command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
255 _exit(-1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
257
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
258 static struct action_list_seed* mark_as_terminated_process(pid_t pid)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
259 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
260 struct action_list_seed *x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
261 if (pid > 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 for (x = action_list_pointer; x; x = x->next) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
263 if (x->pid == pid) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
264 x->pid = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 return x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
266 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
267 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
268 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
269 return NULL;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
270 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
271
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
272 static void waitforpid(pid_t pid)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
273 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
274 if (pid <= 0) return;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
275 for(;;) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
276 pid_t y = wait(NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
277 mark_as_terminated_process(y);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 if (kill(y,0)) break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
279 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
280 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
281 static void run_action_from_list(int action)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
282 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
283 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
284 struct action_list_seed *x = action_list_pointer;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
285 for (; x; x = x->next) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
286 if (!(x->action & action)) continue;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
287 if (x->action & (SHUTDOWN|ONCE|SYSINIT|CTRLALTDEL|WAIT)) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
288 pid = final_run(x);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
289 if (x->action & (SHUTDOWN|SYSINIT|CTRLALTDEL|WAIT)) waitforpid(pid);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
290 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
291 if (x->action & (ASKFIRST|RESPAWN))
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
292 if (!(x->pid)) x->pid = final_run(x);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
293 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
294 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
295
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
296 static void set_defualt(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
297 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
298 sigset_t signal_set_c;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
299
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
300 signal(SIGUSR1,SIG_DFL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
301 signal(SIGUSR2,SIG_DFL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
302 signal(SIGTERM,SIG_DFL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
303 signal(SIGQUIT,SIG_DFL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
304 signal(SIGINT,SIG_DFL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
305 signal(SIGHUP,SIG_DFL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
306 signal(SIGTSTP,SIG_DFL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
307 signal(SIGSTOP,SIG_DFL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
308 sigfillset(&signal_set_c);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
309 sigprocmask(SIG_UNBLOCK,&signal_set_c, NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
310 run_action_from_list(SHUTDOWN);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 error_msg("The system is going down NOW!");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
312 kill(-1, SIGTERM);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
313 error_msg("Sent SIGTERM to all processes");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
314 sync();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
316 kill(-1,SIGKILL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
317 sync();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
318 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
319
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
320 static void halt_poweroff_reboot_handler(int sig_no)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
321 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
322 unsigned int reboot_magic_no = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
323 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
324 set_defualt();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
325 switch (sig_no) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
326 case SIGUSR1:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
327 error_msg("Requesting system halt");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 reboot_magic_no=RB_HALT_SYSTEM;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
329 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 case SIGUSR2:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
331 error_msg("Requesting system poweroff");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
332 reboot_magic_no=RB_POWER_OFF;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
333 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
334 case SIGTERM:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
335 error_msg("Requesting system reboot");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 reboot_magic_no=RB_AUTOBOOT;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 default:
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
339 break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
340 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
342 pid = vfork();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 if (pid == 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
344 reboot(reboot_magic_no);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
345 _exit(EXIT_SUCCESS);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
346 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
347 while(1) sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
348 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
349 static void restart_init_handler(int sig_no)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
350 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
351 struct action_list_seed *x;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
352 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
353 int fd;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
354 for (x = action_list_pointer; x; x = x->next) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
355 if (!(x->action & RESTART)) continue;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
356
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
357 set_defualt();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
358 if (x->terminal_name[0]) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
359 close(0);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
360 fd = open(x->terminal_name,(O_RDWR|O_NONBLOCK),0600);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
361 if (fd != 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
362 error_msg("Unable to open %s,%s\n",x->terminal_name,strerror(errno));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
363 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
364 pid = vfork();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
365 if (pid == 0) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
366 reboot(RB_HALT_SYSTEM);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
367 _exit(EXIT_SUCCESS);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
368 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
369 while(1) sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
370 } else {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
371 dup2(0,1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
372 dup2(0,2);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
373 set_sane_term();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
374 run_command(x->command);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
375 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
376 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
377 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
378 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
379
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
380 static void catch_signal(int sig_no)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
381 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
382 caught_signal = sig_no;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
383 error_msg("signal seen");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
384 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
385
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
386 static void pause_handler(int sig_no)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
387 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
388 int signal_backup,errno_backup;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
389 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
390 errno_backup = errno;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
391 signal_backup = caught_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
392 signal(SIGCONT, catch_signal);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
393 while(1) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
394 if (caught_signal == SIGCONT) break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
395 do
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
396 pid = waitpid(-1,NULL,WNOHANG);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
397 while((pid==-1) && (errno=EINTR));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
398 mark_as_terminated_process(pid);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
399 sleep(1);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
400 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
401 signal(SIGCONT,SIG_DFL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
402 errno = errno_backup;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
403 caught_signal = signal_backup;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
404 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
405
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
406 static void assign_signal_handler(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
407 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
408 struct sigaction sig_act;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
409 signal(SIGUSR1, halt_poweroff_reboot_handler);//halt
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
410 signal(SIGUSR2, halt_poweroff_reboot_handler);//poweroff
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
411 signal(SIGTERM, halt_poweroff_reboot_handler);//reboot
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
412 signal(SIGQUIT, restart_init_handler);//restart init
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
413 memset(&sig_act, 0, sizeof(sig_act));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
414 sigfillset(&sig_act.sa_mask);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
415 sigdelset(&sig_act.sa_mask, SIGCONT);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
416 sig_act.sa_handler = pause_handler;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
417 sigaction(SIGTSTP, &sig_act, NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
418
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
419 memset(&sig_act, 0, sizeof(sig_act));
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
420 sig_act.sa_handler = catch_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
421 sigaction(SIGINT, &sig_act, NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
422 sigaction(SIGHUP, &sig_act, NULL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
423 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
424
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
425 static int check_if_pending_signals(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
426 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
427 int signal_caught = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
428 while(1) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
429 int sig = caught_signal;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
430 if (!sig) return signal_caught;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
431 caught_signal = 0;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
432 signal_caught = 1;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
433 if (sig == SIGINT) run_action_from_list(CTRLALTDEL);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
434 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
435 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
436 void init_main(void)
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
437 {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
438 if (getpid() != 1) error_exit("Already running");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
439 xprintf("Started init\n");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
440 initialize_console();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
441 set_sane_term();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
442
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
443 if (chdir("/")) perror_exit("Can't cd to /");
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
444 setsid();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
445
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
446 set_enviornment();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
447 inittab_parsing();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
448 assign_signal_handler();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
449 run_action_from_list(SYSINIT);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
450 check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
451 run_action_from_list(WAIT);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
452 check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
453 run_action_from_list(ONCE);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
454 while(1) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
455 int suspected_WNOHANG;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
456 suspected_WNOHANG = check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
457 run_action_from_list(RESPAWN | ASKFIRST);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
458 suspected_WNOHANG = suspected_WNOHANG|check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
459 sleep(1);//let cpu breath
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
460 suspected_WNOHANG = suspected_WNOHANG|check_if_pending_signals();
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
461 if (suspected_WNOHANG) suspected_WNOHANG=WNOHANG;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
462 while(1) {
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
463 pid_t pid;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
464 pid = waitpid(-1, NULL, suspected_WNOHANG);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
465 if (pid <= 0) break;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
466 mark_as_terminated_process(pid);
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
467 suspected_WNOHANG = WNOHANG;
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
468 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
469 }
69adf14d70e1 System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
470 }