-
Notifications
You must be signed in to change notification settings - Fork 20
/
sysctl.go
47 lines (40 loc) · 1.01 KB
/
sysctl.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
package installer
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const sysctlDir = "/proc/sys"
func sysctlPath(name string) string {
return filepath.Join(sysctlDir, strings.Replace(name, ".", "/", -1))
}
// sysctlGet get the value of the named parameter from "/proc/sys".
//
// If this returns a non-nil error and os.IsNotExist returns true
// for the error, the parameter is not available on this system.
func sysctlGet(name string) (string, error) {
f, err := os.Open(sysctlPath(name))
if err != nil {
return "", err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
return string(data), nil
}
// sysctlSet the value of the named parameter.
//
// If this returns a non-nil error and os.IsNotExist returns true
// for the error, the parameter is not available on this system.
func sysctlSet(name, value string) error {
f, err := os.OpenFile(sysctlPath(name), os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(value)
return err
}