-
Notifications
You must be signed in to change notification settings - Fork 2
/
kernel_linux.go
172 lines (137 loc) · 3.34 KB
/
kernel_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package system
import (
"bufio"
"compress/gzip"
"fmt"
"os"
"strings"
)
type KernelFeatures struct {
Raw map[string]string
Error string
}
func (p *KernelFeatures) IsConfigured(name string) bool {
if _, ok := p.Raw[name]; ok {
return true
}
return false
}
func (p *KernelFeatures) RawValue(name string) (string, error) {
if val, ok := p.Raw[name]; ok {
return val, nil
}
return "", ErrNotConfigured
}
func (p *KernelFeatures) IsFlag(name string) (bool, error) {
if val, ok := p.Raw[name]; ok {
if (val == "y") || (val == "m") {
return true, nil
}
return false, nil
}
return false, ErrNotConfigured
}
func (p *KernelFeatures) isFlagSet(name string, flag string) (bool, error) {
if val, ok := p.Raw[name]; ok {
if val == flag {
return true, nil
}
return false, nil
}
return false, ErrNotConfigured
}
func (p *KernelFeatures) IsCompiled(name string) (bool, error) {
return p.isFlagSet(name, "y")
}
func (p *KernelFeatures) IsLoadable(name string) (bool, error) {
return p.isFlagSet(name, "m")
}
func NewKernelFeatures() (KernelFeatures, error) {
return NewKernelFeaturesWithProps("")
}
func NewKernelFeaturesWithProps(location string) (KernelFeatures, error) {
var kfeatures KernelFeatures
if location == "" {
bootConfigWithVersion := fmt.Sprintf("/boot/config-%s", defaultSysInfo.Machine)
kernelConfigLocations := []string{
"/proc/config.gz",
"/boot/config",
}
kernelConfigLocations = append(kernelConfigLocations, bootConfigWithVersion)
location = findKernelConfigs(kernelConfigLocations)
if location == "" {
kfeatures.Error = ErrNoConfigs.Error()
return kfeatures, ErrNoConfigs
}
} else {
if !fileExists(location) {
kfeatures.Error = os.ErrNotExist.Error()
return kfeatures, os.ErrNotExist
}
}
rawFeatures, err := readKernelFeatures(location)
if err != nil {
kfeatures.Error = err.Error()
return kfeatures, err
}
kfeatures.Raw = rawFeatures
return kfeatures, nil
}
var DefaultKernelFeatures, _ = NewKernelFeatures()
func findKernelConfigs(locations []string) string {
for _, loc := range locations {
if fileExists(loc) {
return loc
}
}
return ""
}
func fileExists(filePath string) bool {
fileInfo, err := os.Stat(filePath)
if err == nil && (fileInfo.Mode().IsRegular()) {
return true
}
return false
}
func readKernelFeatures(filename string) (map[string]string, error) {
freader, err := os.Open(filename)
if err != nil {
return nil, err
}
defer freader.Close()
areader, err := gzip.NewReader(freader)
if err != nil {
return nil, err
}
defer areader.Close()
var lines []string
scanner := bufio.NewScanner(areader)
kernelFeatures := map[string]string{}
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if line[0] == '#' {
//todo: extract section metadata from comments
continue
}
lineParts := strings.Split(line, "=")
if len(lineParts) == 2 {
flagKey := strings.TrimSpace(lineParts[0])
flagValue := strings.TrimSpace(lineParts[1])
flagValue = strings.Trim(flagValue, "\"")
lines = append(lines, line)
kernelFeatures[flagKey] = flagValue
}
}
if err = scanner.Err(); err != nil {
//if err == bufio.ErrTooLong {
// log.Println("line length error:", err)
//} else {
// log.Println("other scanner error:", err)
//}
return kernelFeatures, err
}
return kernelFeatures, nil
}