-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgod.cpp
More file actions
150 lines (116 loc) · 3.25 KB
/
god.cpp
File metadata and controls
150 lines (116 loc) · 3.25 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
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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#define debug_stdout stdout
#define debug_stderr stderr
#include "ipc/ipc.h"
#include "ipc/debug.h"
#define len(array) (int)(sizeof(array) / sizeof(array[0]))
typedef struct
{
const char * const name;
int respawn; // respawn on error: -1 = infinite , 0 = never, 1 = once, 2 = twice, etc.
// internal
pid_t pid;
int alive;
char* args[10]; // increase as needed
} child;
child children[] =
{
//==================== FILL IN PROCESSES ====================
{
.name = "/usr/bin/cpp_ipc_brief_demo_prod",
.respawn = -1,
},
{
.name = "/usr/bin/cpp_ipc_brief_demo_cons",
.args = { "--my_option", "--my_other_option", },
},
//===========================================================
};
#define n_children len(children)
//==================== STATUSES THAT BYPASS RESPAWN =========
// Children that are configured to respawn will not be respawned if they return with these statuses:
int respawn_bypass_statuses[] = { 0, SIGINT };
//===========================================================
int fork_child(child *c)
{
pid_t pid = fork();
debug_assert(pid != -1, return 1);
if (pid == 0)
{
debug("child %u (%s) was spawned by parent %u\n", getpid(), c->name, getppid());
setpgid(0, 0); // switch process group so ctrl-c only interrupts god
char * child_argv[len(c->args) + 2];
child_argv[0] = (char*)(c->name);
for (int i=0; i < len(c->args); ++i)
child_argv[i+1] = c->args[i];
child_argv[len(c->args) + 1] = NULL;
execv(c->name, child_argv);
}
c->pid = pid;
c->alive = 1;
debug("parent %u spawned child %u (%s)\n", getpid(), c->pid, c->name);
return 0;
}
void exit_handler(int sig)
{
debug("got signal %d\n", sig);
// kill the children manually, since they're in a different group
signal(SIGCHLD, SIG_IGN);
for (int i=0; i < n_children; ++i)
{
if (children[i].alive)
{
debug("killing %u (%s)\n", children[i].pid, children[i].name);
debug_assert(kill(children[i].pid, sig) == 0);
}
}
ipc_cleanup();
debug("exiting %u (god process) with status %d\n", getpid(), sig);
exit(sig);
}
void child_handler(int sig)
{
pid_t pid;
int status;
for (pid_t tmp_pid; (tmp_pid = waitpid(-1, &status, WNOHANG)) > 0;)
pid = tmp_pid;
int child_index = -1;
for (int i = 0; i < n_children; ++i)
if (children[i].pid == pid)
child_index = i;
debug_assert(child_index != -1, return);
child *c = children + child_index;
c->alive = 0;
debug("child %u (%s) exited with status %d\n", c->pid, c->name, status);
if ((c->respawn != 0))
{
int bypass_statuses = 0;
for (int i=0; i < len(respawn_bypass_statuses); ++i)
if (status == respawn_bypass_statuses[i])
++bypass_statuses;
if (bypass_statuses)
debug("respawn bypassed\n");
else
{
debug("respawning child\n");
fork_child(c);
--c->respawn;
}
}
}
int main()
{
debug("starting %u (god process)\n", getpid());
for (int i=0; i < n_children; ++i)
fork_child(children + i);
debug("%u (god process) finished spawning threads\n", getpid());
signal(SIGINT, exit_handler);
signal(SIGCHLD, child_handler);
while(1)
pause();
return 0;
}