-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathterminal.go
484 lines (408 loc) · 10.4 KB
/
terminal.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package terminal
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
"time"
"github.com/creack/pty"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"golang.org/x/xerrors"
"github.com/gitpod-io/gitpod/common-go/log"
)
// NewMux creates a new terminal mux
func NewMux() *Mux {
return &Mux{
terms: make(map[string]*Term),
}
}
// Mux can mux pseudo-terminals
type Mux struct {
aliases []string
terms map[string]*Term
mu sync.RWMutex
}
// Get returns a terminal for the given alias
func (m *Mux) Get(alias string) (*Term, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
term, ok := m.terms[alias]
return term, ok
}
// Start starts a new command in its own pseudo-terminal and returns an alias
// for that pseudo terminal.
func (m *Mux) Start(cmd *exec.Cmd, options TermOptions) (alias string, err error) {
m.mu.Lock()
defer m.mu.Unlock()
pty, err := pty.StartWithSize(cmd, options.Size)
if err != nil {
return "", xerrors.Errorf("cannot start PTY: %w", err)
}
uid, err := uuid.NewRandom()
if err != nil {
return "", xerrors.Errorf("cannot produce alias: %w", err)
}
alias = uid.String()
term, err := newTerm(alias, pty, cmd, options)
if err != nil {
pty.Close()
return "", err
}
m.aliases = append(m.aliases, alias)
m.terms[alias] = term
log.WithField("alias", alias).WithField("cmd", cmd.Path).Info("started new terminal")
go func() {
term.waitErr = cmd.Wait()
close(term.waitDone)
_ = m.CloseTerminal(alias, 0*time.Second)
}()
return alias, nil
}
// Close closes all terminals with closeTerminaldefaultGracePeriod.
func (m *Mux) Close() error {
m.mu.Lock()
defer m.mu.Unlock()
var err error
for k := range m.terms {
cerr := m.doClose(k, closeTerminaldefaultGracePeriod)
if cerr != nil {
log.WithError(cerr).WithField("alias", k).Warn("cannot properly close terminal")
if err != nil {
err = cerr
}
}
}
return err
}
// CloseTerminal closes a terminal and ends the process that runs in it
func (m *Mux) CloseTerminal(alias string, gracePeriod time.Duration) error {
m.mu.Lock()
defer m.mu.Unlock()
return m.doClose(alias, gracePeriod)
}
// doClose closes a terminal and ends the process that runs in it.
// First, the process receives SIGTERM and is given gracePeriod time
// to stop. If it still runs after that time, it receives SIGKILL.
//
// Callers are expected to hold mu.
func (m *Mux) doClose(alias string, gracePeriod time.Duration) error {
term, ok := m.terms[alias]
if !ok {
return ErrNotFound
}
log := log.WithField("alias", alias)
log.Info("closing terminal")
err := term.gracefullyShutdownProcess(gracePeriod)
if err != nil {
log.WithError(err).Warn("did not gracefully shut down terminal")
}
err = term.Stdout.Close()
if err != nil {
log.WithError(err).Warn("cannot close connection to terminal clients")
}
err = term.PTY.Close()
if err != nil {
log.WithError(err).Warn("cannot close pseudo-terminal")
}
i := 0
for i < len(m.aliases) && m.aliases[i] != alias {
i++
}
if i != len(m.aliases) {
m.aliases = append(m.aliases[:i], m.aliases[i+1:]...)
}
delete(m.terms, alias)
return nil
}
func (term *Term) gracefullyShutdownProcess(gracePeriod time.Duration) error {
if term.Command.Process == nil {
// process is alrady gone
return nil
}
if gracePeriod == 0 {
return term.shutdownProcessImmediately()
}
err := term.Command.Process.Signal(unix.SIGTERM)
if err != nil {
return err
}
schan := make(chan error, 1)
go func() {
_, err := term.Wait()
schan <- err
}()
select {
case err = <-schan:
if err == nil {
// process is gone now - we're good
return nil
}
case <-time.After(gracePeriod):
}
// process did not exit in time. Let's kill.
return term.shutdownProcessImmediately()
}
func (term *Term) shutdownProcessImmediately() error {
err := term.Command.Process.Kill()
if err != nil && !strings.Contains(err.Error(), "os: process already finished") {
return err
}
return nil
}
// terminalBacklogSize is the number of bytes of output we'll store in RAM for each terminal.
// The higher this number is, the better the UX, but the higher the resource requirements are.
// For now we assume an average of five terminals per workspace, which makes this consume 1MiB of RAM.
const terminalBacklogSize = 256 << 10
func newTerm(alias string, pty *os.File, cmd *exec.Cmd, options TermOptions) (*Term, error) {
token, err := uuid.NewRandom()
if err != nil {
return nil, err
}
recorder, err := NewRingBuffer(terminalBacklogSize)
if err != nil {
return nil, err
}
timeout := options.ReadTimeout
if timeout == 0 {
timeout = 1<<63 - 1
}
res := &Term{
PTY: pty,
Command: cmd,
Stdout: &multiWriter{
timeout: timeout,
listener: make(map[*multiWriterListener]struct{}),
recorder: recorder,
logStdout: options.LogToStdout,
logLabel: alias,
},
Annotations: options.Annotations,
title: options.Title,
StarterToken: token.String(),
waitDone: make(chan struct{}),
}
rawConn, err := pty.SyscallConn()
if err != nil {
return nil, err
}
rawConn.Control(func(fileFd uintptr) {
res.fd = int(fileFd)
})
go io.Copy(res.Stdout, pty)
return res, nil
}
// TermOptions is a pseudo-terminal configuration
type TermOptions struct {
// timeout after which a listener is dropped. Use 0 for no timeout.
ReadTimeout time.Duration
// Annotations are user-defined metadata that's attached to a terminal
Annotations map[string]string
// Size describes the terminal size.
Size *pty.Winsize
// Title describes the terminal title.
Title string
// LogToStdout forwards the terminal's stdout to supervisor's stdout
LogToStdout bool
}
// Term is a pseudo-terminal
type Term struct {
PTY *os.File
Command *exec.Cmd
StarterToken string
Annotations map[string]string
title string
Stdout *multiWriter
waitErr error
waitDone chan struct{}
fd int
}
func (term *Term) GetTitle() (string, error) {
var b bytes.Buffer
title := term.title
b.WriteString(title)
command, err := term.resolveForegroundCommand()
if title != "" && command != "" {
b.WriteString(": ")
}
b.WriteString(command)
return b.String(), err
}
func (term *Term) resolveForegroundCommand() (string, error) {
pgrp, err := unix.IoctlGetInt(term.fd, unix.TIOCGPGRP)
if err != nil {
return "", err
}
content, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pgrp))
if err != nil {
return "", err
}
end := bytes.Index(content, []byte{0})
if end != -1 {
content = content[:end]
}
start := bytes.LastIndex(content, []byte{os.PathSeparator})
if start != -1 {
content = content[(start + 1):]
}
return string(content), nil
}
// Wait waits for the terminal to exit and returns the resulted process state
func (term *Term) Wait() (*os.ProcessState, error) {
select {
case <-term.waitDone:
}
return term.Command.ProcessState, term.waitErr
}
// multiWriter is like io.MultiWriter, except that we can listener at runtime.
type multiWriter struct {
timeout time.Duration
closed bool
mu sync.RWMutex
listener map[*multiWriterListener]struct{}
// ring buffer to record last 256kb of pty output
// new listener is initialized with the latest recodring first
recorder *RingBuffer
logStdout bool
logLabel string
}
var (
// ErrNotFound means the terminal was not found
ErrNotFound = errors.New("not found")
// ErrReadTimeout happens when a listener takes too long to read
ErrReadTimeout = errors.New("read timeout")
)
type multiWriterListener struct {
io.Reader
closed bool
once sync.Once
closeErr error
closeChan chan struct{}
cchan chan []byte
done chan struct{}
}
func (l *multiWriterListener) Close() error {
return l.CloseWithError(nil)
}
func (l *multiWriterListener) CloseWithError(err error) error {
l.once.Do(func() {
if err != nil {
l.closeErr = err
}
close(l.closeChan)
l.closed = true
// actual cleanup happens in a go routine started by Listen()
})
return nil
}
func (l *multiWriterListener) Done() <-chan struct{} {
return l.closeChan
}
type closedTerminalListener struct {
}
func (closedTerminalListener) Read(p []byte) (n int, err error) {
return 0, io.EOF
}
var closedListener = io.NopCloser(closedTerminalListener{})
// Listen listens in on the multi-writer stream
func (mw *multiWriter) Listen() io.ReadCloser {
mw.mu.Lock()
defer mw.mu.Unlock()
if mw.closed {
return closedListener
}
r, w := io.Pipe()
cchan, done, closeChan := make(chan []byte), make(chan struct{}, 1), make(chan struct{}, 1)
res := &multiWriterListener{
Reader: r,
cchan: cchan,
done: done,
closeChan: closeChan,
}
recording := mw.recorder.Bytes()
go func() {
_, _ = w.Write(recording)
// copy bytes from channel to writer.
// Note: we close the writer independently of the write operation s.t. we don't
// block the closing because the write's blocking.
for b := range cchan {
n, err := w.Write(b)
done <- struct{}{}
if err == nil && n != len(b) {
err = io.ErrShortWrite
}
if err != nil {
_ = res.CloseWithError(err)
}
}
}()
go func() {
// listener cleanup on close
<-closeChan
if res.closeErr != nil {
log.WithError(res.closeErr).Error("terminal listener droped out")
w.CloseWithError(res.closeErr)
} else {
w.Close()
}
close(cchan)
mw.mu.Lock()
delete(mw.listener, res)
mw.mu.Unlock()
}()
mw.listener[res] = struct{}{}
return res
}
func (mw *multiWriter) Write(p []byte) (n int, err error) {
mw.mu.Lock()
defer mw.mu.Unlock()
mw.recorder.Write(p)
if mw.logStdout {
log.WithFields(logrus.Fields{
"terminalOutput": true,
"label": mw.logLabel,
}).Info(string(p))
}
for lstr := range mw.listener {
if lstr.closed {
continue
}
select {
case lstr.cchan <- p:
case <-time.After(mw.timeout):
lstr.CloseWithError(ErrReadTimeout)
}
select {
case <-lstr.done:
case <-time.After(mw.timeout):
lstr.CloseWithError(ErrReadTimeout)
}
}
return len(p), nil
}
func (mw *multiWriter) Close() error {
mw.mu.Lock()
defer mw.mu.Unlock()
mw.closed = true
var err error
for w := range mw.listener {
cerr := w.Close()
if cerr != nil {
err = cerr
}
}
return err
}
func (mw *multiWriter) ListenerCount() int {
mw.mu.Lock()
defer mw.mu.Unlock()
return len(mw.listener)
}