sysprims-pty 0.9.2

Cross-platform PTY interface with sysprims containment
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
//! This crate provides a cross platform API for working with the
//! psuedo terminal (pty) interfaces provided by the system.
//! Unlike other crates in this space, this crate provides a set
//! of traits that allow selecting from different implementations
//! at runtime.
//! This crate is part of [wezterm](https://github.com/wezterm/wezterm).
//!
//! ```no_run
//! use portable_pty::{CommandBuilder, PtySize, native_pty_system, PtySystem};
//! use anyhow::Error;
//!
//! // Use the native pty implementation for the system
//! let pty_system = native_pty_system();
//!
//! // Create a new pty
//! let mut pair = pty_system.openpty(PtySize {
//!     rows: 24,
//!     cols: 80,
//!     // Not all systems support pixel_width, pixel_height,
//!     // but it is good practice to set it to something
//!     // that matches the size of the selected font.  That
//!     // is more complex than can be shown here in this
//!     // brief example though!
//!     pixel_width: 0,
//!     pixel_height: 0,
//! })?;
//!
//! // Spawn a shell into the pty
//! let cmd = CommandBuilder::new("bash");
//! let child = pair.slave.spawn_command(cmd)?;
//!
//! // Read and parse output from the pty with reader
//! let mut reader = pair.master.try_clone_reader()?;
//!
//! // Send data to the pty by writing to the master
//! writeln!(pair.master.take_writer()?, "ls -l\r\n")?;
//! # Ok::<(), Error>(())
//! ```
//!
use anyhow::Error;
use downcast_rs::{impl_downcast, Downcast};
#[cfg(feature = "serde_support")]
use serde_derive::*;
use std::io::Result as IoResult;
#[cfg(windows)]
use std::os::windows::prelude::{AsRawHandle, RawHandle};

/// A PTY child whose exclusive reap ownership can be transferred to sysprims.
///
/// Callers receive this type only after a containment guard has finalized.
/// While containment is active, the guard keeps the child handle private.
#[derive(Debug)]
pub struct ContainedPtyChild {
    #[cfg(unix)]
    pub(crate) child: std::process::Child,
    #[cfg(windows)]
    pub(crate) child: win::WinChild,
}

impl ContainedPtyChild {
    #[cfg(unix)]
    pub(crate) fn new(child: std::process::Child) -> Self {
        Self { child }
    }

    #[cfg(windows)]
    pub(crate) fn new(child: win::WinChild) -> Self {
        Self { child }
    }
}

/// Stage at which a guarded PTY spawn failed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContainedPtySpawnErrorStage {
    /// No child was created.
    BeforeSpawn,
    /// Spawn-time group/Job assignment evidence could not be sealed.
    Receipt,
    /// The exact child and receipt could not be adopted into a guard.
    Adoption,
    /// The assigned child could not be resumed and was resolved through its Job.
    Resume,
}

enum FailedChildRecoveryCommand {
    Adopt(ContainedPtyChild),
    Attempt {
        timeout: std::time::Duration,
        #[cfg(test)]
        fault: FailedChildRecoveryFault,
        response: std::sync::mpsc::SyncSender<IoResult<bool>>,
    },
}

#[cfg(test)]
#[derive(Clone, Copy)]
enum FailedChildRecoveryFault {
    None,
    #[cfg(unix)]
    KillError,
    #[cfg(unix)]
    Deadline,
}

struct FailedChildRecovery {
    sender: std::sync::mpsc::Sender<FailedChildRecoveryCommand>,
    pending: std::sync::Arc<std::sync::atomic::AtomicBool>,
}

impl FailedChildRecovery {
    fn prepare() -> IoResult<Self> {
        let (sender, receiver) = std::sync::mpsc::channel();
        let pending = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let worker_pending = std::sync::Arc::clone(&pending);
        std::thread::Builder::new()
            .name("sysprims-pty-recovery".to_string())
            .spawn(move || failed_child_recovery_worker(receiver, worker_pending))?;
        Ok(Self { sender, pending })
    }

    fn attach(&self, child: ContainedPtyChild) {
        self.pending
            .store(true, std::sync::atomic::Ordering::Release);
        if let Err(error) = self.sender.send(FailedChildRecoveryCommand::Adopt(child)) {
            let child = match error.0 {
                FailedChildRecoveryCommand::Adopt(child) => child,
                FailedChildRecoveryCommand::Attempt { .. } => unreachable!(),
            };
            bounded_local_recovery_or_abort(child);
        }
    }

    fn attempt(&self, timeout: std::time::Duration) -> IoResult<bool> {
        self.attempt_inner(
            timeout,
            #[cfg(test)]
            FailedChildRecoveryFault::None,
        )
    }

    fn attempt_inner(
        &self,
        timeout: std::time::Duration,
        #[cfg(test)] fault: FailedChildRecoveryFault,
    ) -> IoResult<bool> {
        if !self.pending.load(std::sync::atomic::Ordering::Acquire) {
            return Ok(true);
        }

        let timeout = timeout.min(std::time::Duration::from_secs(2));
        let (response, result) = std::sync::mpsc::sync_channel(1);
        self.sender
            .send(FailedChildRecoveryCommand::Attempt {
                timeout,
                #[cfg(test)]
                fault,
                response,
            })
            .map_err(|_| {
                std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    "exact-child recovery owner stopped",
                )
            })?;
        result
            .recv_timeout(timeout + std::time::Duration::from_millis(250))
            .map_err(|error| {
                std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    format!("exact-child recovery response unavailable: {error}"),
                )
            })?
    }

    #[cfg(all(test, unix))]
    fn attempt_with_fault(
        &self,
        timeout: std::time::Duration,
        fault: FailedChildRecoveryFault,
    ) -> IoResult<bool> {
        self.attempt_inner(timeout, fault)
    }

    fn is_pending(&self) -> bool {
        self.pending.load(std::sync::atomic::Ordering::Acquire)
    }
}

fn failed_child_recovery_worker(
    receiver: std::sync::mpsc::Receiver<FailedChildRecoveryCommand>,
    pending: std::sync::Arc<std::sync::atomic::AtomicBool>,
) {
    let mut child = None;
    while let Ok(command) = receiver.recv() {
        match command {
            FailedChildRecoveryCommand::Adopt(adopted) => {
                debug_assert!(child.is_none());
                child = Some(adopted);
            }
            FailedChildRecoveryCommand::Attempt {
                timeout,
                #[cfg(test)]
                fault,
                response,
            } => {
                let result = attempt_failed_child_recovery(
                    &mut child,
                    timeout,
                    #[cfg(test)]
                    fault,
                );
                pending.store(child.is_some(), std::sync::atomic::Ordering::Release);
                let _ = response.send(result);
            }
        }
    }

    // Disconnect means the public error/controller was dropped. Ownership
    // stays on this parent-side worker; the destructor itself never blocks.
    while child.is_some() {
        let _ = attempt_failed_child_recovery(
            &mut child,
            std::time::Duration::from_secs(2),
            #[cfg(test)]
            FailedChildRecoveryFault::None,
        );
        pending.store(child.is_some(), std::sync::atomic::Ordering::Release);
        if child.is_some() {
            std::thread::sleep(std::time::Duration::from_millis(10));
        }
    }
}

fn attempt_failed_child_recovery(
    child: &mut Option<ContainedPtyChild>,
    timeout: std::time::Duration,
    #[cfg(test)] fault: FailedChildRecoveryFault,
) -> IoResult<bool> {
    let owned_child = match child.as_mut() {
        Some(child) => child,
        None => return Ok(true),
    };

    if Child::try_wait(owned_child)?.is_some() {
        child.take();
        return Ok(true);
    }

    #[cfg(test)]
    match fault {
        #[cfg(unix)]
        FailedChildRecoveryFault::KillError => {
            return Err(std::io::Error::new(
                std::io::ErrorKind::PermissionDenied,
                "injected exact-child kill failure",
            ));
        }
        #[cfg(unix)]
        FailedChildRecoveryFault::Deadline => {
            std::thread::sleep(timeout.min(std::time::Duration::from_millis(10)));
            return Ok(false);
        }
        FailedChildRecoveryFault::None => {}
    }

    // This is the exact spawn-owned child handle. Never
    // reconstruct signal authority from an observed PID.
    ChildKiller::kill(owned_child)?;

    let timeout = timeout.min(std::time::Duration::from_secs(2));
    let deadline = std::time::Instant::now() + timeout;
    loop {
        match Child::try_wait(owned_child)? {
            Some(_) => {
                child.take();
                return Ok(true);
            }
            None if std::time::Instant::now() < deadline => {
                std::thread::sleep(std::time::Duration::from_millis(10));
            }
            None => return Ok(false),
        }
    }
}

fn bounded_local_recovery_or_abort(mut child: ContainedPtyChild) -> ! {
    let _ = ChildKiller::kill(&mut child);
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
    loop {
        match Child::try_wait(&mut child) {
            Ok(Some(_)) => {
                std::process::abort();
            }
            Ok(None) if std::time::Instant::now() < deadline => {
                std::thread::sleep(std::time::Duration::from_millis(10));
            }
            Ok(None) | Err(_) => std::process::abort(),
        }
    }
}

/// Error from a guarded PTY spawn.
///
/// Errors after child creation retain an opaque exact-child recovery state.
/// The active child handle is never returned to the caller or discarded.
/// Dropping an error disconnects its bounded controller; a parent-side worker
/// keeps exact-child ownership through direct kill and reap.
pub struct ContainedPtySpawnError {
    stage: ContainedPtySpawnErrorStage,
    source: Error,
    recovery: Option<FailedChildRecovery>,
}

impl ContainedPtySpawnError {
    pub(crate) fn before_spawn(source: Error) -> Self {
        Self {
            stage: ContainedPtySpawnErrorStage::BeforeSpawn,
            source,
            recovery: None,
        }
    }

    pub(crate) fn after_spawn(
        stage: ContainedPtySpawnErrorStage,
        source: Error,
        child: ContainedPtyChild,
        recovery: FailedChildRecovery,
    ) -> Self {
        debug_assert!(stage != ContainedPtySpawnErrorStage::BeforeSpawn);
        recovery.attach(child);
        let mut error = Self {
            stage,
            source,
            recovery: Some(recovery),
        };
        let _ = error.recover(std::time::Duration::from_secs(2));
        error
    }

    #[cfg(all(test, unix))]
    pub(crate) fn after_spawn_pending_for_test(
        stage: ContainedPtySpawnErrorStage,
        source: Error,
        child: ContainedPtyChild,
        recovery: FailedChildRecovery,
    ) -> Self {
        debug_assert!(stage != ContainedPtySpawnErrorStage::BeforeSpawn);
        recovery.attach(child);
        Self {
            stage,
            source,
            recovery: Some(recovery),
        }
    }

    /// Report the stage at which the spawn failed.
    pub fn stage(&self) -> ContainedPtySpawnErrorStage {
        self.stage
    }

    /// Whether exact-child recovery is still in progress.
    pub fn recovery_pending(&self) -> bool {
        self.recovery
            .as_ref()
            .is_some_and(FailedChildRecovery::is_pending)
    }

    /// Make one bounded exact-child recovery attempt.
    ///
    /// The attempt is capped at two seconds. Returns `true` when no child
    /// remains to reap.
    pub fn recover(&mut self, timeout: std::time::Duration) -> IoResult<bool> {
        match self.recovery.as_mut() {
            Some(recovery) => recovery.attempt(timeout),
            None => Ok(true),
        }
    }
}

impl std::fmt::Debug for ContainedPtySpawnError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("ContainedPtySpawnError")
            .field("stage", &self.stage)
            .field("source", &self.source)
            .field("recovery_pending", &self.recovery_pending())
            .finish()
    }
}

impl std::fmt::Display for ContainedPtySpawnError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.source.fmt(formatter)
    }
}

impl std::error::Error for ContainedPtySpawnError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(self.source.as_ref())
    }
}

/// An owned, spawn-time-acquired PTY containment capability.
pub type ContainedPtyGuard = sysprims_timeout::ContainmentGuard<ContainedPtyChild>;

pub mod cmdbuilder;
pub use cmdbuilder::CommandBuilder;

#[cfg(unix)]
pub mod unix;
#[cfg(windows)]
pub mod win;

pub mod serial;

/// Represents the size of the visible display area in the pty
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct PtySize {
    /// The number of lines of text
    pub rows: u16,
    /// The number of columns of text
    pub cols: u16,
    /// The width of a cell in pixels.  Note that some systems never
    /// fill this value and ignore it.
    pub pixel_width: u16,
    /// The height of a cell in pixels.  Note that some systems never
    /// fill this value and ignore it.
    pub pixel_height: u16,
}

impl Default for PtySize {
    fn default() -> Self {
        PtySize {
            rows: 24,
            cols: 80,
            pixel_width: 0,
            pixel_height: 0,
        }
    }
}

/// Represents the master/control end of the pty
pub trait MasterPty: Downcast + Send {
    /// Inform the kernel and thus the child process that the window resized.
    /// It will update the winsize information maintained by the kernel,
    /// and generate a signal for the child to notice and update its state.
    fn resize(&self, size: PtySize) -> Result<(), Error>;
    /// Retrieves the size of the pty as known by the kernel
    fn get_size(&self) -> Result<PtySize, Error>;
    /// Obtain a readable handle; output from the slave(s) is readable
    /// via this stream.
    fn try_clone_reader(&self) -> Result<Box<dyn std::io::Read + Send>, Error>;
    /// Obtain a writable handle; writing to it will send data to the
    /// slave end.
    /// Dropping the writer will send EOF to the slave end.
    /// It is invalid to take the writer more than once.
    fn take_writer(&self) -> Result<Box<dyn std::io::Write + Send>, Error>;

    /// If applicable to the type of the tty, return the local process id
    /// of the process group or session leader
    #[cfg(unix)]
    fn process_group_leader(&self) -> Option<libc::pid_t>;

    /// If get_termios() and process_group_leader() are both implemented and
    /// return Some, then as_raw_fd() should return the same underlying fd
    /// associated with the stream. This is to enable applications that
    /// "know things" to query similar information for themselves.
    #[cfg(unix)]
    fn as_raw_fd(&self) -> Option<unix::RawFd>;

    #[cfg(unix)]
    fn tty_name(&self) -> Option<std::path::PathBuf>;

    /// If applicable to the type of the tty, return the termios
    /// associated with the stream
    #[cfg(unix)]
    fn get_termios(&self) -> Option<nix::sys::termios::Termios> {
        None
    }
}
impl_downcast!(MasterPty);

/// Represents a child process spawned into the pty.
/// This handle can be used to wait for or terminate that child process.
pub trait Child: std::fmt::Debug + ChildKiller + Downcast + Send {
    /// Poll the child to see if it has completed.
    /// Does not block.
    /// Returns None if the child has not yet terminated,
    /// else returns its exit status.
    fn try_wait(&mut self) -> IoResult<Option<ExitStatus>>;
    /// Blocks execution until the child process has completed,
    /// yielding its exit status.
    fn wait(&mut self) -> IoResult<ExitStatus>;
    /// Returns the process identifier of the child process,
    /// if applicable
    fn process_id(&self) -> Option<u32>;
    /// Returns the process handle of the child process, if applicable.
    /// Only available on Windows.
    #[cfg(windows)]
    fn as_raw_handle(&self) -> Option<std::os::windows::io::RawHandle>;
}
impl_downcast!(Child);

/// Represents the ability to signal a Child to terminate
pub trait ChildKiller: std::fmt::Debug + Downcast + Send {
    /// Terminate the child process
    fn kill(&mut self) -> IoResult<()>;

    /// Clone an object that can be split out from the Child in order
    /// to send it signals independently from a thread that may be
    /// blocked in `.wait`.
    fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync>;
}
impl_downcast!(ChildKiller);

/// Represents the slave side of a pty.
/// Can be used to spawn processes into the pty.
pub trait SlavePty {
    /// Spawns the command specified by the provided CommandBuilder
    fn spawn_command(&self, cmd: CommandBuilder) -> Result<Box<dyn Child + Send + Sync>, Error>;

    /// Spawns a command with a sealed same-spawn sysprims containment guard.
    ///
    /// Native Unix PTYs use a sealed session receipt. Native Windows ConPTY
    /// creates the child suspended and seals exact-process Job membership
    /// before one resume. Other PTY implementations reject before spawning.
    fn spawn_contained_command(
        &self,
        cmd: CommandBuilder,
    ) -> Result<ContainedPtyGuard, ContainedPtySpawnError> {
        let _ = cmd;
        Err(ContainedPtySpawnError::before_spawn(anyhow::anyhow!(
            "spawn-time PTY containment is unavailable for this PTY implementation"
        )))
    }
}

/// Represents the exit status of a child process.
#[derive(Debug, Clone)]
pub struct ExitStatus {
    code: u32,
    signal: Option<String>,
}

impl ExitStatus {
    /// Construct an ExitStatus from a process return code
    pub fn with_exit_code(code: u32) -> Self {
        Self { code, signal: None }
    }

    /// Construct an ExitStatus from a signal name
    pub fn with_signal(signal: &str) -> Self {
        Self {
            code: 1,
            signal: Some(signal.to_string()),
        }
    }

    /// Returns true if the status indicates successful completion
    pub fn success(&self) -> bool {
        match self.signal {
            None => self.code == 0,
            Some(_) => false,
        }
    }

    /// Returns the exit code that this ExitStatus was constructed with
    pub fn exit_code(&self) -> u32 {
        self.code
    }

    /// Returns the signal if present that this ExitStatus was constructed with
    pub fn signal(&self) -> Option<&str> {
        self.signal.as_deref()
    }
}

impl From<std::process::ExitStatus> for ExitStatus {
    fn from(status: std::process::ExitStatus) -> ExitStatus {
        #[cfg(unix)]
        {
            use std::os::unix::process::ExitStatusExt;

            if let Some(signal) = status.signal() {
                let signame = unsafe { libc::strsignal(signal) };
                let signal = if signame.is_null() {
                    format!("Signal {signal}")
                } else {
                    let signame = unsafe { std::ffi::CStr::from_ptr(signame) };
                    signame.to_string_lossy().to_string()
                };

                return ExitStatus {
                    code: status.code().map(|c| c as u32).unwrap_or(1),
                    signal: Some(signal),
                };
            }
        }

        let code =
            status
                .code()
                .map(|c| c as u32)
                .unwrap_or_else(|| if status.success() { 0 } else { 1 });

        ExitStatus { code, signal: None }
    }
}

impl std::fmt::Display for ExitStatus {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        if self.success() {
            write!(fmt, "Success")
        } else {
            match &self.signal {
                Some(sig) => write!(fmt, "Terminated by {sig}"),
                None => write!(fmt, "Exited with code {}", self.code),
            }
        }
    }
}

pub struct PtyPair {
    // slave is listed first so that it is dropped first.
    // The drop order is stable and specified by rust rfc 1857
    pub slave: Box<dyn SlavePty + Send>,
    pub master: Box<dyn MasterPty + Send>,
}

/// The `PtySystem` trait allows an application to work with multiple
/// possible Pty implementations at runtime.  This is important on
/// Windows systems which have a variety of implementations.
pub trait PtySystem: Downcast {
    /// Create a new Pty instance with the window size set to the specified
    /// dimensions.  Returns a (master, slave) Pty pair.  The master side
    /// is used to drive the slave side.
    fn openpty(&self, size: PtySize) -> anyhow::Result<PtyPair>;
}
impl_downcast!(PtySystem);

impl Child for std::process::Child {
    fn try_wait(&mut self) -> IoResult<Option<ExitStatus>> {
        std::process::Child::try_wait(self).map(|status| status.map(Into::into))
    }

    fn wait(&mut self) -> IoResult<ExitStatus> {
        std::process::Child::wait(self).map(Into::into)
    }

    fn process_id(&self) -> Option<u32> {
        Some(self.id())
    }

    #[cfg(windows)]
    fn as_raw_handle(&self) -> Option<std::os::windows::io::RawHandle> {
        Some(std::os::windows::io::AsRawHandle::as_raw_handle(self))
    }
}

impl Child for ContainedPtyChild {
    fn try_wait(&mut self) -> IoResult<Option<ExitStatus>> {
        #[cfg(unix)]
        return std::process::Child::try_wait(&mut self.child).map(|status| status.map(Into::into));

        #[cfg(windows)]
        return Child::try_wait(&mut self.child);
    }

    fn wait(&mut self) -> IoResult<ExitStatus> {
        #[cfg(unix)]
        return std::process::Child::wait(&mut self.child).map(Into::into);

        #[cfg(windows)]
        return Child::wait(&mut self.child);
    }

    fn process_id(&self) -> Option<u32> {
        #[cfg(unix)]
        return Some(self.child.id());

        #[cfg(windows)]
        return Child::process_id(&self.child);
    }

    #[cfg(windows)]
    fn as_raw_handle(&self) -> Option<std::os::windows::io::RawHandle> {
        Child::as_raw_handle(&self.child)
    }
}

impl sysprims_timeout::ContainmentChild for ContainedPtyChild {
    fn process_id(&self) -> Option<u32> {
        Child::process_id(self)
    }

    fn try_wait(&mut self) -> IoResult<bool> {
        Child::try_wait(self).map(|status| status.is_some())
    }

    #[cfg(windows)]
    fn raw_process_handle(&self) -> Option<std::os::windows::io::RawHandle> {
        Child::as_raw_handle(self)
    }
}

#[derive(Debug)]
struct ProcessSignaller {
    pid: Option<u32>,

    #[cfg(windows)]
    handle: Option<filedescriptor::OwnedHandle>,
}

#[cfg(windows)]
impl ChildKiller for ProcessSignaller {
    fn kill(&mut self) -> IoResult<()> {
        if let Some(handle) = &self.handle {
            unsafe {
                if winapi::um::processthreadsapi::TerminateProcess(handle.as_raw_handle() as _, 127)
                    == 0
                {
                    return Err(std::io::Error::last_os_error());
                }
            }
        }
        Ok(())
    }
    fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {
        Box::new(Self {
            pid: self.pid,
            handle: self.handle.as_ref().and_then(|h| h.try_clone().ok()),
        })
    }
}

#[cfg(unix)]
impl ChildKiller for ProcessSignaller {
    fn kill(&mut self) -> IoResult<()> {
        if let Some(pid) = self.pid {
            let result = unsafe { libc::kill(pid as i32, libc::SIGHUP) };
            if result != 0 {
                return Err(std::io::Error::last_os_error());
            }
        }
        Ok(())
    }

    fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {
        Box::new(Self { pid: self.pid })
    }
}

impl ChildKiller for std::process::Child {
    fn kill(&mut self) -> IoResult<()> {
        #[cfg(unix)]
        {
            // On unix, we send the SIGHUP signal instead of trying to kill
            // the process. The default behavior of a process receiving this
            // signal is to be killed unless it configured a signal handler.
            let result = unsafe { libc::kill(self.id() as i32, libc::SIGHUP) };
            if result != 0 {
                return Err(std::io::Error::last_os_error());
            }

            // We successfully delivered SIGHUP, but the semantics of Child::kill
            // are that on success the process is dead or shortly about to
            // terminate.  Since SIGUP doesn't guarantee termination, we
            // give the process a bit of a grace period to shutdown or do whatever
            // it is doing in its signal handler befre we proceed with the
            // full on kill.
            for attempt in 0..5 {
                if attempt > 0 {
                    std::thread::sleep(std::time::Duration::from_millis(50));
                }

                if let Ok(Some(_)) = self.try_wait() {
                    // It completed, so report success!
                    return Ok(());
                }
            }

            // it's still alive after a grace period, so proceed with a kill
        }

        std::process::Child::kill(self)
    }

    #[cfg(windows)]
    fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {
        struct RawDup(RawHandle);
        impl AsRawHandle for RawDup {
            fn as_raw_handle(&self) -> RawHandle {
                self.0
            }
        }

        Box::new(ProcessSignaller {
            pid: self.process_id(),
            handle: Child::as_raw_handle(self)
                .as_ref()
                .and_then(|h| filedescriptor::OwnedHandle::dup(&RawDup(*h)).ok()),
        })
    }

    #[cfg(unix)]
    fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {
        Box::new(ProcessSignaller {
            pid: self.process_id(),
        })
    }
}

impl ChildKiller for ContainedPtyChild {
    fn kill(&mut self) -> IoResult<()> {
        ChildKiller::kill(&mut self.child)
    }

    fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {
        ChildKiller::clone_killer(&self.child)
    }
}

pub fn native_pty_system() -> Box<dyn PtySystem + Send> {
    Box::new(NativePtySystem::default())
}

#[cfg(unix)]
pub type NativePtySystem = unix::UnixPtySystem;
#[cfg(windows)]
pub type NativePtySystem = win::conpty::ConPtySystem;