Skip to content

Commit

Permalink
Merge pull request #145 from angelabriel/master
Browse files Browse the repository at this point in the history
Read 1024 Byte from /proc/sys files instead of 512
  • Loading branch information
angelabriel authored Aug 27, 2024
2 parents f11496e + 9b4dca9 commit 36e2cd2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 3 additions & 3 deletions sap/note/ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (vend INISettings) Apply() error {
continue
}

if revertValues && vend.SysctlParams[param.Key] != "" {
if revertValues && vend.SysctlParams[param.Key] != "PNA" {
// revert parameter value
pvendID, flstates = vend.setRevertParamValues(param.Key)
}
Expand Down Expand Up @@ -416,7 +416,7 @@ func (vend INISettings) setRevertParamValues(key string) (string, string) {
func (vend INISettings) createParamSavedStates(key, flstates string) {
// do not write parameter values to the saved state file during
// a pure 'verify' action
if _, ok := vend.ValuesToApply["verify"]; !ok && vend.SysctlParams[key] != "" {
if _, ok := vend.ValuesToApply["verify"]; !ok && vend.SysctlParams[key] != "PNA" {
start := vend.SysctlParams[key]
if key == "UserTasksMax" {
if system.SystemctlIsStarting() {
Expand All @@ -437,7 +437,7 @@ func (vend INISettings) createParamSavedStates(key, flstates string) {
func (vend INISettings) addParamSavedStates(key string) {
// do not write parameter values to the saved state file during
// a pure 'verify' action
if _, ok := vend.ValuesToApply["verify"]; !ok && vend.SysctlParams[key] != "" {
if _, ok := vend.ValuesToApply["verify"]; !ok && vend.SysctlParams[key] != "PNA" {
AddParameterNoteValues(key, vend.SysctlParams[key], vend.ID)
}
}
Expand Down
18 changes: 17 additions & 1 deletion system/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package system
// Manipulate /sys/ switches.

import (
"bytes"
"os"
"path"
"regexp"
Expand All @@ -17,11 +18,26 @@ func GetSysString(parameter string) (string, error) {

// getKeyStringFromPath generalizes the extraction of a string from path
func getKeyStringFromPath(basePath string, parameter string, logFrom string) (string, error) {
val, err := os.ReadFile(path.Join(basePath, strings.Replace(parameter, ".", "/", -1)))
// Seams that os.ReadFile reads only 512 Bytes, if it can not detect the
// filesize (which is the case for /proc/sys files, returns always 0)
// This might not enough for sysctl parameter like
// 'net.ipv4.ip_local_reserved_ports'
srcFile := path.Join(basePath, strings.Replace(parameter, ".", "/", -1))
file, err := os.Open(srcFile)
if err != nil {
WarningLog("failed to open %s : %v", srcFile, err)
return "PNA", err
}
defer file.Close()
val := make([]byte, 1024)
bytesread, err := file.Read(val)
DebugLog("getKeyStringFromPath - bytes read from '%s': '%+v'\n", srcFile, bytesread)
if err != nil {
WarningLog("failed to read %v string key '%s': %v", logFrom, parameter, err)
return "PNA", err
}
// remove NULL
val = bytes.Trim(val, "\x00")
return strings.TrimSpace(string(val)), nil
}

Expand Down
3 changes: 3 additions & 0 deletions system/sysctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ func SetSysctlString(parameter, value string) error {
WarningLog("value is '%s', so sysctl key '%s' is/was not supported by os, skipping.", value, parameter)
return nil
}
if value == "" {
value = "\n"
}
err := os.WriteFile(path.Join("/proc/sys", strings.Replace(parameter, ".", "/", -1)), []byte(value), 0644)
if os.IsNotExist(err) {
WarningLog("sysctl key '%s' is not supported by os, skipping.", parameter)
Expand Down

0 comments on commit 36e2cd2

Please sign in to comment.