subc-daemon 0.47.0

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
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
//! `app.open`: start a known operator app from its installed location.
//!
//! The decision order is fixed and checked top to bottom: `app_open_not_used`
//! on macOS (which never launches through the daemon) and on platforms without
//! a launcher, `app_open_disabled` when the platform switch is off,
//! `app_unknown` for an app the compiled table does not name,
//! `already_running` while a launch entry is live, `app_not_installed` when
//! the installed file is missing, and only then verification and launch.
//!
//! Launch entries live in one table behind one lock. The lock is a leaf: take
//! no other lock while holding it. An entry holds the daemon-generated incarnation id, the launched
//! pid, and the platform liveness handle: a pidfd on Linux, the owned process
//! handle on Windows. Liveness is read from that handle, never by reopening
//! the numeric pid, so a reused pid confers nothing. On Windows the entry is
//! published before the image is resumed, so a child that connects at once
//! is matched. On Linux the entry is published after spawn and tracking, so
//! the image may run first, which is safe because Linux connections are
//! never stamped. Entries retire when no longer live. The table is memory-only: a daemon or installed-service
//! restart drops it, and a relaunched daemon never mistakes an older process
//! for its own launch. Connections from such a process are plain `direct`.
//!
//! Starting the app grants nothing by itself: a shortcut still needs a
//! stamped connection. (A Windows stamp records the live entry's incarnation
//! at connection time; nothing revalidates it yet.) So `app.open` is classified
//! `None` in the privileged-operation classifier and neither prompts in
//! `enforce` mode nor logs in `log` mode.

use std::{
    collections::HashMap,
    path::PathBuf,
    sync::{
        atomic::{AtomicU64, Ordering},
        Mutex, MutexGuard,
    },
};

/// Filesystem paths only exist on platforms with a launcher; the decision and
/// table code below names locations through `PathBuf` alone.
#[cfg(any(windows, target_os = "linux"))]
use std::path::Path;

use semver::Version;

use crate::{
    control::ControlHandler,
    operator_apps::{AppOpenPlatform, OperatorAppPolicy},
    router::{RouteCtx, RouterError},
    Frame,
};

/// One live-or-recently-live launch slot. At most one entry per app exists.
pub(crate) struct LaunchEntry {
    incarnation: u64,
    pid: u32,
    start_time: Option<u64>,
    process: subc_os::Process,
    child: Option<std::process::Child>,
}

impl LaunchEntry {
    /// Whether the retained handle still reports this exact process alive.
    /// The stored start time must match the current observation, so a pid the
    /// kernel has since handed to another process reads as dead even on
    /// platforms whose handle cannot pin the incarnation by itself.
    fn is_live(&self) -> bool {
        self.process
            .observe()
            .is_some_and(|observed| Some(observed.start_time) == self.start_time)
    }

    /// Release a retired entry's handles without signalling anything:
    /// dropping the entry never stops a live child, and an exited direct
    /// child is reaped so it never lingers as a zombie. The daemon never signals here: liveness-triggered retirement
    /// follows an observed exit, and `retire` or a `publish` replacement only
    /// drops the handles and reaps what already exited.
    fn reap(self) {
        if let Some(mut child) = self.child {
            // Our own direct child, if it already exited: release its pid.
            let _ = child.try_wait();
        }
    }

    /// Stop a launch that failed after the child existed but before it could
    /// run: kill the process the daemon still holds, then release it. Used
    /// only on paths that refuse the launch, never on a running app.
    #[cfg(windows)]
    fn stop_rejected(mut self) {
        if let Some(mut child) = self.child.take() {
            let _ = child.kill();
            while matches!(child.try_wait(), Ok(None)) {
                std::thread::yield_now();
            }
        }
        // Dropping `process` closes the retained handle; a suspended child
        // killed above is already gone, and killing an exited child is a
        // harmless error.
    }
}

/// The pid and incarnation of a live launch entry, for replies and stamps.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct LiveLaunch {
    pub incarnation: u64,
    pub pid: u32,
}

/// One launch table for the daemon, behind one leaf lock. No other lock is
/// taken while it is held.
pub(crate) struct LaunchTable {
    inner: Mutex<HashMap<String, LaunchEntry>>,
    next_incarnation: AtomicU64,
}

/// The methods run per platform and in tests: `publish` and friends launch
/// on Windows and Linux, while other builds only exercise them under test.
/// (Check builds without test code would otherwise call the idle ones dead.)
#[allow(dead_code)]
impl LaunchTable {
    pub fn new() -> Self {
        Self {
            inner: Mutex::new(HashMap::new()),
            next_incarnation: AtomicU64::new(1),
        }
    }

    fn lock(&self) -> MutexGuard<'_, HashMap<String, LaunchEntry>> {
        self.inner
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    fn retire_locked(inner: &mut HashMap<String, LaunchEntry>, app: &str) {
        if let Some(entry) = inner.remove(app) {
            entry.reap();
        }
    }

    /// The live entry for `app`, retiring it first when its handle reports
    /// exit. A dead entry never matches again, even through an inherited
    /// socket or pipe handle, and even after `app.open` launches a
    /// replacement with a new incarnation.
    pub fn live_entry(&self, app: &str) -> Option<LiveLaunch> {
        let mut inner = self.lock();
        let entry = inner.get(app)?;
        if !entry.is_live() {
            Self::retire_locked(&mut inner, app);
            return None;
        }
        Some(LiveLaunch {
            incarnation: entry.incarnation,
            pid: entry.pid,
        })
    }

    /// The incarnation whose entry is live for `pid`: a pipe client is
    /// eligible only when its pid is that of a live launch entry.
    pub fn match_live_pid(&self, app: &str, pid: u32) -> Option<u64> {
        let live = self.live_entry(app)?;
        (live.pid == pid).then_some(live.incarnation)
    }

    /// Whether `incarnation` is still the live entry for `app`. A later entry
    /// for the same app never revalidates an older stamp.
    pub fn is_live_incarnation(&self, app: &str, incarnation: u64) -> bool {
        self.with_live_incarnation(app, incarnation, || ())
            .is_some()
    }

    /// Keep retirement and replacement excluded until the caller commits its
    /// decision. The caller already holds every other lock it needs.
    pub(crate) fn with_live_incarnation<T>(
        &self,
        app: &str,
        incarnation: u64,
        commit: impl FnOnce() -> T,
    ) -> Option<T> {
        let mut inner = self.lock();
        let entry = inner.get(app)?;
        if !entry.is_live() {
            Self::retire_locked(&mut inner, app);
            return None;
        }
        (entry.incarnation == incarnation).then(commit)
    }

    /// Publish a launch entry, replacing any entry for the app. Callers retire
    /// dead entries through the liveness check first, so a replaced entry was
    /// already retired and its handles are closed here.
    pub fn publish(
        &self,
        app: &str,
        pid: u32,
        process: subc_os::Process,
        child: Option<std::process::Child>,
    ) -> u64 {
        let incarnation = self.next_incarnation.fetch_add(1, Ordering::Relaxed);
        let start_time = process.observe().map(|observed| observed.start_time);
        let mut inner = self.lock();
        Self::retire_locked(&mut inner, app);
        inner.insert(
            app.to_owned(),
            LaunchEntry {
                incarnation,
                pid,
                start_time,
                process,
                child,
            },
        );
        incarnation
    }

    /// Forget the entry without signalling the process: the launched app
    /// survives a daemon or installed-service restart by design.
    pub fn retire(&self, app: &str) {
        Self::retire_locked(&mut self.lock(), app);
    }

    /// Take the entry for a launch that must be stopped after publishing,
    /// such as a child that cannot be resumed. The caller stops the returned
    /// entry; nothing else in the table refers to the pid afterwards.
    #[cfg(windows)]
    pub fn take(&self, app: &str) -> Option<LaunchEntry> {
        self.lock().remove(app)
    }
}

/// Why `app.open` refused to launch.
#[derive(Debug)]
pub(crate) struct AppOpenRefusal {
    pub code: &'static str,
    pub message: String,
}

impl AppOpenRefusal {
    fn not_used(message: impl Into<String>) -> Self {
        Self {
            code: subc_control::APP_OPEN_NOT_USED,
            message: message.into(),
        }
    }

    fn disabled(message: impl Into<String>) -> Self {
        Self {
            code: subc_control::APP_OPEN_DISABLED,
            message: message.into(),
        }
    }

    fn unknown(message: impl Into<String>) -> Self {
        Self {
            code: subc_control::APP_UNKNOWN,
            message: message.into(),
        }
    }

    pub fn not_installed(message: impl Into<String>) -> Self {
        Self {
            code: subc_control::APP_NOT_INSTALLED,
            message: message.into(),
        }
    }

    /// Refusal when the installed binary fails the daemon's checks. Only the
    /// Windows launch path verifies, and only with an injected test check:
    /// production carries no verifier and fails closed here. Where the daemon
    /// verifies, the test hook receives the expected publisher, the daemon
    /// enforces the floor itself on the returned version, and the held
    /// handle pins the file throughout.
    #[cfg(windows)]
    pub fn verification_failed(message: impl Into<String>) -> Self {
        Self {
            code: subc_control::APP_VERIFICATION_FAILED,
            message: message.into(),
        }
    }
}

/// What the ordered checks decided, before any verification or process starts.
pub(crate) enum AppOpenDecision {
    Refuse(AppOpenRefusal),
    AlreadyRunning { pid: u32 },
    Launch { path: PathBuf },
}

/// The fixed order: `app_open_not_used` (macOS, or a platform without a
/// launcher), `app_open_disabled` (platform switch off), `app_unknown`,
/// retire-or-`already_running`, `app_not_installed`, then launch.
pub(crate) fn decide_app_open(
    policy: &OperatorAppPolicy,
    table: &LaunchTable,
    platform: AppOpenPlatform,
    app: &str,
) -> AppOpenDecision {
    if matches!(platform, AppOpenPlatform::MacOS | AppOpenPlatform::Other) {
        return AppOpenDecision::Refuse(AppOpenRefusal::not_used(
            "this platform never launches apps through the daemon",
        ));
    }
    if !policy.switch_enabled(platform) {
        return AppOpenDecision::Refuse(AppOpenRefusal::disabled(
            "app.open is switched off on this platform",
        ));
    }
    if policy.app_id().is_none_or(|id| id != app) {
        return AppOpenDecision::Refuse(AppOpenRefusal::unknown(format!(
            "no compiled app named {app:?} on this platform"
        )));
    }
    if let Some(live) = table.live_entry(app) {
        // A live entry answers `already_running` even when the installed file
        // has since vanished: the running process is what the caller asked
        // about, and the next call after its exit relaunches and re-checks
        // the install. The daemon starts nothing and relays nothing here.
        return AppOpenDecision::AlreadyRunning { pid: live.pid };
    }
    let Some(path) = policy.installed_path(platform) else {
        return AppOpenDecision::Refuse(AppOpenRefusal::not_installed(format!(
            "no install location is compiled in for {app:?}"
        )));
    };
    if !path.exists() {
        return AppOpenDecision::Refuse(AppOpenRefusal::not_installed(format!(
            "{} is not installed at {}",
            app,
            path.display()
        )));
    }
    AppOpenDecision::Launch { path }
}
impl ControlHandler {
    /// Serve one `app.open` call: run the ordered checks, then verify and
    /// launch, or report the already-running pid. Like `operator.confirm`
    /// this is probed before the module branch, so any connection can ask;
    /// the launch itself grants nothing, and a shortcut still needs a stamped
    /// connection.
    pub(crate) async fn handle_app_open(
        &self,
        _ctx: &RouteCtx,
        frame: Frame,
    ) -> Result<Vec<Frame>, RouterError> {
        // One launch at a time per handler: the liveness check in the
        // decision and the later publish must not interleave across
        // concurrent calls, or two calls could both launch and the second
        // publish would orphan the first process. The guard covers the
        // whole call; `app.open` is rare, so refusals sharing it cost
        // nothing.
        let _launch_guard = self.app_open_launch_lock.lock().await;
        let request = match serde_json::from_slice::<subc_control::AppOpenRequest>(&frame.body) {
            Ok(request) => request,
            Err(_) => {
                return Ok(vec![crate::control::control_error_frame(
                    &frame,
                    "invalid_control_body",
                    "malformed app.open body",
                )?]);
            }
        };
        let app = request.app.clone();
        let platform = AppOpenPlatform::current();
        #[cfg(test)]
        let platform = self.app_open_test_platform.unwrap_or(platform);
        match decide_app_open(
            &self.operator_app_policy,
            &self.launch_table,
            platform,
            &app,
        ) {
            AppOpenDecision::Refuse(refusal) => Ok(vec![crate::control::control_error_frame(
                &frame,
                refusal.code,
                refusal.message,
            )?]),
            AppOpenDecision::AlreadyRunning { pid } => {
                // `already_running` starts nothing and relays nothing: on
                // Windows it calls the optional attention hook (production
                // only logs), and nothing at all happens on Linux.
                #[cfg(windows)]
                self.flash_running_app(pid);
                Ok(vec![crate::control::control_response_body_frame(
                    &frame,
                    &subc_control::AppOpenReply::already_running(pid),
                    "app.open",
                )?])
            }
            AppOpenDecision::Launch { path } => {
                let outcome = self.launch_app(&app, &path).await;
                match outcome {
                    Ok(pid) => Ok(vec![crate::control::control_response_body_frame(
                        &frame,
                        &subc_control::AppOpenReply::launched(pid),
                        "app.open",
                    )?]),
                    Err(refusal) => Ok(vec![crate::control::control_error_frame(
                        &frame,
                        refusal.code,
                        refusal.message,
                    )?]),
                }
            }
        }
    }

    async fn launch_app(&self, app: &str, path: &std::path::Path) -> Result<u32, AppOpenRefusal> {
        #[cfg(test)]
        if let Some(launch) = self.app_open_test_launch {
            return launch();
        }
        #[cfg(windows)]
        {
            self.windows_launch_app(app, path).await
        }
        #[cfg(target_os = "linux")]
        {
            self.linux_launch_app(app, path).await
        }
        #[cfg(not(any(windows, target_os = "linux")))]
        {
            // The ordered platform check refuses before launching on these hosts.
            let _ = (app, path);
            Err(AppOpenRefusal::not_used(
                "this platform has no app launcher",
            ))
        }
    }
}

/// How a Linux launch is started. With `systemd-run` and a user bus the app
/// runs in its own transient scope, so the daemon unit's `KillMode=mixed`
/// cleanup never reaches it. Otherwise the child only gets its own process
/// group: that separates signals but does not move it out of a service
/// cgroup, so service cleanup can still stop it. Either way the daemon holds
/// the pidfd for its launch entry.
#[cfg(target_os = "linux")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum LinuxLaunchStyle {
    Scope,
    Direct,
}

/// Pick the launch style from what the machine offers: a `systemd-run`
/// binary plus a reachable user bus means a user manager is present.
#[cfg(target_os = "linux")]
pub(crate) fn select_linux_launch_style(
    systemd_run_present: bool,
    user_bus_present: bool,
) -> LinuxLaunchStyle {
    if systemd_run_present && user_bus_present {
        LinuxLaunchStyle::Scope
    } else {
        LinuxLaunchStyle::Direct
    }
}

/// Whether the current user has a systemd user manager: the runtime
/// directory's bus socket exists. The system per-user path is checked too, so
/// a detection made before `XDG_RUNTIME_DIR` is set still finds the manager.
#[cfg(target_os = "linux")]
fn user_bus_present() -> bool {
    use std::os::unix::fs::FileTypeExt as _;

    let uid = rustix::process::getuid().as_raw();
    [
        std::env::var_os("XDG_RUNTIME_DIR").map(PathBuf::from),
        Some(PathBuf::from(format!("/run/user/{uid}"))),
    ]
    .into_iter()
    .flatten()
    .any(|dir| {
        // The socket's presence is the manager's presence: no manager leaves
        // no bus behind, and a missing path fails the check harmlessly.
        std::fs::symlink_metadata(dir.join("bus"))
            .is_ok_and(|metadata| metadata.file_type().is_socket())
    })
}

/// Whether `name` names an executable regular file on `PATH`.
#[cfg(target_os = "linux")]
fn find_on_path(name: &str) -> bool {
    use std::os::unix::fs::PermissionsExt as _;

    let Some(path) = std::env::var_os("PATH") else {
        return false;
    };
    std::env::split_paths(&path).any(|dir| {
        let candidate = dir.join(name);
        candidate.is_file()
            && candidate
                .metadata()
                .is_ok_and(|metadata| metadata.permissions().mode() & 0o111 != 0)
    })
}

/// The scope wrapper: `systemd-run` starts the scope and exits, so the app's
/// pid is read from the wrapper's stdout. The wrapper echoes its own pid and
/// execs the app, which keeps the pid; what the daemon opens the pidfd on is
/// the app itself.
#[cfg(target_os = "linux")]
pub(crate) fn systemd_run_command(path: &Path) -> tokio::process::Command {
    let mut command = tokio::process::Command::new("systemd-run");
    command.args([
        "--user",
        "--scope",
        "--",
        "/bin/sh",
        "-c",
        "echo $$; exec \"$0\" \"$@\"",
        &path.display().to_string(),
    ]);
    command
}

/// A clean launch environment still carries everything except the loader
/// overrides: dropping `LD_PRELOAD`, `LD_LIBRARY_PATH` and `LD_AUDIT` keeps
/// a same-user process from injecting code into the app, while keeping `HOME`,
/// `DISPLAY`, `XDG_RUNTIME_DIR` and the rest the app needs to run. A fully
/// cleared environment would break the app the same way it once broke
/// supervised modules (which lost `HOME` and `XDG_RUNTIME_DIR` under an
/// `env_clear` until the daemon stopped clearing).
#[cfg(target_os = "linux")]
fn scrub_app_environment(command: &mut std::process::Command) {
    for key in ["LD_PRELOAD", "LD_LIBRARY_PATH", "LD_AUDIT"] {
        command.env_remove(key);
    }
}

#[cfg(target_os = "linux")]
impl ControlHandler {
    /// Launch the installed Linux app and record its entry. Under a user
    /// manager the app runs in its own transient scope; without one it gets
    /// its own process group. The entry is recorded but never stamped: no
    /// connection from a launched process is ever eligible.
    async fn linux_launch_app(&self, app: &str, path: &Path) -> Result<u32, AppOpenRefusal> {
        let style = if self.operator_app_policy.linux_force_direct {
            LinuxLaunchStyle::Direct
        } else {
            select_linux_launch_style(find_on_path("systemd-run"), user_bus_present())
        };
        match style {
            LinuxLaunchStyle::Direct => self.linux_launch_direct(app, path, &[]),
            LinuxLaunchStyle::Scope => self.linux_launch_scoped(app, path).await,
        }
    }

    /// Start the app as our own child in its own process group, with a clean
    /// environment, and hold its pidfd. The child is never waited on until
    /// its entry retires, so its pid cannot be reused while tracked.
    ///
    /// `extra_env` exists for tests, which poison the loader overrides on the
    /// command to prove the scrub removes them. Production always passes an
    /// empty slice: the command otherwise inherits the daemon environment.
    fn linux_launch_direct(
        &self,
        app: &str,
        path: &Path,
        extra_env: &[(&str, &str)],
    ) -> Result<u32, AppOpenRefusal> {
        use std::os::unix::process::CommandExt as _;

        let mut command = std::process::Command::new(path);
        // Test-only poison rides the command, never the process environment,
        // so parallel tests spawning children cannot observe it.
        for (key, value) in extra_env {
            command.env(key, value);
        }
        // Our own process group, so nothing signaling the daemon's group
        // reaches the app. This does not move the app out of a service
        // cgroup, so a service manager's cleanup can still stop it (a session
        // would need a post-fork `setsid` no safe code here can run). The
        // group plus the pidfd are the containment.
        command.process_group(0);
        scrub_app_environment(&mut command);
        command
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null());
        let mut child = subc_os::child_spawn::spawn(&mut command).map_err(|error| {
            AppOpenRefusal::not_installed(format!(
                "installed file {} could not be started: {error}",
                path.display()
            ))
        })?;
        let pid = child.id();
        let process = subc_os::Process::open(pid).map_err(|error| {
            AppOpenRefusal::not_installed(format!(
                "launched process {pid} cannot be tracked: {error}"
            ))
        })?;
        let Some(process) = process else {
            // Exited before it could be tracked: stop anything left and
            // refuse, rather than report a launch with no entry behind it.
            let _ = child.kill();
            while matches!(child.try_wait(), Ok(None)) {
                std::thread::yield_now();
            }
            return Err(AppOpenRefusal::not_installed(format!(
                "installed file {} exited before it could be tracked",
                path.display()
            )));
        };
        self.launch_table.publish(app, pid, process, Some(child));
        Ok(pid)
    }

    /// Start the app in its own transient user scope and open a pidfd on the
    /// reported pid. A scope child is not ours, so no `Child` is retained and
    /// the user manager reaps it; the pidfd still reports its exit.
    async fn linux_launch_scoped(&self, app: &str, path: &Path) -> Result<u32, AppOpenRefusal> {
        use tokio::io::AsyncBufReadExt as _;

        let mut command = systemd_run_command(path);
        scrub_app_environment_std(&mut command);
        command
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::null())
            .kill_on_drop(true);
        let mut child = subc_os::child_spawn::tokio_spawn(&mut command).map_err(|error| {
            AppOpenRefusal::not_installed(format!("transient scope could not start: {error}"))
        })?;
        let stdout = child.stdout.take().ok_or_else(|| {
            AppOpenRefusal::not_installed("transient scope reported no pid".to_string())
        })?;
        let mut lines = tokio::io::BufReader::new(stdout).lines();
        let pid_line = tokio::time::timeout(std::time::Duration::from_secs(10), lines.next_line())
            .await
            .map_err(|_| {
                AppOpenRefusal::not_installed(
                    "transient scope did not report its pid in time".to_string(),
                )
            })?
            .map_err(|error| {
                AppOpenRefusal::not_installed(format!(
                    "transient scope pid could not be read: {error}"
                ))
            })?
            .ok_or_else(|| {
                AppOpenRefusal::not_installed(
                    "transient scope exited without reporting its pid".to_string(),
                )
            })?;
        let pid: u32 = pid_line.trim().parse().map_err(|_| {
            AppOpenRefusal::not_installed(format!(
                "transient scope reported an unparsable pid: {pid_line:?}"
            ))
        })?;
        // Reap the scope starter: it exits once the scope runs. A timeout here
        // kills the starter, never the scope, and refuses a launch whose
        // bookkeeping did not finish.
        let status = tokio::time::timeout(std::time::Duration::from_secs(10), child.wait())
            .await
            .map_err(|_| {
                AppOpenRefusal::not_installed(
                    "transient scope starter did not exit in time".to_string(),
                )
            })?
            .map_err(|error| {
                AppOpenRefusal::not_installed(format!(
                    "transient scope starter could not be reaped: {error}"
                ))
            })?;
        if !status.success() {
            return Err(AppOpenRefusal::not_installed(format!(
                "transient scope starter exited with {status}"
            )));
        }
        let process = subc_os::Process::open(pid).map_err(|error| {
            AppOpenRefusal::not_installed(format!(
                "scoped process {pid} cannot be tracked: {error}"
            ))
        })?;
        let Some(process) = process else {
            return Err(AppOpenRefusal::not_installed(format!(
                "scoped process {pid} exited before it could be tracked"
            )));
        };
        self.launch_table.publish(app, pid, process, None);
        Ok(pid)
    }
}

/// Mirror of [`scrub_app_environment`] for a Tokio command: the scope starter
/// passes its environment to the app, so the loader overrides are removed
/// from the starter itself.
#[cfg(target_os = "linux")]
fn scrub_app_environment_std(command: &mut tokio::process::Command) {
    for key in ["LD_PRELOAD", "LD_LIBRARY_PATH", "LD_AUDIT"] {
        command.env_remove(key);
    }
}
/// The request passed to the injected Windows check, beside the held
/// capture: the publisher the file's signature must name, the floor its
/// signed version must meet, and the installed path. The check receives the
/// held capture and must read that, never reopen `path`, which is for
/// messages only. Production carries no verifier yet and fails closed.
///
/// Public like `crate::operator_apps::TestOperatorAppPolicy`: it crosses
/// the test-support boundary in `with_test_windows_verifier`.
#[allow(dead_code)]
#[derive(Clone, Debug)]
pub struct WindowsVerifyRequest {
    pub app: String,
    pub publisher: Option<String>,
    pub floor: Version,
    pub path: PathBuf,
}

/// The Windows signature check, injected in tests. It answers the verified
/// version string or the reason verification failed; the daemon enforces the
/// floor itself on the returned version, so a below-floor binary is refused
/// even when the checker passes it through. The check runs on the held
/// capture; the test hook may ignore the handle. Production builds carry no
/// checker: with the publisher unset, the production table never reaches
/// verification, so an unverified binary never launches.
#[allow(dead_code)]
#[cfg(windows)]
pub type WindowsVerifyFn = std::sync::Arc<
    dyn Fn(&subc_os::ExecutableCapture, &WindowsVerifyRequest) -> Result<String, String>
        + Send
        + Sync,
>;

/// A test-only fault hook for opening the tracking handle on a launched pid.
/// The daemon stops a launch it cannot track; the hook makes that open fail
/// so a test can prove nothing is left running.
#[allow(dead_code)]
#[cfg(windows)]
pub type WindowsTrackFn =
    std::sync::Arc<dyn Fn(u32) -> std::io::Result<Option<subc_os::Process>> + Send + Sync>;

/// The already-running attention request, injected in tests. The default only
/// logs: starting nothing and relaying nothing is the guarantee, and the
/// flash itself is best effort.
#[allow(dead_code)]
#[cfg(windows)]
pub type WindowsFlashFn = std::sync::Arc<dyn Fn(u32) + Send + Sync>;

/// What the Windows launch puts in `creation_flags`: suspended so the child
/// cannot run before it is bound, without a console window, and breaking
/// away so it never joins the daemon's root job (`winbase.h`).
/// Tests can omit the breakaway bit where the sandbox job forbids it: a test
/// handler creates no job, so a plain spawn is already outside one,
/// while production always sets it and fails closed when the job forbids it.
#[cfg(windows)]
const CREATE_BREAKAWAY_FROM_JOB: u32 = 0x0100_0000;

#[cfg(windows)]
pub(crate) fn windows_creation_flags(breakaway: bool) -> u32 {
    let mut flags = subc_jobobject::CONTAINMENT_CREATION_FLAGS;
    if breakaway {
        flags |= CREATE_BREAKAWAY_FROM_JOB;
    }
    flags
}

#[cfg(windows)]
impl ControlHandler {
    /// Call the optional attention hook for an already-running app. Only
    /// Windows has one, and production only logs; Linux does nothing, and
    /// macOS never reaches `already_running`.
    fn flash_running_app(&self, pid: u32) {
        #[cfg(windows)]
        if let Some(flash) = self.windows_flash.as_ref() {
            flash(pid);
            return;
        }
        tracing::debug!(
            pid,
            "app already running; no window-flash primitive in this build"
        );
    }

    /// Verify the held file's signature and floor. With no checker configured
    /// verification fails closed: an unverified binary never launches, so a
    /// substitution, a wrong publisher, an unsigned binary and a below-floor
    /// binary are all refused with `app_verification_failed` and stamp
    /// nothing. The check runs on the held capture; production carries no
    /// checker and fails closed here.
    fn verify_windows_image(
        &self,
        capture: &subc_os::ExecutableCapture,
        request: WindowsVerifyRequest,
    ) -> Result<String, AppOpenRefusal> {
        #[cfg(windows)]
        if let Some(verify) = self.windows_verify.as_ref() {
            let version = verify(capture, &request).map_err(AppOpenRefusal::verification_failed)?;
            if !crate::operator_apps::meets_release_floor(Some(&version), &request.floor) {
                return Err(AppOpenRefusal::verification_failed(format!(
                    "verified version {version} is below the release floor"
                )));
            }
            return Ok(version);
        }
        Err(AppOpenRefusal::verification_failed(format!(
            "this build carries no Authenticode verifier for {}",
            request.app
        )))
    }

    /// Open a tracking handle on the launched pid. Tests can fail this step
    /// through `with_test_windows_track` to prove a launch that cannot be
    /// tracked is stopped, never left running.
    fn track_launched_process(&self, pid: u32) -> std::io::Result<Option<subc_os::Process>> {
        #[cfg(windows)]
        if let Some(track) = self.windows_track.as_ref() {
            return track(pid);
        }
        subc_os::Process::open(pid)
    }

    /// Verify, launch suspended outside the daemon's root job, compare the image
    /// identity, and resume or refuse. Every failure after the child exists
    /// stops it and refuses, and a failure before that refuses with no child
    /// to stop: no process is ever left running unverified, and
    /// a substitution of the path or a parent directory between verification
    /// and process creation fails the image comparison and stamps nothing.
    async fn windows_launch_app(&self, app: &str, path: &Path) -> Result<u32, AppOpenRefusal> {
        use std::os::windows::process::CommandExt as _;

        // Pin the installed file with read sharing only: no write or delete
        // sharing, so the name cannot be renamed or replaced while held. The
        // canonicalization plus double open inside rejects a swapped leaf
        // before anything runs.
        let capture = subc_os::ExecutableCapture::open(path).map_err(|error| {
            AppOpenRefusal::not_installed(format!(
                "installed file {} cannot be opened: {error}",
                path.display()
            ))
        })?;
        let request = WindowsVerifyRequest {
            app: app.to_owned(),
            publisher: self
                .operator_app_policy
                .windows_publisher()
                .map(str::to_owned),
            floor: self.operator_app_policy.release_floor().clone(),
            path: path.to_path_buf(),
        };
        // The signature and floor are checked on the held capture, before
        // any process exists.
        let _verified_version = self.verify_windows_image(&capture, request)?;
        let mut command = std::process::Command::new(capture.path());
        command
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null());
        // `creation_flags` replaces the whole mask; the composition lives
        // in `windows_creation_flags` so tests can pin every bit.
        command.creation_flags(windows_creation_flags(
            self.operator_app_policy.windows_breakaway,
        ));
        // `app.open` sends no launch arguments: the wire carries only the app id.
        #[cfg(test)]
        self.windows_spawn_attempts.fetch_add(1, Ordering::Relaxed);
        let mut child = subc_os::child_spawn::spawn(&mut command).map_err(|error| {
            AppOpenRefusal::verification_failed(format!(
                "installed file {} could not be started: {error}",
                path.display()
            ))
        })?;
        let pid = child.id();
        // Bind the spawn handle while the child is still suspended: the file
        // stays pinned across process creation, and the capture's identity is
        // tied to the process handle returned by spawn, never a reopened pid.
        let image = match capture.bind(&child) {
            Ok(image) => image,
            Err(error) => {
                stop_rejected_child(&mut child);
                return Err(AppOpenRefusal::verification_failed(format!(
                    "launched process {pid} could not be bound to its image: {error}"
                )));
            }
        };
        // Compare the new process's image with the held handle. The disk
        // identity must still name the verified file object, checked while
        // the child is suspended and pinned to its start time on both sides,
        // so a pid reused after exit can never pass. A mismatch stops the
        // child and refuses: the replacement is never resumed and never
        // stamped.
        if !matches!(
            image.agreement(image.start_time()),
            subc_os::ImageAgreement::Match(_)
        ) {
            stop_rejected_child(&mut child);
            return Err(AppOpenRefusal::verification_failed(format!(
                "launched process {pid} does not match its verified image"
            )));
        }
        let process = match self.track_launched_process(pid) {
            Ok(Some(process)) => process,
            Ok(None) => {
                stop_rejected_child(&mut child);
                return Err(AppOpenRefusal::verification_failed(format!(
                    "launched process {pid} exited before it could be tracked"
                )));
            }
            Err(error) => {
                stop_rejected_child(&mut child);
                return Err(AppOpenRefusal::verification_failed(format!(
                    "launched process {pid} cannot be tracked: {error}"
                )));
            }
        };
        // Published before the image runs, so a child that connects at once
        // is matched against this entry.
        self.launch_table.publish(app, pid, process, Some(child));
        // Resume the verified child. A child that cannot resume is stopped and
        // its published entry retired, so it never matches: nothing unverified
        // ever runs.
        if let Err(error) = subc_jobobject::resume_main_thread(pid) {
            if let Some(entry) = self.launch_table.take(app) {
                entry.stop_rejected();
            }
            return Err(AppOpenRefusal::verification_failed(format!(
                "launched process {pid} could not be resumed and was stopped: {error}"
            )));
        }
        Ok(pid)
    }

    /// A pipe client is eligible only when its pid is that of a live launch
    /// entry. The stamp records the entry's incarnation at connection time.
    pub(crate) fn stamp_windows_launch(
        &self,
        peer: &subc_os::local_peer::CapturedPeer,
    ) -> Option<crate::operator_apps::OperatorAppStamp> {
        if !self.operator_app_policy.windows_enabled {
            return None;
        }
        self.config_windows_launch_identity(peer)
    }

    /// Review identity is independent of the shortcut-delivery switch. The
    /// retained verified launch incarnation must still match the pipe client.
    #[cfg(windows)]
    pub(crate) fn config_windows_launch_identity(
        &self,
        peer: &subc_os::local_peer::CapturedPeer,
    ) -> Option<crate::operator_apps::OperatorAppStamp> {
        use subc_os::local_peer::ObservedPeer as OsPeer;
        let app = self.operator_app_policy.app_id()?;
        let OsPeer::Observed(observation) = peer.observation() else {
            return None;
        };
        let incarnation = self.launch_table.match_live_pid(app, observation.pid)?;
        Some(crate::operator_apps::OperatorAppStamp::launch_stamp(
            subc_protocol::peer::OperatorApp::new(app, observation.pid),
            peer.clone(),
            incarnation,
        ))
    }
}

/// Stop a rejected Windows child synchronously: a suspended or freshly bound
/// child that failed verification must not run, and relying on drop to reap
/// it would report a failure while the process still exists.
#[cfg(windows)]
fn stop_rejected_child(child: &mut std::process::Child) {
    let _ = child.kill();
    while matches!(child.try_wait(), Ok(None)) {
        std::thread::yield_now();
    }
}

#[cfg(test)]
mod tests;