Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add latest kardianos/service upstream commits #2

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions example/logging/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
var logger service.Logger

// Program structures.
// Define Start and Stop methods.
//
// Define Start and Stop methods.
type program struct {
exit chan struct{}
}
Expand Down Expand Up @@ -54,11 +55,12 @@ func (p *program) Stop(s service.Service) error {
}

// Service setup.
// Define service config.
// Create the service.
// Setup the logger.
// Handle service controls (optional).
// Run the service.
//
// Define service config.
// Create the service.
// Setup the logger.
// Handle service controls (optional).
// Run the service.
func main() {
svcFlag := flag.String("service", "", "Control the system service.")
flag.Parse()
Expand Down
120 changes: 74 additions & 46 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,42 +167,70 @@ func New(i Interface, c *Config) (Service, error) {
}

// KeyValue provides a list of system specific options.
// * OS X
// - LaunchdConfig string () - Use custom launchd config.
// - KeepAlive bool (true) - Prevent the system from stopping the service automatically.
// - RunAtLoad bool (false) - Run the service after its job has been loaded.
// - SessionCreate bool (false) - Create a full user session.
//
// * Solaris
// - Prefix string ("application") - Service FMRI prefix.
//
// * POSIX
// - UserService bool (false) - Install as a current user service.
// - SystemdScript string () - Use custom systemd script.
// - UpstartScript string () - Use custom upstart script.
// - SysvScript string () - Use custom sysv script.
// - OpenRCScript string () - Use custom OpenRC script.
// - RunWait func() (wait for SIGNAL) - Do not install signal but wait for this function to return.
// - ReloadSignal string () [USR1, ...] - Signal to send on reload.
// - PIDFile string () [/run/prog.pid] - Location of the PID file.
// - LogOutput bool (false) - Redirect StdErr & StandardOutPath to files.
// - Restart string (always) - How shall service be restarted.
// - SuccessExitStatus string () - The list of exit status that shall be considered as successful,
// in addition to the default ones.
// - LogDirectory string(/var/log) - The path to the log files directory
//
// * Linux (systemd)
// - LimitNOFILE int (-1) - Maximum open files (ulimit -n)
// (https://serverfault.com/questions/628610/increasing-nproc-for-processes-launched-by-systemd-on-centos-7)
// * Windows
// - DelayedAutoStart bool (false) - After booting, start this service after some delay.
// - Password string () - Password to use when interfacing with the system service manager.
// - Interactive bool (false) - The service can interact with the desktop. (more information https://docs.microsoft.com/en-us/windows/win32/services/interactive-services)
// - DelayedAutoStart bool (false) - after booting start this service after some delay.
// - StartType string ("automatic") - Start service type. (automatic | manual | disabled)
// - OnFailure string ("restart" ) - Action to perform on service failure. (restart | reboot | noaction)
// - OnFailureDelayDuration string ( "1s" ) - Delay before restarting the service, time.Duration string.
// - OnFailureResetPeriod int ( 10 ) - Reset period for errors, seconds.
//
// - OS X
//
// - LaunchdConfig string () - Use custom launchd config.
//
// - KeepAlive bool (true) - Prevent the system from stopping the service automatically.
//
// - RunAtLoad bool (false) - Run the service after its job has been loaded.
//
// - SessionCreate bool (false) - Create a full user session.
//
// - Solaris
//
// - Prefix string ("application") - Service FMRI prefix.
//
// - POSIX
//
// - UserService bool (false) - Install as a current user service.
//
// - SystemdScript string () - Use custom systemd script.
//
// - UpstartScript string () - Use custom upstart script.
//
// - SysvScript string () - Use custom sysv script.
//
// - OpenRCScript string () - Use custom OpenRC script.
//
// - RunWait func() (wait for SIGNAL) - Do not install signal but wait for this function to return.
//
// - ReloadSignal string () [USR1, ...] - Signal to send on reload.
//
// - PIDFile string () [/run/prog.pid] - Location of the PID file.
//
// - LogOutput bool (false) - Redirect StdErr & StandardOutPath to files.
//
// - Restart string (always) - How shall service be restarted.
//
// - SuccessExitStatus string () - The list of exit status that shall be considered as successful,
// in addition to the default ones.
//
// - LogDirectory string(/var/log) - The path to the log files directory
//
// - Linux (systemd)
//
// - LimitNOFILE int (-1) - Maximum open files (ulimit -n)
// (https://serverfault.com/questions/628610/increasing-nproc-for-processes-launched-by-systemd-on-centos-7)
//
// - Windows
//
// - DelayedAutoStart bool (false) - After booting, start this service after some delay.
//
// - Password string () - Password to use when interfacing with the system service manager.
//
// - Interactive bool (false) - The service can interact with the desktop. (more information https://docs.microsoft.com/en-us/windows/win32/services/interactive-services)
//
// - DelayedAutoStart bool (false) - after booting start this service after some delay.
//
// - StartType string ("automatic") - Start service type. (automatic | manual | disabled)
//
// - OnFailure string ("restart" ) - Action to perform on service failure. (restart | reboot | noaction)
//
// - OnFailureDelayDuration string ( "1s" ) - Delay before restarting the service, time.Duration string.
//
// - OnFailureResetPeriod int ( 10 ) - Reset period for errors, seconds.
type KeyValue map[string]interface{}

// bool returns the value of the given name, assuming the value is a boolean.
Expand Down Expand Up @@ -325,16 +353,16 @@ type System interface {
// Interface represents the service interface for a program. Start runs before
// the hosting process is granted control and Stop runs when control is returned.
//
// 1. OS service manager executes user program.
// 2. User program sees it is executed from a service manager (IsInteractive is false).
// 3. User program calls Service.Run() which blocks.
// 4. Interface.Start() is called and quickly returns.
// 5. User program runs.
// 6. OS service manager signals the user program to stop.
// 7. Interface.Stop() is called and quickly returns.
// - For a successful exit, os.Exit should not be called in Interface.Stop().
// 8. Service.Run returns.
// 9. User program should quickly exit.
// 1. OS service manager executes user program.
// 2. User program sees it is executed from a service manager (IsInteractive is false).
// 3. User program calls Service.Run() which blocks.
// 4. Interface.Start() is called and quickly returns.
// 5. User program runs.
// 6. OS service manager signals the user program to stop.
// 7. Interface.Stop() is called and quickly returns.
// - For a successful exit, os.Exit should not be called in Interface.Stop().
// 8. Service.Run returns.
// 9. User program should quickly exit.
type Interface interface {
// Start provides a place to initiate the service. The service doesn't
// signal a completed start until after this function returns, so the
Expand Down
3 changes: 2 additions & 1 deletion service_aix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build aix
//go:build aix
// +build aix

// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
Expand Down
8 changes: 7 additions & 1 deletion service_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"text/template"
)

const version = "freebsd"
const configDir = "/usr/local/etc/rc.d"

type freebsdSystem struct{}

Expand Down Expand Up @@ -88,7 +90,11 @@ func (s *freebsdService) template() *template.Template {
}

func (s *freebsdService) configPath() (cp string, err error) {
cp = "/usr/local/etc/rc.d/" + s.Config.Name
if oserr := os.MkdirAll(configDir, 0755); oserr != nil {
err = oserr
return
}
cp = filepath.Join(configDir, s.Config.Name)
return
}

Expand Down
3 changes: 2 additions & 1 deletion service_go1.8.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build go1.8
//go:build go1.8
// +build go1.8

package service

Expand Down
1 change: 1 addition & 0 deletions service_nosu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.

//go:build !su
// +build !su

package service_test
Expand Down
1 change: 1 addition & 0 deletions service_su_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.

// This needs to be run as root/admin hence the reason there is a build tag
//go:build su
// +build su

package service_test
Expand Down
2 changes: 1 addition & 1 deletion service_systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (s *systemd) Uninstall() error {
if err := os.Remove(cp); err != nil {
return err
}
return nil
return s.run("daemon-reload")
}

func (s *systemd) Logger(errs chan<- error) (Logger, error) {
Expand Down
1 change: 1 addition & 0 deletions servicetest_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.

//go:build darwin || dragonfly || freebsd || linux || nacl || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris

package service_test
Expand Down