Use OwnedFd with NullTty

This commit is contained in:
Marcin Kulik
2024-01-09 16:47:02 +01:00
parent 0048d19d43
commit 8984c2bd2d
2 changed files with 7 additions and 6 deletions

View File

@@ -3,7 +3,6 @@ use crate::tty::Tty;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use nix::errno::Errno; use nix::errno::Errno;
use nix::sys::select::{select, FdSet}; use nix::sys::select::{select, FdSet};
use nix::unistd::pipe;
use nix::{libc, pty, sys::signal, sys::wait, unistd, unistd::ForkResult}; use nix::{libc, pty, sys::signal, sys::wait, unistd, unistd::ForkResult};
use signal_hook::consts::{SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGWINCH}; use signal_hook::consts::{SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGWINCH};
use signal_hook::SigId; use signal_hook::SigId;
@@ -316,7 +315,7 @@ struct SignalFd {
impl SignalFd { impl SignalFd {
fn open(signal: libc::c_int) -> Result<Self> { fn open(signal: libc::c_int) -> Result<Self> {
let (rx, tx) = pipe()?; let (rx, tx) = unistd::pipe()?;
set_non_blocking(&rx)?; set_non_blocking(&rx)?;
set_non_blocking(&tx)?; set_non_blocking(&tx)?;
let rx = unsafe { OwnedFd::from_raw_fd(rx) }; let rx = unsafe { OwnedFd::from_raw_fd(rx) };

View File

@@ -2,7 +2,7 @@ use anyhow::Result;
use nix::{libc, pty, unistd}; use nix::{libc, pty, unistd};
use std::{ use std::{
fs, io, fs, io,
os::fd::{AsFd, AsRawFd, BorrowedFd}, os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd},
}; };
use termion::raw::{IntoRawMode, RawTerminal}; use termion::raw::{IntoRawMode, RawTerminal};
@@ -66,13 +66,15 @@ impl AsFd for DevTty {
} }
pub struct NullTty { pub struct NullTty {
tx: i32, tx: OwnedFd,
_rx: i32, _rx: OwnedFd,
} }
impl NullTty { impl NullTty {
pub fn open() -> Result<Self> { pub fn open() -> Result<Self> {
let (rx, tx) = unistd::pipe()?; let (rx, tx) = unistd::pipe()?;
let rx = unsafe { OwnedFd::from_raw_fd(rx) };
let tx = unsafe { OwnedFd::from_raw_fd(tx) };
Ok(Self { tx, _rx: rx }) Ok(Self { tx, _rx: rx })
} }
@@ -107,6 +109,6 @@ impl io::Write for NullTty {
impl AsFd for NullTty { impl AsFd for NullTty {
fn as_fd(&self) -> BorrowedFd<'_> { fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.tx.as_raw_fd()) } self.tx.as_fd()
} }
} }