aboutsummaryrefslogtreecommitdiffstats
path: root/src/pty.rs
blob: 450ea8cd3e25102370e1c322606df75bf910156b (plain) (blame)
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
use crate::error::*;

use std::os::unix::io::{FromRawFd as _, IntoRawFd as _};

pub struct Pty {
    master: std::fs::File,
    slave: std::path::PathBuf,
}

impl Pty {
    pub fn new() -> Result<Self> {
        let master = nix::pty::posix_openpt(
            nix::fcntl::OFlag::O_RDWR | nix::fcntl::OFlag::O_NOCTTY,
        )?;
        nix::pty::grantpt(&master)?;
        nix::pty::unlockpt(&master)?;

        let slave = nix::pty::ptsname_r(&master)?.into();

        let master_fd = master.into_raw_fd();
        let master = unsafe { std::fs::File::from_raw_fd(master_fd) };

        Ok(Self { master, slave })
    }

    pub fn master(&self) -> &std::fs::File {
        &self.master
    }

    pub fn slave(&self) -> Result<std::fs::File> {
        Ok(std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .open(&self.slave)?)
    }
}