Copyright 2019 The Prometheus Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package procfs

import (
	
	
	
	
	

	
)
A MountInfo is a type that describes the details, options for each mount, parsed from /proc/self/mountinfo. The fields described in each entry of /proc/self/mountinfo is described in the following man page. http://man7.org/linux/man-pages/man5/proc.5.html
Unique Id for the mount
The Id of the parent mount
The value of `st_dev` for the files on this FS
The pathname of the directory in the FS that forms the root for this mount
The pathname of the mount point relative to the root
Mount options
Zero or more optional fields
The Filesystem type
FS specific information or "none"
Superblock options
Reads each line of the mountinfo file, and returns a list of formatted MountInfo structs.
func ( []byte) ([]*MountInfo, error) {
	 := []*MountInfo{}
	 := bufio.NewScanner(bytes.NewReader())
	for .Scan() {
		 := .Text()
		,  := parseMountInfoString()
		if  != nil {
			return nil, 
		}
		 = append(, )
	}

	 := .Err()
	return , 
}
Parses a mountinfo file line, and converts it to a MountInfo struct. An important check here is to see if the hyphen separator, as if it does not exist, it means that the line is malformed.
func ( string) (*MountInfo, error) {
	var  error

	 := strings.Split(, " ")
	 := len()
	if  < 11 {
		return nil, fmt.Errorf("couldn't find enough fields in mount string: %s", )
	}

	if [-4] != "-" {
		return nil, fmt.Errorf("couldn't find separator in expected field: %s", [-4])
	}

	 := &MountInfo{
		MajorMinorVer:  [2],
		Root:           [3],
		MountPoint:     [4],
		Options:        mountOptionsParser([5]),
		OptionalFields: nil,
		FSType:         [-3],
		Source:         [-2],
		SuperOptions:   mountOptionsParser([-1]),
	}

	.MountId,  = strconv.Atoi([0])
	if  != nil {
		return nil, fmt.Errorf("failed to parse mount ID")
	}
	.ParentId,  = strconv.Atoi([1])
	if  != nil {
		return nil, fmt.Errorf("failed to parse parent ID")
Has optional fields, which is a space separated list of values. Example: shared:2 master:7
	if [6] != "" {
		.OptionalFields,  = mountOptionsParseOptionalFields([6 : -4])
		if  != nil {
			return nil, 
		}
	}
	return , nil
}
mountOptionsIsValidField checks a string against a valid list of optional fields keys.
func ( string) bool {
	switch  {
	case
		"shared",
		"master",
		"propagate_from",
		"unbindable":
		return true
	}
	return false
}
mountOptionsParseOptionalFields parses a list of optional fields strings into a double map of strings.
func ( []string) (map[string]string, error) {
	 := make(map[string]string)
	for ,  := range  {
		 := strings.SplitN(, ":", 2)
		 := ""
		if len() == 2 {
			 = [1]
		}
		if mountOptionsIsValidField([0]) {
			[[0]] = 
		}
	}
	return , nil
}
Parses the mount options, superblock options.
func ( string) map[string]string {
	 := make(map[string]string)
	 := strings.Split(, ",")
	for ,  := range  {
		 := strings.Split(, "=")
		if len() < 2 {
			 := [0]
			[] = ""
		} else {
			,  := [0], [1]
			[] = 
		}
	}
	return 
}
Retrieves mountinfo information from `/proc/self/mountinfo`.
func () ([]*MountInfo, error) {
	,  := util.ReadFileNoStat("/proc/self/mountinfo")
	if  != nil {
		return nil, 
	}
	return parseMountInfo()
}
Retrieves mountinfo information from a processes' `/proc/<pid>/mountinfo`.
func ( int) ([]*MountInfo, error) {
	,  := util.ReadFileNoStat(fmt.Sprintf("/proc/%d/mountinfo", ))
	if  != nil {
		return nil, 
	}
	return parseMountInfo()