github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/util/utils_linux.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"syscall"
     8  
     9  	"github.com/containers/psgo"
    10  	"github.com/pkg/errors"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  // GetContainerPidInformationDescriptors returns a string slice of all supported
    15  // format descriptors of GetContainerPidInformation.
    16  func GetContainerPidInformationDescriptors() ([]string, error) {
    17  	return psgo.ListDescriptors(), nil
    18  }
    19  
    20  // FindDeviceNodes parses /dev/ into a set of major:minor -> path, where
    21  // [major:minor] is the device's major and minor numbers formatted as, for
    22  // example, 2:0 and path is the path to the device node.
    23  // Symlinks to nodes are ignored.
    24  func FindDeviceNodes() (map[string]string, error) {
    25  	nodes := make(map[string]string)
    26  	err := filepath.Walk("/dev", func(path string, info os.FileInfo, err error) error {
    27  		if err != nil {
    28  			logrus.Warnf("Error descending into path %s: %v", path, err)
    29  			return filepath.SkipDir
    30  		}
    31  
    32  		// If we aren't a device node, do nothing.
    33  		if info.Mode()&(os.ModeDevice|os.ModeCharDevice) == 0 {
    34  			return nil
    35  		}
    36  
    37  		// We are a device node. Get major/minor.
    38  		sysstat, ok := info.Sys().(*syscall.Stat_t)
    39  		if !ok {
    40  			return errors.Errorf("Could not convert stat output for use")
    41  		}
    42  		major := sysstat.Rdev / 256
    43  		minor := sysstat.Rdev % 256
    44  
    45  		nodes[fmt.Sprintf("%d:%d", major, minor)] = path
    46  
    47  		return nil
    48  	})
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	return nodes, nil
    54  }