Skip to content

Commit

Permalink
runtimetest: add linux devices validation
Browse files Browse the repository at this point in the history
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
  • Loading branch information
Ma Shimiao committed Sep 21, 2016
1 parent 089d971 commit f0ae1dc
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions cmd/runtimetest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,65 @@ func validateDefaultFS(spec *rspec.Spec) error {
return nil
}

func validateLinuxDevices(spec *rspec.Spec) error {
logrus.Debugf("validating linux devices")

for _, device := range spec.Linux.Devices {
fi, err := os.Stat(device.Path)
if err != nil {
return err
}
fStat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("cannot determine state for device %s", device.Path)
}
var devType string
switch fStat.Mode & syscall.S_IFMT {
case syscall.S_IFCHR:
devType = "c"
break
case syscall.S_IFBLK:
devType = "b"
break
case syscall.S_IFIFO:
devType = "p"
break
default:
devType = "unmatched"
}
if devType != device.Type || (devType == "c" && device.Type == "u") {
return fmt.Errorf("device %v expected type is %v, actual is %v", device.Path, device.Type, devType)
}
if devType != "p" {
dev := fStat.Rdev
major := (dev >> 8) & 0xfff
minor := (dev & 0xff) | ((dev >> 12) & 0xfff00)
if int64(major) != device.Major || int64(minor) != device.Minor {
return fmt.Errorf("%v device number expected is %v:%v, actual is %v:%v", device.Path, device.Major, device.Minor, major, minor)
}
}
if device.FileMode != nil {
expected_perm := *device.FileMode & os.ModePerm
actual_perm := fi.Mode() & os.ModePerm
if expected_perm != actual_perm {
return fmt.Errorf("%v filemode expected is %v, actual is %v", device.Path, expected_perm, actual_perm)
}
}
if device.UID != nil {
if *device.UID != fStat.Uid {
return fmt.Errorf("%v uid expected is %v, actual is %v", device.Path, *device.UID, fStat.Uid)
}
}
if device.GID != nil {
if *device.GID != fStat.Gid {
return fmt.Errorf("%v uid expected is %v, actual is %v", device.Path, *device.GID, fStat.Gid)
}
}
}

return nil
}

func validateDefaultDevices(spec *rspec.Spec) error {
logrus.Debugf("validating linux default devices")

Expand Down Expand Up @@ -395,6 +454,7 @@ func validate(context *cli.Context) error {
linuxValidations := []validation{
validateDefaultFS,
validateDefaultDevices,
validateLinuxDevices,
validateSysctls,
validateMaskedPaths,
validateROPaths,
Expand Down

0 comments on commit f0ae1dc

Please sign in to comment.