File: pty_posix.go

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (63 lines) | stat: -rw-r--r-- 1,307 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//go:build freebsd && cgo

package chroot

// #include <fcntl.h>
// #include <stdlib.h>
import "C"

import (
	"github.com/sirupsen/logrus"
	"golang.org/x/sys/unix"
)

func openpt() (int, error) {
	fd, err := C.posix_openpt(C.O_RDWR)
	if err != nil {
		return -1, err
	}
	if _, err := C.grantpt(fd); err != nil {
		return -1, err
	}
	return int(fd), nil
}

func ptsname(fd int) (string, error) {
	path, err := C.ptsname(C.int(fd))
	if err != nil {
		return "", err
	}
	return C.GoString(path), nil
}

func unlockpt(fd int) error {
	if _, err := C.unlockpt(C.int(fd)); err != nil {
		return err
	}
	return nil
}

func getPtyDescriptors() (int, int, error) {
	// Create a pseudo-terminal and open the control side
	controlFd, err := openpt()
	if err != nil {
		logrus.Errorf("error opening PTY control side using posix_openpt: %v", err)
		return -1, -1, err
	}
	if err = unlockpt(controlFd); err != nil {
		logrus.Errorf("error unlocking PTY: %v", err)
		return -1, -1, err
	}
	// Get a handle for the other end.
	ptyName, err := ptsname(controlFd)
	if err != nil {
		logrus.Errorf("error getting PTY name: %v", err)
		return -1, -1, err
	}
	ptyFd, err := unix.Open(ptyName, unix.O_RDWR, 0)
	if err != nil {
		logrus.Errorf("error opening PTY: %v", err)
		return -1, -1, err
	}
	return controlFd, ptyFd, nil
}