github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/syscalls/linux/sys_sched.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package linux
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    20  	"github.com/SagerNet/gvisor/pkg/sentry/arch"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/kernel"
    22  	"github.com/SagerNet/gvisor/pkg/syserror"
    23  )
    24  
    25  const (
    26  	onlyScheduler = linux.SCHED_NORMAL
    27  	onlyPriority  = 0
    28  )
    29  
    30  // SchedParam replicates struct sched_param in sched.h.
    31  //
    32  // +marshal
    33  type SchedParam struct {
    34  	schedPriority int32
    35  }
    36  
    37  // SchedGetparam implements linux syscall sched_getparam(2).
    38  func SchedGetparam(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    39  	pid := args[0].Int()
    40  	param := args[1].Pointer()
    41  	if param == 0 {
    42  		return 0, nil, linuxerr.EINVAL
    43  	}
    44  	if pid < 0 {
    45  		return 0, nil, linuxerr.EINVAL
    46  	}
    47  	if pid != 0 && t.PIDNamespace().TaskWithID(kernel.ThreadID(pid)) == nil {
    48  		return 0, nil, syserror.ESRCH
    49  	}
    50  	r := SchedParam{schedPriority: onlyPriority}
    51  	if _, err := r.CopyOut(t, param); err != nil {
    52  		return 0, nil, err
    53  	}
    54  
    55  	return 0, nil, nil
    56  }
    57  
    58  // SchedGetscheduler implements linux syscall sched_getscheduler(2).
    59  func SchedGetscheduler(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    60  	pid := args[0].Int()
    61  	if pid < 0 {
    62  		return 0, nil, linuxerr.EINVAL
    63  	}
    64  	if pid != 0 && t.PIDNamespace().TaskWithID(kernel.ThreadID(pid)) == nil {
    65  		return 0, nil, syserror.ESRCH
    66  	}
    67  	return onlyScheduler, nil, nil
    68  }
    69  
    70  // SchedSetscheduler implements linux syscall sched_setscheduler(2).
    71  func SchedSetscheduler(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    72  	pid := args[0].Int()
    73  	policy := args[1].Int()
    74  	param := args[2].Pointer()
    75  	if pid < 0 {
    76  		return 0, nil, linuxerr.EINVAL
    77  	}
    78  	if policy != onlyScheduler {
    79  		return 0, nil, linuxerr.EINVAL
    80  	}
    81  	if pid != 0 && t.PIDNamespace().TaskWithID(kernel.ThreadID(pid)) == nil {
    82  		return 0, nil, syserror.ESRCH
    83  	}
    84  	var r SchedParam
    85  	if _, err := r.CopyIn(t, param); err != nil {
    86  		return 0, nil, linuxerr.EINVAL
    87  	}
    88  	if r.schedPriority != onlyPriority {
    89  		return 0, nil, linuxerr.EINVAL
    90  	}
    91  	return 0, nil, nil
    92  }
    93  
    94  // SchedGetPriorityMax implements linux syscall sched_get_priority_max(2).
    95  func SchedGetPriorityMax(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    96  	return onlyPriority, nil, nil
    97  }
    98  
    99  // SchedGetPriorityMin implements linux syscall sched_get_priority_min(2).
   100  func SchedGetPriorityMin(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
   101  	return onlyPriority, nil, nil
   102  }