github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cmd/docker/daemon_unix.go (about) 1 // +build daemon 2 3 package main 4 5 import ( 6 "fmt" 7 8 "os" 9 "os/exec" 10 "path/filepath" 11 "syscall" 12 13 "github.com/spf13/cobra" 14 ) 15 16 const daemonBinary = "dockerd" 17 18 func newDaemonCommand() *cobra.Command { 19 cmd := &cobra.Command{ 20 Use: "daemon", 21 Hidden: true, 22 Args: cobra.ArbitraryArgs, 23 DisableFlagParsing: true, 24 RunE: func(cmd *cobra.Command, args []string) error { 25 return runDaemon() 26 }, 27 Deprecated: "and will be removed in Docker 1.16. Please run `dockerd` directly.", 28 } 29 cmd.SetHelpFunc(helpFunc) 30 return cmd 31 } 32 33 // CmdDaemon execs dockerd with the same flags 34 func runDaemon() error { 35 // Use os.Args[1:] so that "global" args are passed to dockerd 36 return execDaemon(stripDaemonArg(os.Args[1:])) 37 } 38 39 func execDaemon(args []string) error { 40 binaryPath, err := findDaemonBinary() 41 if err != nil { 42 return err 43 } 44 45 return syscall.Exec( 46 binaryPath, 47 append([]string{daemonBinary}, args...), 48 os.Environ()) 49 } 50 51 func helpFunc(cmd *cobra.Command, args []string) { 52 if err := execDaemon([]string{"--help"}); err != nil { 53 fmt.Fprintf(os.Stderr, "%s\n", err.Error()) 54 } 55 } 56 57 // findDaemonBinary looks for the path to the dockerd binary starting with 58 // the directory of the current executable (if one exists) and followed by $PATH 59 func findDaemonBinary() (string, error) { 60 execDirname := filepath.Dir(os.Args[0]) 61 if execDirname != "" { 62 binaryPath := filepath.Join(execDirname, daemonBinary) 63 if _, err := os.Stat(binaryPath); err == nil { 64 return binaryPath, nil 65 } 66 } 67 68 return exec.LookPath(daemonBinary) 69 } 70 71 // stripDaemonArg removes the `daemon` argument from the list 72 func stripDaemonArg(args []string) []string { 73 for i, arg := range args { 74 if arg == "daemon" { 75 return append(args[:i], args[i+1:]...) 76 } 77 } 78 return args 79 }