blackship 0.4.2

A FreeBSD jail manager with state machine control
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
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
//! Jail lifecycle: start, stop, restart

use crate::audit::{AuditEvent, AuditRecord};
use crate::error::{Error, Result};
use crate::hooks::{HookContext, HookPhase, HookRunner};
use crate::jail::chart::{Health, Node, Step};
use crate::jail::state::{JailMachineState as MachineState, State as JailState};
use crate::jail::{
    JailConfig, JailHandle, JailInstance, ParamValue, jail_create, jail_create_with_descriptor,
    jail_getid, jail_remove,
};
use crate::network::{VnetConfig, VnetSetup};
use crate::scope::{ScopeMachineEvent, ScopeRecord};
use std::collections::HashMap;
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use std::time::Instant;
use throttle_machines::gate::Gate;
use throttle_machines::token_bucket::{TokenBucket, TokenBucketParams, TokenBucketState};

use super::Bridge;

/// Reserved params are rejected so manifests cannot bypass jail isolation.
/// `allow.` is matched by prefix: module-registered params are runtime
/// sysctl entries no header can enumerate.
pub fn is_reserved_param(key: &str) -> bool {
    use crate::sys::consts::{
        JAIL_PARAM_CHILDREN_MAX, JAIL_PARAM_DEVFS_RULESET, JAIL_PARAM_ENFORCE_STATFS,
        JAIL_PARAM_ERRMSG, JAIL_PARAM_HOST_HOSTNAME, JAIL_PARAM_IP4_ADDR, JAIL_PARAM_IP6_ADDR,
        JAIL_PARAM_JID, JAIL_PARAM_NAME, JAIL_PARAM_NOPERSIST, JAIL_PARAM_PATH, JAIL_PARAM_PERSIST,
        JAIL_PARAM_SECURELEVEL, JAIL_PARAM_VNET,
    };
    key.starts_with("allow.")
        || matches!(
            key,
            JAIL_PARAM_NAME
                | JAIL_PARAM_JID
                | JAIL_PARAM_PATH
                | JAIL_PARAM_ERRMSG
                | JAIL_PARAM_PERSIST
                | JAIL_PARAM_NOPERSIST
                | JAIL_PARAM_VNET
                | JAIL_PARAM_IP4_ADDR
                | JAIL_PARAM_IP6_ADDR
                | JAIL_PARAM_HOST_HOSTNAME
                | JAIL_PARAM_SECURELEVEL
                | JAIL_PARAM_DEVFS_RULESET
                | JAIL_PARAM_CHILDREN_MAX
                | JAIL_PARAM_ENFORCE_STATFS
        )
}

/// Build the `jail_set` parameter map shared by creation and eva.
pub(super) fn build_jail_params(
    jail_def: &crate::manifest::JailDef,
    full_name: &str,
    is_vnet: bool,
    effective_ip: Option<IpAddr>,
) -> Result<HashMap<String, ParamValue>> {
    let mut params = HashMap::new();

    for (key, value) in &jail_def.params {
        if is_reserved_param(key) {
            eprintln!("Warning: ignoring reserved jail parameter '{key}' in manifest");
            continue;
        }
        let param_value = ParamValue::try_from(value)?;
        params.insert(key.clone(), param_value);
    }

    params.insert(
        "name".to_string(),
        ParamValue::String(full_name.to_string()),
    );

    if let Some(hostname) = &jail_def.hostname {
        params.insert(
            "host.hostname".to_string(),
            ParamValue::String(hostname.clone()),
        );
    }

    if is_vnet {
        // `vnet` is a jailsys CTLTYPE_INT param in the kernel, even though
        // jail.conf exposes it as the strings "inherit"/"new".
        params.insert("vnet".to_string(), ParamValue::Int(libc::JAIL_SYS_NEW));
    } else if let Some(ip) = effective_ip {
        match ip {
            IpAddr::V4(addr) => {
                params.insert("ip4.addr".to_string(), ParamValue::Ipv4(vec![addr]));
            }
            IpAddr::V6(addr) => {
                params.insert("ip6.addr".to_string(), ParamValue::Ipv6(vec![addr]));
            }
        }
    } else if jail_def.network.as_ref().is_some_and(|n| n.shared) {
        // Same jailsys param as `vnet` above.
        params.insert("ip4".to_string(), ParamValue::Int(libc::JAIL_SYS_INHERIT));
    }

    Ok(params)
}

/// Copies the host's resolv.conf into a shared-network jail, if it has none.
fn copy_resolv_conf_if_absent(jail_root: &std::path::Path) {
    let resolv_path = jail_root.join("etc/resolv.conf");

    if crate::blueprint::context::reject_symlink_ancestors(jail_root, &resolv_path).is_err() {
        return;
    }

    if !resolv_path.exists()
        && let Ok(content) = std::fs::read("/etc/resolv.conf")
    {
        if let Some(parent) = resolv_path.parent() {
            let _ = std::fs::create_dir_all(parent); // best-effort
        }
        if let Ok(mut f) = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&resolv_path)
        {
            use std::io::Write;
            let _ = f.write_all(&content); // best-effort
        }
    }
}
/// Mount devfs into the jail root; jail(2) ignores `mount.devfs`.
fn mount_jail_devfs(jail_def: &crate::manifest::JailDef, path: &Path) -> Result<()> {
    let devfs_ruleset = jail_def
        .devfs_ruleset
        .unwrap_or(crate::sys::DEVFS_RULESET_JAIL);
    if devfs_ruleset != 0 {
        let dev_path = path.join("dev");
        if !dev_path.join("null").exists() {
            // A missing dir surfaces as a mount_devfs error just below.
            std::fs::create_dir_all(&dev_path).ok();
            if let Err(e) = crate::sys::mount_devfs(&dev_path)
                .and_then(|()| crate::sys::apply_devfs_ruleset(&dev_path, devfs_ruleset))
            {
                crate::sys::unmount_quiet(&dev_path);
                return Err(e);
            }
        }
    }
    Ok(())
}

/// Move the jail side of a VNET pair into the live jail.
fn attach_vnet(setup: &VnetSetup, full_name: &str, jid: i32, verbose: bool) -> Result<()> {
    setup.attach_to_jail(jid).inspect_err(|e| {
        eprintln!("Error: Failed to attach VNET to jail '{full_name}': {e}");
    })?;
    if verbose {
        println!(
            "  Moved {} into jail {} (JID {})",
            setup.jail_interface(),
            full_name,
            jid
        );
        println!(
            "  Configured {} with {} in jail",
            setup.jail_interface(),
            setup.config.ip
        );
        println!("  Set default gateway to {}", setup.config.gateway);
    }
    Ok(())
}

/// Run /etc/rc inside the jail (jail(2) has no exec.start). Failures only warn.
fn run_jail_rc(full_name: &str, jid: i32) {
    match crate::jail::jexec::jexec_run(jid, &["/bin/sh", "/etc/rc"]) {
        Ok(exit_code) if exit_code != 0 => {
            eprintln!("Warning: /etc/rc in jail '{full_name}' exited with {exit_code}");
        }
        Ok(_) => {}
        Err(e) => {
            eprintln!("Warning: failed to run /etc/rc in jail '{full_name}': {e}");
        }
    }
}

/// Copies a release userland into a jail root with `cp -a`.
fn copy_release(release_path: &Path, path: &Path) -> Result<()> {
    std::fs::create_dir_all(path)
        .map_err(|e| Error::JailOperation(format!("Failed to create jail directory: {e}")))?;

    let status = std::process::Command::new("/bin/cp")
        .arg("-a")
        .arg(format!("{}/.", release_path.display()))
        .arg(path)
        .status()
        .map_err(|e| Error::JailOperation(format!("Failed to execute cp command: {e}")))?;
    if !status.success() {
        return Err(Error::JailOperation(format!(
            "Failed to copy release: cp exited with status {status}"
        )));
    }

    // cp -a may have followed a symlink out of the parent.
    let data_parent = path.parent().unwrap_or_else(|| Path::new("/"));
    if let (Ok(canonical_path), Ok(canonical_parent)) =
        (path.canonicalize(), data_parent.canonicalize())
        && !canonical_path.starts_with(&canonical_parent)
    {
        return Err(Error::JailOperation(format!(
            "Jail path '{}' resolves to '{}' which is outside '{}'",
            path.display(),
            canonical_path.display(),
            canonical_parent.display()
        )));
    }
    Ok(())
}

/// What a `start_jail` attempt holds so far; `unwind_start` gives it back.
#[derive(Default)]
struct StartClaims {
    /// The jail's ZFS dataset was created by this attempt (not inherited from a build).
    zfs_dataset: bool,
    /// IP leased from a network pool.
    allocated_ip: Option<(String, IpAddr)>,
    /// Epair or netgraph pair, with its state record on disk.
    vnet_setup: Option<VnetSetup>,
    /// Set once jail(2) is attempted: devfs may be mounted and the jail is reported failed.
    jail_root: Option<PathBuf>,
    /// The live jail, once jail(2) succeeded.
    jid: Option<i32>,
}

/// The `post_stop` hook runner and the jail root it runs against.
type PostStopHook = (HookRunner, PathBuf);

impl Bridge {
    /// Reconcile this project's running jails to the reserved `autostart` tag.
    ///
    /// Untagged jails are stopped exactly (without cascade expansion), then
    /// tagged jails and their dependency closure are started in graph order.
    pub fn reconcile_autostart(&mut self) -> Result<()> {
        let stop = self.autostart_stop_order()?;
        let start = self.autostart_order()?;

        for name in &stop {
            if self.is_running(name)? {
                self.stop_jail(name)?;
            }
        }
        for name in &start {
            if !self.is_running(name)? {
                self.start_jail(name)?;
            }
        }

        println!(
            "Project autostart reconciled: {} jail(s) should be running.",
            start.len()
        );
        Ok(())
    }

    /// Persist the in-flight scope record. Never fails the jail operation.
    pub(super) fn persist_scope(&self, scope: &mut ScopeRecord) {
        scope.touch();
        if let Err(e) = self.scope_store.save(scope) {
            eprintln!(
                "Warning: failed to persist scope record for '{}': {}",
                scope.name, e
            );
        }
    }

    /// Apply `mutate` to the stored record. Absent records are left absent.
    fn amend_scope<F>(&self, full_name: &str, mutate: F)
    where
        F: FnOnce(&mut ScopeRecord),
    {
        match self.scope_store.get(full_name) {
            Ok(Some(mut scope)) => {
                mutate(&mut scope);
                self.persist_scope(&mut scope);
            }
            Ok(None) => {}
            Err(e) => eprintln!("Warning: failed to read scope record for '{full_name}': {e}"),
        }
    }

    /// Roll back resources allocated during a failed `start_jail`.
    fn rollback_start(
        &mut self,
        full_name: &str,
        vnet_setup: Option<VnetSetup>,
        allocated_ip: Option<&(String, IpAddr)>,
        created_zfs_dataset: bool,
    ) {
        if let Some(setup) = vnet_setup {
            match setup.cleanup() {
                Ok(()) => {
                    if let Err(e) = self.vnet_state_store.delete(full_name) {
                        eprintln!(
                            "Warning: failed to remove VNET state record for '{full_name}': {e}"
                        );
                    }
                    self.audit.record(
                        &AuditRecord::new(full_name, AuditEvent::RollbackStep)
                            .with("step", "vnet")
                            .with("outcome", "released"),
                    );
                    self.amend_scope(full_name, |scope| scope.has_vnet_record = false);
                }
                Err(e) => {
                    eprintln!(
                        "Warning: VNET cleanup failed for '{full_name}': {e} -- keeping state record for manual cleanup"
                    );
                    self.audit.record(
                        &AuditRecord::new(full_name, AuditEvent::RollbackStep)
                            .with("step", "vnet")
                            .with("outcome", "leaked")
                            .with("error", e),
                    );
                }
            }
        }
        if let Some((network_name, ip)) = allocated_ip {
            self.ip_allocator.release(network_name, ip);
            if let Err(e) = self.lease_store.release(network_name, full_name) {
                eprintln!("Warning: failed to release lease for '{full_name}': {e}");
            }
            self.audit.record(
                &AuditRecord::new(full_name, AuditEvent::RollbackStep)
                    .with("step", "lease")
                    .with("outcome", "released")
                    .with("ip", ip),
            );
            self.amend_scope(full_name, |scope| scope.allocated_ips.clear());
        }
        if created_zfs_dataset {
            eprintln!("Cleaning up ZFS dataset...");
            let outcome = match &self.zfs {
                Some(zfs) => match zfs.destroy_jail_dataset(full_name) {
                    Ok(()) => "released",
                    Err(e) => {
                        eprintln!("Warning: failed to destroy dataset for '{full_name}': {e}");
                        "leaked"
                    }
                },
                None => "skipped",
            };
            self.audit.record(
                &AuditRecord::new(full_name, AuditEvent::RollbackStep)
                    .with("step", "zfs")
                    .with("outcome", outcome),
            );
            if outcome == "released" {
                self.amend_scope(full_name, |scope| {
                    scope.zfs_dataset = None;
                    scope.zfs_origin = None;
                });
            }
        }

        self.finish_rollback(full_name);
    }

    /// Park an existing scope record in `Failed` with no live jid.
    fn mark_scope_failed(&self, full_name: &str) {
        self.amend_scope(full_name, |scope| {
            scope.jid = None;
            scope.advance_or_warn(ScopeMachineEvent::Fail);
        });
    }

    /// Drop the scope record when the rollback released everything it claimed.
    fn finish_rollback(&self, full_name: &str) {
        match self.scope_store.get(full_name) {
            Ok(Some(scope)) if !scope.holds_resources() => {
                if let Err(e) = self.scope_store.delete(full_name) {
                    eprintln!("Warning: failed to remove scope record for '{full_name}': {e}");
                }
            }
            Ok(Some(_)) => self.mark_scope_failed(full_name),
            Ok(None) => {}
            Err(e) => eprintln!("Warning: failed to read scope record for '{full_name}': {e}"),
        }
    }

    /// Mark a jail as failed and notify the Warden.
    fn mark_jail_failed(&mut self, full_name: &str, path: &std::path::Path) {
        let jail_config = JailConfig::new(full_name, path);
        let mut instance = JailInstance::new(jail_config);
        // Fresh instance: Stopped -> Starting -> Failed cannot be rejected.
        instance.start().ok();
        instance.fail().ok(); // see above
        self.instances.insert(full_name.to_string(), instance);

        match self.scope_store.get(full_name) {
            Ok(Some(_)) => self.mark_scope_failed(full_name),
            Ok(None) => {
                let mut scope = ScopeRecord::new(full_name);
                scope.advance_or_warn(ScopeMachineEvent::Fail);
                self.persist_scope(&mut scope);
            }
            Err(e) => eprintln!("Warning: failed to read scope record for '{full_name}': {e}"),
        }
        self.audit.event(full_name, AuditEvent::Failed);

        if let Some(handle) = &self.warden_handle
            && let Err(e) = handle.notify_failure(full_name)
        {
            eprintln!("Warning: failed to notify Warden about '{full_name}': {e}");
        }
    }

    /// Start all jails (or a specific one with its dependencies)
    ///
    /// Jails already running are skipped, so bringing up a jail whose
    /// dependencies are up only starts what is missing.
    pub fn up(&mut self, jail: Option<&str>) -> Result<()> {
        for name in &self.jails_for_up(jail)? {
            if self.is_running(name)? {
                println!("Jail '{name}' is already running");
                continue;
            }
            self.start_jail(name)?;
        }

        Ok(())
    }

    /// Stop all jails (or a specific one with its dependents)
    ///
    /// Jails already stopped are skipped.
    pub fn down(&mut self, jail: Option<&str>) -> Result<()> {
        self.down_and_report(jail).map(|_| ())
    }

    /// Stop what `down` would stop, returning the jails actually stopped in
    /// stop order.
    fn down_and_report(&mut self, jail: Option<&str>) -> Result<Vec<String>> {
        let plan = self.down_plan(jail)?;

        for name in &plan.isolate {
            self.isolate_jail(name);
        }

        let mut stopped = Vec::new();
        for name in &plan.stop {
            if !self.is_running(name)? {
                println!("Jail '{name}' is not running");
                continue;
            }
            self.stop_jail(name)?;
            stopped.push(name.clone());
        }

        Ok(stopped)
    }

    fn is_running(&self, name: &str) -> Result<bool> {
        let (_, full_name) = self.resolve_jail_names(name)?;
        Ok(jail_getid(&full_name).is_ok())
    }

    /// Fence a dependent off at the firewall without stopping it. The anchor is
    /// left loaded until the jail exits and `gc` reports it.
    fn isolate_jail(&self, name: &str) {
        let Ok((service_name, full_name)) = self.resolve_jail_names(name) else {
            return;
        };

        if !crate::bulkhead::pf_enabled() {
            eprintln!("Warning: cannot isolate jail '{full_name}': PF is not enabled");
            return;
        }

        let Some(jail_def) = self.config.get_jail(&service_name) else {
            return;
        };
        let ips = self.drainable_ips(&full_name, jail_def);
        match crate::bulkhead::load_drain_anchor(&self.config.config.data_dir, &full_name, &ips) {
            Ok(anchor) => {
                println!("Isolated jail '{full_name}': new connections blocked via {anchor}");
                if let Ok(Some(mut scope)) = self.scope_store.get(&full_name) {
                    scope.drain_anchor = Some(anchor.clone());
                    self.persist_scope(&mut scope);
                }
                self.audit.record(
                    &AuditRecord::new(&full_name, AuditEvent::DrainStart)
                        .with("anchor", &anchor)
                        .with("cascade", "isolate"),
                );
            }
            Err(e) => eprintln!("Warning: failed to isolate jail '{full_name}': {e}"),
        }
    }

    /// Restart jails
    ///
    /// Dependents that `down` cascaded through come back up too, in start
    /// order (the reverse of stop order).
    pub fn restart(&mut self, jail: Option<&str>) -> Result<()> {
        let stopped = self.down_and_report(jail)?;
        self.up(jail)?;
        for name in stopped.iter().rev() {
            if !self.is_running(name)? {
                self.start_jail(name)?;
            }
        }
        Ok(())
    }

    /// Restart a jail (stop then start)
    ///
    /// Used by the Warden for automatic restart on failure
    pub fn restart_jail(&mut self, name: &str) -> Result<()> {
        let (service_name, full_name) = self.resolve_jail_names(name)?;
        println!("Restarting jail '{full_name}'...");

        if let Some(instance) = self.instances.get_mut(&full_name)
            && instance.state() == JailState::Failed
        {
            instance.recover().ok(); // guarded by the Failed check, cannot be rejected
        }

        if jail_getid(&full_name).is_ok() {
            self.stop_jail(&service_name)?;
        }

        self.start_jail(&service_name)?;

        println!("Jail '{full_name}' restarted successfully");
        Ok(())
    }

    /// Start a single jail with cleanup on failure
    pub(super) fn start_jail(&mut self, name: &str) -> Result<()> {
        const REFILL_RATE: f64 = 1.0;

        self.prepare_runtime()?;

        // Rate limiting to prevent thundering herd on `up --all`
        let capacity = self.jail_start_capacity;

        loop {
            let mut state = self
                .rate_limiter
                .lock()
                .expect("rate limiter mutex poisoned");
            let (tokens, last_refill) = *state;
            let now = Instant::now();
            let now_secs = now.duration_since(self.rate_limiter_epoch).as_secs_f64();
            let last_refill_secs = last_refill
                .duration_since(self.rate_limiter_epoch)
                .as_secs_f64();

            let tb_state = TokenBucketState {
                tokens,
                last_refill: last_refill_secs,
            };
            let tb_params = TokenBucketParams {
                capacity,
                refill_rate: REFILL_RATE,
            };
            let result = TokenBucket::check(tb_state, now_secs, tb_params);

            if result.allowed {
                *state = (result.state.tokens, now);
                break;
            }

            let retry_after = result.retry_after;
            drop(state);
            std::thread::sleep(std::time::Duration::from_secs_f64(retry_after));
        }

        let (service_name, full_name) = self.resolve_jail_names(name)?;
        let jail_def = self
            .config
            .get_jail(&service_name)
            .ok_or_else(|| Error::JailNotFound(name.to_string()))?;

        // Check if already running
        if jail_getid(&full_name).is_ok() {
            return Err(Error::JailAlreadyRunning(full_name));
        }

        if jail_def.path.is_none()
            && let Some(fence) = self
                .zfs
                .as_ref()
                .and_then(|zfs| zfs.jail_migration_fence(&full_name))
        {
            return Err(Error::migrated(&full_name, &fence));
        }

        let mut scope = ScopeRecord::new(&full_name);
        scope.cascade = jail_def.cascade;
        scope.ephemeral = jail_def.ephemeral.unwrap_or(false);
        self.persist_scope(&mut scope);
        self.audit.event(&full_name, AuditEvent::Created);

        let mut claims = StartClaims::default();
        if let Err(e) = self.provision_and_start(&service_name, &full_name, &mut scope, &mut claims)
        {
            self.unwind_start(&full_name, claims);
            return Err(e);
        }
        Ok(())
    }

    /// Everything `start_jail` does once the scope record exists. Each resource
    /// lands in `claims` as soon as it is held, so `start_jail` can unwind it.
    fn provision_and_start(
        &mut self,
        service_name: &str,
        full_name: &str,
        scope: &mut ScopeRecord,
        claims: &mut StartClaims,
    ) -> Result<()> {
        let jail_def = self
            .config
            .get_jail(service_name)
            .ok_or_else(|| Error::JailNotFound(service_name.to_string()))?;

        let path = self.claim_jail_root(jail_def, full_name, scope, claims)?;
        self.prepare_jail_root(jail_def, full_name, &path)?;

        // Phase 3: the IP. Inline because `jail_def` borrows `self.config`.
        let effective_ip: Option<IpAddr> = if let Some(network) = &jail_def.network {
            if let Some(static_ip) = network.static_ip() {
                if let Some(net_name) = network
                    .networks
                    .iter()
                    .find(|net_name| !self.runtime_networks.contains_key(*net_name))
                {
                    return Err(Error::Network(format!(
                        "Jail '{full_name}' references unknown network '{net_name}'"
                    )));
                }
                Some(static_ip)
            } else if let Some(first_network) = network.networks.first() {
                let ip = crate::network::allocate_and_record(
                    &mut self.ip_allocator,
                    &self.lease_store,
                    first_network,
                    full_name,
                )?;
                claims.allocated_ip = Some((first_network.clone(), ip));
                scope.allocated_ips.push(ip);
                self.persist_scope(scope);
                self.audit.record(
                    &AuditRecord::new(full_name, AuditEvent::Provisioned)
                        .with("step", "lease")
                        .with("ip", ip)
                        .with("network", first_network),
                );
                crate::fault::trip(crate::fault::Point::AfterLease)?;
                if self.verbose {
                    println!("  Auto-allocated IP {ip} from network '{first_network}'");
                }
                Some(ip)
            } else {
                None
            }
        } else {
            None
        };

        let hook_runner = HookRunner::new(jail_def.hooks.clone()).verbose(self.verbose);
        let mut hook_context = HookContext::new(full_name, &path);

        if let Some(ip) = effective_ip {
            hook_context = hook_context.with_ip(ip.to_string());
        }

        hook_runner.execute_phase(HookPhase::PreStart, &hook_context)?;

        let is_vnet = jail_def.network.as_ref().is_some_and(|n| n.vnet);

        if is_vnet && let Some(network) = &jail_def.network {
            self.claim_vnet(network, full_name, effective_ip, scope, claims)?;
        }

        let params = build_jail_params(jail_def, full_name, is_vnet, effective_ip)?;

        mount_jail_devfs(jail_def, &path)?;

        if jail_def.network.as_ref().is_some_and(|n| n.shared) {
            copy_resolv_conf_if_absent(&path);
        }

        let (jid, jail_handle) = self.create_jail(full_name, &path, &params, scope, claims)?;
        crate::fault::trip(crate::fault::Point::AfterJailCreate)?;
        self.apply_startup_limits(jail_def, service_name, full_name, jid, scope)?;

        if let Some(setup) = &claims.vnet_setup {
            attach_vnet(setup, full_name, jid, self.verbose)?;
        }

        // Nothing past this point fails: hand the claims over to the bridge.
        if let Some(setup) = claims.vnet_setup.take() {
            self.vnet_setups.insert(full_name.to_string(), setup);
        }
        if let Some(alloc) = claims.allocated_ip.take() {
            self.allocated_ips.insert(full_name.to_string(), alloc);
        }

        // After networking, so services can bind their addresses.
        if jail_def.init.unwrap_or(true) {
            run_jail_rc(full_name, jid);
        }

        let hook_context = hook_context.with_jid(jid);

        if let Err(e) = hook_runner.execute_phase(HookPhase::PostStart, &hook_context) {
            eprintln!("Warning: post_start hook failed for jail '{full_name}': {e}");
            eprintln!("Jail is running but may not be fully configured.");
        }

        let mut jail_config = JailConfig::new(full_name, &path);
        if let Some(hostname) = &jail_def.hostname {
            jail_config = jail_config.hostname(hostname);
        }
        if let Some(ip) = effective_ip {
            jail_config = jail_config.ip(ip);
        }
        self.record_running(full_name, jail_config, jid, jail_handle, scope);

        Ok(())
    }

    /// Phase 1: the jail root; a dataset created here is claimed first.
    fn claim_jail_root(
        &self,
        jail_def: &crate::manifest::JailDef,
        full_name: &str,
        scope: &mut ScopeRecord,
        claims: &mut StartClaims,
    ) -> Result<PathBuf> {
        // Create ZFS dataset if needed (a prior `build` may already own it).
        // A clone-ready release provisions the whole root in one zfs clone.
        let path = if let Some(zfs) = &self.zfs {
            if jail_def.path.is_none() {
                if zfs.jail_dataset_exists(full_name)? {
                    zfs.jail_path(full_name)
                } else if let Some(release) = jail_def
                    .release
                    .as_ref()
                    .filter(|release| zfs.release_is_cloneable(release))
                {
                    println!("Provisioning jail '{full_name}' by cloning release '{release}'...");
                    let path = zfs.clone_jail_from_release(release, full_name)?;
                    claims.zfs_dataset = true;
                    path
                } else {
                    let path = zfs.create_jail_dataset(full_name)?;
                    claims.zfs_dataset = true;
                    path
                }
            } else {
                jail_def.effective_path(&self.config.config, full_name)
            }
        } else {
            jail_def.effective_path(&self.config.config, full_name)
        };

        if let Some(zfs) = &self.zfs {
            let ephemeral = jail_def.ephemeral.unwrap_or(true);
            if claims.zfs_dataset {
                zfs.mark_jail_dataset(full_name, ephemeral)?;
            }
            // A dataset an earlier run created and left behind is still ours.
            let reused =
                !claims.zfs_dataset && zfs.jail_dataset_mark(full_name) == Some(true) && ephemeral;
            if claims.zfs_dataset || reused {
                let dataset = zfs.jail_dataset_name(full_name);
                scope.zfs_origin = zfs.dataset_origin(&dataset);
                scope.zfs_dataset = Some(dataset.clone());
                scope.ephemeral = ephemeral;
                self.persist_scope(scope);
                self.audit.record(
                    &AuditRecord::new(full_name, AuditEvent::Provisioned)
                        .with("step", "zfs")
                        .with("dataset", dataset),
                );
            }
        }
        crate::fault::trip(crate::fault::Point::AfterZfs)?;

        Ok(path)
    }

    /// Phase 2: symlink checks, userland, DNS. Claims nothing.
    fn prepare_jail_root(
        &self,
        jail_def: &crate::manifest::JailDef,
        full_name: &str,
        path: &Path,
    ) -> Result<()> {
        // Validate no ancestor of the jail path is a symlink
        if let Some(parent) = path.parent()
            && parent.exists()
        {
            crate::blueprint::context::reject_symlink_ancestors(
                &self.config.config.data_dir,
                path,
            )?;
        }

        // A pre-existing symlink here would redirect jail ops to arbitrary host paths.
        if let Ok(meta) = std::fs::symlink_metadata(path)
            && meta.file_type().is_symlink()
        {
            return Err(Error::JailOperation(format!(
                "Jail path '{}' is a symlink, refusing to use it",
                path.display()
            )));
        }

        // Auto-provision from release when the root is missing or empty.
        // A freshly created ZFS dataset exists but holds no userland, so
        // checking for ./bin instead of bare existence catches both cases.
        if !path.join("bin").exists() {
            let Some(release) = &jail_def.release else {
                return Err(Error::JailPathNotFound(path.to_path_buf()));
            };
            crate::manifest::validate_name("release", release)?;
            let release_path = self.config.config.releases_dir.join(release);
            if !release_path.exists() {
                return Err(Error::JailOperation(format!(
                    "Release '{}' not found at {}. Run 'blackship bootstrap {}' first.",
                    release,
                    release_path.display(),
                    release
                )));
            }
            println!("Provisioning jail '{full_name}' from release '{release}'...");
            if let Err(e) = copy_release(&release_path, path) {
                // Rollback; keep the original error.
                let _ = std::fs::remove_dir_all(path);
                return Err(e);
            }
            println!("Jail '{full_name}' provisioned from release '{release}'");
        }

        // Configure DNS before starting the jail
        if let Some(network) = &jail_def.network {
            super::dns::configure_dns(path, &network.dns)?;
        }

        Ok(())
    }

    /// The bridge name and VNET config a jail asks for. Claims nothing.
    fn vnet_config_for(
        &self,
        network: &crate::manifest::JailNetworkConfig,
        full_name: &str,
        effective_ip: Option<IpAddr>,
    ) -> Result<(String, VnetConfig)> {
        let primary_runtime_network = network
            .networks
            .first()
            .map(|network_name| {
                self.runtime_networks
                    .get(network_name)
                    .cloned()
                    .ok_or_else(|| {
                        Error::Network(format!(
                            "Jail '{full_name}' references unknown network '{network_name}'"
                        ))
                    })
            })
            .transpose()?;

        let bridge_name = network
            .bridge
            .clone()
            .or_else(|| {
                primary_runtime_network
                    .as_ref()
                    .and_then(|runtime| runtime.bridge.clone())
            })
            .ok_or_else(|| {
                Error::Network(format!(
                    "VNET jail '{full_name}' requires a bridge configuration"
                ))
            })?;

        let ip_config = if let Some(ip_cidr) = network.ip_cidr.clone() {
            ip_cidr
        } else {
            let ip = network.ip.or(effective_ip).ok_or_else(|| {
                Error::Network(format!(
                    "VNET jail '{full_name}' requires an IP address or attached network"
                ))
            })?;
            let prefix_len = primary_runtime_network
                .as_ref()
                .map_or(24, |runtime| runtime.subnet.prefix_len());
            format!("{ip}/{prefix_len}")
        };

        let gateway = network
            .gateway
            .or_else(|| {
                primary_runtime_network
                    .as_ref()
                    .map(|runtime| runtime.gateway)
            })
            .ok_or_else(|| {
                Error::Network(format!(
                    "VNET jail '{full_name}' requires a gateway or attached network"
                ))
            })?;

        let mut vnet_config = VnetConfig::new(bridge_name.clone(), ip_config, gateway);

        if let Some(ref mac) = network.mac_address {
            vnet_config = vnet_config.with_mac_address(mac.clone());
        }

        if let Some(vlan_id) = network.vlan_id {
            vnet_config = vnet_config.with_vlan_id(vlan_id);
        }

        // Explicit backend wins, else inherit the attached network's.
        let backend = network.backend.unwrap_or_else(|| {
            match primary_runtime_network
                .as_ref()
                .map(|runtime| runtime.backend.as_str())
            {
                Some("netgraph") => crate::network::vnet::NetworkBackend::Netgraph,
                _ => crate::network::vnet::NetworkBackend::Epair,
            }
        });
        vnet_config = vnet_config.with_backend(backend);

        Ok((bridge_name, vnet_config))
    }

    /// Phase 4: create the VNET pair, claimed before its state record is written.
    fn claim_vnet(
        &self,
        network: &crate::manifest::JailNetworkConfig,
        full_name: &str,
        effective_ip: Option<IpAddr>,
        scope: &mut ScopeRecord,
        claims: &mut StartClaims,
    ) -> Result<()> {
        let (bridge_name, vnet_config) = self.vnet_config_for(network, full_name, effective_ip)?;

        let setup = claims
            .vnet_setup
            .insert(VnetSetup::create(full_name, vnet_config)?);

        if self.verbose {
            println!(
                "  Created {} <-> {} for VNET jail",
                setup.host_interface(),
                setup.jail_interface()
            );
            println!(
                "  Added {} to bridge {}",
                setup.host_interface(),
                bridge_name
            );
        }

        let state_record =
            setup.state_record(full_name, network.networks.first().map(String::as_str));
        self.vnet_state_store.save(&state_record)?;

        scope.has_vnet_record = true;
        self.persist_scope(scope);
        self.audit.record(
            &AuditRecord::new(full_name, AuditEvent::Provisioned)
                .with("step", "vnet")
                .with("host_interface", setup.host_interface())
                .with("bridge", &bridge_name),
        );

        Ok(())
    }

    /// Phase 5: jail(2). Claims the root before the attempt, the jid after.
    fn create_jail(
        &self,
        full_name: &str,
        path: &Path,
        params: &HashMap<String, ParamValue>,
        scope: &mut ScopeRecord,
        claims: &mut StartClaims,
    ) -> Result<(i32, JailHandle)> {
        println!("Starting jail '{full_name}'...");
        // Owning descriptors are only useful while a long-lived supervisor
        // process keeps them open. For one-shot CLI commands like `up`, using
        // an owning descriptor would remove the jail as soon as blackship exits.
        let use_descriptors =
            self.os_version.supports_jail_descriptors() && self.warden_handle.is_some();
        claims.jail_root = Some(path.to_path_buf());
        let created = if use_descriptors {
            jail_create_with_descriptor(path, params)
                .map(|(jid, fd)| (jid, JailHandle::Descriptor { fd, jid }))
        } else {
            jail_create(path, params).map(|jid| (jid, JailHandle::Jid(jid)))
        };
        let (jid, jail_handle) =
            created.inspect_err(|e| eprintln!("Failed to create jail '{full_name}': {e}"))?;
        claims.jid = Some(jid);
        println!(
            "Jail '{}' started with JID {}{}",
            full_name,
            jid,
            if use_descriptors {
                " (owning descriptor)"
            } else {
                ""
            }
        );

        scope.jid = Some(jid);
        self.persist_scope(scope);
        self.audit.record(
            &AuditRecord::new(full_name, AuditEvent::Provisioned)
                .with("step", "jail_create")
                .with("jid", jid),
        );

        Ok((jid, jail_handle))
    }

    /// Phase 6: rctl limits and CPU pinning. Fails closed.
    fn apply_startup_limits(
        &self,
        jail_def: &crate::manifest::JailDef,
        service_name: &str,
        full_name: &str,
        jid: i32,
        scope: &mut ScopeRecord,
    ) -> Result<()> {
        // Fail closed: never run without the configured resource limits.
        let startup_profile = jail_def.startup_resources();
        crate::fault::trip(crate::fault::Point::BeforeRctl)?;
        if !startup_profile.is_empty() {
            crate::rctl::apply_limits(full_name, startup_profile).inspect_err(|e| {
                eprintln!("Failed to apply resource limits for '{full_name}': {e}, stopping jail");
            })?;

            scope.rctl_subject = Some(format!("jail:{full_name}"));
            if jail_def.qos.is_some() {
                scope.qos_phase = crate::scope::QosPhase::Startup;
            }
            self.persist_scope(scope);
            self.audit.record(
                &AuditRecord::new(full_name, AuditEvent::Provisioned)
                    .with("step", "rctl")
                    .with(
                        "qos_phase",
                        if jail_def.qos.is_some() {
                            "startup"
                        } else {
                            "none"
                        },
                    ),
            );
        }

        // Pin the jail to its CPU set, spreading across other jails' pins.
        let other_pins = self.other_cpuset_pins(service_name);
        if let Some(cpu_list) = crate::rctl::resolve_cpu_list(startup_profile, other_pins) {
            crate::rctl::apply_cpuset(jid, &cpu_list).inspect_err(|e| {
                eprintln!("Failed to pin jail '{full_name}' to CPUs: {e}, stopping jail");
            })?;
            println!("  Pinned to CPUs: {cpu_list}");
            scope.cpuset = Some(cpu_list.clone());
            self.persist_scope(scope);
            self.audit.record(
                &AuditRecord::new(full_name, AuditEvent::Provisioned)
                    .with("step", "cpuset")
                    .with("cpus", &cpu_list),
            );
        }

        Ok(())
    }

    /// Last phase: track the instance, finish the scope record, tell the Warden.
    fn record_running(
        &mut self,
        full_name: &str,
        jail_config: JailConfig,
        jid: i32,
        jail_handle: JailHandle,
        scope: &mut ScopeRecord,
    ) {
        let mut instance = JailInstance::new(jail_config);
        instance.jid = Some(jid);
        instance.handle = Some(jail_handle);
        // Fresh instance: Stopped -> Starting -> Running cannot be rejected.
        instance.start().ok();
        instance.started().ok(); // see above
        self.instances.insert(full_name.to_string(), instance);

        scope.advance_or_warn(ScopeMachineEvent::Provisioned);
        self.persist_scope(scope);
        self.audit
            .record(&AuditRecord::new(full_name, AuditEvent::Running).with("jid", jid));

        if let Some(handle) = &self.warden_handle
            && let Err(e) = handle.notify_started(full_name)
        {
            eprintln!("Warning: Failed to notify Warden of jail start: {e}");
        }
    }

    /// Release what a failed `provision_and_start` claimed, newest first.
    /// A jail that refuses to die keeps everything.
    fn unwind_start(&mut self, full_name: &str, claims: StartClaims) {
        if let (Some(jid), Some(root)) = (claims.jid, &claims.jail_root)
            && let Err(e) = jail_remove(jid)
        {
            eprintln!(
                "Warning: Failed to remove jail '{full_name}' (JID {jid}) during rollback: {e}"
            );
            self.mark_jail_failed(full_name, root);
            return;
        }
        if let Some(root) = &claims.jail_root {
            crate::sys::unmount_jail_devfs(root);
        }
        self.rollback_start(
            full_name,
            claims.vnet_setup,
            claims.allocated_ip.as_ref(),
            claims.zfs_dataset,
        );
        if let Some(root) = &claims.jail_root {
            self.mark_jail_failed(full_name, root);
        }
    }

    /// CPU pins already claimed by every jail other than `service_name`.
    pub(super) fn other_cpuset_pins<'a>(
        &'a self,
        service_name: &'a str,
    ) -> impl Iterator<Item = &'a str> {
        self.config
            .jails
            .iter()
            .filter(move |other| other.name != service_name)
            .filter_map(|other| other.startup_resources().cpuset.as_deref())
    }

    /// Addresses a draining jail can still be reached on.
    fn drainable_ips(&self, full_name: &str, jail_def: &crate::manifest::JailDef) -> Vec<IpAddr> {
        let mut ips: Vec<IpAddr> = Vec::new();

        if let Some(network) = &jail_def.network
            && let Some(ip) = network.static_ip()
        {
            ips.push(ip);
        }

        if let Some((_, ip)) = self.allocated_ips.get(full_name) {
            ips.push(*ip);
        }

        if let Ok(Some(scope)) = self.scope_store.get(full_name) {
            ips.extend(scope.allocated_ips);
        }

        ips.sort();
        ips.dedup();
        ips
    }

    /// Refuse new connections and wait for in-flight ones. Returns the anchor.
    fn drain_connections(
        &self,
        full_name: &str,
        jail_def: &crate::manifest::JailDef,
    ) -> Option<String> {
        let drain = jail_def.drain.as_ref()?;
        if !drain.enabled {
            return None;
        }

        if !crate::bulkhead::pf_enabled() {
            eprintln!("Warning: PF is not enabled; skipping connection drain for '{full_name}'");
            return None;
        }

        let ips = self.drainable_ips(full_name, jail_def);
        if ips.is_empty() {
            eprintln!("Warning: no recorded address for '{full_name}'; skipping connection drain");
            return None;
        }

        let anchor = match crate::bulkhead::load_drain_anchor(
            &self.config.config.data_dir,
            full_name,
            &ips,
        ) {
            Ok(anchor) => anchor,
            Err(e) => {
                eprintln!(
                    "Warning: failed to load drain rules for '{full_name}': {e} -- stopping without drain"
                );
                return None;
            }
        };

        self.amend_scope(full_name, |scope| {
            scope.advance_or_warn(ScopeMachineEvent::Drain);
            scope.drain_anchor = Some(anchor.clone());
        });
        self.audit.record(
            &AuditRecord::new(full_name, AuditEvent::DrainStart)
                .with("anchor", &anchor)
                .with("timeout_secs", drain.timeout_secs),
        );

        println!(
            "Draining connections to '{}' (timeout {}s)...",
            full_name, drain.timeout_secs
        );

        let deadline = Instant::now() + std::time::Duration::from_secs(drain.timeout_secs);
        let mut remaining = usize::MAX;
        while Instant::now() < deadline {
            remaining = ips
                .iter()
                .map(|ip| crate::bulkhead::tcp_state_count(*ip))
                .sum();
            if remaining == 0 {
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(500));
        }

        let timed_out = remaining != 0;
        if timed_out {
            eprintln!(
                "Warning: '{}' still had {} TCP state(s) after {}s; proceeding with stop",
                full_name, remaining, drain.timeout_secs
            );
        } else if self.verbose {
            println!("  All TCP states for '{full_name}' closed");
        }

        self.audit.record(
            &AuditRecord::new(full_name, AuditEvent::DrainEnd)
                .with("anchor", &anchor)
                .with("residual_states", remaining)
                .with("timed_out", timed_out),
        );

        Some(anchor)
    }

    /// Drop the drain anchor. Required on every path out of `stop_jail`.
    fn end_drain(&self, full_name: &str, anchor: Option<&str>) {
        let Some(anchor) = anchor else {
            return;
        };
        crate::bulkhead::flush_drain_anchor(anchor);
        self.amend_scope(full_name, |scope| scope.drain_anchor = None);
    }

    pub(super) fn stop_jail(&mut self, name: &str) -> Result<()> {
        let (service_name, full_name) = self.resolve_jail_names(name)?;

        // A held handle is authoritative: resolving the name again would race
        // against jid reuse.
        let handle_jid = self
            .instances
            .get(&full_name)
            .and_then(|instance| instance.handle.as_ref())
            .map(JailHandle::jid);

        let jid = match handle_jid {
            Some(jid) => jid,
            None => match jail_getid(&full_name) {
                Ok(jid) => jid,
                Err(_) => {
                    return Err(Error::JailNotRunning(full_name));
                }
            },
        };

        self.amend_scope(&full_name, |scope| {
            scope.advance_or_warn(ScopeMachineEvent::Terminate);
        });
        self.audit
            .record(&AuditRecord::new(&full_name, AuditEvent::Terminating).with("jid", jid));

        // Remove RCTL limits before stopping
        crate::rctl::remove_limits(&full_name);
        self.amend_scope(&full_name, |scope| scope.rctl_subject = None);

        if self
            .config
            .get_jail(&service_name)
            .and_then(|def| def.drain.as_ref())
            .is_some_and(|drain| drain.enabled)
            && let Some(instance) = self.instances.get_mut(&full_name)
        {
            instance.drain().ok(); // only valid from Running; otherwise nothing to drain
        }

        let (post_stop_hook, drain_anchor) = self.quiesce_jail(&service_name, &full_name, jid)?;

        if let Some(handle) = &self.warden_handle {
            handle.mark_stop_requested(jid);
        }

        println!("Stopping jail '{full_name}'...");
        let owned_handle = self
            .instances
            .get_mut(&full_name)
            .and_then(|instance| instance.handle.take());
        let remove_result = match owned_handle {
            Some(JailHandle::Descriptor { fd, .. }) => fd.remove(),
            _ => jail_remove(jid),
        };
        if let Err(e) = remove_result {
            if let Some(handle) = &self.warden_handle {
                handle.clear_stop_requested(jid);
            }
            self.end_drain(&full_name, drain_anchor.as_deref());
            return Err(e);
        }
        println!("Jail '{full_name}' stopped");

        self.end_drain(&full_name, drain_anchor.as_deref());

        if let Some(handle) = &self.warden_handle
            && let Err(e) = handle.notify_stopped(&full_name, jid)
        {
            handle.clear_stop_requested(jid);
            eprintln!("Warning: Failed to notify Warden of jail stop: {e}");
        }

        let mut stop_result = Ok(());
        if let Some((hook_runner, path)) = post_stop_hook {
            let hook_context = HookContext::new(&full_name, &path);
            if let Err(e) = hook_runner.execute_phase(HookPhase::PostStop, &hook_context) {
                stop_result = Err(e);
            }
            crate::sys::unmount_jail_devfs(&path);
        }

        let leaked_resources = self.settle_stopped(&full_name);

        // The scope record is the GC work-list: a leak keeps it alive in `Failed`.
        let peaks = self.peak_details(&full_name);
        let destroyed = |mut record: AuditRecord| -> AuditRecord {
            record.detail.extend(peaks.iter().cloned());
            record
        };

        if leaked_resources {
            self.mark_scope_failed(&full_name);
            self.audit.record(&destroyed(
                AuditRecord::new(&full_name, AuditEvent::Destroyed).with("residual", "true"),
            ));
        } else {
            self.audit.record(&destroyed(
                AuditRecord::new(&full_name, AuditEvent::Destroyed).with("jid", jid),
            ));
            if let Err(e) = self.scope_store.delete(&full_name) {
                eprintln!("Warning: failed to remove scope record for '{full_name}': {e}");
            }
        }

        stop_result
    }

    /// `pre_stop` hooks, drain, stop command and `rc.shutdown`; returns the
    /// `post_stop` hook and the drain anchor.
    fn quiesce_jail(
        &self,
        service_name: &str,
        full_name: &str,
        jid: i32,
    ) -> Result<(Option<PostStopHook>, Option<String>)> {
        let Some(jail_def) = self.config.get_jail(service_name) else {
            return Ok((None, None));
        };

        let path = jail_def.effective_path(&self.config.config, full_name);
        let hook_runner = HookRunner::new(jail_def.hooks.clone()).verbose(self.verbose);
        let mut hook_context = HookContext::new(full_name, &path).with_jid(jid);

        if let Some(network) = &jail_def.network
            && let Some(ip) = network.static_ip()
        {
            hook_context = hook_context.with_ip(ip.to_string());
        }

        hook_runner.execute_phase(HookPhase::PreStop, &hook_context)?;

        let drain_anchor = self.drain_connections(full_name, jail_def);

        if let Some(stop_cmd) = jail_def.resolve_stop()
            && let Ok(exit_code) = crate::jail::jexec::jexec_run(jid, &["/bin/sh", "-c", &stop_cmd])
            && exit_code != 0
        {
            eprintln!("Warning: stop command in jail '{full_name}' exited with {exit_code}");
        }

        if jail_def.init.unwrap_or(true)
            && let Ok(exit_code) =
                crate::jail::jexec::jexec_run(jid, &["/bin/sh", "/etc/rc.shutdown"])
            && exit_code != 0
        {
            eprintln!("Warning: /etc/rc.shutdown in jail '{full_name}' exited with {exit_code}");
        }

        Ok((Some((hook_runner, path)), drain_anchor))
    }

    /// Move the instance to `Stopped`, then release what leaving `Up` held.
    /// Returns whether a claim leaked.
    fn settle_stopped(&mut self, full_name: &str) -> bool {
        let mut step = None;
        if let Some(instance) = self.instances.get_mut(full_name) {
            // The jail is gone either way; a rejected transition is harmless.
            instance.stop().ok();
            instance.stopped().ok(); // see above
            instance.jid = None;
            instance.handle = None;
            if instance.state() == JailState::Stopped {
                step = instance.last_step().copied();
            }
        }

        // Not tracked by this process: the jail was mid-stop by construction.
        let step = step.or_else(|| {
            Step::phase(
                MachineState::Stopping,
                Health::default(),
                MachineState::Stopped,
            )
            .ok()
        });
        let mut leaked_resources = false;
        for node in step.iter().flat_map(Step::exit) {
            if node == Node::Up {
                leaked_resources = self.release_vnet_on_stop(full_name);
                self.release_ip_on_stop(full_name);
            }
        }
        leaked_resources
    }

    /// Tear down the jail's VNET pair; returns whether anything leaked.
    fn release_vnet_on_stop(&mut self, full_name: &str) -> bool {
        let mut leaked_resources = false;
        if let Some(vnet_setup) = self.vnet_setups.remove(full_name) {
            match vnet_setup.cleanup() {
                Ok(()) => {
                    if let Err(e) = self.vnet_state_store.delete(full_name) {
                        eprintln!(
                            "Warning: failed to remove VNET state record for '{full_name}': {e}"
                        );
                    }
                    if self.verbose {
                        println!("  Cleaned up VNET for bridge {}", vnet_setup.bridge_name);
                    }
                }
                Err(e) => {
                    eprintln!(
                        "Warning: Failed to cleanup VNET setup for jail '{full_name}': {e} -- keeping state record for manual cleanup"
                    );
                    leaked_resources = true;
                }
            }
        } else if let Ok(Some(record)) = self.vnet_state_store.get(full_name) {
            match VnetSetup::cleanup_state(&record) {
                Ok(()) => {
                    if let Err(e) = self.vnet_state_store.delete(full_name) {
                        eprintln!(
                            "Warning: failed to remove VNET state record for '{full_name}': {e}"
                        );
                    }
                }
                Err(e) => {
                    eprintln!(
                        "Warning: Failed to cleanup persisted VNET setup for jail '{full_name}': {e} -- keeping state record for manual cleanup"
                    );
                    leaked_resources = true;
                }
            }
        }

        leaked_resources
    }

    /// Return the jail's IP lease(s) to the pool.
    fn release_ip_on_stop(&mut self, full_name: &str) {
        if let Some((network_name, ip)) = self.allocated_ips.remove(full_name) {
            self.ip_allocator.release(&network_name, &ip);
            if let Err(e) = self.lease_store.release(&network_name, full_name) {
                eprintln!("Warning: failed to release lease for '{full_name}': {e}");
            }
            if self.verbose {
                println!("  Released IP {ip} back to network '{network_name}'");
            }
        } else if let Err(e) = self.lease_store.release_owner(full_name) {
            eprintln!("Warning: failed to release leases for '{full_name}': {e}");
        }
    }

    /// Force cleanup of a failed jail
    pub fn cleanup(&mut self, name: &str, force: bool) -> Result<()> {
        let (service_name, full_name) = self.resolve_jail_names(name)?;
        println!("Cleaning up jail '{full_name}'...");

        let jail_def = self.config.get_jail(&service_name);

        if let Ok(jid) = jail_getid(&full_name) {
            println!("  Removing jail (JID {jid})...");
            if let Err(e) = jail_remove(jid) {
                if force {
                    eprintln!("  Warning: Failed to remove jail: {e}");
                } else {
                    return Err(e);
                }
            }
        }

        if let Some(zfs) = &self.zfs
            && let Some(jail_def) = jail_def
            && jail_def.path.is_none()
        {
            println!("  Destroying ZFS dataset...");
            if let Err(e) = zfs.destroy_jail_dataset(&full_name) {
                if force {
                    eprintln!("  Warning: Failed to destroy dataset: {e}");
                } else {
                    return Err(e);
                }
            }
        }

        self.instances.remove(&full_name);

        if let Some(vnet_setup) = self.vnet_setups.remove(&full_name) {
            println!("  Cleaning up VNET setup...");
            if let Err(e) = vnet_setup.cleanup() {
                if force {
                    eprintln!("  Warning: Failed to cleanup VNET setup: {e}");
                } else {
                    return Err(e);
                }
            }
            if let Err(e) = self.vnet_state_store.delete(&full_name) {
                eprintln!("  Warning: Failed to remove VNET state record: {e}");
            }
        } else if let Ok(Some(record)) = self.vnet_state_store.get(&full_name) {
            println!("  Cleaning up persisted VNET state...");
            if let Err(e) = VnetSetup::cleanup_state(&record) {
                if force {
                    eprintln!("  Warning: Failed to cleanup persisted VNET state: {e}");
                } else {
                    return Err(e);
                }
            }
            if let Err(e) = self.vnet_state_store.delete(&full_name) {
                eprintln!("  Warning: Failed to remove VNET state record: {e}");
            }
        }

        if let Some((network_name, ip)) = self.allocated_ips.remove(&full_name) {
            self.ip_allocator.release(&network_name, &ip);
            if let Err(e) = self.lease_store.release(&network_name, &full_name) {
                eprintln!("  Warning: Failed to release lease: {e}");
            }
            println!("  Released IP {ip} back to network '{network_name}'");
        } else {
            let released = self.lease_store.release_owner(&full_name)?;
            for (network_name, ip) in released {
                println!("  Released IP {ip} back to network '{network_name}'");
            }
        }

        self.audit
            .record(&AuditRecord::new(&full_name, AuditEvent::Destroyed).with("via", "cleanup"));
        if let Err(e) = self.scope_store.delete(&full_name) {
            eprintln!("Warning: failed to remove scope record for '{full_name}': {e}");
        }

        println!("Cleanup complete for jail '{full_name}'");
        Ok(())
    }
}

#[cfg(test)]
mod tests;