From 774f07dd7f6f2451363ccee42bdfb365cb0fdb19 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Fri, 28 Jul 2023 18:57:13 +0000 Subject: [PATCH] internal/router: only do cleanup in cd mode on freebsd --- cmd/ctrld/cli.go | 4 ++-- internal/router/os_freebsd.go | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/ctrld/cli.go b/cmd/ctrld/cli.go index 2369323..e21868d 100644 --- a/cmd/ctrld/cli.go +++ b/cmd/ctrld/cli.go @@ -283,13 +283,13 @@ func initCLI() { rootCertPool = cp } p.onStarted = append(p.onStarted, func() { - mainLog.Load().Debug().Msg("router setup") + mainLog.Load().Debug().Msg("router setup on start") if err := p.router.Setup(); err != nil { mainLog.Load().Error().Err(err).Msg("could not configure router") } }) p.onStopped = append(p.onStopped, func() { - mainLog.Load().Debug().Msg("router cleanup") + mainLog.Load().Debug().Msg("router cleanup on stop") if err := p.router.Cleanup(); err != nil { mainLog.Load().Error().Err(err).Msg("could not cleanup router") } diff --git a/internal/router/os_freebsd.go b/internal/router/os_freebsd.go index 9d9b738..a84fcaa 100644 --- a/internal/router/os_freebsd.go +++ b/internal/router/os_freebsd.go @@ -109,7 +109,7 @@ func (or *osRouter) Setup() error { } func (or *osRouter) Cleanup() error { - if or.cfg.FirstListener().IsDirectDnsListener() { + if or.cdMode { _ = exec.Command(unboundRcPath, "onerestart").Run() _ = exec.Command(dnsmasqRcPath, "onerestart").Run() } @@ -129,10 +129,22 @@ name="{{.Name}}" rcvar="${name}_enable" {{.Name}}_env="IS_DAEMON=1" pidfile="/var/run/${name}.pid" +child_pidfile="/var/run/${name}_child.pid" command="/usr/sbin/daemon" -daemon_args="-P ${pidfile} -r -t \"${name}: daemon\"{{if .WorkingDirectory}} -c {{.WorkingDirectory}}{{end}}" +daemon_args="-P ${pidfile} -p ${child_pidfile} -t \"${name}: daemon\"{{if .WorkingDirectory}} -c {{.WorkingDirectory}}{{end}}" command_args="${daemon_args} {{.Path}}{{range .Arguments}} {{.}}{{end}}" +stop_cmd="ctrld_stop" + +ctrld_stop() { + pid=$(cat ${pidfile}) + child_pid=$(cat ${child_pidfile}) + if [ -e "${child_pidfile}" ]; then + kill -s TERM "${child_pid}" + wait_for_pids "${child_pid}" "${pidfile}" + fi +} + load_rc_config "${name}" run_rc_command "$1" `