...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package procfs
15
16 import (
17 "bufio"
18 "bytes"
19 "fmt"
20 "io"
21 "strings"
22
23 "github.com/prometheus/procfs/internal/util"
24 )
25
26
27 type Crypto struct {
28 Alignmask *uint64
29 Async bool
30 Blocksize *uint64
31 Chunksize *uint64
32 Ctxsize *uint64
33 Digestsize *uint64
34 Driver string
35 Geniv string
36 Internal string
37 Ivsize *uint64
38 Maxauthsize *uint64
39 MaxKeysize *uint64
40 MinKeysize *uint64
41 Module string
42 Name string
43 Priority *int64
44 Refcnt *int64
45 Seedsize *uint64
46 Selftest string
47 Type string
48 Walksize *uint64
49 }
50
51
52
53
54 func (fs FS) Crypto() ([]Crypto, error) {
55 path := fs.proc.Path("crypto")
56 b, err := util.ReadFileNoStat(path)
57 if err != nil {
58 return nil, fmt.Errorf("%s: Cannot read file %v: %w", ErrFileRead, b, err)
59
60 }
61
62 crypto, err := parseCrypto(bytes.NewReader(b))
63 if err != nil {
64 return nil, fmt.Errorf("%s: Cannot parse %v: %w", ErrFileParse, crypto, err)
65 }
66
67 return crypto, nil
68 }
69
70
71 func parseCrypto(r io.Reader) ([]Crypto, error) {
72 var out []Crypto
73
74 s := bufio.NewScanner(r)
75 for s.Scan() {
76 text := s.Text()
77 switch {
78 case strings.HasPrefix(text, "name"):
79
80 out = append(out, Crypto{})
81 case text == "":
82 continue
83 }
84
85 kv := strings.Split(text, ":")
86 if len(kv) != 2 {
87 return nil, fmt.Errorf("%w: Cannot parse line: %q", ErrFileParse, text)
88 }
89
90 k := strings.TrimSpace(kv[0])
91 v := strings.TrimSpace(kv[1])
92
93
94 c := &out[len(out)-1]
95 if err := c.parseKV(k, v); err != nil {
96 return nil, err
97 }
98 }
99
100 if err := s.Err(); err != nil {
101 return nil, err
102 }
103
104 return out, nil
105 }
106
107
108 func (c *Crypto) parseKV(k, v string) error {
109 vp := util.NewValueParser(v)
110
111 switch k {
112 case "async":
113
114 c.Async = v == "yes"
115 case "blocksize":
116 c.Blocksize = vp.PUInt64()
117 case "chunksize":
118 c.Chunksize = vp.PUInt64()
119 case "digestsize":
120 c.Digestsize = vp.PUInt64()
121 case "driver":
122 c.Driver = v
123 case "geniv":
124 c.Geniv = v
125 case "internal":
126 c.Internal = v
127 case "ivsize":
128 c.Ivsize = vp.PUInt64()
129 case "maxauthsize":
130 c.Maxauthsize = vp.PUInt64()
131 case "max keysize":
132 c.MaxKeysize = vp.PUInt64()
133 case "min keysize":
134 c.MinKeysize = vp.PUInt64()
135 case "module":
136 c.Module = v
137 case "name":
138 c.Name = v
139 case "priority":
140 c.Priority = vp.PInt64()
141 case "refcnt":
142 c.Refcnt = vp.PInt64()
143 case "seedsize":
144 c.Seedsize = vp.PUInt64()
145 case "selftest":
146 c.Selftest = v
147 case "type":
148 c.Type = v
149 case "walksize":
150 c.Walksize = vp.PUInt64()
151 }
152
153 return vp.Err()
154 }
155
View as plain text