refactor: fold is_party_alive into party_conn_state

One liveness primitive instead of two. The PID argument was a vestige
(format-checked, never probed, since the kill -0 removal); dropping it
means a roster with a garbage SERVER_PID can no longer mask a live
socket in host/moderation/role resolution.
This commit is contained in:
veg 2026-07-11 12:07:13 +00:00
parent be3fb049d2
commit 6b94255245
5 changed files with 51 additions and 88 deletions

53
party
View file

@ -312,34 +312,6 @@ roster_list() {
# Liveness
# --------
# Liveness check: the socket is a live tmux server we can speak the tmux
# protocol to. `tmux -S list-clients` exits 0 only against a real tmux
# server bound to that exact socket path, which simultaneously rules out
# stale roster pointers (server gone), AF_UNIX impostors (a same-group
# user planting a hand-rolled listener via `nc -lU`), and PID-reuse
# leftovers, none of those answer the tmux handshake. Stronger than any
# PID check, and it works regardless of who owns the server process.
#
# Earlier revisions also gated on `kill -0 $pid` as a cheap pre-filter.
# That broke cross-user discovery on every non-Linux POSIX target: per
# POSIX, kill(2) signal 0 may return EPERM when the caller lacks
# send-permission, and illumos, all the BSDs, and macOS honor that.
# Linux is the outlier that returns 0 for "exists, even if not
# signalable," and Linux ABI environments (LX-branded zones) inherit
# that behavior, which is why the bug hid in the test matrix until a
# native illumos run surfaced it. The PID arg is kept in the function
# signature for caller compatibility and as a numeric-validation guard,
# but is no longer probed. Local var names are prefixed to avoid
# clobbering caller-side `pid` / `sock` (POSIX sh has no real locals).
is_party_alive() {
_pa_pid="$1"
_pa_sock="$2"
case "$_pa_pid" in
'' | *[!0-9]* ) return 1 ;;
esac
"$PARTY_TMUX" -S "$_pa_sock" list-clients >/dev/null 2>&1
}
# Classify the caller's relationship to a party socket. Prints exactly
# one of:
# ok : live tmux server, the caller is authorized.
@ -360,6 +332,20 @@ is_party_alive() {
# to a hidden party, never to a false "ok".
# dead : nothing speaking the tmux protocol at that socket
# (stale roster, killed server, AF_UNIX impostor).
#
# This probe is the ONLY liveness primitive. `tmux -S <sock>
# list-clients` exits 0 only against a real tmux server bound to that
# exact socket path, which rules out stale roster pointers, AF_UNIX
# impostors (a same-group user planting a hand-rolled listener via
# `nc -lU`), and PID-reuse leftovers in one shot. Earlier revisions had
# a separate is_party_alive() that also format-checked the roster's
# SERVER_PID, a vestige of a `kill -0` pre-filter that broke cross-user
# discovery on every non-Linux POSIX target: per POSIX, kill(2) signal
# 0 may return EPERM when the caller lacks send-permission, and
# illumos, all the BSDs, and macOS honor that. Linux (and LX-branded
# zones) return 0 for "exists, even if not signalable", which is why
# the bug hid in the test matrix until a native illumos run surfaced
# it. SERVER_PID stays in the roster as display-only metadata.
party_conn_state() {
_cs_err=$("$PARTY_TMUX" -S "$1" list-clients 2>&1 >/dev/null) \
&& _cs_rc=0 || _cs_rc=$?
@ -712,8 +698,9 @@ EOF
fi
if [ -d "$party_dir" ] && [ -f "$rec" ]; then
roster_read "$rec" 2>/dev/null || true
if is_party_alive "${RR_SERVER_PID:-}" "${RR_SOCKET:-}"; then
echo "party: '$name' is already running (pid $RR_SERVER_PID)" >&2
if [ -n "${RR_SOCKET:-}" ] \
&& [ "$(party_conn_state "$RR_SOCKET")" != dead ]; then
echo "party: '$name' is already running" >&2
exit 1
fi
fi
@ -1078,7 +1065,7 @@ resolve_authoritative_party() {
[ -L "$d" ] && continue
[ -O "$d" ] || continue
roster_read "$rec" || continue
is_party_alive "$RR_SERVER_PID" "$RR_SOCKET" || continue
[ "$(party_conn_state "$RR_SOCKET")" != dead ] || continue
[ "$RR_HOST_USER" = "$USER" ] || continue
[ -z "$target" ] || [ "$target" = "$RR_PARTY_NAME" ] || continue
set -- "$@" "$rec"
@ -1391,7 +1378,7 @@ EOF
tmux_sock="${TMUX%%,*}"
for rec in $(roster_list); do
roster_read "$rec" || continue
is_party_alive "$RR_SERVER_PID" "$RR_SOCKET" || continue
[ "$(party_conn_state "$RR_SOCKET")" != dead ] || continue
[ "$RR_SOCKET" = "$tmux_sock" ] || continue
sock="$RR_SOCKET"; host_sess="$RR_PARTY_NAME"; break
done
@ -1399,7 +1386,7 @@ EOF
if [ -z "$sock" ]; then
for rec in $(roster_list); do
roster_read "$rec" || continue
is_party_alive "$RR_SERVER_PID" "$RR_SOCKET" || continue
[ "$(party_conn_state "$RR_SOCKET")" != dead ] || continue
"$PARTY_TMUX" -S "$RR_SOCKET" list-clients -F '#{client_user}' 2>/dev/null \
| grep -qx "$USER" || continue
sock="$RR_SOCKET"; host_sess="$RR_PARTY_NAME"; break