kernel.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright The OpenTelemetry 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. package utils
  15. import (
  16. "bufio"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "strconv"
  21. "strings"
  22. "syscall"
  23. "github.com/hashicorp/go-version"
  24. )
  25. var unameFn = syscall.Uname
  26. // parse logic adapted from https://github.com/golang/go/blob/go1.21.3/src/internal/syscall/unix/kernel_version_linux.go
  27. func GetLinuxKernelVersion() (*version.Version, error) {
  28. var uname syscall.Utsname
  29. if err := unameFn(&uname); err != nil {
  30. return nil, err
  31. }
  32. var (
  33. values [2]int
  34. value, vi int
  35. )
  36. for _, c := range uname.Release {
  37. if '0' <= c && c <= '9' {
  38. value = (value * 10) + int(c-'0')
  39. } else {
  40. // Note that we're assuming N.N.N here.
  41. // If we see anything else, we are likely to mis-parse it.
  42. values[vi] = value
  43. vi++
  44. if vi >= len(values) {
  45. break
  46. }
  47. value = 0
  48. }
  49. }
  50. ver := fmt.Sprintf("%s.%s", strconv.Itoa(values[0]), strconv.Itoa(values[1]))
  51. return version.NewVersion(ver)
  52. }
  53. type KernelLockdown uint8
  54. const (
  55. KernelLockdownNone KernelLockdown = iota + 1 // Linux Kernel security lockdown mode [none]
  56. KernelLockdownIntegrity // Linux Kernel security lockdown mode [integrity]
  57. KernelLockdownConfidentiality // Linux Kernel security lockdown mode [confidentiality]
  58. KernelLockdownOther // Linux Kernel security lockdown mode unknown
  59. )
  60. // Injectable for tests.
  61. var lockdownPath = "/sys/kernel/security/lockdown"
  62. func KernelLockdownMode() KernelLockdown {
  63. // If we can't find the file, assume no lockdown
  64. if _, err := os.Stat(lockdownPath); err == nil {
  65. f, err := os.Open(lockdownPath)
  66. if err != nil {
  67. return KernelLockdownIntegrity
  68. }
  69. defer f.Close()
  70. scanner := bufio.NewScanner(f)
  71. if scanner.Scan() {
  72. lockdown := scanner.Text()
  73. if strings.Contains(lockdown, "[none]") {
  74. return KernelLockdownNone
  75. } else if strings.Contains(lockdown, "[integrity]") {
  76. return KernelLockdownIntegrity
  77. } else if strings.Contains(lockdown, "[confidentiality]") {
  78. return KernelLockdownConfidentiality
  79. }
  80. return KernelLockdownOther
  81. }
  82. return KernelLockdownIntegrity
  83. }
  84. return KernelLockdownNone
  85. }
  86. // Injectable for tests, this file is one of the files LSCPU looks for.
  87. var cpuPresentPath = "/sys/devices/system/cpu/present"
  88. func GetCPUCountFromSysDevices() (int, error) {
  89. rawFile, err := os.ReadFile(cpuPresentPath)
  90. if err != nil {
  91. return 0, err
  92. }
  93. cpuCount, err := parseCPUList(string(rawFile))
  94. if err != nil {
  95. return 0, err
  96. }
  97. return cpuCount, nil
  98. }
  99. func parseCPUList(raw string) (int, error) {
  100. listPart := strings.Split(raw, ",")
  101. count := 0
  102. for _, v := range listPart {
  103. if strings.Contains(v, "-") {
  104. rangeC, err := parseCPURange(v)
  105. if err != nil {
  106. return 0, fmt.Errorf("error parsing line %s: %w", v, err)
  107. }
  108. count = count + rangeC
  109. } else {
  110. count++
  111. }
  112. }
  113. return count, nil
  114. }
  115. func parseCPURange(cpuRange string) (int, error) {
  116. var first, last int
  117. _, err := fmt.Sscanf(cpuRange, "%d-%d", &first, &last)
  118. if err != nil {
  119. return 0, fmt.Errorf("error reading from range %s: %w", cpuRange, err)
  120. }
  121. return (last - first) + 1, nil
  122. }
  123. var procInfoPath = "/proc/cpuinfo"
  124. func GetCPUCountFromProc() (int, error) {
  125. file, err := os.Open(procInfoPath)
  126. if err != nil {
  127. return 0, err
  128. }
  129. defer file.Close()
  130. scanner := bufio.NewScanner(file)
  131. count := 0
  132. for scanner.Scan() {
  133. if strings.Contains(scanner.Text(), "processor") {
  134. count++
  135. }
  136. }
  137. if err := scanner.Err(); err != nil {
  138. return 0, err
  139. }
  140. return count, nil
  141. }
  142. func GetCPUCount() (int, error) {
  143. var err error
  144. // First try to get the CPU count from /sys/devices
  145. cpuCount, e := GetCPUCountFromSysDevices()
  146. if e == nil {
  147. return cpuCount, nil
  148. }
  149. err = errors.Join(err, e)
  150. // If that fails, try to get the CPU count from /proc
  151. cpuCount, e = GetCPUCountFromProc()
  152. if e == nil {
  153. return cpuCount, nil
  154. }
  155. err = errors.Join(err, e)
  156. return 0, err
  157. }