func SubsystemMountpoints()

in cgroup/util.go [173:226]


func SubsystemMountpoints(rootfsMountpoint string, subsystems map[string]struct{}) (map[string]string, error) {
	if rootfsMountpoint == "" {
		rootfsMountpoint = "/"
	}

	mountinfo, err := os.Open(filepath.Join(rootfsMountpoint, "proc", "self", "mountinfo"))
	if err != nil {
		return nil, err
	}
	defer mountinfo.Close()

	mounts := map[string]string{}
	sc := bufio.NewScanner(mountinfo)
	for sc.Scan() {
		// https://www.kernel.org/doc/Documentation/filesystems/proc.txt
		// Example:
		// 25 21 0:20 / /cgroup/cpu rw,relatime - cgroup cgroup rw,cpu
		line := strings.TrimSpace(sc.Text())
		if line == "" {
			continue
		}

		mount, err := parseMountinfoLine(line)
		if err != nil {
			return nil, err
		}

		if mount.filesystemType != "cgroup" {
			continue
		}

		if !strings.HasPrefix(mount.mountpoint, rootfsMountpoint) {
			continue
		}

		for _, opt := range mount.superOptions {
			// Sometimes the subsystem name is written like "name=blkio".
			fields := strings.SplitN(opt, "=", 2)
			if len(fields) > 1 {
				opt = fields[1]
			}

			// Test if option is a subsystem name.
			if _, found := subsystems[opt]; found {
				// Add the subsystem mount if it does not already exist.
				if _, exists := mounts[opt]; !exists {
					mounts[opt] = mount.mountpoint
				}
			}
		}
	}

	return mounts, sc.Err()
}