rc.d/cmm: stop waits for cmm to exit, and checks it is cmm
The custom stop killed whatever pid the pid file held, ignored whether that worked, removed the file and printed "Stopped cmm." On the gateway cmm was still running right after it. rc.subr's own start, stop and status (command=) send SIGTERM and wait for the exit, and only act on a pid file that names a running cmm. Tested through FreeBSD's rc.subr with a stand-in cmm that takes 2 seconds to exit, and with a stale pid file naming another process. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2e67937271
commit
aa8c49368d
2 changed files with 95 additions and 30 deletions
35
rc.d/cmm
35
rc.d/cmm
|
|
@ -8,6 +8,10 @@
|
|||
# Monitors PF conntrack state changes and offloads flows to CDX
|
||||
# via FCI for hardware-accelerated forwarding.
|
||||
#
|
||||
# cmm daemonizes itself and writes its pid file; rc.subr's stop sends it
|
||||
# SIGTERM and waits until it has exited (it tears its flows down first),
|
||||
# and status and stop check that the pid file names a running cmm.
|
||||
#
|
||||
|
||||
. /etc/rc.subr
|
||||
|
||||
|
|
@ -15,12 +19,9 @@ name="cmm"
|
|||
rcvar="cmm_enable"
|
||||
desc="Connection Management Module for CDX offload"
|
||||
|
||||
command="/usr/local/sbin/${name}"
|
||||
pidfile="/var/run/${name}.pid"
|
||||
|
||||
start_cmd="cmm_start"
|
||||
stop_cmd="cmm_stop"
|
||||
status_cmd="cmm_status"
|
||||
|
||||
start_precmd="cmm_prestart"
|
||||
|
||||
cmm_prestart()
|
||||
|
|
@ -38,32 +39,6 @@ cmm_prestart()
|
|||
fi
|
||||
}
|
||||
|
||||
cmm_start()
|
||||
{
|
||||
echo "Starting ${name}."
|
||||
/usr/local/sbin/cmm ${cmm_flags}
|
||||
}
|
||||
|
||||
cmm_stop()
|
||||
{
|
||||
if [ -f ${pidfile} ]; then
|
||||
kill $(cat ${pidfile}) 2>/dev/null
|
||||
rm -f ${pidfile}
|
||||
echo "Stopped ${name}."
|
||||
else
|
||||
echo "${name} is not running."
|
||||
fi
|
||||
}
|
||||
|
||||
cmm_status()
|
||||
{
|
||||
if [ -f ${pidfile} ] && kill -0 $(cat ${pidfile}) 2>/dev/null; then
|
||||
echo "${name} is running as pid $(cat ${pidfile})."
|
||||
else
|
||||
echo "${name} is not running."
|
||||
fi
|
||||
}
|
||||
|
||||
load_rc_config $name
|
||||
|
||||
: ${cmm_enable:="NO"}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import os
|
|||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
|
|
@ -1122,6 +1123,95 @@ class CheckKernelBinTest(unittest.TestCase):
|
|||
self.assertNotEqual(self.check(path).returncode, 0)
|
||||
|
||||
|
||||
FAKE_CMM_C = r"""
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* stands in for cmm: daemonizes, writes its pid file, and on SIGTERM takes
|
||||
a while to tear down before it removes the pid file and exits */
|
||||
static volatile sig_atomic_t stop;
|
||||
static void term(int sig) { (void)sig; stop = 1; }
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc; (void)argv;
|
||||
if (daemon(0, 0) != 0) return 1;
|
||||
FILE *f = fopen(PIDFILE, "w");
|
||||
if (f == NULL) return 1;
|
||||
fprintf(f, "%d\n", (int)getpid());
|
||||
fclose(f);
|
||||
signal(SIGTERM, term);
|
||||
while (!stop) pause();
|
||||
sleep(2);
|
||||
unlink(PIDFILE);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
@unittest.skipUnless(os.path.exists("/etc/rc.subr") and shutil.which("cc"), "needs FreeBSD's rc.subr and cc")
|
||||
class CmmRcTest(unittest.TestCase):
|
||||
"""rc.d/cmm through FreeBSD's rc.subr, with a stand-in cmm."""
|
||||
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.mkdtemp(prefix="gw-cmm-")
|
||||
self.addCleanup(shutil.rmtree, self.tmp)
|
||||
self.pidfile = os.path.join(self.tmp, "cmm.pid")
|
||||
self.daemon = os.path.join(self.tmp, "cmm")
|
||||
src = os.path.join(self.tmp, "cmm.c")
|
||||
with open(src, "w") as f:
|
||||
f.write(FAKE_CMM_C)
|
||||
subprocess.run(["cc", f'-DPIDFILE="{self.pidfile}"', "-o", self.daemon, src], check=True)
|
||||
with open(os.path.join(REPO, "rc.d", "cmm")) as f:
|
||||
script = f.read()
|
||||
for a, b in (('command="/usr/local/sbin/${name}"', f'command="{self.daemon}"'),
|
||||
('pidfile="/var/run/${name}.pid"', f'pidfile="{self.pidfile}"'),
|
||||
('start_precmd="cmm_prestart"', 'start_precmd=""')): # no cdx/fci here
|
||||
self.assertIn(a, script)
|
||||
script = script.replace(a, b)
|
||||
self.rc = os.path.join(self.tmp, "rc.cmm")
|
||||
with open(self.rc, "w") as f:
|
||||
f.write(script)
|
||||
self.addCleanup(self.kill_leftovers)
|
||||
|
||||
def kill_leftovers(self):
|
||||
subprocess.run(["pkill", "-f", self.daemon], capture_output=True)
|
||||
|
||||
def run_rc(self, cmd):
|
||||
return subprocess.run(["sh", self.rc, cmd], capture_output=True, text=True, timeout=60)
|
||||
|
||||
def running(self):
|
||||
return subprocess.run(["pgrep", "-f", self.daemon], capture_output=True).returncode == 0
|
||||
|
||||
def test_start_status_stop(self):
|
||||
r = self.run_rc("onestart")
|
||||
self.assertEqual(r.returncode, 0, r.stderr)
|
||||
for _ in range(50):
|
||||
if os.path.exists(self.pidfile):
|
||||
break
|
||||
time.sleep(0.1)
|
||||
self.assertTrue(self.running())
|
||||
self.assertIn("is running as pid", self.run_rc("onestatus").stdout)
|
||||
r = self.run_rc("onestop")
|
||||
self.assertEqual(r.returncode, 0, r.stderr)
|
||||
# stop returns only once cmm has exited, which takes it 2 seconds here
|
||||
self.assertFalse(self.running(), r.stdout)
|
||||
self.assertFalse(os.path.exists(self.pidfile))
|
||||
self.assertNotEqual(self.run_rc("onestatus").returncode, 0)
|
||||
|
||||
def test_stale_pidfile_names_another_process(self):
|
||||
other = subprocess.Popen(["sleep", "30"])
|
||||
self.addCleanup(lambda: (other.kill(), other.wait()))
|
||||
with open(self.pidfile, "w") as f:
|
||||
f.write(f"{other.pid}\n")
|
||||
r = self.run_rc("onestop")
|
||||
self.assertNotEqual(r.returncode, 0)
|
||||
self.assertIn("not running", r.stdout + r.stderr)
|
||||
self.assertIsNone(other.poll(), "stop killed a process that is not cmm")
|
||||
|
||||
|
||||
class SignAuxRepoTest(unittest.TestCase):
|
||||
"""build.sh's sign_aux_repo: the aux set's catalogue signed with KEYDIR's key."""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue