Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 8 additions & 6 deletions reexec/reexec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
)

func command(args ...string) *exec.Cmd {
return &exec.Cmd{
Path: Self(),
Args: args,
SysProcAttr: &syscall.SysProcAttr{
Pdeathsig: syscall.SIGTERM,
},
// We try to stay close to exec.Command's behavior, but after
// constructing the cmd, we remove "Self()" from cmd.Args, which
// is prepended by exec.Command.
cmd := exec.Command(Self(), args...)
cmd.Args = cmd.Args[1:]
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGTERM,
}
return cmd
}
10 changes: 6 additions & 4 deletions reexec/reexec_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
)

func command(args ...string) *exec.Cmd {
return &exec.Cmd{
Path: Self(),
Args: args,
}
// We try to stay close to exec.Command's behavior, but after
// constructing the cmd, we remove "Self()" from cmd.Args, which
// is prepended by exec.Command.
cmd := exec.Command(Self(), args...)
Comment thread
thaJeztah marked this conversation as resolved.
cmd.Args = cmd.Args[1:]
Comment thread
thaJeztah marked this conversation as resolved.
return cmd
}
37 changes: 26 additions & 11 deletions reexec/reexec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
)
Expand All @@ -19,7 +20,11 @@ func init() {
panic("Return Error")
})
Register(testReExec2, func() {
fmt.Println("Hello " + testReExec2)
var args string
if len(os.Args) > 1 {
args = fmt.Sprintf("(args: %#v)", os.Args[1:])
}
fmt.Println("Hello", testReExec2, args)
os.Exit(0)
})
Init()
Expand Down Expand Up @@ -62,21 +67,32 @@ func TestRegister(t *testing.T) {

func TestCommand(t *testing.T) {
tests := []struct {
doc string
name string
doc string
cmdAndArgs []string
expOut string
}{
{
doc: "basename",
name: testReExec2,
doc: "basename",
cmdAndArgs: []string{testReExec2},
expOut: "Hello test-reexec2",
},
{
doc: "full path",
name: filepath.Join("something", testReExec2),
doc: "full path",
cmdAndArgs: []string{filepath.Join("something", testReExec2)},
expOut: `Hello test-reexec2`,
},
{
doc: "command with args",
cmdAndArgs: []string{testReExec2, "--some-flag", "some-value", "arg1", "arg2"},
expOut: `Hello test-reexec2 (args: []string{"--some-flag", "some-value", "arg1", "arg2"})`,
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
cmd := Command(tc.name)
cmd := Command(tc.cmdAndArgs...)
if !reflect.DeepEqual(cmd.Args, tc.cmdAndArgs) {
t.Fatalf("got %+v, want %+v", cmd.Args, tc.cmdAndArgs)
}
w, err := cmd.StdinPipe()
if err != nil {
t.Fatalf("Error on pipe creation: %v", err)
Expand All @@ -88,10 +104,9 @@ func TestCommand(t *testing.T) {
t.Errorf("Error on re-exec cmd: %v, out: %v", err, string(out))
}

const expected = "Hello " + testReExec2
actual := strings.TrimSpace(string(out))
if actual != expected {
t.Errorf("got %v, want %v", actual, expected)
if actual != tc.expOut {
t.Errorf("got %v, want %v", actual, tc.expOut)
}
})
}
Expand Down