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