From 6ac2ab89242e7d5350a4053b2c972374b8c6c7f1 Mon Sep 17 00:00:00 2001 From: "isaac.rodman" Date: Tue, 26 Dec 2017 12:44:48 -0700 Subject: [PATCH] pkg/sysctl fixes: support commented KV lines, no post-crit KV set skips, support adding sysctl .conf files - Previously, KV lines which were commented would attempt to be set. Now any commented KV lines will also be ignored. - Comments can start with a hash or semicolon - Splitting KV on both period and forward slash - Some kernels may not have certain features enabled (such as IPv6) in the default etc/sysctl.d/*.conf, and thus pkg/sysctl would only set the KV until the first failure, and then silently skip the rest of the KVs. Now any failure is logged as a WARN, and those lines can now be commented per the above change, as they will be identified. Signed-off-by: Isaac Rodman --- pkg/sysctl/main.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pkg/sysctl/main.go b/pkg/sysctl/main.go index bcb2967b8..1f500bdc4 100644 --- a/pkg/sysctl/main.go +++ b/pkg/sysctl/main.go @@ -22,17 +22,19 @@ func init() { } func sysctl(line []byte) error { + sysctlLineTrimmed := strings.TrimSpace(string(line[:])) + // skip any commented lines + if len(sysctlLineTrimmed) >= 1 && (sysctlLineTrimmed[:1] == "#" || sysctlLineTrimmed[:1] == ";") { + return nil + } // parse line into a string of expected form X.Y.Z=VALUE - sysctlLineKV := strings.Split(string(line[:]), "=") + sysctlLineKV := strings.Split(sysctlLineTrimmed, "=") if len(sysctlLineKV) != 2 { - if len(sysctlLineKV) >= 1 && len(sysctlLineKV[0]) >= 1 && strings.Trim(sysctlLineKV[0], " ")[:1] == "#" { - return nil - } - return fmt.Errorf("Cannot parse %s", string(line)) + return fmt.Errorf("Cannot parse %s", sysctlLineTrimmed) } // trim any extra whitespace - sysctlSetting, sysctlValue := strings.Trim(sysctlLineKV[0], " "), strings.Trim(sysctlLineKV[1], " ") - sysctlFile := filepath.Join(sysctlDir, filepath.Join(strings.Split(sysctlSetting, ".")...)) + sysctlSetting, sysctlValue := strings.TrimSpace(sysctlLineKV[0]), strings.TrimSpace(sysctlLineKV[1]) + sysctlFile := filepath.Join(sysctlDir, filepath.Join(strings.FieldsFunc(sysctlSetting, splitKv)...)) file, err := os.OpenFile(sysctlFile, os.O_WRONLY, 0) if err != nil { return fmt.Errorf("Cannot open %s: %s", sysctlFile, err) @@ -45,6 +47,10 @@ func sysctl(line []byte) error { return nil } +func splitKv(r rune) bool { + return r == '.' || r == '/' +} + func main() { flag.Parse() @@ -65,7 +71,7 @@ func main() { } err = sysctl(line) if err != nil { - log.Fatal(err) + log.Println(fmt.Errorf("WARN: %v", err)) } } }