Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Modify nsinit to register actions for argv 0
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <[email protected]>
  • Loading branch information
Michael Crosby committed Aug 8, 2014
1 parent cdff060 commit 3abf1dd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
21 changes: 19 additions & 2 deletions nsinit/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import (
"github.com/codegangsta/cli"
)

var logPath = os.Getenv("log")
var (
logPath = os.Getenv("log")
argvs = make(map[string]func())
)

func init() {
argvs["nsenter"] = nsenter
}

func preload(context *cli.Context) error {
if logPath != "" {
Expand All @@ -20,7 +27,18 @@ func preload(context *cli.Context) error {
}

func NsInit() {
// we need to check our argv 0 for any registred functions to run instead of the
// normal cli code path

action, exists := argvs[os.Args[0]]
if exists {
action()

return
}

app := cli.NewApp()

app.Name = "nsinit"
app.Version = "0.1"
app.Author = "libcontainer maintainers"
Expand All @@ -36,7 +54,6 @@ func NsInit() {
initCommand,
statsCommand,
configCommand,
nsenterCommand,
pauseCommand,
unpauseCommand,
}
Expand Down
21 changes: 14 additions & 7 deletions nsinit/nsenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@ package nsinit

import (
"log"
"os"

"github.com/codegangsta/cli"
"github.com/docker/libcontainer"
"github.com/docker/libcontainer/namespaces"
_ "github.com/docker/libcontainer/namespaces/nsenter"
"github.com/docker/libcontainer/syncpipe"
)

var nsenterCommand = cli.Command{
Name: "nsenter",
Usage: "init process for entering an existing namespace",
Action: nsenterAction,
func findUserArgs() []string {
i := 0
for _, a := range os.Args {
i++

if a == "--" {
break
}
}

return os.Args[i:]
}

// 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
func nsenterAction(context *cli.Context) {
func nsenter() {
syncPipe, err := syncpipe.NewSyncPipeFromFd(0, 3)
if err != nil {
log.Fatalf("unable to create sync pipe: %s", err)
Expand All @@ -29,7 +36,7 @@ func nsenterAction(context *cli.Context) {
log.Fatalf("reading container config from parent: %s", err)
}

if err := namespaces.FinalizeSetns(config, context.Args()); err != nil {
if err := namespaces.FinalizeSetns(config, findUserArgs()); err != nil {
log.Fatalf("failed to nsenter: %s", err)
}
}

0 comments on commit 3abf1dd

Please sign in to comment.