2016-04-27 13:08:20 -04:00
|
|
|
// +build daemon
|
2016-04-22 12:37:48 -04:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-06-22 13:08:04 -04:00
|
|
|
"fmt"
|
|
|
|
|
2016-04-22 12:37:48 -04:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-04-27 12:11:32 -04:00
|
|
|
"path/filepath"
|
2016-04-22 12:37:48 -04:00
|
|
|
"syscall"
|
2016-06-22 13:08:04 -04:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2016-04-22 12:37:48 -04:00
|
|
|
)
|
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
const daemonBinary = "dockerd"
|
|
|
|
|
|
|
|
func newDaemonCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2016-08-26 12:19:02 -04:00
|
|
|
Use: "daemon",
|
|
|
|
Hidden: true,
|
|
|
|
Args: cobra.ArbitraryArgs,
|
|
|
|
DisableFlagParsing: true,
|
2016-06-22 13:08:04 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runDaemon()
|
|
|
|
},
|
2016-06-09 13:20:55 -04:00
|
|
|
}
|
2016-06-22 13:08:04 -04:00
|
|
|
cmd.SetHelpFunc(helpFunc)
|
|
|
|
return cmd
|
|
|
|
}
|
2016-04-22 12:37:48 -04:00
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
// CmdDaemon execs dockerd with the same flags
|
|
|
|
func runDaemon() error {
|
|
|
|
// Use os.Args[1:] so that "global" args are passed to dockerd
|
|
|
|
return execDaemon(stripDaemonArg(os.Args[1:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func execDaemon(args []string) error {
|
2016-04-27 12:11:32 -04:00
|
|
|
binaryPath, err := findDaemonBinary()
|
2016-04-22 12:37:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return syscall.Exec(
|
2016-04-27 12:11:32 -04:00
|
|
|
binaryPath,
|
2016-04-22 12:37:48 -04:00
|
|
|
append([]string{daemonBinary}, args...),
|
|
|
|
os.Environ())
|
|
|
|
}
|
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
func helpFunc(cmd *cobra.Command, args []string) {
|
|
|
|
if err := execDaemon([]string{"--help"}); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 12:11:32 -04:00
|
|
|
// findDaemonBinary looks for the path to the dockerd binary starting with
|
|
|
|
// the directory of the current executable (if one exists) and followed by $PATH
|
|
|
|
func findDaemonBinary() (string, error) {
|
|
|
|
execDirname := filepath.Dir(os.Args[0])
|
|
|
|
if execDirname != "" {
|
|
|
|
binaryPath := filepath.Join(execDirname, daemonBinary)
|
|
|
|
if _, err := os.Stat(binaryPath); err == nil {
|
|
|
|
return binaryPath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec.LookPath(daemonBinary)
|
|
|
|
}
|
|
|
|
|
2016-04-22 12:37:48 -04:00
|
|
|
// stripDaemonArg removes the `daemon` argument from the list
|
|
|
|
func stripDaemonArg(args []string) []string {
|
|
|
|
for i, arg := range args {
|
|
|
|
if arg == "daemon" {
|
|
|
|
return append(args[:i], args[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|