)
var (
- lockdir = "/var/run"
+ lockdir = "/var/lock"
lockprefix = "crunch-run-"
locksuffix = ".lock"
)
}
// Detach acquires a lock for the given uuid, and starts the current
-// program as a child process (with -nodetach prepended to the given
+// program as a child process (with -detached prepended to the given
// arguments so the child knows not to detach again). The lock is
// passed along to the child process.
func Detach(uuid string, args []string, stdout, stderr io.Writer) int {
}
defer errfile.Close()
- cmd := exec.Command(args[0], append([]string{"-nodetach"}, args[1:]...)...)
+ cmd := exec.Command(args[0], append([]string{"-detached"}, args[1:]...)...)
cmd.Stdout = outfile
cmd.Stderr = errfile
+ // Child inherits lockfile.
cmd.ExtraFiles = []*os.File{lockfile}
+ // Ensure child isn't interrupted even if we receive signals
+ // from parent (sshd) while sending lockfile content to
+ // caller.
+ cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err = cmd.Start()
if err != nil {
os.Remove(outfile.Name())
// acquisition during races, e.g., by connecting to a
// unix socket or checking /proc/$pid/fd/$n ->
// lockfile.
- err = syscall.Flock(int(f.Fd()), syscall.LOCK_SH)
+ err = syscall.Flock(int(f.Fd()), syscall.LOCK_SH|syscall.LOCK_NB)
if err == nil {
// lockfile is stale
err := os.Remove(path)