1package internal
2
3import (
4 "bytes"
5 "fmt"
6 "strings"
7 "syscall"
8)
9
10func cleanNulls(s string) string {
11 return strings.TrimFunc(s, func(r rune) bool {
12 return r == 0
13 })
14}
15
16// Gets the name of the kernel using uname.
17// It returns the given kernel name and any errors it encounters.
18func GetKernelName(uname syscall.Utsname) (string, error) {
19 var b bytes.Buffer
20 for _, v := range uname.Sysname {
21 if err := b.WriteByte(byte(v)); err != nil {
22 return "", fmt.Errorf("failed to write kernel name to buffer: %v", err)
23 }
24 }
25 return cleanNulls(b.String()), nil
26}
27
28// Gets the version of the kernel using uname.
29// It returns the given kernel version and any errors it encounters.
30func GetKernelRelease(uname syscall.Utsname) (string, error) {
31 var b bytes.Buffer
32 for _, v := range uname.Release {
33 if err := b.WriteByte(byte(v)); err != nil {
34 return "", fmt.Errorf("failed to write kernel release to buffer: %v", err)
35 }
36 }
37 return cleanNulls(b.String()), nil
38}
39
40// Gets the machine's architecture using uname.
41// It returns the given architecture and any errors it encounters.
42func GetMachineArch(uname syscall.Utsname) (string, error) {
43 var b bytes.Buffer
44 for _, v := range uname.Machine {
45 if err := b.WriteByte(byte(v)); err != nil {
46 return "", fmt.Errorf("failed to write machine to buffer: %v", err)
47 }
48 }
49 return cleanNulls(b.String()), nil
50}
51
52// Gets the machine's name using uname.
53// It returns the given machine's name and any errors it encounters.
54func GetNodename(uname syscall.Utsname) (string, error) {
55 var b bytes.Buffer
56 for _, v := range uname.Nodename {
57 if err := b.WriteByte(byte(v)); err != nil {
58 return "", fmt.Errorf("failed to write node name to buffer: %v", err)
59 }
60 }
61 return cleanNulls(b.String()), nil
62}