Repository: opencontainers/runc
File: libcontainer/configs/validate/validator.go (Go)

Code before:
		}
		if a[i].Size != b[i].Size {
			return false
		}
	}
	return true
}

func isHostNetNS(path string) (bool, error) {
	const currentProcessNetns = "/proc/self/ns/net"

	var st1, st2 unix.Stat_t

	if err := unix.Stat(currentProcessNetns, &st1); err != nil {
		return false, &os.PathError{Op: "stat", Path: currentProcessNetns, Err: err}
	}
	if err := unix.Stat(path, &st2); err != nil {
		return false, &os.PathError{Op: "stat", Path: path, Err: err}
	}

	return (st1.Dev == st2.Dev) && (st1.Ino == st2.Ino), nil
}

// scheduler is to validate scheduler configs according to https://man7.org/linux/man-pages/man2/sched_setattr.2.html
func scheduler(config *configs.Config) error {
	if config.Scheduler == nil {
		return nil
	}
	niceValue := config.Scheduler.Nice
	if niceValue < -20 || niceValue > 19 {
		return fmt.Errorf("invalid scheduler.nice: %d", niceValue)
	}
	if config.Scheduler.Policy != specs.SchedFIFO && config.Scheduler.Policy != specs.SchedRR {
		if config.Scheduler.Priority != 0 {
			return fmt.Errorf("invalid scheduler.priority: %d", config.Scheduler.Priority)
		}
	}
	if config.Scheduler.Policy != specs.SchedDeadline {
		if config.Scheduler.Runtime != 0 {
			return fmt.Errorf("invalid scheduler.runtime: %d", config.Scheduler.Runtime)
		}
		if config.Scheduler.Deadline != 0 {
			return fmt.Errorf("invalid scheduler.deadline: %d", config.Scheduler.Deadline)
		}
		if config.Scheduler.Period != 0 {
			return fmt.Errorf("invalid scheduler.period: %d", config.Scheduler.Period)
		}
	}
	return nil
}


Review comment (refactor):
I think we can simplify the code by adding something like
```go
s := config.Scheduler
```

and then using `s` everywhere, for example:
```diff
-niceValue := config.Scheduler.Nice
-if niceValue < -20 || niceValue > 19 {
-	return fmt.Errorf("invalid scheduler.nice: %d", niceValue)
+ if s.Nice < -20 || s.Nice > 19 {
+	return fmt.Errorf("invalid scheduler.nice: %d", s.Nice)
```

Code after:
		}
		if a[i].Size != b[i].Size {
			return false
		}
	}
	return true
}

func isHostNetNS(path string) (bool, error) {
	const currentProcessNetns = "/proc/self/ns/net"

	var st1, st2 unix.Stat_t

	if err := unix.Stat(currentProcessNetns, &st1); err != nil {
		return false, &os.PathError{Op: "stat", Path: currentProcessNetns, Err: err}
	}
	if err := unix.Stat(path, &st2); err != nil {
		return false, &os.PathError{Op: "stat", Path: path, Err: err}
	}

	return (st1.Dev == st2.Dev) && (st1.Ino == st2.Ino), nil
}

// scheduler is to validate scheduler configs according to https://man7.org/linux/man-pages/man2/sched_setattr.2.html
func scheduler(config *configs.Config) error {
	s := config.Scheduler
	if s == nil {
		return nil
	}
	if s.Policy == "" {
		return errors.New("scheduler policy is required")
	}
	if s.Nice < -20 || s.Nice > 19 {
		return fmt.Errorf("invalid scheduler.nice: %d", s.Nice)
	}
	if s.Priority != 0 && (s.Policy != specs.SchedFIFO && s.Policy != specs.SchedRR) {
		return errors.New("scheduler.priority can only be specified for SchedFIFO or SchedRR policy")
	}
	if s.Policy != specs.SchedDeadline && (s.Runtime != 0 || s.Deadline != 0 || s.Period != 0) {
		return errors.New("scheduler runtime/deadline/period can only be specified for SchedDeadline policy")
	}
	return nil
}