Skip to content

Instantly share code, notes, and snippets.

@sykesm
Last active May 9, 2016 17:38
Show Gist options
  • Save sykesm/020a27341cca5169250990d250b25db4 to your computer and use it in GitHub Desktop.
Save sykesm/020a27341cca5169250990d250b25db4 to your computer and use it in GitHub Desktop.
Namespace confusion
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
"github.com/cloudfoundry-incubator/ducati-daemon/lib/namespace"
"github.com/cloudfoundry-incubator/ducati-daemon/ossupport"
"github.com/pivotal-golang/lager"
)
func main() {
input := make(chan struct{})
logger := lager.NewLogger("borked")
threadLocker := &ossupport.OSLocker{}
pathOpener := &namespace.PathOpener{
Logger: logger,
ThreadLocker: threadLocker,
}
hostNamespace, err := pathOpener.OpenPath("/proc/self/ns/net")
if err != nil {
panic(err)
}
fmt.Printf("host namespace: %s\n", hostNamespace)
tempDir, err := ioutil.TempDir("", "namespace")
if err != nil {
panic(err)
}
repo, err := namespace.NewRepository(logger, tempDir, threadLocker)
if err != nil {
panic(err)
}
testNamespace, err := repo.Create("test-namesapce")
if err != nil {
panic(err)
}
fmt.Printf("test namespace: %s\n", testNamespace)
err = testNamespace.Execute(func(*os.File) error {
for i := 0; i < 10; i++ {
ready := make(chan struct{})
go wait(input, ready)
<-ready
}
return nil
})
if err != nil {
panic(err)
}
dumpNamespaces()
close(input)
}
func wait(input <-chan struct{}, ready chan struct{}) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
close(ready)
for {
if _, ok := <-input; !ok {
return
}
}
}
func dumpNamespaces() {
findCommand := fmt.Sprintf(`find /proc/%d -type l -name net -exec ls -l {} \;`, os.Getpid())
command := exec.Command("bash", "-c", findCommand)
output, err := command.Output()
fmt.Printf("%s\n", string(output))
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment