1 // +build linux freebsd
13 "github.com/opencontainers/runc/libcontainer/configs"
17 ErrNotADevice = errors.New("not a device node")
20 // Testing dependencies
23 ioutilReadDir = ioutil.ReadDir
26 // Given the path to a device and its cgroup_permissions(which cannot be easily queried) look up the information about a linux device and return that information as a Device struct.
27 func DeviceFromPath(path, permissions string) (*configs.Device, error) {
28 fileInfo, err := osLstat(path)
34 mode = fileInfo.Mode()
35 fileModePermissionBits = os.FileMode.Perm(mode)
38 case mode&os.ModeDevice == 0:
39 return nil, ErrNotADevice
40 case mode&os.ModeCharDevice != 0:
41 fileModePermissionBits |= syscall.S_IFCHR
44 fileModePermissionBits |= syscall.S_IFBLK
47 stat_t, ok := fileInfo.Sys().(*syscall.Stat_t)
49 return nil, fmt.Errorf("cannot determine the device number for device %s", path)
51 devNumber := int(stat_t.Rdev)
52 return &configs.Device{
55 Major: Major(devNumber),
56 Minor: Minor(devNumber),
57 Permissions: permissions,
58 FileMode: fileModePermissionBits,
64 func HostDevices() ([]*configs.Device, error) {
65 return getDevices("/dev")
68 func getDevices(path string) ([]*configs.Device, error) {
69 files, err := ioutilReadDir(path)
73 out := []*configs.Device{}
74 for _, f := range files {
78 // ".lxc" & ".lxd-mounts" added to address https://github.com/lxc/lxd/issues/2825
79 case "pts", "shm", "fd", "mqueue", ".lxc", ".lxd-mounts":
82 sub, err := getDevices(filepath.Join(path, f.Name()))
87 out = append(out, sub...)
90 case f.Name() == "console":
93 device, err := DeviceFromPath(filepath.Join(path, f.Name()), "rwm")
95 if err == ErrNotADevice {
98 if os.IsNotExist(err) {
103 out = append(out, device)