Mercurial > hg > toybox
annotate toys/pending/init.c @ 1363:e65f9a9ba62d draft
Cleanup pass on mkpasswd.c
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 25 Jun 2014 22:54:59 -0500 |
parents | c25ee9918e65 |
children | c0c91437138b |
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 |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
16 System V style init. |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
17 |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
18 First program to run (as PID 1) when the system comes up, reading |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
19 /etc/inittab to determine actions. |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 */ |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 #include "toys.h" |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
23 #include <sys/reboot.h> |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 struct action_list_seed { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 struct action_list_seed *next; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 pid_t pid; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 uint8_t action; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 char *terminal_name; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 char *command; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
31 } *action_list_pointer = NULL; |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 int caught_signal; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 //INITTAB action defination |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 #define SYSINIT 0x01 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 #define WAIT 0x02 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 #define ONCE 0x04 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 #define RESPAWN 0x08 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 #define ASKFIRST 0x10 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 #define CTRLALTDEL 0x20 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 #define SHUTDOWN 0x40 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 #define RESTART 0x80 |
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 static void initialize_console(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 int fd; |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
47 char *p = getenv("CONSOLE"); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
48 |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
49 if (!p) p = getenv("console"); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 if (!p) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 fd = open("/dev/null", O_RDWR); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 if (fd >= 0) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
53 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
|
54 while (fd > 2) close(fd--); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 } |
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 fd = open(p, O_RDWR | O_NONBLOCK | O_NOCTTY); |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
58 if (fd < 0) printf("Unable to open console %s\n",p); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 else { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 dup2(fd,0); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 dup2(fd,1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 dup2(fd,2); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
65 |
1187
18cc63376e66
init: don't use VT_OPENQRY.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1185
diff
changeset
|
66 if (!getenv("TERM")) putenv("TERM=linux"); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
69 static void reset_term(int fd) |
986
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 struct termios terminal; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
72 |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
73 tcgetattr(fd, &terminal); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 terminal.c_line = 0; |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
84 terminal.c_cflag &= CRTSCTS|PARODD|PARENB|CSTOPB|CSIZE|CBAUDEX|CBAUD; |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
85 terminal.c_cflag |= CLOCAL|HUPCL|CREAD; |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
86 |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
87 //enable start/stop input and output control + map CR to NL on input |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
88 terminal.c_iflag = IXON|IXOFF|ICRNL; |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
89 |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
90 //Map NL to CR-NL on output |
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
91 terminal.c_oflag = ONLCR|OPOST; |
986
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; |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
93 tcsetattr(fd, TCSANOW, &terminal); |
986
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 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
|
97 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 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
|
99 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 y = &action_list_pointer; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 x = *y; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
102 while (x) { |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
103 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
|
104 *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
|
105 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
|
106 x->next = NULL; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
107 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 y = &(x)->next; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
110 x = *y; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
112 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 //create a new node |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 if (!x) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 x = xzalloc(sizeof(*x)); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 x->command = xstrdup(command); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 x->terminal_name = xstrdup(term); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 x->action = action; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 *y = x; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 } |
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 static void inittab_parsing(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 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
|
126 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
|
127 uint8_t action = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 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
|
129 "shutdown\0restart\0"; |
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 fd = open("/etc/inittab", O_RDONLY); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 if (fd < 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 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
|
134 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
|
135 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
|
136 } else { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 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
|
138 char *x; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
139 |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
140 if ((x = strchr(p, '#'))) *x = '\0'; |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 line_number++; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
142 token_count = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
143 action = 0; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
144 while ((extracted_token = strsep(&p,":"))) { |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 token_count++; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
146 switch (token_count) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
147 case 1: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 if (*extracted_token) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
149 if (!strncmp(extracted_token, "/dev/", 5)) |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1181
diff
changeset
|
150 tty_name = xmprintf("%s",extracted_token); |
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1181
diff
changeset
|
151 else tty_name = xmprintf("/dev/%s",extracted_token); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 } else tty_name = xstrdup(""); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 case 2: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
155 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
156 case 3: |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
157 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
|
158 if (!strcmp(tmp, extracted_token)) { |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
159 action = 1 << i; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
163 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
|
164 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 case 4: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 command = xstrdup(extracted_token); |
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 default: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
169 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
|
170 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
171 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
172 } //while token |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
173 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
174 if (q) free(q); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
175 if (token_count != 4) continue; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
176 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
|
177 free(tty_name); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
178 free(command); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
179 } //while line |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
180 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
181 close(fd); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
182 } |
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 static void run_command(char *command) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
186 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
187 char *final_command[128]; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
188 int hyphen = (command[0]=='-'); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
189 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
190 command = command + hyphen; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
191 if (!strpbrk(command, "?<>'\";[]{}\\|=()*&^$!`~")) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
192 char *next_command; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
193 char *extracted_command; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
194 int x = 0; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
195 |
986
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; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
199 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
|
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; |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
209 final_command[0] = ("-/bin/sh"+!hyphen); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
210 final_command[1] = "-c"; |
986
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(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
230 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
231 if (pid > 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
232 //parent process or error |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
233 //unblock the signals |
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_UNBLOCK, &signal_set, NULL); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
236 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
237 return pid; |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
238 } else if (pid < 0) { |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
239 perror_msg("fork fail"); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
240 sleep(1); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
241 return 0; |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
242 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
243 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
244 //new born child process |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
245 sigset_t signal_set_c; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
246 sigfillset(&signal_set_c); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
247 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
|
248 setsid(); //new session |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
249 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
250 if (x->terminal_name[0]) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
251 close(0); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
252 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
|
253 if (fd != 0) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
254 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
|
255 _exit(EXIT_FAILURE); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
256 } else { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
257 dup2(0, 1); |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
258 dup2(0, 2); |
986
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 } |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
261 reset_term(0); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
262 run_command(x->command); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
263 _exit(-1); |
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 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
266 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
|
267 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
268 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
|
269 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
270 if (pid > 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
271 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
|
272 if (x->pid == pid) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
273 x->pid = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
274 return x; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
275 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
276 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
277 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
278 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
279 return NULL; |
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 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
282 static void waitforpid(pid_t pid) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
283 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
284 if (pid <= 0) return; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
285 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
286 for(;;) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
287 pid_t y = wait(NULL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
288 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
|
289 if (kill(y, 0)) break; |
986
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 } |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
292 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
293 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
|
294 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
295 pid_t pid; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
296 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
|
297 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
298 for (; x; x = x->next) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
299 if (!(x->action & action)) continue; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
300 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
|
301 pid = final_run(x); |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
302 if (!pid) return; |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
303 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
|
304 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
305 if (x->action & (ASKFIRST|RESPAWN)) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
306 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
|
307 } |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
308 } |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
309 |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
310 static void set_default(void) |
986
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 sigset_t signal_set_c; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
313 |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
314 sigatexit(SIG_DFL); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
315 sigfillset(&signal_set_c); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
316 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
|
317 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
318 run_action_from_list(SHUTDOWN); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
319 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
|
320 kill(-1, SIGTERM); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
321 error_msg("Sent SIGTERM to all processes"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
322 sync(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
323 sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
324 kill(-1,SIGKILL); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
325 sync(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
326 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
327 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
328 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
|
329 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
330 unsigned int reboot_magic_no = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
331 pid_t pid; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
332 |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
333 set_default(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
334 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
335 switch (sig_no) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
336 case SIGUSR1: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
337 error_msg("Requesting system halt"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
338 reboot_magic_no=RB_HALT_SYSTEM; |
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 case SIGUSR2: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
341 error_msg("Requesting system poweroff"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
342 reboot_magic_no=RB_POWER_OFF; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
343 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
344 case SIGTERM: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
345 error_msg("Requesting system reboot"); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
346 reboot_magic_no=RB_AUTOBOOT; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
347 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
348 default: |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
349 break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
350 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
351 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
352 sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
353 pid = vfork(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
354 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
355 if (pid == 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
356 reboot(reboot_magic_no); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
357 _exit(EXIT_SUCCESS); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
358 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
359 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
360 while(1) sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
361 } |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
362 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
363 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
|
364 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
365 struct action_list_seed *x; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
366 pid_t pid; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
367 int fd; |
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 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
|
370 if (!(x->action & RESTART)) continue; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
371 |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
372 set_default(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
373 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
374 if (x->terminal_name[0]) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
375 close(0); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
376 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
|
377 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
378 if (fd != 0) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
379 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
|
380 sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
381 pid = vfork(); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
382 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
383 if (pid == 0) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
384 reboot(RB_HALT_SYSTEM); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
385 _exit(EXIT_SUCCESS); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
386 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
387 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
388 while(1) sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
389 } else { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
390 dup2(0, 1); |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
391 dup2(0, 2); |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
392 reset_term(0); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
393 run_command(x->command); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
394 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
395 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
396 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
397 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
398 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
399 static void catch_signal(int sig_no) |
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 caught_signal = sig_no; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
402 error_msg("signal seen"); |
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 static void pause_handler(int sig_no) |
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 int signal_backup,errno_backup; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
408 pid_t pid; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
409 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
410 errno_backup = errno; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
411 signal_backup = caught_signal; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
412 signal(SIGCONT, catch_signal); |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
413 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
414 while(1) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
415 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
|
416 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
|
417 mark_as_terminated_process(pid); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
418 sleep(1); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
419 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
420 |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
421 signal(SIGCONT, SIG_DFL); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
422 errno = errno_backup; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
423 caught_signal = signal_backup; |
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 |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
426 static int check_if_pending_signals(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
427 { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
428 int signal_caught = 0; |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
429 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
430 while(1) { |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
431 int sig = caught_signal; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
432 if (!sig) return signal_caught; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
433 caught_signal = 0; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
434 signal_caught = 1; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
435 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
|
436 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
437 } |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
438 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
439 void init_main(void) |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
440 { |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
441 struct sigaction sig_act; |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
442 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
443 if (getpid() != 1) error_exit("Already running"); |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
444 printf("Started init\n"); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
445 initialize_console(); |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
446 reset_term(0); |
986
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 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
|
449 setsid(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
450 |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
451 putenv("HOME=/"); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
452 putenv("PATH=/sbin:/usr/sbin:/bin:/usr/bin"); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
453 putenv("SHELL=/bin/sh"); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
454 putenv("USER=root"); |
1298
c25ee9918e65
First pass init cleanup: use sigatexit() to set multiple signal handlers, rename set_sane_term() to reset_term() and have it take the fd it works on as an argument, some whitespace and help text tweaks.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
455 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
456 inittab_parsing(); |
1185
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
457 signal(SIGUSR1, halt_poweroff_reboot_handler);//halt |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
458 signal(SIGUSR2, halt_poweroff_reboot_handler);//poweroff |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
459 signal(SIGTERM, halt_poweroff_reboot_handler);//reboot |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
460 signal(SIGQUIT, restart_init_handler);//restart init |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
461 memset(&sig_act, 0, sizeof(sig_act)); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
462 sigfillset(&sig_act.sa_mask); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
463 sigdelset(&sig_act.sa_mask, SIGCONT); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
464 sig_act.sa_handler = pause_handler; |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
465 sigaction(SIGTSTP, &sig_act, NULL); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
466 memset(&sig_act, 0, sizeof(sig_act)); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
467 sig_act.sa_handler = catch_signal; |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
468 sigaction(SIGINT, &sig_act, NULL); |
2a4f1dc494d0
-Eradicate (char*) casts for strings.
Isaac Dunham <ibid.ag@gmail.com>
parents:
1183
diff
changeset
|
469 sigaction(SIGHUP, &sig_act, NULL); |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
470 run_action_from_list(SYSINIT); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
471 check_if_pending_signals(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
472 run_action_from_list(WAIT); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
473 check_if_pending_signals(); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
474 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
|
475 while (1) { |
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
476 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
|
477 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
478 run_action_from_list(RESPAWN | ASKFIRST); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
479 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
|
480 sleep(1);//let cpu breath |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
481 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
|
482 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
|
483 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
484 while(1) { |
1181
a2f80613be36
Whitespace changes, and collate a couple declarations/first assignment.
Rob Landley <rob@landley.net>
parents:
986
diff
changeset
|
485 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
|
486 |
986
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
487 if (pid <= 0) break; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
488 mark_as_terminated_process(pid); |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
489 suspected_WNOHANG = WNOHANG; |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
490 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
491 } |
69adf14d70e1
System V style init, submitted by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
492 } |