@@ -0,0 +1,269 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "uv.h"
#include "internal.h"

#include <assert.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);


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;
}


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;

/* 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);

if (signal_cb == NULL)
return uv__set_artificial_error(loop, UV_EINVAL);

if (signum <= 0)
return uv__set_artificial_error(loop, UV_EINVAL);

ctx = loop->signal_ctx;

if (ctx == NULL) {
ctx = uv__signal_ctx_new(loop);

if (ctx == NULL)
return uv__set_artificial_error(loop, UV_ENOMEM);

loop->signal_ctx = ctx;
}

if (signum > ctx->nqueues)
return uv__set_artificial_error(loop, UV_EINVAL);

q = ctx->queues + signum;

if (!ngx_queue_empty(q))
goto skip;

/* XXX use a separate signal stack? */
memset(&sa, 0, sizeof(sa));
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);

skip:
ngx_queue_insert_tail(q, &handle->queue);
uv__handle_start(handle);
handle->signum = signum;
handle->signal_cb = signal_cb;

return 0;
}


int uv_signal_stop(uv_signal_t* handle) {
struct signal_ctx* ctx;
struct sigaction sa;
unsigned int signum;
uv_loop_t* loop;

if (!uv__is_active(handle))
return 0;

signum = handle->signum;
loop = handle->loop;
ctx = loop->signal_ctx;
assert(signum > 0);
assert(signum <= ctx->nqueues);

ngx_queue_remove(&handle->queue);
uv__handle_stop(handle);
handle->signum = 0;

if (!ngx_queue_empty(ctx->queues + signum))
goto skip;

memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL; /* XXX restore previous action? */

if (sigaction(signum, &sa, NULL))
return uv__set_artificial_error(loop, UV_EINVAL);

skip:
return 0;
}


void uv__signal_close(uv_signal_t* handle) {
uv_signal_stop(handle);
}


void uv__signal_unregister(uv_loop_t* loop) {
uv__signal_ctx_delete(loop->signal_ctx);
loop->signal_ctx = NULL;
}


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);
}


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;

ctx = container_of(w, struct signal_ctx, io_watcher);
signum = uv__signal_read(ctx->pipefd[0]);
assert(signum > 0);
assert(signum <= ctx->nqueues);

ngx_queue_foreach(q, ctx->queues + signum) {
h = ngx_queue_data(q, uv_signal_t, queue);
h->signal_cb(h, signum);
}
}


static struct signal_ctx* uv__signal_ctx_new(uv_loop_t* loop) {
struct signal_ctx* ctx;
unsigned int nqueues;
unsigned int i;

nqueues = uv__signal_max();
assert(nqueues > 0);

/* 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.
*/
ctx = calloc(1, sizeof(*ctx) + sizeof(ctx->queues[0]) * (nqueues + 1));
if (ctx == NULL)
return NULL;

if (uv__make_pipe(ctx->pipefd, UV__F_NONBLOCK)) {
free(ctx);
return NULL;
}

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;

for (i = 1; i <= nqueues; i++)
ngx_queue_init(ctx->queues + i);

return ctx;
}


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_write(int fd, unsigned int val) {
ssize_t n;

do
n = write(fd, &val, sizeof(val));
while (n == -1 && errno == EINTR);

if (n == sizeof(val))
return;

if (n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
return; /* pipe full - nothing we can do about that */

abort();
}


static unsigned int uv__signal_read(int fd) {
unsigned int val;
ssize_t n;

do
n = read(fd, &val, sizeof(val));
while (n == -1 && errno == EINTR);

if (n == sizeof(val))
return val;

abort();
}


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
}
@@ -135,6 +135,10 @@ INLINE static void uv_process_endgames(uv_loop_t* loop) {
uv__fs_poll_endgame(loop, (uv_fs_poll_t*) handle);
break;

case UV_SIGNAL:
uv_signal_endgame(loop, (uv_signal_t*) handle);
break;

default:
assert(0);
break;
@@ -138,6 +138,10 @@ void uv_close(uv_handle_t* handle, uv_close_cb cb) {
uv_want_endgame(loop, handle);
return;

case UV_SIGNAL:
uv_signal_close(loop, (uv_signal_t*) handle);
return;

default:
/* Not supported */
abort();
@@ -274,6 +274,13 @@ void uv_fs_event_endgame(uv_loop_t* loop, uv_fs_event_t* handle);
void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle);


/*
* Signals.
*/
void uv_signal_close(uv_loop_t* loop, uv_signal_t* handle);
void uv_signal_endgame(uv_loop_t* loop, uv_signal_t* handle);


/*
* Utilities.
*/
@@ -0,0 +1,57 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "uv.h"
#include "internal.h"
#include "handle-inl.h"
#include <assert.h>


int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
uv__handle_init(loop, (uv_handle_t*)handle, UV_SIGNAL);
return 0;
}


int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
/* XXX call uv__handle_start() and bump the refcount? */
return 0;
}


int uv_signal_stop(uv_signal_t* handle) {
return 0;
}


void uv_signal_close(uv_loop_t* loop, uv_signal_t* handle) {
uv__handle_start(handle);
uv_want_endgame(loop, (uv_handle_t*)handle);
}


void uv_signal_endgame(uv_loop_t* loop, uv_signal_t* handle) {
if (handle->flags & UV_HANDLE_CLOSING) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
handle->flags |= UV_HANDLE_CLOSED;
uv__handle_stop(handle);
uv__handle_close(handle);
}
}
@@ -180,6 +180,8 @@ TEST_DECLARE (dlerror)
TEST_DECLARE (poll_duplex)
TEST_DECLARE (poll_unidirectional)
TEST_DECLARE (poll_close)
TEST_DECLARE (we_get_signal)
TEST_DECLARE (we_get_signals)
#ifdef _WIN32
TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows)
TEST_DECLARE (argument_escaping)
@@ -366,6 +368,10 @@ TASK_LIST_START
TEST_ENTRY (spawn_stdout_to_file)
TEST_ENTRY (fs_poll)
TEST_ENTRY (kill)

TEST_ENTRY (we_get_signal)
TEST_ENTRY (we_get_signals)

#ifdef _WIN32
TEST_ENTRY (spawn_detect_pipe_name_collisions_on_windows)
TEST_ENTRY (argument_escaping)
@@ -0,0 +1,157 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "uv.h"
#include "task.h"

#ifdef _WIN32

TEST_IMPL(we_get_signal) {
return 0;
}

#else /* !_WIN32 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>

/* This test does not pretend to be cross-platform. */
#include <pthread.h>
#include <signal.h>
#include <unistd.h>

#define NSIGNALS 10

struct timer_ctx {
unsigned int ncalls;
uv_timer_t handle;
int signum;
};

struct signal_ctx {
enum { CLOSE, STOP } stop_or_close;
unsigned int ncalls;
uv_signal_t handle;
int signum;
};


static void signal_cb(uv_signal_t* handle, int signum) {
struct signal_ctx* ctx = container_of(handle, struct signal_ctx, handle);
ASSERT(signum == ctx->signum);

if (++ctx->ncalls == NSIGNALS) {
if (ctx->stop_or_close == STOP)
uv_signal_stop(handle);
else if (ctx->stop_or_close == CLOSE)
uv_close((uv_handle_t*)handle, NULL);
else
ASSERT(0);
}
}


static void timer_cb(uv_timer_t* handle, int status) {
struct timer_ctx* ctx = container_of(handle, struct timer_ctx, handle);

raise(ctx->signum);

if (++ctx->ncalls == NSIGNALS)
uv_close((uv_handle_t*)handle, NULL);
}


static void start_watcher(uv_loop_t* loop, int signum, struct signal_ctx* ctx) {
ctx->ncalls = 0;
ctx->signum = signum;
ctx->stop_or_close = CLOSE;
ASSERT(0 == uv_signal_init(loop, &ctx->handle));
ASSERT(0 == uv_signal_start(&ctx->handle, signal_cb, signum));
}


static void start_timer(uv_loop_t* loop, int signum, struct timer_ctx* ctx) {
ctx->ncalls = 0;
ctx->signum = signum;
ASSERT(0 == uv_timer_init(loop, &ctx->handle));
ASSERT(0 == uv_timer_start(&ctx->handle, timer_cb, 5, 5));
}


TEST_IMPL(we_get_signal) {
struct signal_ctx sc;
struct timer_ctx tc;
uv_loop_t* loop;

loop = uv_default_loop();
start_timer(loop, SIGCHLD, &tc);
start_watcher(loop, SIGCHLD, &sc);
sc.stop_or_close = STOP; /* stop, don't close the signal handle */
ASSERT(0 == uv_run(loop));
ASSERT(tc.ncalls == NSIGNALS);
ASSERT(sc.ncalls == NSIGNALS);

start_timer(loop, SIGCHLD, &tc);
ASSERT(0 == uv_run(loop));
ASSERT(tc.ncalls == NSIGNALS);
ASSERT(sc.ncalls == NSIGNALS);

sc.ncalls = 0;
sc.stop_or_close = CLOSE; /* now close it when it's done */
uv_signal_start(&sc.handle, signal_cb, SIGCHLD);

start_timer(loop, SIGCHLD, &tc);
ASSERT(0 == uv_run(loop));
ASSERT(tc.ncalls == NSIGNALS);
ASSERT(sc.ncalls == NSIGNALS);

return 0;
}


TEST_IMPL(we_get_signals) {
struct signal_ctx sc[4];
struct timer_ctx tc[2];
uv_loop_t* loop;
unsigned int i;

loop = uv_default_loop();
start_watcher(loop, SIGUSR1, sc + 0);
start_watcher(loop, SIGUSR1, sc + 1);
start_watcher(loop, SIGUSR2, sc + 2);
start_watcher(loop, SIGUSR2, sc + 3);
start_timer(loop, SIGUSR1, tc + 0);
start_timer(loop, SIGUSR2, tc + 1);
ASSERT(0 == uv_run(loop));

for (i = 0; i < ARRAY_SIZE(sc); i++)
ASSERT(sc[i].ncalls == NSIGNALS);

for (i = 0; i < ARRAY_SIZE(tc); i++)
ASSERT(tc[i].ncalls == NSIGNALS);

return 0;
}

#endif /* _WIN32 */
3 uv.gyp
@@ -76,6 +76,7 @@
'src/win/process-stdio.c',
'src/win/req.c',
'src/win/req-inl.h',
'src/win/signal.c',
'src/win/stream.c',
'src/win/stream-inl.h',
'src/win/tcp.c',
@@ -127,6 +128,7 @@
'src/unix/pipe.c',
'src/unix/poll.c',
'src/unix/process.c',
'src/unix/signal.c',
'src/unix/stream.c',
'src/unix/tcp.c',
'src/unix/thread.c',
@@ -256,6 +258,7 @@
'test/test-semaphore.c',
'test/test-shutdown-close.c',
'test/test-shutdown-eof.c',
'test/test-signal.c',
'test/test-spawn.c',
'test/test-fs-poll.c',
'test/test-stdio-over-pipes.c',