Doc `getsockopt()` args (for macOS)

Per the questionable `copilot` review which is detailed for follow up in
https://github.com/goodboy/tractor/issues/418. These constants are
directly linked from the kernel sources fwiw.
ns_aware
Gud Boi 2026-03-05 16:41:37 -05:00
parent 9c3d3bcec1
commit a7e74acdff
1 changed files with 19 additions and 3 deletions

View File

@ -24,7 +24,6 @@ from contextlib import (
from pathlib import Path
import os
import sys
import socket as stdlib_socket
from socket import (
AF_UNIX,
SOCK_STREAM,
@ -361,10 +360,27 @@ def get_peer_pid(sock) -> int|None:
NOTE, should work on MacOS (and others?).
'''
# try to get the peer PID
# try to get the peer PID using a naive soln found from,
# https://stackoverflow.com/a/67971484
#
# NOTE, a more correct soln is likely needed here according to
# the complaints of `copilot` which led to digging into the
# underlying `go`lang issue linked from the above SO answer,
# XXX, darwin-xnu kernel srces defining these constants,
# - SOL_LOCAL
# |_https://github.com/apple/darwin-xnu/blob/main/bsd/sys/un.h#L85
# - LOCAL_PEERPID
# |_https://github.com/apple/darwin-xnu/blob/main/bsd/sys/un.h#L89
#
SOL_LOCAL: int = 0
LOCAL_PEERPID: int = 0x002
try:
pid: int = sock.getsockopt(0, 2)
pid: int = sock.getsockopt(
SOL_LOCAL,
LOCAL_PEERPID,
)
return pid
except socket_error as e:
log.exception(