reexec: only consider basename, not path#190
Merged
Conversation
5b7a11c to
6308535
Compare
6308535 to
c6fd8f5
Compare
The intent of reexec is to provide functionality similar to
multi-call binaries (like "busybox"). However, currently, re-exec
also includes path information when matching. This does not match
behavior like busybox;
date --help 2>&1 | grep Usage
Usage: date [OPTIONS] [+FMT] [[-s] TIME]
mkdir foo
ln -s /bin/busybox ./foo/date
./foo/date --help 2>&1 | grep Usage
This patch changes the behavior to only consider the basename, and
disregard path.
With this patch applied:
package main
import (
"fmt"
"os"
"github.com/moby/sys/reexec"
)
func init() {
reexec.Register("hello-foo", func() {
fmt.Println("Hello Foo")
os.Exit(0)
})
reexec.Register("hello-bar", func() {
fmt.Println("Hello Bar")
os.Exit(0)
})
reexec.Init()
}
func main() {
fmt.Println("Hello Main")
}
executing the binary, including path (`./`) now selects the correct
function to execute.
go build -o ./example
ln -s ./example hello-foo
ln -s ./example hello-bar
ln -s ./example hello-baz
./example
# Hello Main
./hello-foo
# Hello Foo
./hello-bar
# Hello Bar
./hello-baz
# Hello Main
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
c6fd8f5 to
de3fb45
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR updates the reexec package to consider only the basename when matching registered commands, aligning its behavior with multi-call binaries like busybox.
- Updated reexec.Register to panic if the name includes a path component.
- Modified reexec.Init to use the basename of os.Args[0].
- Enhanced tests in reexec_test.go to cover duplicate registration and basename versus full path behavior.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| reexec/reexec.go | Enforces basename-only registration and lookup by using filepath.Base. |
| reexec/reexec_test.go | Adds test cases for duplicate names and verifies behavior for full paths. |
Member
Author
vvoland
approved these changes
Jun 24, 2025
Member
Author
|
I'll bring this one in; did a quick check, and I didn't find any place where we call |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The intent of reexec is to provide functionality similar to multi-call binaries (like "busybox"). However, currently, re-exec also includes path information when matching. This does not match behavior like busybox;
This patch changes the behavior to only consider the basename, and disregard path.
With this patch applied:
Basic example:
executing the binary, including path (
./) now selects the correctfunction to execute.
Before this patch, all of the above would fall through to use
maininstead;