-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathichirou.c
295 lines (256 loc) · 6.55 KB
/
ichirou.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* ichirou - a basic init system for UNIX systems
* Copyright (c) 2020-2021 Emily <elishikawa@jagudev.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/reboot.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define LEN(x) (sizeof (x) / sizeof *(x))
#define TIMEO 30
static void sigpoweroff(void);
static void sigfpoweroff(void);
static void sigffpoweroff(void);
static void sigreap(void);
static void sigreboot(void);
static void sigfreboot(void);
static void sigffreboot(void);
static void sighalt(void);
static void sigfhalt(void);
static void sighibernate(void);
static void spawnwait(char *const argv[]);
static struct {
int signal;
void (*handler)(void);
} signalmap[] = {
{ SIGUSR1, sigpoweroff },
{ SIGUSR2, sigfpoweroff },
{ SIGTTIN, sigffpoweroff },
{ SIGCHLD, sigreap },
{ SIGALRM, sigreap },
{ SIGINT, sigreboot },
{ SIGTERM, sigfreboot },
{ SIGTTOU, sigffreboot },
{ SIGQUIT, sighalt },
{ SIGPWR, sigfhalt },
{ SIGHUP, sighibernate },
};
#include "config.h"
static sigset_t set;
int main(void) {
/* init signal handlers */
int signal;
sigfillset(&set);
sigprocmask(SIG_BLOCK, &set, NULL);
/* iterator var */
size_t i;
/* if not pid 1, exit */
if (getpid() != 1) {
fprintf(stderr, "This program must be run with PID 1\n");
return 1;
}
/* start system initialization script */
chdir("/");
spawnwait(rcinitfile);
spawnwait(servstartcmd);
spawnwait(rcpostinitfile);
/* handle signals */
while (1) {
alarm(TIMEO);
sigwait(&set, &signal);
for (i = 0; i < LEN(signalmap); i++) {
if (signalmap[i].signal == signal) {
signalmap[i].handler();
break;
}
}
}
/* will never be reached */
return 0;
}
/**
* this safely powers off the system
* it stops services, asks processes to terminate,
* kills non-terminating processes, syncs filesystems,
* runs shutdown rcfile and then powers off the system.
**/
static void sigpoweroff(void) {
/* stop services */
spawnwait(servstopcmd);
sigreap();
/* ask processes to terminate */
kill(-1, SIGTERM);
sigreap();
/* after SIGKILLTIMEOUT seconds, kill all processes */
sleep(SIGKILLTIMEOUT);
sigfpoweroff();
}
/**
* this powers off the system with force level 1
* it kills all processes, syncs filesystems,
* runs the shutdown rcfile and then powers off the system.
**/
static void sigfpoweroff(void) {
/* kill all processes */
kill(-1, SIGKILL);
sigreap();
/* sync filesystems */
sync();
/* run rc.shutdown */
spawnwait(rcshutdownfile);
sigreap();
/* power off system */
sigffpoweroff();
}
/**
* this powers off the system with force level 2
* it directly powers off the system.
**/
static void sigffpoweroff(void) {
/* power off system */
if (vfork() == 0) {
reboot(RB_POWER_OFF);
_exit(EXIT_SUCCESS);
}
while (1)
sleep(1);
}
/**
* this reaps child processes
**/
static void sigreap(void) {
/* reap children */
while (waitpid(-1, NULL, WNOHANG) > 0);
alarm(TIMEO);
}
/**
* this safely reboots the system.
* it stops services, asks processes to terminate,
* kills non-terminating processes, syncs filesystems,
* runs shutdown rcfile and then reboots the system.
**/
static void sigreboot(void) {
/* stop services */
spawnwait(servstopcmd);
sigreap();
/* ask processes to terminate */
kill(-1, SIGTERM);
sigreap();
/* after SIGKILLTIMEOUT seconds, kill all processes */
sleep(SIGKILLTIMEOUT);
sigfreboot();
}
/**
* this reboots the system with force level 1
* it kills all processes, syncs filesystems,
* runs the shutdown rcfile and then reboots the system.
**/
static void sigfreboot(void) {
/* kill all processes */
kill(-1, SIGKILL);
sigreap();
/* sync filesystems */
sync();
/* run rc.shutdown */
spawnwait(rcshutdownfile);
sigreap();
/* reboot system */
sigffreboot();
}
/**
* this reboots the system with force level 2
* it directly reboots the system.
**/
static void sigffreboot(void) {
/* reboot system */
if (vfork() == 0) {
reboot(RB_AUTOBOOT);
_exit(EXIT_SUCCESS);
}
while (1)
sleep(1);
}
/**
* this halts the system.
* it performs the steps of a normal shutdown
* and then tells the kernel to halt.
**/
static void sighalt(void) {
/* stop services */
spawnwait(servstopcmd);
sigreap();
/* ask processes to terminate */
kill(-1, SIGTERM);
sigreap();
/* after SIGKILLTIMEOUT seconds, kill all processes */
sleep(SIGKILLTIMEOUT);
kill(-1, SIGKILL);
sigreap();
/* sync filesystems */
sync();
/* run rc.shutdown */
spawnwait(rcshutdownfile);
sigreap();
/* halt system */
sigfhalt();
}
/**
* this halts the system with force level 2
* it directly tells the kernel to halt the system.
**/
static void sigfhalt(void) {
/* halt system */
if (vfork() == 0) {
reboot(RB_HALT_SYSTEM);
_exit(EXIT_SUCCESS);
}
while (1)
sleep(1);
}
/**
* this hibernates the system
* it tells the kernel to hibernate the system.
* yet untested, probably not ready
* for use in production (yet).
**/
static void sighibernate(void) {
/* hibernate system */
if (vfork() == 0) {
reboot(RB_SW_SUSPEND);
_exit(EXIT_SUCCESS);
}
while (1)
sleep(1);
}
static void spawnwait(char *const argv[]) {
pid_t chpid = fork();
switch (chpid) {
case 0:
sigprocmask(SIG_UNBLOCK, &set, NULL);
setsid();
execvp(argv[0], argv);
perror("execvp");
_exit(1);
case -1:
perror("fork");
break;
default:
waitpid(chpid, NULL, WUNTRACED);
}
}