2016-07-03 21:40:27 +00:00
|
|
|
/*
|
2020-02-27 23:26:58 +00:00
|
|
|
Copyright (c) 2016-2020 Roger Light <roger@atchoo.org>
|
2016-07-03 21:40:27 +00:00
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials
|
2020-11-25 17:34:21 +00:00
|
|
|
are made available under the terms of the Eclipse Public License 2.0
|
2016-07-03 21:40:27 +00:00
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
|
|
|
|
|
|
|
The Eclipse Public License is available at
|
2020-11-25 17:34:21 +00:00
|
|
|
https://www.eclipse.org/legal/epl-2.0/
|
2016-07-03 21:40:27 +00:00
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
|
|
|
|
2020-12-01 18:21:59 +00:00
|
|
|
SPDX-License-Identifier: EPL-2.0 OR EDL-1.0
|
|
|
|
|
2016-07-03 21:40:27 +00:00
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
Dmitry Kaukov - windows named events implementation.
|
|
|
|
*/
|
2020-12-02 19:41:46 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
# ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# endif
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
2016-07-03 21:40:27 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2020-12-02 19:41:46 +00:00
|
|
|
#include <stdio.h>
|
2020-11-04 22:20:50 +00:00
|
|
|
#include <stdbool.h>
|
2016-07-03 21:40:27 +00:00
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#ifdef WITH_PERSISTENCE
|
|
|
|
extern bool flag_db_backup;
|
|
|
|
#endif
|
2020-11-04 22:20:50 +00:00
|
|
|
extern bool flag_reload;
|
2016-07-03 21:40:27 +00:00
|
|
|
extern bool flag_tree_print;
|
|
|
|
extern int run;
|
|
|
|
|
|
|
|
#ifdef SIGHUP
|
|
|
|
/* Signal handler for SIGHUP - flag a config reload. */
|
|
|
|
void handle_sighup(int signal)
|
|
|
|
{
|
2019-03-13 14:11:50 +00:00
|
|
|
UNUSED(signal);
|
|
|
|
|
2016-07-03 21:40:27 +00:00
|
|
|
flag_reload = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Signal handler for SIGINT and SIGTERM - just stop gracefully. */
|
|
|
|
void handle_sigint(int signal)
|
|
|
|
{
|
2019-03-13 14:11:50 +00:00
|
|
|
UNUSED(signal);
|
|
|
|
|
2016-07-03 21:40:27 +00:00
|
|
|
run = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Signal handler for SIGUSR1 - backup the db. */
|
|
|
|
void handle_sigusr1(int signal)
|
|
|
|
{
|
2019-03-13 14:11:50 +00:00
|
|
|
UNUSED(signal);
|
|
|
|
|
2016-07-03 21:40:27 +00:00
|
|
|
#ifdef WITH_PERSISTENCE
|
|
|
|
flag_db_backup = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-03 21:42:43 +00:00
|
|
|
/* Signal handler for SIGUSR2 - print subscription / retained tree. */
|
2016-07-03 21:40:27 +00:00
|
|
|
void handle_sigusr2(int signal)
|
|
|
|
{
|
2019-03-13 14:11:50 +00:00
|
|
|
UNUSED(signal);
|
|
|
|
|
2016-07-03 21:40:27 +00:00
|
|
|
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
|
|
|
|
*
|
|
|
|
* (where PID is the PID of the mosquitto process).
|
|
|
|
*/
|
|
|
|
#ifdef WIN32
|
|
|
|
DWORD WINAPI SigThreadProc(void* data)
|
|
|
|
{
|
|
|
|
TCHAR evt_name[MAX_PATH];
|
2018-09-25 04:43:40 +00:00
|
|
|
static HANDLE evt[3];
|
2016-07-03 21:40:27 +00:00
|
|
|
int pid = GetCurrentProcessId();
|
|
|
|
|
2021-01-09 20:59:42 +00:00
|
|
|
UNUSED(data);
|
|
|
|
|
2016-07-03 21:40:27 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
int wr = WaitForMultipleObjects(sizeof(evt) / sizeof(HANDLE), evt, FALSE, INFINITE);
|
|
|
|
switch (wr) {
|
2018-01-06 22:42:13 +00:00
|
|
|
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;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-03 21:40:27 +00:00
|
|
|
}
|
|
|
|
CloseHandle(evt[0]);
|
|
|
|
CloseHandle(evt[1]);
|
|
|
|
CloseHandle(evt[2]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|