Move signals/events code to own file.

This commit is contained in:
Roger A. Light 2016-07-03 22:40:27 +01:00
parent 6bf8b59d89
commit d54359598e
4 changed files with 162 additions and 79 deletions

View File

@ -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

View File

@ -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 $@

View File

@ -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[])
{

154
src/signals.c Normal file
View File

@ -0,0 +1,154 @@
/*
Copyright (c) 2016 Roger Light <roger@atchoo.org>
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 <unistd.h>
# include <grp.h>
# include <assert.h>
#endif
#ifndef WIN32
#include <pwd.h>
#else
#include <process.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#ifndef WIN32
# include <sys/time.h>
#endif
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#ifdef WITH_SYSTEMD
# include <systemd/sd-daemon.h>
#endif
#ifdef WITH_WRAP
#include <tcpd.h>
#endif
#ifdef WITH_WEBSOCKETS
# include <libwebsockets.h>
#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