diff --git a/service/service.go b/service/service.go index c1b80a86..7718a5c4 100644 --- a/service/service.go +++ b/service/service.go @@ -79,7 +79,7 @@ func (s *Service) Start() error { } } if !config.HasServicesToStart() { - infoString := "No service configured, nothing to do" + infoString := "no service configured, nothing to do" logger.Info(logSender, "", infoString) logger.InfoToConsole(infoString) return errors.New(infoString) @@ -216,8 +216,7 @@ func (s *Service) startServices() { // Wait blocks until the service exits func (s *Service) Wait() { if s.PortableMode != 1 { - registerSigHup() - registerSigUSR1() + registerSignals() } <-s.Shutdown } @@ -236,10 +235,10 @@ func (s *Service) loadInitialData() error { return fmt.Errorf("invalid input_file %#v, it must be an absolute path", s.LoadDataFrom) } if s.LoadDataMode < 0 || s.LoadDataMode > 1 { - return fmt.Errorf("Invalid loaddata-mode %v", s.LoadDataMode) + return fmt.Errorf("invalid loaddata-mode %v", s.LoadDataMode) } if s.LoadDataQuotaScan < 0 || s.LoadDataQuotaScan > 2 { - return fmt.Errorf("Invalid loaddata-scan %v", s.LoadDataQuotaScan) + return fmt.Errorf("invalid loaddata-scan %v", s.LoadDataQuotaScan) } info, err := os.Stat(s.LoadDataFrom) if err != nil { diff --git a/service/sighup_unix.go b/service/sighup_unix.go deleted file mode 100644 index 43b2204b..00000000 --- a/service/sighup_unix.go +++ /dev/null @@ -1,51 +0,0 @@ -// +build !windows - -package service - -import ( - "os" - "os/signal" - "syscall" - - "github.com/drakkan/sftpgo/common" - "github.com/drakkan/sftpgo/dataprovider" - "github.com/drakkan/sftpgo/ftpd" - "github.com/drakkan/sftpgo/httpd" - "github.com/drakkan/sftpgo/logger" - "github.com/drakkan/sftpgo/telemetry" - "github.com/drakkan/sftpgo/webdavd" -) - -func registerSigHup() { - sig := make(chan os.Signal, 1) - signal.Notify(sig, syscall.SIGHUP) - go func() { - for range sig { - logger.Debug(logSender, "", "Received reload request") - err := dataprovider.ReloadConfig() - if err != nil { - logger.Warn(logSender, "", "error reloading dataprovider configuration: %v", err) - } - err = httpd.ReloadCertificateMgr() - if err != nil { - logger.Warn(logSender, "", "error reloading cert manager: %v", err) - } - err = ftpd.ReloadCertificateMgr() - if err != nil { - logger.Warn(logSender, "", "error reloading FTPD cert manager: %v", err) - } - err = webdavd.ReloadCertificateMgr() - if err != nil { - logger.Warn(logSender, "", "error reloading WebDAV cert manager: %v", err) - } - err = telemetry.ReloadCertificateMgr() - if err != nil { - logger.Warn(logSender, "", "error reloading telemetry cert manager: %v", err) - } - err = common.ReloadDefender() - if err != nil { - logger.Warn(logSender, "", "error reloading defender's lists: %v", err) - } - } - }() -} diff --git a/service/sighup_windows.go b/service/sighup_windows.go deleted file mode 100644 index c95f0e60..00000000 --- a/service/sighup_windows.go +++ /dev/null @@ -1,3 +0,0 @@ -package service - -func registerSigHup() {} diff --git a/service/signals_unix.go b/service/signals_unix.go new file mode 100644 index 00000000..cb6bc044 --- /dev/null +++ b/service/signals_unix.go @@ -0,0 +1,75 @@ +// +build !windows + +package service + +import ( + "os" + "os/signal" + "syscall" + + "github.com/drakkan/sftpgo/common" + "github.com/drakkan/sftpgo/dataprovider" + "github.com/drakkan/sftpgo/ftpd" + "github.com/drakkan/sftpgo/httpd" + "github.com/drakkan/sftpgo/logger" + "github.com/drakkan/sftpgo/telemetry" + "github.com/drakkan/sftpgo/webdavd" +) + +func registerSignals() { + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGUSR1) + go func() { + for sig := range c { + switch sig { + case syscall.SIGHUP: + handleSIGHUP() + case syscall.SIGUSR1: + handleSIGUSR1() + case syscall.SIGINT, syscall.SIGTERM: + handleInterrupt() + } + } + }() +} + +func handleSIGHUP() { + logger.Debug(logSender, "", "Received reload request") + err := dataprovider.ReloadConfig() + if err != nil { + logger.Warn(logSender, "", "error reloading dataprovider configuration: %v", err) + } + err = httpd.ReloadCertificateMgr() + if err != nil { + logger.Warn(logSender, "", "error reloading cert manager: %v", err) + } + err = ftpd.ReloadCertificateMgr() + if err != nil { + logger.Warn(logSender, "", "error reloading FTPD cert manager: %v", err) + } + err = webdavd.ReloadCertificateMgr() + if err != nil { + logger.Warn(logSender, "", "error reloading WebDAV cert manager: %v", err) + } + err = telemetry.ReloadCertificateMgr() + if err != nil { + logger.Warn(logSender, "", "error reloading telemetry cert manager: %v", err) + } + err = common.ReloadDefender() + if err != nil { + logger.Warn(logSender, "", "error reloading defender's lists: %v", err) + } +} + +func handleSIGUSR1() { + logger.Debug(logSender, "", "Received log file rotation request") + err := logger.RotateLogFile() + if err != nil { + logger.Warn(logSender, "", "error rotating log file: %v", err) + } +} + +func handleInterrupt() { + logger.Debug(logSender, "", "Received interrupt request") + os.Exit(0) +} diff --git a/service/signals_windows.go b/service/signals_windows.go new file mode 100644 index 00000000..362711ec --- /dev/null +++ b/service/signals_windows.go @@ -0,0 +1,19 @@ +package service + +import ( + "os" + "os/signal" + + "github.com/drakkan/sftpgo/logger" +) + +func registerSignals() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + go func() { + for range c { + logger.Debug(logSender, "", "Received interrupt request") + os.Exit(0) + } + }() +} diff --git a/service/sigusr1_unix.go b/service/sigusr1_unix.go deleted file mode 100644 index f6e87a52..00000000 --- a/service/sigusr1_unix.go +++ /dev/null @@ -1,25 +0,0 @@ -// +build !windows - -package service - -import ( - "os" - "os/signal" - "syscall" - - "github.com/drakkan/sftpgo/logger" -) - -func registerSigUSR1() { - sig := make(chan os.Signal, 1) - signal.Notify(sig, syscall.SIGUSR1) - go func() { - for range sig { - logger.Debug(logSender, "", "Received log file rotation request") - err := logger.RotateLogFile() - if err != nil { - logger.Warn(logSender, "", "error rotating log file: %v", err) - } - } - }() -} diff --git a/service/sigusr1_windows.go b/service/sigusr1_windows.go deleted file mode 100644 index c1214303..00000000 --- a/service/sigusr1_windows.go +++ /dev/null @@ -1,3 +0,0 @@ -package service - -func registerSigUSR1() {}