diff --git a/internal/feature.go b/internal/feature.go index 6399be085..6f64822e1 100644 --- a/internal/feature.go +++ b/internal/feature.go @@ -93,7 +93,7 @@ func NewFeatureTest(name string, fn FeatureTestFn, versions ...string) func() er break } - if runtime.GOOS == "linux" && !strings.ContainsRune(version, ':') { + if OnLinux && !strings.ContainsRune(version, ':') { // Allow version numbers without a GOOS prefix on Linux. ft.Version = version break diff --git a/internal/feature_test.go b/internal/feature_test.go index cd57eb464..346e8578f 100644 --- a/internal/feature_test.go +++ b/internal/feature_test.go @@ -83,7 +83,7 @@ func TestFeatureTestNotSupportedOnOS(t *testing.T) { qt.Assert(t, qt.IsNotNil(NewFeatureTest("foo", fn)())) qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "froz:1.0.0")(), ErrNotSupportedOnOS)) qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, runtime.GOOS+":1.0")(), sentinel)) - if runtime.GOOS == "linux" { + if OnLinux { qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "1.0")(), sentinel)) } } diff --git a/internal/goos.go b/internal/goos.go new file mode 100644 index 000000000..0569d5ce0 --- /dev/null +++ b/internal/goos.go @@ -0,0 +1,7 @@ +package internal + +import "runtime" + +const ( + OnLinux = runtime.GOOS == "linux" +) diff --git a/internal/kallsyms/kallsyms.go b/internal/kallsyms/kallsyms.go index 1f6392e8b..2cdfb56a5 100644 --- a/internal/kallsyms/kallsyms.go +++ b/internal/kallsyms/kallsyms.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "os" - "runtime" "slices" "strconv" "strings" @@ -50,7 +49,7 @@ func Module(name string) (string, error) { // Any symbols missing in the kernel are ignored. Returns an error if multiple // symbols with a given name were found. func AssignModules(symbols map[string]string) error { - if runtime.GOOS != "linux" { + if !internal.OnLinux { return fmt.Errorf("read /proc/kallsyms: %w", internal.ErrNotSupportedOnOS) } @@ -169,7 +168,7 @@ func Address(symbol string) (uint64, error) { // Any symbols missing in the kernel are ignored. Returns an error if multiple // addresses were found for a symbol. func AssignAddresses(symbols map[string]uint64) error { - if runtime.GOOS != "linux" { + if !internal.OnLinux { return fmt.Errorf("read /proc/kallsyms: %w", internal.ErrNotSupportedOnOS) } diff --git a/internal/kallsyms/kallsyms_test.go b/internal/kallsyms/kallsyms_test.go index 0ce7a4d85..9b111c34c 100644 --- a/internal/kallsyms/kallsyms_test.go +++ b/internal/kallsyms/kallsyms_test.go @@ -2,13 +2,12 @@ package kallsyms import ( "bytes" - "errors" "os" - "runtime" "testing" "github.com/go-quicktest/qt" + "github.com/cilium/ebpf/internal" "github.com/cilium/ebpf/internal/testutils" ) @@ -177,13 +176,11 @@ func BenchmarkAssignAddresses(b *testing.B) { func mustOpenProcKallsyms(tb testing.TB) *os.File { tb.Helper() - if runtime.GOOS != "linux" { + if !internal.OnLinux { tb.Skip("/proc/kallsyms is a Linux concept") } f, err := os.Open("/proc/kallsyms") - if runtime.GOOS != "linux" && errors.Is(err, os.ErrNotExist) { - } qt.Assert(tb, qt.IsNil(err)) tb.Cleanup(func() { f.Close() }) return f diff --git a/internal/linux/auxv.go b/internal/linux/auxv.go index 717ebf445..07d5efee0 100644 --- a/internal/linux/auxv.go +++ b/internal/linux/auxv.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io" - "runtime" _ "unsafe" "github.com/cilium/ebpf/internal" @@ -51,7 +50,7 @@ func (r *auxvRuntimeReader) ReadAuxvPair() (uint64, uint64, error) { } func newAuxvRuntimeReader() (auxvPairReader, error) { - if runtime.GOOS != "linux" { + if !internal.OnLinux { return nil, fmt.Errorf("read auxv from runtime: %w", internal.ErrNotSupportedOnOS) } diff --git a/internal/tracefs/kprobe.go b/internal/tracefs/kprobe.go index 720e105bc..bdadf4d8e 100644 --- a/internal/tracefs/kprobe.go +++ b/internal/tracefs/kprobe.go @@ -113,7 +113,7 @@ func sanitizeTracefsPath(path ...string) (string, error) { // but may be also be available at /sys/kernel/debug/tracing if debugfs is mounted. // The available tracefs paths will depends on distribution choices. var getTracefsPath = sync.OnceValues(func() (string, error) { - if runtime.GOOS != "linux" { + if !internal.OnLinux { return "", fmt.Errorf("tracefs: %w", internal.ErrNotSupportedOnOS) }