daemon_unix.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // +build !windows
  2. package main
  3. import (
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "syscall"
  8. )
  9. // CmdDaemon execs dockerd with the same flags
  10. func (p DaemonProxy) CmdDaemon(args ...string) error {
  11. // Use os.Args[1:] so that "global" args are passed to dockerd
  12. args = stripDaemonArg(os.Args[1:])
  13. binaryPath, err := findDaemonBinary()
  14. if err != nil {
  15. return err
  16. }
  17. return syscall.Exec(
  18. binaryPath,
  19. append([]string{daemonBinary}, args...),
  20. os.Environ())
  21. }
  22. // findDaemonBinary looks for the path to the dockerd binary starting with
  23. // the directory of the current executable (if one exists) and followed by $PATH
  24. func findDaemonBinary() (string, error) {
  25. execDirname := filepath.Dir(os.Args[0])
  26. if execDirname != "" {
  27. binaryPath := filepath.Join(execDirname, daemonBinary)
  28. if _, err := os.Stat(binaryPath); err == nil {
  29. return binaryPath, nil
  30. }
  31. }
  32. return exec.LookPath(daemonBinary)
  33. }
  34. // stripDaemonArg removes the `daemon` argument from the list
  35. func stripDaemonArg(args []string) []string {
  36. for i, arg := range args {
  37. if arg == "daemon" {
  38. return append(args[:i], args[i+1:]...)
  39. }
  40. }
  41. return args
  42. }