| 70 | } |
| 71 | |
| 72 | func (t *tty) initHostConsole() error { |
| 73 | // Usually all three (stdin, stdout, and stderr) streams are open to |
| 74 | // the terminal, but they might be redirected, so try them all. |
| 75 | for _, s := range []*os.File{os.Stderr, os.Stdout, os.Stdin} { |
| 76 | c, err := console.ConsoleFromFile(s) |
| 77 | if err == nil { |
| 78 | t.hostConsole = c |
| 79 | return nil |
| 80 | } |
| 81 | if errors.Is(err, console.ErrNotAConsole) { |
| 82 | continue |
| 83 | } |
| 84 | // should not happen |
| 85 | return fmt.Errorf("unable to get console: %w", err) |
| 86 | } |
| 87 | // If all streams are redirected, but we still have a controlling |
| 88 | // terminal, it can be obtained by opening /dev/tty. |
| 89 | tty, err := os.Open("/dev/tty") |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | c, err := console.ConsoleFromFile(tty) |
| 94 | if err != nil { |
| 95 | return fmt.Errorf("unable to get console: %w", err) |
| 96 | } |
| 97 | |
| 98 | t.hostConsole = c |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | func (t *tty) recvtty(socket *os.File) (Err error) { |
| 103 | f, err := cmsg.RecvFile(socket) |