Skip to content

Commit 603a024

Browse files
committed
Finish mosquitto_signal for Windows
1 parent 98a7f43 commit 603a024

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

apps/mosquitto_signal/signal_windows.c

+30-12
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
1515
Contributors:
1616
Roger Light - initial implementation and documentation.
1717
*/
18-
#ifdef WIN32
19-
# ifndef WIN32_LEAN_AND_MEAN
20-
# define WIN32_LEAN_AND_MEAN
21-
# endif
22-
# include <windows.h>
23-
# include <psapi.h>
18+
#ifndef WIN32_LEAN_AND_MEAN
19+
# define WIN32_LEAN_AND_MEAN
2420
#endif
21+
#include <windows.h>
22+
#include <psapi.h>
2523

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

3331
#include "mosquitto_signal.h"
3432

35-
void signal_all(const char *eventname)
33+
#undef WITH_TLS
34+
#include "config.h"
35+
36+
static const char *msig_to_string(enum mosq_sig msig)
37+
{
38+
switch(msig){
39+
case MSIG_CONFIG_RELOAD:
40+
return "reload";
41+
case MSIG_LOG_ROTATE:
42+
return "log_rotate";
43+
case MSIG_SHUTDOWN:
44+
return "shutdown";
45+
case MSIG_TREE_PRINT:
46+
return "tree_print";
47+
case MSIG_XTREPORT:
48+
return "xtreport";
49+
default:
50+
return "";
51+
}
52+
}
53+
54+
void signal_all(enum mosq_signal msig)
3655
{
3756
DWORD processes[2048], cbneeded, count;
3857
int pid;
@@ -43,18 +62,17 @@ void signal_all(const char *eventname)
4362
}
4463

4564
count = cbneeded / sizeof(DWORD);
46-
for(int i=0; i<count; i++){
65+
for(DWORD i=0; i<count; i++){
4766
if(processes[i]){
4867
HANDLE hproc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processes[i]);
4968
if(hproc){
5069
HMODULE hmod;
51-
DWORD modcount;
5270
char procname[MAX_PATH];
5371
if(EnumProcessModules(hproc, &hmod, sizeof(hmod), &cbneeded)){
5472
GetModuleBaseName(hproc, hmod, procname, sizeof(procname));
5573
if(!strcasecmp(procname, "mosquitto.exe")){
5674
pid = GetProcessId(hproc);
57-
signal_one(pid, eventname);
75+
send_signal(pid, msig);
5876
}
5977
}
6078
CloseHandle(hproc);
@@ -63,13 +81,13 @@ void signal_all(const char *eventname)
6381
}
6482
}
6583

66-
void signal_one(int pid, const char *eventname)
84+
void send_signal(int pid, enum mosq_signal msig)
6785
{
6886
HANDLE evt;
6987
char eventbuf[MAX_PATH+1];
7088
BOOL res;
7189

72-
snprintf(eventbuf, sizeof(eventbuf), "mosq%d_%s", pid, eventname);
90+
snprintf(eventbuf, sizeof(eventbuf), "mosq%d_%s", pid, msig_to_string(msig));
7391
evt = OpenEvent(EVENT_MODIFY_STATE, FALSE, eventbuf);
7492
if(evt){
7593
res = PulseEvent(evt);

src/signals.c

+39-13
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static bool flag_log_rotate = false;
3939
static bool flag_db_backup = false;
4040
#endif
4141
static bool flag_tree_print = false;
42+
static bool flag_xtreport = false;
4243

4344
static void handle_signal(int signal)
4445
{
@@ -59,6 +60,7 @@ static void handle_signal(int signal)
5960
#ifdef SIGUSR2
6061
}else if(signal == SIGUSR2){
6162
flag_tree_print = true;
63+
flag_xtreport = true;
6264
#endif
6365
#ifdef SIGRTMIN
6466
}else if(signal == SIGRTMIN){
@@ -122,43 +124,57 @@ void signal__flag_check(void)
122124
sub__tree_print(db.normal_subs, 0);
123125
sub__tree_print(db.shared_subs, 0);
124126
flag_tree_print = false;
127+
}
125128
#ifdef WITH_XTREPORT
129+
if(flag_xtreport){
126130
xtreport();
127-
#endif
131+
flag_xtreport = false;
128132
}
133+
#endif
129134
}
130135

131136
/*
132137
*
133138
* Signalling mosquitto process on Win32.
134139
*
135-
* On Windows we we can use named events to pass signals to the mosquitto process.
140+
* On Windows we can use named events to pass signals to the mosquitto process.
136141
* List of events :
137142
*
138143
* mosqPID_shutdown
139144
* mosqPID_reload
140145
* mosqPID_backup
146+
* mosqPID_log_rotate
147+
* mosqPID_tree_print
148+
* mosqPID_xtreport
141149
*
142150
* (where PID is the PID of the mosquitto process).
143151
*/
144152
#ifdef WIN32
153+
154+
#define MOSQ_MAX_EVTS 6
145155
DWORD WINAPI SigThreadProc(void* data)
146156
{
147157
TCHAR evt_name[MAX_PATH];
148-
static HANDLE evt[3];
158+
static HANDLE evt[MOSQ_MAX_EVTS];
149159
int pid = GetCurrentProcessId();
160+
const char *evt_names[MOSQ_MAX_EVTS] = {
161+
"shutdown",
162+
"reload",
163+
"backup",
164+
"log_rotate",
165+
"tree_print",
166+
"xtreport"
167+
};
150168

151169
UNUSED(data);
152170

153-
sprintf_s(evt_name, MAX_PATH, "mosq%d_shutdown", pid);
154-
evt[0] = CreateEvent(NULL, TRUE, FALSE, evt_name);
155-
sprintf_s(evt_name, MAX_PATH, "mosq%d_reload", pid);
156-
evt[1] = CreateEvent(NULL, FALSE, FALSE, evt_name);
157-
sprintf_s(evt_name, MAX_PATH, "mosq%d_backup", pid);
158-
evt[2] = CreateEvent(NULL, FALSE, FALSE, evt_name);
171+
for(int i=0; i<MOSQ_MAX_EVTS; i++){
172+
sprintf_s(evt_name, MAX_PATH, "mosq%d_%s", pid, evt_names[i]);
173+
evt[i] = CreateEvent(NULL, TRUE, FALSE, evt_name);
174+
}
159175

160176
while (g_run) {
161-
int wr = WaitForMultipleObjects(sizeof(evt) / sizeof(HANDLE), evt, FALSE, INFINITE);
177+
int wr = WaitForMultipleObjects(MOSQ_MAX_EVTS, evt, FALSE, INFINITE);
162178
switch (wr) {
163179
case WAIT_OBJECT_0 + 0:
164180
handle_signal(SIGINT);
@@ -172,11 +188,21 @@ DWORD WINAPI SigThreadProc(void* data)
172188
#endif
173189
continue;
174190
break;
191+
case WAIT_OBJECT_0 + 3:
192+
flag_log_rotate = true;
193+
continue;
194+
case WAIT_OBJECT_0 + 4:
195+
flag_tree_print = true;
196+
continue;
197+
case WAIT_OBJECT_0 + 5:
198+
flag_xtreport = true;
199+
continue;
175200
}
176201
}
177-
CloseHandle(evt[0]);
178-
if(evt[1]) CloseHandle(evt[1]);
179-
if(evt[2]) CloseHandle(evt[2]);
202+
for(int i=0; i<MOSQ_MAX_EVTS; i++){
203+
if(evt[i]) CloseHandle(evt[i]);
204+
}
180205
return 0;
181206
}
207+
#undef MOSQ_MAX_EVTS
182208
#endif

0 commit comments

Comments
 (0)