Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/reexec: cleanup and remove some dependencies #47932

Merged
merged 5 commits into from
Jun 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pkg/reexec: unify implementation of Self() and remove stub
This combines the implementations of the Self function, to allow having
a single GoDoc to document the behavior. The naiveSelf function is kept,
because it's used in unit-tests.

There is a minor change in behavior, as this patch removes the stub for
unsupported platforms (non-linux, windows, freebsd or darwin), which will
now use `os.Args[0]`. The stub was added in 21537b8
to fix compilation of https://github.com/ethereum/go-ethereum on OpenBSD,
which had docker/docker as dependency. It looks like that repository no
longer has this dependency, and as this was only to make the code
compilable, is unlikely to be a problem.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
thaJeztah committed Jun 8, 2024
commit 004451c812c38313577d868f2313c59cf12117b4
6 changes: 0 additions & 6 deletions pkg/reexec/command_linux.go
Original file line number Diff line number Diff line change
@@ -5,12 +5,6 @@ import (
"syscall"
)

// Self returns the path to the current process's binary.
// Returns "/proc/self/exe".
func Self() string {
return "/proc/self/exe"
}

// Command returns *exec.Cmd which has Path as current binary. Also it setting
// SysProcAttr.Pdeathsig to SIGTERM.
// This will use the in-memory version (/proc/self/exe) of the current binary,
6 changes: 0 additions & 6 deletions pkg/reexec/command_unix.go
Original file line number Diff line number Diff line change
@@ -6,12 +6,6 @@ import (
"os/exec"
)

// Self returns the path to the current process's binary.
// Uses os.Args[0].
func Self() string {
return naiveSelf()
}

// Command returns *exec.Cmd which has Path as current binary.
// For example if current binary is "docker" at "/usr/bin/", then cmd.Path will
// be set to "/usr/bin/docker".
4 changes: 0 additions & 4 deletions pkg/reexec/command_unsupported.go
Original file line number Diff line number Diff line change
@@ -6,10 +6,6 @@ import (
"os/exec"
)

func Self() string {
return ""
}

// Command is unsupported on operating systems apart from Linux, Windows, and Darwin.
func Command(args ...string) *exec.Cmd {
return nil
6 changes: 0 additions & 6 deletions pkg/reexec/command_windows.go
Original file line number Diff line number Diff line change
@@ -4,12 +4,6 @@ import (
"os/exec"
)

// Self returns the path to the current process's binary.
// Uses os.Args[0].
func Self() string {
return naiveSelf()
}

// Command returns *exec.Cmd which has Path as current binary.
// For example if current binary is "docker.exe" at "C:\", then cmd.Path will
// be set to "C:\docker.exe".
12 changes: 12 additions & 0 deletions pkg/reexec/reexec.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
)

var registeredInitializers = make(map[string]func())
@@ -34,6 +35,17 @@ func Init() bool {
return false
}

// Self returns the path to the current process's binary. On Linux, it
// returns "/proc/self/exe", which provides the in-memory version of the
// current binary, whereas on other platforms it attempts to looks up the
// absolute path for os.Args[0], or otherwise returns os.Args[0] as-is.
func Self() string {
if runtime.GOOS == "linux" {
return "/proc/self/exe"
}
return naiveSelf()
}

func naiveSelf() string {
name := os.Args[0]
if filepath.Base(name) == name {