Add a way to get a pid of a process

Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>
Upstream-commit: 0fc52ca6dd628f2b9163f5d328d1d749c6708de4
Component: engine
This commit is contained in:
Flavio Crisciani
2018-07-26 11:17:46 -07:00
parent 19f93d0875
commit 1fb6cc9e1e
3 changed files with 151 additions and 6 deletions
@@ -394,13 +394,10 @@ func (n *networkNamespace) InvokeFunc(f func()) error {
// InitOSContext initializes OS context while configuring network resources
func InitOSContext() func() {
runtime.LockOSThread()
if err := ns.SetNamespace(); err != nil {
logrus.Error(err)
}
return runtime.UnlockOSThread
}
func nsInvoke(path string, prefunc func(nsFD int) error, postfunc func(callerFD int) error) error {
func nsInvoke(path string, prefunc, postfunc func(int) error) error {
defer InitOSContext()()
newNs, err := netns.GetFromPath(path)
@@ -415,10 +412,14 @@ func nsInvoke(path string, prefunc func(nsFD int) error, postfunc func(callerFD
return fmt.Errorf("failed in prefunc: %v", err)
}
// save the current namespace (host namespace)
curNs, _ := netns.Get()
if err = netns.Set(newNs); err != nil {
return err
}
defer ns.SetNamespace()
defer curNs.Close()
// will restore the previous namespace before unlocking the thread
defer netns.Set(curNs)
// Invoked after the namespace switch.
return postfunc(ns.ParseHandlerInt())
@@ -651,7 +652,10 @@ func (n *networkNamespace) ApplyOSTweaks(types []SandboxType) {
for _, t := range types {
switch t {
case SandboxTypeLoadBalancer:
kernel.ApplyOSTweaks(loadBalancerConfig)
nsInvoke(n.nsPath(),
func(nsFD int) error { return nil },
func(callerFD int) error { kernel.ApplyOSTweaks(loadBalancerConfig); return nil },
)
}
}
}