gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/kernel/context.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 kernel 16 17 import ( 18 "gvisor.dev/gvisor/pkg/context" 19 "gvisor.dev/gvisor/pkg/sentry/kernel/ipc" 20 ) 21 22 // contextID is the kernel package's type for context.Context.Value keys. 23 type contextID int 24 25 const ( 26 // CtxCanTrace is a Context.Value key for a function with the same 27 // signature and semantics as kernel.Task.CanTrace. 28 CtxCanTrace contextID = iota 29 30 // CtxKernel is a Context.Value key for a Kernel. 31 CtxKernel 32 33 // CtxPIDNamespace is a Context.Value key for a PIDNamespace. 34 CtxPIDNamespace 35 36 // CtxTask is a Context.Value key for a Task. 37 CtxTask 38 39 // CtxUTSNamespace is a Context.Value key for a UTSNamespace. 40 CtxUTSNamespace 41 ) 42 43 // ContextCanTrace returns true if ctx is permitted to trace t, in the same sense 44 // as kernel.Task.CanTrace. 45 func ContextCanTrace(ctx context.Context, t *Task, attach bool) bool { 46 if v := ctx.Value(CtxCanTrace); v != nil { 47 return v.(func(*Task, bool) bool)(t, attach) 48 } 49 return false 50 } 51 52 // KernelFromContext returns the Kernel in which ctx is executing, or nil if 53 // there is no such Kernel. 54 func KernelFromContext(ctx context.Context) *Kernel { 55 if v := ctx.Value(CtxKernel); v != nil { 56 return v.(*Kernel) 57 } 58 return nil 59 } 60 61 // PIDNamespaceFromContext returns the PID namespace in which ctx is executing, 62 // or nil if there is no such PID namespace. 63 func PIDNamespaceFromContext(ctx context.Context) *PIDNamespace { 64 if v := ctx.Value(CtxPIDNamespace); v != nil { 65 return v.(*PIDNamespace) 66 } 67 return nil 68 } 69 70 // UTSNamespaceFromContext returns the UTS namespace in which ctx is executing, 71 // or nil if there is no such UTS namespace. 72 func UTSNamespaceFromContext(ctx context.Context) *UTSNamespace { 73 if v := ctx.Value(CtxUTSNamespace); v != nil { 74 return v.(*UTSNamespace) 75 } 76 return nil 77 } 78 79 // IPCNamespaceFromContext returns the IPC namespace in which ctx is executing, 80 // or nil if there is no such IPC namespace. It takes a reference on the 81 // namespace. 82 func IPCNamespaceFromContext(ctx context.Context) *IPCNamespace { 83 if v := ctx.Value(ipc.CtxIPCNamespace); v != nil { 84 return v.(*IPCNamespace) 85 } 86 return nil 87 } 88 89 // TaskFromContext returns the Task associated with ctx, or nil if there is no 90 // such Task. 91 func TaskFromContext(ctx context.Context) *Task { 92 if v := ctx.Value(CtxTask); v != nil { 93 return v.(*Task) 94 } 95 return nil 96 }