|
22 | 22 | // flush_network_table() |
23 | 23 | #include "database/network-table.h" |
24 | 24 | #include "config/config.h" |
| 25 | +// gravity_running |
| 26 | +#include "daemon.h" |
25 | 27 |
|
26 | 28 | static int run_and_stream_command(struct ftl_conn *api, const char *path, const char *const args[], const char *extra_env) |
27 | 29 | { |
@@ -57,6 +59,18 @@ static int run_and_stream_command(struct ftl_conn *api, const char *path, const |
57 | 59 | if(extra_env != NULL) |
58 | 60 | setenv(extra_env, "1", 1); |
59 | 61 |
|
| 62 | + // Detach child into its own session/process group so signals |
| 63 | + // sent to the parent's process group (like SIGTERM) do not |
| 64 | + // propagate to this child. |
| 65 | + (void)setsid(); |
| 66 | + |
| 67 | + // Ignore SIGTERM so systemd's cgroup-level kill (the |
| 68 | + // default KillMode=control-group sends SIGTERM to ALL |
| 69 | + // processes in the cgroup) doesn't terminate gravity |
| 70 | + // mid-run. SIG_IGN is preserved across execv(), unlike |
| 71 | + // custom handlers which are reset to SIG_DFL. |
| 72 | + signal(SIGTERM, SIG_IGN); |
| 73 | + |
60 | 74 | // Run pihole -g |
61 | 75 | execv(path, (char *const *)args); |
62 | 76 |
|
@@ -129,7 +143,16 @@ int api_action_gravity(struct ftl_conn *api) |
129 | 143 | get_bool_var(query, "color", &color); |
130 | 144 |
|
131 | 145 | const char *extra_env = color ? "FORCE_COLOR" : NULL; |
132 | | - return run_and_stream_command(api, "/usr/local/bin/pihole", (const char *const []){ "pihole", "-g", NULL }, extra_env); |
| 146 | + |
| 147 | + gravity_running = 1; |
| 148 | + const int ret = run_and_stream_command(api, "/usr/local/bin/pihole", (const char *const []){ "pihole", "-g", NULL }, extra_env); |
| 149 | + gravity_running = 0; |
| 150 | + |
| 151 | + // If a termination/restart was requested while gravity was running, |
| 152 | + // act on it now rather than waiting up to ~1s for the GC thread to pick it up |
| 153 | + check_if_want_terminate(); |
| 154 | + |
| 155 | + return ret; |
133 | 156 | } |
134 | 157 |
|
135 | 158 | int api_action_restartDNS(struct ftl_conn *api) |
|
0 commit comments