| 1 | package containment |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "syscall" |
| 7 | |
| 8 | "golang.org/x/sys/unix" |
| 9 | ) |
| 10 | |
| 11 | // The Linux system calls the containment needs; sys_darwin.go stands in |
| 12 | // so the runner builds for a Mac, where bubblewrap never runs. |
| 13 | |
| 14 | const oPath = unix.O_PATH |
| 15 | |
| 16 | func pidfdOpen(pid int) (int, error) { return unix.PidfdOpen(pid, 0) } |
| 17 | func pidfdSignal(fd int, sig unix.Signal) error { |
| 18 | return unix.PidfdSendSignal(fd, sig, nil, 0) |
| 19 | } |
| 20 | func parentDeathKill() *syscall.SysProcAttr { |
| 21 | return &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} |
| 22 | } |
| 23 | func pathNoLinks(path string) (int, error) { |
| 24 | return unix.Openat2(unix.AT_FDCWD, path, &unix.OpenHow{Flags: unix.O_PATH | unix.O_CLOEXEC, Resolve: unix.RESOLVE_NO_SYMLINKS | unix.RESOLVE_NO_MAGICLINKS}) |
| 25 | } |
| 26 | |
| 27 | // OpenTerminal opens a pseudo-terminal pair for a launch that needs a |
| 28 | // terminal (StartTerminal), wide enough that the CLI never wraps a line. |
| 29 | func OpenTerminal() (master, tty *os.File, err error) { |
| 30 | master, err = os.OpenFile("/dev/ptmx", os.O_RDWR|unix.O_NOCTTY|unix.O_CLOEXEC, 0) |
| 31 | if err != nil { |
| 32 | return nil, nil, err |
| 33 | } |
| 34 | fd := int(master.Fd()) |
| 35 | if err = unix.IoctlSetPointerInt(fd, unix.TIOCSPTLCK, 0); err != nil { |
| 36 | master.Close() |
| 37 | return nil, nil, err |
| 38 | } |
| 39 | n, err := unix.IoctlGetInt(fd, unix.TIOCGPTN) |
| 40 | if err != nil { |
| 41 | master.Close() |
| 42 | return nil, nil, err |
| 43 | } |
| 44 | tty, err = os.OpenFile(fmt.Sprintf("/dev/pts/%d", n), os.O_RDWR|unix.O_NOCTTY|unix.O_CLOEXEC, 0) |
| 45 | if err != nil { |
| 46 | master.Close() |
| 47 | return nil, nil, err |
| 48 | } |
| 49 | ws := unix.Winsize{Row: 50, Col: 1000} |
| 50 | _ = unix.IoctlSetWinsize(int(tty.Fd()), unix.TIOCSWINSZ, &ws) |
| 51 | return master, tty, nil |
| 52 | } |
| 53 | |