[WIP] Add restart command to instance
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user