No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

154 líneas
3.5 KiB

  1. // Copyright 2019 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package procfs
  14. import (
  15. "bufio"
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "strings"
  20. "github.com/prometheus/procfs/internal/util"
  21. )
  22. // Crypto holds info parsed from /proc/crypto.
  23. type Crypto struct {
  24. Alignmask *uint64
  25. Async bool
  26. Blocksize *uint64
  27. Chunksize *uint64
  28. Ctxsize *uint64
  29. Digestsize *uint64
  30. Driver string
  31. Geniv string
  32. Internal string
  33. Ivsize *uint64
  34. Maxauthsize *uint64
  35. MaxKeysize *uint64
  36. MinKeysize *uint64
  37. Module string
  38. Name string
  39. Priority *int64
  40. Refcnt *int64
  41. Seedsize *uint64
  42. Selftest string
  43. Type string
  44. Walksize *uint64
  45. }
  46. // Crypto parses an crypto-file (/proc/crypto) and returns a slice of
  47. // structs containing the relevant info. More information available here:
  48. // https://kernel.readthedocs.io/en/sphinx-samples/crypto-API.html
  49. func (fs FS) Crypto() ([]Crypto, error) {
  50. path := fs.proc.Path("crypto")
  51. b, err := util.ReadFileNoStat(path)
  52. if err != nil {
  53. return nil, fmt.Errorf("error reading crypto %q: %w", path, err)
  54. }
  55. crypto, err := parseCrypto(bytes.NewReader(b))
  56. if err != nil {
  57. return nil, fmt.Errorf("error parsing crypto %q: %w", path, err)
  58. }
  59. return crypto, nil
  60. }
  61. // parseCrypto parses a /proc/crypto stream into Crypto elements.
  62. func parseCrypto(r io.Reader) ([]Crypto, error) {
  63. var out []Crypto
  64. s := bufio.NewScanner(r)
  65. for s.Scan() {
  66. text := s.Text()
  67. switch {
  68. case strings.HasPrefix(text, "name"):
  69. // Each crypto element begins with its name.
  70. out = append(out, Crypto{})
  71. case text == "":
  72. continue
  73. }
  74. kv := strings.Split(text, ":")
  75. if len(kv) != 2 {
  76. return nil, fmt.Errorf("malformed crypto line: %q", text)
  77. }
  78. k := strings.TrimSpace(kv[0])
  79. v := strings.TrimSpace(kv[1])
  80. // Parse the key/value pair into the currently focused element.
  81. c := &out[len(out)-1]
  82. if err := c.parseKV(k, v); err != nil {
  83. return nil, err
  84. }
  85. }
  86. if err := s.Err(); err != nil {
  87. return nil, err
  88. }
  89. return out, nil
  90. }
  91. // parseKV parses a key/value pair into the appropriate field of c.
  92. func (c *Crypto) parseKV(k, v string) error {
  93. vp := util.NewValueParser(v)
  94. switch k {
  95. case "async":
  96. // Interpret literal yes as true.
  97. c.Async = v == "yes"
  98. case "blocksize":
  99. c.Blocksize = vp.PUInt64()
  100. case "chunksize":
  101. c.Chunksize = vp.PUInt64()
  102. case "digestsize":
  103. c.Digestsize = vp.PUInt64()
  104. case "driver":
  105. c.Driver = v
  106. case "geniv":
  107. c.Geniv = v
  108. case "internal":
  109. c.Internal = v
  110. case "ivsize":
  111. c.Ivsize = vp.PUInt64()
  112. case "maxauthsize":
  113. c.Maxauthsize = vp.PUInt64()
  114. case "max keysize":
  115. c.MaxKeysize = vp.PUInt64()
  116. case "min keysize":
  117. c.MinKeysize = vp.PUInt64()
  118. case "module":
  119. c.Module = v
  120. case "name":
  121. c.Name = v
  122. case "priority":
  123. c.Priority = vp.PInt64()
  124. case "refcnt":
  125. c.Refcnt = vp.PInt64()
  126. case "seedsize":
  127. c.Seedsize = vp.PUInt64()
  128. case "selftest":
  129. c.Selftest = v
  130. case "type":
  131. c.Type = v
  132. case "walksize":
  133. c.Walksize = vp.PUInt64()
  134. }
  135. return vp.Err()
  136. }