mirror of
https://github.com/restic/restic.git
synced 2025-12-11 18:47:50 +00:00
backend,termstatus: Unify foreground/background detection
PR #5358 reintroduced a version of the TIOCGPGRP ioctl call that works on all Unix platforms except Linux, due to a bug/inconsistency in x/sys/unix. This commit fixes that by introducing termstatus.Tcgetpgrp. It also introduces termstatus.Getpgrp and termstatus.Tcsetpgrp to deal with the different signature of unix.Getpgrp in Solaris vs. all other Unix platforms and an int-overflowing constant on AIX, so that some AIX/Solaris-specific code can be removed elsewhere and foreground/background detection is done the same everywhere except on Windows.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
//go:build !aix && !solaris && !windows
|
||||
// +build !aix,!solaris,!windows
|
||||
//go:build unix
|
||||
|
||||
package util
|
||||
|
||||
@@ -10,20 +9,11 @@ import (
|
||||
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/ui/termstatus"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func tcgetpgrp(fd int) (int, error) {
|
||||
return unix.IoctlGetInt(fd, unix.TIOCGPGRP)
|
||||
}
|
||||
|
||||
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) {
|
||||
// run the command in its own process group
|
||||
// this ensures that sending ctrl-c to restic will not immediately stop the backend process.
|
||||
@@ -39,15 +29,15 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
|
||||
}
|
||||
|
||||
// only move child process to foreground if restic is in the foreground
|
||||
prev, err := tcgetpgrp(int(tty.Fd()))
|
||||
prev, err := termstatus.Tcgetpgrp(int(tty.Fd()))
|
||||
if err != nil {
|
||||
_ = tty.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
self := unix.Getpgrp()
|
||||
self := termstatus.Getpgrp()
|
||||
if prev != self {
|
||||
debug.Log("restic is not controlling the tty")
|
||||
debug.Log("restic is not controlling the tty; err = %v", err)
|
||||
if err := tty.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -66,7 +56,7 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
|
||||
}
|
||||
|
||||
// move the command's process group into the foreground
|
||||
err = tcsetpgrp(int(tty.Fd()), cmd.Process.Pid)
|
||||
err = termstatus.Tcsetpgrp(int(tty.Fd()), cmd.Process.Pid)
|
||||
if err != nil {
|
||||
_ = tty.Close()
|
||||
return nil, err
|
||||
@@ -77,7 +67,7 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
|
||||
signal.Reset(unix.SIGTTOU)
|
||||
|
||||
// reset the foreground process group
|
||||
err = tcsetpgrp(int(tty.Fd()), prev)
|
||||
err = termstatus.Tcsetpgrp(int(tty.Fd()), prev)
|
||||
if err != nil {
|
||||
_ = tty.Close()
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user