Skip to content

Commit cc1d50a

Browse files
authored
Merge pull request #49118 from thaJeztah/reexec_clean
pkg/reexec: some cleaning up in preparation of moving to a separate module
2 parents f237ba0 + 8fd177d commit cc1d50a

7 files changed

Lines changed: 60 additions & 66 deletions

File tree

pkg/reexec/command_linux.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

pkg/reexec/command_other.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

pkg/reexec/command_unsupported.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

pkg/reexec/reexec.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Handlers can be registered with a name and the argv 0 of the exec of
44
// the binary will be used to find and execute custom init paths.
55
//
6-
// It is used in dockerd to work around forking limitations when using Go.
6+
// It is used to work around forking limitations when using Go.
77
package reexec
88

99
import (
@@ -36,10 +36,29 @@ func Init() bool {
3636
return false
3737
}
3838

39-
// Self returns the path to the current process's binary. On Linux, it
40-
// returns "/proc/self/exe", which provides the in-memory version of the
41-
// current binary, whereas on other platforms it attempts to looks up the
42-
// absolute path for os.Args[0], or otherwise returns os.Args[0] as-is.
39+
// Command returns an [*exec.Cmd] with its Path set to the path of the current
40+
// binary using the result of [Self].
41+
//
42+
// On Linux, the Pdeathsig of [*exec.Cmd.SysProcAttr] is set to SIGTERM.
43+
// This signal is sent to the process when the OS thread that created
44+
// the process dies.
45+
//
46+
// It is the caller's responsibility to ensure that the creating thread is
47+
// not terminated prematurely. See https://go.dev/issue/27505 for more details.
48+
func Command(args ...string) *exec.Cmd {
49+
return command(args...)
50+
}
51+
52+
// Self returns the path to the current process's binary.
53+
//
54+
// On Linux, it returns "/proc/self/exe", which provides the in-memory version
55+
// of the current binary. This makes it safe to delete or replace the on-disk
56+
// binary (os.Args[0]).
57+
//
58+
// On Other platforms, it attempts to look up the absolute path for os.Args[0],
59+
// or otherwise returns os.Args[0] as-is. For example if current binary is
60+
// "my-binary" at "/usr/bin/" (or "my-binary.exe" at "C:\" on Windows),
61+
// then it returns "/usr/bin/my-binary" and "C:\my-binary.exe" respectively.
4362
func Self() string {
4463
if runtime.GOOS == "linux" {
4564
return "/proc/self/exe"

pkg/reexec/reexec_linux.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package reexec
2+
3+
import (
4+
"os/exec"
5+
"syscall"
6+
)
7+
8+
func command(args ...string) *exec.Cmd {
9+
return &exec.Cmd{
10+
Path: Self(),
11+
Args: args,
12+
SysProcAttr: &syscall.SysProcAttr{
13+
Pdeathsig: syscall.SIGTERM,
14+
},
15+
}
16+
}

pkg/reexec/reexec_other.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build !linux
2+
3+
package reexec
4+
5+
import (
6+
"os/exec"
7+
)
8+
9+
func command(args ...string) *exec.Cmd {
10+
return &exec.Cmd{
11+
Path: Self(),
12+
Args: args,
13+
}
14+
}

pkg/reexec/reexec_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"testing"
77
)
88

9+
const testReExec = "test-reexec"
10+
911
func init() {
10-
Register("reexec", func() {
12+
Register(testReExec, func() {
1113
panic("Return Error")
1214
})
1315
Init()
@@ -16,17 +18,17 @@ func init() {
1618
func TestRegister(t *testing.T) {
1719
defer func() {
1820
if r := recover(); r != nil {
19-
const expected = `reexec func already registered under name "reexec"`
21+
const expected = `reexec func already registered under name "test-reexec"`
2022
if r != expected {
2123
t.Errorf("got %q, want %q", r, expected)
2224
}
2325
}
2426
}()
25-
Register("reexec", func() {})
27+
Register(testReExec, func() {})
2628
}
2729

2830
func TestCommand(t *testing.T) {
29-
cmd := Command("reexec")
31+
cmd := Command(testReExec)
3032
w, err := cmd.StdinPipe()
3133
if err != nil {
3234
t.Fatalf("Error on pipe creation: %v", err)

0 commit comments

Comments
 (0)