Debian Codegraph @

internal/dev/tty.go

raw symbols SCIP
1 // Copyright 2018 psgo authors
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 package dev
16
17 import (
18 "os"
19 "strings"
20 "syscall"
21 )
22
23 // TTY represents a tty including its minor and major device number and the
24 // path to the tty.
25 type TTY struct {
26 // Minor device number.
27 Minor uint64
28 // Major device number.
29 Major uint64
30 // Path to the tty device.
31 Path string
32 }
33
34 // FindTTY return the corresponding TTY to the ttyNr or nil of non could be
35 // found.
36 func FindTTY(ttyNr uint64, devices *[]TTY) (*TTY, error) {
37 // (man 5 proc) The minor device number is contained in the combination
38 // of bits 31 to 20 and 7 to 0; the major device number is in bits 15
39 // to 8.
40 maj := (ttyNr >> 8) & 0xFF
41 min := (ttyNr & 0xFF) | ((ttyNr >> 20) & 0xFFF)
42
43 if devices == nil {
44 devs, err := TTYs()
45 if err != nil {
46 return nil, err
47 }
48 devices = devs
49 }
50
51 for _, t := range *devices {
52 if t.Minor == min && t.Major == maj {
53 return &t, nil
54 }
55 }
56
57 return nil, nil
58 }
59
60 // majDevNum returns the major device number of rdev (see stat_t.Rdev).
61 func majDevNum(rdev uint64) uint64 {
62 return (rdev >> 8) & 0xfff
63 }
64
65 // minDevNum returns the minor device number of rdev (see stat_t.Rdev).
66 func minDevNum(rdev uint64) uint64 {
67 return (rdev & 0xff) | ((rdev >> 12) & 0xfff00)
68 }
69
70 // TTYs parses /dev for tty and pts devices.
71 func TTYs() (*[]TTY, error) {
72 devDir, err := os.Open("/dev/")
73 if err != nil {
74 return nil, err
75 }
76 defer devDir.Close() //nolint:errcheck
77
78 devices := []string{}
79 devTTYs, err := devDir.Readdirnames(0)
80 if err != nil {
81 return nil, err
82 }
83 for _, d := range devTTYs {
84 if !strings.HasPrefix(d, "tty") {
85 continue
86 }
87 devices = append(devices, "/dev/"+d)
88 }
89
90 devPTSDir, err := os.Open("/dev/pts/")
91 if err != nil {
92 return nil, err
93 }
94 defer devPTSDir.Close() //nolint:errcheck
95
96 devPTSs, err := devPTSDir.Readdirnames(0)
97 if err != nil {
98 return nil, err
99 }
100 for _, d := range devPTSs {
101 devices = append(devices, "/dev/pts/"+d)
102 }
103
104 ttys := []TTY{}
105 for _, dev := range devices {
106 fi, err := os.Stat(dev)
107 if err != nil {
108 if os.IsNotExist(err) {
109 // catch race conditions
110 continue
111 }
112 return nil, err
113 }
114 s := fi.Sys().(*syscall.Stat_t)
115 t := TTY{
116 // Rdev is type uint32 on mips arch so we have to cast to uint64
117 Minor: minDevNum(uint64(s.Rdev)),
118 Major: majDevNum(uint64(s.Rdev)),
119 Path: dev,
120 }
121 ttys = append(ttys, t)
122 }
123
124 return &ttys, nil
125 }