diff options
author | Sergey Poznyakoff <gray@nxc.no> | 2017-08-16 11:39:33 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@nxc.no> | 2017-08-16 13:29:03 +0300 |
commit | 6b921aed867170fe37e0c1e6ae3df9d550d21d92 (patch) | |
tree | d6e5c007e17531cae77d6fedade5ff18cef890a7 | |
parent | 6c73ee9d7565a1533d27a7cb81beefe211869f64 (diff) | |
download | nssync-6b921aed867170fe37e0c1e6ae3df9d550d21d92.tar.gz nssync-6b921aed867170fe37e0c1e6ae3df9d550d21d92.tar.bz2 |
Get rid of the select loop, use MHD internal polling thread instead
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/nssync.c | 19 | ||||
-rw-r--r-- | src/nssync.h | 6 | ||||
-rw-r--r-- | src/server.c | 46 |
4 files changed, 39 insertions, 36 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 114d367..3999d28 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,11 +27,13 @@ nssync_SOURCES = \ if COND_MICROHTTPD nssync_SOURCES += server.c + PTHREAD_L=-lpthread else nssync_SOURCES += noserver.c + PTHREAD_L= endif -LDADD=@GRECS_LDADD@ ../runcap/libruncap.a +LDADD=$(PTHREAD_L) @GRECS_LDADD@ ../runcap/libruncap.a BUILT_SOURCES = cmdline.h EXTRA_DIST = cmdline.h cmdline.opt diff --git a/src/nssync.c b/src/nssync.c index 13d1246..86025a9 100644 --- a/src/nssync.c +++ b/src/nssync.c @@ -457,8 +457,6 @@ create_pidfile(void) } fprintf(fp, "%lu", (unsigned long) getpid()); fclose(fp); - - atexit(remove_pidfile); } /* Check whether pidfile NAME exists and if so, whether its PID is still @@ -626,10 +624,12 @@ nssync(struct json_value **ret_json) return res; } +/* Signal handing */ void sigfatal(int sig) { - exit(0); + remove_pidfile(); + _exit(0); } void @@ -656,7 +656,7 @@ set_signals(int *sigv, void (*sighandler)(int)) set_signal(sigv[i], sighandler); } -static int fatal_signals[] = { +int fatal_signals[] = { SIGHUP, SIGINT, SIGQUIT, @@ -678,12 +678,12 @@ main(int argc, char **argv) get_host_addresses(); runas(); - - check_pidfile(); + check_pidfile(); + + debug(1,("start up")); if (server_mode) { - set_signals(fatal_signals, sigfatal); - set_signal(SIGPIPE, sigign); + set_signals(fatal_signals, sigign); nssync_server(); } else { set_signals(fatal_signals, sigfatal); @@ -693,6 +693,7 @@ main(int argc, char **argv) exit(EX_UNAVAILABLE); } } - + debug(1,("shut down")); + remove_pidfile(); exit(0); } diff --git a/src/nssync.h b/src/nssync.h index 3887afd..8daed6e 100644 --- a/src/nssync.h +++ b/src/nssync.h @@ -91,8 +91,14 @@ void verror(const char *fmt, va_list ap); void error(const char *fmt, ...) __PRINTFLIKE(1,2); void debug_printf(const char *fmt, ...) __PRINTFLIKE(1,2); void info_printf(const char *fmt, ...) __PRINTFLIKE(1,2); +void start_syslog(void); void runas(void); +void create_pidfile(void); + +void set_signal(int sig, void (*sighandler)(int)); +void set_signals(int *sigv, void (*sighandler)(int)); +extern int fatal_signals[]; void dlz_error(char const *zone, char const *fmt, ...); void dlz_success(char const *zone); diff --git a/src/server.c b/src/server.c index 0086ae3..3c217e1 100644 --- a/src/server.c +++ b/src/server.c @@ -20,6 +20,8 @@ #include <sys/types.h> #include <sys/socket.h> #include <tcpd.h> +#include <signal.h> +#include <pthread.h> #define MHD_PLATFORM_H #include <microhttpd.h> @@ -246,6 +248,9 @@ nssync_server(void) { int fd; struct MHD_Daemon *mhd; + sigset_t sigs; + int i; + int sn; if (!server_addr) { if (grecs_str_to_sockaddr(&server_addr, DEFAULT_NSSYNC_ADDR, @@ -268,37 +273,26 @@ nssync_server(void) start_syslog(); } create_pidfile(); - + + /* Block the 'fatal signals' and SIGPIPE in the handling thread */ + sigemptyset(&sigs); + for (i = 0; fatal_signals[i]; i++) + sigaddset(&sigs, fatal_signals[i]); + sigaddset(&sigs, SIGPIPE); + pthread_sigmask(SIG_BLOCK, &sigs, NULL); + MHD_set_panic_func(nssync_mhd_panic, NULL); - mhd = MHD_start_daemon(MHD_USE_ERROR_LOG, 0, + mhd = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD + | MHD_USE_ERROR_LOG, 0, nssync_mhd_acl, NULL, nssync_mhd_handler, NULL, MHD_OPTION_LISTEN_SOCKET, fd, MHD_OPTION_EXTERNAL_LOGGER, nssync_mhd_logger, NULL, MHD_OPTION_END); - while (1) { - fd_set rs, ws, es; - int max; - int rc; - - FD_ZERO(&rs); - FD_ZERO(&ws); - FD_ZERO(&es); - if (MHD_get_fdset(mhd, &rs, &ws, &es, &max) != MHD_YES) { - //FIXME - break; - } - rc = select(max + 1, &rs, &ws, &es, NULL); - if (rc == -1) { - if (errno == EINTR) - continue; - error("select: %s", strerror(errno)); - break; - } - MHD_run(mhd); - } + /* Unblock only the fatal signals */ + sigdelset(&sigs, SIGPIPE); + pthread_sigmask(SIG_UNBLOCK, &sigs, NULL); + /* Wait for signal to arrive */ + sigwait(&sigs, &sn); MHD_stop_daemon(mhd); - - - exit(0);//FIXME } |