Debian Codegraph @

pkg/ns/ns_linux.go

raw symbols SCIP
1 // Copyright 2015-2017 CNI 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 nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/
16
17 import (
18 "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/"
19 "osscip-go gomod github.com/golang/go/src go1.24.2 os/"
20 "runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/"
21 "syncscip-go gomod github.com/golang/go/src go1.24.2 sync/"
22 "syscallscip-go gomod github.com/golang/go/src go1.24.2 syscall/"
23
24 "golang.org/x/sys/unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/"
25 )
26
27 // Returns an object representing the current OS thread's network namespace
28 func GetCurrentNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetCurrentNS().() (NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#, error) {
29 // Lock the thread in case other goroutine executes in it and changes its
30 // network namespace after getCurrentThreadNetNSPath(), otherwise it might
31 // return an unexpected network namespace.
32 runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/.LockOSThreadscip-go gomod github.com/golang/go/src go1.24.2 runtime/LockOSThread().()
33 defer runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/.UnlockOSThreadscip-go gomod github.com/golang/go/src go1.24.2 runtime/UnlockOSThread().()
34 return getCurrentNSNoLockscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/getCurrentNSNoLock().()
35 }
36
37 func getCurrentNSNoLockscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/getCurrentNSNoLock().() (NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#, error) {
38 return GetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetNS().(getCurrentThreadNetNSPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/getCurrentThreadNetNSPath().())
39 }
40
41 func getCurrentThreadNetNSPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/getCurrentThreadNetNSPath().() string {
42 // /proc/self/ns/net returns the namespace of the main thread, not
43 // of whatever thread this goroutine is running on. Make sure we
44 // use the thread's net namespace since the thread is switching around
45 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("/proc/%d/task/%d/ns/net", osscip-go gomod github.com/golang/go/src go1.24.2 os/.Getpidscip-go gomod github.com/golang/go/src go1.24.2 os/Getpid().(), unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.Gettidscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/Gettid().())
46 }
47
48 func (ns *netNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#) Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#Close().() error {
49 if err := ns.errorIfClosedscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#errorIfClosed().(); err != nil {
50 return err
51 }
52
53 if err := ns.filescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#file..Closescip-go gomod github.com/golang/go/src go1.24.2 os/File#Close().(); err != nil {
54 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Failed to close %q: %v", ns.filescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#file..Namescip-go gomod github.com/golang/go/src go1.24.2 os/File#Name().(), err)
55 }
56 ns.closedscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#closed. = true
57
58 return nil
59 }
60
61 func (ns *netNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#) Setscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#Set().() error {
62 if err := ns.errorIfClosedscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#errorIfClosed().(); err != nil {
63 return err
64 }
65
66 if err := unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.Setnsscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/Setns().(int(ns.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#Fd().()), unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.CLONE_NEWNETscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/CLONE_NEWNET.); err != nil {
67 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Error switching to ns %v: %v", ns.filescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#file..Namescip-go gomod github.com/golang/go/src go1.24.2 os/File#Name().(), err)
68 }
69
70 return nil
71 }
72
73 type NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS# interface {
74 // Executes the passed closure in this object's network namespace,
75 // attempting to restore the original namespace before returning.
76 // However, since each OS thread can have a different network namespace,
77 // and Go's thread scheduling is highly variable, callers cannot
78 // guarantee any specific namespace is set unless operations that
79 // require that namespace are wrapped with Do(). Also, no code called
80 // from Do() should call runtime.UnlockOSThread(), or the risk
81 // of executing code in an incorrect namespace will be greater. See
82 // https://github.com/golang/go/wiki/LockOSThread for further details.
83 Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(toRun func(NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error) error
84
85 // Sets the current network namespace to this object's network namespace.
86 // Note that since Go's thread scheduling is highly variable, callers
87 // cannot guarantee the requested namespace will be the current namespace
88 // after this function is called; to ensure this wrap operations that
89 // require the namespace with Do() instead.
90 Setscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Set.() error
91
92 // Returns the filesystem path representing this object's network namespace
93 Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Path.() string
94
95 // Returns a file descriptor representing this object's network namespace
96 Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.() uintptr
97
98 // Cleans up this instance of the network namespace; if this instance
99 // is the last user the namespace will be destroyed
100 Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.() error
101 }
102
103 type netNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS# struct {
104 filescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#file. *osscip-go gomod github.com/golang/go/src go1.24.2 os/.Filescip-go gomod github.com/golang/go/src go1.24.2 os/File#
105 closedscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#closed. bool
106 }
107
108 // netNS implements the NetNS interface
109 var _ NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS# = &netNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#{}
110
111 const (
112 // https://github.com/torvalds/linux/blob/master/include/uapi/linux/magic.h
113 NSFS_MAGICscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSFS_MAGIC. = unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.NSFS_MAGICscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/NSFS_MAGIC.
114 PROCFS_MAGICscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/PROCFS_MAGIC. = unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.PROC_SUPER_MAGICscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/PROC_SUPER_MAGIC.
115 )
116
117 type NSPathNotExistErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotExistErr# struct{ msgscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotExistErr#msg. string }
118
119 func (e NSPathNotExistErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotExistErr#) Errorscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotExistErr#Error().() string { return e.msgscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotExistErr#msg. }
120
121 type NSPathNotNSErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotNSErr# struct{ msgscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotNSErr#msg. string }
122
123 func (e NSPathNotNSErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotNSErr#) Errorscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotNSErr#Error().() string { return e.msgscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotNSErr#msg. }
124
125 func IsNSorErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/IsNSorErr().(nspath string) error {
126 stat := syscallscip-go gomod github.com/golang/go/src go1.24.2 syscall/.Statfs_tscip-go gomod github.com/golang/go/src go1.24.2 syscall/Statfs_t#{}
127 if err := syscallscip-go gomod github.com/golang/go/src go1.24.2 syscall/.Statfsscip-go gomod github.com/golang/go/src go1.24.2 syscall/Statfs().(nspath, &stat); err != nil {
128 if osscip-go gomod github.com/golang/go/src go1.24.2 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.24.2 os/IsNotExist().(err) {
129 err = NSPathNotExistErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotExistErr#{msgscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotExistErr#msg.: fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("failed to Statfs %q: %v", nspath, err)}
130 } else {
131 err = fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to Statfs %q: %v", nspath, err)
132 }
133 return err
134 }
135
136 switch stat.Typescip-go gomod github.com/golang/go/src go1.24.2 syscall/Statfs_t#Type. {
137 case PROCFS_MAGICscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/PROCFS_MAGIC., NSFS_MAGICscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSFS_MAGIC.:
138 return nil
139 default:
140 return NSPathNotNSErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotNSErr#{msgscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotNSErr#msg.: fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("unknown FS magic on %q: %x", nspath, stat.Typescip-go gomod github.com/golang/go/src go1.24.2 syscall/Statfs_t#Type.)}
141 }
142 }
143
144 // Returns an object representing the namespace referred to by @path
145 func GetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetNS().(nspath string) (NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#, error) {
146 err := IsNSorErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/IsNSorErr().(nspath)
147 if err != nil {
148 return nil, err
149 }
150
151 fd, err := osscip-go gomod github.com/golang/go/src go1.24.2 os/.Openscip-go gomod github.com/golang/go/src go1.24.2 os/Open().(nspath)
152 if err != nil {
153 return nil, err
154 }
155
156 return &netNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#{filescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#file.: fd}, nil
157 }
158
159 // Returns a new empty NetNS.
160 // Calling Close() let the kernel garbage collect the network namespace.
161 func TempNetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/TempNetNS().() (NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#, error) {
162 var tempNS NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#
163 var err error
164 var wg syncscip-go gomod github.com/golang/go/src go1.24.2 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.24.2 sync/WaitGroup#
165 wg.Addscip-go gomod github.com/golang/go/src go1.24.2 sync/WaitGroup#Add().(1)
166
167 // Create the new namespace in a new goroutine so that if we later fail
168 // to switch the namespace back to the original one, we can safely
169 // leave the thread locked to die without a risk of the current thread
170 // left lingering with incorrect namespace.
171 go func() {
172 defer wg.Donescip-go gomod github.com/golang/go/src go1.24.2 sync/WaitGroup#Done().()
173 runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/.LockOSThreadscip-go gomod github.com/golang/go/src go1.24.2 runtime/LockOSThread().()
174
175 var threadNS NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#
176 // save a handle to current network namespace
177 threadNS, err = getCurrentNSNoLockscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/getCurrentNSNoLock().()
178 if err != nil {
179 err = fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to open current namespace: %v", err)
180 return
181 }
182 defer threadNS.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.()
183
184 // create the temporary network namespace
185 err = unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.Unsharescip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/Unshare().(unixscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/.CLONE_NEWNETscip-go gomod golang.org/x/sys v0.35.0 `golang.org/x/sys/unix`/CLONE_NEWNET.)
186 if err != nil {
187 return
188 }
189
190 // get a handle to the temporary network namespace
191 tempNS, err = getCurrentNSNoLockscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/getCurrentNSNoLock().()
192
193 err2 := threadNS.Setscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Set.()
194 if err2 == nil {
195 // Unlock the current thread only when we successfully switched back
196 // to the original namespace; otherwise leave the thread locked which
197 // will force the runtime to scrap the current thread, that is maybe
198 // not as optimal but at least always safe to do.
199 runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/.UnlockOSThreadscip-go gomod github.com/golang/go/src go1.24.2 runtime/UnlockOSThread().()
200 }
201 }()
202
203 wg.Waitscip-go gomod github.com/golang/go/src go1.24.2 sync/WaitGroup#Wait().()
204 return tempNS, err
205 }
206
207 func (ns *netNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#) Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#Path().() string {
208 return ns.filescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#file..Namescip-go gomod github.com/golang/go/src go1.24.2 os/File#Name().()
209 }
210
211 func (ns *netNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#) Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#Fd().() uintptr {
212 return ns.filescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#file..Fdscip-go gomod github.com/golang/go/src go1.24.2 os/File#Fd().()
213 }
214
215 func (ns *netNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#) errorIfClosedscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#errorIfClosed().() error {
216 if ns.closedscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#closed. {
217 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("%q has already been closed", ns.filescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#file..Namescip-go gomod github.com/golang/go/src go1.24.2 os/File#Name().())
218 }
219 return nil
220 }
221
222 func (ns *netNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#) Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#Do().(toRun func(NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error) error {
223 if err := ns.errorIfClosedscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#errorIfClosed().(); err != nil {
224 return err
225 }
226
227 containedCall := func(hostNS NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error {
228 threadNS, err := getCurrentNSNoLockscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/getCurrentNSNoLock().()
229 if err != nil {
230 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to open current netns: %v", err)
231 }
232 defer threadNS.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.()
233
234 // switch to target namespace
235 if err = ns.Setscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#Set().(); err != nil {
236 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("error switching to ns %v: %v", ns.filescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/netNS#file..Namescip-go gomod github.com/golang/go/src go1.24.2 os/File#Name().(), err)
237 }
238 defer func() {
239 err := threadNS.Setscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Set.() // switch back
240 if err == nil {
241 // Unlock the current thread only when we successfully switched back
242 // to the original namespace; otherwise leave the thread locked which
243 // will force the runtime to scrap the current thread, that is maybe
244 // not as optimal but at least always safe to do.
245 runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/.UnlockOSThreadscip-go gomod github.com/golang/go/src go1.24.2 runtime/UnlockOSThread().()
246 }
247 }()
248
249 return toRun(hostNS)
250 }
251
252 // save a handle to current network namespace
253 hostNS, err := GetCurrentNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetCurrentNS().()
254 if err != nil {
255 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Failed to open current namespace: %v", err)
256 }
257 defer hostNS.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.()
258
259 var wg syncscip-go gomod github.com/golang/go/src go1.24.2 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.24.2 sync/WaitGroup#
260 wg.Addscip-go gomod github.com/golang/go/src go1.24.2 sync/WaitGroup#Add().(1)
261
262 // Start the callback in a new green thread so that if we later fail
263 // to switch the namespace back to the original one, we can safely
264 // leave the thread locked to die without a risk of the current thread
265 // left lingering with incorrect namespace.
266 var innerError error
267 go func() {
268 defer wg.Donescip-go gomod github.com/golang/go/src go1.24.2 sync/WaitGroup#Done().()
269 runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/.LockOSThreadscip-go gomod github.com/golang/go/src go1.24.2 runtime/LockOSThread().()
270 innerError = containedCall(hostNS)
271 }()
272 wg.Waitscip-go gomod github.com/golang/go/src go1.24.2 sync/WaitGroup#Wait().()
273
274 return innerError
275 }
276
277 // WithNetNSPath executes the passed closure under the given network
278 // namespace, restoring the original namespace afterwards.
279 func WithNetNSPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/WithNetNSPath().(nspath string, toRun func(NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error) error {
280 ns, err := GetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetNS().(nspath)
281 if err != nil {
282 return err
283 }
284 defer ns.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.()
285 return ns.Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(toRun)
286 }