From 6547573c4d81728a63f7679dbd663ad37c652694 Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Mon, 10 Oct 2022 23:56:32 +0200 Subject: [PATCH 1/4] Do not create windows services with an empty environment key (#343) Doing so will create services that are broken and will fail when started: `Error 87: The parameter is incorrect` This is a regression from https://github.com/kardianos/service/pull/312 --- service_windows.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/service_windows.go b/service_windows.go index 07ad04a..6b0333f 100644 --- a/service_windows.go +++ b/service_windows.go @@ -241,6 +241,10 @@ func lowPrivSvc(m *mgr.Mgr, name string) (*mgr.Service, error) { } func (ws *windowsService) setEnvironmentVariablesInRegistry() error { + if len(ws.EnvVars) == 0 { + return nil + } + k, _, err := registry.CreateKey( registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\`+ws.Name, registry.QUERY_VALUE|registry.SET_VALUE|registry.CREATE_SUB_KEY) From 380dcf887eeb2694bdc25540951e7b7259a2f145 Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Mon, 10 Oct 2022 23:59:33 +0200 Subject: [PATCH 2/4] Replace deprecated IsAnInteractiveSession() call (#344) Using the service manager from an remote ssh command promt fails with `The service process could not connect to the service controller` Thanks to the detailed analysis from @kelseyma the fix was very straight forward. Using IsWindowsService() solved this problem for me. The mentioned issue at https://github.com/golang/go/issues/44921 has also been fixed in the meantime, so there is no reason not to use IsWindowsService() instead. Fixes #300 --- service_windows.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service_windows.go b/service_windows.go index 6b0333f..2b854d7 100644 --- a/service_windows.go +++ b/service_windows.go @@ -149,11 +149,11 @@ func (l WindowsLogger) NInfof(eventID uint32, format string, a ...interface{}) e var interactive = false func init() { - var err error - interactive, err = svc.IsAnInteractiveSession() + isService, err := svc.IsWindowsService() if err != nil { panic(err) } + interactive = !isService } func (ws *windowsService) String() string { From 645b10f1a80a7242c3650f8a2f7fb4d7fc55967c Mon Sep 17 00:00:00 2001 From: optim-aizawa <66113133+optim-aizawa@users.noreply.github.com> Date: Tue, 11 Oct 2022 07:00:23 +0900 Subject: [PATCH 3/4] Add EnvVars to linux init.d (#339) * change module name * add init.d env * add init.d env --- service_openrc_linux.go | 4 ++++ service_sysv_linux.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/service_openrc_linux.go b/service_openrc_linux.go index 72eaba4..c678e3a 100644 --- a/service_openrc_linux.go +++ b/service_openrc_linux.go @@ -230,6 +230,10 @@ command_args="{{range .Arguments}}{{.}} {{end}}" name=$(basename $(readlink -f $command)) supervise_daemon_args="--stdout {{.LogDirectory}}/${name}.log --stderr {{.LogDirectory}}/${name}.err" +{{range $k, $v := .EnvVars -}} +export {{$k}}={{$v}} +{{end -}} + {{- if .Dependencies }} depend() { {{- range $i, $dep := .Dependencies}} diff --git a/service_sysv_linux.go b/service_sysv_linux.go index 4865040..5a98a51 100644 --- a/service_sysv_linux.go +++ b/service_sysv_linux.go @@ -207,6 +207,10 @@ pid_file="/var/run/$name.pid" stdout_log="{{.LogDirectory}}/$name.log" stderr_log="{{.LogDirectory}}/$name.err" +{{range $k, $v := .EnvVars -}} +export {{$k}}={{$v}} +{{end -}} + [ -e /etc/sysconfig/$name ] && . /etc/sysconfig/$name get_pid() { From f4a4df2b5eb5bdd0169d5ec60e48bbb4d0a6e029 Mon Sep 17 00:00:00 2001 From: Nate Felton Date: Mon, 10 Oct 2022 18:02:22 -0400 Subject: [PATCH 4/4] Update launchd template (#346) * Update XML prolog and DTD Also converts single-quotes to double-quotes * Remove whitespace from parent dict * Only populate EnvironmentVariables when available * Only add additional arguments when defined Also cleans up whitespace around each argument * Cleanup whitespace and only populate when defined * Convert remaining spaces to tabs * Sort keys. Similar to `plutil -convert xml1` --- service_darwin.go | 88 +++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/service_darwin.go b/service_darwin.go index 8379714..727ef2c 100644 --- a/service_darwin.go +++ b/service_darwin.go @@ -290,46 +290,58 @@ func (s *darwinLaunchdService) SystemLogger(errs chan<- error) (Logger, error) { return newSysLogger(s.Name, errs) } -var launchdConfig = ` - - - +var launchdConfig = ` + + + + Disabled + + {{- if .EnvVars}} EnvironmentVariables - {{range $k, $v := .EnvVars -}} - {{html $k}} - {{html $v}} - {{end -}} + {{- range $k, $v := .EnvVars}} + {{html $k}} + {{html $v}} + {{- end}} - Label - {{html .Name}} - ProgramArguments - - {{html .Path}} - {{range .Config.Arguments}} - {{html .}} - {{end}} - - {{if .UserName}}UserName - {{html .UserName}}{{end}} - {{if .ChRoot}}RootDirectory - {{html .ChRoot}}{{end}} - {{if .WorkingDirectory}}WorkingDirectory - {{html .WorkingDirectory}}{{end}} - SessionCreate - <{{bool .SessionCreate}}/> - KeepAlive - <{{bool .KeepAlive}}/> - RunAtLoad - <{{bool .RunAtLoad}}/> - Disabled - - - {{if .StandardOutPath}}StandardOutPath - {{html .StandardOutPath}}{{end}} - {{if .StandardErrorPath}}StandardErrorPath - {{html .StandardErrorPath}}{{end}} - + {{- end}} + KeepAlive + <{{bool .KeepAlive}}/> + Label + {{html .Name}} + ProgramArguments + + {{html .Path}} + {{- if .Config.Arguments}} + {{- range .Config.Arguments}} + {{html .}} + {{- end}} + {{- end}} + + {{- if .ChRoot}} + RootDirectory + {{html .ChRoot}} + {{- end}} + RunAtLoad + <{{bool .RunAtLoad}}/> + SessionCreate + <{{bool .SessionCreate}}/> + {{- if .StandardErrorPath}} + StandardErrorPath + {{html .StandardErrorPath}} + {{- end}} + {{- if .StandardOutPath}} + StandardOutPath + {{html .StandardOutPath}} + {{- end}} + {{- if .UserName}} + UserName + {{html .UserName}} + {{- end}} + {{- if .WorkingDirectory}} + WorkingDirectory + {{html .WorkingDirectory}} + {{- end}} + `