~ubuntu-branches/ubuntu/saucy/nodejs/saucy-proposed

« back to all changes in this revision

Viewing changes to deps/uv/src/win/signal.c

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
37
void uv_signals_init() {
 
38
  InitializeCriticalSection(&uv__signal_lock);
 
39
}
 
40
 
 
41
 
 
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
 */
 
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
 
 
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
 
 
127
static uv_err_t 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 uv_ok_;
 
134
 
 
135
  if (!SetConsoleCtrlHandler(uv__signal_control_handler, TRUE))
 
136
    return uv__new_sys_error(GetLastError());
 
137
 
 
138
  uv__signal_control_handler_refs++;
 
139
 
 
140
  return uv_ok_;
 
141
}
 
142
 
 
143
 
 
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
 
 
165
static uv_err_t 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 uv_ok_;
 
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 uv_ok_;
 
184
 
 
185
    default:
 
186
      /* Invalid signal. */
 
187
      return uv__new_artificial_error(UV_EINVAL);
 
188
  }
 
189
}
 
190
 
 
191
 
 
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
 
 
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
 
 
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
 
 
261
int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
 
262
  uv_err_t 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
    uv__set_artificial_error(handle->loop, UV_EINVAL);
 
269
    return -1;
 
270
  }
 
271
 
 
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;
 
278
    return 0;
 
279
  }
 
280
 
 
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. */
 
285
    assert(r == 0);
 
286
  }
 
287
 
 
288
  EnterCriticalSection(&uv__signal_lock);
 
289
 
 
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);
 
295
    return -1;
 
296
  }
 
297
 
 
298
  handle->signum = signum;
 
299
  RB_INSERT(uv_signal_tree_s, &uv__signal_tree, handle);
 
300
 
 
301
  LeaveCriticalSection(&uv__signal_lock);
 
302
 
 
303
  handle->signal_cb = signal_cb;
 
304
  uv__handle_start(handle);
 
305
 
 
306
  return 0;
 
307
}
 
308
 
 
309
 
 
310
void uv_process_signal_req(uv_loop_t* loop, uv_signal_t* handle,
 
311
    uv_req_t* req) {
 
312
  unsigned long dispatched_signum;
 
313
 
 
314
  assert(handle->type == UV_SIGNAL);
 
315
  assert(req->type == UV_SIGNAL_REQ);
 
316
 
 
317
  dispatched_signum = InterlockedExchange(&handle->pending_signum, 0);
 
318
  assert(dispatched_signum != 0);
 
319
 
 
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);
 
325
 
 
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);
 
330
  }
 
331
}
 
332
 
 
333
 
 
334
void uv_signal_close(uv_loop_t* loop, uv_signal_t* handle) {
 
335
  uv_signal_stop(handle);
 
336
  uv__handle_closing(handle);
 
337
 
 
338
  if (handle->pending_signum == 0) {
 
339
    uv_want_endgame(loop, (uv_handle_t*) handle);
 
340
  }
 
341
}
 
342
 
 
343
 
 
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));
 
347
 
 
348
  assert(handle->signum == 0);
 
349
  assert(handle->pending_signum == 0);
 
350
 
 
351
  handle->flags |= UV_HANDLE_CLOSED;
 
352
 
 
353
  uv__handle_close(handle);
 
354
}