improve signals handling

This commit is contained in:
Nicola Murino 2021-03-25 19:31:21 +01:00
parent 0e50310a66
commit d5f092284a
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
7 changed files with 98 additions and 87 deletions

View file

@ -78,7 +78,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)
@ -215,8 +215,7 @@ func (s *Service) startServices() {
// Wait blocks until the service exits
func (s *Service) Wait() {
if s.PortableMode != 1 {
registerSigHup()
registerSigUSR1()
registerSignals()
}
<-s.Shutdown
}
@ -235,10 +234,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 {

View file

@ -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)
}
}
}()
}

View file

@ -1,3 +0,0 @@
package service
func registerSigHup() {}

75
service/signals_unix.go Normal file
View file

@ -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)
}

View file

@ -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)
}
}()
}

View file

@ -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)
}
}
}()
}

View file

@ -1,3 +0,0 @@
package service
func registerSigUSR1() {}