2016-04-27 13:08:20 -04:00
|
|
|
// +build daemon
|
2016-04-22 12:37:48 -04:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-04-27 12:11:32 -04:00
|
|
|
"path/filepath"
|
2016-04-22 12:37:48 -04:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdDaemon execs dockerd with the same flags
|
|
|
|
func (p DaemonProxy) CmdDaemon(args ...string) error {
|
2016-06-09 13:20:55 -04:00
|
|
|
// Special case for handling `docker help daemon`. When pkg/mflag is removed
|
|
|
|
// we can support this on the daemon side, but that is not possible with
|
|
|
|
// pkg/mflag because it uses os.Exit(1) instead of returning an error on
|
|
|
|
// unexpected args.
|
|
|
|
if len(args) == 0 || args[0] != "--help" {
|
|
|
|
// Use os.Args[1:] so that "global" args are passed to dockerd
|
|
|
|
args = stripDaemonArg(os.Args[1:])
|
|
|
|
}
|
2016-04-22 12:37:48 -04:00
|
|
|
|
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-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
|
|
|
|
}
|