github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/vfs/context.go (about)

     1  // Copyright 2019 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 vfs
    16  
    17  import (
    18  	"github.com/nicocha30/gvisor-ligolo/pkg/context"
    19  )
    20  
    21  // contextID is this package's type for context.Context.Value keys.
    22  type contextID int
    23  
    24  const (
    25  	// CtxMountNamespace is a Context.Value key for a MountNamespace.
    26  	CtxMountNamespace contextID = iota
    27  
    28  	// CtxRoot is a Context.Value key for a VFS root.
    29  	CtxRoot
    30  )
    31  
    32  // MountNamespaceFromContext returns the MountNamespace used by ctx. If ctx is
    33  // not associated with a MountNamespace, MountNamespaceFromContext returns nil.
    34  //
    35  // A reference is taken on the returned MountNamespace.
    36  func MountNamespaceFromContext(ctx context.Context) *MountNamespace {
    37  	if v := ctx.Value(CtxMountNamespace); v != nil {
    38  		return v.(*MountNamespace)
    39  	}
    40  	return nil
    41  }
    42  
    43  type mountNamespaceContext struct {
    44  	context.Context
    45  	mntns *MountNamespace
    46  }
    47  
    48  // Value implements Context.Value.
    49  func (mc mountNamespaceContext) Value(key any) any {
    50  	switch key {
    51  	case CtxMountNamespace:
    52  		mc.mntns.IncRef()
    53  		return mc.mntns
    54  	default:
    55  		return mc.Context.Value(key)
    56  	}
    57  }
    58  
    59  // WithMountNamespace returns a copy of ctx with the given MountNamespace.
    60  func WithMountNamespace(ctx context.Context, mntns *MountNamespace) context.Context {
    61  	return &mountNamespaceContext{
    62  		Context: ctx,
    63  		mntns:   mntns,
    64  	}
    65  }
    66  
    67  // RootFromContext returns the VFS root used by ctx. It takes a reference on
    68  // the returned VirtualDentry. If ctx does not have a specific VFS root,
    69  // RootFromContext returns a zero-value VirtualDentry.
    70  func RootFromContext(ctx context.Context) VirtualDentry {
    71  	if v := ctx.Value(CtxRoot); v != nil {
    72  		return v.(VirtualDentry)
    73  	}
    74  	return VirtualDentry{}
    75  }
    76  
    77  type rootContext struct {
    78  	context.Context
    79  	root VirtualDentry
    80  }
    81  
    82  // WithRoot returns a copy of ctx with the given root.
    83  func WithRoot(ctx context.Context, root VirtualDentry) context.Context {
    84  	return &rootContext{
    85  		Context: ctx,
    86  		root:    root,
    87  	}
    88  }
    89  
    90  // Value implements Context.Value.
    91  func (rc rootContext) Value(key any) any {
    92  	switch key {
    93  	case CtxRoot:
    94  		rc.root.IncRef()
    95  		return rc.root
    96  	default:
    97  		return rc.Context.Value(key)
    98  	}
    99  }