-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.c
More file actions
72 lines (60 loc) · 1.53 KB
/
init.c
File metadata and controls
72 lines (60 loc) · 1.53 KB
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
/*
init.c:
main file - WIP small init system
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "power.h"
#include "startup-scripts.h"
/* kill zombie processes */
void killZombies(int sig)
{
pid_t zombie_pid;
int zombie_status;
while ((zombie_pid = waitpid(-1, &zombie_status, WNOHANG)) > 0)
{
/* "service monitoring" */
for (int i = 0; i < service_count; i++)
{
if (zombie_pid == services[i].pid && services[i].is_active)
{
if (WIFEXITED(zombie_status)) /* if service exited normally */
{
printf("[monitor] Service '%s' (PID %d) exited normally with status %d\n",
services[i].command, services[i].pid, WEXITSTATUS(zombie_status));
}
else if (WIFSIGNALED(zombie_status)) /* if service was killed by a signal */
{
printf("[monitor] Service '%s' (PID %d) was killed by signal %d\n",
services[i].command, services[i].pid, WTERMSIG(zombie_status));
}
services[i].is_active = 0; /* mark service dead */
break;
}
}
}
return;
}
int main(int argc, char *argv[])
{
if (argc > 1)
printf("[info] There are arguments passed to this program\n\n");
signal(SIGCHLD, killZombies);
/* power */
/* (take a look at power.c) */
signal(SIGTERM, powerControl);
signal(SIGINT, powerControl);
signal(SIGHUP, powerControl);
/* run commands */
printf("[init] Running start commands\n");
runCommands();
/* main loop */
while (1)
{
pause(); // sleep until a signal received
}
return 0; // haha scary
}