Mercurial > hg > toybox
annotate toys/pending/init.c @ 1181:a2f80613be36 draft
Whitespace changes, and collate a couple declarations/first assignment.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 05 Jan 2014 14:43:27 -0600 |
parents | 69adf14d70e1 |
children | 0752b2d58909 |
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" |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
20 #include <linux/vt.h> |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
21 #include <sys/reboot.h> |
986
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; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
29 } *action_list_pointer = NULL; |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 int caught_signal; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 //INITTAB action defination |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 #define SYSINIT 0x01 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 #define WAIT 0x02 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 #define ONCE 0x04 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 #define RESPAWN 0x08 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 #define ASKFIRST 0x10 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 #define CTRLALTDEL 0x20 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 #define SHUTDOWN 0x40 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 #define RESTART 0x80 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 static void initialize_console(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 int fd; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 char *p = (p = getenv("CONSOLE")) ? p : getenv("console"); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
46 |
986
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) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
50 while (fd < 2) fd = dup(fd); |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
51 while (fd > 2) close(fd--); |
986
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 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
62 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 p = getenv("TERM"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 #ifdef VT_OPENQRY |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 int terminal_no; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 if (ioctl(0, VT_OPENQRY, &terminal_no)) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 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
|
68 } else |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 #endif |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
70 if (!p) putenv((char*)"TERM=linux"); |
986
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 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 static void set_sane_term(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 struct termios terminal; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
76 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 tcgetattr(0, &terminal); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
78 terminal.c_cc[VINTR] = 3; //ctrl-c |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
79 terminal.c_cc[VQUIT] = 28; /*ctrl-\*/ |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
80 terminal.c_cc[VERASE] = 127; //ctrl-? |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
81 terminal.c_cc[VKILL] = 21; //ctrl-u |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
82 terminal.c_cc[VEOF] = 4; //ctrl-d |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
83 terminal.c_cc[VSTART] = 17; //ctrl-q |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
84 terminal.c_cc[VSTOP] = 19; //ctrl-s |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
85 terminal.c_cc[VSUSP] = 26; //ctrl-z |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 terminal.c_line = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 tcsetattr(0, TCSANOW, &terminal); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 } |
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 static void set_enviornment(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 putenv((char*)"HOME=/"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
99 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
|
100 putenv((char*)"SHELL=/bin/sh"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 putenv((char*)"USER=root"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 } |
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 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
|
105 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
106 struct action_list_seed *x,**y; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
107 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 y = &action_list_pointer; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 x = *y; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
110 while (x) { |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
111 if (!(strcmp(x->command, command)) && !(strcmp(x->terminal_name, term))) { |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
112 *y = x->next; //remove from the list |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
113 while(*y) y = &(*y)->next; //traverse through list till end |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 x->next = NULL; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 break; |
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 y = &(x)->next; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 x = *y; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
120 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 //create a new node |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
122 if (!x) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
123 x = xzalloc(sizeof(*x)); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 x->command = xstrdup(command); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 x->terminal_name = xstrdup(term); |
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 x->action = action; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 *y = x; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
131 static void inittab_parsing(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 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
|
134 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
|
135 uint8_t action = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
136 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
|
137 "shutdown\0restart\0"; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
138 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
139 fd = open("/etc/inittab", O_RDONLY); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
140 if (fd < 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 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
|
142 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
|
143 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
|
144 } else { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 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
|
146 char *x; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
147 |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
148 if ((x = strchr(p, '#'))) *x = '\0'; |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 line_number++; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 token_count = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 action = 0; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
152 while ((extracted_token = strsep(&p,":"))) { |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 token_count++; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 switch (token_count) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
155 case 1: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
156 if (*extracted_token) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
157 if (!strncmp(extracted_token, "/dev/", 5)) |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 tty_name = xmsprintf("%s",extracted_token); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
159 else tty_name = xmsprintf("/dev/%s",extracted_token); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 } else tty_name = xstrdup(""); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 case 2: |
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 case 3: |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
165 for (tmp = act_name, i = 0; *tmp; i++, tmp += strlen(tmp) +1) { |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
166 if (!strcmp(tmp, extracted_token)) { |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 action = 1 << i; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
168 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
169 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
170 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
171 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
|
172 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
173 case 4: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
174 command = xstrdup(extracted_token); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
175 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
176 default: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
177 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
|
178 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
179 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
180 } //while token |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
181 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
182 if (q) free(q); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
183 if (token_count != 4) continue; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
184 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
|
185 free(tty_name); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
186 free(command); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
187 } //while line |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
188 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
189 close(fd); |
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 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
192 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
193 static void run_command(char *command) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
194 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
195 char *final_command[128]; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
196 int hyphen = (command[0]=='-'); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
197 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
198 command = command + hyphen; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
199 if (!strpbrk(command, "?<>'\";[]{}\\|=()*&^$!`~")) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
200 char *next_command; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
201 char *extracted_command; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
202 int x = 0; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
203 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
204 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
|
205 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
|
206 command = next_command + hyphen; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
207 while ((extracted_command = strsep(&next_command," \t"))) { |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
208 if (*extracted_command) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
209 final_command[x] = extracted_command; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
210 x++; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
211 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
212 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
213 final_command[x] = NULL; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
214 } else { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
215 snprintf(toybuf, sizeof(toybuf), "exec %s", command); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
216 command = "-/bin/sh"+1; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
217 final_command[0] = (char*)("-/bin/sh"+!hyphen); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
218 final_command[1] = (char*)"-c"; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
219 final_command[2] = toybuf; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
220 final_command[3] = NULL; |
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 if (hyphen) ioctl(0, TIOCSCTTY, 0); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
223 execvp(command, final_command); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
224 error_msg("unable to run %s",command); |
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 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
227 //runs all same type of actions |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
228 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
|
229 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
230 pid_t pid; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
231 int fd; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
232 sigset_t signal_set; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
233 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
234 sigfillset(&signal_set); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
235 sigprocmask(SIG_BLOCK, &signal_set, NULL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
236 if (x->action & ASKFIRST) pid = fork(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
237 else pid = vfork(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
238 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
239 if (pid > 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
240 //parent process or error |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
241 //unblock the signals |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
242 sigfillset(&signal_set); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
243 sigprocmask(SIG_UNBLOCK, &signal_set, NULL); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
244 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
245 return pid; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
246 } else if (pid < 0) perror_exit("fork fail"); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
247 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
248 //new born child process |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
249 sigset_t signal_set_c; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
250 sigfillset(&signal_set_c); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
251 sigprocmask(SIG_UNBLOCK, &signal_set_c, NULL); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
252 setsid(); //new session |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
253 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
254 if (x->terminal_name[0]) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
255 close(0); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
256 fd = open(x->terminal_name, (O_RDWR|O_NONBLOCK),0600); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
257 if (fd != 0) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
258 error_msg("Unable to open %s,%s\n", x->terminal_name, strerror(errno)); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
259 _exit(EXIT_FAILURE); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
260 } else { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
261 dup2(0, 1); |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
262 dup2(0, 2); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
263 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
264 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
265 set_sane_term(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
266 run_command(x->command); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
267 _exit(-1); |
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 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
270 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
|
271 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
272 struct action_list_seed *x; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
273 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
274 if (pid > 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
275 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
|
276 if (x->pid == pid) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
277 x->pid = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
278 return x; |
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 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
282 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
283 return NULL; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
284 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
285 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
286 static void waitforpid(pid_t pid) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
287 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
288 if (pid <= 0) return; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
289 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
290 for(;;) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
291 pid_t y = wait(NULL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
292 mark_as_terminated_process(y); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
293 if (kill(y, 0)) break; |
986
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 run_action_from_list(int action) |
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 pid_t pid; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
299 struct action_list_seed *x = action_list_pointer; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
300 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
301 for (; x; x = x->next) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
302 if (!(x->action & action)) continue; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
303 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
|
304 pid = final_run(x); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
305 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
|
306 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
307 if (x->action & (ASKFIRST|RESPAWN)) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
308 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
|
309 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
310 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
311 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
312 static void set_defualt(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
313 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
314 sigset_t signal_set_c; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
315 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
316 signal(SIGUSR1,SIG_DFL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
317 signal(SIGUSR2,SIG_DFL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
318 signal(SIGTERM,SIG_DFL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
319 signal(SIGQUIT,SIG_DFL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
320 signal(SIGINT,SIG_DFL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
321 signal(SIGHUP,SIG_DFL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
322 signal(SIGTSTP,SIG_DFL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
323 signal(SIGSTOP,SIG_DFL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
324 sigfillset(&signal_set_c); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
325 sigprocmask(SIG_UNBLOCK,&signal_set_c, NULL); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
326 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
327 run_action_from_list(SHUTDOWN); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
328 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
|
329 kill(-1, SIGTERM); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
330 error_msg("Sent SIGTERM to all processes"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
331 sync(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
332 sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
333 kill(-1,SIGKILL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
334 sync(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
335 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
336 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
337 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
|
338 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
339 unsigned int reboot_magic_no = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
340 pid_t pid; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
341 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
342 set_defualt(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
343 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
344 switch (sig_no) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
345 case SIGUSR1: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
346 error_msg("Requesting system halt"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
347 reboot_magic_no=RB_HALT_SYSTEM; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
348 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
349 case SIGUSR2: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
350 error_msg("Requesting system poweroff"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
351 reboot_magic_no=RB_POWER_OFF; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
352 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
353 case SIGTERM: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
354 error_msg("Requesting system reboot"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
355 reboot_magic_no=RB_AUTOBOOT; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
356 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
357 default: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
358 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
359 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
360 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
361 sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
362 pid = vfork(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
363 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
364 if (pid == 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
365 reboot(reboot_magic_no); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
366 _exit(EXIT_SUCCESS); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
367 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
368 |
986
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 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
371 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
|
372 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
373 struct action_list_seed *x; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
374 pid_t pid; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
375 int fd; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
376 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
377 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
|
378 if (!(x->action & RESTART)) continue; |
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 set_defualt(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
381 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
382 if (x->terminal_name[0]) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
383 close(0); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
384 fd = open(x->terminal_name, (O_RDWR|O_NONBLOCK),0600); |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
385 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
386 if (fd != 0) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
387 error_msg("Unable to open %s,%s\n", x->terminal_name, strerror(errno)); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
388 sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
389 pid = vfork(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
390 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
391 if (pid == 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
392 reboot(RB_HALT_SYSTEM); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
393 _exit(EXIT_SUCCESS); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
394 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
395 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
396 while(1) sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
397 } else { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
398 dup2(0, 1); |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
399 dup2(0, 2); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
400 set_sane_term(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
401 run_command(x->command); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
402 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
403 } |
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 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
407 static void catch_signal(int sig_no) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
408 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
409 caught_signal = sig_no; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
410 error_msg("signal seen"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
411 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
412 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
413 static void pause_handler(int sig_no) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
414 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
415 int signal_backup,errno_backup; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
416 pid_t pid; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
417 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
418 errno_backup = errno; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
419 signal_backup = caught_signal; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
420 signal(SIGCONT, catch_signal); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
421 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
422 while(1) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
423 if (caught_signal == SIGCONT) break; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
424 do pid = waitpid(-1,NULL,WNOHANG); while((pid==-1) && (errno=EINTR)); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
425 mark_as_terminated_process(pid); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
426 sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
427 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
428 |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
429 signal(SIGCONT, SIG_DFL); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
430 errno = errno_backup; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
431 caught_signal = signal_backup; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
432 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
433 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
434 static void assign_signal_handler(void) |
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 struct sigaction sig_act; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
437 |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
438 signal(SIGUSR1, halt_poweroff_reboot_handler); //halt |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
439 signal(SIGUSR2, halt_poweroff_reboot_handler); //poweroff |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
440 signal(SIGTERM, halt_poweroff_reboot_handler); //reboot |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
441 signal(SIGQUIT, restart_init_handler); //restart init |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
442 memset(&sig_act, 0, sizeof(sig_act)); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
443 sigfillset(&sig_act.sa_mask); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
444 sigdelset(&sig_act.sa_mask, SIGCONT); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
445 sig_act.sa_handler = pause_handler; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
446 sigaction(SIGTSTP, &sig_act, NULL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
447 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
448 memset(&sig_act, 0, sizeof(sig_act)); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
449 sig_act.sa_handler = catch_signal; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
450 sigaction(SIGINT, &sig_act, NULL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
451 sigaction(SIGHUP, &sig_act, NULL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
452 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
453 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
454 static int check_if_pending_signals(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
455 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
456 int signal_caught = 0; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
457 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
458 while(1) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
459 int sig = caught_signal; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
460 if (!sig) return signal_caught; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
461 caught_signal = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
462 signal_caught = 1; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
463 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
|
464 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
465 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
466 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
467 void init_main(void) |
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 if (getpid() != 1) error_exit("Already running"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
470 xprintf("Started init\n"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
471 initialize_console(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
472 set_sane_term(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
473 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
474 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
|
475 setsid(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
476 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
477 set_enviornment(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
478 inittab_parsing(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
479 assign_signal_handler(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
480 run_action_from_list(SYSINIT); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
481 check_if_pending_signals(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
482 run_action_from_list(WAIT); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
483 check_if_pending_signals(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
484 run_action_from_list(ONCE); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
485 while (1) { |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
486 int suspected_WNOHANG = check_if_pending_signals(); |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
487 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
488 run_action_from_list(RESPAWN | ASKFIRST); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
489 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
|
490 sleep(1);//let cpu breath |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
491 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
|
492 if (suspected_WNOHANG) suspected_WNOHANG=WNOHANG; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
493 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
494 while(1) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
495 pid_t pid = waitpid(-1, NULL, suspected_WNOHANG); |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
496 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
497 if (pid <= 0) break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
498 mark_as_terminated_process(pid); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
499 suspected_WNOHANG = WNOHANG; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
500 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
501 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
502 } |