cubefs
153 строки · 3.5 Кб
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
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 {
28Alignmask *uint64
29Async bool
30Blocksize *uint64
31Chunksize *uint64
32Ctxsize *uint64
33Digestsize *uint64
34Driver string
35Geniv string
36Internal string
37Ivsize *uint64
38Maxauthsize *uint64
39MaxKeysize *uint64
40MinKeysize *uint64
41Module string
42Name string
43Priority *int64
44Refcnt *int64
45Seedsize *uint64
46Selftest string
47Type string
48Walksize *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) {
55path := fs.proc.Path("crypto")
56b, err := util.ReadFileNoStat(path)
57if err != nil {
58return nil, fmt.Errorf("error reading crypto %q: %w", path, err)
59}
60
61crypto, err := parseCrypto(bytes.NewReader(b))
62if err != nil {
63return nil, fmt.Errorf("error parsing crypto %q: %w", path, err)
64}
65
66return crypto, nil
67}
68
69// parseCrypto parses a /proc/crypto stream into Crypto elements.
70func parseCrypto(r io.Reader) ([]Crypto, error) {
71var out []Crypto
72
73s := bufio.NewScanner(r)
74for s.Scan() {
75text := s.Text()
76switch {
77case strings.HasPrefix(text, "name"):
78// Each crypto element begins with its name.
79out = append(out, Crypto{})
80case text == "":
81continue
82}
83
84kv := strings.Split(text, ":")
85if len(kv) != 2 {
86return nil, fmt.Errorf("malformed crypto line: %q", text)
87}
88
89k := strings.TrimSpace(kv[0])
90v := strings.TrimSpace(kv[1])
91
92// Parse the key/value pair into the currently focused element.
93c := &out[len(out)-1]
94if err := c.parseKV(k, v); err != nil {
95return nil, err
96}
97}
98
99if err := s.Err(); err != nil {
100return nil, err
101}
102
103return out, nil
104}
105
106// parseKV parses a key/value pair into the appropriate field of c.
107func (c *Crypto) parseKV(k, v string) error {
108vp := util.NewValueParser(v)
109
110switch k {
111case "async":
112// Interpret literal yes as true.
113c.Async = v == "yes"
114case "blocksize":
115c.Blocksize = vp.PUInt64()
116case "chunksize":
117c.Chunksize = vp.PUInt64()
118case "digestsize":
119c.Digestsize = vp.PUInt64()
120case "driver":
121c.Driver = v
122case "geniv":
123c.Geniv = v
124case "internal":
125c.Internal = v
126case "ivsize":
127c.Ivsize = vp.PUInt64()
128case "maxauthsize":
129c.Maxauthsize = vp.PUInt64()
130case "max keysize":
131c.MaxKeysize = vp.PUInt64()
132case "min keysize":
133c.MinKeysize = vp.PUInt64()
134case "module":
135c.Module = v
136case "name":
137c.Name = v
138case "priority":
139c.Priority = vp.PInt64()
140case "refcnt":
141c.Refcnt = vp.PInt64()
142case "seedsize":
143c.Seedsize = vp.PUInt64()
144case "selftest":
145c.Selftest = v
146case "type":
147c.Type = v
148case "walksize":
149c.Walksize = vp.PUInt64()
150}
151
152return vp.Err()
153}
154