Update portmaster-start to create pid-lock file for notifier

This commit is contained in:
Patrick Pacher 2021-05-04 09:37:47 +02:00
parent 0c2ff73f71
commit fe29293474
No known key found for this signature in database
GPG key ID: E8CD2DA160925A6D
2 changed files with 14 additions and 7 deletions

View file

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

View file

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