Debian Codegraph @

api/v1/cmd/msh/tty.go

raw symbols SCIP
1 package main
2
3 import (
4 "fmt"
5 "log"
6 "os"
7 "os/signal"
8 "sync"
9 "syscall"
10
11 "github.com/mesos/mesos-go/api/v1/lib"
12 )
13
14 // #include <stdio.h>
15 // #include <stdlib.h>
16 // #include <termios.h>
17 // #include <unistd.h>
18 // #include <fcntl.h>
19 // #include <sys/ioctl.h>
20 //
21 // /* because golang doesn't like the ... param of ioctl */
22 // int ioctl_winsize(int d, unsigned long request, void *buf) {
23 // return ioctl(d, request, buf);
24 // }
25 //
26 import "C"
27 import "unsafe"
28
29 type cleanups struct {
30 ops []func()
31 once sync.Once
32 }
33
34 func (c *cleanups) unwind() {
35 c.once.Do(func() {
36 for _, f := range c.ops {
37 defer f()
38 }
39 })
40 }
41
42 func (c *cleanups) push(f func()) {
43 if f != nil {
44 c.ops = append(c.ops, f)
45 }
46 }
47
48 type ttyDevice struct {
49 fd int
50 cancel chan struct{}
51 winch chan mesos.TTYInfo_WindowSize
52 cleanups *cleanups
53 original_winsize C.struct_winsize
54 }
55
56 func (t *ttyDevice) Done() <-chan struct{} { return t.cancel }
57 func (t *ttyDevice) Close() { t.cleanups.unwind() }
58
59 func initTTY() (_ *ttyDevice, err error) {
60 return newTTY(
61 ttyConsoleAttach(&os.Stdin, &os.Stdout, &os.Stderr),
62 ttyWinch,
63 ttyTermReset,
64 )
65 }
66
67 func newTTY(opts ...ttyOption) (_ *ttyDevice, err error) {
68 tty := ttyDevice{
69 cancel: make(chan struct{}),
70 cleanups: new(cleanups),
71 }
72 tty.cleanups.push(func() { close(tty.cancel) })
73 defer func() {
74 if err != nil {
75 tty.Close()
76 }
77 }()
78
79 ttyname := C.ctermid((*C.char)(unsafe.Pointer(nil)))
80 if p := (*C.char)(unsafe.Pointer(ttyname)); p == nil {
81 err = fmt.Errorf("failed to get tty name")
82 return
83 }
84
85 tty.fd, _ = syscall.Open(C.GoString(ttyname), syscall.O_RDWR, 0)
86 if tty.fd < 0 {
87 err = fmt.Errorf("failed to open tty device: %d", tty.fd)
88 return
89 }
90 tty.cleanups.push(func() { syscall.Close(tty.fd) })
91
92 var original_termios C.struct_termios
93 result := C.tcgetattr(C.int(tty.fd), &original_termios)
94 if result < 0 {
95 err = fmt.Errorf("failed getting termios: %d", result)
96 return
97 }
98
99 new_termios := original_termios
100 C.cfmakeraw(&new_termios)
101 result = C.tcsetattr(C.int(tty.fd), C.TCSANOW, &new_termios)
102 if result < 0 {
103 err = fmt.Errorf("failed setting termios: %d", result)
104 return
105 }
106 tty.cleanups.push(func() {
107 r := C.tcsetattr(C.int(tty.fd), C.TCSANOW, &original_termios)
108 if r < 0 {
109 log.Printf("failed to set original termios: %d", r)
110 }
111 })
112
113 // use this local var instead of tty.original_winsize to avoid cgo complaints about double-pointers
114 var original_winsize C.struct_winsize
115 result = C.ioctl_winsize(0, C.TIOCGWINSZ, unsafe.Pointer(&original_winsize))
116 if result < 0 {
117 err = fmt.Errorf("failed to get winsize: %d", result)
118 return
119 }
120 tty.original_winsize = original_winsize
121 tty.cleanups.push(func() {
122 r := C.ioctl_winsize(0, C.TIOCSWINSZ, unsafe.Pointer(&original_winsize))
123 if r < 0 {
124 log.Printf("failed to set winsize: %d", r)
125 }
126 })
127
128 log.Printf("original window size is %d x %d\n", tty.original_winsize.ws_col, tty.original_winsize.ws_row)
129
130 for _, f := range opts {
131 if f != nil {
132 f(&tty)
133 }
134 }
135
136 return &tty, nil
137 }
138
139 type ttyOption func(*ttyDevice)
140
141 func ttyConsoleAttach(stdin, stdout, stderr **os.File) ttyOption {
142 swapfd := func(newfd uintptr, name string, target **os.File) func() {
143 f := os.NewFile(newfd, name)
144 if f == nil {
145 panic(fmt.Sprintf("failed to swap fd for %q", name))
146 }
147 old := *target
148 *target = f
149 return func() {
150 *target = old
151 }
152 }
153 return func(tty *ttyDevice) {
154 tty.cleanups.push(swapfd(uintptr(tty.fd), "tty", stdout))
155 tty.cleanups.push(swapfd(uintptr(tty.fd), "tty", stderr))
156 tty.cleanups.push(swapfd(uintptr(tty.fd), "tty", stdin))
157 }
158 }
159
160 func ttyWinch(tty *ttyDevice) {
161 // translate window-size signals into chan events
162 c := make(chan os.Signal, 1)
163 tty.winch = make(chan mesos.TTYInfo_WindowSize, 1)
164 tty.winch <- mesos.TTYInfo_WindowSize{
165 Rows: uint32(tty.original_winsize.ws_row),
166 Columns: uint32(tty.original_winsize.ws_col),
167 }
168 go func() {
169 defer signal.Reset(os.Signal(syscall.SIGWINCH))
170 for {
171 select {
172 case <-c:
173 signal.Ignore(os.Signal(syscall.SIGWINCH))
174 var temp_winsize C.struct_winsize
175 r := C.ioctl_winsize(0, C.TIOCGWINSZ, unsafe.Pointer(&temp_winsize))
176 if r < 0 {
177 panic(fmt.Sprintf("failed to get winsize: %d", r))
178 }
179 ws := mesos.TTYInfo_WindowSize{
180 Rows: uint32(temp_winsize.ws_row),
181 Columns: uint32(temp_winsize.ws_col),
182 }
183 select {
184 case <-tty.Done():
185 return
186 case tty.winch <- ws:
187 signal.Notify(c, os.Signal(syscall.SIGWINCH))
188 }
189 case <-tty.Done():
190 return
191 }
192 }
193 }()
194 signal.Notify(c, os.Signal(syscall.SIGWINCH))
195 }
196
197 func ttyTermReset(tty *ttyDevice) {
198 var (
199 // cleanup properly upon SIGTERM
200 term = make(chan os.Signal, 1)
201 done = make(chan struct{})
202 )
203 go func() {
204 select {
205 case <-term:
206 tty.cleanups.unwind()
207 os.Exit(0)
208 case <-done:
209 //println("stop waiting for SIGTERM")
210 }
211 }()
212 tty.cleanups.push(func() {
213 signal.Reset(os.Signal(syscall.SIGTERM))
214 close(done) // stop waiting for a signal
215 })
216 signal.Notify(term, os.Signal(syscall.SIGTERM))
217 }