uv: Upgrade to v0.10.18
[platform/upstream/nodejs.git] / deps / uv / src / unix / signal.c
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 "uv.h"
22 #include "internal.h"
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31
32 typedef struct {
33   uv_signal_t* handle;
34   int signum;
35 } uv__signal_msg_t;
36
37 RB_HEAD(uv__signal_tree_s, uv_signal_s);
38
39
40 static int uv__signal_unlock();
41 static void uv__signal_event(uv_loop_t* loop, uv__io_t* w, unsigned int events);
42 static int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2);
43 static void uv__signal_stop(uv_signal_t* handle);
44
45
46 static pthread_once_t uv__signal_global_init_guard = PTHREAD_ONCE_INIT;
47 static struct uv__signal_tree_s uv__signal_tree =
48     RB_INITIALIZER(uv__signal_tree);
49 static int uv__signal_lock_pipefd[2];
50
51
52 RB_GENERATE_STATIC(uv__signal_tree_s,
53                    uv_signal_s, tree_entry,
54                    uv__signal_compare)
55
56
57 static void uv__signal_global_init(void) {
58   if (uv__make_pipe(uv__signal_lock_pipefd, 0))
59     abort();
60
61   if (uv__signal_unlock())
62     abort();
63 }
64
65
66 void uv__signal_global_once_init(void) {
67   pthread_once(&uv__signal_global_init_guard, uv__signal_global_init);
68 }
69
70
71
72 static int uv__signal_lock(void) {
73   int r;
74   char data;
75
76   do {
77     r = read(uv__signal_lock_pipefd[0], &data, sizeof data);
78   } while (r < 0 && errno == EINTR);
79
80   return (r < 0) ? -1 : 0;
81 }
82
83
84 static int uv__signal_unlock(void) {
85   int r;
86   char data = 42;
87
88   do {
89     r = write(uv__signal_lock_pipefd[1], &data, sizeof data);
90   } while (r < 0 && errno == EINTR);
91
92   return (r < 0) ? -1 : 0;
93 }
94
95
96 static void uv__signal_block_and_lock(sigset_t* saved_sigmask) {
97   sigset_t new_mask;
98
99   if (sigfillset(&new_mask))
100     abort();
101
102   if (pthread_sigmask(SIG_SETMASK, &new_mask, saved_sigmask))
103     abort();
104
105   if (uv__signal_lock())
106     abort();
107 }
108
109
110 static void uv__signal_unlock_and_unblock(sigset_t* saved_sigmask) {
111   if (uv__signal_unlock())
112     abort();
113
114   if (pthread_sigmask(SIG_SETMASK, saved_sigmask, NULL))
115     abort();
116 }
117
118
119 inline static uv_signal_t* uv__signal_first_handle(int signum) {
120   /* This function must be called with the signal lock held. */
121   uv_signal_t lookup;
122   uv_signal_t* handle;
123
124   lookup.signum = signum;
125   lookup.loop = NULL;
126
127   handle = RB_NFIND(uv__signal_tree_s, &uv__signal_tree, &lookup);
128
129   if (handle != NULL && handle->signum == signum)
130     return handle;
131
132   return NULL;
133 }
134
135
136 static void uv__signal_handler(int signum) {
137   uv__signal_msg_t msg;
138   uv_signal_t* handle;
139   int saved_errno;
140
141   saved_errno = errno;
142   memset(&msg, 0, sizeof msg);
143
144   if (uv__signal_lock()) {
145     errno = saved_errno;
146     return;
147   }
148
149   for (handle = uv__signal_first_handle(signum);
150        handle != NULL && handle->signum == signum;
151        handle = RB_NEXT(uv__signal_tree_s, &uv__signal_tree, handle)) {
152     int r;
153
154     msg.signum = signum;
155     msg.handle = handle;
156
157     /* write() should be atomic for small data chunks, so the entire message
158      * should be written at once. In theory the pipe could become full, in
159      * which case the user is out of luck.
160      */
161     do {
162       r = write(handle->loop->signal_pipefd[1], &msg, sizeof msg);
163     } while (r == -1 && errno == EINTR);
164
165     assert(r == sizeof msg ||
166            (r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)));
167
168     if (r != -1)
169       handle->caught_signals++;
170   }
171
172   uv__signal_unlock();
173   errno = saved_errno;
174 }
175
176
177 static uv_err_t uv__signal_register_handler(int signum) {
178   /* When this function is called, the signal lock must be held. */
179   struct sigaction sa;
180
181   /* XXX use a separate signal stack? */
182   memset(&sa, 0, sizeof(sa));
183   if (sigfillset(&sa.sa_mask))
184     abort();
185   sa.sa_handler = uv__signal_handler;
186
187   /* XXX save old action so we can restore it later on? */
188   if (sigaction(signum, &sa, NULL))
189     return uv__new_sys_error(errno);
190
191   return uv_ok_;
192 }
193
194
195 static void uv__signal_unregister_handler(int signum) {
196   /* When this function is called, the signal lock must be held. */
197   struct sigaction sa;
198
199   memset(&sa, 0, sizeof(sa));
200   sa.sa_handler = SIG_DFL;
201
202   /* sigaction can only fail with EINVAL or EFAULT; an attempt to deregister a
203    * signal implies that it was successfully registered earlier, so EINVAL
204    * should never happen.
205    */
206   if (sigaction(signum, &sa, NULL))
207     abort();
208 }
209
210
211 static int uv__signal_loop_once_init(uv_loop_t* loop) {
212   /* Return if already initialized. */
213   if (loop->signal_pipefd[0] != -1)
214     return 0;
215
216   if (uv__make_pipe(loop->signal_pipefd, UV__F_NONBLOCK))
217     return -1;
218
219   uv__io_init(&loop->signal_io_watcher,
220               uv__signal_event,
221               loop->signal_pipefd[0]);
222   uv__io_start(loop, &loop->signal_io_watcher, UV__POLLIN);
223
224   return 0;
225 }
226
227
228 void uv__signal_loop_cleanup(uv_loop_t* loop) {
229   ngx_queue_t* q;
230
231   /* Stop all the signal watchers that are still attached to this loop. This
232    * ensures that the (shared) signal tree doesn't contain any invalid entries
233    * entries, and that signal handlers are removed when appropriate.
234    */
235   ngx_queue_foreach(q, &loop->handle_queue) {
236     uv_handle_t* handle = ngx_queue_data(q, uv_handle_t, handle_queue);
237
238     if (handle->type == UV_SIGNAL)
239       uv__signal_stop((uv_signal_t*) handle);
240   }
241
242   if (loop->signal_pipefd[0] != -1) {
243     close(loop->signal_pipefd[0]);
244     loop->signal_pipefd[0] = -1;
245   }
246
247   if (loop->signal_pipefd[1] != -1) {
248     close(loop->signal_pipefd[1]);
249     loop->signal_pipefd[1] = -1;
250   }
251 }
252
253
254 int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
255   if (uv__signal_loop_once_init(loop))
256     return uv__set_sys_error(loop, errno);
257
258   uv__handle_init(loop, (uv_handle_t*) handle, UV_SIGNAL);
259   handle->signum = 0;
260   handle->caught_signals = 0;
261   handle->dispatched_signals = 0;
262
263   return 0;
264 }
265
266
267 void uv__signal_close(uv_signal_t* handle) {
268
269   uv__signal_stop(handle);
270
271   /* If there are any caught signals "trapped" in the signal pipe, we can't
272    * call the close callback yet. Otherwise, add the handle to the finish_close
273    * queue.
274    */
275   if (handle->caught_signals == handle->dispatched_signals) {
276     uv__make_close_pending((uv_handle_t*) handle);
277   }
278 }
279
280
281 int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
282   sigset_t saved_sigmask;
283
284   assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
285
286   /* If the user supplies signum == 0, then return an error already. If the
287    * signum is otherwise invalid then uv__signal_register will find out
288    * eventually.
289    */
290   if (signum == 0) {
291     uv__set_artificial_error(handle->loop, UV_EINVAL);
292     return -1;
293   }
294
295   /* Short circuit: if the signal watcher is already watching {signum} don't
296    * go through the process of deregistering and registering the handler.
297    * Additionally, this avoids pending signals getting lost in the small time
298    * time frame that handle->signum == 0.
299    */
300   if (signum == handle->signum) {
301     handle->signal_cb = signal_cb;
302     return 0;
303   }
304
305   /* If the signal handler was already active, stop it first. */
306   if (handle->signum != 0) {
307     uv__signal_stop(handle);
308   }
309
310   uv__signal_block_and_lock(&saved_sigmask);
311
312   /* If at this point there are no active signal watchers for this signum (in
313    * any of the loops), it's time to try and register a handler for it here.
314    */
315   if (uv__signal_first_handle(signum) == NULL) {
316     uv_err_t err = uv__signal_register_handler(signum);
317     if (err.code != UV_OK) {
318       /* Registering the signal handler failed. Must be an invalid signal. */
319       handle->loop->last_err = err;
320       uv__signal_unlock_and_unblock(&saved_sigmask);
321       return -1;
322     }
323   }
324
325   handle->signum = signum;
326   RB_INSERT(uv__signal_tree_s, &uv__signal_tree, handle);
327
328   uv__signal_unlock_and_unblock(&saved_sigmask);
329
330   handle->signal_cb = signal_cb;
331   uv__handle_start(handle);
332
333   return 0;
334 }
335
336
337 static void uv__signal_event(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
338   uv__signal_msg_t* msg;
339   uv_signal_t* handle;
340   char buf[sizeof(uv__signal_msg_t) * 32];
341   size_t bytes, end, i;
342   int r;
343
344   bytes = 0;
345
346   do {
347     r = read(loop->signal_pipefd[0], buf + bytes, sizeof(buf) - bytes);
348
349     if (r == -1 && errno == EINTR)
350       continue;
351
352     if (r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
353       /* If there are bytes in the buffer already (which really is extremely
354        * unlikely if possible at all) we can't exit the function here. We'll
355        * spin until more bytes are read instead.
356        */
357       if (bytes > 0)
358         continue;
359
360       /* Otherwise, there was nothing there. */
361       return;
362     }
363
364     /* Other errors really should never happen. */
365     if (r == -1)
366       abort();
367
368     bytes += r;
369
370     /* `end` is rounded down to a multiple of sizeof(uv__signal_msg_t). */
371     end = (bytes / sizeof(uv__signal_msg_t)) * sizeof(uv__signal_msg_t);
372
373     for (i = 0; i < end; i += sizeof(uv__signal_msg_t)) {
374       msg = (uv__signal_msg_t*) (buf + i);
375       handle = msg->handle;
376
377       if (msg->signum == handle->signum) {
378         assert(!(handle->flags & UV_CLOSING));
379         handle->signal_cb(handle, handle->signum);
380       }
381
382       handle->dispatched_signals++;
383
384       /* If uv_close was called while there were caught signals that were not
385        * yet dispatched, the uv__finish_close was deferred. Make close pending
386        * now if this has happened.
387        */
388       if ((handle->flags & UV_CLOSING) &&
389           (handle->caught_signals == handle->dispatched_signals)) {
390         uv__make_close_pending((uv_handle_t*) handle);
391       }
392     }
393
394     bytes -= end;
395
396     /* If there are any "partial" messages left, move them to the start of the
397      * the buffer, and spin. This should not happen.
398      */
399     if (bytes) {
400       memmove(buf, buf + end, bytes);
401       continue;
402     }
403   } while (end == sizeof buf);
404 }
405
406
407 static int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2) {
408   /* Compare signums first so all watchers with the same signnum end up
409    * adjacent.
410    */
411   if (w1->signum < w2->signum) return -1;
412   if (w1->signum > w2->signum) return 1;
413
414   /* Sort by loop pointer, so we can easily look up the first item after
415    * { .signum = x, .loop = NULL }.
416    */
417   if (w1->loop < w2->loop) return -1;
418   if (w1->loop > w2->loop) return 1;
419
420   if (w1 < w2) return -1;
421   if (w1 > w2) return 1;
422
423   return 0;
424 }
425
426
427 int uv_signal_stop(uv_signal_t* handle) {
428   assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
429   uv__signal_stop(handle);
430   return 0;
431 }
432
433
434 static void uv__signal_stop(uv_signal_t* handle) {
435   uv_signal_t* removed_handle;
436   sigset_t saved_sigmask;
437
438   /* If the watcher wasn't started, this is a no-op. */
439   if (handle->signum == 0)
440     return;
441
442   uv__signal_block_and_lock(&saved_sigmask);
443
444   removed_handle = RB_REMOVE(uv__signal_tree_s, &uv__signal_tree, handle);
445   assert(removed_handle == handle);
446   (void) removed_handle;
447
448   /* Check if there are other active signal watchers observing this signal. If
449    * not, unregister the signal handler.
450    */
451   if (uv__signal_first_handle(handle->signum) == NULL)
452     uv__signal_unregister_handler(handle->signum);
453
454   uv__signal_unlock_and_unblock(&saved_sigmask);
455
456   handle->signum = 0;
457   uv__handle_stop(handle);
458 }