Browse code

reexec: Use in-memory binary on linux instead of os.Args[0]

This keeps reexec working properly even if the on-disk binary was replaced.

Signed-off-by: Tibor Vass <tibor@docker.com>

Tibor Vass authored on 2015/07/25 02:51:51
Showing 3 changed files
... ...
@@ -7,10 +7,16 @@ import (
7 7
 	"syscall"
8 8
 )
9 9
 
10
+// Self returns the path to the current process's binary.
11
+// Returns "/proc/self/exe".
12
+func Self() string {
13
+	return "/proc/self/exe"
14
+}
15
+
10 16
 // Command returns *exec.Cmd which have Path as current binary. Also it setting
11 17
 // SysProcAttr.Pdeathsig to SIGTERM.
12
-// For example if current binary is "docker" at "/usr/bin", then cmd.Path will
13
-// be set to "/usr/bin/docker".
18
+// This will use the in-memory version (/proc/self/exe) of the current binary,
19
+// it is thus safe to delete or replace the on-disk binary (os.Args[0]).
14 20
 func Command(args ...string) *exec.Cmd {
15 21
 	return &exec.Cmd{
16 22
 		Path: Self(),
... ...
@@ -6,6 +6,12 @@ import (
6 6
 	"os/exec"
7 7
 )
8 8
 
9
+// Self returns the path to the current process's binary.
10
+// Uses os.Args[0].
11
+func Self() string {
12
+	return naiveSelf()
13
+}
14
+
9 15
 // Command returns *exec.Cmd which have Path as current binary.
10 16
 // For example if current binary is "docker.exe" at "C:\", then cmd.Path will
11 17
 // be set to "C:\docker.exe".
... ...
@@ -30,8 +30,7 @@ func Init() bool {
30 30
 	return false
31 31
 }
32 32
 
33
-// Self returns the path to the current processes binary
34
-func Self() string {
33
+func naiveSelf() string {
35 34
 	name := os.Args[0]
36 35
 	if filepath.Base(name) == name {
37 36
 		if lp, err := exec.LookPath(name); err == nil {