oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build darwin || testbuild
2
3package main
4
5import (
6 "context"
7 "errors"
8 "io"
9 "os"
10 "strconv"
11
12 "golang.org/x/sys/unix"
13
14 "amadan.net/oficina/equipo/internal/runner"
15)
16
17// toolProxy is a Mac runner's MCP tool endpoint, the CLI's only MCP
18// server there (provider.DarwinToolMCP): equipo-runner tool-proxy
19// --socket <path>. Like the Linux tool proxy it reads no environment and
20// no configuration and writes nothing to stderr: 2 for other arguments,
21// 1 when it cannot serve. A Linux release does not have it; Linux runs
22// the certified equipo-tool-proxy instead.
23func toolProxy(ctx context.Context, args []string, input io.Reader, output io.Writer) (int, bool) {
24 if len(args) != 2 || args[0] != "--socket" {
25 return 2, true
26 }
27 if dropInherited() != nil {
28 return 1, true
29 }
30 if runner.ServeDarwinToolProxy(ctx, input, output, args[1]) != nil {
31 return 1, true
32 }
33 return 0, true
34}
35
36// errInherited: the endpoint could not list or close what it inherited.
37var errInherited = errors.New("tool endpoint: inherited descriptors")
38
39// dropInherited lets go of what the endpoint inherited from the CLI that
40// started it: a token's descriptor (CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR)
41// and the CLI's whole environment. It serves with neither.
42//
43// A descriptor above 2 is inherited exactly when close-on-exec is clear:
44// one that had it set did not survive exec, and every descriptor the Go
45// runtime and standard library open carries it (the poller's kqueue or
46// epoll and eventfd, pipes, files, sockets; this binary has no cgo). So
47// the runtime's own descriptors are kept by what they are, not guessed
48// by number, whenever they were opened. The list comes from /dev/fd (on
49// Linux a link to /proc/self/fd).
50//
51// The environment is emptied: the endpoint needs none of it. That clears
52// the process's own copy; the original strings exec placed in memory
53// remain, which is why a token never travels in the environment (task 8
54// passes it by descriptor).
55func dropInherited() error {
56 entries, err := os.ReadDir("/dev/fd")
57 if err != nil {
58 return errInherited
59 }
60 for _, e := range entries {
61 fd, err := strconv.Atoi(e.Name())
62 if err != nil {
63 return errInherited
64 }
65 if fd <= 2 {
66 continue
67 }
68 flags, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0)
69 if errors.Is(err, unix.EBADF) {
70 continue // the listing's own descriptor, closed already
71 }
72 if err != nil {
73 return errInherited
74 }
75 if flags&unix.FD_CLOEXEC == 0 {
76 if err := unix.Close(fd); err != nil {
77 return errInherited
78 }
79 }
80 }
81 os.Clearenv()
82 return nil
83}
84