Fix signal-args bad behaviour

Closes #3644. Thanks to Gianfranco Costamagna.
This commit is contained in:
Roger A. Light 2026-05-11 18:23:21 +01:00
parent 5a830e1a78
commit 41246e3f05
No known key found for this signature in database
GPG key ID: 779B22DFB3E717B7

View file

@ -4,10 +4,21 @@
from mosq_test_helper import *
def do_test(args, rc_expected, response=None, input=None):
proc = subprocess.run([mosq_paths.mosquitto_signal]
+ args,
capture_output=True, encoding='utf-8', timeout=2, input=input)
import os
import platform
import signal
def do_test(args, rc_expected, response=None, input=None, sig=None):
if sig is not None:
signal.signal(sig, signal.SIG_IGN)
try:
proc = subprocess.run([mosq_paths.mosquitto_signal]
+ args,
capture_output=True, encoding='utf-8', timeout=2, input=input)
except KeyboardInterrupt:
if sig == signal.SIGINT:
pass
if response is not None:
if response not in proc.stderr:
@ -19,18 +30,37 @@ def do_test(args, rc_expected, response=None, input=None):
print(proc.returncode)
raise ValueError(args)
if sig is not None:
signal.signal(sig, signal.SIG_DFL)
if platform.system() == 'Windows':
sighup = None
sigint = None
sigusr1 = None
sigusr2 = None
sigrtmin = None
else:
sighup = signal.SIGHUP
sigint = signal.SIGINT
sigusr1 = signal.SIGUSR1
sigusr2 = signal.SIGUSR2
sigrtmin = signal.SIGRTMIN
pid = os.getpid()
do_test([], 1) # For the usage message
do_test(["--help"], 1)
do_test(["--invalid"], 1, response="Error: One of -a or -p must be used.")
do_test(["-p"], 1, response="Error: -p argument given but process ID missing.")
do_test(["-p", "0"], 1, response="Error: Process ID must be >0.")
do_test(["-p", "1"], 1, response="Error: No signal given.")
do_test(["-p", str(pid)], 1, response="Error: No signal given.")
do_test(["-a"], 1, response="Error: No signal given.")
do_test(["-p", "1", "invalid"], 1, response="Error: Unknown signal 'invalid'.")
do_test(["-p", "1", "config-reload"], 0)
do_test(["-p", "1", "log-rotate"], 0)
do_test(["-p", "1", "shutdown"], 0)
do_test(["-p", "1", "tree-print"], 0)
do_test(["-p", "1", "xtreport"], 0)
do_test(["-p", str(pid), "invalid"], 1, response="Error: Unknown signal 'invalid'.")
do_test(["-p", str(pid), "config-reload"], 0, sig=sighup)
do_test(["-p", str(pid), "log-rotate"], 0, sig=sighup)
do_test(["-p", str(pid), "shutdown"], 0, sig=sigint)
do_test(["-p", str(pid), "tree-print"], 0, sig=sigusr2)
do_test(["-p", str(pid), "xtreport"], 0, sig=sigrtmin)
do_test(["-a", "config-reload"], 0)
exit(0)