[WIP] Add restart command to instance

This commit is contained in:
Vladimir Stoilov
2024-09-26 13:51:42 +03:00
parent c9631daa3e
commit 61babe2822
15 changed files with 81 additions and 98 deletions

View File

@@ -55,7 +55,10 @@ func initialize() *service.Instance {
// Create instance.
var execCmdLine bool
instance, err := service.New(&service.ServiceConfig{})
instance, err := service.New(&service.ServiceConfig{
IsRunningAsService: isRunningAsService(),
DefaultRestartCommand: defaultRestartCommand,
})
switch {
case err == nil:
// Continue

View File

@@ -4,14 +4,19 @@ import (
"fmt"
"log/slog"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
processInfo "github.com/shirou/gopsutil/process"
"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/service"
)
var defaultRestartCommand = exec.Command("systemctl", "restart", "portmaster")
func run(instance *service.Instance) {
// Set default log level.
log.SetLogLevel(log.WarningLevel)
@@ -98,3 +103,20 @@ func run(instance *service.Instance) {
os.Exit(instance.ExitCode())
}
func isRunningAsService() bool {
// Get the current process ID
pid := os.Getpid()
currentProcess, err := processInfo.NewProcess(int32(pid))
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
}

View File

@@ -9,6 +9,7 @@ import (
"fmt"
"log/slog"
"os"
"os/exec"
"os/signal"
"sync"
"syscall"
@@ -24,6 +25,8 @@ var (
// wait groups
runWg sync.WaitGroup
finishWg sync.WaitGroup
defaultRestartCommand = exec.Command("sc.exe", "restart", "PortmasterCore")
)
const serviceName = "PortmasterCore"
@@ -168,3 +171,11 @@ func registerSignalHandler(instance *service.Instance) {
}
}()
}
func isRunningAsService() bool {
isService, err := svc.IsWindowsService()
if err != nil {
return false
}
return isService
}