-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatchdog.cpp
More file actions
300 lines (246 loc) · 6.74 KB
/
watchdog.cpp
File metadata and controls
300 lines (246 loc) · 6.74 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <node_api.h>
#include <stdio.h>
#include <time.h>
#include <uv.h>
#if !defined(_MSC_VER)
#include <unistd.h>
#endif
#include <stdlib.h>
#include <memory.h>
#include <inttypes.h>
#define EXIT_CODE (87)
class WatchdogThread
{
public:
WatchdogThread(uint64_t timeout, bool terminate, bool print)
: m_timeout(timeout)
, m_terminate(terminate)
, m_print(print)
, m_running(false)
, m_triggered(false)
, m_timestamp(uv_hrtime())
{
uv_mutex_init(&m_mutex);
uv_cond_init(&m_condition);
}
~WatchdogThread()
{
uv_cond_destroy(&m_condition);
uv_mutex_destroy(&m_mutex);
}
bool start()
{
m_timestamp = uv_hrtime();
m_triggered = false;
m_running = true;
if (uv_thread_create(&m_id, threadEntry, (void*)this))
{
m_running = false;
return false;
}
return true;
}
bool stop()
{
uv_mutex_lock(&m_mutex);
m_timestamp = uv_hrtime();
m_running = false;
uv_cond_signal(&m_condition);
uv_mutex_unlock(&m_mutex);
uv_thread_join(&m_id);
return m_triggered;
}
void ping()
{
m_timestamp = uv_hrtime();
}
private:
void threadFunc()
{
do {
uint64_t now = uv_hrtime();
if ((now - m_timestamp) > m_timeout)
{
if (!m_triggered) {
m_triggered = true;
if (m_terminate)
{
if (m_print)
{
fprintf(stderr, "FATAL: canary - watchdog timeout detected (no ping after %" PRIu64 "ms), exiting application.\n", m_timeout / 1000000);
}
exit(EXIT_CODE);
}
else if (m_print)
{
fprintf(stderr, "canary - watchdog timeout detected (no ping after %" PRIu64 "ms)\n", m_timeout / 1000000);
}
}
}
uv_mutex_lock(&m_mutex);
uv_cond_timedwait(&m_condition, &m_mutex, (uint64_t)1e9);
uv_mutex_unlock(&m_mutex);
} while (m_running);
}
static void threadEntry(void* arg)
{
WatchdogThread* instance = (WatchdogThread*)arg;
instance->threadFunc();
}
uint64_t m_timeout;
bool m_terminate;
bool m_print;
volatile bool m_running;
volatile bool m_triggered;
volatile uint64_t m_timestamp;
uv_mutex_t m_mutex;
uv_cond_t m_condition;
uv_thread_t m_id;
};
WatchdogThread* s_thread = nullptr;
bool get_start_arguments(napi_env env, napi_callback_info info, uint64_t& timeout, bool& terminate, bool& print)
{
napi_status status;
do {
size_t argc = 1;
napi_value argv[1];
status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
if (status != napi_ok)
{
napi_throw_error(env, nullptr, "Could not get callback info");
break;
}
napi_value timeout_value;
status = napi_get_named_property(env, argv[0], "timeout", &timeout_value);
if (status != napi_ok)
{
napi_throw_error(env, nullptr, "Could not get timeout property");
break;
}
status = napi_get_value_int64(env, timeout_value, (int64_t*)&timeout);
if (status != napi_ok)
{
napi_throw_error(env, nullptr, "Could not read timeout property as integer");
break;
}
napi_value terminate_value;
status = napi_get_named_property(env, argv[0], "terminate", &terminate_value);
if (status != napi_ok)
{
napi_throw_error(env, nullptr, "Could not get terminate property");
break;
}
status = napi_get_value_bool(env, terminate_value, &terminate);
if (status != napi_ok)
{
napi_throw_error(env, nullptr, "Could not read terminate property as boolean");
break;
}
napi_value print_value;
status = napi_get_named_property(env, argv[0], "print", &print_value);
if (status != napi_ok)
{
napi_throw_error(env, nullptr, "Could not get print property");
break;
}
status = napi_get_value_bool(env, print_value, &print);
if (status != napi_ok)
{
napi_throw_error(env, nullptr, "Could not read print property as boolean");
break;
}
return true;
} while (false);
return false;
}
/*!
*
* start - Start watchdog
*
* arguments:
* 0 - options object
* timeout - watchdog timeout in ms
* terminate - terminate when triggered
* print - print when triggered
*
**/
napi_value start(napi_env env, napi_callback_info cbinfo)
{
if (s_thread)
{
napi_throw_error(env, nullptr, "Watchdog already running");
return nullptr;
}
uint64_t timeout;
bool terminate, print;
if (!get_start_arguments(env, cbinfo, timeout, terminate, print))
{
return nullptr;
}
s_thread = new WatchdogThread(timeout * 1000000, terminate, print);
if (!s_thread->start())
{
delete s_thread;
s_thread = nullptr;
napi_throw_error(env, NULL, "Failed to launch watchdog thread");
return nullptr;
}
return nullptr;
}
/*!
*
* stop - Stop watchdog
*
* returns true if watchdog was triggered
*
**/
napi_value stop(napi_env env, napi_callback_info args)
{
if (!s_thread)
{
napi_throw_error(env, nullptr, "Watchdog is not running");
return nullptr;
}
bool triggered = s_thread->stop();
delete s_thread;
s_thread = nullptr;
napi_value output;
napi_get_boolean(env, triggered, &output);
return output;
}
/*!
*
* ping - Update ping timestamp
*
**/
napi_value ping(napi_env env, napi_callback_info args)
{
if (!s_thread)
{
napi_throw_error(env, nullptr, "Watchdog is not running");
return nullptr;
}
s_thread->ping();
return nullptr;
}
#define REGISTER_FUNCTION(NAME)\
{\
napi_status status;\
napi_value fn;\
status = napi_create_function(env, nullptr, 0, NAME, nullptr, &fn);\
if (status != napi_ok) break;\
status = napi_set_named_property(env, exports, #NAME, fn);\
if (status != napi_ok) break;\
}
napi_value init(napi_env env, napi_value exports)
{
do {
REGISTER_FUNCTION(start);
REGISTER_FUNCTION(stop);
REGISTER_FUNCTION(ping);
return exports;
} while (false);
napi_throw_error(env, nullptr, "Failed to initialize");
return nullptr;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, init)