Skip to content

Commit 3f46db7

Browse files
jeffhostetlerdscho
authored andcommitted
compat/fsmonitor/fsm-health-win32: add framework for periodically monitoring
Create framework in Win32 version of the "health" thread to periodically inspect the system and shutdown if warranted. This version just include the setup for the timeout in WaitForMultipleObjects() and calls (currently empty) table of functions. A later commit will add functions to the table to actually inspect the system. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent 66ad532 commit 3f46db7

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

compat/fsmonitor/fsm-health-win32.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,40 @@
44
#include "fsm-health.h"
55
#include "fsmonitor--daemon.h"
66

7+
/*
8+
* Every minute wake up and test our health.
9+
*/
10+
#define WAIT_FREQ_MS (60 * 1000)
11+
12+
enum interval_fn_ctx { CTX_INIT = 0, CTX_TERM, CTX_TIMER };
13+
14+
typedef int (interval_fn)(struct fsmonitor_daemon_state *state,
15+
enum interval_fn_ctx ctx);
16+
17+
static interval_fn *table[] = {
18+
NULL, /* must be last */
19+
};
20+
21+
/*
22+
* Call all of the functions in the table.
23+
* Shortcut and return first error.
24+
*
25+
* Return 0 if all succeeded.
26+
*/
27+
static int call_all(struct fsmonitor_daemon_state *state,
28+
enum interval_fn_ctx ctx)
29+
{
30+
int k;
31+
32+
for (k = 0; table[k]; k++) {
33+
int r = table[k](state, ctx);
34+
if (r)
35+
return r;
36+
}
37+
38+
return 0;
39+
}
40+
741
struct fsm_health_data
842
{
943
HANDLE hEventShutdown;
@@ -45,24 +79,42 @@ void fsm_health__dtor(struct fsmonitor_daemon_state *state)
4579
void fsm_health__loop(struct fsmonitor_daemon_state *state)
4680
{
4781
struct fsm_health_data *data = state->health_data;
82+
int r;
83+
84+
r = call_all(state, CTX_INIT);
85+
if (r < 0)
86+
goto force_error_stop;
87+
if (r > 0)
88+
goto force_shutdown;
4889

4990
for (;;) {
5091
DWORD dwWait = WaitForMultipleObjects(data->nr_handles,
5192
data->hHandles,
52-
FALSE, INFINITE);
93+
FALSE, WAIT_FREQ_MS);
5394

5495
if (dwWait == WAIT_OBJECT_0 + HEALTH_SHUTDOWN)
5596
goto clean_shutdown;
5697

98+
if (dwWait == WAIT_TIMEOUT) {
99+
r = call_all(state, CTX_TIMER);
100+
if (r < 0)
101+
goto force_error_stop;
102+
if (r > 0)
103+
goto force_shutdown;
104+
continue;
105+
}
106+
57107
error(_("health thread wait failed [GLE %ld]"),
58108
GetLastError());
59109
goto force_error_stop;
60110
}
61111

62112
force_error_stop:
63113
state->health_error_code = -1;
114+
force_shutdown:
64115
ipc_server_stop_async(state->ipc_server_data);
65116
clean_shutdown:
117+
call_all(state, CTX_TERM);
66118
return;
67119
}
68120

0 commit comments

Comments
 (0)