summaryrefslogtreecommitdiff
path: root/deps/uv/src/unix/signal.c
diff options
context:
space:
mode:
authorBert Belder <bertbelder@gmail.com>2012-10-17 18:33:40 +0200
committerBert Belder <bertbelder@gmail.com>2012-10-17 18:33:45 +0200
commitc11c19b92cb38532b1892c54ae25502873b14699 (patch)
treec86112a45643c6d098b1a5c04d43bab401189ead /deps/uv/src/unix/signal.c
parent4b238b4c2ae8f5cf409cea5ca214b480b00e1a86 (diff)
downloadandroid-node-v8-c11c19b92cb38532b1892c54ae25502873b14699.tar.gz
android-node-v8-c11c19b92cb38532b1892c54ae25502873b14699.tar.bz2
android-node-v8-c11c19b92cb38532b1892c54ae25502873b14699.zip
uv: upgrade to 1e32cb0
Diffstat (limited to 'deps/uv/src/unix/signal.c')
-rw-r--r--deps/uv/src/unix/signal.c500
1 files changed, 342 insertions, 158 deletions
diff --git a/deps/uv/src/unix/signal.c b/deps/uv/src/unix/signal.c
index 8ef640b6fa..45555b52b7 100644
--- a/deps/uv/src/unix/signal.c
+++ b/deps/uv/src/unix/signal.c
@@ -22,248 +22,432 @@
#include "internal.h"
#include <assert.h>
+#include <errno.h>
+#include <signal.h>
#include <stdlib.h>
#include <string.h>
-#include <signal.h>
#include <unistd.h>
-#include <errno.h>
-struct signal_ctx {
- int pipefd[2];
- uv__io_t io_watcher;
- unsigned int nqueues;
- ngx_queue_t queues[1]; /* variable length */
-};
-static void uv__signal_handler(int signum);
-static void uv__signal_event(uv_loop_t* loop, uv__io_t* w, int events);
-static struct signal_ctx* uv__signal_ctx_new(uv_loop_t* loop);
-static void uv__signal_ctx_delete(struct signal_ctx* ctx);
-static void uv__signal_write(int fd, unsigned int val);
-static unsigned int uv__signal_read(int fd);
-static unsigned int uv__signal_max(void);
+typedef struct {
+ uv_signal_t* handle;
+ int signum;
+} uv__signal_msg_t;
+RB_HEAD(uv__signal_tree_s, uv_signal_s);
-int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
- uv__handle_init(loop, (uv_handle_t*)handle, UV_SIGNAL);
- handle->signum = 0;
- return 0;
+
+static int uv__signal_unlock();
+static void uv__signal_event(uv_loop_t* loop, uv__io_t* watcher, int events);
+static int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2);
+static void uv__signal_stop(uv_signal_t* handle);
+
+
+static pthread_once_t uv__signal_global_init_guard = PTHREAD_ONCE_INIT;
+static struct uv__signal_tree_s uv__signal_tree =
+ RB_INITIALIZER(uv__signal_tree);
+static int uv__signal_lock_pipefd[2];
+
+
+RB_GENERATE_STATIC(uv__signal_tree_s,
+ uv_signal_s, tree_entry,
+ uv__signal_compare)
+
+
+static void uv__signal_global_init() {
+ if (uv__make_pipe(uv__signal_lock_pipefd, 0))
+ abort();
+
+ if (uv__signal_unlock())
+ abort();
}
-int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum_) {
- struct signal_ctx* ctx;
- struct sigaction sa;
- unsigned int signum;
- uv_loop_t* loop;
- ngx_queue_t* q;
+void uv__signal_global_once_init(void) {
+ pthread_once(&uv__signal_global_init_guard, uv__signal_global_init);
+}
- /* XXX doing this check in uv_signal_init() - the logical place for it -
- * leads to an infinite loop when uv__loop_init() inits a signal watcher
- */
- /* FIXME */
- assert(handle->loop == uv_default_loop() &&
- "uv_signal_t is currently only supported by the default loop");
- loop = handle->loop;
- signum = signum_;
- if (uv__is_active(handle))
- return uv__set_artificial_error(loop, UV_EBUSY);
+static int uv__signal_lock() {
+ int r;
+ char data;
+
+ do {
+ r = read(uv__signal_lock_pipefd[0], &data, sizeof data);
+ } while (r < 0 && errno == EINTR);
+
+ return (r < 0) ? -1 : 0;
+}
+
- if (signal_cb == NULL)
- return uv__set_artificial_error(loop, UV_EINVAL);
+static int uv__signal_unlock() {
+ int r;
+ char data = 42;
- if (signum <= 0)
- return uv__set_artificial_error(loop, UV_EINVAL);
+ do {
+ r = write(uv__signal_lock_pipefd[1], &data, sizeof data);
+ } while (r < 0 && errno == EINTR);
+
+ return (r < 0) ? -1 : 0;
+}
- ctx = loop->signal_ctx;
- if (ctx == NULL) {
- ctx = uv__signal_ctx_new(loop);
+static void uv__signal_block_and_lock(sigset_t* saved_sigmask) {
+ sigset_t new_mask;
- if (ctx == NULL)
- return uv__set_artificial_error(loop, UV_ENOMEM);
+ if (sigfillset(&new_mask))
+ abort();
+
+ if (pthread_sigmask(SIG_SETMASK, &new_mask, saved_sigmask))
+ abort();
+
+ if (uv__signal_lock())
+ abort();
+}
- loop->signal_ctx = ctx;
+
+static void uv__signal_unlock_and_unblock(sigset_t* saved_sigmask) {
+ if (uv__signal_unlock())
+ abort();
+
+ if (pthread_sigmask(SIG_SETMASK, saved_sigmask, NULL))
+ abort();
+}
+
+
+inline static uv_signal_t* uv__signal_first_handle(int signum) {
+ /* This function must be called with the signal lock held. */
+ uv_signal_t lookup;
+ uv_signal_t* handle;
+
+ lookup.signum = signum;
+ lookup.loop = NULL;
+
+ handle = RB_NFIND(uv__signal_tree_s, &uv__signal_tree, &lookup);
+
+ if (handle != NULL && handle->signum == signum)
+ return handle;
+
+ return NULL;
+}
+
+
+void uv__signal_handler(int signum) {
+ uv__signal_msg_t msg;
+ uv_signal_t* handle;
+
+ memset(&msg, 0, sizeof msg);
+
+ uv__signal_lock();
+
+ for (handle = uv__signal_first_handle(signum);
+ handle != NULL && handle->signum == signum;
+ handle = RB_NEXT(uv__signal_tree_s, &uv__signal_tree, handle)) {
+ int r;
+
+ msg.signum = signum;
+ msg.handle = handle;
+
+ /* write() should be atomic for small data chunks, so the entire message
+ * should be written at once. In theory the pipe could become full, in
+ * which case the user is out of luck.
+ */
+ do {
+ r = write(handle->loop->signal_pipefd[1], &msg, sizeof msg);
+ } while (r == -1 && errno == EINTR);
+
+ assert(r == sizeof msg ||
+ (r == -1 && errno != EAGAIN && errno != EWOULDBLOCK));
+
+ if (r != -1)
+ handle->caught_signals++;
}
- if (signum > ctx->nqueues)
- return uv__set_artificial_error(loop, UV_EINVAL);
+ uv__signal_unlock();
+}
- q = ctx->queues + signum;
- if (!ngx_queue_empty(q))
- goto skip;
+static uv_err_t uv__signal_register_handler(int signum) {
+ /* When this function is called, the signal lock must be held. */
+ struct sigaction sa;
/* XXX use a separate signal stack? */
memset(&sa, 0, sizeof(sa));
+ if (sigfillset(&sa.sa_mask))
+ abort();
sa.sa_handler = uv__signal_handler;
/* XXX save old action so we can restore it later on? */
if (sigaction(signum, &sa, NULL))
- return uv__set_artificial_error(loop, UV_EINVAL);
+ return uv__new_sys_error(errno);
-skip:
- ngx_queue_insert_tail(q, &handle->queue);
- uv__handle_start(handle);
- handle->signum = signum;
- handle->signal_cb = signal_cb;
-
- return 0;
+ return uv_ok_;
}
-int uv_signal_stop(uv_signal_t* handle) {
- struct signal_ctx* ctx;
+static void uv__signal_unregister_handler(int signum) {
+ /* When this function is called, the signal lock must be held. */
struct sigaction sa;
- unsigned int signum;
- uv_loop_t* loop;
+ int r;
- if (!uv__is_active(handle))
- return 0;
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = SIG_DFL;
- signum = handle->signum;
- loop = handle->loop;
- ctx = loop->signal_ctx;
- assert(signum > 0);
- assert(signum <= ctx->nqueues);
+ r = sigaction(signum, &sa, NULL);
+ /* sigaction can only fail with EINVAL or EFAULT; an attempt to deregister a
+ * signal implies that it was successfully registered earlier, so EINVAL
+ * should never happen.
+ */
+ assert(r == 0);
+}
- ngx_queue_remove(&handle->queue);
- uv__handle_stop(handle);
- handle->signum = 0;
- if (!ngx_queue_empty(ctx->queues + signum))
- goto skip;
+static int uv__signal_loop_once_init(uv_loop_t* loop) {
+ /* Return if already initialized. */
+ if (loop->signal_pipefd[0] != -1)
+ return 0;
- memset(&sa, 0, sizeof(sa));
- sa.sa_handler = SIG_DFL; /* XXX restore previous action? */
+ if (uv__make_pipe(loop->signal_pipefd, UV__F_NONBLOCK))
+ return -1;
- if (sigaction(signum, &sa, NULL))
- return uv__set_artificial_error(loop, UV_EINVAL);
+ uv__io_init(&loop->signal_io_watcher,
+ uv__signal_event,
+ loop->signal_pipefd[0],
+ UV__IO_READ);
+ uv__io_start(loop, &loop->signal_io_watcher);
-skip:
return 0;
}
-void uv__signal_close(uv_signal_t* handle) {
- uv_signal_stop(handle);
-}
+void uv__signal_loop_cleanup(uv_loop_t* loop) {
+ ngx_queue_t* q;
+ /* Stop all the signal watchers that are still attached to this loop. This
+ * ensures that the (shared) signal tree doesn't contain any invalid entries
+ * entries, and that signal handlers are removed when appropriate.
+ */
+ ngx_queue_foreach(q, &loop->handle_queue) {
+ uv_handle_t* handle = ngx_queue_data(q, uv_handle_t, handle_queue);
-void uv__signal_unregister(uv_loop_t* loop) {
- uv__signal_ctx_delete(loop->signal_ctx);
- loop->signal_ctx = NULL;
+ if (handle->type == UV_SIGNAL)
+ uv__signal_stop((uv_signal_t*) handle);
+ }
+
+ if (loop->signal_pipefd[0] != -1) {
+ close(loop->signal_pipefd[0]);
+ loop->signal_pipefd[0] = -1;
+ }
+
+ if (loop->signal_pipefd[1] != -1) {
+ close(loop->signal_pipefd[1]);
+ loop->signal_pipefd[1] = -1;
+ }
}
-static void uv__signal_handler(int signum) {
- struct signal_ctx* ctx = uv_default_loop()->signal_ctx;
- uv__signal_write(ctx->pipefd[1], (unsigned int) signum);
+int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
+ if (uv__signal_loop_once_init(loop))
+ return uv__set_sys_error(loop, errno);
+
+ uv__handle_init(loop, (uv_handle_t*) handle, UV_SIGNAL);
+ handle->signum = 0;
+ handle->caught_signals = 0;
+ handle->dispatched_signals = 0;
+
+ return 0;
}
-static void uv__signal_event(uv_loop_t* loop, uv__io_t* w, int events) {
- struct signal_ctx* ctx;
- unsigned int signum;
- uv_signal_t* h;
- ngx_queue_t* q;
+void uv__signal_close(uv_signal_t* handle) {
- ctx = container_of(w, struct signal_ctx, io_watcher);
- signum = uv__signal_read(ctx->pipefd[0]);
- assert(signum > 0);
- assert(signum <= ctx->nqueues);
+ uv__signal_stop(handle);
- ngx_queue_foreach(q, ctx->queues + signum) {
- h = ngx_queue_data(q, uv_signal_t, queue);
- h->signal_cb(h, signum);
+ /* If there are any caught signals "trapped" in the signal pipe, we can't
+ * call the close callback yet. Otherwise, add the handle to the finish_close
+ * queue.
+ */
+ if (handle->caught_signals == handle->dispatched_signals) {
+ uv__make_close_pending((uv_handle_t*) handle);
}
}
-static struct signal_ctx* uv__signal_ctx_new(uv_loop_t* loop) {
- struct signal_ctx* ctx;
- unsigned int nqueues;
- unsigned int i;
+int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
+ sigset_t saved_sigmask;
+
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
- nqueues = uv__signal_max();
- assert(nqueues > 0);
+ /* If the user supplies signum == 0, then return an error already. If the
+ * signum is otherwise invalid then uv__signal_register will find out
+ * eventually.
+ */
+ if (signum == 0) {
+ uv__set_artificial_error(handle->loop, UV_EINVAL);
+ return -1;
+ }
- /* The first ctx->queues entry is never used. It wastes a few bytes of memory
- * but it saves us from having to substract 1 from the signum all the time -
- * which inevitably someone will forget to do.
+ /* Short circuit: if the signal watcher is already watching {signum} don't
+ * go through the process of deregistering and registering the handler.
+ * Additionally, this avoids pending signals getting lost in the small time
+ * time frame that handle->signum == 0.
*/
- ctx = calloc(1, sizeof(*ctx) + sizeof(ctx->queues[0]) * (nqueues + 1));
- if (ctx == NULL)
- return NULL;
+ if (signum == handle->signum) {
+ handle->signal_cb = signal_cb;
+ return 0;
+ }
+
+ /* If the signal handler was already active, stop it first. */
+ if (handle->signum != 0) {
+ uv__signal_stop(handle);
+ }
+
+ uv__signal_block_and_lock(&saved_sigmask);
- if (uv__make_pipe(ctx->pipefd, UV__F_NONBLOCK)) {
- free(ctx);
- return NULL;
+ /* If at this point there are no active signal watchers for this signum (in
+ * any of the loops), it's time to try and register a handler for it here.
+ */
+ if (uv__signal_first_handle(signum) == NULL) {
+ uv_err_t err = uv__signal_register_handler(signum);
+ if (err.code != UV_OK) {
+ /* Registering the signal handler failed. Must be an invalid signal. */
+ handle->loop->last_err = err;
+ uv__signal_unlock_and_unblock(&saved_sigmask);
+ return -1;
+ }
}
- uv__io_init(&ctx->io_watcher, uv__signal_event, ctx->pipefd[0], UV__IO_READ);
- uv__io_start(loop, &ctx->io_watcher);
- ctx->nqueues = nqueues;
+ handle->signum = signum;
+ RB_INSERT(uv__signal_tree_s, &uv__signal_tree, handle);
- for (i = 1; i <= nqueues; i++)
- ngx_queue_init(ctx->queues + i);
+ uv__signal_unlock_and_unblock(&saved_sigmask);
- return ctx;
+ handle->signal_cb = signal_cb;
+ uv__handle_start(handle);
+
+ return 0;
}
-static void uv__signal_ctx_delete(struct signal_ctx* ctx) {
- if (ctx == NULL) return;
- close(ctx->pipefd[0]);
- close(ctx->pipefd[1]);
- free(ctx);
+static void uv__signal_event(uv_loop_t* loop, uv__io_t* watcher, int events) {
+ uv__signal_msg_t* msg;
+ uv_signal_t* handle;
+ char buf[sizeof(uv__signal_msg_t) * 32];
+ size_t bytes, end, i;
+ int r;
+
+ bytes = 0;
+
+ do {
+ r = read(loop->signal_pipefd[0], buf + bytes, sizeof(buf) - bytes);
+
+ if (r == -1 && errno == EINTR)
+ continue;
+
+ if (r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
+ /* If there are bytes in the buffer already (which really is extremely
+ * unlikely if possible at all) we can't exit the function here. We'll
+ * spin until more bytes are read instead.
+ */
+ if (bytes > 0)
+ continue;
+
+ /* Otherwise, there was nothing there. */
+ return;
+ }
+
+ /* Other errors really should never happen. */
+ if (r == -1)
+ abort();
+
+ bytes += r;
+
+ /* `end` is rounded down to a multiple of sizeof(uv__signal_msg_t). */
+ end = (bytes / sizeof(uv__signal_msg_t)) * sizeof(uv__signal_msg_t);
+
+ for (i = 0; i < end; i += sizeof(uv__signal_msg_t)) {
+ msg = (uv__signal_msg_t*) (buf + i);
+ handle = msg->handle;
+
+ if (msg->signum == handle->signum) {
+ assert(!(handle->flags & UV_CLOSING));
+ handle->signal_cb(handle, handle->signum);
+ }
+
+ handle->dispatched_signals++;
+
+ /* If uv_close was called while there were caught signals that were not
+ * yet dispatched, the uv__finish_close was deferred. Make close pending
+ * now if this has happened.
+ */
+ if ((handle->flags & UV_CLOSING) &&
+ (handle->caught_signals == handle->dispatched_signals)) {
+ uv__make_close_pending((uv_handle_t*) handle);
+ }
+ }
+
+ bytes -= end;
+
+ /* If there are any "partial" messages left, move them to the start of the
+ * the buffer, and spin. This should not happen.
+ */
+ if (bytes) {
+ memmove(buf, buf + end, bytes);
+ continue;
+ }
+ } while (end == sizeof buf);
}
-static void uv__signal_write(int fd, unsigned int val) {
- ssize_t n;
+static int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2) {
+ /* Compare signums first so all watchers with the same signnum end up
+ * adjacent.
+ */
+ if (w1->signum < w2->signum) return -1;
+ if (w1->signum > w2->signum) return 1;
- do
- n = write(fd, &val, sizeof(val));
- while (n == -1 && errno == EINTR);
+ /* Sort by loop pointer, so we can easily look up the first item after
+ * { .signum = x, .loop = NULL }.
+ */
+ if (w1->loop < w2->loop) return -1;
+ if (w1->loop > w2->loop) return 1;
- if (n == sizeof(val))
- return;
+ if (w1 < w2) return -1;
+ if (w1 > w2) return 1;
- if (n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
- return; /* pipe full - nothing we can do about that */
+ return 0;
+}
- abort();
+
+int uv_signal_stop(uv_signal_t* handle) {
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
+ uv__signal_stop(handle);
+ return 0;
}
-static unsigned int uv__signal_read(int fd) {
- unsigned int val;
- ssize_t n;
+static void uv__signal_stop(uv_signal_t* handle) {
+ uv_signal_t* removed_handle;
+ sigset_t saved_sigmask;
- do
- n = read(fd, &val, sizeof(val));
- while (n == -1 && errno == EINTR);
+ /* If the watcher wasn't started, this is a no-op. */
+ if (handle->signum == 0)
+ return;
- if (n == sizeof(val))
- return val;
+ uv__signal_block_and_lock(&saved_sigmask);
- abort();
-}
+ removed_handle = RB_REMOVE(uv__signal_tree_s, &uv__signal_tree, handle);
+ assert(removed_handle == handle);
+
+ /* Check if there are other active signal watchers observing this signal. If
+ * not, unregister the signal handler.
+ */
+ if (uv__signal_first_handle(handle->signum) == NULL)
+ uv__signal_unregister_handler(handle->signum);
+ uv__signal_unlock_and_unblock(&saved_sigmask);
-static unsigned int uv__signal_max(void) {
-#if defined(_SC_RTSIG_MAX)
- int max = sysconf(_SC_RTSIG_MAX);
- if (max != -1) return max;
-#endif
-#if defined(SIGRTMAX)
- return SIGRTMAX;
-#elif defined(NSIG)
- return NSIG;
-#else
- return 32;
-#endif
+ handle->signum = 0;
+ uv__handle_stop(handle);
}