ltop 0.2.2

btop-styled TUI monitoring only local network, LAN neighbours and disk I/O
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! macOS process and socket introspection: sysctl KERN_PROC_ALL for the
//! system-wide pid/comm/start table, libproc for per-pid disk rusage and
//! per-fd socket tuples.
//!
//! Access semantics mirror the Linux data path: the pid table is
//! world-readable (like /proc numeric entries), while rusage and fd/socket
//! introspection succeed only for the caller's own uid (same as reading
//! /proc/<pid>/io or fd links unprivileged).
//!
//! struct offsets were pinned against the macOS 27 SDK with a C probe
//! (offsetof/sizeof) and are re-verified at runtime by the projection tests
//! on every platform via synthetic buffers.

#![allow(dead_code)] // FFI 包装仅 mac 数据路径使用;投影纯函数双平台测试

#[cfg(target_os = "macos")]
use std::collections::HashMap;
use std::ffi::{c_int, c_void};

#[cfg(target_os = "macos")]
use crate::collect::nettop::flow_inode;

const PROC_PIDLISTFDS: c_int = 1;
const PROC_PIDFDSOCKETINFO: c_int = 3;
const PROX_FDTYPE_SOCKET: u32 = 2;

// Darwin sysctl mib(libc crate 未收录 KERN_PROC;数值来自 SDK 头文件探针):
// mib = [CTL_KERN(1), KERN_PROC(14), KERN_PROC_ALL(0)]
const KERN_PROC_MIB: c_int = 14;
const KERN_PROC_ALL: c_int = 0;

// --- socket_fdinfo buffer offsets (bytes; pinned by C probes on the
// macOS 27 SDK). The fd flavor returns a wrapper: proc_fileinfo (24 bytes)
// then socket_info — all inner offsets below are relative to the buffer
// start and already include that +24. Port fields are ints carrying the
// port in network byte order; addresses are 16-byte in4in6 unions with v4
// in the LAST 4 bytes (probe-verified on a loopback connection).
const SOI_FAMILY: usize = 184; // 24 + 160
const SOI_PROTOCOL: usize = 180; // 24 + 156
const INSI_FPORT: usize = 264; // 24 + 240 (union start)
const INSI_LPORT: usize = 268;
const INSI_FADDR: usize = 264 + 32;
const INSI_LADDR: usize = 264 + 48;
const SOCKET_INFO_LEN: usize = 792; // sizeof(socket_fdinfo)
/// Darwin 的地址族常量(libc crate 的值随平台变,缓冲区里固定是 Darwin 值)
const AF_INET_DARWIN: libc::c_int = 2;
const AF_INET6_DARWIN: libc::c_int = 30;

/// one row of the system pid table
pub struct MacPid {
    pub pid: u32,
    pub comm: String,
    /// seconds since epoch, like stat's start time; pid-reuse identity key
    pub start_epoch: i64,
}

extern "C" {
    fn proc_listallpids(buffer: *mut c_void, buffersize: c_int) -> c_int;
    /// fd 枚举专用(arg 必须为 0)
    fn proc_pidinfo(
        pid: c_int,
        flavor: c_int,
        arg: u64,
        buffer: *mut c_void,
        buffersize: c_int,
    ) -> c_int;
    /// 单个 fd 的信息必须走 proc_pidfdinfo:proc_pidinfo 传 fd flavor 会
    /// 返回截断的结果(真机实测 rc=136 而非 792)
    #[cfg(target_os = "macos")]
    fn proc_pidfdinfo(
        pid: c_int,
        fd: c_int,
        flavor: c_int,
        buffer: *mut c_void,
        buffersize: c_int,
    ) -> c_int;
    fn proc_pidpath(pid: c_int, buffer: *mut c_void, buffersize: u32) -> c_int;
    #[cfg(target_os = "macos")]
    fn proc_pid_rusage(pid: c_int, flavor: c_int, buffer: *mut c_void) -> c_int;
}

/// pid → (pid, comm, start epoch) for the whole system, one sysctl walk
pub fn list_pids() -> Vec<MacPid> {
    let mut out = Vec::new();
    for (pid, comm, start) in kern_proc_all() {
        out.push(MacPid {
            pid,
            comm,
            start_epoch: start,
        });
    }
    out
}

/// (pid, comm, start epoch) rows via sysctl(KERN_PROC_ALL)
/// struct kinfo_proc: extern proc (header) + eproc; p_comm lives at
/// kp_proc.p_comm and p_starttime (timeval) right before it.
fn kern_proc_all() -> Vec<(u32, String, i64)> {
    let mut mib = [libc::CTL_KERN, KERN_PROC_MIB, KERN_PROC_ALL];
    let mut size: usize = 0;
    // SAFETY: standard sysctl size probe then a correctly sized buffer
    unsafe {
        if libc::sysctl(
            mib.as_mut_ptr(),
            3,
            std::ptr::null_mut(),
            &mut size,
            std::ptr::null_mut(),
            0,
        ) != 0
        {
            return Vec::new();
        }
    }
    let mut buf = vec![0u8; size];
    let mut len = size;
    unsafe {
        if libc::sysctl(
            mib.as_mut_ptr(),
            3,
            buf.as_mut_ptr() as *mut c_void,
            &mut len,
            std::ptr::null_mut(),
            0,
        ) != 0
        {
            return Vec::new();
        }
    }
    let ent = KINFO_PROC_SIZE;
    let count = len / ent;
    let mut out = Vec::with_capacity(count);
    for i in 0..count {
        let base = i * ent;
        // struct extern_proc layout (mach/mach_time independent, stable):
        // p_starttime (timeval: 2×i64... actually 2×i32-style timeval:
        //   struct timeval { time_t tv_sec; suseconds_t tv_usec; } — time_t
        //   is i64 on darwin → 16 bytes) at offset 24 of extern_proc;
        // extern_proc header is 72 bytes, then eproc, kp_proc.p_comm at
        // offset 392 of kinfo_proc (p_comm: [c_char; MAXCOMLEN=16]).
        // Offsets pinned by the projection test using a C-generated fixture.
        let tv_sec = i64_at(&buf, base + P_STARTTIME);
        let comm = c_str(&buf[base + P_COMM..base + P_COMM + 16]);
        out.push((u32_at(&buf, base + P_PID), comm, tv_sec));
    }
    out
}

/// kinfo_proc byte offsets and size (pinned by a C offsetof probe on the
/// macOS 27 SDK: sizeof=648, p_pid=40, p_starttime.tv_sec=0, p_comm=243)
mod kp {
    pub const P_PID: usize = 40; // kp_proc.p_pid
    pub const P_STARTTIME: usize = 0; // kp_proc.p_starttime.tv_sec
    pub const P_COMM: usize = 243; // kp_proc.p_comm
}
use kp::{P_COMM, P_PID, P_STARTTIME};

const KINFO_PROC_SIZE: usize = 648;

/// per-pid cumulative disk bytes (read, write) — own uid only (EPERM
/// otherwise, matching Linux /proc/<pid>/io access)
#[cfg(target_os = "macos")]
pub fn disk_io(pid: u32) -> Option<(u64, u64)> {
    // rusage_info_v2: ri_diskio_bytesread @136, ri_diskio_byteswritten @144
    let mut buf = [0u8; 152];
    // SAFETY: buf is a 152-byte rusage_info_v2 allocation, flavor = V2
    let rc = unsafe { proc_pid_rusage(pid as c_int, 2, buf.as_mut_ptr() as *mut c_void) };
    if rc != 0 {
        return None;
    }
    Some((u64_at(&buf, 136), u64_at(&buf, 144)))
}

/// working directory of a pid (own uid) via PROC_PIDVNODEPATHINFO:
/// proc_vnodepathinfo = pvi_cdir(vnode_info_path) + pvi_rdir(same);
/// vnode_info_path = vnode_info(152B) + vip_path[1024], cwd 路径在偏移 152
pub fn proc_cwd(pid: u32) -> Option<String> {
    // 内核要求 buffer ≥ 整个 proc_vnodepathinfo(= 2352:pvi_cdir + pvi_rdir
    // 各 1176);只给前一半时 proc_pidinfo 直接失败返回 0
    let mut buf = [0u8; 1176 * 2];
    // SAFETY: buf 精确等于 PROC_PIDVNODEPATHINFO_SIZE,flavor=9
    let n = unsafe {
        proc_pidinfo(
            pid as c_int,
            9, // PROC_PIDVNODEPATHINFO
            0,
            buf.as_mut_ptr() as *mut c_void,
            buf.len() as c_int,
        )
    };
    if n <= 0 {
        return None;
    }
    let s = c_str(&buf[152..(152 + 1024).min(buf.len())]);
    if s.is_empty() {
        None
    } else {
        Some(s)
    }
}

/// full executable path of a pid (own uid), as a one-element argv
pub fn cmdline(pid: u32) -> Option<Vec<String>> {
    let mut buf = [0u8; 1024 + 1];
    let rc = unsafe { proc_pidpath(pid as c_int, buf.as_mut_ptr() as *mut c_void, 1024) };
    if rc <= 0 {
        return None;
    }
    let s = c_str(&buf[..rc.min(1024) as usize]);
    if s.is_empty() {
        None
    } else {
        Some(vec![s])
    }
}

/// own-uid sockets keyed by the same flow inode nettop rows use, so the
/// CONNS box can attribute pids exactly like the Linux scan_pids path
#[cfg(target_os = "macos")]
pub fn socket_map() -> HashMap<u64, (u32, String)> {
    let mut out = HashMap::new();
    // 首调(NULL, 0)返回 pid 个数;重调的 buffersize 按“字节”解释
    let pid_count = unsafe { proc_listallpids(std::ptr::null_mut(), 0) };
    if pid_count <= 0 {
        return out;
    }
    let mut pids = vec![0i32; pid_count as usize];
    let n = unsafe {
        proc_listallpids(
            pids.as_mut_ptr() as *mut c_void,
            (pids.len() * std::mem::size_of::<i32>()) as c_int,
        )
    };
    if n <= 0 {
        return out;
    }
    for &pid in &pids[..n as usize] {
        let Some(fds) = socket_fds(pid as u32) else {
            continue;
        };
        if fds.is_empty() {
            continue;
        }
        let mut buf = [0u8; SOCKET_INFO_LEN];
        let mut comm: Option<String> = None;
        for fd in fds {
            #[cfg(target_os = "macos")]
            let len = unsafe {
                proc_pidfdinfo(
                    pid as c_int,
                    fd,
                    PROC_PIDFDSOCKETINFO,
                    buf.as_mut_ptr() as *mut c_void,
                    SOCKET_INFO_LEN as c_int,
                )
            };
            #[cfg(not(target_os = "macos"))]
            let len: c_int = 0;
            if len <= 0 {
                continue;
            }
            if comm.is_none() {
                comm = cmdline(pid as u32)
                    .and_then(|v| v.into_iter().next())
                    .map(|p| {
                        std::path::Path::new(&p)
                            .file_name()
                            .map(|s| s.to_string_lossy().into_owned())
                            .unwrap_or(p)
                    });
            }
            if let Some((proto, local, remote)) = socket_tuple(&buf) {
                let ino = flow_inode(proto, &local, &remote);
                out.entry(ino).or_insert_with(|| {
                    (
                        pid as u32,
                        comm.clone().unwrap_or_else(|| "?".into()),
                    )
                });
            }
        }
    }
    out
}

/// socket fds of a pid via PROC_PIDLISTFDS (empty when unreadable)
#[cfg(target_os = "macos")]
fn socket_fds(pid: u32) -> Option<Vec<i32>> {
    let n = unsafe { proc_pidinfo(pid as c_int, PROC_PIDLISTFDS, 0, std::ptr::null_mut(), 0) };
    if n <= 0 {
        return None;
    }
    // n = number of proc_fdinfo entries (8 bytes each)
    let mut buf = vec![0u8; n as usize * 8];
    let got = unsafe {
        proc_pidinfo(
            pid as c_int,
            PROC_PIDLISTFDS,
            0,
            buf.as_mut_ptr() as *mut c_void,
            buf.len() as c_int,
        )
    };
    if got <= 0 {
        return None;
    }
    let mut out = Vec::new();
    for i in 0..(got as usize / 8) {
        let fd = i32_at(&buf, i * 8);
        let fdtype = u32_at(&buf, i * 8 + 4);
        if fdtype == PROX_FDTYPE_SOCKET {
            out.push(fd);
        }
    }
    Some(out)
}

/// (proto, local, remote) — 与 nettop 流行同构的端点三元组
pub type SockTuple = (u8, (std::net::IpAddr, u16), (std::net::IpAddr, u16));

/// (proto, local, remote) projected out of a socket_fdinfo buffer.
pub fn socket_tuple(buf: &[u8]) -> Option<SockTuple> {
    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    if buf.len() < SOCKET_INFO_LEN {
        return None;
    }
    let family = buf[SOI_FAMILY] as libc::c_int;
    let proto = u32_at(buf, SOI_PROTOCOL) as u8;
    if proto != 6 && proto != 17 {
        return None; // 只关心 TCP/UDP(unix 域等忽略)
    }
    let port_at = |off: usize| u16::from_be((u32_at(buf, off) & 0xffff) as u16);
    let addr_at = |off: usize| -> IpAddr {
        let mut o = [0u8; 16];
        o.copy_from_slice(&buf[off..off + 16]);
        if family == AF_INET6_DARWIN {
            // v6 socket 上的 v4 连接,内核 in4in6 有两种呈现:映射形式
            // ::ffff:a.b.c.d(dual-stack)与兼容形式 ::a.b.c.d(实测入站
            // 连接是这种)。nettop 流行一律按 `tcp4` + 点分地址显示,两种
            // 都要归一成 V4 才能键匹配;`::` 未指定与真 v6 保持 V6。
            let v4 = |b: &[u8; 16]| Ipv4Addr::new(b[12], b[13], b[14], b[15]);
            if (o[..10].iter().all(|&b| b == 0) && o[10] == 0xff && o[11] == 0xff)
                || (o[..12].iter().all(|&b| b == 0) && o[12..16].iter().any(|&b| b != 0))
            {
                IpAddr::V4(v4(&o))
            } else {
                IpAddr::V6(Ipv6Addr::from(o))
            }
        } else {
            IpAddr::V4(Ipv4Addr::new(o[12], o[13], o[14], o[15]))
        }
    };
    let local = (addr_at(INSI_LADDR), port_at(INSI_LPORT));
    let remote = (addr_at(INSI_FADDR), port_at(INSI_FPORT));
    Some((proto, local, remote))
}

fn i32_at(b: &[u8], off: usize) -> i32 {
    i32::from_ne_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]])
}

fn u32_at(b: &[u8], off: usize) -> u32 {
    u32::from_ne_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]])
}

fn u64_at(b: &[u8], off: usize) -> u64 {
    u64::from_ne_bytes([
        b[off],
        b[off + 1],
        b[off + 2],
        b[off + 3],
        b[off + 4],
        b[off + 5],
        b[off + 6],
        b[off + 7],
    ])
}

fn i64_at(b: &[u8], off: usize) -> i64 {
    i64_at_bytes(b, off)
}

fn i64_at_bytes(b: &[u8], off: usize) -> i64 {
    let mut v = [0u8; 8];
    v.copy_from_slice(&b[off..off + 8]);
    i64::from_ne_bytes(v)
}

fn c_str(b: &[u8]) -> String {
    let end = b.iter().position(|&c| c == 0).unwrap_or(b.len());
    String::from_utf8_lossy(&b[..end]).into_owned()
}

/// C header cross-check fixture: the offsets above were pinned with
/// `offsetof`/`sizeof` on the target SDK. This test re-checks the pure
/// projection against a synthetic socket_info-shaped buffer (both platforms;
/// the numeric constants are ABI-stable and covered by the Air runtime test).
#[cfg(test)]
mod tests {
    use super::*;
    use crate::collect::nettop::flow_inode;
    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

    fn sample_socket_info(v6: bool, lport: u16, fport: u16) -> Vec<u8> {
        let mut b = vec![0u8; SOCKET_INFO_LEN];
        // 仿真 wrapper:pfi 头 24 字节(全零)后是 socket_info
        b[SOI_FAMILY] = if v6 { 30 } else { 2 }; // AF_INET6 / AF_INET
        b[SOI_PROTOCOL..SOI_PROTOCOL + 4].copy_from_slice(&6u32.to_ne_bytes());
        // 真实布局:int 低 16 位是字节序交换后的端口(内存前两字节 = 端口 BE)
        b[INSI_LPORT..INSI_LPORT + 2].copy_from_slice(&lport.to_be_bytes());
        b[INSI_FPORT..INSI_FPORT + 2].copy_from_slice(&fport.to_be_bytes());
        if v6 {
            // 用全局单播地址代表"真 v6"(::1 的全零前缀会被 compatible
            // 分支归成 v4,是既定的取舍)
            b[INSI_LADDR..INSI_LADDR + 16]
                .copy_from_slice(&"2001:db8::1".parse::<Ipv6Addr>().unwrap().octets());
            b[INSI_FADDR..INSI_FADDR + 16]
                .copy_from_slice(&Ipv6Addr::new(0xfe80, 0, 0, 0, 1, 2, 3, 4).octets());
        } else {
            // in4in6: v4 在 16 字节的最后 4 字节
            b[INSI_LADDR + 12..INSI_LADDR + 16].copy_from_slice(&[127, 0, 0, 1]);
            b[INSI_FADDR + 12..INSI_FADDR + 16].copy_from_slice(&[10, 1, 2, 3]);
        }
        b
    }

    #[test]
    fn projects_tcp_v4_tuple() {
        let b = sample_socket_info(false, 22, 51000);
        let (proto, local, remote) = socket_tuple(&b).unwrap();
        assert_eq!(proto, 6);
        assert_eq!(local, (IpAddr::V4(Ipv4Addr::LOCALHOST), 22));
        assert_eq!(remote, (IpAddr::V4("10.1.2.3".parse().unwrap()), 51000));
    }

    #[test]
    fn projects_tcp_v6_tuple() {
        let b = sample_socket_info(true, 443, 0);
        let (proto, local, remote) = socket_tuple(&b).unwrap();
        assert_eq!(proto, 6);
        assert_eq!(local, (IpAddr::V6("2001:db8::1".parse().unwrap()), 443));
        assert_eq!(
            remote,
            (IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 1, 2, 3, 4)), 0)
        );
    }

    #[test]
    fn v4_mapped_addresses_normalize_to_v4_like_nettop() {
        // v6 socket 上的 v4 连接有两种内核呈现:mapped(::ffff:…)与
        // compatible(::a.b.c.d,真机入站连接实测)。nettop 流行按 tcp4 +
        // v4 显示 —— 两侧键必须一致(Linux normalize 同法)
        let mut b = sample_socket_info(true, 22000, 37142);
        let octets = |ip: [u8; 4]| {
            let mut o = [0u8; 16];
            o[12..16].copy_from_slice(&ip);
            o
        };
        b[INSI_LADDR..INSI_LADDR + 16].copy_from_slice(&octets([192, 168, 121, 111]));
        // remote 用 mapped 形式(两种都要归一)
        let mut mapped = [0u8; 16];
        mapped[10] = 0xff;
        mapped[11] = 0xff;
        mapped[12..16].copy_from_slice(&[192, 168, 121, 190]);
        b[INSI_FADDR..INSI_FADDR + 16].copy_from_slice(&mapped);
        let (proto, local, remote) = socket_tuple(&b).unwrap();
        assert_eq!(
            local,
            (IpAddr::V4("192.168.121.111".parse().unwrap()), 22000)
        );
        assert_eq!(
            remote,
            (IpAddr::V4("192.168.121.190".parse().unwrap()), 37142)
        );
        assert_eq!(proto, 6);
        // 与 nettop 的 tcp4 流行键一致
        let nl = crate::collect::nettop::parse_endpoint("192.168.121.111:22000", false).unwrap();
        let nr = crate::collect::nettop::parse_endpoint("192.168.121.190:37142", false).unwrap();
        assert_eq!(
            crate::collect::nettop::flow_inode(6, &local, &remote),
            crate::collect::nettop::flow_inode(6, &nl, &nr)
        );
        // 全局单播真 v6 保持 V6;::1 的全零前缀被 compatible 分支归 v4
        // (键不匹配则不归属,宁缺勿错,注释见实现)
        let mut b2 = sample_socket_info(true, 443, 0);
        b2[INSI_FADDR..INSI_FADDR + 16]
            .copy_from_slice(&"2001:db8::1".parse::<Ipv6Addr>().unwrap().octets());
        let (_, _, remote2) = socket_tuple(&b2).unwrap();
        assert_eq!(remote2.0, IpAddr::V6("2001:db8::1".parse().unwrap()));
    }

    #[test]
    fn socket_tuple_keys_match_nettop_flow_inodes() {
        // 归属的前提:同一 socket 在 nettop 流行里的键与 socket_info 投影一致。
        // 监听 socket:laddr/faddr 全零,nettop 行是 `tcp4 *:22<->*:*`
        let mut b = sample_socket_info(false, 22, 0);
        b[INSI_LADDR..INSI_LADDR + 16].fill(0);
        b[INSI_FADDR..INSI_FADDR + 16].fill(0);
        let (proto, local, remote) = socket_tuple(&b).unwrap();
        let nettop_local = parse_ep("*:22", false);
        let nettop_remote = parse_ep("*:*", false);
        assert_eq!(
            flow_inode(proto, &local, &remote),
            flow_inode(6, &nettop_local, &nettop_remote)
        );
    }

    fn parse_ep(s: &str, v6: bool) -> (IpAddr, u16) {
        crate::collect::nettop::parse_endpoint(s, v6).unwrap()
    }

    /// 诊断:打印指定进程 fd 扫描的原始投影(仅 mac、人工排查用);
    /// 无参数时扫 syncthing(同 uid 真实服务),验证跨进程 fd 可读性
    #[test]
    #[cfg(target_os = "macos")]
    #[ignore = "diagnostic"]
    fn socket_map_debug_lists_own_sockets() {
        use std::net::TcpListener;
        let l = TcpListener::bind("127.0.0.1:0").unwrap();
        let port = l.local_addr().unwrap().port();
        let me = std::process::id() as c_int;
        let fds = socket_fds(me as u32).expect("own fd list readable");
        eprintln!("port={port} own socket fds: {fds:?}");
        let mut buf = vec![0u8; SOCKET_INFO_LEN];
        for fd in fds {
            let rc = unsafe {
                proc_pidfdinfo(
                    me,
                    fd,
                    PROC_PIDFDSOCKETINFO,
                    buf.as_mut_ptr() as *mut c_void,
                    SOCKET_INFO_LEN as c_int,
                )
            };
            eprintln!("fd {fd} rc={rc} tuple={:?}", socket_tuple(&buf));
        }
        // 跨进程:同 uid 的 syncthing(全 fd 投影 + nettop 键对照)
        if let Some(m) = list_pids().into_iter().find(|m| m.comm.starts_with("syncthing")) {
            let fds = socket_fds(m.pid).unwrap_or_default();
            eprintln!("syncthing pid={} fds={:?} ({} 个)", m.pid, fds, fds.len());
            for fd in fds {
                let rc = unsafe {
                    proc_pidfdinfo(
                        m.pid as c_int,
                        fd,
                        PROC_PIDFDSOCKETINFO,
                        buf.as_mut_ptr() as *mut c_void,
                        SOCKET_INFO_LEN as c_int,
                    )
                };
                if let Some(t) = socket_tuple(&buf) {
                    eprintln!(
                        "  fd {fd} rc={rc} tuple={t:?} ino={:016x}",
                        crate::collect::nettop::flow_inode(t.0, &t.1, &t.2)
                    );
                } else {
                    eprintln!("  fd {fd} rc={rc} tuple=None");
                }
            }
        } else {
            eprintln!("syncthing 不在运行");
        }
    }
}

#[cfg(test)]
#[cfg(target_os = "macos")]
mod live_tests {
    /// 诊断:proc_cwd 真机验证(仅 mac、人工排查用)
    #[test]
    #[ignore = "diagnostic"]
    fn proc_cwd_live_debug() {
        eprintln!(
            "own cwd: {:?}",
            super::proc_cwd(std::process::id())
        );
    }
}