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:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
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
18  * IN THE SOFTWARE.
19  */
20 
21 #include <assert.h>
22 #include <signal.h>
23 
24 #include "uv.h"
25 #include "internal.h"
26 #include "handle-inl.h"
27 #include "req-inl.h"
28 
29 
30 RB_HEAD(uv_signal_tree_s, uv_signal_s);
31 
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;
35 
36 
uv_signals_init()37 void uv_signals_init() {
38   InitializeCriticalSection(&uv__signal_lock);
39 }
40 
41 
uv__signal_compare(uv_signal_t * w1,uv_signal_t * w2)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 */
44   /* adjacent. */
45   if (w1->signum < w2->signum) return -1;
46   if (w1->signum > w2->signum) return 1;
47 
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;
52 
53   if ((uintptr_t) w1 < (uintptr_t) w2) return -1;
54   if ((uintptr_t) w1 > (uintptr_t) w2) return 1;
55 
56   return 0;
57 }
58 
59 
60 RB_GENERATE_STATIC(uv_signal_tree_s, uv_signal_s, tree_entry, uv__signal_compare);
61 
62 
63 /*
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.
67  */
uv__signal_dispatch(int signum)68 int uv__signal_dispatch(int signum) {
69   uv_signal_t lookup;
70   uv_signal_t* handle;
71   int dispatched = 0;
72 
73   EnterCriticalSection(&uv__signal_lock);
74 
75   lookup.signum = signum;
76   lookup.loop = NULL;
77 
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);
82 
83     if (!previous) {
84       POST_COMPLETION_FOR_REQ(handle->loop, &handle->signal_req);
85     }
86 
87     dispatched = 1;
88   }
89 
90   LeaveCriticalSection(&uv__signal_lock);
91 
92   return dispatched;
93 }
94 
95 
uv__signal_control_handler(DWORD type)96 static BOOL WINAPI uv__signal_control_handler(DWORD type) {
97   switch (type) {
98     case CTRL_C_EVENT:
99       return uv__signal_dispatch(SIGINT);
100 
101     case CTRL_BREAK_EVENT:
102       return uv__signal_dispatch(SIGBREAK);
103 
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. */
110         Sleep(INFINITE);
111         return TRUE;
112       }
113       return FALSE;
114 
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. */
119 
120     default:
121       /* We don't handle these. */
122       return FALSE;
123   }
124 }
125 
126 
uv__signal_register_control_handler()127 static int uv__signal_register_control_handler() {
128   /* When this function is called, the uv__signal_lock must be held. */
129 
130   /* If the console control handler has already been hooked, just add a */
131   /* reference. */
132   if (uv__signal_control_handler_refs > 0)
133     return 0;
134 
135   if (!SetConsoleCtrlHandler(uv__signal_control_handler, TRUE))
136     return GetLastError();
137 
138   uv__signal_control_handler_refs++;
139 
140   return 0;
141 }
142 
143 
uv__signal_unregister_control_handler()144 static void uv__signal_unregister_control_handler() {
145   /* When this function is called, the uv__signal_lock must be held. */
146   BOOL r;
147 
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--;
152     return;
153   }
154 
155   assert(uv__signal_control_handler_refs == 1);
156 
157   r = SetConsoleCtrlHandler(uv__signal_control_handler, FALSE);
158   /* This should never fail; if it does it is probably a bug in libuv. */
159   assert(r);
160 
161   uv__signal_control_handler_refs--;
162 }
163 
164 
uv__signal_register(int signum)165 static int uv__signal_register(int signum) {
166   switch (signum) {
167     case SIGINT:
168     case SIGBREAK:
169     case SIGHUP:
170       return uv__signal_register_control_handler();
171 
172     case SIGWINCH:
173       /* SIGWINCH is generated in tty.c. No need to register anything. */
174       return 0;
175 
176     case SIGILL:
177     case SIGABRT_COMPAT:
178     case SIGFPE:
179     case SIGSEGV:
180     case SIGTERM:
181     case SIGABRT:
182       /* Signal is never raised. */
183       return 0;
184 
185     default:
186       /* Invalid signal. */
187       return ERROR_INVALID_PARAMETER;
188   }
189 }
190 
191 
uv__signal_unregister(int signum)192 static void uv__signal_unregister(int signum) {
193   switch (signum) {
194     case SIGINT:
195     case SIGBREAK:
196     case SIGHUP:
197       uv__signal_unregister_control_handler();
198       return;
199 
200     case SIGWINCH:
201       /* SIGWINCH is generated in tty.c. No need to unregister anything. */
202       return;
203 
204     case SIGILL:
205     case SIGABRT_COMPAT:
206     case SIGFPE:
207     case SIGSEGV:
208     case SIGTERM:
209     case SIGABRT:
210       /* Nothing is registered for this signal. */
211       return;
212 
213     default:
214       /* Libuv bug. */
215       assert(0 && "Invalid signum");
216       return;
217   }
218 }
219 
220 
uv_signal_init(uv_loop_t * loop,uv_signal_t * handle)221 int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
222   uv_req_t* req;
223 
224   uv__handle_init(loop, (uv_handle_t*) handle, UV_SIGNAL);
225   handle->pending_signum = 0;
226   handle->signum = 0;
227   handle->signal_cb = NULL;
228 
229   req = &handle->signal_req;
230   uv_req_init(loop, req);
231   req->type = UV_SIGNAL_REQ;
232   req->data = handle;
233 
234   return 0;
235 }
236 
237 
uv_signal_stop(uv_signal_t * handle)238 int uv_signal_stop(uv_signal_t* handle) {
239   uv_signal_t* removed_handle;
240 
241   /* If the watcher wasn't started, this is a no-op. */
242   if (handle->signum == 0)
243     return 0;
244 
245   EnterCriticalSection(&uv__signal_lock);
246 
247   uv__signal_unregister(handle->signum);
248 
249   removed_handle = RB_REMOVE(uv_signal_tree_s, &uv__signal_tree, handle);
250   assert(removed_handle == handle);
251 
252   LeaveCriticalSection(&uv__signal_lock);
253 
254   handle->signum = 0;
255   uv__handle_stop(handle);
256 
257   return 0;
258 }
259 
260 
uv_signal_start(uv_signal_t * handle,uv_signal_cb signal_cb,int signum)261 int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
262   int err;
263 
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 */
266   /* eventually. */
267   if (signum == 0) {
268     return UV_EINVAL;
269   }
270 
271   /* Short circuit: if the signal watcher is already watching {signum} don't */
272   /* go through the process of deregistering and registering the handler. */
273   /* Additionally, this avoids pending signals getting lost in the (small) */
274   /* time frame that handle->signum == 0. */
275   if (signum == handle->signum) {
276     handle->signal_cb = signal_cb;
277     return 0;
278   }
279 
280   /* If the signal handler was already active, stop it first. */
281   if (handle->signum != 0) {
282     int r = uv_signal_stop(handle);
283     /* uv_signal_stop is infallible. */
284     assert(r == 0);
285   }
286 
287   EnterCriticalSection(&uv__signal_lock);
288 
289   err = uv__signal_register(signum);
290   if (err) {
291     /* Uh-oh, didn't work. */
292     LeaveCriticalSection(&uv__signal_lock);
293     return uv_translate_sys_error(err);
294   }
295 
296   handle->signum = signum;
297   RB_INSERT(uv_signal_tree_s, &uv__signal_tree, handle);
298 
299   LeaveCriticalSection(&uv__signal_lock);
300 
301   handle->signal_cb = signal_cb;
302   uv__handle_start(handle);
303 
304   return 0;
305 }
306 
307 
uv_process_signal_req(uv_loop_t * loop,uv_signal_t * handle,uv_req_t * req)308 void uv_process_signal_req(uv_loop_t* loop, uv_signal_t* handle,
309     uv_req_t* req) {
310   unsigned long dispatched_signum;
311 
312   assert(handle->type == UV_SIGNAL);
313   assert(req->type == UV_SIGNAL_REQ);
314 
315   dispatched_signum = InterlockedExchange(&handle->pending_signum, 0);
316   assert(dispatched_signum != 0);
317 
318   /* Check if the pending signal equals the signum that we are watching for. */
319   /* These can get out of sync when the handler is stopped and restarted */
320   /* while the signal_req is pending. */
321   if (dispatched_signum == handle->signum)
322     handle->signal_cb(handle, dispatched_signum);
323 
324   if (handle->flags & UV__HANDLE_CLOSING) {
325     /* When it is closing, it must be stopped at this point. */
326     assert(handle->signum == 0);
327     uv_want_endgame(loop, (uv_handle_t*) handle);
328   }
329 }
330 
331 
uv_signal_close(uv_loop_t * loop,uv_signal_t * handle)332 void uv_signal_close(uv_loop_t* loop, uv_signal_t* handle) {
333   uv_signal_stop(handle);
334   uv__handle_closing(handle);
335 
336   if (handle->pending_signum == 0) {
337     uv_want_endgame(loop, (uv_handle_t*) handle);
338   }
339 }
340 
341 
uv_signal_endgame(uv_loop_t * loop,uv_signal_t * handle)342 void uv_signal_endgame(uv_loop_t* loop, uv_signal_t* handle) {
343   assert(handle->flags & UV__HANDLE_CLOSING);
344   assert(!(handle->flags & UV_HANDLE_CLOSED));
345 
346   assert(handle->signum == 0);
347   assert(handle->pending_signum == 0);
348 
349   handle->flags |= UV_HANDLE_CLOSED;
350 
351   uv__handle_close(handle);
352 }
353