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