forked from TrueCloudLab/restic
Merge pull request #3018 from greatroar/background-check
Fix IsProcessBackground on Linux with stdin redirection
This commit is contained in:
commit
39fe1e96fe
4 changed files with 36 additions and 15 deletions
|
@ -4,6 +4,6 @@ package termstatus
|
|||
|
||||
// IsProcessBackground reports whether the current process is running in the
|
||||
// background. Not implemented for this platform.
|
||||
func IsProcessBackground() bool {
|
||||
func IsProcessBackground(uintptr) bool {
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -1,21 +1,23 @@
|
|||
package termstatus
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/restic/restic/internal/debug"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// IsProcessBackground reports whether the current process is running in the background.
|
||||
func IsProcessBackground() bool {
|
||||
var pid int
|
||||
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdin), syscall.TIOCGPGRP, uintptr(unsafe.Pointer(&pid)))
|
||||
|
||||
if err != 0 {
|
||||
// IsProcessBackground reports whether the current process is running in the
|
||||
// background. fd must be a file descriptor for the terminal.
|
||||
func IsProcessBackground(fd uintptr) bool {
|
||||
bg, err := isProcessBackground(fd)
|
||||
if err != nil {
|
||||
debug.Log("Can't check if we are in the background. Using default behaviour. Error: %s\n", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
return pid != syscall.Getpgrp()
|
||||
return bg
|
||||
}
|
||||
|
||||
func isProcessBackground(fd uintptr) (bool, error) {
|
||||
pid, err := unix.IoctlGetInt(int(fd), unix.TIOCGPGRP)
|
||||
return pid != unix.Getpgrp(), err
|
||||
}
|
||||
|
|
19
internal/ui/termstatus/background_linux_test.go
Normal file
19
internal/ui/termstatus/background_linux_test.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package termstatus
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
func TestIsProcessBackground(t *testing.T) {
|
||||
tty, err := os.Open("/dev/tty")
|
||||
if err != nil {
|
||||
t.Skipf("can't open terminal: %v", err)
|
||||
}
|
||||
defer tty.Close()
|
||||
|
||||
_, err = isProcessBackground(tty.Fd())
|
||||
rtest.OK(t, err)
|
||||
}
|
|
@ -95,7 +95,7 @@ func (t *Terminal) run(ctx context.Context) {
|
|||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if IsProcessBackground() {
|
||||
if IsProcessBackground(t.fd) {
|
||||
// ignore all messages, do nothing, we are in the background process group
|
||||
continue
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ func (t *Terminal) run(ctx context.Context) {
|
|||
return
|
||||
|
||||
case msg := <-t.msg:
|
||||
if IsProcessBackground() {
|
||||
if IsProcessBackground(t.fd) {
|
||||
// ignore all messages, do nothing, we are in the background process group
|
||||
continue
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ func (t *Terminal) run(ctx context.Context) {
|
|||
}
|
||||
|
||||
case stat := <-t.status:
|
||||
if IsProcessBackground() {
|
||||
if IsProcessBackground(t.fd) {
|
||||
// ignore all messages, do nothing, we are in the background process group
|
||||
continue
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue