-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
pty_zos.go
141 lines (117 loc) · 2.76 KB
/
pty_zos.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//go:build zos
// +build zos
package pty
import (
"os"
"runtime"
"syscall"
"unsafe"
)
const (
SYS_UNLOCKPT = 0x37B
SYS_GRANTPT = 0x37A
SYS_POSIX_OPENPT = 0xC66
SYS_FCNTL = 0x18C
SYS___PTSNAME_A = 0x718
SETCVTON = 1
O_NONBLOCK = 0x04
F_SETFL = 4
F_CONTROL_CVT = 13
)
type f_cnvrt struct {
Cvtcmd int32
Pccsid int16
Fccsid int16
}
func open() (pty, tty *os.File, err error) {
ptmxfd, err := openpt(os.O_RDWR | syscall.O_NOCTTY)
if err != nil {
return nil, nil, err
}
// Needed for z/OS so that the characters are not garbled if ptyp* is untagged
cvtreq := f_cnvrt{Cvtcmd: SETCVTON, Pccsid: 0, Fccsid: 1047}
if _, err = fcntl(uintptr(ptmxfd), F_CONTROL_CVT, uintptr(unsafe.Pointer(&cvtreq))); err != nil {
return nil, nil, err
}
p := os.NewFile(uintptr(ptmxfd), "/dev/ptmx")
if p == nil {
return nil, nil, err
}
// In case of error after this point, make sure we close the ptmx fd.
defer func() {
if err != nil {
_ = p.Close() // Best effort.
}
}()
sname, err := ptsname(ptmxfd)
if err != nil {
return nil, nil, err
}
_, err = grantpt(ptmxfd)
if err != nil {
return nil, nil, err
}
if _, err = unlockpt(ptmxfd); err != nil {
return nil, nil, err
}
ptsfd, err := syscall.Open(sname, os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
return nil, nil, err
}
if _, err = fcntl(uintptr(ptsfd), F_CONTROL_CVT, uintptr(unsafe.Pointer(&cvtreq))); err != nil {
return nil, nil, err
}
t := os.NewFile(uintptr(ptsfd), sname)
if err != nil {
return nil, nil, err
}
return p, t, nil
}
func openpt(oflag int) (fd int, err error) {
r0, _, e1 := runtime.CallLeFuncWithErr(runtime.GetZosLibVec()+SYS_POSIX_OPENPT<<4, uintptr(oflag))
fd = int(r0)
if e1 != 0 {
err = syscall.Errno(e1)
}
return
}
func fcntl(fd uintptr, cmd int, arg uintptr) (val int, err error) {
r0, _, e1 := runtime.CallLeFuncWithErr(runtime.GetZosLibVec()+SYS_FCNTL<<4, uintptr(fd), uintptr(cmd), arg)
val = int(r0)
if e1 != 0 {
err = syscall.Errno(e1)
}
return
}
func ptsname(fd int) (name string, err error) {
r0, _, e1 := runtime.CallLeFuncWithPtrReturn(runtime.GetZosLibVec()+SYS___PTSNAME_A<<4, uintptr(fd))
name = u2s(unsafe.Pointer(r0))
if e1 != 0 {
err = syscall.Errno(e1)
}
return
}
func grantpt(fildes int) (rc int, err error) {
r0, _, e1 := runtime.CallLeFuncWithErr(runtime.GetZosLibVec()+SYS_GRANTPT<<4, uintptr(fildes))
rc = int(r0)
if e1 != 0 {
err = syscall.Errno(e1)
}
return
}
func unlockpt(fildes int) (rc int, err error) {
r0, _, e1 := runtime.CallLeFuncWithErr(runtime.GetZosLibVec()+SYS_UNLOCKPT<<4, uintptr(fildes))
rc = int(r0)
if e1 != 0 {
err = syscall.Errno(e1)
}
return
}
func u2s(cstr unsafe.Pointer) string {
str := (*[1024]uint8)(cstr)
i := 0
for str[i] != 0 {
i++
}
return string(str[:i])
}