github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xsys/sys.go (about) 1 //+build linux 2 3 /* 4 Copyright 2015 The Kubernetes Authors. 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 http://www.apache.org/licenses/LICENSE-2.0 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 package xsys 16 17 import ( 18 "io/ioutil" 19 "path" 20 "strconv" 21 "strings" 22 ) 23 24 const ( 25 sysctlBase = "/proc/sys" 26 // VMOvercommitMemory refers to the sysctl variable responsible for defining 27 // the memory over-commit policy used by kernel. 28 VMOvercommitMemory = "vm/overcommit_memory" 29 // VMPanicOnOOM refers to the sysctl variable responsible for defining 30 // the OOM behavior used by kernel. 31 VMPanicOnOOM = "vm/panic_on_oom" 32 // KernelPanic refers to the sysctl variable responsible for defining 33 // the timeout after a panic for the kernel to reboot. 34 KernelPanic = "kernel/panic" 35 // KernelPanicOnOops refers to the sysctl variable responsible for defining 36 // the kernel behavior when an oops or BUG is encountered. 37 KernelPanicOnOops = "kernel/panic_on_oops" 38 // RootMaxKeys refers to the sysctl variable responsible for defining 39 // the maximum number of keys that the root user (UID 0 in the root user namespace) may own. 40 RootMaxKeys = "kernel/keys/root_maxkeys" 41 // RootMaxBytes refers to the sysctl variable responsible for defining 42 // the maximum number of bytes of data that the root user (UID 0 in the root user namespace) 43 // can hold in the payloads of the keys owned by root. 44 RootMaxBytes = "kernel/keys/root_maxbytes" 45 46 // VMOvercommitMemoryAlways represents that kernel performs no memory over-commit handling. 47 VMOvercommitMemoryAlways = 1 48 // VMPanicOnOOMInvokeOOMKiller represents that kernel calls the oom_killer function when OOM occurs. 49 VMPanicOnOOMInvokeOOMKiller = 0 50 51 // KernelPanicOnOopsAlways represents that kernel panics on kernel oops. 52 KernelPanicOnOopsAlways = 1 53 // KernelPanicRebootTimeout is the timeout seconds after a panic for the kernel to reboot. 54 KernelPanicRebootTimeout = 10 55 56 // RootMaxKeysSetting is the maximum number of keys that the root user (UID 0 in the root user namespace) may own. 57 // Needed since docker creates a new key per container. 58 RootMaxKeysSetting = 1000000 59 // RootMaxBytesSetting is the maximum number of bytes of data that the root user (UID 0 in the root user namespace) 60 // can hold in the payloads of the keys owned by root. 61 // Allocate 25 bytes per key * number of MaxKeys. 62 RootMaxBytesSetting = RootMaxKeysSetting * 25 63 ) 64 65 // Interface is an injectable interface for running sysctl commands. 66 type Interface interface { 67 // GetSysctl returns the value for the specified sysctl setting 68 GetSysctl(sysctl string) (int, error) 69 // SetSysctl modifies the specified sysctl flag to the new value 70 SetSysctl(sysctl string, newVal int) error 71 } 72 73 // New returns a new Interface for accessing sysctl 74 func New() Interface { 75 return &procSysctl{} 76 } 77 78 // procSysctl implements Interface by reading and writing files under /proc/sys 79 type procSysctl struct{} 80 81 // GetSysctl returns the value for the specified sysctl setting 82 func (*procSysctl) GetSysctl(sysctl string) (int, error) { 83 data, err := ioutil.ReadFile(path.Join(sysctlBase, sysctl)) 84 if err != nil { 85 return -1, err 86 } 87 val, err := strconv.Atoi(strings.Trim(string(data), " \n")) 88 if err != nil { 89 return -1, err 90 } 91 return val, nil 92 } 93 94 // SetSysctl modifies the specified sysctl flag to the new value 95 func (*procSysctl) SetSysctl(sysctl string, newVal int) error { 96 return ioutil.WriteFile(path.Join(sysctlBase, sysctl), []byte(strconv.Itoa(newVal)), 0640) 97 }