mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-23 21:23:05 +09:00
Bound the server shutdown wait and the output drain after a session process exits. Descendants can keep stdio open after the session process exits, so neither wait can depend on EOF alone. Abort the SSH channel on drain timeout without sending EOF concurrently with an outstanding output write. Close all PTY master descriptors on cancellation and let the terminal driver send SIGHUP. Duplicate the masters with CLOEXEC and O_NONBLOCK before wrapping them with os.NewFile so the runtime poller can interrupt pending I/O on Close. Keep the PTY child's blocking mode intact. Use RawConn.Control for duplication and window changes to synchronize with Close and avoid restoring blocking mode on the original master through File.Fd. Non-PTY sessions receive SIGHUP explicitly. Keep SIGKILL escalation for monitor sessions and add it for directly launched sessions. Use os.Process.Signal on the direct path to synchronize signaling with reaping. Make the escalation delay configurable alongside the shutdown and I/O drain timeouts. Use a process-completion channel to stop the cancellation worker on normal exit, and join the worker before sending exit status so any cancellation message is written before channel teardown. Cover normal completion and cancellation through live monitored SSH sessions. Fixes tailscale/corp#50380 Fixes #22059 Updates #20628 Updates #21827 Signed-off-by: James Tucker <james@tailscale.com> Change-Id: I2a66741ba156a607dc3df70a79e140f19737b7a5
156 lines
4.5 KiB
Go
156 lines
4.5 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build (linux && !android) || (darwin && !ios) || freebsd || openbsd
|
|
|
|
// Session processes are launched by the connection's privileged monitor.
|
|
// tailscaled keeps the parent I/O descriptors and passes the child's stdio
|
|
// and optional environment pipe via SCM_RIGHTS.
|
|
|
|
package tailssh
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// hangupEscalation is the delay between SIGHUP and SIGKILL on session teardown.
|
|
const hangupEscalation = 5 * time.Second
|
|
|
|
// monExitRegistry matches exit notifications to child handles, including
|
|
// notifications received before the session registers its waiter.
|
|
type monExitRegistry struct {
|
|
mu sync.Mutex
|
|
waiters map[monitorProcess]*monExitStatus
|
|
pending map[monitorProcess]int // exits received before registration
|
|
}
|
|
|
|
// dispatchExit is the monitorClient exit callback, called from the
|
|
// client's read-loop goroutine.
|
|
func (s *monExitRegistry) dispatchExit(pid monitorProcess, code int) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if ch, ok := s.waiters[pid]; ok {
|
|
delete(s.waiters, pid)
|
|
ch.complete(code)
|
|
return
|
|
}
|
|
if s.pending == nil {
|
|
s.pending = make(map[monitorProcess]int)
|
|
}
|
|
s.pending[pid] = code
|
|
}
|
|
|
|
// registerSessionExit delivers and removes a pending exit, or registers
|
|
// a waiter for a future notification.
|
|
func (s *monExitRegistry) registerSessionExit(pid monitorProcess, ch *monExitStatus) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if code, ok := s.pending[pid]; ok {
|
|
delete(s.pending, pid)
|
|
ch.complete(code)
|
|
return
|
|
}
|
|
if s.waiters == nil {
|
|
s.waiters = make(map[monitorProcess]*monExitStatus)
|
|
}
|
|
s.waiters[pid] = ch
|
|
}
|
|
|
|
func newMonExitStatus() *monExitStatus {
|
|
return &monExitStatus{done: make(chan struct{})}
|
|
}
|
|
|
|
func (s *monExitStatus) complete(code int) {
|
|
s.code = code
|
|
close(s.done)
|
|
}
|
|
|
|
// launchViaMonitor passes cmd and the child's descriptors to the monitor.
|
|
// After dispatch, cancellation does not abandon the exec reply: a successful
|
|
// launch always registers the child so run can terminate it and wait for exit.
|
|
func (ss *sshSession) launchViaMonitor(cmd *exec.Cmd, envFile *os.File, stdio []*os.File, ctty bool) error {
|
|
if len(stdio) != 3 {
|
|
return fmt.Errorf("internal error: monitor exec needs 3 stdio fds, got %d", len(stdio))
|
|
}
|
|
if err := ss.ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
mc, err := ss.conn.connMonitor()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
child, err := mc.ExecIncubator(ss.ctx, cmd.Args, cmd.Env, cmd.Dir, ctty, envFile, stdio...)
|
|
if err != nil {
|
|
return fmt.Errorf("exec on session monitor: %w", err)
|
|
}
|
|
exit := newMonExitStatus()
|
|
ss.conn.monitor.exits.registerSessionExit(child, exit)
|
|
ss.monChild = child
|
|
ss.monExit = exit
|
|
ss.monDead = mc.Gone()
|
|
return nil
|
|
}
|
|
|
|
// waitMonitorProcess returns the child's exit code, or an error if its monitor
|
|
// dies without reporting an exit. ok is false for directly launched sessions.
|
|
func (ss *sshSession) waitMonitorProcess() (code int, ok bool, err error) {
|
|
if ss.monExit == nil {
|
|
return 0, false, nil
|
|
}
|
|
select {
|
|
case <-ss.monExit.done:
|
|
return ss.monExit.code, true, nil
|
|
case <-ss.monDead:
|
|
// The read loop delivers notifications before closing monDead.
|
|
// Recheck here because both cases may have become ready together.
|
|
select {
|
|
case <-ss.monExit.done:
|
|
return ss.monExit.code, true, nil
|
|
default:
|
|
return 1, true, errors.New("session monitor exited before the session process")
|
|
}
|
|
}
|
|
}
|
|
|
|
// hangupMonitorProcess sends SIGHUP for non-PTY sessions and schedules SIGKILL
|
|
// if the child has not exited. It returns false for directly launched sessions.
|
|
func (ss *sshSession) hangupMonitorProcess() bool {
|
|
if ss.monChild.PID == 0 {
|
|
return false
|
|
}
|
|
mc := ss.conn.connMonitorIfAny()
|
|
if mc == nil {
|
|
// Monitor teardown owns cleanup. Its reaped PIDs may already have
|
|
// been reused, so tailscaled must not signal them directly.
|
|
return true
|
|
}
|
|
if ss.ptyReq == nil {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := mc.Signal(ctx, ss.monChild, "SIGHUP"); err != nil {
|
|
ss.logf("ssh: SIGHUP session process group: %v", err)
|
|
}
|
|
}
|
|
time.AfterFunc(knobDuration(sshHupEscalation(), hangupEscalation), func() { ss.escalateMonitorProcess(mc) })
|
|
return true
|
|
}
|
|
|
|
func (ss *sshSession) escalateMonitorProcess(mc *monitorClient) {
|
|
select {
|
|
case <-ss.monExit.done:
|
|
return
|
|
default:
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := mc.Signal(ctx, ss.monChild, "SIGKILL"); err != nil {
|
|
ss.vlogf("ssh: SIGKILL session process group: %v", err)
|
|
}
|
|
}
|