mirror of
https://github.com/fleetdm/fleet
synced 2026-04-28 17:07:43 +00:00
For https://github.com/fleetdm/fleet/issues/9943 This will help us avoid issues like this where the log message never worked right: https://github.com/fleetdm/fleet/pull/28296#discussion_r2047505191 Most of the changes are no-op type changes like removing unneeded typecast or disabling gosec on reviewed lines of code # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [x] Added/updated automated tests - [x] A detailed QA plan exists on the associated ticket (if it isn't there, work with the product group's QA engineer to add it) - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. - [x] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).
39 lines
1.4 KiB
Go
39 lines
1.4 KiB
Go
//go:build windows
|
|
|
|
package scripts
|
|
|
|
import (
|
|
"context"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
func ExecCmd(ctx context.Context, scriptPath string, env []string) (output []byte, exitCode int, err error) {
|
|
// initialize to -1 in case the process never starts
|
|
exitCode = -1
|
|
|
|
// for Windows, we execute the file with powershell.
|
|
cmd := exec.CommandContext(ctx, "powershell", "-MTA", "-ExecutionPolicy", "Bypass", "-File", scriptPath)
|
|
cmd.Env = env
|
|
cmd.Dir = filepath.Dir(scriptPath)
|
|
cmd.WaitDelay = time.Second
|
|
output, err = cmd.CombinedOutput()
|
|
|
|
// we still check if the context was cancelled before setting an exitCode !=
|
|
// -1, as killing a process on Windows is not straightforward (see the
|
|
// WaitDelay documentation) and may have timed out even if exit code is
|
|
// reported as 1, so keep it to -1 in that case so that all user messages are
|
|
// as expected.
|
|
if cmd.ProcessState != nil && ctx.Err() == nil {
|
|
// The windows exit code is a 32-bit unsigned integer, but the
|
|
// interpreter treats it like a signed integer. When a process
|
|
// is killed, it returns 0xFFFFFFFF (interpreted as -1). We
|
|
// convert the integer to an signed 32-bit integer to flip it
|
|
// to a -1 to match our expectations, and fit in our db column.
|
|
//
|
|
// https://en.wikipedia.org/wiki/Exit_status#Windows
|
|
exitCode = int(int32(cmd.ProcessState.ExitCode())) // nolint:gosec
|
|
}
|
|
return output, exitCode, err
|
|
}
|