WriteSysctls sets the given sysctls to the requested values.
(sysctls map[string]string)
| 23 | |
| 24 | // WriteSysctls sets the given sysctls to the requested values. |
| 25 | func WriteSysctls(sysctls map[string]string) error { |
| 26 | // We are going to write multiple sysctls, which require writing to an |
| 27 | // unmasked procfs which is not going to be cached. To avoid creating a new |
| 28 | // procfs instance for each one, just allocate one handle for all of them. |
| 29 | proc, err := procfs.OpenUnsafeProcRoot() |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | defer proc.Close() |
| 34 | |
| 35 | for key, value := range sysctls { |
| 36 | keyPath := strings.ReplaceAll(key, ".", "/") |
| 37 | |
| 38 | sysctlFile, err := procfsOpenRoot(proc, "sys/"+keyPath, unix.O_WRONLY|unix.O_TRUNC|unix.O_CLOEXEC) |
| 39 | if err != nil { |
| 40 | return fmt.Errorf("open sysctl %s file: %w", key, err) |
| 41 | } |
| 42 | defer sysctlFile.Close() |
| 43 | |
| 44 | _, err = sysctlFile.WriteString(value) |
| 45 | if err != nil { |
| 46 | return fmt.Errorf("failed to write sysctl %s = %q: %w", key, value, err) |
| 47 | } |
| 48 | } |
| 49 | return nil |
| 50 | } |
no test coverage detected
searching dependent graphs…