MCPcopy Index your code
hub / github.com/linuxkit/linuxkit / sysctl

Function sysctl

pkg/sysctl/main.go:23–47  ·  view source on GitHub ↗
(line []byte)

Source from the content-addressed store, hash-verified

21}
22
23func sysctl(line []byte) error {
24 sysctlLineTrimmed := strings.TrimSpace(string(line[:]))
25 // skip any commented lines
26 if len(sysctlLineTrimmed) >= 1 && (sysctlLineTrimmed[:1] == "#" || sysctlLineTrimmed[:1] == ";") {
27 return nil
28 }
29 // parse line into a string of expected form X.Y.Z=VALUE
30 sysctlLineKV := strings.Split(sysctlLineTrimmed, "=")
31 if len(sysctlLineKV) != 2 {
32 return fmt.Errorf("Cannot parse %s", sysctlLineTrimmed)
33 }
34 // trim any extra whitespace
35 sysctlSetting, sysctlValue := strings.TrimSpace(sysctlLineKV[0]), strings.TrimSpace(sysctlLineKV[1])
36 sysctlFile := filepath.Join(sysctlDir, filepath.Join(strings.FieldsFunc(sysctlSetting, splitKv)...))
37 file, err := os.OpenFile(sysctlFile, os.O_WRONLY, 0)
38 if err != nil {
39 return fmt.Errorf("Cannot open %s: %s", sysctlFile, err)
40 }
41 defer file.Close()
42 _, err = file.Write([]byte(sysctlValue))
43 if err != nil {
44 return fmt.Errorf("Cannot write to %s: %s", sysctlFile, err)
45 }
46 return nil
47}
48
49func splitKv(r rune) bool {
50 return r == '.' || r == '/'

Callers 1

mainFunction · 0.85

Calls 2

CloseMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected