diff --git a/pkg/ebpf_internal/version.go b/pkg/ebpf_internal/version.go index 4de2444..bd59dbc 100644 --- a/pkg/ebpf_internal/version.go +++ b/pkg/ebpf_internal/version.go @@ -1,10 +1,11 @@ -// Copied from https://github.com/cilium/ebpf/blob/v0.10.0/internal/version.go +// Modified from https://github.com/cilium/ebpf/blob/v0.10.0/internal/version.go package internal import ( "fmt" "sync" + "syscall" "github.com/v2rayA/dae/pkg/ebpf_internal/internal/unix" ) @@ -89,10 +90,34 @@ func (v Version) Kernel() uint32 { return uint32(uint8(v[0]))<<16 | uint32(uint8(v[1]))<<8 | uint32(uint8(s)) } +// utsnameToString converts the utsname to a string and returns it. +func utsnameToString(unameArray [65]int8) string { + var byteString [65]byte + var indexLength int + for ; unameArray[indexLength] != 0; indexLength++ { + byteString[indexLength] = uint8(unameArray[indexLength]) + } + return string(byteString[:indexLength]) +} + // KernelVersion returns the version of the currently running kernel. func KernelVersion() (Version, error) { kernelVersion.once.Do(func() { kernelVersion.version, kernelVersion.err = detectKernelVersion() + if kernelVersion.err != nil { + // Try syscall. + var utsname syscall.Utsname + if err := syscall.Uname(&utsname); err != nil { + kernelVersion.err = fmt.Errorf("%w; %v", kernelVersion.err, err) + return + } + version, err := NewVersion(utsnameToString(utsname.Release)) + if err != nil { + kernelVersion.err = fmt.Errorf("%w; %v", kernelVersion.err, err) + return + } + kernelVersion.version = version + } }) if kernelVersion.err != nil {