From ddbc0c1b37f349abdac875e56a1f0c0fab9884dc Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Thu, 25 May 2023 17:20:42 +0200 Subject: [PATCH 1/2] termstatus: Fix IsProcessBackground for 64-bit big-endian Linux Fixes #4223. Verified with QEMU on linux/amd64. --- internal/ui/termstatus/background_linux.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/ui/termstatus/background_linux.go b/internal/ui/termstatus/background_linux.go index 2c32faf17..db96c2c53 100644 --- a/internal/ui/termstatus/background_linux.go +++ b/internal/ui/termstatus/background_linux.go @@ -18,6 +18,10 @@ func IsProcessBackground(fd uintptr) bool { } func isProcessBackground(fd uintptr) (bool, error) { - pid, err := unix.IoctlGetInt(int(fd), unix.TIOCGPGRP) - return pid != unix.Getpgrp(), err + // We need to use IoctlGetUint32 here, because pid_t is 32-bit even on + // 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 } From 51dc80be5b8f8d49eed2830abf9109dabb7ad5a2 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Thu, 25 May 2023 17:31:51 +0200 Subject: [PATCH 2/2] backend: Use IoctlSetPointerInt for tcsetpgrp 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. --- internal/backend/foreground_unix.go | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/internal/backend/foreground_unix.go b/internal/backend/foreground_unix.go index 2b59bdf6c..fcc0dfe78 100644 --- a/internal/backend/foreground_unix.go +++ b/internal/backend/foreground_unix.go @@ -7,21 +7,17 @@ import ( "os" "os/exec" "os/signal" - "syscall" - "unsafe" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" + + "golang.org/x/sys/unix" ) 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 + // 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) { @@ -35,11 +31,11 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) { return bg, cmd.Start() } - signal.Ignore(syscall.SIGTTIN) - signal.Ignore(syscall.SIGTTOU) + signal.Ignore(unix.SIGTTIN) + signal.Ignore(unix.SIGTTOU) // run the command in its own process group - cmd.SysProcAttr = &syscall.SysProcAttr{ + cmd.SysProcAttr = &unix.SysProcAttr{ 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 - prev := syscall.Getpgrp() + prev := unix.Getpgrp() err = tcsetpgrp(int(tty.Fd()), cmd.Process.Pid) if err != nil { _ = tty.Close() @@ -59,8 +55,8 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) { } bg = func() error { - signal.Reset(syscall.SIGTTIN) - signal.Reset(syscall.SIGTTOU) + signal.Reset(unix.SIGTTIN) + signal.Reset(unix.SIGTTOU) // reset the foreground process group err = tcsetpgrp(int(tty.Fd()), prev)