Skip to content

reexec: only consider basename, not path#190

Merged
thaJeztah merged 1 commit intomoby:mainfrom
thaJeztah:reexec_no_path
Jun 24, 2025
Merged

reexec: only consider basename, not path#190
thaJeztah merged 1 commit intomoby:mainfrom
thaJeztah:reexec_no_path

Conversation

@thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Jun 23, 2025

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:

Basic example:

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

Before this patch, all of the above would fall through to use main instead;

./example
# Hello Main

./hello-foo
# Hello Main

./hello-bar
# Hello Main

./hello-baz
# Hello Main

@thaJeztah thaJeztah force-pushed the reexec_no_path branch 3 times, most recently from 5b7a11c to 6308535 Compare June 23, 2025 14:17
@thaJeztah thaJeztah marked this pull request as ready for review June 23, 2025 14:17
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>
@thaJeztah thaJeztah requested a review from Copilot June 23, 2025 16:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@thaJeztah
Copy link
Member Author

/cc @tianon @vvoland ptal

@thaJeztah
Copy link
Member Author

I'll bring this one in; did a quick check, and I didn't find any place where we call Register with a command that includes a path (but will open draft PR's before doing a new release - some other changes still coming)

@thaJeztah thaJeztah merged commit e387c1f into moby:main Jun 24, 2025
20 checks passed
@thaJeztah thaJeztah deleted the reexec_no_path branch June 24, 2025 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants