Add support for signals using signalfd(2)

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2013-07-23 03:19:13 +02:00
parent b1de765841
commit 1319f2ba5a
6 changed files with 190 additions and 23 deletions
+3 -2
View File
@@ -40,14 +40,14 @@ incdir ?= $(prefix)/include
DISTFILES = README LICENSE test.c
HEADER = uev.h
OBJS := main.o io.o timer.o
OBJS := main.o io.o timer.o signal.o
SRCS := $(OBJS:.o=.c)
DEPS := $(addprefix .,$(SRCS:.c=.d))
VER = 1
SOLIB = $(LIBNAME).so.$(VER)
SYMLIB = $(LIBNAME).so
STATICLIB = $(LIBNAME).a
TARGET = $(SOLIB) $(STATICLIB)
TARGET = $(STATICLIB) $(SOLIB)
# Pattern rules
.c.o:
@@ -91,6 +91,7 @@ uninstall:
strip: $(TARGET)
@printf " STRIP %s\n" $(TARGET)
@$(STRIP) --strip-unneeded $(TARGET)
@size $(TARGET)
test: Makefile test.o $(STATICLIB)
@printf " TEST %s\n" $(STATICLIB)
+19 -8
View File
@@ -26,6 +26,7 @@
#include <errno.h>
#include <stdlib.h> /* calloc(), free() */
#include <sys/epoll.h>
#include <sys/signalfd.h> /* struct signalfd_siginfo */
#include <unistd.h> /* close(), read() */
#include "uev.h"
@@ -157,7 +158,7 @@ int uev_run(uev_t *ctx)
while ((nfds = epoll_wait(ctx->efd, events, UEV_MAX_EVENTS, -1)) < 0) {
if (EINTR == errno)
continue; /* Signalled, try again */
exit:
result = -1;
ctx->running = 0;
break;
@@ -166,17 +167,27 @@ int uev_run(uev_t *ctx)
for (i = 0; i < nfds; i++) {
w = (uev_watcher_t *)events[i].data.ptr;
if (UEV_TIMER_TYPE == w->type) {
uint64_t exp;
if (read(w->fd, &exp, sizeof(exp)) != sizeof(exp))
goto exit;
}
if (UEV_SIGNAL_TYPE == w->type) {
struct signalfd_siginfo fdsi;
ssize_t sz = sizeof(fdsi);
if (read(w->fd, &fdsi, sz) != sz)
goto exit;
w->signo = fdsi.ssi_signo;
}
if (w->cb)
w->cb((struct uev *)ctx, w, w->arg);
if (UEV_TIMER_TYPE == w->type) {
uint64_t exp;
if (read(w->fd, &exp, sizeof(exp)) != sizeof(exp)) {
result = -1;
ctx->running = 0;
}
if (!w->period)
uev_timer_delete(ctx, w);
}
+130
View File
@@ -0,0 +1,130 @@
/* libuev - Micro event loop library
*
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
* Copyright (c) 2013 Joachim Nilsson <troglobit()gmail!com>
*
* 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 <errno.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <unistd.h> /* close(), read() */
#include "uev.h"
/**
* Create a signal watcher
* @param ctx A valid libuev context
* @param handler Timer callback
* @param data Optional callback argument
* @param signo Signal to watch for
*
* @return The new watcher, or %NULL with @param errno set on error.
*/
uev_watcher_t *uev_signal_create(uev_t *ctx, uev_cb_t *handler, void *data, int signo)
{
int fd;
sigset_t mask;
uev_watcher_t *w;
sigemptyset(&mask);
fd = signalfd(-1, &mask, SFD_NONBLOCK);
if (fd < 0)
return NULL;
w = uev_watcher_create(ctx, UEV_SIGNAL_TYPE, fd, UEV_DIR_INBOUND, handler, data);
if (!w)
goto exit;
if (uev_signal_set(ctx, w, signo)) {
uev_watcher_delete(ctx, w);
exit:
close(fd);
return NULL;
}
return w;
}
/**
* Reset a signal watcher
* @param ctx A valid libuev context
* @param w Watcher to reset
* @param signo New signal to watch for
*
* @return POSIX OK(0) or non-zero with @param errno set.
*/
int uev_signal_set(uev_t *ctx, uev_watcher_t *w, int signo)
{
sigset_t mask;
if (!ctx || !w) {
errno = EINVAL;
return -1;
}
sigemptyset(&mask);
sigaddset(&mask, signo);
/* Block signals so that they aren't handled
according to their default dispositions */
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
return -1;
if (signalfd(w->fd, &mask, SFD_NONBLOCK) < 0)
return -1;
return 0;
}
/**
* Delete a signal watcher
* @param ctx A valid libuev context
* @param w Watcher to delete
*
* @return POSIX OK(0) or non-zero with @param errno set.
*/
int uev_signal_delete(uev_t *ctx, uev_watcher_t *w)
{
int fd;
if (!ctx || !w) {
errno = EINVAL;
return -1;
}
uev_timer_set(ctx, w, 0, 0);
fd = w->fd;
uev_watcher_delete(ctx, w);
close(fd);
return 0;
}
/**
* Local Variables:
* version-control: t
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+11 -1
View File
@@ -23,8 +23,9 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* intptr_t */
#include "uev.h"
@@ -54,6 +55,11 @@ static void periodic_task(uev_t *ctx __attribute__ ((unused)), uev_watcher_t *w
fprintf(stderr, "|");
}
static void signal_cb(uev_t *ctx __attribute__ ((unused)), uev_watcher_t *w, void *data __attribute__ ((unused)))
{
fprintf(stderr, w->signo == SIGINT ? "^Cv" : "^\v");
}
static void pipe_read_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)), void *data __attribute__ ((unused)))
{
int cnt;
@@ -114,6 +120,10 @@ int main(void)
/* Periodic background task */
uev_timer_create(ctx, periodic_task, NULL, 200, 200);
/* Signal watchers, Ctrl-C => SIGINT, Ctrl-\ => SIGQUIT */
uev_signal_create(ctx, signal_cb, NULL, SIGINT);
uev_signal_create(ctx, signal_cb, NULL, SIGQUIT);
/* Start event loop */
uev_run(ctx);
+13 -6
View File
@@ -56,7 +56,7 @@ static struct timespec msec2tspec(int msec)
* timer ends its life there, while a periodic task's @param timeout is
* reset to the @param period and restarted.
*
* @return The new timer, or %NULL if invalid pointers or out or memory.
* @return The new watcher, or %NULL with @param errno set on error.
*/
uev_watcher_t *uev_timer_create(uev_t *ctx, uev_cb_t *handler, void *data, int timeout, int period)
{
@@ -84,9 +84,11 @@ uev_watcher_t *uev_timer_create(uev_t *ctx, uev_cb_t *handler, void *data, int t
/**
* Reset or reschedule a timer
* @param ctx A valid libuev context
* @param w I/O watcher
* @param w Watcher to reset
* @param timeout Timeout in milliseconds before @param handler is called
* @param period For periodic timers this is the period time that @param timeout is reset to
*
* @return POSIX OK(0) or non-zero with @param errno set.
* @return POSIX OK(0) or non-zero with @param errno set on error.
*/
int uev_timer_set(uev_t *ctx, uev_watcher_t *w, int timeout, int period)
{
@@ -112,20 +114,25 @@ int uev_timer_set(uev_t *ctx, uev_watcher_t *w, int timeout, int period)
/**
* Delete a timer watcher
* @param ctx A valid libuev context
* @param w I/O watcher
* @param w Watcher to delete
*
* @return POSIX OK(0) or non-zero with @param errno set.
* @return POSIX OK(0) or non-zero with @param errno set on error.
*/
int uev_timer_delete(uev_t *ctx, uev_watcher_t *w)
{
int fd;
if (!ctx || !w) {
errno = EINVAL;
return -1;
}
uev_timer_set(ctx, w, 0, 0);
fd = w->fd;
uev_watcher_delete(ctx, w);
close(fd);
return uev_watcher_delete(ctx, w);
return 0;
}
/**
+14 -6
View File
@@ -40,10 +40,11 @@ typedef enum {
UEV_DIR_OUTBOUND = 1
} uev_dir_t;
/* File, Timer, ... */
/* I/O, timer, or signal */
typedef enum {
UEV_FILE_TYPE = 1,
UEV_TIMER_TYPE,
UEV_SIGNAL_TYPE,
} uev_type_t;
/* I/O event watcher */
@@ -51,16 +52,19 @@ typedef struct uev_watcher {
LIST_ENTRY(uev_watcher) link; /* For queue.h linked list */
/* Common to all watchers */
uev_type_t type; /* I/O, timer, or signal */
int fd; /* File descriptor */
uev_type_t type;
int fd;
/* Watcher callback with optional argument */
void (*cb)(struct uev *, struct uev_watcher *, void *);
void *arg;
/* Timer watchers */
int timeout; /* Timeout in milliseconds */
int period; /* Period time, in milliseconds */
/* Timer watchers, time in milliseconds */
int timeout;
int period;
/* Signal watchers */
int signo;
} uev_watcher_t;
/* Main libuev context type */
@@ -85,6 +89,10 @@ int uev_timer_set (uev_t *ctx, uev_watcher_t *w, int timeout, int
uev_watcher_t *uev_timer_create (uev_t *ctx, uev_cb_t *cb, void *data, int timeout, int period);
int uev_timer_delete (uev_t *ctx, uev_watcher_t *w);
uev_watcher_t *uev_signal_create (uev_t *ctx, uev_cb_t *handler, void *data, int signo);
int uev_signal_set (uev_t *ctx, uev_watcher_t *w, int signo);
int uev_signal_delete (uev_t *ctx, uev_watcher_t *w);
uev_t *uev_ctx_create (void);
void uev_ctx_delete (uev_t *uev);