• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-artifact / 1607731365

19 Dec 2024 12:59PM UTC coverage: 77.125% (-0.01%) from 77.135%
1607731365

push

gitlab-ci

web-flow
Merge pull request #655 from lluiscampos/MEN-7876-enable-tty-echo-always

MEN-7876: Restore tty echo also when cancelling the context

0 of 1 new or added line in 1 file covered. (0.0%)

5799 of 7519 relevant lines covered (77.12%)

133.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cli/util/tty.go
1
// Copyright 2021 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
//go:build linux || aix || darwin || dragonfly || freebsd || netbsd || opbenbsd
16
// +build linux aix darwin dragonfly freebsd netbsd opbenbsd
17

18
package util
19

20
import (
21
        "context"
22
        "os"
23

24
        "github.com/pkg/errors"
25
        "golang.org/x/sys/unix"
26
)
27

28
// Disable TTY echo of stdin.
29
func DisableEcho(fd int) (*unix.Termios, error) {
×
30
        term, err := unix.IoctlGetTermios(fd, ioctlGetTermios)
×
31
        if err != nil {
×
32
                return nil, err
×
33
        }
×
34

35
        newTerm := *term
×
36
        newTerm.Lflag &^= unix.ECHO
×
37
        newTerm.Lflag |= unix.ICANON | unix.ISIG
×
38
        newTerm.Iflag |= unix.ICRNL
×
39
        if err := unix.IoctlSetTermios(fd, ioctlSetTermios, &newTerm); err != nil {
×
40
                return nil, err
×
41
        }
×
42
        return term, nil
×
43
}
44

45
// Signal handler to re-enable tty echo on interrupt. The signal handler is
46
// transparent with system default, and immedeately releases the channel and
47
// calling the system sighandler after termios is set.
48
func EchoSigHandler(
49
        ctx context.Context,
50
        sigChan chan os.Signal,
51
        errChan chan error,
52
        term *unix.Termios) {
×
53
        for {
×
54
                var (
×
55
                        sig       os.Signal
×
56
                        sigRecved bool
×
57
                )
×
58
                select {
×
59
                case <-ctx.Done():
×
60
                        errChan <- nil
×
NEW
61
                        _ = unix.IoctlSetTermios(int(os.Stdin.Fd()), ioctlSetTermios, term)
×
62
                        return
×
63
                case sig, sigRecved = <-sigChan:
×
64
                }
65
                if sig == unix.SIGWINCH || sig == unix.SIGURG {
×
66
                        // Though SIGCHLD is ignored by default, in this context
×
67
                        // we want to restore echo state.
×
68
                        continue
×
69
                }
70
                // Restore Termios
71
                _ = unix.IoctlSetTermios(int(os.Stdin.Fd()), ioctlSetTermios, term)
×
72
                if sigRecved {
×
73
                        switch sig {
×
74
                        case unix.SIGCHLD:
×
75
                                // SIGCHLD is expected when ssh terminates.
×
76
                                errChan <- nil
×
77
                        default:
×
78
                                errChan <- errors.Errorf("Received signal: %s",
×
79
                                        unix.SignalName(sig.(unix.Signal)))
×
80
                                // Relay signal to default handler
×
81
                                _ = unix.Kill(os.Getpid(), sig.(unix.Signal))
×
82
                        }
83
                } else {
×
84
                        errChan <- nil
×
85
                        return
×
86
                }
×
87
        }
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc