Download this file
| 1 | //go:build linux |
| 2 | |
| 3 | package host |
| 4 | |
| 5 | import ( |
| 6 | "github.com/carlosframework/platform/internal/hostbox" |
| 7 | "golang.org/x/sys/unix" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | var claimContainmentAgentRoot = agentUnitCgroup |
| 13 | var claimContainmentInspect = inspectClaimContainment |
| 14 | |
| 15 | func inspectClaimContainment(relative string) (hostbox.LaunchGroup, error) { |
| 16 | if !filepath.IsLocal(relative) || filepath.Clean(relative) != relative || relative == "." { |
| 17 | return hostbox.LaunchGroup{}, ErrClaimStopInventory |
| 18 | } |
| 19 | fd, err := unix.Open("/sys/fs/cgroup", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0) |
| 20 | if err != nil { |
| 21 | return hostbox.LaunchGroup{}, err |
| 22 | } |
| 23 | for _, part := range strings.Split(relative, "/") { |
| 24 | next, e := unix.Openat(fd, part, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0) |
| 25 | unix.Close(fd) |
| 26 | if e != nil { |
| 27 | return hostbox.LaunchGroup{}, e |
| 28 | } |
| 29 | fd = next |
| 30 | } |
| 31 | defer unix.Close(fd) |
| 32 | var st unix.Stat_t |
| 33 | if err = unix.Fstat(fd, &st); err != nil { |
| 34 | return hostbox.LaunchGroup{}, err |
| 35 | } |
| 36 | return hostbox.LaunchGroup{RelativePath: relative, Device: uint64(st.Dev), Inode: st.Ino}, nil |
| 37 | } |
| 38 | |