2014-08-13 01:46:43 +08:00
|
|
|
package main
|
2014-06-06 05:28:09 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2014-08-09 02:16:56 +08:00
|
|
|
"os"
|
2014-06-06 05:28:09 +08:00
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/namespaces"
|
2014-08-09 01:43:18 +08:00
|
|
|
_ "github.com/docker/libcontainer/namespaces/nsenter"
|
2014-08-07 09:44:41 +08:00
|
|
|
"github.com/docker/libcontainer/syncpipe"
|
2014-06-06 05:28:09 +08:00
|
|
|
)
|
|
|
|
|
2014-08-09 02:16:56 +08:00
|
|
|
func findUserArgs() []string {
|
|
|
|
i := 0
|
|
|
|
for _, a := range os.Args {
|
|
|
|
i++
|
|
|
|
|
|
|
|
if a == "--" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Args[i:]
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
// this expects that we already have our namespaces setup by the C initializer
|
|
|
|
// we are expected to finalize the namespace and exec the user's application
|
2014-08-09 02:16:56 +08:00
|
|
|
func nsenter() {
|
2014-08-07 09:44:41 +08:00
|
|
|
syncPipe, err := syncpipe.NewSyncPipeFromFd(0, 3)
|
2014-06-06 05:28:09 +08:00
|
|
|
if err != nil {
|
2014-08-07 09:44:41 +08:00
|
|
|
log.Fatalf("unable to create sync pipe: %s", err)
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
var config *libcontainer.Config
|
|
|
|
if err := syncPipe.ReadFromParent(&config); err != nil {
|
|
|
|
log.Fatalf("reading container config from parent: %s", err)
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-09 02:16:56 +08:00
|
|
|
if err := namespaces.FinalizeSetns(config, findUserArgs()); err != nil {
|
2014-06-06 05:28:09 +08:00
|
|
|
log.Fatalf("failed to nsenter: %s", err)
|
|
|
|
}
|
|
|
|
}
|