-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec_unix.go
More file actions
36 lines (32 loc) · 1.4 KB
/
Copy pathexec_unix.go
File metadata and controls
36 lines (32 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// "unix" is a build tag rather than a GOOS, so the _unix filename suffix constrains nothing.
// Without it this file builds everywhere, and collides with exec_other.go.
//go:build unix
package main
import (
"fmt"
"os/exec"
"syscall"
"github.com/fgm/envrun/env"
)
// run replaces the envrun process with the command, and returns only if it cannot.
//
// There is no envrun left after execve: the command keeps the same PID,
// and its signals, exit status, standard streams and controlling terminal are its own,
// exactly as if it had read the environment file itself.
// That is what closes #35 and #1,
// and why the only statuses reachable here are envrun's own 125/126/127.
// The reasoning, and the options declined, are in ADR-001.
func run(e env.Vars, opaque []string, name string, args []string) int {
// LookPath rather than letting execve resolve the name:
// only Go's own lookup distinguishes "not found" from "found but not executable",
// which is what 127 and 126 report.
path, err := exec.LookPath(name)
// Inverted deliberately: syscall.Exec returns only on failure,
// so there is no happy path to end on, and err is non-nil at every line below.
if err == nil {
// argv[0] is the name as given, not the resolved path, as exec.Command passes it.
err = syscall.Exec(path, append([]string{name}, args...), e.Environ(opaque))
}
fail(fmt.Errorf("running %s: %w", name, err))
return exitStatus(err)
}