diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bce11c9da..ba103fb61 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -178,6 +178,16 @@ jobs: TAGS=$(go -C ../../scripts/profile-tags run . local) go build -tags "${TAGS}" ./... + # agentop builds on Windows (#1520). Most of its tests drive a Unix shell, a service + # manager or signals, so only the tests of the Windows-specific code and of commands + # that work there run here for now. + - name: Build, vet and test cmd/agentop + working-directory: cmd/agentop + run: | + go build ./... + go vet ./... + go test -run 'TestRefuseUnsupported|TestProcessAlive|TestLockFile|TestWritable|TestTerminateRefusesOnWindows|TestNoRecordWanted|TestWithRecordOff|TestAddRecordOff' . + # The cpex plugin's FFI-backed code sits behind the `cpex` build tag and needs # cgo plus the pinned libcpex_ffi.a, so no other job compiles or tests it. It # runs in Alpine to link the same musl asset the authbridge-cpex image does, diff --git a/cmd/agentop/cmd_bob_launch.go b/cmd/agentop/cmd_bob_launch.go index 98474b95c..f4208488c 100644 --- a/cmd/agentop/cmd_bob_launch.go +++ b/cmd/agentop/cmd_bob_launch.go @@ -10,7 +10,6 @@ import ( "path/filepath" "runtime" "strings" - "syscall" "time" ) @@ -217,7 +216,7 @@ var bobProbeLaunch = func(shell string) (string, error) { env = append(env, kv) } cmd.Env = append(env, "VSCODE_RESOLVING_ENVIRONMENT=1", "__CFBundleIdentifier="+bobBundleID) - cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} + cmd.SysProcAttr = detached() out, err := cmd.Output() if ctx.Err() != nil { return "", fmt.Errorf("%s did not finish within %s", shell, bobProbeTimeout) diff --git a/cmd/agentop/cmd_claudecode_test.go b/cmd/agentop/cmd_claudecode_test.go index 7a361dfed..d707a36cc 100644 --- a/cmd/agentop/cmd_claudecode_test.go +++ b/cmd/agentop/cmd_claudecode_test.go @@ -35,7 +35,7 @@ listener: health_addr: 127.0.0.1:47604 tls_bridge: mode: enabled - ca_dir: "CADIR" + ca_dir: 'CADIR' generate_ca: true pipeline: outbound: diff --git a/cmd/agentop/cmd_config_migrate_pricing_test.go b/cmd/agentop/cmd_config_migrate_pricing_test.go index 2aa3cb1f5..c6c3218af 100644 --- a/cmd/agentop/cmd_config_migrate_pricing_test.go +++ b/cmd/agentop/cmd_config_migrate_pricing_test.go @@ -28,7 +28,7 @@ stats: address: 127.0.0.1:47602 tls_bridge: mode: enabled - ca_dir: "CADIR" + ca_dir: 'CADIR' generate_ca: true pipeline: outbound: diff --git a/cmd/agentop/cmd_config_migrate_test.go b/cmd/agentop/cmd_config_migrate_test.go index 17235e48f..7b9db47da 100644 --- a/cmd/agentop/cmd_config_migrate_test.go +++ b/cmd/agentop/cmd_config_migrate_test.go @@ -20,7 +20,7 @@ stats: address: 127.0.0.1:47602 tls_bridge: mode: enabled - ca_dir: "CADIR" + ca_dir: 'CADIR' generate_ca: true pipeline: outbound: @@ -42,7 +42,7 @@ const indentedConfig = ` mode: proxy-sidecar address: 127.0.0.1:47602 tls_bridge: mode: enabled - ca_dir: "CADIR" + ca_dir: 'CADIR' generate_ca: true pipeline: outbound: diff --git a/cmd/agentop/cmd_exec_test.go b/cmd/agentop/cmd_exec_test.go index 7ff95a736..25ed0da67 100644 --- a/cmd/agentop/cmd_exec_test.go +++ b/cmd/agentop/cmd_exec_test.go @@ -597,7 +597,7 @@ func TestRunExec_RelaysSIGTERM(t *testing.T) { // Signal THIS process, the way `timeout` signals agentop. runExec's relay must // pass it to the child. - if err := syscall.Kill(syscall.Getpid(), syscall.SIGTERM); err != nil { + if err := killPID(syscall.Getpid(), syscall.SIGTERM); err != nil { t.Fatal(err) } @@ -1073,10 +1073,10 @@ func TestRunExec_SurvivesSIGINT(t *testing.T) { // Signal THIS process, as a tty would. If runExec did not catch SIGINT, the // whole test binary would die here rather than one test failing. - if err := syscall.Kill(syscall.Getpid(), syscall.SIGINT); err != nil { + if err := killPID(syscall.Getpid(), syscall.SIGINT); err != nil { t.Fatal(err) } - if err := syscall.Kill(child, syscall.SIGTERM); err != nil { + if err := killPID(child, syscall.SIGTERM); err != nil { t.Fatal(err) } diff --git a/cmd/agentop/cmd_observe_test.go b/cmd/agentop/cmd_observe_test.go index 0e6cdc48e..89c8c1619 100644 --- a/cmd/agentop/cmd_observe_test.go +++ b/cmd/agentop/cmd_observe_test.go @@ -9,7 +9,6 @@ import ( "os/exec" "path/filepath" "strings" - "syscall" "testing" "github.com/rossoctl/cortex/cmd/agentop/tui" @@ -526,7 +525,7 @@ func runAgentopChild(t *testing.T, args ...string) (string, error) { // Not every argv exits on its own: one that reaches tui.Run opens the terminal. In // a session of its own the child has no controlling terminal, so that open fails // and the child exits. - cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} + cmd.SysProcAttr = sessionAttr() out, err := cmd.CombinedOutput() return string(out), err } diff --git a/cmd/agentop/cmd_service_launchd_integration_test.go b/cmd/agentop/cmd_service_launchd_integration_test.go index 5edb81ffc..bae221a66 100644 --- a/cmd/agentop/cmd_service_launchd_integration_test.go +++ b/cmd/agentop/cmd_service_launchd_integration_test.go @@ -260,7 +260,7 @@ func TestServiceLifecycle_RealLaunchd(t *testing.T) { // pid is the supervisor's, and it stays the same while a new proxy takes over. // syscall.Kill gets a pid from childPID, which never returns one below 2: kill(2) // reads 0 as this process's own group, which holds `go test` too. - if err := syscall.Kill(proxy, syscall.SIGKILL); err != nil { + if err := killPID(proxy, syscall.SIGKILL); err != nil { t.Fatalf("kill -9 proxy %d: %v", proxy, err) } // The wait follows whatever supervisor launchd reports now, so that a broken @@ -293,7 +293,7 @@ func TestServiceLifecycle_RealLaunchd(t *testing.T) { t.Error("after stop the label is not disabled, so the next login would start Cortex again") } for _, pid := range []int{sup, newProxy} { - if !waitUntil(10*time.Second, func() bool { return syscall.Kill(pid, 0) != nil }) { + if !waitUntil(10*time.Second, func() bool { return killPID(pid, 0) != nil }) { t.Errorf("process %d was still running 10s after stop", pid) } } diff --git a/cmd/agentop/cmd_service_platform.go b/cmd/agentop/cmd_service_platform.go index 6bc08b90c..ede86244b 100644 --- a/cmd/agentop/cmd_service_platform.go +++ b/cmd/agentop/cmd_service_platform.go @@ -19,7 +19,6 @@ import ( "runtime" "strconv" "strings" - "syscall" "time" ) @@ -578,11 +577,11 @@ func resolvePath(p string) (string, error) { // before giving up rather than reporting success on a process that still holds the // ports. A current one is gone in well under a second. func stopPID(pid int) error { - if err := syscall.Kill(pid, syscall.SIGTERM); err != nil { + if err := terminate(pid); err != nil { return err } for i := 0; i < 90; i++ { - if err := syscall.Kill(pid, 0); err != nil { + if !processAlive(pid) { return nil } time.Sleep(200 * time.Millisecond) diff --git a/cmd/agentop/cmd_service_systemd_integration_test.go b/cmd/agentop/cmd_service_systemd_integration_test.go index ecc58c858..c27f3b4b3 100644 --- a/cmd/agentop/cmd_service_systemd_integration_test.go +++ b/cmd/agentop/cmd_service_systemd_integration_test.go @@ -239,7 +239,7 @@ func TestSupervisorRestartsAfterCrash_RealSystemd(t *testing.T) { // on a slim image missing /bin/kill, that would surface as a failed test with // AGENTOP_SYSTEMD_TESTS=required set, rather than the environment-problem skip it // actually is. - if err := syscall.Kill(pid, syscall.SIGKILL); err != nil { + if err := killPID(pid, syscall.SIGKILL); err != nil { t.Fatalf("kill -9 %d: %v", pid, err) } @@ -506,7 +506,7 @@ func TestServiceLifecycle_RealSystemd(t *testing.T) { if !ok { t.Fatalf("no MainPID to kill; systemd says %+v", unit()) } - if err := syscall.Kill(pid, syscall.SIGKILL); err != nil { + if err := killPID(pid, syscall.SIGKILL); err != nil { t.Fatalf("kill -9 %d: %v", pid, err) } if !waitUntil(3*time.Second, func() bool { return unit().sub == "auto-restart" }) { diff --git a/cmd/agentop/cmd_setup_test.go b/cmd/agentop/cmd_setup_test.go index bf85d2e6e..3b9d4bc84 100644 --- a/cmd/agentop/cmd_setup_test.go +++ b/cmd/agentop/cmd_setup_test.go @@ -155,7 +155,7 @@ func TestSetupSignalsCatchesInterruptAndTerminate(t *testing.T) { defer signal.Stop(guard) sigs, stop := setupSignals() for _, s := range []syscall.Signal{syscall.SIGINT, syscall.SIGTERM} { - if err := syscall.Kill(os.Getpid(), s); err != nil { + if err := killPID(os.Getpid(), s); err != nil { t.Fatal(err) } <-guard @@ -167,7 +167,7 @@ func TestSetupSignalsCatchesInterruptAndTerminate(t *testing.T) { // os/signal hands each signal to every subscriber before it takes the next, so // once guard has the second, any send of the first to sigs has happened. for _, s := range []syscall.Signal{syscall.SIGTERM, syscall.SIGINT} { - if err := syscall.Kill(os.Getpid(), s); err != nil { + if err := killPID(os.Getpid(), s); err != nil { t.Fatal(err) } <-guard diff --git a/cmd/agentop/cmd_uninstall_test.go b/cmd/agentop/cmd_uninstall_test.go index fc773cfdd..f10a9711a 100644 --- a/cmd/agentop/cmd_uninstall_test.go +++ b/cmd/agentop/cmd_uninstall_test.go @@ -1312,7 +1312,7 @@ func TestUninstallARealSigintStillShowsTheCursor(t *testing.T) { run: func(*checklist.Running) (string, []string, error) { fn(); return "done", nil, nil }} } return []removal{step("first", func() {}), step("second", func() { - _ = syscall.Kill(os.Getpid(), syscall.SIGINT) + _ = killPID(os.Getpid(), syscall.SIGINT) <-arrived }), step("third", func() {})} } diff --git a/cmd/agentop/cmd_update_auto.go b/cmd/agentop/cmd_update_auto.go index e966a1d53..45d332994 100644 --- a/cmd/agentop/cmd_update_auto.go +++ b/cmd/agentop/cmd_update_auto.go @@ -9,7 +9,6 @@ import ( "os" "path/filepath" "strings" - "syscall" "time" "github.com/rossoctl/cortex/cmd/agentop/updatestatus" @@ -52,12 +51,9 @@ func lockUpdate(cortexDir string) (unlock func(), held bool, err error) { if err != nil { return nil, false, err } - if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil { + if held, err := lockFile(f); held || err != nil { _ = f.Close() - if errors.Is(err, syscall.EWOULDBLOCK) { - return nil, true, nil - } - return nil, false, err + return nil, held, err } return func() { _ = f.Close() }, false, nil } diff --git a/cmd/agentop/cmd_update_test.go b/cmd/agentop/cmd_update_test.go index db84a10fc..bb89ab0e9 100644 --- a/cmd/agentop/cmd_update_test.go +++ b/cmd/agentop/cmd_update_test.go @@ -487,7 +487,7 @@ func TestUpdateScriptExitsWithTheInstallersStatusOnInterrupt(t *testing.T) { installStub(t, "sh", "#!"+dash+"\nexec "+dash+" \"$@\"\n") installStub(t, "curl", "#!/bin/sh\nprintf '%s\\n' \"trap 'exit 3' INT\" 'echo ready' 'sleep 5 >/dev/null & wait'\n") cmd := exec.Command("sh", "-c", updateScript, installerURL) - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + cmd.SysProcAttr = groupAttr() out, err := cmd.StdoutPipe() if err != nil { t.Fatal(err) @@ -498,7 +498,7 @@ func TestUpdateScriptExitsWithTheInstallersStatusOnInterrupt(t *testing.T) { if line, _ := bufio.NewReader(out).ReadString('\n'); line != "ready\n" { t.Fatalf("installer said %q", line) } - _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT) + _ = killPID(-cmd.Process.Pid, syscall.SIGINT) _ = cmd.Wait() if code := cmd.ProcessState.ExitCode(); code != 3 { t.Errorf("exit %d, want the installer's 3", code) diff --git a/cmd/agentop/codex_daemon.go b/cmd/agentop/codex_daemon.go index 6ac70643f..7d3e83948 100644 --- a/cmd/agentop/codex_daemon.go +++ b/cmd/agentop/codex_daemon.go @@ -141,7 +141,7 @@ var codexStopUpdater = func(codexHome string) error { if codexIsRecorded(u) != codexRecordMatches { return nil } - if err := syscall.Kill(-u.PID, syscall.SIGTERM); err != nil && !errors.Is(err, syscall.ESRCH) { + if err := terminateGroup(u.PID); err != nil { return fmt.Errorf("stopping Codex's updater (pid %d): %w", u.PID, err) } deadline := time.Now().Add(codexUpdaterStopTimeout) diff --git a/cmd/agentop/codex_daemon_test.go b/cmd/agentop/codex_daemon_test.go index 8625b2df3..bf9f1e8f0 100644 --- a/cmd/agentop/codex_daemon_test.go +++ b/cmd/agentop/codex_daemon_test.go @@ -683,7 +683,7 @@ func TestCodexRun(t *testing.T) { b, _ := os.ReadFile(pids) //nolint:gosec // the test's own file for _, f := range strings.Fields(string(b)) { if pid, err := strconv.Atoi(f); err == nil { - _ = syscall.Kill(pid, syscall.SIGKILL) + _ = killPID(pid, syscall.SIGKILL) } } }) @@ -703,13 +703,13 @@ func TestCodexRun(t *testing.T) { func startGroupLeader(t *testing.T) (*exec.Cmd, <-chan struct{}) { t.Helper() cmd := exec.Command("/bin/sh", "-c", "sleep 30") - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + cmd.SysProcAttr = groupAttr() if err := cmd.Start(); err != nil { t.Fatal(err) } exited := make(chan struct{}) go func() { _ = cmd.Wait(); close(exited) }() - t.Cleanup(func() { _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); <-exited }) + t.Cleanup(func() { _ = killPID(-cmd.Process.Pid, syscall.SIGKILL); <-exited }) prev := codexDaemonAlive t.Cleanup(func() { codexDaemonAlive = prev }) codexDaemonAlive = func(pid int) bool { diff --git a/cmd/agentop/killpid_unix_test.go b/cmd/agentop/killpid_unix_test.go new file mode 100644 index 000000000..d49d30e00 --- /dev/null +++ b/cmd/agentop/killpid_unix_test.go @@ -0,0 +1,26 @@ +//go:build !windows + +package main + +import ( + "syscall" + + "golang.org/x/sys/unix" +) + +// Process helpers for tests, for Unix. killpid_windows_test.go has the Windows half (#1520). + +// killPID sends sig to pid (to the group led by -pid when pid is negative). +func killPID(pid int, sig syscall.Signal) error { return syscall.Kill(pid, sig) } + +// groupAttr starts a child as the leader of a process group of its own. +func groupAttr() *syscall.SysProcAttr { return &syscall.SysProcAttr{Setpgid: true} } + +// sessionAttr starts a child in a session of its own. +func sessionAttr() *syscall.SysProcAttr { return &syscall.SysProcAttr{Setsid: true} } + +// mkfifo makes a named pipe at path. +func mkfifo(path string, mode uint32) error { return syscall.Mkfifo(path, mode) } + +// getsid is pid's session id. +func getsid(pid int) (int, error) { return unix.Getsid(pid) } diff --git a/cmd/agentop/killpid_windows_test.go b/cmd/agentop/killpid_windows_test.go new file mode 100644 index 000000000..83ecf13df --- /dev/null +++ b/cmd/agentop/killpid_windows_test.go @@ -0,0 +1,24 @@ +//go:build windows + +package main + +import ( + "errors" + "syscall" +) + +// The Windows half of killpid_unix_test.go. Windows has no signals to send to another process, +// no process groups of the Unix kind and no sessions. The tests that use these need a Unix +// service or shell and do not run on Windows; these keep them compiling there (#1520). + +var errNoUnixProcess = errors.New("not on Windows") + +func killPID(int, syscall.Signal) error { return errNoUnixProcess } + +func groupAttr() *syscall.SysProcAttr { return nil } + +func sessionAttr() *syscall.SysProcAttr { return nil } + +func getsid(int) (int, error) { return 0, errNoUnixProcess } + +func mkfifo(string, uint32) error { return errNoUnixProcess } diff --git a/cmd/agentop/main.go b/cmd/agentop/main.go index 04bbf7d7e..4b1334783 100644 --- a/cmd/agentop/main.go +++ b/cmd/agentop/main.go @@ -22,6 +22,7 @@ import ( "os/exec" "os/signal" "path/filepath" + "runtime" "strings" "syscall" @@ -129,6 +130,9 @@ func main() { // argument selects a subcommand, and anything else falls through to the // terminal UI, preserving the original flags-only invocation. if len(os.Args) > 1 && !strings.HasPrefix(os.Args[1], "-") { + if refuseUnsupported(runtime.GOOS, os.Args[1], os.Stderr) { + os.Exit(1) + } switch os.Args[1] { case "tools": os.Exit(runTools(os.Args[2:], os.Stdout, os.Stderr)) diff --git a/cmd/agentop/platform_guard.go b/cmd/agentop/platform_guard.go new file mode 100644 index 000000000..66ec1085a --- /dev/null +++ b/cmd/agentop/platform_guard.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "io" +) + +// unsupportedOnWindows are the subcommands that install, run or replace Cortex as a service. +// On Windows they need a service backend, a way to stop the proxy without killing it, and a +// way to replace a running .exe, none of which exists yet (#1520). Everything else in agentop +// talks to a running proxy over HTTP, or edits a settings file, and works there. +var unsupportedOnWindows = map[string]bool{ + "service": true, + "setup": true, + "uninstall": true, + "update": true, +} + +// refuseUnsupported reports whether subcommand cmd cannot run on goos, and says so on stderr. +// goos is a parameter so the rule is testable on every OS. +func refuseUnsupported(goos, cmd string, stderr io.Writer) bool { + if goos != "windows" || !unsupportedOnWindows[cmd] { + return false + } + fmt.Fprintf(stderr, "agentop: `agentop %s` is not supported on Windows yet (#1520).\n"+ + " Run the proxy in a terminal instead: cortex --local\n", cmd) + return true +} diff --git a/cmd/agentop/platform_guard_test.go b/cmd/agentop/platform_guard_test.go new file mode 100644 index 000000000..bcb1d8616 --- /dev/null +++ b/cmd/agentop/platform_guard_test.go @@ -0,0 +1,116 @@ +package main + +import ( + "bytes" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" + "time" +) + +// On Windows, the subcommands that install, run or replace Cortex refuse with a message naming +// #1520; every other subcommand, and every subcommand elsewhere, is let through. +func TestRefuseUnsupported(t *testing.T) { + for _, cmd := range []string{"service", "setup", "uninstall", "update"} { + var stderr bytes.Buffer + if !refuseUnsupported("windows", cmd, &stderr) || !strings.Contains(stderr.String(), "#1520") { + t.Errorf("windows %s: not refused (stderr %q)", cmd, stderr.String()) + } + if refuseUnsupported("linux", cmd, &stderr) || refuseUnsupported("darwin", cmd, &stderr) { + t.Errorf("%s refused off Windows", cmd) + } + } + for _, cmd := range []string{"observe", "exec", "cost", "configure", "doctor", "export"} { + if refuseUnsupported("windows", cmd, &bytes.Buffer{}) { + t.Errorf("windows %s refused", cmd) + } + } +} + +// processAlive is true for this process, and false for a child that has exited and for pids +// that name nothing. +func TestProcessAlive(t *testing.T) { + if !processAlive(os.Getpid()) { + t.Fatal("this process is not alive") + } + if processAlive(0) || processAlive(-1) { + t.Fatal("pid 0 or -1 is alive") + } + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } + // The test binary itself, asked to run no test: it starts and exits at once on every OS. + cmd := exec.Command(exe, "-test.run=^$") + if err := cmd.Run(); err != nil { + t.Fatalf("run the child: %v", err) + } + pid := cmd.Process.Pid + for deadline := time.Now().Add(5 * time.Second); processAlive(pid); { + if time.Now().After(deadline) { + t.Fatalf("pid %d of an exited child is still alive", pid) + } + time.Sleep(20 * time.Millisecond) + } +} + +// lockFile: the first lock is taken, a second one on the same file is held. +func TestLockFile(t *testing.T) { + p := filepath.Join(t.TempDir(), "update.lock") + open := func() *os.File { + f, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = f.Close() }) + return f + } + if held, err := lockFile(open()); held || err != nil { + t.Fatalf("first lock: held %v, err %v", held, err) + } + if held, err := lockFile(open()); !held || err != nil { + t.Fatalf("second lock: held %v, err %v; want held", held, err) + } +} + +// writable: a temp dir is, a missing dir is not, and nothing is left behind. +func TestWritable(t *testing.T) { + dir := t.TempDir() + if err := writable(dir); err != nil { + t.Fatalf("writable(%s) = %v", dir, err) + } + if entries, _ := os.ReadDir(dir); len(entries) != 0 { + t.Fatalf("writable left %d entries in %s", len(entries), dir) + } + if err := writable(filepath.Join(dir, "missing")); err == nil { + t.Fatal("a missing dir is writable") + } + // A file (setup checks the shell profile this way): writable, and left unchanged. + f := filepath.Join(dir, ".zshrc") + if err := os.WriteFile(f, []byte("export A=1\n"), 0o600); err != nil { + t.Fatal(err) + } + if err := writable(f); err != nil { + t.Fatalf("writable(%s) = %v for a writable file", f, err) + } + if b, _ := os.ReadFile(f); string(b) != "export A=1\n" { + t.Fatalf("writable changed the file: %q", b) + } +} + +// On Windows, stopping a process refuses rather than killing it: there is no SIGTERM, and a +// killed proxy skips its flushes. +func TestTerminateRefusesOnWindows(t *testing.T) { + if runtime.GOOS != "windows" { + t.Skip("the Windows half") + } + if err := terminate(os.Getpid()); err == nil { + t.Fatal("terminate did not refuse") + } + if err := terminateGroup(os.Getpid()); err == nil { + t.Fatal("terminateGroup did not refuse") + } +} diff --git a/cmd/agentop/proc_unix.go b/cmd/agentop/proc_unix.go new file mode 100644 index 000000000..192709187 --- /dev/null +++ b/cmd/agentop/proc_unix.go @@ -0,0 +1,49 @@ +//go:build !windows + +package main + +import ( + "errors" + "os" + "syscall" +) + +// The process and file primitives agentop needs, for Unix. proc_windows.go has the Windows +// half (#1520). + +// detached starts a child in a session of its own, so it outlives agentop's terminal. +func detached() *syscall.SysProcAttr { return &syscall.SysProcAttr{Setsid: true} } + +// processAlive reports whether pid names a live process (kill -0). +func processAlive(pid int) bool { return pid > 0 && syscall.Kill(pid, 0) == nil } + +// terminate asks pid to stop (SIGTERM). +func terminate(pid int) error { return syscall.Kill(pid, syscall.SIGTERM) } + +// terminateGroup asks the process group led by pid to stop. A group already gone is not an +// error. +func terminateGroup(pid int) error { + if err := syscall.Kill(-pid, syscall.SIGTERM); err != nil && !errors.Is(err, syscall.ESRCH) { + return err + } + return nil +} + +// lockFile takes an exclusive lock on f without waiting. held is true when another process +// has it. +func lockFile(f *os.File) (held bool, err error) { + if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil { + if errors.Is(err, syscall.EWOULDBLOCK) { + return true, nil + } + return false, err + } + return false, nil +} + +// writable reports whether this process may write path: a file, or a directory to create files +// in. +func writable(path string) error { + const wOK = 0x2 + return syscall.Access(path, wOK) +} diff --git a/cmd/agentop/proc_windows.go b/cmd/agentop/proc_windows.go new file mode 100644 index 000000000..f89e382d3 --- /dev/null +++ b/cmd/agentop/proc_windows.go @@ -0,0 +1,87 @@ +//go:build windows + +package main + +import ( + "errors" + "os" + "syscall" + + "golang.org/x/sys/windows" +) + +// The Windows half of proc_unix.go (#1520). + +// errNoGracefulStop is what stopping another process returns on Windows. Windows has no +// SIGTERM: Go's Process.Signal can only kill, and a killed proxy skips its plugin stops and its +// ledger and archive flushes. Until the proxy has a stop path of its own, agentop refuses +// rather than kill it. +var errNoGracefulStop = errors.New("stopping a process gracefully is not supported on Windows yet (#1520)") + +// detached starts a child in a process group of its own, with no console, so it outlives +// agentop's console window. +func detached() *syscall.SysProcAttr { + return &syscall.SysProcAttr{CreationFlags: windows.CREATE_NEW_PROCESS_GROUP | windows.DETACHED_PROCESS} +} + +// processAlive reports whether pid names a live process: it can be opened and has not exited. +// Two differences from kill -0 on Unix. A process of another user can often be opened with +// PROCESS_QUERY_LIMITED_INFORMATION, so it reads as alive here, where Unix's EPERM reads it as +// gone. And a process that exited with code 259 reads as still running, since that is +// STILL_ACTIVE's value. +func processAlive(pid int) bool { + if pid <= 0 { + return false + } + h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid)) + if err != nil { + return false + } + defer windows.CloseHandle(h) //nolint:errcheck // a query handle + var code uint32 + if err := windows.GetExitCodeProcess(h, &code); err != nil { + return false + } + const stillActive = 259 // STILL_ACTIVE + return code == stillActive +} + +// terminate refuses: see errNoGracefulStop. +func terminate(int) error { return errNoGracefulStop } + +// terminateGroup refuses: see errNoGracefulStop. +func terminateGroup(int) error { return errNoGracefulStop } + +// lockFile takes an exclusive lock on f without waiting (LockFileEx). held is true when another +// process has it. +func lockFile(f *os.File) (held bool, err error) { + var ol windows.Overlapped + err = windows.LockFileEx(windows.Handle(f.Fd()), windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &ol) + if err == nil { + return false, nil + } + if errors.Is(err, windows.ERROR_LOCK_VIOLATION) { + return true, nil + } + return false, err +} + +// writable reports whether this process may write path: for a directory, create files in it, +// by creating and removing one; for a file, open it for appending, which writes nothing. Windows +// has no access(2), and its ACLs are what decide. +func writable(path string) error { + if fi, err := os.Stat(path); err == nil && !fi.IsDir() { + f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0) + if err != nil { + return err + } + return f.Close() + } + f, err := os.CreateTemp(path, ".agentop-writable-*") + if err != nil { + return err + } + name := f.Name() + _ = f.Close() + return os.Remove(name) +} diff --git a/cmd/agentop/setup_port_preflight_test.go b/cmd/agentop/setup_port_preflight_test.go index 8a5d7109e..e82bd36ad 100644 --- a/cmd/agentop/setup_port_preflight_test.go +++ b/cmd/agentop/setup_port_preflight_test.go @@ -7,7 +7,6 @@ import ( "strconv" "strings" "sync" - "syscall" "testing" ) @@ -106,7 +105,7 @@ func TestApplyUnsupervised_CannotStartIsAFailure(t *testing.T) { // healthy", which is the state the false-ready shipped through. Its first // answer waits for the proxy to have logged, on a FIFO the proxy writes next. logged := filepath.Join(t.TempDir(), "logged") - if err := syscall.Mkfifo(logged, 0o600); err != nil { + if err := mkfifo(logged, 0o600); err != nil { t.Fatal(err) } gaveUp := make(chan struct{}) diff --git a/cmd/agentop/setup_prior_test.go b/cmd/agentop/setup_prior_test.go index a71e1347d..60caa6dbe 100644 --- a/cmd/agentop/setup_prior_test.go +++ b/cmd/agentop/setup_prior_test.go @@ -78,7 +78,7 @@ func detachedSleeperAt(t *testing.T, path string) int { // be reused by then. t.Cleanup(func() { if ranFrom(pid, path) { - _ = syscall.Kill(pid, syscall.SIGKILL) + _ = killPID(pid, syscall.SIGKILL) } }) waitReady() diff --git a/cmd/agentop/setup_probe.go b/cmd/agentop/setup_probe.go index 10aa79c9e..9cd31a93d 100644 --- a/cmd/agentop/setup_probe.go +++ b/cmd/agentop/setup_probe.go @@ -9,7 +9,6 @@ import ( "regexp" "strconv" "strings" - "syscall" "time" ) @@ -209,7 +208,7 @@ func ourProxy(binDir, pidFile string, pid int, exe string) bool { return samePath(exe, filepath.Join(binDir, proxyBinary)) || samePath(exe, filepath.Join(binDir, preRenameBinary)) } -func alive(pid int) bool { return pid > 0 && syscall.Kill(pid, 0) == nil } +func alive(pid int) bool { return processAlive(pid) } // psComm is ps's comm for pid, and whether ps answered at all. func psComm(pid int) (string, bool) { diff --git a/cmd/agentop/setup_probe_test.go b/cmd/agentop/setup_probe_test.go index 67a6f1ff0..f0bb9ad93 100644 --- a/cmd/agentop/setup_probe_test.go +++ b/cmd/agentop/setup_probe_test.go @@ -11,8 +11,6 @@ import ( "strconv" "strings" "testing" - - "golang.org/x/sys/unix" ) func TestSetupSupervisorUsable(t *testing.T) { @@ -338,7 +336,7 @@ func TestStartUnsupervised(t *testing.T) { if readPIDFile(filepath.Join(dir, "proxy.pid")) != pid { t.Error("the pidfile does not name the started process") } - if sid, err := unix.Getsid(pid); err != nil || sid != pid { + if sid, err := getsid(pid); err != nil || sid != pid { t.Errorf("the proxy's session = %d %v, want its own (%d)", sid, err, pid) } diff --git a/cmd/agentop/setup_step_binaries.go b/cmd/agentop/setup_step_binaries.go index 9a85ce602..d4f8e0d1b 100644 --- a/cmd/agentop/setup_step_binaries.go +++ b/cmd/agentop/setup_step_binaries.go @@ -8,7 +8,6 @@ import ( "path/filepath" "slices" "strings" - "syscall" "github.com/rossoctl/cortex/cmd/agentop/checklist" "github.com/rossoctl/cortex/cmd/agentop/updatestatus" @@ -249,8 +248,7 @@ func checkWritable(env *setupEnv, path string) *problem { } p = parent } - const wOK = 0x2 - if err := syscall.Access(p, wOK); err != nil { + if err := writable(p); err != nil { return &problem{reason: env.tilde(p) + " is not writable (" + err.Error() + ")"} } return nil diff --git a/cmd/agentop/setup_step_service_test.go b/cmd/agentop/setup_step_service_test.go index 1eb08e91e..7b533b90f 100644 --- a/cmd/agentop/setup_step_service_test.go +++ b/cmd/agentop/setup_step_service_test.go @@ -227,7 +227,7 @@ func stopProcessOnCleanup(t *testing.T, pid *int) { // is reaped: by this wait, or by startUnsupervised's goroutine, which leaves this // wait an ECHILD. func killAndReap(pid int) { - _ = syscall.Kill(pid, syscall.SIGKILL) + _ = killPID(pid, syscall.SIGKILL) if p, err := os.FindProcess(pid); err == nil { _, _ = p.Wait() } diff --git a/cmd/agentop/setup_unsupervised.go b/cmd/agentop/setup_unsupervised.go index 0cd059b8a..a00102b05 100644 --- a/cmd/agentop/setup_unsupervised.go +++ b/cmd/agentop/setup_unsupervised.go @@ -5,7 +5,6 @@ import ( "os/exec" "path/filepath" "strconv" - "syscall" "time" ) @@ -27,7 +26,7 @@ func startUnsupervised(bin, cortexDir, healthURL string) (pid int, healthy bool, } cmd := exec.Command(bin, "--local", "--supervise") //nolint:gosec // the installed cortex cmd.Stdout, cmd.Stderr = logf, logf - cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} + cmd.SysProcAttr = detached() err = cmd.Start() _ = logf.Close() if err != nil {