github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/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/ttpreport/gvisor-ligolo/pkg/abi/linux" 19 "github.com/ttpreport/gvisor-ligolo/pkg/errors/linuxerr" 20 "github.com/ttpreport/gvisor-ligolo/pkg/sentry/arch" 21 "github.com/ttpreport/gvisor-ligolo/pkg/sentry/kernel" 22 ) 23 24 const ( 25 onlyScheduler = linux.SCHED_NORMAL 26 onlyPriority = 0 27 ) 28 29 // SchedParam replicates struct sched_param in sched.h. 30 // 31 // +marshal 32 type SchedParam struct { 33 schedPriority int32 34 } 35 36 // SchedGetparam implements linux syscall sched_getparam(2). 37 func SchedGetparam(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 38 pid := args[0].Int() 39 param := args[1].Pointer() 40 if param == 0 { 41 return 0, nil, linuxerr.EINVAL 42 } 43 if pid < 0 { 44 return 0, nil, linuxerr.EINVAL 45 } 46 if pid != 0 && t.PIDNamespace().TaskWithID(kernel.ThreadID(pid)) == nil { 47 return 0, nil, linuxerr.ESRCH 48 } 49 r := SchedParam{schedPriority: onlyPriority} 50 if _, err := r.CopyOut(t, param); err != nil { 51 return 0, nil, err 52 } 53 54 return 0, nil, nil 55 } 56 57 // SchedGetscheduler implements linux syscall sched_getscheduler(2). 58 func SchedGetscheduler(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 59 pid := args[0].Int() 60 if pid < 0 { 61 return 0, nil, linuxerr.EINVAL 62 } 63 if pid != 0 && t.PIDNamespace().TaskWithID(kernel.ThreadID(pid)) == nil { 64 return 0, nil, linuxerr.ESRCH 65 } 66 return onlyScheduler, nil, nil 67 } 68 69 // SchedSetscheduler implements linux syscall sched_setscheduler(2). 70 func SchedSetscheduler(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 71 pid := args[0].Int() 72 policy := args[1].Int() 73 param := args[2].Pointer() 74 if pid < 0 { 75 return 0, nil, linuxerr.EINVAL 76 } 77 if policy != onlyScheduler { 78 return 0, nil, linuxerr.EINVAL 79 } 80 if pid != 0 && t.PIDNamespace().TaskWithID(kernel.ThreadID(pid)) == nil { 81 return 0, nil, linuxerr.ESRCH 82 } 83 var r SchedParam 84 if _, err := r.CopyIn(t, param); err != nil { 85 return 0, nil, linuxerr.EINVAL 86 } 87 if r.schedPriority != onlyPriority { 88 return 0, nil, linuxerr.EINVAL 89 } 90 return 0, nil, nil 91 } 92 93 // SchedGetPriorityMax implements linux syscall sched_get_priority_max(2). 94 func SchedGetPriorityMax(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 95 return onlyPriority, nil, nil 96 } 97 98 // SchedGetPriorityMin implements linux syscall sched_get_priority_min(2). 99 func SchedGetPriorityMin(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 100 return onlyPriority, nil, nil 101 }