Merge pull request #4339 from greatroar/ioctl

backend, termstatus: fix ioctl calls for 64-bit big-endian platforms
This commit is contained in:
Michael Eischer 2023-05-26 19:34:14 +02:00 committed by GitHub
commit 658aa4c0f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 17 deletions

View File

@ -7,21 +7,17 @@ import (
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
"syscall"
"unsafe"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"golang.org/x/sys/unix"
) )
func tcsetpgrp(fd int, pid int) error { func tcsetpgrp(fd int, pid int) error {
_, _, errno := syscall.RawSyscall(syscall.SYS_IOCTL, uintptr(fd), // IoctlSetPointerInt silently casts to int32 internally,
uintptr(syscall.TIOCSPGRP), uintptr(unsafe.Pointer(&pid))) // so this assumes pid fits in 31 bits.
if errno == 0 { return unix.IoctlSetPointerInt(fd, unix.TIOCSPGRP, pid)
return nil
}
return errno
} }
func startForeground(cmd *exec.Cmd) (bg func() error, err error) { func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
@ -35,11 +31,11 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
return bg, cmd.Start() return bg, cmd.Start()
} }
signal.Ignore(syscall.SIGTTIN) signal.Ignore(unix.SIGTTIN)
signal.Ignore(syscall.SIGTTOU) signal.Ignore(unix.SIGTTOU)
// run the command in its own process group // run the command in its own process group
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &unix.SysProcAttr{
Setpgid: true, Setpgid: true,
} }
@ -51,7 +47,7 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
} }
// move the command's process group into the foreground // move the command's process group into the foreground
prev := syscall.Getpgrp() prev := unix.Getpgrp()
err = tcsetpgrp(int(tty.Fd()), cmd.Process.Pid) err = tcsetpgrp(int(tty.Fd()), cmd.Process.Pid)
if err != nil { if err != nil {
_ = tty.Close() _ = tty.Close()
@ -59,8 +55,8 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
} }
bg = func() error { bg = func() error {
signal.Reset(syscall.SIGTTIN) signal.Reset(unix.SIGTTIN)
signal.Reset(syscall.SIGTTOU) signal.Reset(unix.SIGTTOU)
// reset the foreground process group // reset the foreground process group
err = tcsetpgrp(int(tty.Fd()), prev) err = tcsetpgrp(int(tty.Fd()), prev)

View File

@ -18,6 +18,10 @@ func IsProcessBackground(fd uintptr) bool {
} }
func isProcessBackground(fd uintptr) (bool, error) { func isProcessBackground(fd uintptr) (bool, error) {
pid, err := unix.IoctlGetInt(int(fd), unix.TIOCGPGRP) // We need to use IoctlGetUint32 here, because pid_t is 32-bit even on
return pid != unix.Getpgrp(), err // 64-bit Linux. IoctlGetInt doesn't work on big-endian platforms:
// https://github.com/golang/go/issues/45585
// https://github.com/golang/go/issues/60429
pid, err := unix.IoctlGetUint32(int(fd), unix.TIOCGPGRP)
return int(pid) != unix.Getpgrp(), err
} }