File: background_linux.go

package info (click to toggle)
restic 0.18.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,824 kB
  • sloc: sh: 3,704; makefile: 50; python: 34
file content (27 lines) | stat: -rw-r--r-- 860 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package termstatus

import (
	"github.com/restic/restic/internal/debug"

	"golang.org/x/sys/unix"
)

// 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 bg
}

func isProcessBackground(fd uintptr) (bool, error) {
	// 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
}