2022-03-28 20:23:47 +00:00
|
|
|
//go:build !windows
|
2015-08-16 11:16:02 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2015-08-16 13:30:36 +00:00
|
|
|
"os"
|
2015-08-16 11:16:02 +00:00
|
|
|
"os/user"
|
|
|
|
"strconv"
|
2015-08-16 13:30:36 +00:00
|
|
|
"syscall"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2022-10-14 15:40:49 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2015-08-16 11:16:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// uidGidInt returns uid, gid of the user as a number.
|
2022-10-14 15:40:49 +00:00
|
|
|
func uidGidInt(u *user.User) (uid, gid uint32, err error) {
|
|
|
|
ui, err := strconv.ParseUint(u.Uid, 10, 32)
|
2015-08-16 11:16:02 +00:00
|
|
|
if err != nil {
|
2022-10-14 15:40:49 +00:00
|
|
|
return 0, 0, errors.Errorf("invalid UID %q", u.Uid)
|
2015-08-16 11:16:02 +00:00
|
|
|
}
|
2022-10-14 15:40:49 +00:00
|
|
|
gi, err := strconv.ParseUint(u.Gid, 10, 32)
|
2015-08-16 11:16:02 +00:00
|
|
|
if err != nil {
|
2022-10-14 15:40:49 +00:00
|
|
|
return 0, 0, errors.Errorf("invalid GID %q", u.Gid)
|
2015-08-16 11:16:02 +00:00
|
|
|
}
|
2022-10-14 15:40:49 +00:00
|
|
|
return uint32(ui), uint32(gi), nil
|
2015-08-16 11:16:02 +00:00
|
|
|
}
|
2015-08-16 13:30:36 +00:00
|
|
|
|
|
|
|
// checkProcess will check if the process retaining the lock
|
|
|
|
// exists and responds to SIGHUP signal.
|
|
|
|
// Returns true if the process exists and responds.
|
2022-11-11 20:45:55 +00:00
|
|
|
func (l *Lock) processExists() bool {
|
2015-08-16 13:30:36 +00:00
|
|
|
proc, err := os.FindProcess(l.PID)
|
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("error searching for process %d: %v\n", l.PID, err)
|
2015-08-16 13:30:36 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-01-30 18:35:46 +00:00
|
|
|
defer func() {
|
|
|
|
_ = proc.Release()
|
|
|
|
}()
|
2015-08-16 13:30:36 +00:00
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("sending SIGHUP to process %d\n", l.PID)
|
2015-08-16 13:30:36 +00:00
|
|
|
err = proc.Signal(syscall.SIGHUP)
|
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("signal error: %v, lock is probably stale\n", err)
|
2015-08-16 13:30:36 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|