Ejemplo n.º 1
0
// deviceFromPath takes the path to a device and it's cgroup_permissions(which cannot be easily queried) and looks up the information about a linux device.
func deviceFromPath(path, permissions string) (*specs.Device, *specs.DeviceCgroup, error) {
	fileInfo, err := os.Lstat(path)
	if err != nil {
		return nil, nil, err
	}
	var (
		devType                rune
		mode                   = fileInfo.Mode()
		fileModePermissionBits = os.FileMode.Perm(mode)
	)
	switch {
	case mode&os.ModeDevice == 0:
		return nil, nil, devices.ErrNotADevice
	case mode&os.ModeCharDevice != 0:
		fileModePermissionBits |= syscall.S_IFCHR
		devType = 'c'
	default:
		fileModePermissionBits |= syscall.S_IFBLK
		devType = 'b'
	}
	statt, ok := fileInfo.Sys().(*syscall.Stat_t)
	if !ok {
		return nil, nil, fmt.Errorf("cannot determine the device number for device %s", path)
	}
	devNumber := int(statt.Rdev)
	major := devices.Major(devNumber)
	minor := devices.Minor(devNumber)
	t := string(devType)
	dev := &specs.Device{
		Type:     t,
		Path:     path,
		Major:    major,
		Minor:    minor,
		FileMode: &fileModePermissionBits,
		UID:      &statt.Uid,
		GID:      &statt.Gid,
	}
	dc := &specs.DeviceCgroup{
		Allow:  true,
		Type:   &t,
		Major:  &major,
		Minor:  &minor,
		Access: &permissions,
	}
	return dev, dc, nil
}
Ejemplo n.º 2
0
// deviceFromPath takes the path to a device and it's cgroup_permissions(which cannot be easily queried) and looks up the information about a linux device.
func deviceFromPath(path, permissions string) (*specs.Device, error) {
	fileInfo, err := os.Lstat(path)
	if err != nil {
		return nil, err
	}
	var (
		devType                rune
		mode                   = fileInfo.Mode()
		fileModePermissionBits = os.FileMode.Perm(mode)
	)
	switch {
	case mode&os.ModeDevice == 0:
		return nil, devices.ErrNotADevice
	case mode&os.ModeCharDevice != 0:
		fileModePermissionBits |= syscall.S_IFCHR
		devType = 'c'
	default:
		fileModePermissionBits |= syscall.S_IFBLK
		devType = 'b'
	}
	statt, ok := fileInfo.Sys().(*syscall.Stat_t)
	if !ok {
		return nil, fmt.Errorf("cannot determine the device number for device %s", path)
	}
	devNumber := int(statt.Rdev)
	return &specs.Device{
		Type:        devType,
		Path:        path,
		Major:       devices.Major(devNumber),
		Minor:       devices.Minor(devNumber),
		Permissions: permissions,
		FileMode:    fileModePermissionBits,
		UID:         statt.Uid,
		GID:         statt.Gid,
	}, nil
}