LIBS = lib
TESTS = tst-trie
-lib_SOURCES = str.c buffer.c common.c epoll.c server.c trie.c file.c rbl.c
+lib_SOURCES = str.c buffer.c common.c server.c trie.c file.c rbl.c
tst-trie_SOURCES = tst-trie.c lib.a
all:
#include "common.h"
-sig_atomic_t sigint = false;
-sig_atomic_t sighup = false;
-
bool daemon_process = true;
int log_level = LOG_INFO;
bool log_syslog = false;
void common_sighandler(int sig)
{
switch (sig) {
- case SIGTERM:
- case SIGINT:
- sigint = true;
- return;
-
- case SIGHUP:
- sighup = true;
- return;
-
default:
err("Killed (got signal %d)...", sig);
exit(-1);
#define UNIXERR(fun) err("%s:%d:%s %s: %m", \
__FILE__, __LINE__, __func__, fun)
-extern sig_atomic_t sigint;
-extern sig_atomic_t sighup;
extern int log_level;
extern bool log_syslog;
static inline void common_startup(void)
{
signal(SIGPIPE, SIG_IGN);
- signal(SIGINT, &common_sighandler);
- signal(SIGTERM, &common_sighandler);
- signal(SIGHUP, &common_sighandler);
signal(SIGSEGV, &common_sighandler);
}
+++ /dev/null
-/******************************************************************************/
-/* pfixtools: a collection of postfix related tools */
-/* ~~~~~~~~~ */
-/* ________________________________________________________________________ */
-/* */
-/* Redistribution and use in source and binary forms, with or without */
-/* modification, are permitted provided that the following conditions */
-/* are met: */
-/* */
-/* 1. Redistributions of source code must retain the above copyright */
-/* notice, this list of conditions and the following disclaimer. */
-/* 2. Redistributions in binary form must reproduce the above copyright */
-/* notice, this list of conditions and the following disclaimer in the */
-/* documentation and/or other materials provided with the distribution. */
-/* 3. The names of its contributors may not be used to endorse or promote */
-/* products derived from this software without specific prior written */
-/* permission. */
-/* */
-/* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
-/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
-/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
-/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
-/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
-/* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
-/* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
-/* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
-/* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
-/* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
-/* THE POSSIBILITY OF SUCH DAMAGE. */
-/******************************************************************************/
-
-/*
- * Copyright © 2007 Pierre Habouzit
- */
-
-#include "epoll.h"
-
-static int epollfd = -1;
-
-static int epoll_initialize(void)
-{
- epollfd = epoll_create(128);
- return epollfd < 0 ? -1 : 0;
-}
-
-static void epoll_shutdown(void)
-{
- close(epollfd);
-}
-
-module_init(epoll_initialize);
-module_exit(epoll_shutdown);
-
-void epoll_register(int fd, uint32_t events, void *ptr)
-{
- struct epoll_event evt = { .events = events, .data.ptr = ptr };
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &evt)) {
- UNIXERR("epoll_ctl");
- abort();
- }
-}
-
-void epoll_modify(int fd, uint32_t events, void *ptr)
-{
- struct epoll_event evt = { .events = events, .data.ptr = ptr };
- if (epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &evt)) {
- UNIXERR("epoll_ctl");
- abort();
- }
-}
-
-void epoll_unregister(int fd)
-{
- if (epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL)) {
- UNIXERR("epoll_ctl");
- abort();
- }
-}
-
-int epoll_select(struct epoll_event *events, int maxevents, int timeout)
-{
- return epoll_wait(epollfd, events, maxevents, timeout);
-}
+++ /dev/null
-/******************************************************************************/
-/* pfixtools: a collection of postfix related tools */
-/* ~~~~~~~~~ */
-/* ________________________________________________________________________ */
-/* */
-/* Redistribution and use in source and binary forms, with or without */
-/* modification, are permitted provided that the following conditions */
-/* are met: */
-/* */
-/* 1. Redistributions of source code must retain the above copyright */
-/* notice, this list of conditions and the following disclaimer. */
-/* 2. Redistributions in binary form must reproduce the above copyright */
-/* notice, this list of conditions and the following disclaimer in the */
-/* documentation and/or other materials provided with the distribution. */
-/* 3. The names of its contributors may not be used to endorse or promote */
-/* products derived from this software without specific prior written */
-/* permission. */
-/* */
-/* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
-/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
-/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
-/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
-/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
-/* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
-/* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
-/* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
-/* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
-/* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
-/* THE POSSIBILITY OF SUCH DAMAGE. */
-/******************************************************************************/
-
-/*
- * Copyright © 2007 Pierre Habouzit
- */
-
-#ifndef PFIXTOOLS_EPOLL_H
-#define PFIXTOOLS_EPOLL_H
-
-#include <sys/epoll.h>
-#include "common.h"
-
-void epoll_register(int fd, uint32_t events, void *ptr);
-void epoll_modify(int fd, uint32_t events, void *ptr);
-void epoll_unregister(int fd);
-int epoll_select(struct epoll_event *events, int maxevents, int timeout);
-
-#endif
#include <unbound.h>
#include <netdb.h>
#include "array.h"
-#include "epoll.h"
#include "server.h"
#include "rbl.h"
{
int retval = 0;
debug("rbl_handler called: ub_fd triggered");
- epoll_modify(event->fd, 0, event);
+ server_none(event);
if ((retval = ub_process(ctx)) != 0) {
err("error in DNS resolution: %s", ub_strerror(retval));
}
- epoll_modify(event->fd, EPOLLIN, event);
+ server_ro(event);
return 0;
}
static PA(server_t) listeners = ARRAY_INIT;
static PA(server_t) server_pool = ARRAY_INIT;
+struct ev_loop *global_loop = NULL;
+static start_client_t client_start = NULL;
+static delete_client_t client_delete = NULL;
+static run_client_t client_run = NULL;
+static refresh_t config_refresh = NULL;
+static void *config_ptr = NULL;
+
static server_t* server_new(void)
{
server_t* server = p_new(server_t, 1);
static void server_wipe(server_t *server)
{
- server->listener = false;
if (server->fd >= 0) {
- epoll_unregister(server->fd);
+ ev_io_stop(global_loop, &server->io);
close(server->fd);
server->fd = -1;
}
array_add(server_pool, server);
}
+static int server_init(void)
+{
+ global_loop = ev_default_loop(0);
+ return 0;
+}
+
static void server_shutdown(void)
{
- printf("Server shutdown");
array_deep_wipe(listeners, server_delete);
array_deep_wipe(server_pool, server_delete);
}
-
+module_init(server_init);
module_exit(server_shutdown);
-int start_server(int port, start_listener_t starter, delete_client_t deleter)
+static void client_cb(EV_P_ struct ev_io *w, int events)
+{
+ server_t *server = (server_t*)w;
+
+ debug("Entering client_cb for %p, %d (%d | %d)", w, events, EV_WRITE, EV_READ);
+
+ if (events & EV_WRITE && server->obuf.len) {
+ if (buffer_write(&server->obuf, server->fd) < 0) {
+ server_release(server);
+ return;
+ }
+ if (!server->obuf.len) {
+ ev_io_set(&server->io, server->fd, EV_READ);
+ }
+ }
+
+ if (events & EV_READ) {
+ if (server->run(server, config_ptr) < 0) {
+ server_release(server);
+ return;
+ }
+ }
+}
+
+static int start_client(server_t *server, start_client_t starter,
+ run_client_t runner, delete_client_t deleter)
{
- struct sockaddr_in addr = {
- .sin_family = AF_INET,
- .sin_addr = { htonl(INADDR_LOOPBACK) },
- };
server_t *tmp;
void* data = NULL;
int sock;
- addr.sin_port = htons(port);
- sock = tcp_listen_nonblock((const struct sockaddr *)&addr, sizeof(addr));
+ sock = accept_nonblock(server->fd);
if (sock < 0) {
+ UNIXERR("accept");
return -1;
}
if (starter) {
- data = starter();
- if (data == NULL) {
- close(sock);
- return -1;
- }
+ data = starter(server);
+ if (data == NULL) {
+ close(sock);
+ return -1;
+ }
}
tmp = server_acquire();
tmp->fd = sock;
- tmp->listener = true;
tmp->data = data;
- tmp->run = NULL;
+ tmp->run = runner;
tmp->clear_data = deleter;
- epoll_register(sock, EPOLLIN, tmp);
- array_add(listeners, tmp);
+ ev_io_init(&tmp->io, client_cb, tmp->fd, EV_READ);
+ ev_io_start(global_loop, &tmp->io);
return 0;
}
-static int start_client(server_t *server, start_client_t starter,
- run_client_t runner, delete_client_t deleter)
+static void server_cb(EV_P_ struct ev_io *w, int events)
{
+ server_t *server = (server_t*)w;
+ if (start_client(server, client_start, client_run, client_delete) != 0) {
+ ev_unloop(EV_A_ EVUNLOOP_ALL);
+ }
+}
+
+int start_server(int port, start_listener_t starter, delete_client_t deleter)
+{
+ struct sockaddr_in addr = {
+ .sin_family = AF_INET,
+ .sin_addr = { htonl(INADDR_LOOPBACK) },
+ };
server_t *tmp;
void* data = NULL;
int sock;
- sock = accept_nonblock(server->fd);
+ addr.sin_port = htons(port);
+ sock = tcp_listen_nonblock((const struct sockaddr *)&addr, sizeof(addr));
if (sock < 0) {
- UNIXERR("accept");
return -1;
}
if (starter) {
- data = starter(server);
- if (data == NULL) {
- close(sock);
- return -1;
- }
+ data = starter();
+ if (data == NULL) {
+ close(sock);
+ return -1;
+ }
}
tmp = server_acquire();
- tmp->listener = false;
tmp->fd = sock;
tmp->data = data;
- tmp->run = runner;
+ tmp->run = NULL;
tmp->clear_data = deleter;
- epoll_register(sock, EPOLLIN, tmp);
+ ev_io_init(&tmp->io, server_cb, tmp->fd, EV_READ);
+ ev_io_start(global_loop, &tmp->io);
+ array_add(listeners, tmp);
return 0;
}
}
server_t *tmp = server_acquire();
- tmp->listener = false;
tmp->fd = fd;
tmp->data = data;
tmp->run = runner;
tmp->clear_data = NULL;
- epoll_register(fd, EPOLLIN, tmp);
+ ev_io_init(&tmp->io, client_cb, tmp->fd, EV_READ);
+ ev_io_start(global_loop, &tmp->io);
return tmp;
}
+static void refresh_cb(EV_P_ struct ev_signal *w, int event)
+{
+ if (!config_refresh(config_ptr)) {
+ ev_unloop(EV_A_ EVUNLOOP_ALL);
+ }
+}
+
+static void exit_cb(EV_P_ struct ev_signal *w, int event)
+{
+ ev_unloop(EV_A_ EVUNLOOP_ALL);
+}
+
int server_loop(start_client_t starter, delete_client_t deleter,
run_client_t runner, refresh_t refresh, void* config)
{
- info("entering processing loop");
- while (!sigint) {
- struct epoll_event evts[1024];
- int n;
-
- if (sighup && refresh) {
- sighup = false;
- info("refreshing...");
- if (!refresh(config)) {
- crit("error while refreshing configuration");
- return EXIT_FAILURE;
- }
- info("refresh done, processing loop restarts");
- }
+ struct ev_signal ev_sighup;
+ struct ev_signal ev_sigint;
+ struct ev_signal ev_sigterm;
- n = epoll_select(evts, countof(evts), -1);
- if (n < 0) {
- if (errno != EAGAIN && errno != EINTR) {
- UNIXERR("epoll_wait");
- return EXIT_FAILURE;
- }
- continue;
- }
+ client_start = starter;
+ client_delete = deleter;
+ client_run = runner;
+ config_refresh = refresh;
+ config_ptr = config;
- while (--n >= 0) {
- server_t *d = evts[n].data.ptr;
-
- if (d->listener) {
- (void)start_client(d, starter, runner, deleter);
- continue;
- }
-
- if ((evts[n].events & EPOLLOUT) && d->obuf.len) {
- if (buffer_write(&d->obuf, d->fd) < 0) {
- server_release(d);
- continue;
- }
- if (!d->obuf.len) {
- epoll_modify(d->fd, EPOLLIN, d);
- }
- }
-
- if (evts[n].events & EPOLLIN) {
- if (d->run(d, config) < 0) {
- server_release(d);
- }
- continue;
- }
- }
+ if (refresh != NULL) {
+ ev_signal_init(&ev_sighup, refresh_cb, SIGHUP);
+ ev_signal_start(global_loop, &ev_sighup);
}
+ ev_signal_init(&ev_sigint, exit_cb, SIGINT);
+ ev_signal_start(global_loop, &ev_sigint);
+ ev_signal_init(&ev_sigterm, exit_cb, SIGTERM);
+ ev_signal_start(global_loop, &ev_sigterm);
+
+ info("entering processing loop");
+ ev_loop(global_loop, 0);
info("exit requested");
return EXIT_SUCCESS;
}
#ifndef PFIXTOOLS_SERVER_H
#define PFIXTOOLS_SERVER_H
+#include <ev.h>
#include "buffer.h"
typedef struct server_t server_t;
-#define INVALID_EVENT (NULL)
-
typedef void *(*start_listener_t)(void);
typedef void (*delete_client_t)(void*);
typedef void *(*start_client_t)(server_t*);
typedef int (*run_client_t)(server_t*, void*);
typedef bool (*refresh_t)(void*);
-typedef bool (*event_handler_t)(server_t *, void*);
-struct server_t {
- unsigned listener : 1;
+extern struct ev_loop *global_loop;
+struct server_t {
+ struct ev_io io;
int fd;
buffer_t ibuf;
server_t *server_register(int fd, run_client_t runner, void *data);
void server_release(server_t *server);
+static inline void server_none(server_t *server)
+{
+ ev_io_stop(global_loop, &server->io);
+}
+
+static inline void server_rw(server_t *server)
+{
+ ev_io_stop(global_loop, &server->io);
+ ev_io_set(&server->io, server->fd, EV_READ | EV_WRITE);
+ ev_io_start(global_loop, &server->io);
+}
+
+static inline void server_ro(server_t *server)
+{
+ ev_io_stop(global_loop, &server->io);
+ ev_io_set(&server->io, server->fd, EV_READ);
+ ev_io_start(global_loop, &server->io);
+}
+
int server_loop(start_client_t starter, delete_client_t deleter,
run_client_t runner, refresh_t refresh, void *config);
PROGRAMS = pfix-srsd
pfix-srsd_SOURCES = main-srsd.c ../common/lib.a
-pfix-srsd_LIBADD = -lsrs2
+pfix-srsd_LIBADD = -lsrs2 -lev
all:
#include <srs2.h>
-#include "epoll.h"
#include "mem.h"
#include "buffer.h"
#include "server.h"
return -1;
}
if (srsd->obuf.len) {
- epoll_modify(srsd->fd, EPOLLIN | EPOLLOUT, srsd);
+ server_rw(srsd);
}
return 0;
}
buffer_consume(&srsd->ibuf, nl - srsd->ibuf.data);
}
if (srsd->obuf.len) {
- epoll_modify(srsd->fd, EPOLLIN | EPOLLOUT, srsd);
+ server_rw(srsd);
}
return 0;
}
FILTERS = iplist.c greylist.c strlist.c match.c
postlicyd_SOURCES = main-postlicyd.c ../common/lib.a filter.c config.c query.c $(FILTERS) $(GENERATED)
-postlicyd_LIBADD = $(UB_LIBS) $(TC_LIBS)
+postlicyd_LIBADD = $(UB_LIBS) $(TC_LIBS) -lev
tst-rbl_SOURCES = tst-rbl.c ../common/lib.a filter.c config.c query.c iplist.c $(GENERATED)
#include "buffer.h"
#include "common.h"
-#include "epoll.h"
#include "policy_tokens.h"
#include "server.h"
#include "config.h"
static bool config_refresh(void *mconfig)
{
if (filter_running > 0) {
- sighup = true;
sleep(1);
return true;
}
}
buffer_addstr(&pcy->obuf, "\n\n");
buffer_consume(&pcy->ibuf, query->eoq - pcy->ibuf.data);
- epoll_modify(pcy->fd, EPOLLIN | EPOLLOUT, pcy);
+ server_rw(pcy);
}
static const filter_t *next_filter(server_t *pcy, const filter_t *filter,
static int policy_run(server_t *pcy, void* vconfig)
{
- if (sighup) {
- return 0;
- }
-
int search_offs = MAX(0, (int)(pcy->ibuf.len - 1));
int nb = buffer_read(&pcy->ibuf, pcy->fd, -1);
const char *eoq;
if (!query_parse(pcy->data, pcy->ibuf.data))
return -1;
query->eoq = eoq + strlen("\n\n");
- epoll_modify(pcy->fd, 0, pcy);
+ server_none(pcy);
return policy_process(pcy, mconfig) ? 0 : -1;
}