-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #502 from essentialkaos/develop
Version 13.4.0
- Loading branch information
Showing
16 changed files
with
360 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package sysctl | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2024 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func ExampleGet() { | ||
paramValue, err := Get("kernel.random.boot_id") | ||
|
||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
fmt.Printf("Boot ID: %s\n", paramValue) | ||
} | ||
|
||
func ExampleGetI() { | ||
paramValue, err := GetI("kernel.pty.max") | ||
|
||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
fmt.Printf("PTY Max: %d\n", paramValue) | ||
} | ||
|
||
func ExampleGetI64() { | ||
paramValue, err := GetI64("fs.file-max") | ||
|
||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
fmt.Printf("File max: %d\n", paramValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//go:build linux || darwin | ||
// +build linux darwin | ||
|
||
// Package sysctl provides methods for reading kernel parameters | ||
package sysctl | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2024 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// binary is sysctl binary name | ||
var binary = "sysctl" | ||
|
||
// procFS is path to procfs | ||
var procFS = "/proc/sys" | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// Get returns kernel parameter value as a string | ||
func Get(param string) (string, error) { | ||
switch { | ||
case param == "": | ||
return "", fmt.Errorf("Kernel parameter name cannot be empty") | ||
case !strings.Contains(param, ".") || strings.ContainsAny(param, " /\n\t"): | ||
return "", fmt.Errorf("Invalid parameter name %q", param) | ||
} | ||
|
||
return getKernelParam(param) | ||
} | ||
|
||
// GetI returns kernel parameter value as an int | ||
func GetI(param string) (int, error) { | ||
p, err := Get(param) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
i, err := strconv.Atoi(p) | ||
|
||
if err != nil { | ||
return 0, fmt.Errorf("Can't parse %q parameter as int: %w", param, err) | ||
} | ||
|
||
return i, nil | ||
} | ||
|
||
// GetI64 returns kernel parameter value as an int64 | ||
func GetI64(param string) (int64, error) { | ||
p, err := Get(param) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
i, err := strconv.ParseInt(p, 10, 64) | ||
|
||
if err != nil { | ||
return 0, fmt.Errorf("Can't parse %q parameter as int64: %w", param, err) | ||
} | ||
|
||
return i, nil | ||
} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sysctl | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2024 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// getKernelParam reads kernel parameter from procfs | ||
func getKernelParam(param string) (string, error) { | ||
output, err := exec.Command(binary, "-n", param).Output() | ||
return strings.Trim(string(output), "\n\r"), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package sysctl | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2024 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/essentialkaos/ek/v13/path" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// getKernelParam reads kernel parameter from procfs | ||
func getKernelParam(param string) (string, error) { | ||
p, err := os.ReadFile(path.Clean(path.Join( | ||
procFS, strings.ReplaceAll(param, ".", "/"), | ||
))) | ||
|
||
if err != nil { | ||
return "", fmt.Errorf("Can't read parameter %q: %w", param, err) | ||
} | ||
|
||
return strings.Trim(string(p), "\n\r"), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//go:build !linux && !darwin | ||
// +build !linux,!darwin | ||
|
||
// Package sysctl provides methods for reading kernel parameters | ||
package sysctl | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2024 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// Get returns kernel parameter value as a string | ||
func Get(param string) (string, error) { | ||
return "", nil | ||
} | ||
|
||
// GetI returns kernel parameter value as an int | ||
func GetI(param string) (int, error) { | ||
return 0, nil | ||
} | ||
|
||
// GetI64 returns kernel parameter value as an int64 | ||
func GetI64(param string) (int64, error) { | ||
return 0, nil | ||
} |
Oops, something went wrong.