running-process-platform-internal 4.10.16

Blessed platform process operations for running-process (implementation detail)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
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
//! Windows implementation root for the process capability.
#[cfg(feature = "independent-spawn")]
#[path = "independent_broker_unsupported.rs"]
mod independent_broker;
#[cfg(feature = "independent-spawn")]
pub use independent_broker::{run as independent_broker_run, spawn as independent_broker_spawn};
#[cfg(feature = "independent-spawn")]
mod independent_spawn;
#[cfg(feature = "independent-spawn")]
mod scheduler_error;
#[cfg(feature = "independent-spawn")]
mod scheduler_launch;
#[cfg(feature = "independent-spawn")]
pub use independent_spawn::{spawn as independent_spawn, IndependentChild};
#[cfg(feature = "independent-spawn")]
mod independent_io;
#[cfg(feature = "independent-spawn")]
pub(crate) use independent_io::open_regular as independent_open_regular;
#[cfg(feature = "independent-spawn")]
pub(crate) const INDEPENDENT_ZERO_WRITE_PENDING: bool = true;

#[path = "platform_win/foreground.rs"]
pub(crate) mod foreground;

/// Native niceness for the portable `ProcessPriority::Low` intent.
pub(crate) const PRIORITY_NICE_LOW: i32 = 1;
/// Native niceness for the portable `ProcessPriority::High` intent.
pub(crate) const PRIORITY_NICE_HIGH: i32 = -15;

#[path = "platform_win/autostart.rs"]
pub(crate) mod autostart;

#[path = "platform_win/resources.rs"]
pub(crate) mod resources;
pub use resources::{
    fd_exhaustion_error as resources_fd_exhaustion_error,
    inode_capacity as resources_inode_capacity,
    signals_fd_exhaustion as resources_signals_fd_exhaustion,
    signals_storage_exhaustion as resources_signals_storage_exhaustion,
    storage_exhaustion_error as resources_storage_exhaustion_error,
};

pub use autostart::{
    register as autostart_register,
    render_registration as autostart_render_registration,
    unregister as autostart_unregister,
};

#[path = "platform_win/process_inspect.rs"]
pub(crate) mod process_inspect;
pub use process_inspect::{
    process_executable_path, process_force_kill, process_same_executable_path,
    process_fault_code_name, process_signal_terminate, ProcessLiveness,
};

#[path = "platform_win/loaded_images.rs"]
mod loaded_images;
pub use loaded_images::{
    loaded_images as process_loaded_images,
    open_loaded_image_file as process_open_loaded_image_file,
};

#[path = "platform_win/raw_write.rs"]
pub(crate) mod raw_write;
pub use raw_write::write_all_to_descriptor as fs_write_all_to_descriptor;

/// Whether a handle another process holds open keeps a file from being removed.
///
/// Windows refuses to remove a file another process holds open, so
/// callers use the answer to decide whether releasing handles before a
/// recursive delete means anything here.
pub const fn fs_open_handles_block_removal() -> bool {
    true
}

#[path = "platform_win/shutdown_request.rs"]
pub(crate) mod shutdown_request;
pub use shutdown_request::install_shutdown_request_handler as process_install_shutdown_request_handler;

#[path = "platform_win/process_owner_death.rs"]
pub(crate) mod process_owner_death;
pub use process_owner_death::{
    install_owner_death_cleanup as process_install_owner_death_cleanup,
    owner_death_cleanup_target as process_owner_death_cleanup_target,
};

#[path = "platform_win/host.rs"]
pub(crate) mod host;
pub use host::{
    boot_id as host_boot_id, current_process_privilege as host_current_process_privilege,
    environment_keys_are_case_insensitive as host_environment_keys_are_case_insensitive,
    filesystem_device_id as host_filesystem_device_id, hostname as host_hostname,
    login_environment as host_login_environment, machine_id as host_machine_id,
    namespace_id as host_namespace_id, user_machine_identity as host_user_machine_identity,
    PrivilegedIdentity as HostPrivilegedIdentity,
};
pub use host::login_environment_block as host_login_environment_block;

/// Windows has no control groups.
pub fn host_process_cgroup() -> Option<io::Result<String>> {
    None
}

#[cfg(feature = "fs")]
#[path = "platform_win/fs.rs"]
pub(crate) mod fs;
#[cfg(feature = "fs")]
pub use fs::{
    create_private_file as fs_create_private_file,
    is_link_handle as fs_is_link_handle, open_read_no_follow as fs_open_read_no_follow,
    decode_path_bytes as fs_decode_path_bytes,
    replace_file as fs_replace_file, sync_directory as fs_sync_directory,
    user_config_dir as fs_user_config_dir,
    user_data_dir as fs_user_data_dir, encode_path_bytes as fs_encode_path_bytes,
    file_identity as fs_file_identity, is_lock_conflict as fs_is_lock_conflict,
    open_lock_file as fs_open_lock_file, path_identity as fs_path_identity,
    try_lock_exclusive as fs_try_lock_exclusive, unlock as fs_unlock,
    user_run_data_root as fs_user_run_data_root, user_runtime_dir as fs_user_runtime_dir,
    user_state_dir as fs_user_state_dir,
    user_state_dir_from_environment as fs_user_state_dir_from_environment,
    state_home_from_environment as fs_state_home_from_environment,
    FileIdentity as FsFileIdentity,
};

#[path = "platform_win/ape.rs"]
pub(crate) mod ape;
pub use ape::{
    default_loader_dirs as ape_default_loader_dirs, is_exec_format_error as ape_is_exec_format_error,
    is_executable as ape_is_executable, mark_executable as ape_mark_executable,
    anonymous_executable as ape_anonymous_executable,
    private_exec_dir as ape_private_exec_dir,
    route_through_execvp as ape_route_through_execvp, APE_LOADER_HOST,
    APE_EXECVP_SHELL_FALLBACK, APE_NEEDS_LOADER, APE_SHELL, APE_SYSTEM_LOADERS,
};
#[cfg(feature = "async-process")]
pub use ape::route_tokio_through_execvp as ape_route_tokio_through_execvp;

#[path = "platform_win/executable.rs"]
pub(crate) mod executable;
pub use executable::{
    file_name as executable_file_name,
    sibling_of_current_image as executable_sibling_of_current_image,
    EXECUTABLE_EXTENSION,
};

#[cfg(feature = "ipc")]
#[path = "platform_win/ipc.rs"]
pub(crate) mod ipc;
#[cfg(feature = "private-dir")]
#[path = "platform_win/ipc_private_dir.rs"]
mod ipc_private_dir;
#[cfg(feature = "ipc")]
pub use ipc::{
    current_user_id as ipc_current_user_id, Endpoint as IpcEndpoint,
    endpoint_is_filesystem_backed as ipc_endpoint_is_filesystem_backed,
    handoff_transport_available as ipc_handoff_transport_available,
    nonblocking_zero_read_is_pending as ipc_nonblocking_zero_read_is_pending,
    select_endpoint_address as ipc_select_endpoint_address,
    InheritedListener as IpcInheritedListener, Listener as IpcListener,
    ListenerNonblockingMode as IpcListenerNonblockingMode, PeerIdentity as IpcPeerIdentity,
    PeerIdentitySource as IpcPeerIdentitySource, Stream as IpcStream,
};
#[cfg(feature = "ipc")]
pub const LEGACY_SCM_RIGHTS_TRANSPORT_SUPPORTED: bool = false;
#[cfg(feature = "ipc")]
pub const LEGACY_DUPLICATE_HANDLE_TRANSPORT_SUPPORTED: bool = true;
#[cfg(feature = "ipc")]
pub use ipc::legacy_duplicate_handle;
#[cfg(feature = "ipc")]
pub fn legacy_send_fd_to(
    _socket: &std::path::Path,
    _sent_fd: i32,
    _payload: &[u8],
) -> Result<(), crate::LegacyHandoffError> {
    Err(crate::LegacyHandoffError::new(
        crate::platform::ipc::HandoffTransferErrorKind::Unsupported,
        None,
    ))
}
#[cfg(feature = "ipc")]
pub fn legacy_send_fd_over(
    _socket_fd: i32,
    _sent_fd: i32,
    _payload: &[u8],
) -> Result<(), crate::LegacyHandoffError> {
    Err(crate::LegacyHandoffError::new(
        crate::platform::ipc::HandoffTransferErrorKind::Unsupported,
        None,
    ))
}
#[cfg(feature = "private-dir")]
pub use ipc_private_dir::{
    ensure_owner_private_directory as private_dir_ensure_owner_private_directory,
    owner_private_directory as private_dir_owner_private_directory,
};
#[cfg(feature = "ipc")]
pub fn ipc_broker_endpoint_name(bare_name: &str, _path_scoped: bool) -> std::io::Result<String> {
    Ok(ipc_component_endpoint_path("broker-v2", bare_name))
}

/// Named-pipe path for `bare_name`. The kernel pipe namespace is already
/// per-machine and name-keyed, so `component` needs no directory of its own;
/// callers keep their services apart with the name prefix (`rpp-probe-...`).
#[cfg(feature = "ipc")]
pub fn ipc_component_endpoint_path(_component: &str, bare_name: &str) -> String {
    format!(r"\\.\pipe\{bare_name}")
}

/// Per-user runtime directory of `component`, for runtime files a service
/// publishes (its pipes need none). `LOCALAPPDATA` is per-user and
/// non-roaming; the per-user temp directory stands in when it is unset. Pure.
#[cfg(feature = "ipc")]
pub fn ipc_component_runtime_dir(component: &str) -> std::path::PathBuf {
    component_runtime_dir_in(
        crate::env_vars::LOCALAPPDATA.os(),
        std::env::temp_dir(),
        component,
    )
}

#[cfg(feature = "ipc")]
fn component_runtime_dir_in(
    local_app_data: Option<std::ffi::OsString>,
    temp_dir: std::path::PathBuf,
    component: &str,
) -> std::path::PathBuf {
    local_app_data
        .map(std::path::PathBuf::from)
        .unwrap_or(temp_dir)
        .join("running-process")
        .join(component)
}

/// Windows named-pipe names are capped by `MAX_PATH` while the long-path
/// prefix is not in use.
#[cfg(feature = "ipc")]
const WINDOWS_MAX_PATH: usize = 260;

#[cfg(feature = "ipc")]
pub fn ipc_endpoint_name_limit() -> crate::platform::ipc::EndpointNameLimit {
    crate::platform::ipc::EndpointNameLimit {
        max_bytes: WINDOWS_MAX_PATH,
        label: "Windows MAX_PATH",
    }
}

#[cfg(feature = "ipc")]
pub fn ipc_broker_v1_endpoint_path(
    bare_name: &str,
) -> Result<String, crate::platform::ipc::EndpointNameTooLong> {
    let path = format!(r"\\.\pipe\{bare_name}");
    if path.len() > WINDOWS_MAX_PATH {
        return Err(crate::platform::ipc::EndpointNameTooLong {
            len: path.len(),
            max: WINDOWS_MAX_PATH,
            limit_label: "Windows MAX_PATH",
        });
    }
    Ok(path)
}

#[cfg(feature = "ipc")]
pub fn ipc_endpoint_scope_bytes(path: &std::path::Path) -> Vec<u8> {
    // Windows paths and named pipes are case-insensitive. Hash one
    // slash/case-normalized spelling so callers cannot split the broker
    // merely by varying path presentation.
    path.to_string_lossy()
        .replace('\\', "/")
        .to_lowercase()
        .into_bytes()
}

#[cfg(feature = "ipc")]
pub fn ipc_broker_v2_runtime_dir() -> std::path::PathBuf {
    // Named pipes have no directory, so this is chosen rather than derived.
    // `data_local_dir` is per-user and non-roaming, which is what a
    // machine-local endpoint file wants -- a roaming profile would carry a
    // port from another machine.
    dirs::data_local_dir()
        .map(|dir| dir.join("running-process").join("broker-v2"))
        .unwrap_or_else(crate::platform::ipc::per_user_runtime_fallback)
}
#[cfg(feature = "ipc")]
pub fn into_legacy_ipc_stream(stream: IpcStream) -> interprocess::local_socket::Stream {
    stream.0
}

#[cfg(feature = "ipc")]
pub fn from_legacy_ipc_stream(stream: interprocess::local_socket::Stream) -> IpcStream {
    ipc::Stream(stream)
}
#[cfg(feature = "ipc")]
pub fn legacy_ipc_name(path: &str) -> Result<interprocess::local_socket::Name<'_>, String> {
    ipc::legacy_name(path)
}
#[cfg(feature = "ipc-async")]
pub use ipc::{
    AsyncListener as IpcAsyncListener, AsyncStream as IpcAsyncStream,
    IntoAsyncListener as IpcIntoAsyncListener, IntoAsyncStream as IpcIntoAsyncStream,
};

#[cfg(feature = "session-relay")]
#[path = "platform_win_session_relay.rs"]
mod session_relay;
#[cfg(feature = "session-relay")]
pub use session_relay::relay_local_socket_session;

#[path = "platform_win/console.rs"]
mod console;
pub use console::monitor_console_windows;

#[cfg(feature = "pty")]
#[path = "platform_win/terminal.rs"]
pub mod terminal;
#[cfg(feature = "terminal-graphics")]
#[path = "platform_win/terminal_graphics.rs"]
mod terminal_graphics;
#[cfg(feature = "terminal-graphics")]
pub use terminal_graphics::active_graphics_probe;

#[path = "platform_win/terminal_input.rs"]
pub mod terminal_input;

#[cfg(feature = "window-icon")]
#[path = "platform_win/window_icon.rs"]
mod window_icon;
#[cfg(feature = "window-icon")]
pub use window_icon::{icon_support as window_icon_support_impl, set_icon as set_window_icon_impl};

#[path = "platform_win_descendants.rs"]
mod descendants;
pub use descendants::{assign_child_to_windows_job, WindowsJobHandle};

/// Attach to an already-running root by polling the process table (#1015).
pub use descendants::start_snapshot_descendant_monitor as start_attached_descendant_monitor;

pub fn exact_trace_capability() -> crate::platform::process::ExactTraceCapability {
    crate::platform::process::ExactTraceCapability {
        available: false,
        backend: "windows-debug-process",
        reason: "the exact DEBUG_PROCESS supervisor is not available in this build",
        non_invasive_backend: "job-object-iocp",
        non_invasive_grade:
            crate::platform::process::NonInvasiveObservationGrade::KernelNotification,
    }
}

pub fn current_executable_build_id() -> Option<Vec<u8>> {
    None
}

pub struct TracedChild(std::process::Child);

impl TracedChild {
    pub fn id(&self) -> u32 {
        self.0.id()
    }

    pub fn try_wait_code(&mut self) -> std::io::Result<Option<i32>> {
        self.0.try_wait().map(|status| status.map(exit_code))
    }

    pub fn kill(&mut self) -> std::io::Result<()> {
        self.0.kill()
    }

    pub fn take_stdin(&mut self) -> Option<std::process::ChildStdin> {
        self.0.stdin.take()
    }

    pub fn take_stdout(&mut self) -> Option<std::process::ChildStdout> {
        self.0.stdout.take()
    }

    pub fn take_stderr(&mut self) -> Option<std::process::ChildStderr> {
        self.0.stderr.take()
    }

    pub fn wait_code(&mut self) -> std::io::Result<i32> {
        self.0.wait().map(exit_code)
    }
}

pub fn configure_exact_trace(_command: &mut std::process::Command) -> std::io::Result<()> {
    Err(std::io::Error::new(
        std::io::ErrorKind::Unsupported,
        exact_trace_capability().reason,
    ))
}

pub fn start_exact_trace(
    _command: std::process::Command,
    _emit: Box<dyn Fn(crate::platform::process::ExactTraceEvent) + Send>,
    _complete: Box<dyn FnOnce() + Send>,
) -> std::io::Result<TracedChild> {
    Err(std::io::Error::new(
        std::io::ErrorKind::Unsupported,
        exact_trace_capability().reason,
    ))
}

pub fn shell_command(command: &str) -> std::process::Command {
    let mut shell = std::process::Command::new("cmd.exe");
    shell.args(["/D", "/S", "/C", command]);
    shell
}

pub fn compat_shell_command(command: &str) -> std::process::Command {
    use std::os::windows::process::CommandExt;

    let mut shell = std::process::Command::new("cmd");
    shell.raw_arg("/D /S /C \"");
    shell.raw_arg(command);
    shell.raw_arg("\"");
    shell
}

pub fn canonical_environment_pairs(pairs: Vec<(String, String)>) -> Vec<(String, String)> {
    let mut seen = std::collections::BTreeMap::new();
    for (key, value) in pairs {
        seen.insert(key.to_ascii_uppercase(), (key, value));
    }
    seen.into_values().collect()
}

/// Windows reports descendants through its Job Object IOCP path during spawn.
pub fn start_descendant_monitor(
    _root_pid: u32,
    _stop: std::sync::Arc<crate::platform::process::DescendantMonitorStop>,
    _emit: Box<dyn Fn(crate::platform::process::DescendantEvent) + Send>,
) -> std::io::Result<()> {
    Ok(())
}

#[cfg(feature = "async-process")]
use std::ffi::OsStr;
use std::io;
use std::io::Read;
use std::os::windows::io::AsRawHandle;
use std::sync::Mutex;

#[cfg(feature = "async-process")]
use tokio::process::{Child, Command};
// Each import carries the gate its users carry, so a build without
// `async-process` does not import symbols nothing references. Only
// `ERROR_INVALID_HANDLE` and the console pair have ungated users
// (`soft_terminate_process_group`); everything else here is reached solely
// from the async spawn/identity paths. `process_start_key` looks like a
// second consumer of `CloseHandle` / `FILETIME` / `GetProcessTimes` but
// imports all three itself, function-locally.
use windows_sys::Win32::Foundation::ERROR_INVALID_HANDLE;
#[cfg(feature = "async-process")]
use windows_sys::Win32::Foundation::{
    CloseHandle, DuplicateHandle, DUPLICATE_SAME_ACCESS, ERROR_INVALID_PARAMETER, FILETIME, HANDLE,
};
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_BREAK_EVENT};
#[cfg(feature = "async-process")]
use windows_sys::Win32::System::JobObjects::{
    AssignProcessToJobObject, CreateJobObjectW, JobObjectExtendedLimitInformation,
    SetInformationJobObject, JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
};
#[cfg(feature = "async-process")]
use windows_sys::Win32::System::Threading::{
    GetCurrentProcess, GetExitCodeProcess, GetProcessTimes, TerminateProcess,
};

#[cfg(feature = "async-process")]
use crate::SpawnSpec;

#[derive(Default)]
pub struct CaptureCancellation { handles: Mutex<CaptureHandles> }
#[derive(Default)]
struct CaptureHandles { stdout: Option<usize>, stderr: Option<usize> }
pub fn prepare_capture_reader<R>(reader: R, cancellation: &CaptureCancellation, stream: crate::platform::process::CaptureStream) -> io::Result<Box<dyn Read + Send>>
where R: Read + AsRawHandle + Send + 'static {
    let mut handles = cancellation.handles.lock().expect("capture pipe handles mutex poisoned");
    match stream { crate::platform::process::CaptureStream::Stdout => handles.stdout = Some(reader.as_raw_handle() as usize), crate::platform::process::CaptureStream::Stderr => handles.stderr = Some(reader.as_raw_handle() as usize) }
    Ok(Box::new(reader))
}
pub fn capture_reader_done(cancellation: &CaptureCancellation, stream: crate::platform::process::CaptureStream) {
    let mut handles = cancellation.handles.lock().expect("capture pipe handles mutex poisoned");
    match stream { crate::platform::process::CaptureStream::Stdout => handles.stdout = None, crate::platform::process::CaptureStream::Stderr => handles.stderr = None }
}
pub fn cancel_capture_reader(cancellation: &CaptureCancellation) {
    use winapi::shared::ntdef::HANDLE;
    use winapi::um::ioapiset::CancelIoEx;
    let handles = cancellation.handles.lock().expect("capture pipe handles mutex poisoned");
    for handle in [handles.stdout, handles.stderr].into_iter().flatten() {
        // SAFETY: the slot remains populated until its reader completion callback runs.
        unsafe { CancelIoEx(handle as HANDLE, std::ptr::null_mut()); }
    }
}

#[cfg(feature = "async-process")]
#[path = "platform_win_output.rs"]
mod output_shutdown;
#[cfg(feature = "async-process")]
pub(crate) use output_shutdown::shutdown_output_reader;

#[path = "platform_win_file_handles.rs"]
mod file_handles;
pub use file_handles::read_process_file_handles;
#[path = "platform_win_cmdline.rs"]
mod cmdline;
pub use cmdline::{read_process_argv, read_process_cmdline};

#[cfg(feature = "process-inspection")]
#[path = "platform/process_tree.rs"]
mod process_tree;

#[cfg(feature = "process-inspection")]
pub fn kill_tree(pid: u32, timeout: std::time::Duration) -> io::Result<u32> {
    process_tree::kill_tree(pid, timeout, process_start_key)
}

pub fn exit_code(status: std::process::ExitStatus) -> i32 {
    status.code().unwrap_or(1)
}

/// The signal that terminated `status`'s process. Windows processes do not
/// die from signals, so there is never one to report.
pub fn exit_signal(_status: &std::process::ExitStatus) -> Option<i32> {
    None
}

pub fn set_process_name(_name: &str) {}

pub fn configure_trampoline_command(command: &mut std::process::Command) {
    use std::os::windows::process::CommandExt;
    command.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
}

pub fn configure_process_command(
    command: &mut std::process::Command,
    config: crate::platform::process::ProcessCommandConfig,
) -> io::Result<()> {
    configure_process_command_inner(command, config, false)
}

/// Root-facade-only launch seam for bounded owner-death containment.
///
/// This must be `pub` because `running-process` is a separate package, but
/// applications should use its semantic bounded-run options instead of this
/// implementation-detail function.
#[doc(hidden)]
pub fn configure_process_command_for_bounded_owner_death(
    command: &mut std::process::Command,
    config: crate::platform::process::ProcessCommandConfig,
) -> io::Result<()> {
    // NativeProcess already owns one per-spawn KILL_ON_JOB_CLOSE job. Do not
    // allocate or assign a second job for the bounded option.
    configure_process_command_inner(command, config, true)
}

fn configure_process_command_inner(
    command: &mut std::process::Command,
    config: crate::platform::process::ProcessCommandConfig,
    _kill_when_owner_dies: bool,
) -> io::Result<()> {
    use std::os::windows::process::CommandExt;
    const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
    const CREATE_NO_WINDOW: u32 = 0x0800_0000;
    const CREATE_NEW_CONSOLE: u32 = 0x0000_0010;
    const DETACHED_PROCESS: u32 = 0x0000_0008;
    let caller = config.creation_flags.unwrap_or(0);
    let group = if config.create_process_group { CREATE_NEW_PROCESS_GROUP } else { 0 };
    let caller_has_console_opinion =
        caller & (CREATE_NO_WINDOW | CREATE_NEW_CONSOLE | DETACHED_PROCESS) != 0;
    let no_window = if caller_has_console_opinion || parent_has_console() { 0 } else { CREATE_NO_WINDOW };
    let priority = match config.nice {
        Some(value) if value >= 15 => 0x0000_0040,
        Some(value) if value >= 1 => 0x0000_4000,
        Some(value) if value <= -15 => 0x0000_0080,
        Some(value) if value <= -1 => 0x0000_8000,
        _ => 0,
    };
    let flags = caller | group | no_window | priority;
    if flags != 0 {
        command.creation_flags(flags);
    }
    Ok(())
}

pub fn trampoline_exit_code(status: std::process::ExitStatus) -> i32 {
    status.code().unwrap_or(1)
}

/// Request a Ctrl+Break event for a child-owned Windows process group.
pub fn soft_terminate_process_group(pid: u32) -> io::Result<()> {
    // SAFETY: the Windows API receives only a numeric process-group id and
    // does not retain Rust pointers or references.
    let ok = unsafe { GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid) };
    if ok == 0 {
        let error = io::Error::last_os_error();
        // A closed or detached console target no longer needs a soft step.
        if error.raw_os_error() != Some(ERROR_INVALID_HANDLE as i32) {
            return Err(error);
        }
    }
    Ok(())
}

pub fn process_snapshot() -> Vec<crate::platform::process::ProcessSnapshot> {
    Vec::new()
}

pub fn process_snapshot_for_pid(_pid: u32) -> Option<crate::platform::process::ProcessSnapshot> {
    None
}

/// Windows compatibility stub for the Unix post-fork descriptor hook.
///
/// # Safety
/// This shares the Unix API contract and may only be called from the spawn
/// layer's post-fork/pre-exec boundary, even though it is a no-op on Windows.
pub unsafe fn unix_mark_extra_fds_close_on_exec() {}

pub fn configure_sync_daemon_command(_command: &mut std::process::Command) -> io::Result<()> { Ok(()) }
pub fn configure_sync_daemon_command_with_inheritance(
    _command: &mut std::process::Command,
    _inheritance: crate::platform::process::DaemonExecInheritance,
) -> io::Result<()> { Ok(()) }

pub fn configure_sync_contained_command(_command: &mut std::process::Command) -> io::Result<()> { Ok(()) }

pub fn parent_has_console() -> bool {
    unsafe { windows_sys::Win32::System::Console::GetConsoleCP() != 0 }
}

pub fn sync_child_native_handle(child: &std::process::Child) -> usize {
    use std::os::windows::io::AsRawHandle;
    child.as_raw_handle() as usize
}
pub fn observer_backend(scope: crate::platform::process::ObserverScope, category: crate::platform::process::ObserverCategory) -> crate::platform::process::ObserverBackend {
    use crate::platform::process::{ObserverBackend as B, ObserverCategory as C, ObserverScope as S, ObserverSupport as P};
    match (scope, category) {
        (S::SystemWide, C::File) | (S::SystemWide, C::Network) | (S::SystemWide, C::Process) => B { support:P::Unavailable, backend:"etw", reason:"Phase 3: Windows ETW backend not yet implemented" },
        (S::LaunchedProcessTree, C::File) => B { support:P::Partial, backend:"nt-handle-snapshot", reason:"Windows NtQuerySystemInformation + DuplicateHandle + NtQueryObject snapshot via read_process_file_handles (#539 slice 4; no streaming file events)" },
        (S::LaunchedProcessTree, C::Network) => B { support:P::Unavailable, backend:"none", reason:"#539: no-admin per-child network backend deferred to a follow-up issue" },
        (S::LaunchedProcessTree, C::Process) => B { support:P::Supported, backend:"job-object-iocp", reason:"Windows Job Object IOCP descendant lifecycle (#539 slice 2)" },
    }
}

/// Apply a process priority expressed as a Unix nice value by mapping it to
/// the nearest Windows priority class.
pub fn apply_process_priority(pid: u32, nice: i32) -> io::Result<()> {
    use windows_sys::Win32::Foundation::CloseHandle;
    use windows_sys::Win32::System::Threading::{
        OpenProcess, SetPriorityClass, ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS,
        HIGH_PRIORITY_CLASS, IDLE_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS,
        PROCESS_QUERY_INFORMATION, PROCESS_SET_INFORMATION,
    };
    let priority_class = if nice >= 15 {
        IDLE_PRIORITY_CLASS
    } else if nice >= 1 {
        BELOW_NORMAL_PRIORITY_CLASS
    } else if nice <= -15 {
        HIGH_PRIORITY_CLASS
    } else if nice <= -1 {
        ABOVE_NORMAL_PRIORITY_CLASS
    } else {
        NORMAL_PRIORITY_CLASS
    };
    // SAFETY: OpenProcess takes plain values; the returned handle is closed
    // below on every path that obtains one.
    let handle = unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION, 0, pid) };
    if handle.is_null() {
        return Err(io::Error::last_os_error());
    }
    // SAFETY: `handle` is a live process handle owned by this function.
    let set_ok = unsafe { SetPriorityClass(handle, priority_class) };
    // SAFETY: `handle` is closed exactly once.
    let close_ok = unsafe { CloseHandle(handle) };
    if close_ok == 0 || set_ok == 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

/// Windows `CREATE_NEW_PROCESS_GROUP`.
const CREATE_NEW_PROCESS_GROUP_FLAG: u32 = 0x0000_0200;

/// Deliver Ctrl+Break to the child-owned console process group `pid`.
///
/// Windows can only target a process group, so the child must have been
/// created with `CREATE_NEW_PROCESS_GROUP`; `create_process_group` is a Unix
/// notion and is ignored here.
pub fn send_interrupt(pid: u32, creationflags: Option<u32>, _create_process_group: bool) -> io::Result<()> {
    if creationflags.unwrap_or(0) & CREATE_NEW_PROCESS_GROUP_FLAG == 0 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "send_interrupt on Windows requires CREATE_NEW_PROCESS_GROUP",
        ));
    }
    // SAFETY: the Windows API receives only a numeric process-group id.
    if unsafe { GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid) } == 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

pub fn unix_set_priority(_pid: u32, _nice: i32) -> io::Result<()> { Err(io::Error::new(io::ErrorKind::Unsupported, "Unix priority is unavailable on Windows")) }
pub fn unix_signal_process(_pid: u32, _signal: crate::platform::process::UnixSignalKind) -> io::Result<()> { Err(io::Error::new(io::ErrorKind::Unsupported, "Unix signals are unavailable on Windows")) }
pub fn unix_signal_process_group(_pid: i32, _signal: crate::platform::process::UnixSignalKind) -> io::Result<()> { Err(io::Error::new(io::ErrorKind::Unsupported, "Unix signals are unavailable on Windows")) }
pub fn unix_signal_raw(_signal: crate::platform::process::UnixSignalKind) -> i32 { 0 }

#[cfg(feature = "async-process")]
pub fn configure_compat_tokio_command(
    command: &mut Command,
    show_console: bool,
    kill_when_owner_dies: bool,
) -> io::Result<()> {
    let flags = compat_tokio_creation_flags(show_console, kill_when_owner_dies);
    if flags != 0 {
        command.creation_flags(flags);
    }
    Ok(())
}

#[cfg(feature = "async-process")]
const CREATE_SUSPENDED: u32 = 0x0000_0004;

/// `CREATE_SUSPENDED` when the child must join the owner-death job before it
/// runs a single instruction (#887). Assigning it after `CreateProcess` returns
/// leaves a window in which the child can start a grandchild that never joins
/// the job.
#[cfg(feature = "async-process")]
fn compat_tokio_creation_flags(show_console: bool, kill_when_owner_dies: bool) -> u32 {
    let console = if show_console {
        0
    } else {
        0x0800_0000 // CREATE_NO_WINDOW
    };
    let suspended = if kill_when_owner_dies { CREATE_SUSPENDED } else { 0 };
    console | suspended
}

/// Put a child spawned `CREATE_SUSPENDED` into the owner-death job, then let it
/// run.
///
/// If containment or the resume fails, the child is terminated: it has not run
/// a single instruction, so killing it is strictly safer than letting it run
/// outside the job.
#[cfg(feature = "async-process")]
fn contain_and_resume(child: &Child) -> io::Result<()> {
    let process = child.raw_handle();
    let outcome = assign(process).and_then(|()| match child.id() {
        Some(pid) => resume_primary_thread(pid),
        None => Err(io::Error::new(
            io::ErrorKind::NotFound,
            "the child exited before it could be resumed",
        )),
    });
    if outcome.is_err() {
        if let Some(process) = process {
            // SAFETY: `process` is the live handle Tokio owns for this child.
            unsafe { TerminateProcess(process, 1) };
        }
    }
    outcome
}

/// Resume the one thread of a process created `CREATE_SUSPENDED`.
///
/// A freshly created suspended process has exactly one thread, so the first
/// thread owned by `pid` is the primary thread. Uses documented Win32 calls
/// only; std and Tokio do not expose the primary-thread handle.
#[cfg(feature = "async-process")]
fn resume_primary_thread(pid: u32) -> io::Result<()> {
    use winapi::um::handleapi::{CloseHandle as CloseWinHandle, INVALID_HANDLE_VALUE};
    use winapi::um::processthreadsapi::{OpenThread, ResumeThread};
    use winapi::um::tlhelp32::{
        CreateToolhelp32Snapshot, Thread32First, Thread32Next, TH32CS_SNAPTHREAD, THREADENTRY32,
    };
    use winapi::um::winnt::THREAD_SUSPEND_RESUME;

    // SAFETY: a plain snapshot call; the handle is closed on every path below.
    let snapshot = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0) };
    if snapshot == INVALID_HANDLE_VALUE {
        return Err(io::Error::last_os_error());
    }
    // SAFETY: THREADENTRY32 is plain data; `dwSize` is set before first use.
    let mut entry: THREADENTRY32 = unsafe { std::mem::zeroed() };
    entry.dwSize = std::mem::size_of::<THREADENTRY32>() as u32;
    let mut outcome = Err(io::Error::new(
        io::ErrorKind::NotFound,
        "no thread found for the suspended child",
    ));
    // SAFETY: `snapshot` is live and `entry` is initialised as required.
    let mut more = unsafe { Thread32First(snapshot, &mut entry) } != 0;
    while more {
        if entry.th32OwnerProcessID == pid {
            // SAFETY: opening a thread by id; the handle is closed right after.
            let thread = unsafe { OpenThread(THREAD_SUSPEND_RESUME, 0, entry.th32ThreadID) };
            if thread.is_null() {
                outcome = Err(io::Error::last_os_error());
            } else {
                // SAFETY: `thread` is a live handle with suspend/resume access.
                let previous = unsafe { ResumeThread(thread) };
                outcome = if previous == u32::MAX {
                    Err(io::Error::last_os_error())
                } else {
                    Ok(())
                };
                // SAFETY: closes the handle opened above.
                unsafe { CloseWinHandle(thread) };
            }
            break;
        }
        // SAFETY: as for `Thread32First`.
        more = unsafe { Thread32Next(snapshot, &mut entry) } != 0;
    }
    // SAFETY: closes the snapshot created above.
    unsafe { CloseWinHandle(snapshot) };
    outcome
}

#[cfg(feature = "async-process")]
pub fn after_compat_tokio_spawn(child: &Child, kill_when_owner_dies: bool) -> io::Result<()> {
    if kill_when_owner_dies {
        contain_and_resume(child)
    } else {
        Ok(())
    }
}

#[cfg(feature = "process-inspection")]
fn process_start_key(pid: sysinfo::Pid, _process: &sysinfo::Process) -> io::Result<u64> {
    use windows_sys::Win32::Foundation::{CloseHandle, FILETIME};
    use windows_sys::Win32::System::Threading::{
        GetProcessTimes, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION,
    };

    let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid.as_u32()) };
    if handle.is_null() {
        return Err(io::Error::last_os_error());
    }
    let mut creation: FILETIME = unsafe { std::mem::zeroed() };
    let mut exit: FILETIME = unsafe { std::mem::zeroed() };
    let mut kernel: FILETIME = unsafe { std::mem::zeroed() };
    let mut user: FILETIME = unsafe { std::mem::zeroed() };
    let queried = unsafe { GetProcessTimes(handle, &mut creation, &mut exit, &mut kernel, &mut user) };
    let query_error = if queried == 0 { Some(io::Error::last_os_error()) } else { None };
    unsafe { CloseHandle(handle); }
    if let Some(error) = query_error {
        return Err(error);
    }
    Ok((u64::from(creation.dwHighDateTime) << 32) | u64::from(creation.dwLowDateTime))
}

#[cfg(feature = "async-process")]
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
#[cfg(feature = "async-process")]
const CREATE_NO_WINDOW: u32 = 0x0800_0000;

/// A console-less parent (e.g. a daemon) makes Windows give every child its
/// own visible console unless `CREATE_NO_WINDOW` is set.
#[cfg(feature = "async-process")]
fn spawn_creation_flags(
    group: u32,
    priority: u32,
    parent_has_console: bool,
    kill_when_owner_dies: bool,
) -> u32 {
    let no_window = if parent_has_console { 0 } else { CREATE_NO_WINDOW };
    // Owner-death children start suspended and are resumed once they are in the
    // job (#887); see `contain_and_resume`.
    let suspended = if kill_when_owner_dies { CREATE_SUSPENDED } else { 0 };
    group | priority | no_window | suspended
}

/// Configure a caller-built command for [`crate::SpawnSpec::from_std_command`].
///
/// Windows owner-death containment for these commands is the per-spawn
/// kill-on-close Job Object (with its descendant observer and memory limit)
/// that `NativeProcess` assigns after spawn. `SpawnSpec` cannot express that
/// job yet (#850), so the combination is refused rather than launched
/// uncontained.
#[cfg(feature = "async-process")]
pub(crate) fn configure_override_command(
    command: &mut std::process::Command,
    config: crate::platform::process::ProcessCommandConfig,
    kill_when_owner_dies: bool,
) -> io::Result<()> {
    if kill_when_owner_dies {
        return Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "owner-death containment of a caller-built command needs the per-spawn Job Object, \
             which SpawnSpec cannot express yet",
        ));
    }
    configure_process_command(command, config)
}

#[cfg(feature = "async-process")]
pub(crate) fn configure_command(
    command: &mut Command,
    create_process_group: bool,
    kill_when_owner_dies: bool,
    nice: Option<i32>,
) -> io::Result<()> {
    let group = if create_process_group {
        CREATE_NEW_PROCESS_GROUP
    } else {
        0
    };
    // Preserve the existing ProcessCommandConfig mapping: Windows receives a
    // priority *class*, not a Unix nice value with equivalent arithmetic.
    let priority = match nice {
        Some(value) if value >= 15 => 0x0000_0040,
        Some(value) if value >= 1 => 0x0000_4000,
        Some(value) if value <= -15 => 0x0000_0080,
        Some(value) if value <= -1 => 0x0000_8000,
        _ => 0,
    };
    let flags = spawn_creation_flags(group, priority, parent_has_console(), kill_when_owner_dies);
    if flags != 0 {
        command.creation_flags(flags);
    }
    Ok(())
}

#[cfg(feature = "async-process")]
pub(crate) fn after_spawn(
    child: &Child,
    kill_when_owner_dies: bool,
    _nice: Option<i32>,
) -> io::Result<()> {
    if kill_when_owner_dies {
        contain_and_resume(child)
    } else {
        Ok(())
    }
}

#[cfg(feature = "async-process")]
pub(crate) struct AsyncChildIdentity {
    pid: u32,
    process: HANDLE,
    creation_time: u64,
}

#[cfg(feature = "async-process")]
unsafe impl Send for AsyncChildIdentity {}

#[cfg(feature = "async-process")]
unsafe impl Sync for AsyncChildIdentity {}

#[cfg(feature = "async-process")]
impl Drop for AsyncChildIdentity {
    fn drop(&mut self) {
        unsafe { CloseHandle(self.process) };
    }
}

#[cfg(feature = "async-process")]
pub(crate) fn async_child_identity(child: &Child) -> Option<AsyncChildIdentity> {
    let pid = child.id()?;
    let raw = child.raw_handle()? as HANDLE;
    let mut process = std::ptr::null_mut();
    if unsafe {
        DuplicateHandle(
            GetCurrentProcess(),
            raw,
            GetCurrentProcess(),
            &mut process,
            0,
            0,
            DUPLICATE_SAME_ACCESS,
        )
    } == 0
    {
        return None;
    }
    let Ok((creation_time, _, _)) = async_process_times(process) else {
        unsafe { CloseHandle(process) };
        return None;
    };
    Some(AsyncChildIdentity {
        pid,
        process,
        creation_time,
    })
}

#[cfg(feature = "async-process")]
pub(crate) fn signal_async_child(identity: &AsyncChildIdentity) -> io::Result<()> {
    if !identity_matches(identity) {
        return Err(io::Error::new(
            io::ErrorKind::BrokenPipe,
            "child process launch identity no longer matches",
        ));
    }
    if unsafe { TerminateProcess(identity.process, 1) } != 0 {
        return Ok(());
    }
    let error = io::Error::last_os_error();
    if error.raw_os_error() == Some(ERROR_INVALID_PARAMETER as i32) {
        Ok(())
    } else {
        Err(error)
    }
}

#[cfg(feature = "async-process")]
pub(crate) fn signal_async_child_group(identity: &AsyncChildIdentity) -> io::Result<()> {
    if !identity_matches(identity) {
        return Err(io::Error::new(
            io::ErrorKind::BrokenPipe,
            "child process launch identity no longer matches",
        ));
    }
    if unsafe { GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, identity.pid) } != 0 {
        return Ok(());
    }
    let error = io::Error::last_os_error();
    if error.raw_os_error() == Some(ERROR_INVALID_HANDLE as i32) {
        Ok(())
    } else {
        Err(error)
    }
}

#[cfg(feature = "async-process")]
pub(crate) fn async_child_cpu_time(
    identity: &AsyncChildIdentity,
) -> io::Result<Option<std::time::Duration>> {
    let Ok((creation_time, user, kernel)) = async_process_times(identity.process) else {
        return Ok(None);
    };
    if creation_time != identity.creation_time {
        return Ok(None);
    }
    Ok(Some(std::time::Duration::from_nanos(
        user.saturating_add(kernel).saturating_mul(100),
    )))
}

#[cfg(feature = "async-process")]
fn identity_matches(identity: &AsyncChildIdentity) -> bool {
    const STILL_ACTIVE: u32 = 259;
    let mut exit_code = 0_u32;
    if unsafe { GetExitCodeProcess(identity.process, &mut exit_code) } == 0
        || exit_code != STILL_ACTIVE
    {
        return false;
    }
    matches!(
        async_process_times(identity.process),
        Ok((creation_time, _, _)) if creation_time == identity.creation_time
    )
}

#[cfg(feature = "async-process")]
fn async_process_times(process: HANDLE) -> io::Result<(u64, u64, u64)> {
    let mut creation: FILETIME = unsafe { std::mem::zeroed() };
    let mut exit: FILETIME = unsafe { std::mem::zeroed() };
    let mut kernel: FILETIME = unsafe { std::mem::zeroed() };
    let mut user: FILETIME = unsafe { std::mem::zeroed() };
    if unsafe { GetProcessTimes(process, &mut creation, &mut exit, &mut kernel, &mut user) } == 0 {
        return Err(io::Error::last_os_error());
    }
    let ticks = |time: FILETIME| (u64::from(time.dwHighDateTime) << 32) | u64::from(time.dwLowDateTime);
    Ok((ticks(creation), ticks(user), ticks(kernel)))
}

#[cfg(feature = "async-process")]
pub(crate) fn shell_spec(command: &OsStr) -> SpawnSpec {
    SpawnSpec::new("cmd.exe").arg("/C").arg(command)
}

// Only the async-process spawn paths place a child in an owner-death job;
// without that feature these items have no caller and fail `-D dead-code`.
#[cfg(feature = "async-process")]
struct Job(HANDLE);
#[cfg(feature = "async-process")]
unsafe impl Send for Job {}

#[cfg(feature = "async-process")]
impl Drop for Job {
    fn drop(&mut self) {
        // SAFETY: the handle came from `CreateJobObjectW` and is owned here.
        unsafe { CloseHandle(self.0) };
    }
}

/// Owner-death jobs that may still contain a live process.
///
/// Every contained child gets its own empty job. One process-wide job stops
/// accepting children once the owner itself joins another job: Windows assigns
/// a process only to a job that is empty or already in its job chain, so a
/// populated shared job rejects later children with `ERROR_ACCESS_DENIED`
/// (#1207). A job is released only after it has no active processes, because
/// closing a kill-on-close job earlier would terminate what it contains. The
/// remaining handles close when the owner exits, which is the containment.
#[cfg(feature = "async-process")]
static JOBS: Mutex<Vec<Job>> = Mutex::new(Vec::new());

#[cfg(feature = "async-process")]
fn create() -> io::Result<Job> {
    // SAFETY: both arguments may be null: default security and an unnamed job.
    let handle = unsafe { CreateJobObjectW(std::ptr::null(), std::ptr::null()) };
    if handle.is_null() {
        return Err(io::Error::last_os_error());
    }
    let job = Job(handle);
    // SAFETY: an all-zero extended limit structure is valid; only the
    // kill-on-close flag is then set.
    let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { std::mem::zeroed() };
    info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
    // SAFETY: `info` is valid for the length passed, and the class matches it.
    let set = unsafe {
        SetInformationJobObject(
            job.0,
            JobObjectExtendedLimitInformation,
            (&info as *const JOBOBJECT_EXTENDED_LIMIT_INFORMATION).cast(),
            std::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
        )
    };
    if set == 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(job)
}

/// Whether releasing `job` could still terminate a process.
#[cfg(feature = "async-process")]
fn job_may_contain_processes(job: &Job) -> bool {
    use windows_sys::Win32::System::JobObjects::{
        JobObjectBasicAccountingInformation, QueryInformationJobObject,
        JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,
    };

    // SAFETY: an all-zero accounting structure is valid to overwrite.
    let mut info: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = unsafe { std::mem::zeroed() };
    // SAFETY: `info` is valid for the length passed, and the class matches it.
    let queried = unsafe {
        QueryInformationJobObject(
            job.0,
            JobObjectBasicAccountingInformation,
            (&mut info as *mut JOBOBJECT_BASIC_ACCOUNTING_INFORMATION).cast(),
            std::mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,
            std::ptr::null_mut(),
        )
    };
    // A job that cannot be queried is kept rather than risk killing a child.
    queried == 0 || info.ActiveProcesses != 0
}

/// Place a freshly spawned child in its own owner-death job.
///
/// Every step here used to fail silently, which is the wrong shape for this
/// operation: a caller passes `kill_when_owner_dies: true` precisely because
/// it does not want the child to outlive it, and a caller that is told
/// nothing cannot tell containment from its absence. All three failures are
/// now reported.
#[cfg(feature = "async-process")]
fn assign(child: Option<HANDLE>) -> io::Result<()> {
    let Some(child) = child else {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "cannot contain a child that exposes no process handle",
        ));
    };
    let job = create().map_err(|error| {
        io::Error::other(format!("owner-death job object could not be created: {error}"))
    })?;
    // SAFETY: `job.0` is the live job created above and `child` is the handle
    // Tokio/std owns for the child just spawned. On failure the job is closed
    // without containing anything.
    if unsafe { AssignProcessToJobObject(job.0, child) } == 0 {
        return Err(io::Error::last_os_error());
    }
    let mut jobs = JOBS.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
    jobs.retain(job_may_contain_processes);
    jobs.push(job);
    Ok(())
}
#[path = "platform_win/sync_spawn.rs"]
mod sync_spawn;
pub use sync_spawn::{spawn_sync, spawn_sync_daemon, spawn_sync_daemon_with_inheritance};
#[cfg(feature = "independent-spawn")]
pub(crate) use sync_spawn::spawn_sync_owned_daemon;

/// Replace this process's image with `command`.
///
/// Windows has no `execve`, so this always fails. It exists so the facade has
/// one shape on every host; callers check
/// [`can_replace_current_image`](crate::platform::process::can_replace_current_image)
/// first and start a successor instead.
pub fn process_replace_current_image(_command: &mut std::process::Command) -> std::io::Error {
    std::io::Error::new(
        std::io::ErrorKind::Unsupported,
        "this host cannot replace a running process image",
    )
}

/// This host has no `execve`; a caller must start a successor and exit.
pub const fn process_can_replace_current_image() -> bool {
    false
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "async-process")]
    use super::compat_tokio_creation_flags;
    use std::ffi::OsStr;

    #[cfg(feature = "async-process")]
    #[test]
    fn tokio_spawn_owns_console_creation_flags() {
        assert_eq!(compat_tokio_creation_flags(false, false), 0x0800_0000);
        assert_eq!(compat_tokio_creation_flags(true, false), 0);
    }

    #[cfg(feature = "async-process")]
    #[test]
    fn owner_death_children_start_suspended_so_they_join_the_job_before_running() {
        const CREATE_SUSPENDED: u32 = 0x0000_0004;
        assert_eq!(compat_tokio_creation_flags(false, true) & CREATE_SUSPENDED, CREATE_SUSPENDED);
        assert_eq!(compat_tokio_creation_flags(true, true), CREATE_SUSPENDED);
        assert_eq!(compat_tokio_creation_flags(false, false) & CREATE_SUSPENDED, 0);
        assert_eq!(super::spawn_creation_flags(0, 0, true, true), CREATE_SUSPENDED);
        assert_eq!(super::spawn_creation_flags(0, 0, true, false), 0);
    }

    #[cfg(feature = "async-process")]
    #[test]
    fn console_less_parent_spawns_children_without_a_window() {
        use super::{spawn_creation_flags, CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW};

        // #1214: a parent with no console (a daemon) must not let Windows give
        // every child its own visible console.
        assert_eq!(spawn_creation_flags(0, 0, false, false), CREATE_NO_WINDOW);
        assert_eq!(
            spawn_creation_flags(CREATE_NEW_PROCESS_GROUP, 0x0000_0040, false, false),
            CREATE_NEW_PROCESS_GROUP | 0x0000_0040 | CREATE_NO_WINDOW
        );
        assert_eq!(spawn_creation_flags(0, 0, true, false), 0);
    }

    #[cfg(feature = "async-process")]
    fn cmd_child(script: &str, kill_when_owner_dies: bool) -> tokio::process::Command {
        let mut command = tokio::process::Command::new("cmd.exe");
        command.args(["/c", script]).kill_on_drop(true);
        super::configure_compat_tokio_command(&mut command, false, kill_when_owner_dies).unwrap();
        command
    }

    #[cfg(feature = "async-process")]
    fn runtime() -> tokio::runtime::Runtime {
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap()
    }

    #[cfg(feature = "async-process")]
    #[test]
    fn suspended_owner_death_child_is_resumed_and_runs_to_its_own_exit_code() {
        runtime().block_on(async {
            let mut child = cmd_child("exit 7", true).spawn().unwrap();
            super::after_compat_tokio_spawn(&child, true).unwrap();
            let status = tokio::time::timeout(std::time::Duration::from_secs(30), child.wait())
                .await
                .expect("a child left suspended would hang here")
                .unwrap();
            assert_eq!(status.code(), Some(7));
        });
    }

    #[cfg(feature = "async-process")]
    #[test]
    fn owner_death_child_is_in_a_job_by_the_time_spawn_returns() {
        use windows_sys::Win32::System::JobObjects::IsProcessInJob;

        runtime().block_on(async {
            let mut child = cmd_child("ping -n 6 127.0.0.1 > nul", true).spawn().unwrap();
            super::after_compat_tokio_spawn(&child, true).unwrap();
            let mut in_job = 0;
            // SAFETY: the handle is the live child handle Tokio owns; a null job
            // asks whether the process is in any job.
            let ok = unsafe {
                IsProcessInJob(child.raw_handle().unwrap(), std::ptr::null_mut(), &mut in_job)
            };
            assert_ne!(ok, 0, "IsProcessInJob failed");
            assert_ne!(in_job, 0, "the child must already be contained");
            child.kill().await.unwrap();
        });
    }

    #[cfg(feature = "async-process")]
    #[test]
    fn non_owner_death_child_is_not_suspended_or_contained() {
        runtime().block_on(async {
            let mut child = cmd_child("exit 3", false).spawn().unwrap();
            super::after_compat_tokio_spawn(&child, false).unwrap();
            let status = tokio::time::timeout(std::time::Duration::from_secs(30), child.wait())
                .await
                .expect("an ordinary child must run without being resumed")
                .unwrap();
            assert_eq!(status.code(), Some(3));
        });
    }

    #[test]
    fn fault_code_names_are_byte_exact() {
        // #974 PR 2: moved out of probe-daemon's crash store, spelling unchanged.
        assert_eq!(super::process_fault_code_name(0xC000_0005), "0xC0000005");
        assert_eq!(super::process_fault_code_name(0x8000_0003), "0x80000003");
    }

    #[cfg(feature = "ipc")]
    #[test]
    fn component_runtime_dir_is_byte_exact_for_the_probe() {
        // #974 PR 2: probe-daemon's discovery directory, formerly derived in
        // the daemon from `LOCALAPPDATA` with the temp directory as fallback.
        assert_eq!(
            super::component_runtime_dir_in(
                Some(std::ffi::OsString::from(r"C:\Users\u\AppData\Local")),
                std::path::PathBuf::from(r"C:\Temp"),
                "probe",
            ),
            std::path::PathBuf::from(r"C:\Users\u\AppData\Local\running-process\probe")
        );
        assert_eq!(
            super::component_runtime_dir_in(None, std::path::PathBuf::from(r"C:\Temp"), "probe"),
            std::path::PathBuf::from(r"C:\Temp\running-process\probe")
        );
    }

    #[cfg(feature = "ipc")]
    #[test]
    fn component_endpoint_path_is_the_bare_pipe_name_whatever_the_component() {
        // #974: the pipe namespace is machine-wide and name-keyed, so the
        // component needs no directory; services stay apart by name prefix.
        assert_eq!(
            super::ipc_component_endpoint_path("probe", "rpp-probe-abc-0"),
            r"\\.\pipe\rpp-probe-abc-0"
        );
        assert_eq!(
            super::ipc_component_endpoint_path("broker-v2", "rpb-v2-x-0"),
            r"\\.\pipe\rpb-v2-x-0"
        );
        assert_eq!(
            super::ipc_broker_endpoint_name("rpb-v2-x-0", false).unwrap(),
            super::ipc_component_endpoint_path("broker-v2", "rpb-v2-x-0")
        );
    }

    #[test]
    fn shell_command_preserves_round_trippable_cmd_quoting_contract() {
        let command_text = "echo alpha beta ^& gamma";
        let mut command = super::shell_command(command_text);
        assert_eq!(command.get_program(), OsStr::new("cmd.exe"));
        assert_eq!(
            command.get_args().collect::<Vec<_>>(),
            [
                OsStr::new("/D"),
                OsStr::new("/S"),
                OsStr::new("/C"),
                OsStr::new(command_text)
            ]
        );
        let output = command.output().expect("shell command should execute");
        assert!(output.status.success());
        assert_eq!(output.stdout, b"alpha beta & gamma\r\n");
    }

    #[test]
    fn compat_shell_command_preserves_nested_quotes_for_standard_spawn() {
        let command_text = "if \"alpha beta\"==\"alpha beta\" (echo shell-ok)";
        let mut command = super::compat_shell_command(command_text);
        assert_eq!(command.get_program(), OsStr::new("cmd"));
        assert_eq!(
            command.get_args().collect::<Vec<_>>(),
            [
                OsStr::new("/D /S /C \""),
                OsStr::new(command_text),
                OsStr::new("\"")
            ]
        );
        let output = command.output().expect("compat shell command should execute");
        assert!(output.status.success());
        assert_eq!(output.stdout, b"shell-ok\r\n");
    }
}

#[cfg(all(test, feature = "ipc"))]
mod endpoint_naming_tests {
    use super::{ipc_broker_v1_endpoint_path, ipc_endpoint_name_limit, WINDOWS_MAX_PATH};

    #[test]
    fn the_v1_address_is_a_named_pipe_carrying_the_bare_name() {
        let address = ipc_broker_v1_endpoint_path("rpb-v1-abc-shared").expect("derive address");
        assert!(address.starts_with(r"\\.\pipe\"));
        assert!(address.ends_with("rpb-v1-abc-shared"));
    }

    #[test]
    fn an_over_long_name_is_refused_against_max_path() {
        let err = ipc_broker_v1_endpoint_path(&"a".repeat(WINDOWS_MAX_PATH))
            .expect_err("must exceed MAX_PATH");
        assert_eq!(err.max, WINDOWS_MAX_PATH);
        assert_eq!(err.limit_label, "Windows MAX_PATH");
        assert!(err.len > WINDOWS_MAX_PATH);
    }

    #[test]
    fn the_reported_budget_is_max_path() {
        let limit = ipc_endpoint_name_limit();
        assert_eq!(limit.max_bytes, WINDOWS_MAX_PATH);
        assert_eq!(limit.label, "Windows MAX_PATH");
    }

    #[test]
    fn the_scope_spelling_folds_case_and_separators() {
        // Named pipes and paths are case-insensitive here, so two callers
        // spelling the same install differently must hash identically. This
        // pins the spelling itself: changing it re-scopes every deployed
        // broker, and the stability tests upstream would not notice.
        use super::ipc_endpoint_scope_bytes;

        let mixed = ipc_endpoint_scope_bytes(std::path::Path::new(r"C:\Program Files\App\Broker.exe"));
        assert_eq!(mixed, b"c:/program files/app/broker.exe".to_vec());

        let other = ipc_endpoint_scope_bytes(std::path::Path::new("c:/PROGRAM FILES/app/BROKER.exe"));
        assert_eq!(mixed, other);
    }

}

/// Pins the per-host answers that facade callers branch on, so a change to
/// either is a visible, reviewed edit rather than a silent behaviour change.
#[cfg(test)]
mod host_semantics_tests {
    const ABSENT_PID: u32 = 0x7fff_fffe;

    #[test]
    fn priority_on_absent_pid_reports_the_os_error() {
        assert!(super::apply_process_priority(ABSENT_PID, 0).is_err());
    }

    #[test]
    fn interrupt_requires_a_new_process_group() {
        for flags in [None, Some(0)] {
            let error = super::send_interrupt(1, flags, true).unwrap_err();
            assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);
            assert_eq!(
                error.to_string(),
                "send_interrupt on Windows requires CREATE_NEW_PROCESS_GROUP"
            );
        }
    }

    #[test]
    fn open_handles_block_removal_matches_this_host() {
        assert!(super::fs_open_handles_block_removal());
    }

    #[cfg(feature = "ipc")]
    #[test]
    fn handoff_transport_is_available() {
        assert!(super::ipc_handoff_transport_available());
    }
}