Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pkgs/mcpx/internal/daemon/exe_bytes_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package daemon

import (
"bytes"
"encoding/binary"
"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("kern.procargs2", 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
}
18 changes: 18 additions & 0 deletions pkgs/mcpx/internal/daemon/exe_bytes_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package daemon

import (
"fmt"
"os"
"testing"
)

// Linux: /proc/<pid>/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
}
22 changes: 7 additions & 15 deletions pkgs/mcpx/internal/daemon/exec_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -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/<pid>/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()
Expand Down
8 changes: 7 additions & 1 deletion pkgs/mcpx/internal/daemon/takeover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions pkgs/mcpx/scripts/darwin-compile-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/developing-today/code/pkgs/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/developing-today/code/pkgs/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

Expand Down
Loading