tty.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2021 Jeffrey H. Johnson <trnsz@pobox.com>
  2. // Copyright 2018 psgo authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package dev
  16. import (
  17. "os"
  18. "strings"
  19. "syscall"
  20. )
  21. // TTY represents a tty including its minor and major device number and the
  22. // path to the tty.
  23. type TTY struct {
  24. // Minor device number.
  25. Minor uint64
  26. // Major device number.
  27. Major uint64
  28. // Path to the tty device.
  29. Path string
  30. }
  31. // FindTTY return the corresponding TTY to the ttyNr or nil of non could be
  32. // found.
  33. func FindTTY(ttyNr uint64, devices *[]TTY) (*TTY, error) {
  34. // (man 5 proc) The minor device number is contained in the combination
  35. // of bits 31 to 20 and 7 to 0; the major device number is in bits 15
  36. // to 8.
  37. maj := (ttyNr >> 8) & 0xFF
  38. min := (ttyNr & 0xFF) | ((ttyNr >> 20) & 0xFFF)
  39. if devices == nil {
  40. devs, err := TTYs()
  41. if err != nil {
  42. return nil, err
  43. }
  44. devices = devs
  45. }
  46. for _, t := range *devices {
  47. if t.Minor == min && t.Major == maj {
  48. return &t, nil
  49. }
  50. }
  51. return nil, nil
  52. }
  53. // majDevNum returns the major device number of rdev (see stat_t.Rdev).
  54. func majDevNum(rdev uint64) uint64 {
  55. return (rdev >> 8) & 0xFFF
  56. }
  57. // minDevNum returns the minor device number of rdev (see stat_t.Rdev).
  58. func minDevNum(rdev uint64) uint64 {
  59. return (rdev & 0xFF) | ((rdev >> 12) & 0xFFF00)
  60. }
  61. // TTYs parses /dev for tty and pts devices.
  62. func TTYs() (*[]TTY, error) {
  63. devDir, err := os.Open("/dev/")
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer devDir.Close()
  68. devices := []string{}
  69. devTTYs, err := devDir.Readdirnames(0)
  70. if err != nil {
  71. return nil, err
  72. }
  73. for _, d := range devTTYs {
  74. if !strings.HasPrefix(d, "tty") {
  75. continue
  76. }
  77. devices = append(devices, "/dev/"+d)
  78. }
  79. devPTSDir, err := os.Open("/dev/pts/")
  80. if err != nil {
  81. return nil, err
  82. }
  83. defer devPTSDir.Close()
  84. devPTSs, err := devPTSDir.Readdirnames(0)
  85. if err != nil {
  86. return nil, err
  87. }
  88. for _, d := range devPTSs {
  89. devices = append(devices, "/dev/pts/"+d)
  90. }
  91. ttys := []TTY{}
  92. for _, dev := range devices {
  93. fi, err := os.Stat(dev)
  94. if err != nil {
  95. if os.IsNotExist(err) {
  96. // catch race conditions
  97. continue
  98. }
  99. return nil, err
  100. }
  101. s := fi.Sys().(*syscall.Stat_t)
  102. t := TTY{
  103. // Rdev is type uint32 on mips arch so we have to cast to uint64
  104. Minor: minDevNum(uint64(s.Rdev)),
  105. Major: majDevNum(uint64(s.Rdev)),
  106. Path: dev,
  107. }
  108. ttys = append(ttys, t)
  109. }
  110. return &ttys, nil
  111. }