Mercurial > vinit
annotate vinit.c @ 7:7d40d4ae7377 default tip @
misc: update copyright years
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 02 Jan 2022 10:26:58 +0100 |
parents | 3db6ca1fe07d |
children |
rev | line source |
---|---|
1 | 1 /* |
2 | 2 * vinit.c -- the Vanilla Linux init |
1 | 3 * |
7
7d40d4ae7377
misc: update copyright years
David Demelier <markand@malikania.fr>
parents:
4
diff
changeset
|
4 * Copyright (c) 2021-2022 David Demelier <markand@malikania.fr> |
1 | 5 * |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
0 | 19 #include <sys/wait.h> |
20 #include <errno.h> | |
21 #include <signal.h> | |
22 #include <stdarg.h> | |
23 #include <stdio.h> | |
24 #include <stdlib.h> | |
25 #include <stdnoreturn.h> | |
26 #include <string.h> | |
27 #include <unistd.h> | |
28 | |
4
3db6ca1fe07d
misc: merge scripts from vinit-script
David Demelier <markand@malikania.fr>
parents:
2
diff
changeset
|
29 static char * const rcinit[] = { SYSCONFDIR "/vinit.start", NULL }; |
3db6ca1fe07d
misc: merge scripts from vinit-script
David Demelier <markand@malikania.fr>
parents:
2
diff
changeset
|
30 static char * const rchalt[] = { SYSCONFDIR "/vinit.shutdown", "halt", NULL }; |
3db6ca1fe07d
misc: merge scripts from vinit-script
David Demelier <markand@malikania.fr>
parents:
2
diff
changeset
|
31 static char * const rcpoweroff[] = { SYSCONFDIR "/vinit.shutdown", "poweroff", NULL }; |
3db6ca1fe07d
misc: merge scripts from vinit-script
David Demelier <markand@malikania.fr>
parents:
2
diff
changeset
|
32 static char * const rcreboot[] = { SYSCONFDIR "/vinit.shutdown", "reboot", NULL }; |
0 | 33 |
34 static sigset_t set; | |
35 | |
36 noreturn static void | |
37 die(const char *fmt, ...) | |
38 { | |
39 va_list ap; | |
40 | |
41 va_start(ap, fmt); | |
42 vfprintf(stderr, fmt, ap); | |
43 va_end(ap); | |
44 exit(1); | |
45 } | |
46 | |
47 static void | |
48 spawn(char * const args[]) | |
49 { | |
50 switch (fork()) { | |
51 case 0: | |
52 sigprocmask(SIG_UNBLOCK, &set, NULL); | |
53 setsid(); | |
54 execv(args[0], args); | |
55 perror("execv"); | |
56 exit(1); | |
1 | 57 case -1: |
0 | 58 perror("fork"); |
59 default: | |
60 break; | |
61 } | |
62 } | |
63 | |
64 int | |
65 main(void) | |
66 { | |
67 int sig; | |
68 | |
69 if (getpid() != 1) | |
70 die("abort: must be ran as PID 1\n"); | |
71 if (chdir("/") < 0) | |
72 die("abort: %s\n", strerror(errno)); | |
73 | |
74 sigfillset(&set); | |
75 sigprocmask(SIG_BLOCK, &set, NULL); | |
76 spawn(rcinit); | |
77 | |
78 for (;;) { | |
79 sigwait(&set, &sig); | |
80 | |
81 /* Match busybox defaults. */ | |
82 switch (sig) { | |
83 case SIGUSR1: | |
84 spawn(rchalt); | |
85 break; | |
86 case SIGUSR2: | |
87 spawn(rcpoweroff); | |
88 break; | |
89 case SIGTERM: | |
90 spawn(rcreboot); | |
91 break; | |
92 case SIGCHLD: | |
93 while (waitpid(-1, NULL, WNOHANG) > 0) | |
94 continue; | |
95 default: | |
96 break; | |
97 } | |
98 } | |
99 } |