oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package creds
2
3import (
4 "bytes"
5 "errors"
6 "os"
7 "unsafe"
8
9 "golang.org/x/sys/unix"
10)
11
12// OpenTerminal opens a pseudo-terminal pair for a sign-in's CLI, wide
13// enough that it never wraps a line: /dev/ptmx, then grantpt, unlockpt
14// and ptsname as ioctls (no cgo).
15func OpenTerminal() (master, tty *os.File, err error) {
16 master, err = os.OpenFile("/dev/ptmx", os.O_RDWR|unix.O_NOCTTY|unix.O_CLOEXEC, 0)
17 if err != nil {
18 return nil, nil, err
19 }
20 fd := master.Fd()
21 for _, op := range []uintptr{unix.TIOCPTYGRANT, unix.TIOCPTYUNLK} {
22 if _, _, e := unix.Syscall(unix.SYS_IOCTL, fd, op, 0); e != 0 {
23 master.Close()
24 return nil, nil, e
25 }
26 }
27 // The pointer is converted in the call itself (unsafe.Pointer rule 4).
28 var name [128]byte
29 if _, _, e := unix.Syscall(unix.SYS_IOCTL, fd, unix.TIOCPTYGNAME, uintptr(unsafe.Pointer(&name[0]))); e != 0 {
30 master.Close()
31 return nil, nil, e
32 }
33 n := bytes.IndexByte(name[:], 0)
34 if n <= 0 {
35 master.Close()
36 return nil, nil, errors.New("creds: no terminal name")
37 }
38 tty, err = os.OpenFile(string(name[:n]), os.O_RDWR|unix.O_NOCTTY|unix.O_CLOEXEC, 0)
39 if err != nil {
40 master.Close()
41 return nil, nil, err
42 }
43 ws := unix.Winsize{Row: 50, Col: 1000}
44 _ = unix.IoctlSetWinsize(int(tty.Fd()), unix.TIOCSWINSZ, &ws)
45 return master, tty, nil
46}
47