mirror of
https://github.com/safing/portmaster
synced 2025-09-02 10:39:22 +00:00
Merge pull request #901 from safing/fix/add-per-user-locking-for-notifier
Add per-user locking option to portmaster-start run cmds
This commit is contained in:
commit
b4325845bc
2 changed files with 20 additions and 3 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -13,8 +14,22 @@ import (
|
||||||
processInfo "github.com/shirou/gopsutil/process"
|
processInfo "github.com/shirou/gopsutil/process"
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkAndCreateInstanceLock(path, name string) (pid int32, err error) {
|
func checkAndCreateInstanceLock(path, name string, perUser bool) (pid int32, err error) {
|
||||||
lockFilePath := filepath.Join(dataRoot.Path, path, fmt.Sprintf("%s-lock.pid", name))
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
// read current pid file
|
// read current pid file
|
||||||
data, err := ioutil.ReadFile(lockFilePath)
|
data, err := ioutil.ReadFile(lockFilePath)
|
||||||
|
|
|
@ -46,6 +46,7 @@ type Options struct {
|
||||||
Identifier string // component identifier
|
Identifier string // component identifier
|
||||||
ShortIdentifier string // populated automatically
|
ShortIdentifier string // populated automatically
|
||||||
LockPathPrefix string
|
LockPathPrefix string
|
||||||
|
LockPerUser bool
|
||||||
PIDFile bool
|
PIDFile bool
|
||||||
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
|
||||||
|
@ -73,6 +74,7 @@ func init() {
|
||||||
{
|
{
|
||||||
Name: "Portmaster Notifier",
|
Name: "Portmaster Notifier",
|
||||||
Identifier: "notifier/portmaster-notifier",
|
Identifier: "notifier/portmaster-notifier",
|
||||||
|
LockPerUser: true,
|
||||||
AllowDownload: false,
|
AllowDownload: false,
|
||||||
AllowHidingWindow: true,
|
AllowHidingWindow: true,
|
||||||
PIDFile: true,
|
PIDFile: true,
|
||||||
|
@ -162,7 +164,7 @@ func run(opts *Options, cmdArgs []string) (err error) {
|
||||||
|
|
||||||
// check for duplicate instances
|
// check for duplicate instances
|
||||||
if opts.PIDFile {
|
if opts.PIDFile {
|
||||||
pid, err := checkAndCreateInstanceLock(opts.LockPathPrefix, opts.ShortIdentifier)
|
pid, err := checkAndCreateInstanceLock(opts.LockPathPrefix, opts.ShortIdentifier, opts.LockPerUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to exec lock: %w", err)
|
return fmt.Errorf("failed to exec lock: %w", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue