diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1b447cea..0b9e783a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -37,6 +37,7 @@ set (MOSQ_SRCS ../lib/send_disconnect.c ../lib/send_publish.c send_suback.c + signals.c ../lib/send_subscribe.c ../lib/send_unsubscribe.c sys_tree.c sys_tree.h diff --git a/src/Makefile b/src/Makefile index 7dccade9..7d80c6f7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -43,6 +43,7 @@ OBJS= mosquitto.o \ send_subscribe.o \ send_unsubscribe.o \ service.o \ + signals.o \ subs.o \ sys_tree.o \ time_mosq.o \ @@ -160,6 +161,9 @@ send_unsubscribe.o : ../lib/send_unsubscribe.c ../lib/send_mosq.h service.o : service.c mosquitto_broker.h ${CROSS_COMPILE}${CC} $(BROKER_CFLAGS) -c $< -o $@ +signals.o : signals.c mosquitto_broker.h + ${CROSS_COMPILE}${CC} $(BROKER_CFLAGS) -c $< -o $@ + subs.o : subs.c mosquitto_broker.h ${CROSS_COMPILE}${CC} $(BROKER_CFLAGS) -c $< -o $@ diff --git a/src/mosquitto.c b/src/mosquitto.c index f6d83aac..7367d70d 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -71,6 +71,9 @@ int deny_severity = LOG_INFO; void handle_sigint(int signal); void handle_sigusr1(int signal); void handle_sigusr2(int signal); +#ifdef SIGHUP +void handle_sighup(int signal); +#endif struct mosquitto_db *mosquitto__get_db(void) { @@ -157,27 +160,6 @@ int restore_privileges(void) return MOSQ_ERR_SUCCESS; } -#ifdef SIGHUP -/* Signal handler for SIGHUP - flag a config reload. */ -void handle_sighup(int signal) -{ - flag_reload = true; -} -#endif - -/* Signal handler for SIGINT and SIGTERM - just stop gracefully. */ -void handle_sigint(int signal) -{ - run = 0; -} - -/* Signal handler for SIGUSR1 - backup the db. */ -void handle_sigusr1(int signal) -{ -#ifdef WITH_PERSISTENCE - flag_db_backup = true; -#endif -} void mosquitto__daemonise(void) { @@ -208,64 +190,6 @@ void mosquitto__daemonise(void) #endif } -/* Signal handler for SIGUSR2 - vacuum the db. */ -void handle_sigusr2(int signal) -{ - flag_tree_print = true; -} - -/* -* -* Signalling mosquitto process on Win32. -* -* On Windows we we can use named events to pass signals to the mosquitto process. -* List of events : -* -* mosqPID_shutdown -* mosqPID_reload -* mosqPID_backup -* mosqPID_vacuum -* -* (where PID is the PID of the mosquitto process). -*/ -#ifdef WIN32 -DWORD WINAPI SigThreadProc(void* data) { - TCHAR evt_name[MAX_PATH]; - static HANDLE evt[4]; - int pid = GetCurrentProcessId(); - sprintf_s(evt_name, MAX_PATH, "mosq%d_shutdown", pid); - evt[0] = CreateEvent(NULL, TRUE, FALSE, evt_name); - sprintf_s(evt_name, MAX_PATH, "mosq%d_reload", pid); - evt[1] = CreateEvent(NULL, FALSE, FALSE, evt_name); - sprintf_s(evt_name, MAX_PATH, "mosq%d_backup", pid); - evt[2] = CreateEvent(NULL, FALSE, FALSE, evt_name); - sprintf_s(evt_name, MAX_PATH, "mosq%d_vacuum", pid); - evt[3] = CreateEvent(NULL, FALSE, FALSE, evt_name); - while (true) { - int wr = WaitForMultipleObjects(sizeof(evt) / sizeof(HANDLE), evt, FALSE, INFINITE); - switch (wr) { - case WAIT_OBJECT_0 + 0: - handle_sigint(SIGINT); - break; - case WAIT_OBJECT_0 + 1: - flag_reload = true; - continue; - case WAIT_OBJECT_0 + 2: - handle_sigusr1(0); - continue; - case WAIT_OBJECT_0 + 3: - handle_sigusr2(0); - continue; - } - break; - } - CloseHandle(evt[0]); - CloseHandle(evt[1]); - CloseHandle(evt[2]); - CloseHandle(evt[3]); - return 0; -} -#endif int main(int argc, char *argv[]) { diff --git a/src/signals.c b/src/signals.c new file mode 100644 index 00000000..3f93dcaf --- /dev/null +++ b/src/signals.c @@ -0,0 +1,154 @@ +/* +Copyright (c) 2016 Roger Light + +All rights reserved. This program and the accompanying materials +are made available under the terms of the Eclipse Public License v1.0 +and Eclipse Distribution License v1.0 which accompany this distribution. + +The Eclipse Public License is available at + http://www.eclipse.org/legal/epl-v10.html +and the Eclipse Distribution License is available at + http://www.eclipse.org/org/documents/edl-v10.php. + +Contributors: + Roger Light - initial implementation and documentation. + Dmitry Kaukov - windows named events implementation. +*/ + +#include "config.h" + +#ifndef WIN32 +/* For initgroups() */ +# define _BSD_SOURCE +# include +# include +# include +#endif + +#ifndef WIN32 +#include +#else +#include +#include +#include +#endif + +#ifndef WIN32 +# include +#endif + +#include +#include +#include +#include +#ifdef WITH_SYSTEMD +# include +#endif +#ifdef WITH_WRAP +#include +#endif +#ifdef WITH_WEBSOCKETS +# include +#endif + +#include "mosquitto_broker.h" +#include "memory_mosq.h" +#include "util_mosq.h" + +struct mosquitto_db int_db; + +extern bool flag_reload; +#ifdef WITH_PERSISTENCE +extern bool flag_db_backup; +#endif +extern bool flag_tree_print; +extern int run; + +void handle_sigint(int signal); +void handle_sigusr1(int signal); +void handle_sigusr2(int signal); + +#ifdef SIGHUP +/* Signal handler for SIGHUP - flag a config reload. */ +void handle_sighup(int signal) +{ + flag_reload = true; +} +#endif + +/* Signal handler for SIGINT and SIGTERM - just stop gracefully. */ +void handle_sigint(int signal) +{ + run = 0; +} + +/* Signal handler for SIGUSR1 - backup the db. */ +void handle_sigusr1(int signal) +{ +#ifdef WITH_PERSISTENCE + flag_db_backup = true; +#endif +} + +/* Signal handler for SIGUSR2 - vacuum the db. */ +void handle_sigusr2(int signal) +{ + flag_tree_print = true; +} + +/* + * + * Signalling mosquitto process on Win32. + * + * On Windows we we can use named events to pass signals to the mosquitto process. + * List of events : + * + * mosqPID_shutdown + * mosqPID_reload + * mosqPID_backup + * mosqPID_vacuum + * + * (where PID is the PID of the mosquitto process). + */ +#ifdef WIN32 +DWORD WINAPI SigThreadProc(void* data) +{ + TCHAR evt_name[MAX_PATH]; + static HANDLE evt[4]; + int pid = GetCurrentProcessId(); + + sprintf_s(evt_name, MAX_PATH, "mosq%d_shutdown", pid); + evt[0] = CreateEvent(NULL, TRUE, FALSE, evt_name); + sprintf_s(evt_name, MAX_PATH, "mosq%d_reload", pid); + evt[1] = CreateEvent(NULL, FALSE, FALSE, evt_name); + sprintf_s(evt_name, MAX_PATH, "mosq%d_backup", pid); + evt[2] = CreateEvent(NULL, FALSE, FALSE, evt_name); + sprintf_s(evt_name, MAX_PATH, "mosq%d_vacuum", pid); + evt[3] = CreateEvent(NULL, FALSE, FALSE, evt_name); + + while (true) { + int wr = WaitForMultipleObjects(sizeof(evt) / sizeof(HANDLE), evt, FALSE, INFINITE); + switch (wr) { + case WAIT_OBJECT_0 + 0: + handle_sigint(SIGINT); + break; + case WAIT_OBJECT_0 + 1: + flag_reload = true; + continue; + case WAIT_OBJECT_0 + 2: + handle_sigusr1(0); + continue; + case WAIT_OBJECT_0 + 3: + handle_sigusr2(0); + continue; + } + break; + } + CloseHandle(evt[0]); + CloseHandle(evt[1]); + CloseHandle(evt[2]); + CloseHandle(evt[3]); + return 0; +} +#endif +