Skip to content
Snippets Groups Projects
Verified Commit e7ab6ad5 authored by meskio's avatar meskio :tent:
Browse files

[feat] only one systray can be in execution

Create a systray.pid file to check if another systray is in execution
and only one systray is allowed to be running.

- Resolves: #17
parent 5dd03466
No related branches found
No related tags found
1 merge request!9[bug] use our own fork of the systray library
...@@ -30,6 +30,12 @@ const ( ...@@ -30,6 +30,12 @@ const (
var printer *message.Printer var printer *message.Printer
func main() { func main() {
err := acquirePID()
if err != nil {
log.Fatal(err)
}
defer releasePID()
conf := parseConfig() conf := parseConfig()
initPrinter() initPrinter()
...@@ -37,22 +43,28 @@ func main() { ...@@ -37,22 +43,28 @@ func main() {
b, err := bitmask.Init() b, err := bitmask.Init()
if err != nil { if err != nil {
log.Fatal(err) log.Print(err)
return
} }
defer b.Close() defer b.Close()
checkAndInstallHelpers(b, notify) err = checkAndInstallHelpers(b, notify)
if err != nil {
log.Printf("Is bitmask running? %v", err)
return
}
run(b, conf) run(b, conf)
} }
func checkAndInstallHelpers(b *bitmask.Bitmask, notify *notificator) { func checkAndInstallHelpers(b *bitmask.Bitmask, notify *notificator) error {
helpers, priviledge, err := b.VPNCheck() helpers, priviledge, err := b.VPNCheck()
if (err != nil && err.Error() == "nopolkit") || (err == nil && !priviledge) { if (err != nil && err.Error() == "nopolkit") || (err == nil && !priviledge) {
log.Printf("No polkit found") log.Printf("No polkit found")
notify.authAgent() notify.authAgent()
} else if err != nil { } else if err != nil {
notify.bitmaskNotRunning() notify.bitmaskNotRunning()
log.Fatal("Is bitmask running? ", err) return err
} }
if !helpers { if !helpers {
...@@ -61,6 +73,7 @@ func checkAndInstallHelpers(b *bitmask.Bitmask, notify *notificator) { ...@@ -61,6 +73,7 @@ func checkAndInstallHelpers(b *bitmask.Bitmask, notify *notificator) {
log.Println("Error installing helpers: ", err) log.Println("Error installing helpers: ", err)
} }
} }
return nil
} }
func initPrinter() { func initPrinter() {
......
pid.go 0 → 100644
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"syscall"
"0xacab.org/leap/bitmask-systray/bitmask"
)
var pidFile = filepath.Join(bitmask.ConfigPath, "systray.pid")
func acquirePID() error {
pid := syscall.Getpid()
current, err := getPID()
if err != nil {
return err
}
if current != 0 && current != pid {
proc, err := os.FindProcess(current)
if err != nil {
return err
}
err = proc.Signal(syscall.Signal(0))
if err == nil {
return fmt.Errorf("Another systray is running with pid: %d", current)
}
}
return setPID(pid)
}
func releasePID() error {
pid := syscall.Getpid()
current, err := getPID()
if err != nil {
return err
}
if current != 0 && current != pid {
return fmt.Errorf("Can't release pid file, is not own by this process")
}
if current == pid {
return os.Remove(pidFile)
}
return nil
}
func getPID() (int, error) {
_, err := os.Stat(pidFile)
if os.IsNotExist(err) {
return 0, nil
}
if err != nil {
return 0, err
}
file, err := os.Open(pidFile)
if err != nil {
return 0, err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return 0, err
}
if len(b) == 0 {
return 0, nil
}
return strconv.Atoi(string(b))
}
func setPID(pid int) error {
file, err := os.Create(pidFile)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(fmt.Sprintf("%d", pid))
return err
}
...@@ -116,7 +116,10 @@ func (bt *bmTray) onReady() { ...@@ -116,7 +116,10 @@ func (bt *bmTray) onReady() {
case <-mQuit.ClickedCh: case <-mQuit.ClickedCh:
systray.Quit() systray.Quit()
// XXX: this a hack as quit doesn't work
// this should be done by defer in the main function
bt.bm.Close() bt.bm.Close()
releasePID()
log.Println("Quit now...") log.Println("Quit now...")
os.Exit(0) os.Exit(0)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment