From 854a244ebb3c454c6358ff8f310f2c578d3f44b4 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 9 Aug 2023 02:45:38 +0000 Subject: [PATCH] Fix restart command when ctrld service was already stopped --- cmd/ctrld/cli.go | 11 +++---- cmd/ctrld/service.go | 10 ++++++ internal/router/synology/synology.go | 49 +++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/cmd/ctrld/cli.go b/cmd/ctrld/cli.go index 7a4d5aa..9396090 100644 --- a/cmd/ctrld/cli.go +++ b/cmd/ctrld/cli.go @@ -550,13 +550,10 @@ func initCLI() { return } initLogging() - tasks := []task{{s.Restart, true}} - // On Windows, s.Restart will return error unless service is running. - if runtime.GOOS == "windows" { - tasks = []task{ - {s.Start, false}, - {s.Restart, true}, - } + + tasks := []task{ + {s.Stop, false}, + {s.Start, true}, } if doTasks(tasks) { dir, err := userHomeDir() diff --git a/cmd/ctrld/service.go b/cmd/ctrld/service.go index dfec02e..28a70c6 100644 --- a/cmd/ctrld/service.go +++ b/cmd/ctrld/service.go @@ -63,6 +63,16 @@ func (s *sysV) Stop() error { return err } +func (s *sysV) Restart() error { + if !s.installed() { + return service.ErrNotInstalled + } + // We don't care about error returned by s.Stop, + // because the service may already be stopped. + _ = s.Stop() + return s.Start() +} + func (s *sysV) Status() (service.Status, error) { if !s.installed() { return service.StatusUnknown, service.ErrNotInstalled diff --git a/internal/router/synology/synology.go b/internal/router/synology/synology.go index 3ad0388..a367d4a 100644 --- a/internal/router/synology/synology.go +++ b/internal/router/synology/synology.go @@ -27,7 +27,8 @@ func New(cfg *ctrld.Config) *Synology { return &Synology{cfg: cfg} } -func (s *Synology) ConfigureService(config *service.Config) error { +func (s *Synology) ConfigureService(svc *service.Config) error { + svc.Option["UpstartScript"] = upstartScript return nil } @@ -86,3 +87,49 @@ func restartDNSMasq() error { } return nil } + +// Copied from https://github.com/kardianos/service/blob/6fe2824ee8248e776b0f8be39aaeff45a45a4f6c/service_upstart_linux.go#L232 +// With modification to wait for dhcpserver started before ctrld. + +// The upstart script should stop with an INT or the Go runtime will terminate +// the program before the Stop handler can run. +const upstartScript = `# {{.Description}} + +{{if .DisplayName}}description "{{.DisplayName}}"{{end}} + +{{if .HasKillStanza}}kill signal INT{{end}} +{{if .ChRoot}}chroot {{.ChRoot}}{{end}} +{{if .WorkingDirectory}}chdir {{.WorkingDirectory}}{{end}} +start on filesystem or runlevel [2345] +stop on runlevel [!2345] + +start on started dhcpserver + +{{if and .UserName .HasSetUIDStanza}}setuid {{.UserName}}{{end}} + +respawn +respawn limit 10 5 +umask 022 + +console none + +pre-start script + test -x {{.Path}} || { stop; exit 0; } +end script + +# Start +script + {{if .LogOutput}} + stdout_log="/var/log/{{.Name}}.out" + stderr_log="/var/log/{{.Name}}.err" + {{end}} + + if [ -f "/etc/sysconfig/{{.Name}}" ]; then + set -a + source /etc/sysconfig/{{.Name}} + set +a + fi + + exec {{if and .UserName (not .HasSetUIDStanza)}}sudo -E -u {{.UserName}} {{end}}{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}{{if .LogOutput}} >> $stdout_log 2>> $stderr_log{{end}} +end script +`