mirror of
https://github.com/octoleo/restic.git
synced 2024-12-22 19:08:55 +00:00
51dc80be5b
This function casts its argument to int32 before passing it to the system call, so that big-endian CPUs read the lower rather than the upper 32 bits of the pid. This also gets rid of the last import of "unsafe" in the Unix build. I changed syscall to x/sys/unix while I was at it, to remove one more import line. The constants and types there are aliases for their syscall counterparts.
73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
//go:build !aix && !solaris && !windows
|
|
// +build !aix,!solaris,!windows
|
|
|
|
package backend
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"os/signal"
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func tcsetpgrp(fd int, pid int) error {
|
|
// IoctlSetPointerInt silently casts to int32 internally,
|
|
// so this assumes pid fits in 31 bits.
|
|
return unix.IoctlSetPointerInt(fd, unix.TIOCSPGRP, pid)
|
|
}
|
|
|
|
func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
|
|
// open the TTY, we need the file descriptor
|
|
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
|
|
if err != nil {
|
|
debug.Log("unable to open tty: %v", err)
|
|
bg = func() error {
|
|
return nil
|
|
}
|
|
return bg, cmd.Start()
|
|
}
|
|
|
|
signal.Ignore(unix.SIGTTIN)
|
|
signal.Ignore(unix.SIGTTOU)
|
|
|
|
// run the command in its own process group
|
|
cmd.SysProcAttr = &unix.SysProcAttr{
|
|
Setpgid: true,
|
|
}
|
|
|
|
// start the process
|
|
err = cmd.Start()
|
|
if err != nil {
|
|
_ = tty.Close()
|
|
return nil, errors.Wrap(err, "cmd.Start")
|
|
}
|
|
|
|
// move the command's process group into the foreground
|
|
prev := unix.Getpgrp()
|
|
err = tcsetpgrp(int(tty.Fd()), cmd.Process.Pid)
|
|
if err != nil {
|
|
_ = tty.Close()
|
|
return nil, err
|
|
}
|
|
|
|
bg = func() error {
|
|
signal.Reset(unix.SIGTTIN)
|
|
signal.Reset(unix.SIGTTOU)
|
|
|
|
// reset the foreground process group
|
|
err = tcsetpgrp(int(tty.Fd()), prev)
|
|
if err != nil {
|
|
_ = tty.Close()
|
|
return err
|
|
}
|
|
|
|
return tty.Close()
|
|
}
|
|
|
|
return bg, nil
|
|
}
|