blob: 5f2a37a78b3fcd04da02f6b64dd7d9f38d5fd339 [file] [log] [blame]
Abhay Kumara61c5222025-11-10 07:32:50 +00001// 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
14package procfs
15
16import (
17 "bufio"
18 "bytes"
19 "fmt"
20 "io"
21 "strings"
22
23 "github.com/prometheus/procfs/internal/util"
24)
25
26// Crypto holds info parsed from /proc/crypto.
27type 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// Crypto parses an crypto-file (/proc/crypto) and returns a slice of
52// structs containing the relevant info. More information available here:
53// https://kernel.readthedocs.io/en/sphinx-samples/crypto-API.html
54func (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("%w: 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("%w: Cannot parse %v: %w", ErrFileParse, crypto, err)
65 }
66
67 return crypto, nil
68}
69
70// parseCrypto parses a /proc/crypto stream into Crypto elements.
71func 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 // Each crypto element begins with its name.
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 // Parse the key/value pair into the currently focused element.
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// parseKV parses a key/value pair into the appropriate field of c.
108func (c *Crypto) parseKV(k, v string) error {
109 vp := util.NewValueParser(v)
110
111 switch k {
112 case "async":
113 // Interpret literal yes as true.
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}