Improve start and shutdown controls and flow
This commit is contained in:
@@ -7,39 +7,37 @@ import (
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
processInfo "github.com/shirou/gopsutil/process"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/safing/portmaster/base/log"
|
||||
"github.com/safing/portmaster/service"
|
||||
)
|
||||
|
||||
func run(instance *service.Instance) {
|
||||
// Set default log level.
|
||||
log.SetLogLevel(log.WarningLevel)
|
||||
_ = log.Start()
|
||||
type LinuxSystemService struct {
|
||||
instance *service.Instance
|
||||
}
|
||||
|
||||
// Start
|
||||
go func() {
|
||||
err := instance.Start()
|
||||
if err != nil {
|
||||
fmt.Printf("instance start failed: %s\n", err)
|
||||
func NewSystemService(instance *service.Instance) *LinuxSystemService {
|
||||
return &LinuxSystemService{instance: instance}
|
||||
}
|
||||
|
||||
// Print stack on start failure, if enabled.
|
||||
if printStackOnExit {
|
||||
printStackTo(os.Stdout, "PRINTING STACK ON START FAILURE")
|
||||
}
|
||||
func (s *LinuxSystemService) Run() {
|
||||
// Start instance.
|
||||
err := s.instance.Start()
|
||||
if err != nil {
|
||||
slog.Error("failed to start", "err", err)
|
||||
|
||||
os.Exit(1)
|
||||
// Print stack on start failure, if enabled.
|
||||
if printStackOnExit {
|
||||
printStackTo(os.Stderr, "PRINTING STACK ON START FAILURE")
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for signal.
|
||||
signalCh := make(chan os.Signal, 1)
|
||||
if enableInputSignals {
|
||||
go inputSignals(signalCh)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Subscribe to signals.
|
||||
signalCh := make(chan os.Signal, 1)
|
||||
signal.Notify(
|
||||
signalCh,
|
||||
os.Interrupt,
|
||||
@@ -47,74 +45,61 @@ func run(instance *service.Instance) {
|
||||
syscall.SIGINT,
|
||||
syscall.SIGTERM,
|
||||
syscall.SIGQUIT,
|
||||
sigUSR1,
|
||||
syscall.SIGUSR1,
|
||||
)
|
||||
|
||||
select {
|
||||
case sig := <-signalCh:
|
||||
// Only print and continue to wait if SIGUSR1
|
||||
if sig == sigUSR1 {
|
||||
printStackTo(os.Stderr, "PRINTING STACK ON REQUEST")
|
||||
} else {
|
||||
fmt.Println(" <INTERRUPT>") // CLI output.
|
||||
slog.Warn("program was interrupted, stopping")
|
||||
// Wait for shutdown signal.
|
||||
wait:
|
||||
for {
|
||||
select {
|
||||
case sig := <-signalCh:
|
||||
// Only print and continue to wait if SIGUSR1
|
||||
if sig == syscall.SIGUSR1 {
|
||||
printStackTo(os.Stdout, "PRINTING STACK ON REQUEST")
|
||||
continue wait
|
||||
} else {
|
||||
// Trigger shutdown.
|
||||
fmt.Printf(" <SIGNAL: %v>", sig) // CLI output.
|
||||
slog.Warn("received stop signal", "signal", sig)
|
||||
s.instance.Shutdown()
|
||||
break wait
|
||||
}
|
||||
case <-s.instance.ShuttingDown():
|
||||
break wait
|
||||
}
|
||||
|
||||
case <-instance.Stopped():
|
||||
log.Shutdown()
|
||||
os.Exit(instance.ExitCode())
|
||||
}
|
||||
|
||||
// Wait for shutdown to finish.
|
||||
|
||||
// Catch signals during shutdown.
|
||||
// Rapid unplanned disassembly after 5 interrupts.
|
||||
go func() {
|
||||
forceCnt := 5
|
||||
for {
|
||||
<-signalCh
|
||||
forceCnt--
|
||||
if forceCnt > 0 {
|
||||
fmt.Printf(" <INTERRUPT> again, but already shutting down - %d more to force\n", forceCnt)
|
||||
} else {
|
||||
printStackTo(os.Stderr, "PRINTING STACK ON FORCED EXIT")
|
||||
os.Exit(1)
|
||||
// Force exit after 5 interrupts.
|
||||
forceCnt := 5
|
||||
for {
|
||||
select {
|
||||
case <-s.instance.ShutdownComplete():
|
||||
return
|
||||
case sig := <-signalCh:
|
||||
if sig != syscall.SIGUSR1 {
|
||||
forceCnt--
|
||||
if forceCnt > 0 {
|
||||
fmt.Printf(" <SIGNAL: %s> again, but already shutting down - %d more to force\n", sig, forceCnt)
|
||||
} else {
|
||||
printStackTo(os.Stderr, "PRINTING STACK ON FORCED EXIT")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Rapid unplanned disassembly after 3 minutes.
|
||||
go func() {
|
||||
time.Sleep(3 * time.Minute)
|
||||
printStackTo(os.Stderr, "PRINTING STACK - TAKING TOO LONG FOR SHUTDOWN")
|
||||
os.Exit(1)
|
||||
}()
|
||||
|
||||
// Stop instance.
|
||||
if err := instance.Stop(); err != nil {
|
||||
slog.Error("failed to stop", "err", err)
|
||||
}
|
||||
log.Shutdown()
|
||||
|
||||
// Print stack on shutdown, if enabled.
|
||||
if printStackOnExit {
|
||||
printStackTo(os.Stdout, "PRINTING STACK ON EXIT")
|
||||
}
|
||||
|
||||
// Check if restart was trigger and send start service command if true.
|
||||
if isRunningAsService() && instance.ShouldRestart {
|
||||
_ = runServiceRestart()
|
||||
}
|
||||
|
||||
os.Exit(instance.ExitCode())
|
||||
}
|
||||
|
||||
func runServiceRestart() error {
|
||||
func (s *LinuxSystemService) RestartService() error {
|
||||
// Check if user defined custom command for restarting the service.
|
||||
restartCommand, exists := os.LookupEnv("PORTMASTER_RESTART_COMMAND")
|
||||
|
||||
// Run the service restart
|
||||
var cmd *exec.Cmd
|
||||
if exists && restartCommand != "" {
|
||||
log.Debugf(`instance: running custom restart command: "%s"`, restartCommand)
|
||||
slog.Debug("running custom restart command", "command", restartCommand)
|
||||
cmd = exec.Command("sh", "-c", restartCommand)
|
||||
} else {
|
||||
cmd = exec.Command("systemctl", "restart", "portmaster")
|
||||
@@ -125,28 +110,30 @@ func runServiceRestart() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func isRunningAsService() bool {
|
||||
// Get the current process ID
|
||||
func (s *LinuxSystemService) IsService() bool {
|
||||
// Get own process ID
|
||||
pid := os.Getpid()
|
||||
|
||||
// Get parent process ID.
|
||||
currentProcess, err := processInfo.NewProcess(int32(pid)) //nolint:gosec
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
ppid, err := currentProcess.Ppid()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if the parent process ID is 1 == init system
|
||||
return ppid == 1
|
||||
}
|
||||
|
||||
func platformSpecificChecks() {
|
||||
// If flag is set. Run recover IP tables and exit. (Can be true only on linux)
|
||||
func runPlatformSpecifics(cmd *cobra.Command, args []string) {
|
||||
// If recover-iptables flag is set, run the recover-iptables command.
|
||||
// This is for backwards compatibility
|
||||
if recoverIPTables {
|
||||
exitCode := 0
|
||||
err := recoverIPTablesCmd()
|
||||
err := recover(cmd, args)
|
||||
if err != nil {
|
||||
fmt.Printf("failed: %s", err)
|
||||
exitCode = 1
|
||||
|
||||
Reference in New Issue
Block a user