From 23a73750bb37a1c5a321b07515c458e84391930c Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 20:56:06 -0500 Subject: [PATCH 1/2] test(mcpx): darwin test-compile portability for internal/daemon (#237) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit unixPair: drop the Linux-only syscall.SOCK_CLOEXEC from shared test code; create the socketpair plainly and set close-on-exec via syscall.CloseOnExec (portable across linux/darwin). exeBytes: split into per-OS helpers — linux keeps the /proc//exe inode read; darwin resolves the child exec path via kern.procargs2 (x/sys/unix SysctlRaw) and reads that file. The shared exec_watch_test.go no longer assumes /proc. darwin-compile-check.sh: DARWIN_COMPILE_EXCEPTIONS is now empty (the internal/daemon entry was the last one). Gate verified on the laptop: darwin/arm64 test-compile scan passes with 0 exceptions skipped; internal/daemon compiles for darwin/arm64; targeted linux tests pass. --- .../internal/daemon/exe_bytes_darwin_test.go | 39 +++++++++++++++++++ .../internal/daemon/exe_bytes_linux_test.go | 18 +++++++++ pkgs/mcpx/internal/daemon/exec_watch_test.go | 22 ++++------- pkgs/mcpx/internal/daemon/takeover_test.go | 8 +++- pkgs/mcpx/scripts/darwin-compile-check.sh | 9 ++--- 5 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go create mode 100644 pkgs/mcpx/internal/daemon/exe_bytes_linux_test.go diff --git a/pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go b/pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go new file mode 100644 index 0000000000..dac737073a --- /dev/null +++ b/pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go @@ -0,0 +1,39 @@ +package daemon + +import ( + "bytes" + "encoding/binary" + "fmt" + "os" + "testing" + + "golang.org/x/sys/unix" +) + +// Darwin has no /proc. The closest equivalent is the child's exec path +// from kern.procargs2 (layout: int32 argc, then the NUL-terminated exec +// path, then argv). Reading that path gives the program the child was +// started from; the exec-change test replaces the file at the path +// before restarting, so the restarted child's path content is the new +// program — the same comparison the linux /proc read makes. +func exeBytes(t *testing.T, pid int) []byte { + t.Helper() + raw, err := unix.SysctlRaw(fmt.Sprintf("kern.procargs2.%d", pid)) + if err != nil { + t.Fatalf("sysctl kern.procargs2.%d: %v", pid, err) + } + if len(raw) < 5 { + t.Fatalf("kern.procargs2.%d: short reply (%d bytes)", pid, len(raw)) + } + _ = binary.LittleEndian.Uint32(raw[:4]) // argc; the exec path follows it + rest := raw[4:] + end := bytes.IndexByte(rest, 0) + if end <= 0 { + t.Fatalf("kern.procargs2.%d: no exec path in reply", pid) + } + b, err := os.ReadFile(string(rest[:end])) + if err != nil { + t.Fatalf("read child exec path %q: %v", rest[:end], err) + } + return b +} diff --git a/pkgs/mcpx/internal/daemon/exe_bytes_linux_test.go b/pkgs/mcpx/internal/daemon/exe_bytes_linux_test.go new file mode 100644 index 0000000000..af3791a097 --- /dev/null +++ b/pkgs/mcpx/internal/daemon/exe_bytes_linux_test.go @@ -0,0 +1,18 @@ +package daemon + +import ( + "fmt" + "os" + "testing" +) + +// Linux: /proc//exe is the inode the child is actually executing, +// even after that path has been replaced on disk. +func exeBytes(t *testing.T, pid int) []byte { + t.Helper() + b, err := os.ReadFile(fmt.Sprintf("/proc/%d/exe", pid)) + if err != nil { + t.Fatalf("read /proc/%d/exe: %v", pid, err) + } + return b +} diff --git a/pkgs/mcpx/internal/daemon/exec_watch_test.go b/pkgs/mcpx/internal/daemon/exec_watch_test.go index aeabc8fd88..249d6e5bb0 100644 --- a/pkgs/mcpx/internal/daemon/exec_watch_test.go +++ b/pkgs/mcpx/internal/daemon/exec_watch_test.go @@ -7,7 +7,6 @@ import ( "fmt" "os" "path/filepath" - "runtime" "strings" "sync" "syscall" @@ -129,20 +128,13 @@ func waitFor(t *testing.T, d time.Duration, what string, cond func() bool) { } } -// exeBytes is the program a running child is executing, read through /proc. A -// child keeps the file it was started from even after that path is replaced, -// so this is what tells the old program from the new one. -func exeBytes(t *testing.T, pid int) []byte { - t.Helper() - if runtime.GOOS != "linux" { - t.Skipf("exeBytes reads /proc/%d/exe, which only exists on linux", pid) - } - b, err := os.ReadFile(fmt.Sprintf("/proc/%d/exe", pid)) - if err != nil { - t.Fatalf("read /proc/%d/exe: %v", pid, err) - } - return b -} +// exeBytes is the program a running child is executing. A child keeps +// the file it was started from even after that path is replaced, so +// this is what tells the old program from the new one. The read is +// per-OS (exe_bytes_linux_test.go, exe_bytes_darwin_test.go): linux +// reads the running inode through /proc//exe; darwin has no +// /proc, so it resolves the child's exec path via kern.procargs2 +// and reads that file. func mustRead(t *testing.T, path string) []byte { t.Helper() diff --git a/pkgs/mcpx/internal/daemon/takeover_test.go b/pkgs/mcpx/internal/daemon/takeover_test.go index eb3f8122cd..0a86147a9c 100644 --- a/pkgs/mcpx/internal/daemon/takeover_test.go +++ b/pkgs/mcpx/internal/daemon/takeover_test.go @@ -21,7 +21,13 @@ import ( func unixPair(t *testing.T) (*net.UnixConn, *net.UnixConn) { t.Helper() - fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0) + // SOCK_CLOEXEC is Linux-only (undefined on darwin, #237): create the + // pair plainly and set close-on-exec on both ends the portable way. + fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) + if err == nil { + syscall.CloseOnExec(fds[0]) + syscall.CloseOnExec(fds[1]) + } if err != nil { t.Fatal(err) } diff --git a/pkgs/mcpx/scripts/darwin-compile-check.sh b/pkgs/mcpx/scripts/darwin-compile-check.sh index e84635e69b..c36c4bd365 100755 --- a/pkgs/mcpx/scripts/darwin-compile-check.sh +++ b/pkgs/mcpx/scripts/darwin-compile-check.sh @@ -14,11 +14,10 @@ cd "$(dirname "$0")/.." # The single exception list. A package named here may fail the darwin # test compile without failing this gate; every other package must -# compile. Empty this list (one line) when the last entry is fixed. -# github.com/dezren39/mcpx/internal/daemon — #237: takeover_test.go -# uses syscall.SOCK_CLOEXEC (undefined on darwin). DELETE this entry -# when #237's compile fix lands. -DARWIN_COMPILE_EXCEPTIONS="${DARWIN_COMPILE_EXCEPTIONS-github.com/dezren39/mcpx/internal/daemon}" +# compile. Empty since #237 landed (the internal/daemon entry — its +# takeover_test.go used the Linux-only syscall.SOCK_CLOEXEC — was the +# last one). Add an entry only with a tracking issue. +DARWIN_COMPILE_EXCEPTIONS="${DARWIN_COMPILE_EXCEPTIONS-}" export GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 From 007a1e7ec0faff76b4af91edf775c598758df157 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 21:25:11 -0500 Subject: [PATCH 2/2] fix(mcpx): pass PID as separate SysctlRaw arg in darwin exeBytes (Codex P2 on #3288) kern.procargs2 is handler-backed: name2oid rejects the PID formatted into the OID string with ENOENT, so the darwin exec-change test would fail at runtime on macOS. SysctlRaw takes the PID as a numeric MIB arg. --- pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go b/pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go index dac737073a..16c00bbd71 100644 --- a/pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go +++ b/pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go @@ -3,7 +3,6 @@ package daemon import ( "bytes" "encoding/binary" - "fmt" "os" "testing" @@ -18,7 +17,7 @@ import ( // program — the same comparison the linux /proc read makes. func exeBytes(t *testing.T, pid int) []byte { t.Helper() - raw, err := unix.SysctlRaw(fmt.Sprintf("kern.procargs2.%d", pid)) + raw, err := unix.SysctlRaw("kern.procargs2", pid) if err != nil { t.Fatalf("sysctl kern.procargs2.%d: %v", pid, err) }