/
pty_linux.go
90 lines (77 loc) · 2.1 KB
/
pty_linux.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2015-2020 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package pty provides basic pty support.
// It implments much of exec.Command
// but the Start() function starts two goroutines that relay the
// data for Stdin, Stdout, and Stdout such that proper kernel pty
// processing is done. We did not simply embed an exec.Command
// as we can no guarantee that we can implement all aspects of it
// for all time to come.
package pty
import (
"fmt"
"os"
"syscall"
"unsafe"
"github.com/u-root/u-root/pkg/termios"
)
// New returns a new Pty.
func New() (*Pty, error) {
tty, err := termios.New()
if err != nil {
return nil, err
}
restorer, err := tty.Get()
if err != nil {
return nil, err
}
ptm, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
if err != nil {
return nil, err
}
if err := ptsunlock(ptm); err != nil {
return nil, err
}
sname, err := ptsname(ptm)
if err != nil {
return nil, err
}
// It can take a non-zero time for a pts to appear, it seems.
// Ten tries is reported to be far more than enough.
// We could consider something like inotify rather than polling?
for i := 0; i < 10; i++ {
_, err := os.Stat(sname)
if err == nil {
break
}
}
pts, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
return nil, err
}
return &Pty{Ptm: ptm, Pts: pts, Sname: sname, Kid: -1, TTY: tty, Restorer: restorer}, nil
}
func ptsname(f *os.File) (string, error) {
var n uintptr
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n)))
if err != 0 {
return "", err
}
return fmt.Sprintf("/dev/pts/%d", n), nil
}
func ptsunlock(f *os.File) error {
var u uintptr
// use TIOCSPTLCK with a zero valued arg to clear the slave pty lock
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
if err != 0 {
return err
}
return nil
}
func sysLinux(p *Pty) {
p.C.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}
}
func init() {
sys = sysLinux
}