diff --git a/pkg/cgroups/cgroupid.go b/pkg/cgroups/cgroupid.go index 56737853a..8413c17f6 100644 --- a/pkg/cgroups/cgroupid.go +++ b/pkg/cgroups/cgroupid.go @@ -1,13 +1,10 @@ package cgroups import ( - "encoding/binary" "fmt" "os" "path/filepath" "sync" - - "golang.org/x/sys/unix" ) // CgroupID implements mapping kernel cgroup IDs to cgroupfs paths with transparent caching. @@ -25,15 +22,6 @@ func NewCgroupID(root string) *CgroupID { } } -func getID(path string) uint64 { - h, _, err := unix.NameToHandleAt(unix.AT_FDCWD, path, 0) - if err != nil { - return 0 - } - - return binary.LittleEndian.Uint64(h.Bytes()) -} - // Find finds the path for the given cgroup id. func (cgid *CgroupID) Find(id uint64) (string, error) { found := false diff --git a/pkg/cgroups/cgroupid_linux.go b/pkg/cgroups/cgroupid_linux.go new file mode 100644 index 000000000..a68db1daf --- /dev/null +++ b/pkg/cgroups/cgroupid_linux.go @@ -0,0 +1,16 @@ +package cgroups + +import ( + "encoding/binary" + + "golang.org/x/sys/unix" +) + +func getID(path string) uint64 { + h, _, err := unix.NameToHandleAt(unix.AT_FDCWD, path, 0) + if err != nil { + return 0 + } + + return binary.LittleEndian.Uint64(h.Bytes()) +} diff --git a/pkg/cgroups/cgroupid_other.go b/pkg/cgroups/cgroupid_other.go new file mode 100644 index 000000000..4c969c780 --- /dev/null +++ b/pkg/cgroups/cgroupid_other.go @@ -0,0 +1,8 @@ +//go:build !linux +// +build !linux + +package cgroups + +func getID(path string) uint64 { + panic("not implemented") +} diff --git a/pkg/topology/topology.go b/pkg/topology/topology.go index 84463378f..6eeb116f4 100644 --- a/pkg/topology/topology.go +++ b/pkg/topology/topology.go @@ -19,9 +19,6 @@ import ( "os" "path/filepath" "strings" - "syscall" - - "golang.org/x/sys/unix" ) // to mock in tests @@ -249,43 +246,6 @@ func (h *Hint) String() string { return "" } -// FindSysFsDevice for given argument returns physical device where it is linked to. -// For device nodes it will return path for device itself. For regular files or directories -// this function returns physical device where this inode resides (storage device). -// If result device is a virtual one (e.g. tmpfs), error will be returned. -// For non-existing path, no error returned and path is empty. -func FindSysFsDevice(dev string) (string, error) { - fi, err := os.Stat(dev) - if err != nil { - if os.IsNotExist(err) { - return "", nil - } - return "", fmt.Errorf("unable to get stat for %s: %w", dev, err) - } - - devType := "block" - rdev := fi.Sys().(*syscall.Stat_t).Dev - if mode := fi.Mode(); mode&os.ModeDevice != 0 { - rdev = fi.Sys().(*syscall.Stat_t).Rdev - if mode&os.ModeCharDevice != 0 { - devType = "char" - } - } - - major := int64(unix.Major(rdev)) - minor := int64(unix.Minor(rdev)) - if major == 0 { - return "", fmt.Errorf("%s is a virtual device node: %w", dev, err) - } - - realDevPath, err := findSysFsDevice(devType, major, minor) - if err != nil { - return "", fmt.Errorf("failed to find sysfs device for %s: %w", dev, err) - } - - return realDevPath, nil -} - // FindGivenSysFsDevice returns the physical device with the given device type, // major, and minor numbers. func FindGivenSysFsDevice(devType string, major, minor int64) (string, error) { diff --git a/pkg/topology/topology_linux.go b/pkg/topology/topology_linux.go new file mode 100644 index 000000000..0770dc851 --- /dev/null +++ b/pkg/topology/topology_linux.go @@ -0,0 +1,46 @@ +package topology + +import ( + "fmt" + "os" + "syscall" + + "golang.org/x/sys/unix" +) + +// FindSysFsDevice for given argument returns physical device where it is linked to. +// For device nodes it will return path for device itself. For regular files or directories +// this function returns physical device where this inode resides (storage device). +// If result device is a virtual one (e.g. tmpfs), error will be returned. +// For non-existing path, no error returned and path is empty. +func FindSysFsDevice(dev string) (string, error) { + fi, err := os.Stat(dev) + if err != nil { + if os.IsNotExist(err) { + return "", nil + } + return "", fmt.Errorf("unable to get stat for %s: %w", dev, err) + } + + devType := "block" + rdev := fi.Sys().(*syscall.Stat_t).Dev + if mode := fi.Mode(); mode&os.ModeDevice != 0 { + rdev = fi.Sys().(*syscall.Stat_t).Rdev + if mode&os.ModeCharDevice != 0 { + devType = "char" + } + } + + major := int64(unix.Major(rdev)) + minor := int64(unix.Minor(rdev)) + if major == 0 { + return "", fmt.Errorf("%s is a virtual device node: %w", dev, err) + } + + realDevPath, err := findSysFsDevice(devType, major, minor) + if err != nil { + return "", fmt.Errorf("failed to find sysfs device for %s: %w", dev, err) + } + + return realDevPath, nil +} diff --git a/pkg/topology/topology_other.go b/pkg/topology/topology_other.go new file mode 100644 index 000000000..6b98be3fa --- /dev/null +++ b/pkg/topology/topology_other.go @@ -0,0 +1,15 @@ +//go:build !linux +// +build !linux + +package topology + +import "errors" + +// FindSysFsDevice for given argument returns physical device where it is linked to. +// For device nodes it will return path for device itself. For regular files or directories +// this function returns physical device where this inode resides (storage device). +// If result device is a virtual one (e.g. tmpfs), error will be returned. +// For non-existing path, no error returned and path is empty. +func FindSysFsDevice(dev string) (string, error) { + return "", errors.New("not implemented") +}