mirror of
https://github.com/restic/restic.git
synced 2025-08-12 14:47:41 +00:00
Move backend/sftp.StartForeground to backend/
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
package sftp
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
)
|
||||
|
||||
func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
|
||||
// run the command in it's own process group so that SIGINT
|
||||
// is not sent to it.
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
Setpgid: true,
|
||||
}
|
||||
|
||||
// start the process
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cmd.Start")
|
||||
}
|
||||
|
||||
bg = func() error { return nil }
|
||||
return bg, nil
|
||||
}
|
@@ -1,79 +0,0 @@
|
||||
// +build !solaris
|
||||
// +build !windows
|
||||
|
||||
package sftp
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
)
|
||||
|
||||
func tcsetpgrp(fd int, pid int) error {
|
||||
_, _, errno := syscall.RawSyscall(syscall.SYS_IOCTL, uintptr(fd),
|
||||
uintptr(syscall.TIOCSPGRP), uintptr(unsafe.Pointer(&pid)))
|
||||
if errno == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return errno
|
||||
}
|
||||
|
||||
// startForeground runs cmd in the foreground, by temporarily switching to the
|
||||
// new process group created for cmd. The returned function `bg` switches back
|
||||
// to the previous process group.
|
||||
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(syscall.SIGTTIN)
|
||||
signal.Ignore(syscall.SIGTTOU)
|
||||
|
||||
// run the command in its own process group
|
||||
cmd.SysProcAttr = &syscall.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 := syscall.Getpgrp()
|
||||
err = tcsetpgrp(int(tty.Fd()), cmd.Process.Pid)
|
||||
if err != nil {
|
||||
_ = tty.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bg = func() error {
|
||||
signal.Reset(syscall.SIGTTIN)
|
||||
signal.Reset(syscall.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
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
package sftp
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
)
|
||||
|
||||
// startForeground runs cmd in the foreground, by temporarily switching to the
|
||||
// new process group created for cmd. The returned function `bg` switches back
|
||||
// to the previous process group.
|
||||
func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
|
||||
// just start the process and hope for the best
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cmd.Start")
|
||||
}
|
||||
|
||||
bg = func() error { return nil }
|
||||
return bg, nil
|
||||
}
|
@@ -65,7 +65,7 @@ func startClient(program string, args ...string) (*SFTP, error) {
|
||||
return nil, errors.Wrap(err, "cmd.StdoutPipe")
|
||||
}
|
||||
|
||||
bg, err := startForeground(cmd)
|
||||
bg, err := backend.StartForeground(cmd)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cmd.Start")
|
||||
}
|
||||
|
Reference in New Issue
Block a user