From 7f5b0a5a99d73ee8d39a692ab3d289399a408e39 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Thu, 29 Nov 2007 11:13:30 +0100 Subject: [PATCH] Have an epoll module. --- Makefile | 4 ++-- epoll.c | 52 ++++++++++++++++++++++++++++++++++++++++ epoll.h | 41 ++++++++++++++++++++++++++++++++ main-postlicyd.c | 62 +++++++++++++++++++++++++++++++++++++----------- main-srsd.c | 29 +++++----------------- 5 files changed, 149 insertions(+), 39 deletions(-) create mode 100644 epoll.c create mode 100644 epoll.h diff --git a/Makefile b/Makefile index 0dbc640..249f9df 100644 --- a/Makefile +++ b/Makefile @@ -43,11 +43,11 @@ TESTS = tst-rbl GENERATED = tokens.h tokens.c -postlicyd_SOURCES = common.c str.c buffer.c rbl.c \ +postlicyd_SOURCES = common.c epoll.c str.c buffer.c rbl.c \ $(GENERATED) postfix.c main-postlicyd.c postlicyd_LIBADD = -lpthread -pfix-srsd_SOURCES = common.c buffer.c str.c main-srsd.c +pfix-srsd_SOURCES = common.c epoll.c buffer.c str.c main-srsd.c pfix-srsd_LIBADD = -lsrs2 tst-rbl_SOURCES = tst-rbl.c diff --git a/epoll.c b/epoll.c new file mode 100644 index 0000000..3d16789 --- /dev/null +++ b/epoll.c @@ -0,0 +1,52 @@ +/******************************************************************************/ +/* 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" + +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); diff --git a/epoll.h b/epoll.h new file mode 100644 index 0000000..f4e1453 --- /dev/null +++ b/epoll.h @@ -0,0 +1,41 @@ +/******************************************************************************/ +/* 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 + +#include "common.h" + +extern int epollfd; + diff --git a/main-postlicyd.c b/main-postlicyd.c index 6810bba..70d9d69 100644 --- a/main-postlicyd.c +++ b/main-postlicyd.c @@ -33,11 +33,10 @@ * Copyright © 2006-2007 Pierre Habouzit */ -#include -#include #include #include "common.h" +#include "epoll.h" /* administrivia {{{ */ @@ -67,16 +66,17 @@ void *job_run(void *_fd) int fd = (intptr_t)_fd; close(fd); + pthread_detach(pthread_self()); return NULL; } -static void main_loop(void) +static int main_loop(void) { + int exitcode = EXIT_SUCCESS; int sock = -1; while (!sigint) { int fd = accept(sock, NULL, 0); - pthread_attr_t attr; pthread_t dummy; if (fd < 0) { @@ -85,24 +85,58 @@ static void main_loop(void) continue; } - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_create(&dummy, &attr, job_run, (void *)(intptr_t)fd); - pthread_attr_destroy(&attr); + pthread_create(&dummy, NULL, job_run, (void *)(intptr_t)fd); } close(sock); + return exitcode; } -int main(void) +int main(int argc, char *argv[]) { - if (atexit(common_shutdown)) { - fputs("Cannot hook my atexit function, quitting !\n", stderr); + const char *pidfile = NULL; + FILE *f = NULL; + int res; + + common_initialize(); + for (int c = 0; (c = getopt(argc, argv, "h" "p:")) >= 0; ) { + switch (c) { + case 'p': + pidfile = optarg; + break; + default: + //usage(); + return EXIT_FAILURE; + } + } + + if (pidfile) { + f = fopen(pidfile, "w"); + if (!f) { + syslog(LOG_CRIT, "unable to write pidfile %s", pidfile); + } + fprintf(f, "%d\n", getpid()); + fflush(f); + } + + if (daemon_detach() < 0) { + syslog(LOG_CRIT, "unable to fork"); return EXIT_FAILURE; } - common_initialize(); - main_loop(); + if (f) { + rewind(f); + ftruncate(fileno(f), 0); + fprintf(f, "%d\n", getpid()); + fflush(f); + } + res = main_loop(); + if (f) { + rewind(f); + ftruncate(fileno(f), 0); + fclose(f); + f = NULL; + } syslog(LOG_INFO, "Stopping..."); - return EXIT_SUCCESS; + return res; } diff --git a/main-srsd.c b/main-srsd.c index 891f7ee..57835cd 100644 --- a/main-srsd.c +++ b/main-srsd.c @@ -33,14 +33,11 @@ * Copyright © 2005-2007 Pierre Habouzit */ -#include -#include -#include -#include +#include "common.h" #include -#include "common.h" +#include "epoll.h" #include "mem.h" #include "buffer.h" @@ -163,7 +160,7 @@ int process_srs(srs_t *srs, const char *domain, srsd_t *srsd) return 0; } -int start_listener(int epollfd, int port, bool decoder) +int start_listener(int port, bool decoder) { struct sockaddr_in addr = { .sin_family = AF_INET, @@ -232,17 +229,10 @@ void usage(void) int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec) { int exitcode = EXIT_SUCCESS; - int epollfd = epoll_create(128); - - if (epollfd < 0) { - UNIXERR("epoll_create"); - exitcode = EXIT_FAILURE; - goto error; - } - if (start_listener(epollfd, port_enc, false) < 0) + if (start_listener(port_enc, false) < 0) return EXIT_FAILURE; - if (start_listener(epollfd, port_dec, true) < 0) + if (start_listener(port_dec, true) < 0) return EXIT_FAILURE; while (!sigint) { @@ -328,9 +318,6 @@ int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec) } } - close(epollfd); - - error: return exitcode; } @@ -386,13 +373,9 @@ int main(int argc, char *argv[]) int res; srs_t *srs; - if (atexit(common_shutdown)) { - fputs("Cannot hook my atexit function, quitting !\n", stderr); - return EXIT_FAILURE; - } common_initialize(); - for (int c = 0; (c = getopt(argc, argv, "he:d:p:u")) >= 0; ) { + for (int c = 0; (c = getopt(argc, argv, "hu" "e:d:p:")) >= 0; ) { switch (c) { case 'e': port_enc = atoi(optarg); -- 2.20.1