command_linux.go 980 B

123456789101112131415161718192021222324252627282930313233
  1. package reexec // import "github.com/docker/docker/pkg/reexec"
  2. import (
  3. "os/exec"
  4. "syscall"
  5. "golang.org/x/sys/unix"
  6. )
  7. // Self returns the path to the current process's binary.
  8. // Returns "/proc/self/exe".
  9. func Self() string {
  10. return "/proc/self/exe"
  11. }
  12. // Command returns *exec.Cmd which has Path as current binary. Also it setting
  13. // SysProcAttr.Pdeathsig to SIGTERM.
  14. // This will use the in-memory version (/proc/self/exe) of the current binary,
  15. // it is thus safe to delete or replace the on-disk binary (os.Args[0]).
  16. //
  17. // As SysProcAttr.Pdeathsig is set, the signal will be sent to the process when
  18. // the OS thread which created the process dies. It is the caller's
  19. // responsibility to ensure that the creating thread is not terminated
  20. // prematurely. See https://go.dev/issue/27505 for more details.
  21. func Command(args ...string) *exec.Cmd {
  22. return &exec.Cmd{
  23. Path: Self(),
  24. Args: args,
  25. SysProcAttr: &syscall.SysProcAttr{
  26. Pdeathsig: unix.SIGTERM,
  27. },
  28. }
  29. }