Merge pull request #298 from safing/feature/notifier-lock

Update portmaster-start to create pid-lock file for notifier
This commit is contained in:
Daniel 2021-05-04 21:49:26 +02:00 committed by GitHub
commit 168ef32c44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View file

@ -13,8 +13,9 @@ import (
processInfo "github.com/shirou/gopsutil/process" processInfo "github.com/shirou/gopsutil/process"
) )
func checkAndCreateInstanceLock(name string) (pid int32, err error) { func checkAndCreateInstanceLock(path, name string) (pid int32, err error) {
lockFilePath := filepath.Join(dataRoot.Path, fmt.Sprintf("%s-lock.pid", name))
lockFilePath := filepath.Join(dataRoot.Path, path, fmt.Sprintf("%s-lock.pid", name))
// read current pid file // read current pid file
data, err := ioutil.ReadFile(lockFilePath) data, err := ioutil.ReadFile(lockFilePath)
@ -93,7 +94,7 @@ func createInstanceLock(lockFilePath string) error {
return nil return nil
} }
func deleteInstanceLock(name string) error { func deleteInstanceLock(path, name string) error {
lockFilePath := filepath.Join(dataRoot.Path, fmt.Sprintf("%s-lock.pid", name)) lockFilePath := filepath.Join(dataRoot.Path, path, fmt.Sprintf("%s-lock.pid", name))
return os.Remove(lockFilePath) return os.Remove(lockFilePath)
} }

View file

@ -36,6 +36,8 @@ var (
type Options struct { type Options struct {
Name string Name string
Identifier string // component identifier Identifier string // component identifier
LockPathPrefix string
PIDFile bool
ShortIdentifier string // populated automatically ShortIdentifier string // populated automatically
SuppressArgs bool // do not use any args SuppressArgs bool // do not use any args
AllowDownload bool // allow download of component if it is not yet available AllowDownload bool // allow download of component if it is not yet available
@ -50,6 +52,7 @@ func init() {
Identifier: "core/portmaster-core", Identifier: "core/portmaster-core",
AllowDownload: true, AllowDownload: true,
AllowHidingWindow: true, AllowHidingWindow: true,
PIDFile: true,
}, },
{ {
Name: "Portmaster App", Name: "Portmaster App",
@ -62,12 +65,15 @@ func init() {
Identifier: "notifier/portmaster-notifier", Identifier: "notifier/portmaster-notifier",
AllowDownload: false, AllowDownload: false,
AllowHidingWindow: true, AllowHidingWindow: true,
PIDFile: true,
LockPathPrefix: "exec",
}, },
{ {
Name: "Safing Privacy Network", Name: "Safing Privacy Network",
Identifier: "hub/spn-hub", Identifier: "hub/spn-hub",
AllowDownload: true, AllowDownload: true,
AllowHidingWindow: true, AllowHidingWindow: true,
PIDFile: true,
}, },
}) })
} }
@ -129,8 +135,8 @@ func run(opts *Options, cmdArgs []string) (err error) {
args := getExecArgs(opts, cmdArgs) args := getExecArgs(opts, cmdArgs)
// check for duplicate instances // check for duplicate instances
if opts.ShortIdentifier == "core" || opts.ShortIdentifier == "hub" { if opts.PIDFile {
pid, err := checkAndCreateInstanceLock(opts.ShortIdentifier) pid, err := checkAndCreateInstanceLock(opts.LockPathPrefix, opts.ShortIdentifier)
if err != nil { if err != nil {
return fmt.Errorf("failed to exec lock: %w", err) return fmt.Errorf("failed to exec lock: %w", err)
} }
@ -138,7 +144,7 @@ func run(opts *Options, cmdArgs []string) (err error) {
return fmt.Errorf("another instance of %s is already running: PID %d", opts.Name, pid) return fmt.Errorf("another instance of %s is already running: PID %d", opts.Name, pid)
} }
defer func() { defer func() {
err := deleteInstanceLock(opts.ShortIdentifier) err := deleteInstanceLock(opts.LockPathPrefix, opts.ShortIdentifier)
if err != nil { if err != nil {
log.Printf("failed to delete instance lock: %s\n", err) log.Printf("failed to delete instance lock: %s\n", err)
} }