tailscale/tool/gocross/exec_other.go
Aaron Klotz 4bb03609bc tool/gocross: ensure child process error codes are propagated on non-Unix
The Unix implementation of doExec propagates error codes by virtue of
the fact that it does an execve; the replacement binary will return the
exit code.

On non-Unix, we need to simulate these semantics by checking for an
ExitError and, when present, passing that value on to os.Exit.

We also add error handling to the doExec call for the benefit of
handling any errors where doExec fails before being able to execute
the desired binary.

Updates https://github.com/tailscale/corp/issues/29940

Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2025-09-15 12:33:26 -07:00

31 lines
535 B
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
//go:build !unix
package main
import (
"errors"
"os"
"os/exec"
)
func doExec(cmd string, args []string, env []string) error {
c := exec.Command(cmd, args[1:]...)
c.Env = env
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err := c.Run()
// Propagate ExitErrors within this func to give us similar semantics to
// the Unix variant.
var ee *exec.ExitError
if errors.As(err, &ee) {
os.Exit(ee.ExitCode())
}
return err
}