| 21 | } |
| 22 | |
| 23 | func 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 | |
| 49 | func splitKv(r rune) bool { |
| 50 | return r == '.' || r == '/' |