diff --git a/changelog/2315.changed.md b/changelog/2315.changed.md
new file mode 100644
index 0000000000..bf437876d6
--- /dev/null
+++ b/changelog/2315.changed.md
@@ -0,0 +1,12 @@
+Change the `ForkptyResult` type to the following repr so that the uninitialized
+`master` field won't be accessed in the child process:
+
+```rs
+pub enum ForkptyResult {
+ Parent {
+ child: Pid,
+ master: OwnedFd,
+ },
+ Child,
+}
+```
diff --git a/src/pty.rs b/src/pty.rs
index 74f8ecf0df..818b25b1b1 100644
--- a/src/pty.rs
+++ b/src/pty.rs
@@ -12,8 +12,6 @@ use std::os::unix::prelude::*;
use crate::errno::Errno;
#[cfg(not(target_os = "aix"))]
use crate::sys::termios::Termios;
-#[cfg(feature = "process")]
-use crate::unistd::ForkResult;
#[cfg(all(feature = "process", not(target_os = "aix")))]
use crate::unistd::Pid;
use crate::{fcntl, unistd, Result};
@@ -31,15 +29,19 @@ pub struct OpenptyResult {
feature! {
#![feature = "process"]
-/// Representation of a master with a forked pty
-///
-/// This is returned by [`forkpty`].
+/// A successful result of [`forkpty()`].
#[derive(Debug)]
-pub struct ForkptyResult {
- /// The master port in a virtual pty pair
- pub master: OwnedFd,
- /// Metadata about forked process
- pub fork_result: ForkResult,
+pub enum ForkptyResult {
+ /// This is the parent process of the underlying fork.
+ Parent {
+ /// The PID of the fork's child process
+ child: Pid,
+ /// A file descriptor referring to master side of the pseudoterminal of
+ /// the child process.
+ master: OwnedFd,
+ },
+ /// This is the child process of the underlying fork.
+ Child,
}
}
@@ -300,9 +302,7 @@ pub fn openpty<
feature! {
#![feature = "process"]
-/// Create a new pseudoterminal, returning the master file descriptor and forked pid.
-/// in `ForkptyResult`
-/// (see [`forkpty`](https://man7.org/linux/man-pages/man3/forkpty.3.html)).
+/// Create a new process operating in a pseudoterminal.
///
/// If `winsize` is not `None`, the window size of the slave will be set to
/// the values in `winsize`. If `termios` is not `None`, the pseudoterminal's
@@ -319,6 +319,11 @@ feature! {
/// special care must be taken to only invoke code you can control and audit.
///
/// [async-signal-safe]: https://man7.org/linux/man-pages/man7/signal-safety.7.html
+///
+/// # Reference
+///
+/// * [FreeBSD](https://man.freebsd.org/cgi/man.cgi?query=forkpty)
+/// * [Linux](https://man7.org/linux/man-pages/man3/forkpty.3.html)
#[cfg(not(target_os = "aix"))]
pub unsafe fn forkpty<'a, 'b, T: Into