Skip to content

Commit ca863cb

Browse files
committed
properly set argv0 when spawning a process
1 parent 96c2526 commit ca863cb

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

pkg/shellexec/shellexec.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"log"
1212
"os"
1313
"os/exec"
14+
"path/filepath"
1415
"runtime"
1516
"strings"
1617
"sync"
@@ -122,6 +123,31 @@ type PipePty struct {
122123
remoteStdoutRead *os.File
123124
}
124125

126+
func setCmdArgv0FromPath(cmd *exec.Cmd) {
127+
if cmd == nil {
128+
return
129+
}
130+
execPath := cmd.Path
131+
if execPath == "" {
132+
if len(cmd.Args) == 0 {
133+
return
134+
}
135+
execPath = cmd.Args[0]
136+
}
137+
if execPath == "" {
138+
return
139+
}
140+
argv0 := filepath.Base(execPath)
141+
if argv0 == "" {
142+
return
143+
}
144+
if len(cmd.Args) == 0 {
145+
cmd.Args = []string{argv0}
146+
return
147+
}
148+
cmd.Args[0] = argv0
149+
}
150+
125151
func (pp *PipePty) Fd() uintptr {
126152
return pp.remoteStdinWrite.Fd()
127153
}
@@ -157,6 +183,7 @@ func StartWslShellProcNoWsh(ctx context.Context, termSize waveobj.TermSize, cmdS
157183
conn.Infof(ctx, "WSL-NEWSESSION (StartWslShellProcNoWsh)")
158184

159185
ecmd := exec.Command("wsl.exe", "~", "-d", client.Name())
186+
setCmdArgv0FromPath(ecmd)
160187

161188
if termSize.Rows == 0 || termSize.Cols == 0 {
162189
termSize.Rows = shellutil.DefaultTermRows
@@ -275,6 +302,7 @@ func StartWslShellProc(ctx context.Context, termSize waveobj.TermSize, cmdStr st
275302
}
276303
log.Printf("full combined command: %s", cmdCombined)
277304
ecmd := exec.Command("wsl.exe", "~", "-d", client.Name(), "--", "sh", "-c", cmdCombined)
305+
setCmdArgv0FromPath(ecmd)
278306
if termSize.Rows == 0 || termSize.Cols == 0 {
279307
termSize.Rows = shellutil.DefaultTermRows
280308
termSize.Cols = shellutil.DefaultTermCols
@@ -619,6 +647,7 @@ func StartLocalShellProc(logCtx context.Context, termSize waveobj.TermSize, cmdS
619647
}
620648
blocklogger.Debugf(logCtx, "[conndebug] shell:%s shellOpts:%v\n", shellPath, shellOpts)
621649
ecmd = exec.Command(shellPath, shellOpts...)
650+
setCmdArgv0FromPath(ecmd)
622651
ecmd.Env = os.Environ()
623652
if shellType == shellutil.ShellType_zsh {
624653
shellutil.UpdateCmdEnv(ecmd, map[string]string{"ZDOTDIR": shellutil.GetLocalZshZDotDir()})
@@ -627,6 +656,7 @@ func StartLocalShellProc(logCtx context.Context, termSize waveobj.TermSize, cmdS
627656
isShell = false
628657
shellOpts = append(shellOpts, "-c", cmdStr)
629658
ecmd = exec.Command(shellPath, shellOpts...)
659+
setCmdArgv0FromPath(ecmd)
630660
ecmd.Env = os.Environ()
631661
}
632662

@@ -693,6 +723,7 @@ func StartLocalShellProc(logCtx context.Context, termSize waveobj.TermSize, cmdS
693723
}
694724

695725
func RunSimpleCmdInPty(ecmd *exec.Cmd, termSize waveobj.TermSize) ([]byte, error) {
726+
setCmdArgv0FromPath(ecmd)
696727
ecmd.Env = os.Environ()
697728
shellutil.UpdateCmdEnv(ecmd, shellutil.WaveshellLocalEnvVars(shellutil.DefaultTermType))
698729
if termSize.Rows == 0 || termSize.Cols == 0 {

0 commit comments

Comments
 (0)