-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Signed-off-by: Michael Schubert <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,9 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
|
@@ -257,17 +259,8 @@ func utsnameStr(in []int8) string { | |
return string(out) | ||
} | ||
|
||
func currentVersion() (int, error) { | ||
var buf syscall.Utsname | ||
if err := syscall.Uname(&buf); err != nil { | ||
return -1, err | ||
} | ||
|
||
releaseStr := strings.Trim(utsnameStr(buf.Release[:]), "\x00") | ||
|
||
kernelVersionStr := strings.Split(releaseStr, "-")[0] | ||
|
||
kernelVersionParts := strings.Split(kernelVersionStr, ".") | ||
func kernelVersionFromString(versionString string) (int, error) { | ||
kernelVersionParts := strings.Split(versionString, ".") | ||
if len(kernelVersionParts) != 3 { | ||
return -1, errors.New("not enough version information") | ||
} | ||
|
@@ -292,6 +285,63 @@ func currentVersion() (int, error) { | |
return out, nil | ||
} | ||
|
||
func currentVersionUname() (int, error) { | ||
var buf syscall.Utsname | ||
if err := syscall.Uname(&buf); err != nil { | ||
return -1, err | ||
} | ||
releaseStr := strings.Trim(utsnameStr(buf.Release[:]), "\x00") | ||
kernelVersionStr := strings.Split(releaseStr, "-")[0] | ||
return kernelVersionFromString(kernelVersionStr) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
func currentVersionUbuntu() (int, error) { | ||
procVersion, err := ioutil.ReadFile("/proc/version_signature") | ||
if err != nil { | ||
return -1, err | ||
} | ||
var u1, u2, releaseStr string | ||
_, err = fmt.Sscanf(string(procVersion), "%s %s %s", &u1, &u2, &releaseStr) | ||
This comment has been minimized.
Sorry, something went wrong.
alban
Member
|
||
if err != nil { | ||
return -1, err | ||
} | ||
kernelVersionStr := strings.Split(releaseStr, "-")[0] | ||
return kernelVersionFromString(kernelVersionStr) | ||
} | ||
|
||
var debianVersionRegex = regexp.MustCompile(`.* SMP Debian (\d+\.\d+.\d+-\d+) .*`) | ||
|
||
func currentVersionDebian() (int, error) { | ||
procVersion, err := ioutil.ReadFile("/proc/version") | ||
if err != nil { | ||
return -1, err | ||
} | ||
match := debianVersionRegex.FindStringSubmatch(string(procVersion)) | ||
if len(match) != 2 { | ||
return -1, fmt.Errorf("failed to get kernel version from /proc/version: %s", procVersion) | ||
} | ||
kernelVersionStr := strings.Split(match[1], "-")[0] | ||
return kernelVersionFromString(kernelVersionStr) | ||
} | ||
|
||
func currentVersion() (int, error) { | ||
This comment has been minimized.
Sorry, something went wrong.
alban
Member
|
||
onUbuntu, err := pathExists("/proc/version_signature") | ||
if err != nil { | ||
return -1, err | ||
} | ||
if onUbuntu { | ||
return currentVersionUbuntu() | ||
This comment has been minimized.
Sorry, something went wrong.
alban
Member
|
||
} | ||
onDebian, err := pathExists("/etc/debian_version") | ||
This comment has been minimized.
Sorry, something went wrong.
alban
Member
|
||
if err != nil { | ||
return -1, err | ||
} | ||
if onDebian { | ||
return currentVersionDebian() | ||
} | ||
return currentVersionUname() | ||
} | ||
|
||
func elfReadMaps(file *elf.File) (map[string]*Map, error) { | ||
maps := make(map[string]*Map) | ||
for sectionIdx, section := range file.Sections { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package elf | ||
|
||
import "os" | ||
|
||
func pathExists(path string) (bool, error) { | ||
This comment has been minimized.
Sorry, something went wrong.
alban
Member
|
||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true, nil | ||
} | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return true, err | ||
} |
kernelVersionFromString is always preceded by the Split on "-". Could this be factored inside
kernelVersionFromString
?Also does the
Split()
work when there is no dash or when the dash is added in an unconventional location?An example of missing dash / unconventional location is
4.8.6rkt-v1
. See rkt/rkt#3489 for details.Those cases are worth being tested in an unit test in elf_test.go. Or better, in a separate file kernversion.go and kernversion_test.go.