diag_server/
new_pty.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4// UNSAFETY: Calling openpty.
5#![expect(unsafe_code)]
6
7use std::fs::File;
8use std::io;
9use std::os::fd::FromRawFd;
10use std::ptr::null_mut;
11
12pub(crate) fn new_pty() -> io::Result<(File, File)> {
13    // SAFETY: calling openpty as documented
14    unsafe {
15        let mut primary = 0;
16        let mut secondary = 0;
17        if libc::openpty(
18            &mut primary,
19            &mut secondary,
20            null_mut(),
21            null_mut(),
22            null_mut(),
23        ) < 0
24        {
25            return Err(io::Error::last_os_error());
26        }
27        let primary = File::from_raw_fd(primary);
28        let secondary = File::from_raw_fd(secondary);
29        Ok((primary, secondary))
30    }
31}