Skip to content

Commit

Permalink
Finish mosquitto_signal for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Oct 29, 2024
1 parent 98a7f43 commit 32e100e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 25 deletions.
42 changes: 30 additions & 12 deletions apps/mosquitto_signal/signal_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#ifdef WIN32
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# include <psapi.h>
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <psapi.h>

#include <ctype.h>
#include <errno.h>
Expand All @@ -32,7 +30,28 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause

#include "mosquitto_signal.h"

void signal_all(const char *eventname)
#undef WITH_TLS
#include "config.h"

static const char *msig_to_string(enum mosq_sig sig)
{
switch(sig){
case MSIG_CONFIG_RELOAD:
return "reload";
case MSIG_LOG_ROTATE:
return "log_rotate";
case MSIG_SHUTDOWN:
return "shutdown";
case MSIG_TREE_PRINT:
return "tree_print";
case MSIG_XTREPORT:
return "xtreport";
default:
return "";
}
}

void signal_all(enum mosq_signal sig)
{
DWORD processes[2048], cbneeded, count;
int pid;
Expand All @@ -43,18 +62,17 @@ void signal_all(const char *eventname)
}

count = cbneeded / sizeof(DWORD);
for(int i=0; i<count; i++){
for(DWORD i=0; i<count; i++){
if(processes[i]){
HANDLE hproc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processes[i]);
if(hproc){
HMODULE hmod;
DWORD modcount;
char procname[MAX_PATH];
if(EnumProcessModules(hproc, &hmod, sizeof(hmod), &cbneeded)){
GetModuleBaseName(hproc, hmod, procname, sizeof(procname));
if(!strcasecmp(procname, "mosquitto.exe")){
pid = GetProcessId(hproc);
signal_one(pid, eventname);
send_signal(pid, sig);
}
}
CloseHandle(hproc);
Expand All @@ -63,13 +81,13 @@ void signal_all(const char *eventname)
}
}

void signal_one(int pid, const char *eventname)
void send_signal(int pid, enum mosq_signal msig)
{
HANDLE evt;
char eventbuf[MAX_PATH+1];
BOOL res;

snprintf(eventbuf, sizeof(eventbuf), "mosq%d_%s", pid, eventname);
snprintf(eventbuf, sizeof(eventbuf), "mosq%d_%s", pid, msig_to_string(sig));
evt = OpenEvent(EVENT_MODIFY_STATE, FALSE, eventbuf);
if(evt){
res = PulseEvent(evt);
Expand Down
52 changes: 39 additions & 13 deletions src/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static bool flag_log_rotate = false;
static bool flag_db_backup = false;
#endif
static bool flag_tree_print = false;
static bool flag_xtreport = false;

static void handle_signal(int signal)
{
Expand All @@ -59,6 +60,7 @@ static void handle_signal(int signal)
#ifdef SIGUSR2
}else if(signal == SIGUSR2){
flag_tree_print = true;
flag_xtreport = true;
#endif
#ifdef SIGRTMIN
}else if(signal == SIGRTMIN){
Expand Down Expand Up @@ -122,43 +124,57 @@ void signal__flag_check(void)
sub__tree_print(db.normal_subs, 0);
sub__tree_print(db.shared_subs, 0);
flag_tree_print = false;
}
#ifdef WITH_XTREPORT
if(flag_xtreport){
xtreport();
#endif
flag_xtreport = false;
}
#endif
}

/*
*
* Signalling mosquitto process on Win32.
*
* On Windows we we can use named events to pass signals to the mosquitto process.
* On Windows we can use named events to pass signals to the mosquitto process.
* List of events :
*
* mosqPID_shutdown
* mosqPID_reload
* mosqPID_backup
* mosqPID_log_rotate
* mosqPID_tree_print
* mosqPID_xtreport
*
* (where PID is the PID of the mosquitto process).
*/
#ifdef WIN32

#define MOSQ_MAX_EVTS 6
DWORD WINAPI SigThreadProc(void* data)
{
TCHAR evt_name[MAX_PATH];
static HANDLE evt[3];
static HANDLE evt[MOSQ_MAX_EVTS];
int pid = GetCurrentProcessId();
const char *evt_names[MOSQ_MAX_EVTS] = {
"shutdown",
"reload",
"backup",
"log_rotate",
"tree_print",
"xtreport"
};

UNUSED(data);

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);
for(int i=0; i<MOSQ_MAX_EVTS; i++){
sprintf_s(evt_name, MAX_PATH, "mosq%d_%s", pid, evt_names[i]);
evt[i] = CreateEvent(NULL, TRUE, FALSE, evt_name);
}

while (g_run) {
int wr = WaitForMultipleObjects(sizeof(evt) / sizeof(HANDLE), evt, FALSE, INFINITE);
int wr = WaitForMultipleObjects(MOSQ_MAX_EVTS, evt, FALSE, INFINITE);
switch (wr) {
case WAIT_OBJECT_0 + 0:
handle_signal(SIGINT);
Expand All @@ -172,11 +188,21 @@ DWORD WINAPI SigThreadProc(void* data)
#endif
continue;
break;
case WAIT_OBJECT_0 + 3:
flag_log_rotate = true;
continue;
case WAIT_OBJECT_0 + 4:
flag_tree_print = true;
continue;
case WAIT_OBJECT_0 + 5:
flag_xtreport = true;
continue;
}
}
CloseHandle(evt[0]);
if(evt[1]) CloseHandle(evt[1]);
if(evt[2]) CloseHandle(evt[2]);
for(int i=0; i<MOSQ_MAX_EVTS; i++){
if(evt[i]) CloseHandle(evt[i]);
}
return 0;
}
#undef MOSQ_MAX_EVTS
#endif

0 comments on commit 32e100e

Please sign in to comment.