subc-os 0.1.51

Operating-system primitives for the subc daemon that need unsafe code, behind a safe API: per-process identity, signalling, memory and CPU usage, and the launch-nonce descriptor handoff.
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
//! The process's single registration connection. [`supervised`] caches the
//! validation decision once, independently of whether the connection has been
//! taken. [`take_registration_channel`] transfers ownership exactly once; a
//! second take is an error, never permission to connect as `direct`. Refused
//! descriptors are left untouched and reported by [`registration_refusal`].
//! Errors after validation are fatal adoption errors, not refusals.
//!
//! Call an accessor before spawning anything: adoption sets close-on-exec on
//! Unix. An inherited variable without the descriptor does not confer identity.
//! The SDK owns framing, claims, and process exit on registration-channel EOF.
//!
//! [`HelperClaimHandoff`] passes only an explicit `helper` claim, never a launch
//! nonce. Unix uses an inherited one-read pipe. Windows reserves a single-use
//! named pipe and checks both process IDs; it does not commandeer stdin, which
//! helpers may need for stdio protocols. Only pipe metadata enters the child's
//! environment. The daemon's nonce handoff to ungated spawns is unchanged.
//! [`PreparedHelperClaimHandoff`] separates preparation from delivery for runtimes
//! that own their child process: put its child descriptor in the chosen stdio
//! slot, spawn, then complete with the live child's PID and a freshly minted claim.
//! Drop it on a spawn error. [`HelperClaimHandoff`] is the convenience wrapper
//! for Rust callers using `std::process::Command` with a claim already minted.

use std::{
    ffi::OsString,
    fmt,
    sync::{Mutex, OnceLock},
};

#[cfg(all(windows, feature = "tokio"))]
mod tokio_pipe;

pub const REGISTRATION_FD_ENV: &str = "SUBC_REGISTRATION_FD";
pub const REGISTRATION_PIPE_ENV: &str = "SUBC_REGISTRATION_PIPE";
pub const DAEMON_PID_ENV: &str = "SUBC_DAEMON_PID";
pub const HELPER_CLAIM_FD_ENV: &str = "SUBC_HELPER_CLAIM_FD";
pub const HELPER_CLAIM_PIPE_ENV: &str = "SUBC_HELPER_CLAIM_PIPE";
pub const HELPER_PARENT_PID_ENV: &str = "SUBC_HELPER_PARENT_PID";

/// A single duplex registration instance reserved before process creation.
/// Retaining this value keeps the name reserved even after a foreign client is
/// disconnected. No stream is exposed until the direct child's PID is checked.
#[cfg(windows)]
pub struct RegistrationPipe(
    crate::LaunchNoncePipeHandoff,
    std::sync::Arc<std::sync::atomic::AtomicBool>,
);

/// Cancellation for an owned registration instance, before or after acceptance.
/// Disconnect is synchronous; closing this duplicate never releases another owner's handle.
#[cfg(windows)]
#[derive(Debug)]
pub struct RegistrationPipeShutdown {
    pipe: std::fs::File,
    cancelled: std::sync::Arc<std::sync::atomic::AtomicBool>,
}

#[cfg(windows)]
impl RegistrationPipeShutdown {
    pub fn shutdown(self) {
        use std::os::windows::io::AsRawHandle;
        self.cancelled
            .store(true, std::sync::atomic::Ordering::Release);
        // SAFETY: this owned duplicate keeps the original server instance alive
        // throughout disconnect. An already-disconnected instance is harmless.
        #[allow(unsafe_code)]
        unsafe {
            windows_sys::Win32::System::Pipes::DisconnectNamedPipe(self.pipe.as_raw_handle())
        };
    }
}

#[cfg(windows)]
impl RegistrationPipe {
    pub fn new() -> std::io::Result<Self> {
        crate::LaunchNoncePipeHandoff::registration().map(|pipe| {
            Self(
                pipe,
                std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
            )
        })
    }

    pub fn name(&self) -> &str {
        self.0.name()
    }

    pub fn shutdown_handle(&self) -> std::io::Result<RegistrationPipeShutdown> {
        Ok(RegistrationPipeShutdown {
            pipe: self.0.registration_shutdown_handle()?,
            cancelled: self.1.clone(),
        })
    }

    /// Accept only the owned child's connection, without reading any bytes.
    /// The caller must still own the child while its process handle is pinned.
    /// Use the spawn's registration deadline, not the legacy nonce serve window.
    /// The returned file owns an overlapped handle, not a synchronous stream.
    pub fn accept(self, pid: u32, deadline: std::time::Instant) -> std::io::Result<std::fs::File> {
        self.0.accept_registration(pid, deadline, &self.1)
    }
}

/// Parent-owned sources for the final, allocation-free descriptor placement.
/// Sources stay above all four reserved slots so stdio setup and placement
/// callbacks cannot overwrite one before it is copied.
#[cfg(unix)]
#[derive(Default)]
pub struct ChildDescriptorSlots {
    pub(crate) sources: [Option<std::os::fd::OwnedFd>; 4],
}

#[cfg(unix)]
#[allow(unsafe_code)]
impl ChildDescriptorSlots {
    pub fn nonce(&mut self, nonce: crate::LaunchNonceHandoff) {
        self.sources[0] = Some(nonce.into_descriptor());
    }

    pub fn channel(&mut self, channel: std::os::fd::OwnedFd) -> std::io::Result<()> {
        self.sources[1] = Some(outside_child_slots(channel)?);
        Ok(())
    }

    pub fn acknowledgement(&mut self, ack: std::os::fd::OwnedFd) -> std::io::Result<()> {
        self.sources[2] = Some(outside_child_slots(ack)?);
        Ok(())
    }

    /// Install the script transport at fd 6, away from runtime IPC at fd 3.
    pub fn script(&mut self, reader: std::os::fd::OwnedFd) -> std::io::Result<()> {
        self.sources[3] = Some(outside_child_slots(reader)?);
        Ok(())
    }

    /// Install after the placement barrier. Drop the command immediately after
    /// spawn to close the parent's copies of the child-only descriptors.
    pub fn install(self, command: &mut std::process::Command) {
        use std::os::unix::process::CommandExt;
        // SAFETY: sources are owned by the callback. Only dup2 and fcntl run in
        // the forked child; no allocation, destructors or locks run there.
        unsafe { command.pre_exec(move || self.place()) };
    }

    pub(crate) fn place(&self) -> std::io::Result<()> {
        use std::os::fd::AsRawFd;
        for (index, source) in self.sources.iter().enumerate() {
            if let Some(source) = source {
                let slot = index as i32 + 3;
                // SAFETY: parent preparation owns all sources above slot 6.
                place_descriptor(source.as_raw_fd(), slot)?;
            }
        }
        Ok(())
    }
}

#[cfg(unix)]
#[allow(unsafe_code)]
pub(crate) fn place_descriptor(source: i32, slot: i32) -> std::io::Result<()> {
    // SAFETY: callers retain the source across fork. Both calls are
    // async-signal-safe, and an errno-only error needs no allocation.
    if unsafe { libc::dup2(source, slot) } == -1
        || unsafe { libc::fcntl(slot, libc::F_SETFD, 0) } == -1
    {
        return Err(std::io::Error::last_os_error());
    }
    Ok(())
}

#[cfg(unix)]
#[allow(unsafe_code)]
pub(crate) fn outside_child_slots(
    descriptor: std::os::fd::OwnedFd,
) -> std::io::Result<std::os::fd::OwnedFd> {
    use std::os::fd::{AsRawFd, FromRawFd};
    // SAFETY: duplicate a live descriptor, retaining CLOEXEC in the parent.
    let copy = unsafe { libc::fcntl(descriptor.as_raw_fd(), libc::F_DUPFD_CLOEXEC, 7) };
    if copy == -1 {
        return Err(std::io::Error::last_os_error());
    }
    // SAFETY: fcntl returned a new descriptor owned only here.
    Ok(unsafe { std::os::fd::OwnedFd::from_raw_fd(copy) })
}

/// A socketpair ready for a supervised Unix spawn. Creation and Darwin's
/// CLOEXEC setup hold the same lock as every daemon fork.
#[cfg(unix)]
#[allow(unsafe_code)]
pub fn registration_socketpair(
) -> std::io::Result<(std::os::unix::net::UnixStream, std::os::fd::OwnedFd, String)> {
    use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
    let _guard = crate::child_spawn::creation_lock();
    let mut pair = [-1; 2];
    #[cfg(target_os = "linux")]
    let kind = libc::SOCK_STREAM | libc::SOCK_CLOEXEC;
    #[cfg(not(target_os = "linux"))]
    let kind = libc::SOCK_STREAM;
    // SAFETY: socketpair writes exactly two descriptors into the local array.
    if unsafe { libc::socketpair(libc::AF_UNIX, kind, 0, pair.as_mut_ptr()) } == -1 {
        return Err(std::io::Error::last_os_error());
    }
    // SAFETY: both newly created descriptors are uniquely owned here.
    let (parent, child) = unsafe { (OwnedFd::from_raw_fd(pair[0]), OwnedFd::from_raw_fd(pair[1])) };
    #[cfg(not(target_os = "linux"))]
    for fd in [&parent, &child] {
        // SAFETY: creation and flag setup are serialized against every fork.
        if unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_SETFD, libc::FD_CLOEXEC) } == -1 {
            return Err(std::io::Error::last_os_error());
        }
    }
    let parent = outside_child_slots(parent)?;
    let child = outside_child_slots(child)?;
    let mut stat = std::mem::MaybeUninit::<libc::stat>::uninit();
    // SAFETY: fstat initializes the local buffer on success.
    if unsafe { libc::fstat(child.as_raw_fd(), stat.as_mut_ptr()) } == -1 {
        return Err(std::io::Error::last_os_error());
    }
    let inode = unsafe { stat.assume_init() }.st_ino;
    Ok((parent.into(), child, format!("4:{inode}")))
}

/// Why a named channel was not adopted. No variant contains a path or a secret.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RegistrationRefusal {
    Malformed,
    NotOpen,
    NotSocket,
    WrongInode,
    NamedSocket,
    WrongPeer,
    Closed,
    Unsupported,
}

impl fmt::Display for RegistrationRefusal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Malformed => "invalid registration metadata",
            Self::NotOpen => "registration channel is not open",
            Self::NotSocket => "registration descriptor is not a socket",
            Self::WrongInode => "registration socket identity does not match",
            Self::NamedSocket => "registration socket or its peer is named",
            Self::WrongPeer => "registration peer is not the expected process",
            Self::Closed => "registration channel is already closed",
            Self::Unsupported => "registration channel is unsupported on this platform",
        })
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RegistrationChannelError {
    AlreadyTaken,
    Adoption { errno: Option<i32> },
}

impl fmt::Display for RegistrationChannelError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::AlreadyTaken => f.write_str("registration channel already taken"),
            Self::Adoption { .. } => f.write_str("could not adopt validated registration channel"),
        }
    }
}
impl std::error::Error for RegistrationChannelError {}

/// Uniquely owned transport with no cloning API.
pub struct RegistrationChannel {
    #[cfg(unix)]
    stream: std::os::unix::net::UnixStream,
    #[cfg(windows)]
    file: std::fs::File,
}

impl fmt::Debug for RegistrationChannel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("RegistrationChannel(<redacted>)")
    }
}

impl RegistrationChannel {
    #[cfg(unix)]
    pub fn into_unix_stream(self) -> std::os::unix::net::UnixStream {
        self.stream
    }

    /// The file owns an overlapped, non-inheritable duplex pipe handle. Transfer
    /// it to an overlapped transport (for example Tokio), not synchronous I/O.
    #[cfg(windows)]
    pub fn into_file(self) -> std::fs::File {
        self.file
    }
}

struct Decision {
    supervised: bool,
    refusal: Option<RegistrationRefusal>,
    channel: Mutex<Result<Option<RegistrationChannel>, RegistrationChannelError>>,
}

impl Decision {
    fn refused(refusal: Option<RegistrationRefusal>) -> Self {
        Self {
            supervised: false,
            refusal,
            channel: Mutex::new(Ok(None)),
        }
    }

    fn validated(channel: Result<RegistrationChannel, RegistrationChannelError>) -> Self {
        Self {
            supervised: true,
            refusal: None,
            channel: Mutex::new(channel.map(Some)),
        }
    }

    fn take(&self) -> Result<Option<RegistrationChannel>, RegistrationChannelError> {
        if !self.supervised {
            return Ok(None);
        }
        let mut channel = self.channel.lock().unwrap_or_else(|e| e.into_inner());
        std::mem::replace(&mut *channel, Err(RegistrationChannelError::AlreadyTaken))
    }
}

struct ChannelCell(OnceLock<Decision>);
impl ChannelCell {
    const fn new() -> Self {
        Self(OnceLock::new())
    }
    fn get(&self, lookup: impl FnMut(&str) -> Option<OsString>) -> &Decision {
        self.0.get_or_init(|| decide(lookup))
    }
}
static PROCESS_CHANNEL: ChannelCell = ChannelCell::new();

fn process_decision() -> &'static Decision {
    PROCESS_CHANNEL.get(|key| std::env::var_os(key))
}

/// Whether this process validated its registration channel, even after a take.
pub fn supervised() -> bool {
    process_decision().supervised
}

/// Take the one registered connection. Every error is fatal to an SDK caller;
/// only `Ok(None)` permits an unsupervised connection.
pub fn take_registration_channel() -> Result<Option<RegistrationChannel>, RegistrationChannelError>
{
    process_decision().take()
}

/// A safe-to-log explanation for a refused handoff, cached with the decision.
pub fn registration_refusal() -> Option<RegistrationRefusal> {
    process_decision().refusal
}

fn pid(value: Option<OsString>) -> Result<u32, RegistrationRefusal> {
    value
        .and_then(|v| v.into_string().ok())
        .and_then(|v| v.parse().ok())
        .filter(|pid| *pid > 0)
        .ok_or(RegistrationRefusal::Malformed)
}

fn decide(mut lookup: impl FnMut(&str) -> Option<OsString>) -> Decision {
    #[cfg(unix)]
    let value = lookup(REGISTRATION_FD_ENV);
    #[cfg(windows)]
    let value = lookup(REGISTRATION_PIPE_ENV);
    #[cfg(not(any(unix, windows)))]
    let value = lookup(REGISTRATION_FD_ENV).or_else(|| lookup(REGISTRATION_PIPE_ENV));
    let Some(value) = value else {
        return Decision::refused(None);
    };
    let daemon = match pid(lookup(DAEMON_PID_ENV)) {
        Ok(pid) => pid,
        Err(reason) => return Decision::refused(Some(reason)),
    };
    #[cfg(unix)]
    return unix::adopt(&value, daemon);
    #[cfg(windows)]
    return windows::adopt(&value, daemon);
    #[cfg(not(any(unix, windows)))]
    {
        let _ = (value, daemon);
        Decision::refused(Some(RegistrationRefusal::Unsupported))
    }
}

#[cfg(unix)]
#[allow(unsafe_code)]
mod unix {
    use super::*;
    use std::{
        ffi::OsStr,
        io,
        mem::{size_of, zeroed},
        os::fd::{FromRawFd, RawFd},
    };

    fn stat(fd: RawFd) -> io::Result<libc::stat> {
        // SAFETY: fstat initializes the local output on success.
        let mut stat = unsafe { zeroed() };
        if unsafe { libc::fstat(fd, &mut stat) } == -1 {
            return Err(io::Error::last_os_error());
        }
        Ok(stat)
    }

    fn unnamed(fd: RawFd, peer: bool) -> bool {
        // SAFETY: both socket-name calls receive a sockaddr_un-sized buffer.
        let mut addr: libc::sockaddr_un = unsafe { zeroed() };
        let mut len = size_of::<libc::sockaddr_un>() as libc::socklen_t;
        let result = unsafe {
            if peer {
                libc::getpeername(fd, (&mut addr as *mut libc::sockaddr_un).cast(), &mut len)
            } else {
                libc::getsockname(fd, (&mut addr as *mut libc::sockaddr_un).cast(), &mut len)
            }
        };
        // Abstract Unix addresses start with NUL too. Only the absence of a
        // path, not its first byte, distinguishes an unnamed socket on Linux.
        #[cfg(target_os = "macos")]
        let no_path = {
            // Darwin returns a padded sockaddr for unnamed socketpairs. It
            // has no abstract namespace, so an all-zero path is still unnamed.
            #[cfg(test)]
            eprintln!(
                "Darwin socket name: peer={peer} len={len} family={} zero_path={}",
                addr.sun_family,
                addr.sun_path.iter().all(|byte| *byte == 0)
            );
            addr.sun_path.iter().all(|byte| *byte == 0)
        };
        #[cfg(not(target_os = "macos"))]
        let no_path = len as usize <= std::mem::offset_of!(libc::sockaddr_un, sun_path);
        result == 0 && addr.sun_family as i32 == libc::AF_UNIX && no_path
    }

    pub(super) fn peer_pid(fd: RawFd) -> Option<u32> {
        #[cfg(target_os = "linux")]
        {
            // SAFETY: SO_PEERCRED fills a ucred of the declared size.
            let mut cred: libc::ucred = unsafe { zeroed() };
            let mut len = size_of::<libc::ucred>() as libc::socklen_t;
            let ok = unsafe {
                libc::getsockopt(
                    fd,
                    libc::SOL_SOCKET,
                    libc::SO_PEERCRED,
                    (&mut cred as *mut libc::ucred).cast(),
                    &mut len,
                )
            } == 0;
            (ok && len as usize == size_of::<libc::ucred>() && cred.pid > 0)
                .then_some(cred.pid as u32)
        }
        #[cfg(target_os = "macos")]
        {
            let mut pid: libc::pid_t = 0;
            let mut len = size_of::<libc::pid_t>() as libc::socklen_t;
            // SAFETY: LOCAL_PEERPID fills a pid_t of the declared size.
            let ok = unsafe {
                libc::getsockopt(
                    fd,
                    libc::SOL_LOCAL,
                    libc::LOCAL_PEERPID,
                    (&mut pid as *mut libc::pid_t).cast(),
                    &mut len,
                )
            } == 0;
            (ok && len as usize == size_of::<libc::pid_t>() && pid > 0).then_some(pid as u32)
        }
        #[cfg(not(any(target_os = "linux", target_os = "macos")))]
        {
            let _ = fd;
            None
        }
    }

    pub(super) fn adopt(value: &OsStr, daemon: u32) -> Decision {
        adopt_with_stat(value, daemon, stat)
    }

    pub(super) fn adopt_with_stat(
        value: &OsStr,
        daemon: u32,
        stat: impl Fn(RawFd) -> io::Result<libc::stat>,
    ) -> Decision {
        let validate = || {
            let (fd, inode) = value
                .to_str()
                .and_then(|v| v.split_once(':'))
                .and_then(|(fd, inode)| {
                    Some((fd.parse::<RawFd>().ok()?, inode.parse::<u64>().ok()?))
                })
                .filter(|(fd, _)| *fd >= 0)
                .ok_or(RegistrationRefusal::Malformed)?;
            let stat = stat(fd).map_err(|_| RegistrationRefusal::NotOpen)?;
            if stat.st_mode & libc::S_IFMT != libc::S_IFSOCK {
                return Err(RegistrationRefusal::NotSocket);
            }
            // Darwin commonly reports zero for socket inodes, on either end: the
            // daemon's fstat may read zero for a socket the child later reads as
            // nonzero. When both sides know an inode it stays binding; when
            // either side read zero, the check falls back to socket names and
            // peer PID.
            // ino_t is narrower than u64 on some Unix targets.
            #[allow(clippy::unnecessary_cast)]
            let actual_inode = stat.st_ino as u64;
            if !inode_matches(actual_inode, inode) {
                return Err(RegistrationRefusal::WrongInode);
            }
            if !unnamed(fd, false) || !unnamed(fd, true) {
                return Err(RegistrationRefusal::NamedSocket);
            }
            #[cfg(not(any(target_os = "linux", target_os = "macos")))]
            return Err(RegistrationRefusal::Unsupported);
            #[cfg(any(target_os = "linux", target_os = "macos"))]
            if peer_pid(fd) != Some(daemon) {
                return Err(RegistrationRefusal::WrongPeer);
            }
            // Older SDKs can leak this endpoint through exec. The daemon shuts
            // it down when the owner registers by nonce, so it no longer proves
            // supervision. Peek without consuming handshake bytes or blocking.
            let mut byte = 0_u8;
            loop {
                let read = unsafe {
                    libc::recv(
                        fd,
                        std::ptr::from_mut(&mut byte).cast(),
                        1,
                        libc::MSG_PEEK | libc::MSG_DONTWAIT,
                    )
                };
                if read > 0 {
                    break;
                }
                let error = io::Error::last_os_error();
                if read < 0 && error.kind() == io::ErrorKind::Interrupted {
                    continue;
                }
                if read < 0 && error.kind() == io::ErrorKind::WouldBlock {
                    break;
                }
                return Err(RegistrationRefusal::Closed);
            }
            Ok(fd)
        };
        let fd = match validate() {
            Ok(fd) => fd,
            Err(reason) => return Decision::refused(Some(reason)),
        };
        // SAFETY: all checks passed. This process owns the inherited descriptor;
        // the OnceLock prevents any second adoption or independent reader.
        let stream = unsafe { std::os::unix::net::UnixStream::from_raw_fd(fd) };
        // Preserve existing flags, but ensure no subsequent exec inherits it.
        let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
        let channel = if flags == -1
            || unsafe { libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) } == -1
        {
            Err(RegistrationChannelError::Adoption {
                errno: io::Error::last_os_error().raw_os_error(),
            })
        } else {
            Ok(RegistrationChannel { stream })
        };
        Decision::validated(channel)
    }

    pub(super) fn inode_matches(actual: u64, expected: u64) -> bool {
        (cfg!(target_os = "macos") && (actual == 0 || expected == 0)) || actual == expected
    }
}

#[cfg(windows)]
#[allow(unsafe_code)]
mod windows {
    use super::*;
    use std::{
        ffi::OsStr,
        os::windows::{
            ffi::OsStrExt,
            io::{AsRawHandle, FromRawHandle, OwnedHandle},
        },
        ptr::{null, null_mut},
        time::{Duration, Instant},
    };
    use windows_sys::Win32::{
        Foundation::{ERROR_PIPE_BUSY, GENERIC_READ, INVALID_HANDLE_VALUE},
        Storage::FileSystem::{
            CreateFileW, FILE_FLAG_OVERLAPPED, FILE_WRITE_DATA, OPEN_EXISTING,
            SECURITY_IDENTIFICATION, SECURITY_SQOS_PRESENT,
        },
        System::Pipes::GetNamedPipeServerProcessId,
    };

    // A busy instance may still be finishing its previous connect attempt. Do
    // not turn a refused or unavailable channel into an unbounded startup wait.
    const OPEN_WINDOW: Duration = Duration::from_millis(500);

    pub(super) fn adopt(name: &OsStr, daemon: u32) -> Decision {
        let name: Vec<u16> = name.encode_wide().chain(Some(0)).collect();
        if name.len() <= 1 || name[..name.len() - 1].contains(&0) {
            return Decision::refused(Some(RegistrationRefusal::Malformed));
        }
        let deadline = Instant::now() + OPEN_WINDOW;
        let pipe = loop {
            // SAFETY: valid NUL-terminated name; null security attributes make
            // the handle non-inheritable. Identification prevents impersonation.
            let handle = unsafe {
                CreateFileW(
                    name.as_ptr(),
                    // GENERIC_WRITE also requests FILE_CREATE_PIPE_INSTANCE.
                    // A client needs data writes, never server-creation rights.
                    GENERIC_READ | FILE_WRITE_DATA,
                    0,
                    null(),
                    OPEN_EXISTING,
                    FILE_FLAG_OVERLAPPED | SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION,
                    null_mut(),
                )
            };
            if handle != INVALID_HANDLE_VALUE && !handle.is_null() {
                // SAFETY: CreateFileW returned a fresh owned handle.
                break unsafe { OwnedHandle::from_raw_handle(handle) };
            }
            if std::io::Error::last_os_error().raw_os_error() != Some(ERROR_PIPE_BUSY as i32)
                || Instant::now() >= deadline
            {
                return Decision::refused(Some(RegistrationRefusal::NotOpen));
            }
            std::thread::sleep(Duration::from_millis(5));
        };
        let mut server = 0;
        // SAFETY: live pipe and writable scalar output. No bytes have been sent.
        if unsafe { GetNamedPipeServerProcessId(pipe.as_raw_handle(), &mut server) } == 0
            || server != daemon
        {
            return Decision::refused(Some(RegistrationRefusal::WrongPeer));
        }
        Decision::validated(Ok(RegistrationChannel {
            file: std::fs::File::from(pipe),
        }))
    }
}

/// One single-use claim. Debug and errors never expose its bytes.
pub struct HelperClaim(zeroize::Zeroizing<String>);
impl HelperClaim {
    pub fn value(&self) -> &str {
        &self.0
    }
}
impl fmt::Debug for HelperClaim {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("HelperClaim(<redacted>)")
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum HelperClaimError {
    InvalidHandoff,
    AlreadyTaken,
}
impl fmt::Display for HelperClaimError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::InvalidHandoff => "invalid helper claim handoff",
            Self::AlreadyTaken => "helper claim already taken",
        })
    }
}
impl std::error::Error for HelperClaimError {}

type HelperResult = Result<Option<HelperClaim>, HelperClaimError>;
static HELPER_CLAIM: OnceLock<Mutex<HelperResult>> = OnceLock::new();

/// Take an explicit helper claim once. Reconnecting requires a fresh claim
/// from the owner, not another read of the same pipe. No nonce fallback exists.
pub fn helper_claim() -> HelperResult {
    let mut result = HELPER_CLAIM
        .get_or_init(|| Mutex::new(read_helper_claim(|key| std::env::var_os(key))))
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    if matches!(&*result, Ok(None)) {
        return Ok(None);
    }
    std::mem::replace(&mut *result, Err(HelperClaimError::AlreadyTaken))
}

fn read_helper_claim(mut lookup: impl FnMut(&str) -> Option<OsString>) -> HelperResult {
    #[cfg(unix)]
    let read = {
        let Some(value) = lookup(HELPER_CLAIM_FD_ENV) else {
            return Ok(None);
        };
        helper_unix::read(&value)
    };
    #[cfg(windows)]
    let read = {
        let Some(value) = lookup(HELPER_CLAIM_PIPE_ENV) else {
            return Ok(None);
        };
        let parent =
            pid(lookup(HELPER_PARENT_PID_ENV)).map_err(|_| HelperClaimError::InvalidHandoff)?;
        crate::launch_nonce::read_helper_pipe(&value, parent)
    };
    #[cfg(any(unix, windows))]
    {
        let value = read
            .map_err(|_| HelperClaimError::InvalidHandoff)?
            .ok_or(HelperClaimError::InvalidHandoff)?;
        Ok(Some(HelperClaim(zeroize::Zeroizing::new(value))))
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = &mut lookup;
        Err(HelperClaimError::InvalidHandoff)
    }
}

pub(crate) fn check_claim(claim: &str) -> std::io::Result<()> {
    // POSIX guarantees at least 512 bytes in an empty pipe. Bound the claim
    // before writing, since the helper has not been spawned to drain it yet.
    if claim.is_empty() || claim.len() > 512 {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "invalid helper claim length",
        ));
    }
    Ok(())
}

#[cfg(unix)]
#[derive(Debug)]
pub struct HelperClaimHandoff(PreparedHelperClaimHandoff);
#[cfg(unix)]
impl HelperClaimHandoff {
    pub fn new(claim: &str) -> std::io::Result<Self> {
        check_claim(claim)?;
        let mut prepared = PreparedHelperClaimHandoff::new(3)?;
        prepared.write_claim(claim)?;
        Ok(Self(prepared))
    }

    /// Install last, after all other pre-exec hooks. Only descriptor metadata
    /// is placed in the environment; stdin and the other stdio are untouched.
    pub fn install_last(self, command: &mut std::process::Command) {
        self.0.install_last(command);
    }
}

#[cfg(windows)]
pub use crate::launch_nonce::{HelperClaimHandoff, HELPER_CLAIM_CONNECT_WINDOW};

#[cfg(unix)]
mod helper_unix;
#[cfg(unix)]
pub use helper_unix::PreparedHelperClaimHandoff;

/// Reserved single-instance helper pipe. Preparation contains no claim bytes;
/// completion checks the live child's PID before delivering exactly one claim.
#[cfg(windows)]
pub struct PreparedHelperClaimHandoff(crate::launch_nonce::PreparedHelperPipe);
#[cfg(windows)]
impl fmt::Debug for PreparedHelperClaimHandoff {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("PreparedHelperClaimHandoff(<redacted>)")
    }
}
#[cfg(windows)]
impl PreparedHelperClaimHandoff {
    #[cfg(test)]
    pub(crate) fn name(&self) -> &str {
        self.0.name()
    }
    pub fn new(_child_fd: i32) -> std::io::Result<Self> {
        Ok(Self(crate::launch_nonce::PreparedHelperPipe::reserve()?))
    }
    pub fn environment(&self) -> Vec<(OsString, OsString)> {
        vec![
            (HELPER_CLAIM_PIPE_ENV.into(), self.0.name().into()),
            (
                HELPER_PARENT_PID_ENV.into(),
                std::process::id().to_string().into(),
            ),
        ]
    }
    pub fn child_fd(&self) -> Option<i32> {
        None
    }
    /// Retain the child until completion returns to prevent PID reuse.
    pub fn complete(self, child_pid: u32, claim: &str) -> std::io::Result<()> {
        check_claim(claim)?;
        self.0.deliver_helper(child_pid, claim)
    }
    pub fn abort(self) {}
}

#[cfg(all(test, unix))]
#[allow(unsafe_code)]
mod tests {
    use super::*;
    use std::{
        io::{Read, Write},
        os::fd::{AsRawFd, IntoRawFd},
        os::unix::net::{UnixListener, UnixStream},
        process::{Command, Stdio},
    };

    const CHANNEL_TEST: &str = "registration_channel::tests::registration_channel_contract";
    const HELPER_TEST: &str = "registration_channel::tests::helper_claim_handoff_contract";
    const MODE: &str = "SUBC_OS_REGISTRATION_TEST_MODE";
    const CLAIM: &str = "test-helper-claim-not-for-logs";

    #[cfg(target_os = "macos")]
    #[test]
    fn darwin_zero_inode_retains_socket_names_and_peer_pid_checks() {
        fn no_inode(fd: i32) -> std::io::Result<libc::stat> {
            let mut stat = std::mem::MaybeUninit::<libc::stat>::uninit();
            // SAFETY: the writable output is read only after successful fstat.
            if unsafe { libc::fstat(fd, stat.as_mut_ptr()) } == -1 {
                return Err(std::io::Error::last_os_error());
            }
            let mut stat = unsafe { stat.assume_init() };
            stat.st_ino = 0;
            Ok(stat)
        }
        let (stream, _peer) = UnixStream::pair().unwrap();
        let fd = stream.into_raw_fd();
        let decision =
            unix::adopt_with_stat(format!("{fd}:999").as_ref(), std::process::id(), no_inode);
        assert!(
            decision.supervised,
            "zero inode socketpair refused: {:?}",
            decision.refusal
        );
        drop(decision.take().unwrap());
        let (stream, _peer) = UnixStream::pair().unwrap();
        let value = format!("{}:999", stream.as_raw_fd());
        assert_eq!(
            unix::adopt_with_stat(value.as_ref(), std::process::id() + 1, no_inode).refusal,
            Some(RegistrationRefusal::WrongPeer)
        );
        let file = std::fs::File::open("/dev/null").unwrap();
        let value = format!("{}:999", file.as_raw_fd());
        assert_eq!(
            unix::adopt_with_stat(value.as_ref(), std::process::id(), no_inode).refusal,
            Some(RegistrationRefusal::NotSocket)
        );
        // A relative address stays below Darwin's sockaddr_un path limit even
        // when the checkout or the test runner's scratch root has a long name.
        let path =
            std::path::PathBuf::from(format!("target/scratch/zero-inode-{}", std::process::id()));
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        let listener = UnixListener::bind(&path).unwrap();
        let stream = UnixStream::connect(&path).unwrap();
        let (_peer, _) = listener.accept().unwrap();
        let value = format!("{}:999", stream.as_raw_fd());
        assert_eq!(
            unix::adopt_with_stat(value.as_ref(), std::process::id(), no_inode).refusal,
            Some(RegistrationRefusal::NamedSocket)
        );
        drop(listener);
        std::fs::remove_file(path).unwrap();
        eprintln!("Darwin zero-inode fallback: socketpair adopted; non-socket, named peer and wrong peer PID refused");
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn darwin_daemon_side_zero_inode_keeps_the_other_checks() {
        // The daemon wrote inode 0 while the child reads a real one, as seen on
        // a macOS VM. Adoption still requires an unnamed socket from the daemon.
        fn some_inode(fd: i32) -> std::io::Result<libc::stat> {
            let mut stat = std::mem::MaybeUninit::<libc::stat>::uninit();
            // SAFETY: the writable output is read only after successful fstat.
            if unsafe { libc::fstat(fd, stat.as_mut_ptr()) } == -1 {
                return Err(std::io::Error::last_os_error());
            }
            let mut stat = unsafe { stat.assume_init() };
            stat.st_ino = 2;
            Ok(stat)
        }
        let (stream, _peer) = UnixStream::pair().unwrap();
        let fd = stream.into_raw_fd();
        let decision =
            unix::adopt_with_stat(format!("{fd}:0").as_ref(), std::process::id(), some_inode);
        assert!(
            decision.supervised,
            "daemon-side zero inode refused: {:?}",
            decision.refusal
        );
        drop(decision.take().unwrap());
        let (stream, _peer) = UnixStream::pair().unwrap();
        let value = format!("{}:0", stream.as_raw_fd());
        assert_eq!(
            unix::adopt_with_stat(value.as_ref(), std::process::id() + 1, some_inode).refusal,
            Some(RegistrationRefusal::WrongPeer)
        );
    }

    #[test]
    fn unix_channel_parent_prepares_cloexec_sources() {
        let (parent, child, metadata) = registration_socketpair().unwrap();
        for fd in [parent.as_raw_fd(), child.as_raw_fd()] {
            assert!(fd >= 6, "socket source collided with a reserved slot");
            // SAFETY: inspect flags on these owned descriptors.
            let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
            assert_ne!(flags & libc::FD_CLOEXEC, 0);
        }
        let actual = inode(child.as_raw_fd());
        assert_eq!(metadata, format!("4:{actual}"));
        let peer = unix::peer_pid(child.as_raw_fd());
        eprintln!(
            "socketpair: peer_pid={peer:?} inode={actual} usable_inode={} both_cloexec=true",
            actual != 0
        );
        assert_eq!(peer, Some(std::process::id()));
        assert!(unix::inode_matches(actual, actual));
        assert!(!unix::inode_matches(12, 13));
        #[cfg(target_os = "macos")]
        assert!(
            unix::inode_matches(0, 13),
            "zero inode must retain the other three validators"
        );
        #[cfg(target_os = "macos")]
        assert!(
            unix::inode_matches(2, 0),
            "a daemon that read zero must fall back like a child that read zero"
        );
        #[cfg(not(target_os = "macos"))]
        assert!(!unix::inode_matches(2, 0));
    }

    #[test]
    fn retired_endpoint_is_not_supervision() {
        let (stream, peer) = UnixStream::pair().unwrap();
        let fd = stream.into_raw_fd();
        peer.shutdown(std::net::Shutdown::Both).unwrap();
        refuse(
            format!("{fd}:{}", inode(fd)),
            std::process::id(),
            RegistrationRefusal::Closed,
        );
        // Refusal must leave the descriptor open without taking ownership.
        assert_eq!(unsafe { libc::close(fd) }, 0);
    }

    fn child(test: &str, mode: &str) -> Command {
        let mut command = Command::new(std::env::current_exe().unwrap());
        command
            .args([test, "--exact", "--nocapture"])
            .env(MODE, mode)
            .env_remove(HELPER_CLAIM_FD_ENV)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        command
    }

    fn success(output: std::process::Output) {
        assert!(
            output.status.success(),
            "child failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        assert!(!String::from_utf8_lossy(&output.stdout).contains(CLAIM));
        assert!(!String::from_utf8_lossy(&output.stderr).contains(CLAIM));
    }

    // ino_t's width depends on the Unix target.
    #[allow(clippy::unnecessary_cast)]
    fn inode(fd: i32) -> u64 {
        let mut stat = std::mem::MaybeUninit::<libc::stat>::uninit();
        // SAFETY: a writable stat buffer, read only after successful fstat.
        assert_eq!(unsafe { libc::fstat(fd, stat.as_mut_ptr()) }, 0);
        unsafe { stat.assume_init() }.st_ino as u64
    }

    fn lookup(value: String, daemon: u32) -> impl FnMut(&str) -> Option<OsString> {
        move |key| match key {
            REGISTRATION_FD_ENV => Some(value.clone().into()),
            DAEMON_PID_ENV => Some(daemon.to_string().into()),
            _ => None,
        }
    }

    fn refuse(value: String, daemon: u32, reason: RegistrationRefusal) {
        let cell = ChannelCell::new();
        let decision = cell.get(lookup(value, daemon));
        assert!(!decision.supervised);
        assert_eq!(decision.refusal, Some(reason));
        assert!(decision.take().unwrap().is_none());
        assert!(cell
            .get(|_| panic!("cached refusal reread environment"))
            .take()
            .unwrap()
            .is_none());
    }

    #[test]
    fn registration_channel_contract() {
        if std::env::var(MODE).as_deref() == Ok("inherited-channel") {
            assert!(supervised(), "inherited socketpair failed validation");
            let mut stream = take_registration_channel()
                .unwrap()
                .unwrap()
                .into_unix_stream();
            assert!(matches!(
                take_registration_channel(),
                Err(RegistrationChannelError::AlreadyTaken)
            ));
            let mut byte = [0];
            stream.read_exact(&mut byte).unwrap();
            assert_eq!(&byte, b"x");
            stream.write_all(b"x").unwrap();
            success(child(CHANNEL_TEST, "missing-channel").output().unwrap());
            return;
        }
        if std::env::var(MODE).as_deref() == Ok("missing-channel") {
            assert!(std::env::var_os(REGISTRATION_FD_ENV).is_some());
            assert!(
                !supervised(),
                "a variable without its descriptor is not supervision"
            );
            assert!(take_registration_channel().unwrap().is_none());
            assert!(registration_refusal().is_some());
            return;
        }

        let (stream, mut peer) = UnixStream::pair().unwrap();
        let fd = stream.into_raw_fd();
        let identity = inode(fd);
        // SAFETY: fcntl on the test's owned socket. Simulate the inheritable
        // descriptor delivered by the daemon before exercising adoption.
        let original_flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
        assert_ne!(original_flags & libc::FD_CLOEXEC, 0);
        assert_eq!(unsafe { libc::fcntl(fd, libc::F_SETFD, 0) }, 0);
        let cell = ChannelCell::new();
        let metadata = format!("{fd}:{identity}");
        let decision = cell.get(lookup(metadata.clone(), std::process::id()));
        assert!(
            decision.supervised,
            "socketpair validation: {:?}",
            decision.refusal
        );
        assert_eq!(decision.refusal, None);
        assert_ne!(
            unsafe { libc::fcntl(fd, libc::F_GETFD) } & libc::FD_CLOEXEC,
            0
        );
        eprintln!(
            "socketpair: pid={} inode={identity} initial_cloexec={} adopted_cloexec=true",
            std::process::id(),
            original_flags & libc::FD_CLOEXEC != 0
        );
        let mut stream = decision.take().unwrap().unwrap().into_unix_stream();
        assert!(matches!(
            decision.take(),
            Err(RegistrationChannelError::AlreadyTaken)
        ));
        assert!(
            cell.get(|_| panic!("cached adoption reread environment"))
                .supervised
        );
        peer.write_all(b"x").unwrap();
        let mut byte = [0];
        stream.read_exact(&mut byte).unwrap();
        assert_eq!(&byte, b"x");
        success(
            child(CHANNEL_TEST, "missing-channel")
                .env(REGISTRATION_FD_ENV, metadata)
                .env(DAEMON_PID_ENV, std::process::id().to_string())
                .output()
                .unwrap(),
        );

        // Exercise the actual exec boundary, including peer credentials on a
        // socketpair made by the parent and the child's process-wide accessors.
        let (inherited, mut peer) = UnixStream::pair().unwrap();
        let source = inherited.as_raw_fd();
        let mut command = child(CHANNEL_TEST, "inherited-channel");
        command
            .env(REGISTRATION_FD_ENV, format!("4:{}", inode(source)))
            .env(DAEMON_PID_ENV, std::process::id().to_string());
        use std::os::unix::process::CommandExt;
        // SAFETY: only async-signal-safe, allocation-free descriptor placement
        // runs after fork; the source stays owned until spawn has returned.
        unsafe {
            command.pre_exec(move || {
                if libc::dup2(source, 4) == -1 || libc::fcntl(4, libc::F_SETFD, 0) == -1 {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(())
            });
        }
        let process = command.spawn().unwrap();
        drop(inherited);
        peer.set_read_timeout(Some(std::time::Duration::from_secs(5)))
            .unwrap();
        peer.write_all(b"x").unwrap();
        peer.read_exact(&mut byte).unwrap();
        assert_eq!(&byte, b"x");
        success(process.wait_with_output().unwrap());

        // OnceLock serializes first callers and the transport has only one owner.
        let (other, _peer) = UnixStream::pair().unwrap();
        let fd = other.into_raw_fd();
        let metadata = format!("{fd}:{}", inode(fd));
        let cell = ChannelCell::new();
        std::thread::scope(|scope| {
            let handles: Vec<_> = (0..8)
                .map(|_| {
                    let cell = &cell;
                    let metadata = metadata.clone();
                    scope.spawn(move || {
                        cell.get(lookup(metadata, std::process::id()))
                            .take()
                            .is_ok()
                    })
                })
                .collect();
            assert_eq!(
                handles
                    .into_iter()
                    .map(|h| h.join().unwrap() as usize)
                    .sum::<usize>(),
                1
            );
        });

        let file = std::fs::File::open("/dev/null").unwrap();
        refuse(
            format!("{}:{}", file.as_raw_fd(), inode(file.as_raw_fd())),
            std::process::id(),
            RegistrationRefusal::NotSocket,
        );
        assert!(
            file.metadata().is_ok(),
            "refusal must not close an unrelated file"
        );
        for malformed in ["", "3", "-1:0", "x:1", "3:x"] {
            refuse(
                malformed.into(),
                std::process::id(),
                RegistrationRefusal::Malformed,
            );
        }
        let (socket, _peer) = UnixStream::pair().unwrap();
        let fd = socket.as_raw_fd();
        let identity = inode(fd);
        if !cfg!(target_os = "macos") || identity != 0 {
            refuse(
                format!("{fd}:{}", identity.wrapping_add(1)),
                std::process::id(),
                RegistrationRefusal::WrongInode,
            );
        } else {
            eprintln!(
                "Darwin socket inode unavailable; exercising PID and unnamed-socket fallback"
            );
            let fd = socket.try_clone().unwrap().into_raw_fd();
            assert!(ChannelCell::new()
                .get(lookup(format!("{fd}:999"), std::process::id()))
                .take()
                .unwrap()
                .is_some());
        }
        refuse(
            format!("{fd}:{identity}"),
            std::process::id().wrapping_add(1),
            RegistrationRefusal::WrongPeer,
        );
        assert!(
            socket.peer_addr().is_ok(),
            "wrong-peer refusal leaves descriptor open"
        );

        let path = std::path::PathBuf::from(format!(
            "target/scratch/registration-{}",
            std::process::id()
        ));
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        let listener = UnixListener::bind(&path).unwrap();
        let connected = UnixStream::connect(&path).unwrap();
        let (_accepted, _) = listener.accept().unwrap();
        let fd = connected.as_raw_fd();
        refuse(
            format!("{fd}:{}", inode(fd)),
            std::process::id(),
            RegistrationRefusal::NamedSocket,
        );
        drop(listener);
        std::fs::remove_file(path).unwrap();

        let cell = ChannelCell::new();
        assert!(!cell.get(|_| None).supervised);
        assert!(
            !cell
                .get(|_| panic!("absent decision reread environment"))
                .supervised
        );
    }

    #[test]
    fn helper_claim_handoff_contract() {
        match std::env::var(MODE).as_deref() {
            Ok("helper") => {
                let claim = helper_claim().unwrap().unwrap();
                assert!(claim.value() == CLAIM, "claim mismatch");
                assert!(!format!("{claim:?}").contains(CLAIM));
                assert!(matches!(
                    helper_claim(),
                    Err(HelperClaimError::AlreadyTaken)
                ));
                let mut stdin = String::new();
                std::io::stdin().read_to_string(&mut stdin).unwrap();
                assert_eq!(stdin, "stdio-still-available");
                let metadata = std::env::var_os(HELPER_CLAIM_FD_ENV).unwrap();
                success(
                    child(HELPER_TEST, "helper-descendant")
                        .env(HELPER_CLAIM_FD_ENV, metadata)
                        .output()
                        .unwrap(),
                );
                return;
            }
            Ok("helper-descendant") => {
                assert!(
                    helper_claim().is_err(),
                    "a consumed pipe cannot hand the claim to descendants"
                );
                return;
            }
            _ => {}
        }
        let handoff = HelperClaimHandoff::new(CLAIM).unwrap();
        assert!(!format!("{handoff:?}").contains(CLAIM));
        let mut command = child(HELPER_TEST, "helper");
        command.stdin(Stdio::piped());
        handoff.install_last(&mut command);
        assert!(!format!("{command:?}").contains(CLAIM));
        let mut process = command.spawn().unwrap();
        process
            .stdin
            .take()
            .unwrap()
            .write_all(b"stdio-still-available")
            .unwrap();
        success(process.wait_with_output().unwrap());
        assert!(HelperClaimHandoff::new("").is_err());
        assert!(HelperClaimHandoff::new(&"x".repeat(513)).is_err());
    }
}

#[cfg(test)]
#[allow(unsafe_code)]
mod prepared_helper_tests {
    use super::*;
    use std::process::{Command, Stdio};
    const MODE: &str = "SUBC_PREPARED_HELPER_TEST";
    #[test]
    fn prepared_helper_split_delivery_and_abort() {
        if let Ok(mode) = std::env::var(MODE) {
            let value = helper_claim();
            if mode == "read" {
                assert!(value.unwrap().unwrap().value() == "test-single-use-claim");
            } else {
                assert!(value.is_err(), "aborted handoff must deliver no bytes");
            }
            return;
        }
        #[cfg(unix)]
        let modes = ["read", "abort"].as_slice();
        #[cfg(windows)]
        let modes = ["read", "abort", "refuse"].as_slice();
        for mode in modes.iter().copied() {
            let prepared = PreparedHelperClaimHandoff::new(3).unwrap();
            let mut command = Command::new(std::env::current_exe().unwrap());
            command.args(["--exact", "registration_channel::prepared_helper_tests::prepared_helper_split_delivery_and_abort", "--nocapture"])
                .env(MODE, mode).envs(prepared.environment()).stdout(Stdio::piped()).stderr(Stdio::piped());
            #[cfg(unix)]
            {
                use std::os::unix::process::CommandExt;
                let fd = prepared.child_fd().unwrap();
                assert_ne!(
                    unsafe { libc::fcntl(fd, libc::F_GETFD) } & libc::FD_CLOEXEC,
                    0
                );
                unsafe {
                    command.pre_exec(move || {
                        if libc::dup2(fd, 3) == -1 || libc::fcntl(3, libc::F_SETFD, 0) == -1 {
                            return Err(std::io::Error::last_os_error());
                        }
                        Ok(())
                    });
                }
            }
            let child = command.spawn().unwrap();
            if mode == "read" {
                prepared
                    .complete(child.id(), "test-single-use-claim")
                    .unwrap();
            } else if mode == "refuse" {
                assert!(
                    prepared
                        .complete(std::process::id(), "test-single-use-claim")
                        .is_err(),
                    "foreign helper must receive no bytes"
                );
            } else {
                prepared.abort();
            }
            let output = child.wait_with_output().unwrap();
            assert!(
                output.status.success(),
                "{}{}",
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr)
            );
        }
    }
}