Skip to content

Commit e7050c2

Browse files
committed
god: add bypass statuses for respawning
1 parent 8857e0c commit e7050c2

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

examples/cpp_ipc_brief_demo/god.cpp

+25-11
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include "ipc/ipc.h"
1010
#include "ipc/debug.h"
1111

12+
#define len(array) (int)(sizeof(array) / sizeof(array[0]))
13+
1214
typedef struct
1315
{
1416
const char * const name;
15-
int respawn; // -1 = infinite , 0 = never, 1 = once, 2 = twice, etc.
17+
int respawn; // respawn on error: -1 = infinite , 0 = never, 1 = once, 2 = twice, etc.
1618

1719
// internal
1820
pid_t pid;
@@ -26,17 +28,20 @@ child children[] =
2628
//==================== FILL IN PROCESSES ====================
2729
{
2830
.name = "/usr/bin/cpp_ipc_brief_demo_prod",
29-
.respawn = 1,
31+
.respawn = -1,
3032
},
3133
{
3234
.name = "/usr/bin/cpp_ipc_brief_demo_cons",
3335
.args = { "--my_option", "--my_other_option", },
3436
},
3537
//===========================================================
3638
};
39+
#define n_children len(children)
3740

38-
#define n_children (int)(sizeof(children) / sizeof(children[0]))
39-
#define lengthof(var) (sizeof(var) / sizeof(var[0]))
41+
//==================== STATUSES THAT BYPASS RESPAWN =========
42+
// Children that are configured to respawn will not be respawned if they return with these statuses:
43+
int respawn_bypass_statuses[] = { 0, SIGINT };
44+
//===========================================================
4045

4146
int fork_child(child *c)
4247
{
@@ -50,11 +55,11 @@ int fork_child(child *c)
5055

5156
setpgid(0, 0); // switch process group so ctrl-c only interrupts god
5257

53-
char * child_argv[lengthof(c->args) + 2];
58+
char * child_argv[len(c->args) + 2];
5459
child_argv[0] = (char*)(c->name);
55-
for (int i=0; i < lengthof(c->args); ++i)
60+
for (int i=0; i < len(c->args); ++i)
5661
child_argv[i+1] = c->args[i];
57-
child_argv[lengthof(c->args) + 1] = NULL;
62+
child_argv[len(c->args) + 1] = NULL;
5863

5964
execv(c->name, child_argv);
6065
}
@@ -107,13 +112,22 @@ void child_handler(int sig)
107112
c->alive = 0;
108113
debug("child %u (%s) exited with status %d\n", c->pid, c->name, status);
109114

110-
if (c->respawn != 0)
115+
if ((c->respawn != 0))
111116
{
112-
debug("respawning child\n");
113-
fork_child(c);
117+
int bypass_statuses = 0;
118+
for (int i=0; i < len(respawn_bypass_statuses); ++i)
119+
if (status == respawn_bypass_statuses[i])
120+
++bypass_statuses;
121+
122+
if (bypass_statuses)
123+
debug("respawn bypassed\n");
124+
else
125+
{
126+
debug("respawning child\n");
127+
fork_child(c);
114128

115-
if (c->respawn > 0)
116129
--c->respawn;
130+
}
117131
}
118132
}
119133

0 commit comments

Comments
 (0)