1
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
* Permission is hereby granted, free of charge, to any person obtaining a copy
3
* of this software and associated documentation files (the "Software"), to
4
* deal in the Software without restriction, including without limitation the
5
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6
* sell copies of the Software, and to permit persons to whom the Software is
7
* furnished to do so, subject to the following conditions:
9
* The above copyright notice and this permission notice shall be included in
10
* all copies or substantial portions of the Software.
12
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26
#include "handle-inl.h"
30
RB_HEAD(uv_signal_tree_s, uv_signal_s);
32
static struct uv_signal_tree_s uv__signal_tree = RB_INITIALIZER(uv__signal_tree);
33
static ssize_t volatile uv__signal_control_handler_refs = 0;
34
static CRITICAL_SECTION uv__signal_lock;
37
void uv_signals_init() {
38
InitializeCriticalSection(&uv__signal_lock);
42
static int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2) {
43
/* Compare signums first so all watchers with the same signnum end up */
45
if (w1->signum < w2->signum) return -1;
46
if (w1->signum > w2->signum) return 1;
48
/* Sort by loop pointer, so we can easily look up the first item after */
49
/* { .signum = x, .loop = NULL } */
50
if ((uintptr_t) w1->loop < (uintptr_t) w2->loop) return -1;
51
if ((uintptr_t) w1->loop > (uintptr_t) w2->loop) return 1;
53
if ((uintptr_t) w1 < (uintptr_t) w2) return -1;
54
if ((uintptr_t) w1 > (uintptr_t) w2) return 1;
60
RB_GENERATE_STATIC(uv_signal_tree_s, uv_signal_s, tree_entry, uv__signal_compare);
64
* Dispatches signal {signum} to all active uv_signal_t watchers in all loops.
65
* Returns 1 if the signal was dispatched to any watcher, or 0 if there were
66
* no active signal watchers observing this signal.
68
int uv__signal_dispatch(int signum) {
73
EnterCriticalSection(&uv__signal_lock);
75
lookup.signum = signum;
78
for (handle = RB_NFIND(uv_signal_tree_s, &uv__signal_tree, &lookup);
79
handle != NULL && handle->signum == signum;
80
handle = RB_NEXT(uv_signal_tree_s, &uv__signal_tree, handle)) {
81
unsigned long previous = InterlockedExchange(&handle->pending_signum, signum);
84
POST_COMPLETION_FOR_REQ(handle->loop, &handle->signal_req);
90
LeaveCriticalSection(&uv__signal_lock);
96
static BOOL WINAPI uv__signal_control_handler(DWORD type) {
99
return uv__signal_dispatch(SIGINT);
101
case CTRL_BREAK_EVENT:
102
return uv__signal_dispatch(SIGBREAK);
104
case CTRL_CLOSE_EVENT:
105
if (uv__signal_dispatch(SIGHUP)) {
106
/* Windows will terminate the process after the control handler */
107
/* returns. After that it will just terminate our process. Therefore */
108
/* block the signal handler so the main loop has some time to pick */
109
/* up the signal and do something for a few seconds. */
115
case CTRL_LOGOFF_EVENT:
116
case CTRL_SHUTDOWN_EVENT:
117
/* These signals are only sent to services. Services have their own */
118
/* notification mechanism, so there's no point in handling these. */
121
/* We don't handle these. */
127
static uv_err_t uv__signal_register_control_handler() {
128
/* When this function is called, the uv__signal_lock must be held. */
130
/* If the console control handler has already been hooked, just add a */
132
if (uv__signal_control_handler_refs > 0)
135
if (!SetConsoleCtrlHandler(uv__signal_control_handler, TRUE))
136
return uv__new_sys_error(GetLastError());
138
uv__signal_control_handler_refs++;
144
static void uv__signal_unregister_control_handler() {
145
/* When this function is called, the uv__signal_lock must be held. */
148
/* Don't unregister if the number of console control handlers exceeds one. */
149
/* Just remove a reference in that case. */
150
if (uv__signal_control_handler_refs > 1) {
151
uv__signal_control_handler_refs--;
155
assert(uv__signal_control_handler_refs == 1);
157
r = SetConsoleCtrlHandler(uv__signal_control_handler, FALSE);
158
/* This should never fail; if it does it is probably a bug in libuv. */
161
uv__signal_control_handler_refs--;
165
static uv_err_t uv__signal_register(int signum) {
170
return uv__signal_register_control_handler();
173
/* SIGWINCH is generated in tty.c. No need to register anything. */
182
/* Signal is never raised. */
186
/* Invalid signal. */
187
return uv__new_artificial_error(UV_EINVAL);
192
static void uv__signal_unregister(int signum) {
197
uv__signal_unregister_control_handler();
201
/* SIGWINCH is generated in tty.c. No need to unregister anything. */
210
/* Nothing is registered for this signal. */
215
assert(0 && "Invalid signum");
221
int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
224
uv__handle_init(loop, (uv_handle_t*) handle, UV_SIGNAL);
225
handle->pending_signum = 0;
227
handle->signal_cb = NULL;
229
req = &handle->signal_req;
230
uv_req_init(loop, req);
231
req->type = UV_SIGNAL_REQ;
238
int uv_signal_stop(uv_signal_t* handle) {
239
uv_signal_t* removed_handle;
241
/* If the watcher wasn't started, this is a no-op. */
242
if (handle->signum == 0)
245
EnterCriticalSection(&uv__signal_lock);
247
uv__signal_unregister(handle->signum);
249
removed_handle = RB_REMOVE(uv_signal_tree_s, &uv__signal_tree, handle);
250
assert(removed_handle == handle);
252
LeaveCriticalSection(&uv__signal_lock);
255
uv__handle_stop(handle);
261
int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
264
/* If the user supplies signum == 0, then return an error already. If the */
265
/* signum is otherwise invalid then uv__signal_register will find out */
268
uv__set_artificial_error(handle->loop, UV_EINVAL);
272
/* Short circuit: if the signal watcher is already watching {signum} don't */
273
/* go through the process of deregistering and registering the handler. */
274
/* Additionally, this avoids pending signals getting lost in the (small) */
275
/* time frame that handle->signum == 0. */
276
if (signum == handle->signum) {
277
handle->signal_cb = signal_cb;
281
/* If the signal handler was already active, stop it first. */
282
if (handle->signum != 0) {
283
int r = uv_signal_stop(handle);
284
/* uv_signal_stop is infallible. */
288
EnterCriticalSection(&uv__signal_lock);
290
err = uv__signal_register(signum);
291
if (err.code != UV_OK) {
292
/* Uh-oh, didn't work. */
293
handle->loop->last_err = err;
294
LeaveCriticalSection(&uv__signal_lock);
298
handle->signum = signum;
299
RB_INSERT(uv_signal_tree_s, &uv__signal_tree, handle);
301
LeaveCriticalSection(&uv__signal_lock);
303
handle->signal_cb = signal_cb;
304
uv__handle_start(handle);
310
void uv_process_signal_req(uv_loop_t* loop, uv_signal_t* handle,
312
unsigned long dispatched_signum;
314
assert(handle->type == UV_SIGNAL);
315
assert(req->type == UV_SIGNAL_REQ);
317
dispatched_signum = InterlockedExchange(&handle->pending_signum, 0);
318
assert(dispatched_signum != 0);
320
/* Check if the pending signal equals the signum that we are watching for. */
321
/* These can get out of sync when the handler is stopped and restarted */
322
/* while the signal_req is pending. */
323
if (dispatched_signum == handle->signum)
324
handle->signal_cb(handle, dispatched_signum);
326
if (handle->flags & UV__HANDLE_CLOSING) {
327
/* When it is closing, it must be stopped at this point. */
328
assert(handle->signum == 0);
329
uv_want_endgame(loop, (uv_handle_t*) handle);
334
void uv_signal_close(uv_loop_t* loop, uv_signal_t* handle) {
335
uv_signal_stop(handle);
336
uv__handle_closing(handle);
338
if (handle->pending_signum == 0) {
339
uv_want_endgame(loop, (uv_handle_t*) handle);
344
void uv_signal_endgame(uv_loop_t* loop, uv_signal_t* handle) {
345
assert(handle->flags & UV__HANDLE_CLOSING);
346
assert(!(handle->flags & UV_HANDLE_CLOSED));
348
assert(handle->signum == 0);
349
assert(handle->pending_signum == 0);
351
handle->flags |= UV_HANDLE_CLOSED;
353
uv__handle_close(handle);