[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [sandboxed-tor-browser/master] Bug 20993: Handle the lock file better.



commit f2ae16a227ad0a1baa301fdd6c38c8460e9396ff
Author: Yawning Angel <yawning@xxxxxxxxxxxxxxx>
Date:   Thu Dec 15 20:14:18 2016 +0000

    Bug 20993: Handle the lock file better.
    
    Instead of relying on creating the lock file with O_EXCL, just use
    `flock()`, so that users don't need to manually remove the lock file if
    the app crashes.
---
 ChangeLog                                       |  1 +
 src/cmd/sandboxed-tor-browser/internal/ui/ui.go | 13 +++++++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5e3ee14..01e28d9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,5 @@
 Changes in version 0.0.3 - UNRELEASED:
+ * Bug 20993: Handle the lock file better.
  * Bug 20970: Firefox crashes if the security slider is left at the default
    on certain pages.
  * Bug 20973: Silence Gdk warnings on systems with integrated png loader.
diff --git a/src/cmd/sandboxed-tor-browser/internal/ui/ui.go b/src/cmd/sandboxed-tor-browser/internal/ui/ui.go
index cd9a8c1..973024c 100644
--- a/src/cmd/sandboxed-tor-browser/internal/ui/ui.go
+++ b/src/cmd/sandboxed-tor-browser/internal/ui/ui.go
@@ -31,6 +31,7 @@ import (
 	"os/exec"
 	"path/filepath"
 	"strings"
+	"syscall"
 
 	"git.schwanenlied.me/yawning/grab.git"
 	"git.schwanenlied.me/yawning/hpkp.git"
@@ -328,7 +329,6 @@ type lockFile struct {
 
 func (l *lockFile) unlock() {
 	defer l.f.Close()
-	os.Remove(l.f.Name())
 }
 
 func newLockFile(c *Common) (*lockFile, error) {
@@ -338,9 +338,18 @@ func newLockFile(c *Common) (*lockFile, error) {
 	p := filepath.Join(c.Cfg.RuntimeDir, lockFileName)
 
 	var err error
-	if l.f, err = os.OpenFile(p, os.O_CREATE|os.O_EXCL, utils.FileMode); err != nil {
+	if l.f, err = os.OpenFile(p, os.O_CREATE, utils.FileMode); err != nil {
 		return nil, err
 	}
+
+	fd := int(l.f.Fd())
+	if err = syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
+		if err == syscall.EWOULDBLOCK {
+			return nil, fmt.Errorf("`sandboxed-tor-browser` is already running")
+		}
+		return nil, err
+	}
+
 	return l, nil
 }
 

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits