Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / opencontainers / runc / libcontainer / devices / devices_unix.go
1 // +build linux freebsd
2
3 package devices
4
5 import (
6         "errors"
7         "fmt"
8         "io/ioutil"
9         "os"
10         "path/filepath"
11         "syscall"
12
13         "github.com/opencontainers/runc/libcontainer/configs"
14 )
15
16 var (
17         ErrNotADevice = errors.New("not a device node")
18 )
19
20 // Testing dependencies
21 var (
22         osLstat       = os.Lstat
23         ioutilReadDir = ioutil.ReadDir
24 )
25
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)
29         if err != nil {
30                 return nil, err
31         }
32         var (
33                 devType                rune
34                 mode                   = fileInfo.Mode()
35                 fileModePermissionBits = os.FileMode.Perm(mode)
36         )
37         switch {
38         case mode&os.ModeDevice == 0:
39                 return nil, ErrNotADevice
40         case mode&os.ModeCharDevice != 0:
41                 fileModePermissionBits |= syscall.S_IFCHR
42                 devType = 'c'
43         default:
44                 fileModePermissionBits |= syscall.S_IFBLK
45                 devType = 'b'
46         }
47         stat_t, ok := fileInfo.Sys().(*syscall.Stat_t)
48         if !ok {
49                 return nil, fmt.Errorf("cannot determine the device number for device %s", path)
50         }
51         devNumber := int(stat_t.Rdev)
52         return &configs.Device{
53                 Type:        devType,
54                 Path:        path,
55                 Major:       Major(devNumber),
56                 Minor:       Minor(devNumber),
57                 Permissions: permissions,
58                 FileMode:    fileModePermissionBits,
59                 Uid:         stat_t.Uid,
60                 Gid:         stat_t.Gid,
61         }, nil
62 }
63
64 func HostDevices() ([]*configs.Device, error) {
65         return getDevices("/dev")
66 }
67
68 func getDevices(path string) ([]*configs.Device, error) {
69         files, err := ioutilReadDir(path)
70         if err != nil {
71                 return nil, err
72         }
73         out := []*configs.Device{}
74         for _, f := range files {
75                 switch {
76                 case f.IsDir():
77                         switch f.Name() {
78                         // ".lxc" & ".lxd-mounts" added to address https://github.com/lxc/lxd/issues/2825
79                         case "pts", "shm", "fd", "mqueue", ".lxc", ".lxd-mounts":
80                                 continue
81                         default:
82                                 sub, err := getDevices(filepath.Join(path, f.Name()))
83                                 if err != nil {
84                                         return nil, err
85                                 }
86
87                                 out = append(out, sub...)
88                                 continue
89                         }
90                 case f.Name() == "console":
91                         continue
92                 }
93                 device, err := DeviceFromPath(filepath.Join(path, f.Name()), "rwm")
94                 if err != nil {
95                         if err == ErrNotADevice {
96                                 continue
97                         }
98                         if os.IsNotExist(err) {
99                                 continue
100                         }
101                         return nil, err
102                 }
103                 out = append(out, device)
104         }
105         return out, nil
106 }