From 35bd8f80c0f4be5d8fd49f65a5cb92b03c0005f3 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Sun, 16 Aug 2015 15:30:36 +0200 Subject: [PATCH] Split out process check as separate function. This will allow the checks to be changed for different operating systems. Issue #260 is related to this, but this does not change any current behaviour. --- lock.go | 19 ++++--------------- lock_unix.go | 24 ++++++++++++++++++++++++ lock_windows.go | 15 +++++++++++++++ 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/lock.go b/lock.go index dd24e8b15..0d4000fec 100644 --- a/lock.go +++ b/lock.go @@ -12,7 +12,6 @@ import ( "github.com/restic/restic/backend" "github.com/restic/restic/debug" "github.com/restic/restic/repository" - "runtime" ) // Lock represents a process locking the repository for an operation. @@ -195,22 +194,12 @@ func (l *Lock) Stale() bool { return true } - proc, err := os.FindProcess(l.PID) - if err != nil { - debug.Log("Lock.Stale", "error searching for process %d: %v\n", l.PID, err) + // check if we can reach the process retaining the lock + exists := l.processExists() + if !exists { + debug.Log("Lock.Stale", "could not reach process, %d, lock is probably stale\n", l.PID) return true } - defer proc.Release() - - // Windows does not have SIGHUP - if runtime.GOOS != "windows" { - debug.Log("Lock.Stale", "sending SIGHUP to process %d\n", l.PID) - err = proc.Signal(syscall.SIGHUP) - if err != nil { - debug.Log("Lock.Stale", "signal error: %v, lock is probably stale\n", err) - return true - } - } debug.Log("Lock.Stale", "lock not stale\n") return false diff --git a/lock_unix.go b/lock_unix.go index 04d5005ba..aaf0cfdd5 100644 --- a/lock_unix.go +++ b/lock_unix.go @@ -3,8 +3,12 @@ package restic import ( + "os" "os/user" "strconv" + "syscall" + + "github.com/restic/restic/debug" ) // uidGidInt returns uid, gid of the user as a number. @@ -22,3 +26,23 @@ func uidGidInt(u user.User) (uid, gid uint32, err error) { gid = uint32(gi) return } + +// checkProcess will check if the process retaining the lock +// exists and responds to SIGHUP signal. +// Returns true if the process exists and responds. +func (l Lock) processExists() bool { + proc, err := os.FindProcess(l.PID) + if err != nil { + debug.Log("Lock.Stale", "error searching for process %d: %v\n", l.PID, err) + return false + } + defer proc.Release() + + debug.Log("Lock.Stale", "sending SIGHUP to process %d\n", l.PID) + err = proc.Signal(syscall.SIGHUP) + if err != nil { + debug.Log("Lock.Stale", "signal error: %v, lock is probably stale\n", err) + return false + } + return true +} diff --git a/lock_windows.go b/lock_windows.go index 2e0c68b48..fc700a9b5 100644 --- a/lock_windows.go +++ b/lock_windows.go @@ -1,10 +1,25 @@ package restic import ( + "os" "os/user" + + "github.com/restic/restic/debug" ) // uidGidInt always returns 0 on Windows, since uid isn't numbers func uidGidInt(u user.User) (uid, gid uint32, err error) { return 0, 0, nil } + +// checkProcess will check if the process retaining the lock exists. +// Returns true if the process exists. +func (l Lock) processExists() bool { + proc, err := os.FindProcess(l.PID) + if err != nil { + debug.Log("Lock.Stale", "error searching for process %d: %v\n", l.PID, err) + return false + } + proc.Release() + return true +}