mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
* fix(cli): keep LAN access for sessions that outlive their runner on macOS macOS attributes Local Network decisions to a process's responsible process, which a child inherits from its spawner. Sessions spawned by a runner that launchd started (directly, or through the npm launcher bin/hapi.cjs) inherit the runner's or the launcher's pid. Once that process exits (upgrade, version handoff) while the sessions are kept, every new process in their trees that connects to a LAN address fails with EHOSTUNREACH. When the spawner may hand its responsibility on, detached spawnHappyCLI children (sessions, runner handoff, runner start) are started with HAPI_DISCLAIM_RESPONSIBILITY=<spawner pid>. In a direct child of that pid, the entry hook removes the variable from the libc environment and re-executes process.execPath in place via posix_spawn(POSIX_SPAWN_SETEXEC) with responsibility_spawnattrs_setdisclaim, so the child becomes its own responsible process. The pid, stdio, process group, cwd and the rest of the environment are unchanged, so the runner's ChildProcess tracking keeps working. The spawner may hand its responsibility on when it is its own responsible process, or when its responsible process is its direct parent, is the npm launcher (which now passes HAPI_LAUNCHER_PID) and is itself its own responsible process. A spawner started from Terminal or over SSH keeps the inherited responsibility and with it the implicit LAN allowance macOS grants there. The lookup uses responsibility_get_pid_responsible_for_pid through bun:ffi; when it is unavailable the spawn is not disclaimed and this is logged once. Binaries without the hook ignore the variable, and a copy that leaks into a rolled-back tree is ignored because its parent is not the spawner. The private disclaim symbol may disappear: the hook then execs without it, and any other failure continues in-process; the reason is written to the hapi log. * fix(cli): print --help and --version when HAPI_HOME cannot be created The entry point imported runtime/responsibility to read the npm launcher's pid. That module imports the logger, whose configuration creates HAPI_HOME, so `hapi --help` and `hapi --version` failed before printing when that directory could not be created. Move the launcher pid into a dependency-free module, read it from there, and make the optional disclaim fallback note best effort. Add an entry point test that runs both flags with an unwritable HAPI_HOME.
85 lines
3.2 KiB
TypeScript
85 lines
3.2 KiB
TypeScript
/**
|
|
* Whether this process may hand its macOS responsibility to the detached
|
|
* processes it spawns (see disclaimExec.ts).
|
|
*
|
|
* That is the case when this process is its own responsible process, as a
|
|
* runner started directly by launchd is, or when its responsible process is the
|
|
* npm launcher (`bin/hapi.cjs`) that launchd started and that started this
|
|
* process. In both cases the inherited responsible pid goes away when the runner
|
|
* or its launcher exits. A process started from Terminal or over SSH has
|
|
* Terminal or sshd-session as its responsible process, and macOS grants their
|
|
* descendants LAN access implicitly; those keep the inherited responsibility.
|
|
*
|
|
* Observed on macOS 27: once a process's responsible process has exited,
|
|
* `responsibility_get_pid_responsible_for_pid` reports the process itself. Such a
|
|
* spawner then counts as its own responsible process. Its tree has already lost
|
|
* LAN access for new processes, so disclaiming its children is no worse.
|
|
*/
|
|
import { logger } from '@/ui/logger'
|
|
import { getLauncherPid } from './launcherPid'
|
|
|
|
type ResponsibleFor = (pid: number) => number
|
|
|
|
let lookup: ResponsibleFor | null | undefined
|
|
|
|
export interface ResponsibilityFacts {
|
|
pid: number
|
|
ppid: number
|
|
launcherPid: number | null
|
|
responsibleFor: ResponsibleFor
|
|
}
|
|
|
|
export function ownsResponsibility(facts: ResponsibilityFacts): boolean {
|
|
const responsible = facts.responsibleFor(facts.pid)
|
|
if (responsible === facts.pid) {
|
|
return true
|
|
}
|
|
return facts.launcherPid !== null
|
|
&& responsible === facts.launcherPid
|
|
&& responsible === facts.ppid
|
|
&& facts.responsibleFor(responsible) === responsible
|
|
}
|
|
|
|
function warnOnce(reason: string): void {
|
|
logger.debug(`[WARN] [DISCLAIM] macOS responsibility lookup unavailable (${reason}); detached children keep the inherited responsible process`)
|
|
}
|
|
|
|
function loadLookup(): ResponsibleFor | null {
|
|
if (process.platform !== 'darwin') {
|
|
return null
|
|
}
|
|
if (!process.versions.bun) {
|
|
warnOnce('not running under Bun')
|
|
return null
|
|
}
|
|
try {
|
|
// Loaded lazily: this module is also imported under Node (tests), where
|
|
// bun:ffi does not exist.
|
|
const { dlopen, FFIType } = require('bun:ffi') as typeof import('bun:ffi')
|
|
const { symbols } = dlopen('/usr/lib/libSystem.B.dylib', {
|
|
responsibility_get_pid_responsible_for_pid: { args: [FFIType.i32], returns: FFIType.i32 }
|
|
})
|
|
return (pid) => symbols.responsibility_get_pid_responsible_for_pid(pid)
|
|
} catch (error) {
|
|
warnOnce(error instanceof Error ? error.message : String(error))
|
|
return null
|
|
}
|
|
}
|
|
|
|
/** False whenever the answer is unknown, so callers fail open to no disclaim. */
|
|
export function canDisclaimForChildren(): boolean {
|
|
if (lookup === undefined) {
|
|
lookup = loadLookup()
|
|
}
|
|
if (!lookup) {
|
|
return false
|
|
}
|
|
try {
|
|
return ownsResponsibility({ pid: process.pid, ppid: process.ppid, launcherPid: getLauncherPid(), responsibleFor: lookup })
|
|
} catch (error) {
|
|
warnOnce(error instanceof Error ? error.message : String(error))
|
|
lookup = null
|
|
return false
|
|
}
|
|
}
|