diff --git a/cmds/portmaster-start/lock.go b/cmds/portmaster-start/lock.go index 33ba8d8e..0db86606 100644 --- a/cmds/portmaster-start/lock.go +++ b/cmds/portmaster-start/lock.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "io/fs" "log" "os" "os/user" @@ -14,21 +15,7 @@ import ( ) func checkAndCreateInstanceLock(path, name string, perUser bool) (pid int32, err error) { - var lockFilePath string - if perUser { - // Get user ID for per-user lock file. - var userID string - usr, err := user.Current() - if err != nil { - log.Printf("failed to get current user: %s\n", err) - userID = "no-user" - } else { - userID = usr.Uid - } - lockFilePath = filepath.Join(dataRoot.Path, path, fmt.Sprintf("%s-%s-lock.pid", name, userID)) - } else { - lockFilePath = filepath.Join(dataRoot.Path, path, fmt.Sprintf("%s-lock.pid", name)) - } + lockFilePath := getLockFilePath(path, name, perUser) // read current pid file data, err := os.ReadFile(lockFilePath) @@ -100,7 +87,23 @@ func createInstanceLock(lockFilePath string) error { return nil } -func deleteInstanceLock(path, name string) error { - lockFilePath := filepath.Join(dataRoot.Path, path, fmt.Sprintf("%s-lock.pid", name)) - return os.Remove(lockFilePath) +func deleteInstanceLock(path, name string, perUser bool) error { + return os.Remove(getLockFilePath(path, name, perUser)) +} + +func getLockFilePath(path, name string, perUser bool) string { + if !perUser { + return filepath.Join(dataRoot.Path, path, fmt.Sprintf("%s-lock.pid", name)) + } + + // Get user ID for per-user lock file. + var userID string + usr, err := user.Current() + if err != nil { + log.Printf("failed to get current user: %s\n", err) + userID = "no-user" + } else { + userID = usr.Uid + } + return filepath.Join(dataRoot.Path, path, fmt.Sprintf("%s-%s-lock.pid", name, userID)) } diff --git a/cmds/portmaster-start/run.go b/cmds/portmaster-start/run.go index 34c9129b..0536f30a 100644 --- a/cmds/portmaster-start/run.go +++ b/cmds/portmaster-start/run.go @@ -172,7 +172,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.LockPathPrefix, opts.ShortIdentifier) + err := deleteInstanceLock(opts.LockPathPrefix, opts.ShortIdentifier, opts.LockPerUser) if err != nil { log.Printf("failed to delete instance lock: %s\n", err) }