github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/uniqueid/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 uniqueid defines context.Context keys for obtaining system-wide
    16  // unique identifiers.
    17  package uniqueid
    18  
    19  import (
    20  	"github.com/SagerNet/gvisor/pkg/context"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/socket/unix/transport"
    22  )
    23  
    24  // contextID is the kernel package's type for context.Context.Value keys.
    25  type contextID int
    26  
    27  const (
    28  	// CtxGlobalUniqueID is a Context.Value key for a system-wide
    29  	// unique identifier.
    30  	CtxGlobalUniqueID contextID = iota
    31  
    32  	// CtxGlobalUniqueIDProvider is a Context.Value key for a
    33  	// system-wide unique identifier generator.
    34  	CtxGlobalUniqueIDProvider
    35  
    36  	// CtxInotifyCookie is a Context.Value key for a unique inotify
    37  	// event cookie.
    38  	CtxInotifyCookie
    39  )
    40  
    41  // GlobalFromContext returns a system-wide unique identifier from ctx.
    42  func GlobalFromContext(ctx context.Context) uint64 {
    43  	return ctx.Value(CtxGlobalUniqueID).(uint64)
    44  }
    45  
    46  // GlobalProviderFromContext returns a system-wide unique identifier from ctx.
    47  func GlobalProviderFromContext(ctx context.Context) transport.UniqueIDProvider {
    48  	return ctx.Value(CtxGlobalUniqueIDProvider).(transport.UniqueIDProvider)
    49  }
    50  
    51  // InotifyCookie generates a unique inotify event cookie from ctx.
    52  func InotifyCookie(ctx context.Context) uint32 {
    53  	return ctx.Value(CtxInotifyCookie).(uint32)
    54  }