Merge commit 'pan/master' into not-linux
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 14 Oct 2008 22:28:35 +0000 (00:28 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 14 Oct 2008 22:28:35 +0000 (00:28 +0200)
Conflicts:
postlicyd/filter.c
postlicyd/main-postlicyd.c

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
16 files changed:
common.ld [deleted file]
common/Makefile
common/common.c
common/common.h
common/epoll.c [deleted file]
common/epoll.h [deleted file]
common/rbl.c
common/server.c
common/server.h
mk/cflags.mk
mk/common.mk
pfix-srsd/Makefile
pfix-srsd/main-srsd.c
postlicyd/Makefile
postlicyd/filter.c
postlicyd/main-postlicyd.c

diff --git a/common.ld b/common.ld
deleted file mode 100644 (file)
index 6f7485f..0000000
--- a/common.ld
+++ /dev/null
@@ -1,10 +0,0 @@
-SECTIONS {
-    .rodata : {
-        . = ALIGN(8);
-        __madinit = . ;
-        *( .mad.init )
-        QUAD(0)
-        *( .mad.exit )
-        __madexit = . ;
-    }
-}
index 3f30e73..6a57421 100644 (file)
@@ -32,7 +32,7 @@
 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:
index 43b2bf2..9fdf961 100644 (file)
@@ -41,9 +41,6 @@
 
 #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;
@@ -53,15 +50,6 @@ static FILE *pidfile = NULL;
 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);
@@ -292,8 +280,16 @@ int common_setup(const char* pidfilename, bool unsafe, const char* runas_user,
     return EXIT_SUCCESS;
 }
 
-extern initcall_t __madinit[];
-extern exitcall_t __madexit[];
+#include "array.h"
+
+ARRAY(exitcall_t)
+
+static A(exitcall_t) __exit = ARRAY_INIT;
+
+void common_register_exit(exitcall_t _exit)
+{
+    array_add(__exit, _exit);
+}
 
 static void common_shutdown(void)
 {
@@ -301,23 +297,22 @@ static void common_shutdown(void)
         info("stopping...");
     }
     pidfile_close();
-    for (int i = -1; __madexit[i]; i--) {
-        (*__madexit[i])();
+    for (int i = array_len(__exit) - 1 ; i >= 0 ; --i) {
+        array_elt(__exit, i)();
     }
+    array_wipe(__exit);
 }
 
-static void __attribute__((__constructor__,__used__))
-common_initialize(void)
+void common_init(void)
 {
+    static bool __ran = false;
+    if (__ran) {
+        return;
+    }
     if (atexit(common_shutdown)) {
         fputs("Cannot hook my atexit function, quitting !\n", stderr);
         abort();
     }
-
-    for (int i = 0; __madinit[i]; i++) {
-        if ((*__madinit[i])()) {
-            exit(EXIT_FAILURE);
-        }
-    }
+    __ran = true;
 }
 
index 22299b7..40eb0a2 100644 (file)
 typedef int  (*initcall_t)(void);
 typedef void (*exitcall_t)(void);
 
-#define __init __attribute__((__used__,__section__(".mad.init")))
-#define __exit __attribute__((__used__,__section__(".mad.exit")))
-
-#define module_init(fn)  static __init initcall_t __init_##fn = fn;
-#define module_exit(fn)  static __exit exitcall_t __exit_##fn = fn;
+void common_register_exit(exitcall_t _exit);
+void common_init(void);
+
+#define module_init(fn)                                                        \
+    __attribute__((constructor,used))                                          \
+    static void __init_wrapper__ ## fn (void) {                                \
+        common_init();                                                         \
+        if (fn() != 0) {                                                       \
+            exit(-1);                                                          \
+        }                                                                      \
+    }
+#define module_exit(fn)                                                        \
+    __attribute__((constructor,used))                                          \
+    static void __exit_wrapper ## fn(void) {                                   \
+        common_init();                                                         \
+        common_register_exit(fn);                                              \
+    }
 
 #define likely(expr)    __builtin_expect((expr) != 0, 1)
 #define unlikely(expr)  __builtin_expect((expr) != 0, 0)
@@ -104,8 +116,6 @@ typedef void (*exitcall_t)(void);
 #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;
 
@@ -130,9 +140,6 @@ int common_setup(const char* pidfile, bool unsafe, const char* runas_user,
 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);
 }
 
diff --git a/common/epoll.c b/common/epoll.c
deleted file mode 100644 (file)
index 896fc2c..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-/******************************************************************************/
-/*          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);
-}
diff --git a/common/epoll.h b/common/epoll.h
deleted file mode 100644 (file)
index fdc2665..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/******************************************************************************/
-/*          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
index 4f8c23f..e7c79fb 100644 (file)
@@ -36,7 +36,6 @@
 #include <unbound.h>
 #include <netdb.h>
 #include "array.h"
-#include "epoll.h"
 #include "server.h"
 #include "rbl.h"
 
@@ -126,11 +125,11 @@ static int rbl_handler(server_t *event, void *config)
 {
     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;
 }
 
index 838e9f7..b47da2f 100644 (file)
  */
 
 #include "server.h"
-#include "epoll.h"
 #include "common.h"
 
 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);
@@ -49,9 +55,8 @@ static server_t* server_new(void)
 
 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;
     }
@@ -60,7 +65,7 @@ static void server_wipe(server_t *server)
     }
 }
 
-static void server_delete(server_t **server)
+void server_delete(server_t **server)
 {
     if (*server) {
         buffer_wipe(&(*server)->ibuf);
@@ -85,78 +90,115 @@ void server_release(server_t *server)
     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;
 }
 
@@ -167,68 +209,51 @@ server_t *server_register(int fd, run_client_t runner, void *data)
     }
 
     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;
 }
index 33d7157..2757eae 100644 (file)
 #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;
@@ -66,8 +65,28 @@ ARRAY(server_t);
 int start_server(int port, start_listener_t starter, delete_client_t deleter);
 
 server_t *server_register(int fd, run_client_t runner, void *data);
+void server_delete(server_t **server);
 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);
 
index c05a843..07f1870 100644 (file)
@@ -50,7 +50,7 @@ CFLAGS += -Wchar-subscripts
 # warn about undefined preprocessor identifiers
 CFLAGS += -Wundef
 # warn about local variable shadowing another local variable
-CFLAGS += -Wshadow
+#CFLAGS += -Wshadow
 # warn about casting of pointers to increased alignment requirements
 CFLAGS += -Wcast-align
 # make string constants const
@@ -70,7 +70,7 @@ CFLAGS += $(if $(GCC4),-Winit-self)
 # warn about pointer arithmetic on void* and function pointers
 CFLAGS += -Wpointer-arith
 # warn about multiple declarations
-CFLAGS += -Wredundant-decls
+#CFLAGS += -Wredundant-decls
 # warn if the format string is not a string literal
 CFLAGS += -Wformat-nonliteral
 # do not warn about zero-length formats.
index 12e943b..4e4be6f 100644 (file)
@@ -1,8 +1,8 @@
 include ../mk/cflags.mk
 
 prefix ?= /usr/local
-LDFLAGS += -Wl,--warn-common
-CFLAGS  += --std=gnu99 -D_GNU_SOURCE -I../ -I../common
+LDFLAGS += -L/opt/local/lib
+CFLAGS  += --std=gnu99 -I../ -I../common -I/opt/local/include
 
 INSTALL_PROGS = $(addprefix install-,$(PROGRAMS))
 
@@ -49,8 +49,8 @@ $(LIBS:=.a): $$(patsubst %.c,.%.o,$$($$(patsubst %.a,%,$$@)_SOURCES)) Makefile
        $(RM) $@
        $(AR) rcs $@ $(filter %.o,$^)
 
-$(PROGRAMS) $(TESTS): $$(patsubst %.c,.%.o,$$($$@_SOURCES)) Makefile ../common.ld
-       $(CC) -o $@ $(filter %.ld,$^) $(filter %.o,$^) $(LDFLAGS) $($@_LIBADD) $(filter %.a,$^)
+$(PROGRAMS) $(TESTS): $$(patsubst %.c,.%.o,$$($$@_SOURCES)) Makefile
+       $(CC) -o $@ $(filter %.o,$^) $(LDFLAGS) $($@_LIBADD) $(filter %.a,$^)
 
 -include $(foreach p,$(PROGRAMS) $(TESTS),$(patsubst %.c,.%.dep,$(filter %.c,$($p_SOURCES))))
 
index 0be9e82..fbe3c60 100644 (file)
@@ -32,7 +32,7 @@
 PROGRAMS  = pfix-srsd
 
 pfix-srsd_SOURCES = main-srsd.c ../common/lib.a
-pfix-srsd_LIBADD  = -lsrs2
+pfix-srsd_LIBADD  = -lsrs2 -lev
 
 all:
 
index 037a458..440e56f 100644 (file)
@@ -38,7 +38,6 @@
 
 #include <srs2.h>
 
-#include "epoll.h"
 #include "mem.h"
 #include "buffer.h"
 #include "server.h"
@@ -127,7 +126,7 @@ int process_srs(server_t *srsd, void* vconfig)
                 return -1;
             }
             if (srsd->obuf.len) {
-              epoll_modify(srsd->fd, EPOLLIN | EPOLLOUT, srsd);
+                server_rw(srsd);
             }
             return 0;
         }
@@ -175,7 +174,7 @@ int process_srs(server_t *srsd, void* vconfig)
         buffer_consume(&srsd->ibuf, nl - srsd->ibuf.data);
     }
     if (srsd->obuf.len) {
-      epoll_modify(srsd->fd, EPOLLIN | EPOLLOUT, srsd);
+        server_rw(srsd);
     }
     return 0;
 }
index 659588a..64c7433 100644 (file)
@@ -43,7 +43,7 @@ UB_LIBS   = -lunbound
 FILTERS                = iplist.c greylist.c strlist.c match.c counters.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)
 
index 14bfc9b..e108b25 100644 (file)
@@ -281,6 +281,7 @@ bool filter_add_hook(filter_t *filter, const char *name, int name_len,
                      const char *value, int value_len)
 {
     filter_hook_t hook;
+    hook.filter_id = -1;
     hook.type  = hook_tokenize(name, name_len);
     if (hook.type == HTK_UNKNOWN) {
         err("unknown hook type %.*s", name_len, name);
@@ -295,6 +296,7 @@ bool filter_add_hook(filter_t *filter, const char *name, int name_len,
 
     /* Value format is (counter:id:incr)?(postfix:reply|filter_name)
      */
+    hook.value = NULL;
     if (strncmp(value, "counter:", 8) == 0) {
         char *end = NULL;
         value += 8;
@@ -327,7 +329,6 @@ bool filter_add_hook(filter_t *filter, const char *name, int name_len,
         return false;
     }
     hook.value = m_strdup(hook.postfix ? value + 8 : value);
-    hook.filter_id = -1;
     array_add(filter->hooks, hook);
     return true;
 }
index bf68941..9baf34a 100644 (file)
@@ -38,7 +38,6 @@
 
 #include "buffer.h"
 #include "common.h"
-#include "epoll.h"
 #include "policy_tokens.h"
 #include "server.h"
 #include "config.h"
@@ -52,8 +51,9 @@
 
 DECLARE_MAIN
 
-static config_t *config = NULL;
-
+static config_t *config  = NULL;
+static bool refresh      = false;
+static PA(server_t) busy = ARRAY_INIT;
 
 static void *query_starter(server_t* server)
 {
@@ -73,12 +73,17 @@ static void query_stopper(void *data)
 
 static bool config_refresh(void *mconfig)
 {
+    refresh = true;
     if (filter_running > 0) {
-        sighup = true;
-        sleep(1);
         return true;
     }
-    return config_reload(mconfig);
+    bool ret = config_reload(mconfig);
+    foreach (server_t **server, busy) {
+        server_ro(*server);
+    }}
+    array_len(busy) = 0;
+    refresh = false;
+    return ret;
 }
 
 static void policy_answer(server_t *pcy, const char *message)
@@ -105,7 +110,7 @@ static void policy_answer(server_t *pcy, const char *message)
     }
     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,
@@ -186,7 +191,8 @@ static bool policy_process(server_t *pcy, const config_t *mconfig)
 
 static int policy_run(server_t *pcy, void* vconfig)
 {
-    if (sighup) {
+    if (refresh) {
+        array_add(busy, pcy);
         return 0;
     }
 
@@ -220,7 +226,7 @@ static int policy_run(server_t *pcy, void* vconfig)
         filter_context_clean(&context->context);
         m_strcat(context->context.instance, 64, query->instance);
     }
-    epoll_modify(pcy->fd, 0, pcy);
+    server_none(pcy);
     return policy_process(pcy, mconfig) ? 0 : -1;
 }
 
@@ -240,6 +246,9 @@ static void policy_async_handler(filter_context_t *context,
     if (!ok) {
         server_release(server);
     }
+    if (refresh && filter_running == 0) {
+        config_refresh(config);
+    }
 }
 
 static int postlicyd_init(void)
@@ -247,7 +256,13 @@ static int postlicyd_init(void)
     filter_async_handler_register(policy_async_handler);
     return 0;
 }
+
+static void postlicyd_shutdown(void)
+{
+    array_deep_wipe(busy, server_delete);
+}
 module_init(postlicyd_init);
+module_exit(postlicyd_shutdown);
 
 int start_listener(int port)
 {