| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "syscall" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/connect" |
| 10 | "amadan.net/oficina/equipo/internal/runner/containment" |
| 11 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | "golang.org/x/sys/unix" |
| 14 | ) |
| 15 | |
| 16 | // execTerminal runs the fake setup-token on a terminal with no |
| 17 | // namespace, in its own process group, for the attempt's state machine |
| 18 | // (the contained launch is TestHeadlessSignInContained's). Its Stop |
| 19 | // kills the group and waits. |
| 20 | func (r *tokenRig) execTerminal(ctx context.Context, attempt string) (connect.Terminal, error) { |
| 21 | master, tty, err := containment.OpenTerminal() |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | dir := r.t.TempDir() |
| 26 | cmd := exec.Command(r.cfg.ClaudePath, "setup-token") |
| 27 | cmd.Dir = dir |
| 28 | cmd.Env = append(provider.LoginEnvironment(wire.ProviderClaude, dir), "BROWSER="+provider.SignInBrowser, "TERM=xterm-256color") |
| 29 | cmd.Stdin, cmd.Stdout, cmd.Stderr = tty, tty, tty |
| 30 | cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true} |
| 31 | if err = cmd.Start(); err != nil { |
| 32 | tty.Close() |
| 33 | master.Close() |
| 34 | return nil, err |
| 35 | } |
| 36 | tty.Close() |
| 37 | t := &execTerm{File: master, cmd: cmd, done: make(chan struct{})} |
| 38 | go func() { _ = cmd.Wait(); close(t.done) }() |
| 39 | return t, nil |
| 40 | } |
| 41 | |
| 42 | type execTerm struct { |
| 43 | *os.File |
| 44 | cmd *exec.Cmd |
| 45 | done chan struct{} |
| 46 | } |
| 47 | |
| 48 | func (t *execTerm) Done() <-chan struct{} { return t.done } |
| 49 | func (t *execTerm) Stop() error { |
| 50 | _ = unix.Kill(-t.cmd.Process.Pid, unix.SIGKILL) |
| 51 | <-t.done |
| 52 | return t.File.Close() |
| 53 | } |
| 54 | |