Move ThreadGroupIDFromContext to kernel/auth.

This function doesn't belong in the global context package. Move to a more
suitable package to break the dependency cycle.

PiperOrigin-RevId: 406942122
This commit is contained in:
Adin Scannell
2021-11-01 16:07:40 -07:00
committed by gVisor bot
parent df6043afa0
commit 9776edb3fa
5 changed files with 17 additions and 24 deletions
-20
View File
@@ -29,26 +29,6 @@ import (
"gvisor.dev/gvisor/pkg/log"
)
type contextID int
// Globally accessible values from a context. These keys are defined in the
// context package to resolve dependency cycles by not requiring the caller to
// import packages usually required to get these information.
const (
// CtxThreadGroupID is the current thread group ID when a context represents
// a task context. The value is represented as an int32.
CtxThreadGroupID contextID = iota
)
// ThreadGroupIDFromContext returns the current thread group ID when ctx
// represents a task context.
func ThreadGroupIDFromContext(ctx Context) (tgid int32, ok bool) {
if tgid := ctx.Value(CtxThreadGroupID); tgid != nil {
return tgid.(int32), true
}
return 0, false
}
// A Context represents a thread of execution (hereafter "goroutine" to reflect
// Go idiosyncrasy). It carries state associated with the goroutine across API
// boundaries.
+13
View File
@@ -24,6 +24,10 @@ type contextID int
const (
// CtxCredentials is a Context.Value key for Credentials.
CtxCredentials contextID = iota
// CtxThreadGroupID is the current thread group ID when a context represents
// a task context. The value is represented as an int32.
CtxThreadGroupID contextID = iota
)
// CredentialsFromContext returns a copy of the Credentials used by ctx, or a
@@ -35,6 +39,15 @@ func CredentialsFromContext(ctx context.Context) *Credentials {
return NewAnonymousCredentials()
}
// ThreadGroupIDFromContext returns the current thread group ID when ctx
// represents a task context.
func ThreadGroupIDFromContext(ctx context.Context) (tgid int32, ok bool) {
if tgid := ctx.Value(CtxThreadGroupID); tgid != nil {
return tgid.(int32), true
}
return 0, false
}
// ContextWithCredentials returns a copy of ctx carrying creds.
func ContextWithCredentials(ctx context.Context, creds *Credentials) context.Context {
return &authContext{ctx, creds}
+1 -1
View File
@@ -399,7 +399,7 @@ func (q *Queue) Flush(ctx context.Context) {
q.mu.Lock()
defer q.mu.Unlock()
pid, ok := context.ThreadGroupIDFromContext(ctx)
pid, ok := auth.ThreadGroupIDFromContext(ctx)
if ok {
if q.subscriber != nil && pid == q.subscriber.pid {
q.subscriber = nil
+2 -2
View File
@@ -444,7 +444,7 @@ func (s *Shm) AddMapping(ctx context.Context, _ memmap.MappingSpace, _ hostarch.
s.mu.Lock()
defer s.mu.Unlock()
s.attachTime = ktime.NowFromContext(ctx)
if pid, ok := context.ThreadGroupIDFromContext(ctx); ok {
if pid, ok := auth.ThreadGroupIDFromContext(ctx); ok {
s.lastAttachDetachPID = pid
} else {
// AddMapping is called during a syscall, so ctx should always be a task
@@ -468,7 +468,7 @@ func (s *Shm) RemoveMapping(ctx context.Context, _ memmap.MappingSpace, _ hostar
// If called from a non-task context we also won't have a threadgroup
// id. Silently skip updating the lastAttachDetachPid in that case.
if pid, ok := context.ThreadGroupIDFromContext(ctx); ok {
if pid, ok := auth.ThreadGroupIDFromContext(ctx); ok {
s.lastAttachDetachPID = pid
} else {
log.Debugf("Couldn't obtain pid when removing mapping to %s, not updating the last detach pid.", s.debugLocked())
+1 -1
View File
@@ -86,7 +86,7 @@ func (t *Task) contextValue(key interface{}, isTaskGoroutine bool) interface{} {
return t
case auth.CtxCredentials:
return t.creds.Load()
case context.CtxThreadGroupID:
case auth.CtxThreadGroupID:
return int32(t.tg.ID())
case fs.CtxRoot:
if !isTaskGoroutine {