xref: /third_party/node/deps/uv/src/win/signal.c (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
21cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
31cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
41cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the
51cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
61cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
71cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
81cb0ef41Sopenharmony_ci *
91cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
101cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software.
111cb0ef41Sopenharmony_ci *
121cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
131cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
141cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
151cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
161cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
171cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
181cb0ef41Sopenharmony_ci * IN THE SOFTWARE.
191cb0ef41Sopenharmony_ci */
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci#include <assert.h>
221cb0ef41Sopenharmony_ci#include <signal.h>
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci#include "uv.h"
251cb0ef41Sopenharmony_ci#include "internal.h"
261cb0ef41Sopenharmony_ci#include "handle-inl.h"
271cb0ef41Sopenharmony_ci#include "req-inl.h"
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciRB_HEAD(uv_signal_tree_s, uv_signal_s);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cistatic struct uv_signal_tree_s uv__signal_tree = RB_INITIALIZER(uv__signal_tree);
331cb0ef41Sopenharmony_cistatic CRITICAL_SECTION uv__signal_lock;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cistatic BOOL WINAPI uv__signal_control_handler(DWORD type);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciint uv__signal_start(uv_signal_t* handle,
381cb0ef41Sopenharmony_ci                     uv_signal_cb signal_cb,
391cb0ef41Sopenharmony_ci                     int signum,
401cb0ef41Sopenharmony_ci                     int oneshot);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_civoid uv__signals_init(void) {
431cb0ef41Sopenharmony_ci  InitializeCriticalSection(&uv__signal_lock);
441cb0ef41Sopenharmony_ci  if (!SetConsoleCtrlHandler(uv__signal_control_handler, TRUE))
451cb0ef41Sopenharmony_ci    abort();
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_civoid uv__signal_cleanup(void) {
501cb0ef41Sopenharmony_ci  /* TODO(bnoordhuis) Undo effects of uv_signal_init()? */
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cistatic int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2) {
551cb0ef41Sopenharmony_ci  /* Compare signums first so all watchers with the same signnum end up
561cb0ef41Sopenharmony_ci   * adjacent. */
571cb0ef41Sopenharmony_ci  if (w1->signum < w2->signum) return -1;
581cb0ef41Sopenharmony_ci  if (w1->signum > w2->signum) return 1;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  /* Sort by loop pointer, so we can easily look up the first item after
611cb0ef41Sopenharmony_ci   * { .signum = x, .loop = NULL }. */
621cb0ef41Sopenharmony_ci  if ((uintptr_t) w1->loop < (uintptr_t) w2->loop) return -1;
631cb0ef41Sopenharmony_ci  if ((uintptr_t) w1->loop > (uintptr_t) w2->loop) return 1;
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  if ((uintptr_t) w1 < (uintptr_t) w2) return -1;
661cb0ef41Sopenharmony_ci  if ((uintptr_t) w1 > (uintptr_t) w2) return 1;
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  return 0;
691cb0ef41Sopenharmony_ci}
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciRB_GENERATE_STATIC(uv_signal_tree_s, uv_signal_s, tree_entry, uv__signal_compare)
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci/*
761cb0ef41Sopenharmony_ci * Dispatches signal {signum} to all active uv_signal_t watchers in all loops.
771cb0ef41Sopenharmony_ci * Returns 1 if the signal was dispatched to any watcher, or 0 if there were
781cb0ef41Sopenharmony_ci * no active signal watchers observing this signal.
791cb0ef41Sopenharmony_ci */
801cb0ef41Sopenharmony_ciint uv__signal_dispatch(int signum) {
811cb0ef41Sopenharmony_ci  uv_signal_t lookup;
821cb0ef41Sopenharmony_ci  uv_signal_t* handle;
831cb0ef41Sopenharmony_ci  int dispatched;
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  dispatched = 0;
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  EnterCriticalSection(&uv__signal_lock);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  lookup.signum = signum;
901cb0ef41Sopenharmony_ci  lookup.loop = NULL;
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  for (handle = RB_NFIND(uv_signal_tree_s, &uv__signal_tree, &lookup);
931cb0ef41Sopenharmony_ci       handle != NULL && handle->signum == signum;
941cb0ef41Sopenharmony_ci       handle = RB_NEXT(uv_signal_tree_s, &uv__signal_tree, handle)) {
951cb0ef41Sopenharmony_ci    unsigned long previous = InterlockedExchange(
961cb0ef41Sopenharmony_ci            (volatile LONG*) &handle->pending_signum, signum);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci    if (handle->flags & UV_SIGNAL_ONE_SHOT_DISPATCHED)
991cb0ef41Sopenharmony_ci      continue;
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci    if (!previous) {
1021cb0ef41Sopenharmony_ci      POST_COMPLETION_FOR_REQ(handle->loop, &handle->signal_req);
1031cb0ef41Sopenharmony_ci    }
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci    dispatched = 1;
1061cb0ef41Sopenharmony_ci    if (handle->flags & UV_SIGNAL_ONE_SHOT)
1071cb0ef41Sopenharmony_ci      handle->flags |= UV_SIGNAL_ONE_SHOT_DISPATCHED;
1081cb0ef41Sopenharmony_ci  }
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  LeaveCriticalSection(&uv__signal_lock);
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci  return dispatched;
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_cistatic BOOL WINAPI uv__signal_control_handler(DWORD type) {
1171cb0ef41Sopenharmony_ci  switch (type) {
1181cb0ef41Sopenharmony_ci    case CTRL_C_EVENT:
1191cb0ef41Sopenharmony_ci      return uv__signal_dispatch(SIGINT);
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci    case CTRL_BREAK_EVENT:
1221cb0ef41Sopenharmony_ci      return uv__signal_dispatch(SIGBREAK);
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci    case CTRL_CLOSE_EVENT:
1251cb0ef41Sopenharmony_ci      if (uv__signal_dispatch(SIGHUP)) {
1261cb0ef41Sopenharmony_ci        /* Windows will terminate the process after the control handler
1271cb0ef41Sopenharmony_ci         * returns. After that it will just terminate our process. Therefore
1281cb0ef41Sopenharmony_ci         * block the signal handler so the main loop has some time to pick up
1291cb0ef41Sopenharmony_ci         * the signal and do something for a few seconds. */
1301cb0ef41Sopenharmony_ci        Sleep(INFINITE);
1311cb0ef41Sopenharmony_ci        return TRUE;
1321cb0ef41Sopenharmony_ci      }
1331cb0ef41Sopenharmony_ci      return FALSE;
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci    case CTRL_LOGOFF_EVENT:
1361cb0ef41Sopenharmony_ci    case CTRL_SHUTDOWN_EVENT:
1371cb0ef41Sopenharmony_ci      /* These signals are only sent to services. Services have their own
1381cb0ef41Sopenharmony_ci       * notification mechanism, so there's no point in handling these. */
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci    default:
1411cb0ef41Sopenharmony_ci      /* We don't handle these. */
1421cb0ef41Sopenharmony_ci      return FALSE;
1431cb0ef41Sopenharmony_ci  }
1441cb0ef41Sopenharmony_ci}
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ciint uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
1481cb0ef41Sopenharmony_ci  uv__handle_init(loop, (uv_handle_t*) handle, UV_SIGNAL);
1491cb0ef41Sopenharmony_ci  handle->pending_signum = 0;
1501cb0ef41Sopenharmony_ci  handle->signum = 0;
1511cb0ef41Sopenharmony_ci  handle->signal_cb = NULL;
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci  UV_REQ_INIT(&handle->signal_req, UV_SIGNAL_REQ);
1541cb0ef41Sopenharmony_ci  handle->signal_req.data = handle;
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci  return 0;
1571cb0ef41Sopenharmony_ci}
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ciint uv_signal_stop(uv_signal_t* handle) {
1611cb0ef41Sopenharmony_ci  uv_signal_t* removed_handle;
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci  /* If the watcher wasn't started, this is a no-op. */
1641cb0ef41Sopenharmony_ci  if (handle->signum == 0)
1651cb0ef41Sopenharmony_ci    return 0;
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci  EnterCriticalSection(&uv__signal_lock);
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci  removed_handle = RB_REMOVE(uv_signal_tree_s, &uv__signal_tree, handle);
1701cb0ef41Sopenharmony_ci  assert(removed_handle == handle);
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  LeaveCriticalSection(&uv__signal_lock);
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  handle->signum = 0;
1751cb0ef41Sopenharmony_ci  uv__handle_stop(handle);
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci  return 0;
1781cb0ef41Sopenharmony_ci}
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ciint uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
1821cb0ef41Sopenharmony_ci  return uv__signal_start(handle, signal_cb, signum, 0);
1831cb0ef41Sopenharmony_ci}
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ciint uv_signal_start_oneshot(uv_signal_t* handle,
1871cb0ef41Sopenharmony_ci                            uv_signal_cb signal_cb,
1881cb0ef41Sopenharmony_ci                            int signum) {
1891cb0ef41Sopenharmony_ci  return uv__signal_start(handle, signal_cb, signum, 1);
1901cb0ef41Sopenharmony_ci}
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ciint uv__signal_start(uv_signal_t* handle,
1941cb0ef41Sopenharmony_ci                            uv_signal_cb signal_cb,
1951cb0ef41Sopenharmony_ci                            int signum,
1961cb0ef41Sopenharmony_ci                            int oneshot) {
1971cb0ef41Sopenharmony_ci  /* Test for invalid signal values. */
1981cb0ef41Sopenharmony_ci  if (signum <= 0 || signum >= NSIG)
1991cb0ef41Sopenharmony_ci    return UV_EINVAL;
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci  /* Short circuit: if the signal watcher is already watching {signum} don't go
2021cb0ef41Sopenharmony_ci   * through the process of deregistering and registering the handler.
2031cb0ef41Sopenharmony_ci   * Additionally, this avoids pending signals getting lost in the (small) time
2041cb0ef41Sopenharmony_ci   * frame that handle->signum == 0. */
2051cb0ef41Sopenharmony_ci  if (signum == handle->signum) {
2061cb0ef41Sopenharmony_ci    handle->signal_cb = signal_cb;
2071cb0ef41Sopenharmony_ci    return 0;
2081cb0ef41Sopenharmony_ci  }
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci  /* If the signal handler was already active, stop it first. */
2111cb0ef41Sopenharmony_ci  if (handle->signum != 0) {
2121cb0ef41Sopenharmony_ci    int r = uv_signal_stop(handle);
2131cb0ef41Sopenharmony_ci    /* uv_signal_stop is infallible. */
2141cb0ef41Sopenharmony_ci    assert(r == 0);
2151cb0ef41Sopenharmony_ci  }
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci  EnterCriticalSection(&uv__signal_lock);
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci  handle->signum = signum;
2201cb0ef41Sopenharmony_ci  if (oneshot)
2211cb0ef41Sopenharmony_ci    handle->flags |= UV_SIGNAL_ONE_SHOT;
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci  RB_INSERT(uv_signal_tree_s, &uv__signal_tree, handle);
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci  LeaveCriticalSection(&uv__signal_lock);
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci  handle->signal_cb = signal_cb;
2281cb0ef41Sopenharmony_ci  uv__handle_start(handle);
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci  return 0;
2311cb0ef41Sopenharmony_ci}
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_civoid uv__process_signal_req(uv_loop_t* loop, uv_signal_t* handle,
2351cb0ef41Sopenharmony_ci    uv_req_t* req) {
2361cb0ef41Sopenharmony_ci  long dispatched_signum;
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci  assert(handle->type == UV_SIGNAL);
2391cb0ef41Sopenharmony_ci  assert(req->type == UV_SIGNAL_REQ);
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci  dispatched_signum = InterlockedExchange(
2421cb0ef41Sopenharmony_ci          (volatile LONG*) &handle->pending_signum, 0);
2431cb0ef41Sopenharmony_ci  assert(dispatched_signum != 0);
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ci  /* Check if the pending signal equals the signum that we are watching for.
2461cb0ef41Sopenharmony_ci   * These can get out of sync when the handler is stopped and restarted while
2471cb0ef41Sopenharmony_ci   * the signal_req is pending. */
2481cb0ef41Sopenharmony_ci  if (dispatched_signum == handle->signum)
2491cb0ef41Sopenharmony_ci    handle->signal_cb(handle, dispatched_signum);
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci  if (handle->flags & UV_SIGNAL_ONE_SHOT)
2521cb0ef41Sopenharmony_ci    uv_signal_stop(handle);
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci  if (handle->flags & UV_HANDLE_CLOSING) {
2551cb0ef41Sopenharmony_ci    /* When it is closing, it must be stopped at this point. */
2561cb0ef41Sopenharmony_ci    assert(handle->signum == 0);
2571cb0ef41Sopenharmony_ci    uv__want_endgame(loop, (uv_handle_t*) handle);
2581cb0ef41Sopenharmony_ci  }
2591cb0ef41Sopenharmony_ci}
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_civoid uv__signal_close(uv_loop_t* loop, uv_signal_t* handle) {
2631cb0ef41Sopenharmony_ci  uv_signal_stop(handle);
2641cb0ef41Sopenharmony_ci  uv__handle_closing(handle);
2651cb0ef41Sopenharmony_ci
2661cb0ef41Sopenharmony_ci  if (handle->pending_signum == 0) {
2671cb0ef41Sopenharmony_ci    uv__want_endgame(loop, (uv_handle_t*) handle);
2681cb0ef41Sopenharmony_ci  }
2691cb0ef41Sopenharmony_ci}
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_civoid uv__signal_endgame(uv_loop_t* loop, uv_signal_t* handle) {
2731cb0ef41Sopenharmony_ci  assert(handle->flags & UV_HANDLE_CLOSING);
2741cb0ef41Sopenharmony_ci  assert(!(handle->flags & UV_HANDLE_CLOSED));
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci  assert(handle->signum == 0);
2771cb0ef41Sopenharmony_ci  assert(handle->pending_signum == 0);
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ci  handle->flags |= UV_HANDLE_CLOSED;
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci  uv__handle_close(handle);
2821cb0ef41Sopenharmony_ci}
283