mirror of
https://github.com/troglobit/libuev.git
synced 2026-10-31 12:20:29 +00:00
Add support for signals using signalfd(2)
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
b1de765841
commit
1319f2ba5a
6 files changed
+190
-23
No files matched your search
@@ -40,14 +40,14 @@ incdir ?= $(prefix)/include
|
|||||||
DISTFILES = README LICENSE test.c
|
DISTFILES = README LICENSE test.c
|
||||||
HEADER = uev.h
|
HEADER = uev.h
|
||||||
|
|
||||||
OBJS := main.o io.o timer.o
|
OBJS := main.o io.o timer.o signal.o
|
||||||
SRCS := $(OBJS:.o=.c)
|
SRCS := $(OBJS:.o=.c)
|
||||||
DEPS := $(addprefix .,$(SRCS:.c=.d))
|
DEPS := $(addprefix .,$(SRCS:.c=.d))
|
||||||
VER = 1
|
VER = 1
|
||||||
SOLIB = $(LIBNAME).so.$(VER)
|
SOLIB = $(LIBNAME).so.$(VER)
|
||||||
SYMLIB = $(LIBNAME).so
|
SYMLIB = $(LIBNAME).so
|
||||||
STATICLIB = $(LIBNAME).a
|
STATICLIB = $(LIBNAME).a
|
||||||
TARGET = $(SOLIB) $(STATICLIB)
|
TARGET = $(STATICLIB) $(SOLIB)
|
||||||
|
|
||||||
# Pattern rules
|
# Pattern rules
|
||||||
.c.o:
|
.c.o:
|
||||||
@@ -91,6 +91,7 @@ uninstall:
|
|||||||
strip: $(TARGET)
|
strip: $(TARGET)
|
||||||
@printf " STRIP %s\n" $(TARGET)
|
@printf " STRIP %s\n" $(TARGET)
|
||||||
@$(STRIP) --strip-unneeded $(TARGET)
|
@$(STRIP) --strip-unneeded $(TARGET)
|
||||||
|
@size $(TARGET)
|
||||||
|
|
||||||
test: Makefile test.o $(STATICLIB)
|
test: Makefile test.o $(STATICLIB)
|
||||||
@printf " TEST %s\n" $(STATICLIB)
|
@printf " TEST %s\n" $(STATICLIB)
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h> /* calloc(), free() */
|
#include <stdlib.h> /* calloc(), free() */
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
|
#include <sys/signalfd.h> /* struct signalfd_siginfo */
|
||||||
#include <unistd.h> /* close(), read() */
|
#include <unistd.h> /* close(), read() */
|
||||||
|
|
||||||
#include "uev.h"
|
#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) {
|
while ((nfds = epoll_wait(ctx->efd, events, UEV_MAX_EVENTS, -1)) < 0) {
|
||||||
if (EINTR == errno)
|
if (EINTR == errno)
|
||||||
continue; /* Signalled, try again */
|
continue; /* Signalled, try again */
|
||||||
|
exit:
|
||||||
result = -1;
|
result = -1;
|
||||||
ctx->running = 0;
|
ctx->running = 0;
|
||||||
break;
|
break;
|
||||||
@@ -166,17 +167,27 @@ int uev_run(uev_t *ctx)
|
|||||||
for (i = 0; i < nfds; i++) {
|
for (i = 0; i < nfds; i++) {
|
||||||
w = (uev_watcher_t *)events[i].data.ptr;
|
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)
|
if (w->cb)
|
||||||
w->cb((struct uev *)ctx, w, w->arg);
|
w->cb((struct uev *)ctx, w, w->arg);
|
||||||
|
|
||||||
if (UEV_TIMER_TYPE == w->type) {
|
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)
|
if (!w->period)
|
||||||
uev_timer_delete(ctx, w);
|
uev_timer_delete(ctx, w);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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:
|
||||||
|
*/
|
||||||
@@ -23,8 +23,9 @@
|
|||||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h> /* intptr_t */
|
#include <unistd.h> /* intptr_t */
|
||||||
|
|
||||||
#include "uev.h"
|
#include "uev.h"
|
||||||
@@ -54,6 +55,11 @@ static void periodic_task(uev_t *ctx __attribute__ ((unused)), uev_watcher_t *w
|
|||||||
fprintf(stderr, "|");
|
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)))
|
static void pipe_read_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)), void *data __attribute__ ((unused)))
|
||||||
{
|
{
|
||||||
int cnt;
|
int cnt;
|
||||||
@@ -114,6 +120,10 @@ int main(void)
|
|||||||
/* Periodic background task */
|
/* Periodic background task */
|
||||||
uev_timer_create(ctx, periodic_task, NULL, 200, 200);
|
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 */
|
/* Start event loop */
|
||||||
uev_run(ctx);
|
uev_run(ctx);
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ static struct timespec msec2tspec(int msec)
|
|||||||
* timer ends its life there, while a periodic task's @param timeout is
|
* timer ends its life there, while a periodic task's @param timeout is
|
||||||
* reset to the @param period and restarted.
|
* 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)
|
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
|
* Reset or reschedule a timer
|
||||||
* @param ctx A valid libuev context
|
* @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)
|
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
|
* Delete a timer watcher
|
||||||
* @param ctx A valid libuev context
|
* @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 uev_timer_delete(uev_t *ctx, uev_watcher_t *w)
|
||||||
{
|
{
|
||||||
|
int fd;
|
||||||
|
|
||||||
if (!ctx || !w) {
|
if (!ctx || !w) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uev_timer_set(ctx, w, 0, 0);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -40,10 +40,11 @@ typedef enum {
|
|||||||
UEV_DIR_OUTBOUND = 1
|
UEV_DIR_OUTBOUND = 1
|
||||||
} uev_dir_t;
|
} uev_dir_t;
|
||||||
|
|
||||||
/* File, Timer, ... */
|
/* I/O, timer, or signal */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
UEV_FILE_TYPE = 1,
|
UEV_FILE_TYPE = 1,
|
||||||
UEV_TIMER_TYPE,
|
UEV_TIMER_TYPE,
|
||||||
|
UEV_SIGNAL_TYPE,
|
||||||
} uev_type_t;
|
} uev_type_t;
|
||||||
|
|
||||||
/* I/O event watcher */
|
/* I/O event watcher */
|
||||||
@@ -51,16 +52,19 @@ typedef struct uev_watcher {
|
|||||||
LIST_ENTRY(uev_watcher) link; /* For queue.h linked list */
|
LIST_ENTRY(uev_watcher) link; /* For queue.h linked list */
|
||||||
|
|
||||||
/* Common to all watchers */
|
/* Common to all watchers */
|
||||||
uev_type_t type; /* I/O, timer, or signal */
|
uev_type_t type;
|
||||||
int fd; /* File descriptor */
|
int fd;
|
||||||
|
|
||||||
/* Watcher callback with optional argument */
|
/* Watcher callback with optional argument */
|
||||||
void (*cb)(struct uev *, struct uev_watcher *, void *);
|
void (*cb)(struct uev *, struct uev_watcher *, void *);
|
||||||
void *arg;
|
void *arg;
|
||||||
|
|
||||||
/* Timer watchers */
|
/* Timer watchers, time in milliseconds */
|
||||||
int timeout; /* Timeout in milliseconds */
|
int timeout;
|
||||||
int period; /* Period time, in milliseconds */
|
int period;
|
||||||
|
|
||||||
|
/* Signal watchers */
|
||||||
|
int signo;
|
||||||
} uev_watcher_t;
|
} uev_watcher_t;
|
||||||
|
|
||||||
/* Main libuev context type */
|
/* 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);
|
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);
|
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);
|
uev_t *uev_ctx_create (void);
|
||||||
void uev_ctx_delete (uev_t *uev);
|
void uev_ctx_delete (uev_t *uev);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user