github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/kernel/uts_namespace.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/sentry/kernel/auth" 19 "github.com/SagerNet/gvisor/pkg/sync" 20 ) 21 22 // UTSNamespace represents a UTS namespace, a holder of two system identifiers: 23 // the hostname and domain name. 24 // 25 // +stateify savable 26 type UTSNamespace struct { 27 // mu protects all fields below. 28 mu sync.Mutex `state:"nosave"` 29 hostName string 30 domainName string 31 32 // userns is the user namespace associated with the UTSNamespace. 33 // Privileged operations on this UTSNamespace must have appropriate 34 // capabilities in userns. 35 // 36 // userns is immutable. 37 userns *auth.UserNamespace 38 } 39 40 // NewUTSNamespace creates a new UTS namespace. 41 func NewUTSNamespace(hostName, domainName string, userns *auth.UserNamespace) *UTSNamespace { 42 return &UTSNamespace{ 43 hostName: hostName, 44 domainName: domainName, 45 userns: userns, 46 } 47 } 48 49 // UTSNamespace returns the task's UTS namespace. 50 func (t *Task) UTSNamespace() *UTSNamespace { 51 t.mu.Lock() 52 defer t.mu.Unlock() 53 return t.utsns 54 } 55 56 // HostName returns the host name of this UTS namespace. 57 func (u *UTSNamespace) HostName() string { 58 u.mu.Lock() 59 defer u.mu.Unlock() 60 return u.hostName 61 } 62 63 // SetHostName sets the host name of this UTS namespace. 64 func (u *UTSNamespace) SetHostName(host string) { 65 u.mu.Lock() 66 defer u.mu.Unlock() 67 u.hostName = host 68 } 69 70 // DomainName returns the domain name of this UTS namespace. 71 func (u *UTSNamespace) DomainName() string { 72 u.mu.Lock() 73 defer u.mu.Unlock() 74 return u.domainName 75 } 76 77 // SetDomainName sets the domain name of this UTS namespace. 78 func (u *UTSNamespace) SetDomainName(domain string) { 79 u.mu.Lock() 80 defer u.mu.Unlock() 81 u.domainName = domain 82 } 83 84 // UserNamespace returns the user namespace associated with this UTS namespace. 85 func (u *UTSNamespace) UserNamespace() *auth.UserNamespace { 86 u.mu.Lock() 87 defer u.mu.Unlock() 88 return u.userns 89 } 90 91 // Clone makes a copy of this UTS namespace, associating the given user 92 // namespace. 93 func (u *UTSNamespace) Clone(userns *auth.UserNamespace) *UTSNamespace { 94 u.mu.Lock() 95 defer u.mu.Unlock() 96 return &UTSNamespace{ 97 hostName: u.hostName, 98 domainName: u.domainName, 99 userns: userns, 100 } 101 }