diff --git a/proxmox-sys/src/fd/borrowed_fd.rs b/proxmox-sys/src/fd/borrowed_fd.rs index e900f442..11f823aa 100644 --- a/proxmox-sys/src/fd/borrowed_fd.rs +++ b/proxmox-sys/src/fd/borrowed_fd.rs @@ -1,3 +1,5 @@ +#![allow(deprecated)] + use std::marker::PhantomData; use std::os::unix::io::{AsRawFd, RawFd}; diff --git a/proxmox-sys/src/fd/fd_impl.rs b/proxmox-sys/src/fd/fd_impl.rs index 6781d706..31f15e4f 100644 --- a/proxmox-sys/src/fd/fd_impl.rs +++ b/proxmox-sys/src/fd/fd_impl.rs @@ -9,8 +9,10 @@ use nix::NixPath; /// `RawFd` is required without the corresponding handler object (such as when only the file /// descriptor number is required in a closure which may be dropped instead of being executed). #[derive(Debug)] +#[deprecated(note = "use std::os::unix::io::OwnedFd instead")] pub struct Fd(pub RawFd); +#[allow(deprecated)] impl Drop for Fd { fn drop(&mut self) { // `>= 0` instead of `!= -1` to also handle things like AT_FDCWD @@ -22,11 +24,14 @@ impl Drop for Fd { } } +#[allow(deprecated)] impl Fd { + #[deprecated(note = "use proxmox_sys::fd::cwd instead")] pub const fn cwd() -> Self { Self(libc::AT_FDCWD) } + #[deprecated(note = "use proxmox_sys::fd::open instead")] pub fn open

(path: &P, oflag: OFlag, mode: Mode) -> Result where P: ?Sized + NixPath, @@ -34,6 +39,7 @@ impl Fd { nix::fcntl::open(path, oflag, mode).map(Self) } + #[deprecated(note = "use proxmox_sys::fd::openat instead")] pub fn openat(dirfd: &D, path: &P, oflag: OFlag, mode: Mode) -> Result where D: AsRawFd, @@ -48,18 +54,21 @@ impl Fd { } } +#[allow(deprecated)] impl FromRawFd for Fd { unsafe fn from_raw_fd(fd: RawFd) -> Self { Self(fd) } } +#[allow(deprecated)] impl AsRawFd for Fd { fn as_raw_fd(&self) -> RawFd { self.0 } } +#[allow(deprecated)] impl IntoRawFd for Fd { fn into_raw_fd(mut self) -> RawFd { let fd = self.0; @@ -68,18 +77,21 @@ impl IntoRawFd for Fd { } } +#[allow(deprecated)] impl AsRef for Fd { fn as_ref(&self) -> &FdRef { self.as_fd_ref() } } +#[allow(deprecated)] impl Borrow for Fd { fn borrow(&self) -> &FdRef { self.as_fd_ref() } } +#[allow(deprecated)] impl std::ops::Deref for Fd { type Target = FdRef; diff --git a/proxmox-sys/src/fd/mod.rs b/proxmox-sys/src/fd/mod.rs index 13acf6a9..f005b6ab 100644 --- a/proxmox-sys/src/fd/mod.rs +++ b/proxmox-sys/src/fd/mod.rs @@ -1,5 +1,11 @@ //! Raw file descriptor related structures. +use std::os::unix::io::AsRawFd; + +use nix::fcntl::OFlag; +use nix::sys::stat::Mode; +use nix::NixPath; + mod fd_impl; pub use fd_impl::*; @@ -9,14 +15,39 @@ pub use raw_fd_num::*; mod borrowed_fd; pub use borrowed_fd::*; -use std::os::unix::io::RawFd; +use std::os::unix::io::{FromRawFd, OwnedFd, RawFd}; use nix::fcntl::{fcntl, FdFlag, F_GETFD, F_SETFD}; /// Change the `O_CLOEXEC` flag of an existing file descriptor. pub fn fd_change_cloexec(fd: RawFd, on: bool) -> Result<(), anyhow::Error> { + change_cloexec(fd, on) +} + +/// Change the `O_CLOEXEC` flag of an existing file descriptor. +pub fn change_cloexec(fd: RawFd, on: bool) -> Result<(), anyhow::Error> { let mut flags = unsafe { FdFlag::from_bits_unchecked(fcntl(fd, F_GETFD)?) }; flags.set(FdFlag::FD_CLOEXEC, on); fcntl(fd, F_SETFD(flags))?; Ok(()) } + +pub fn cwd() -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(libc::AT_FDCWD) } +} + +pub fn open

(path: &P, oflag: OFlag, mode: Mode) -> Result +where + P: ?Sized + NixPath, +{ + nix::fcntl::open(path, oflag, mode).map(|fd| unsafe { OwnedFd::from_raw_fd(fd) }) +} + +pub fn openat(dirfd: &D, path: &P, oflag: OFlag, mode: Mode) -> Result +where + D: AsRawFd, + P: ?Sized + NixPath, +{ + nix::fcntl::openat(dirfd.as_raw_fd(), path, oflag, mode) + .map(|fd| unsafe { OwnedFd::from_raw_fd(fd) }) +} diff --git a/proxmox-sys/src/fs/dir.rs b/proxmox-sys/src/fs/dir.rs index 567a2dbf..bdef85e4 100644 --- a/proxmox-sys/src/fs/dir.rs +++ b/proxmox-sys/src/fs/dir.rs @@ -1,5 +1,5 @@ use std::ffi::CStr; -use std::os::unix::io::AsRawFd; +use std::os::unix::io::{AsRawFd, OwnedFd}; use std::path::Path; use anyhow::{bail, Error}; @@ -8,7 +8,6 @@ use nix::fcntl::OFlag; use nix::sys::stat; use nix::unistd; -use crate::fd::Fd; use crate::fs::{fchown, CreateOptions}; /// Creates directory at the provided path with specified ownership. @@ -66,11 +65,11 @@ fn create_path_do( use std::path::Component; let mut iter = path.components().peekable(); - let at: Fd = match iter.peek() { + let at: OwnedFd = match iter.peek() { Some(Component::Prefix(_)) => bail!("illegal prefix path component encountered"), Some(Component::RootDir) => { let _ = iter.next(); - Fd::open( + crate::fd::open( unsafe { CStr::from_bytes_with_nul_unchecked(b"/\0") }, OFlag::O_DIRECTORY, stat::Mode::empty(), @@ -78,11 +77,11 @@ fn create_path_do( } Some(Component::CurDir) => { let _ = iter.next(); - Fd::cwd() + crate::fd::cwd() } Some(Component::ParentDir) => { let _ = iter.next(); - Fd::open( + crate::fd::open( unsafe { CStr::from_bytes_with_nul_unchecked(b"..\0") }, OFlag::O_DIRECTORY, stat::Mode::empty(), @@ -90,7 +89,7 @@ fn create_path_do( } Some(Component::Normal(_)) => { // simply do not advance the iterator, heavy lifting happens in create_path_at_do() - Fd::cwd() + crate::fd::cwd() } None => bail!("create_path on empty path?"), }; @@ -99,7 +98,7 @@ fn create_path_do( } fn create_path_at_do( - mut at: Fd, + mut at: OwnedFd, mut iter: std::iter::Peekable, intermediate_opts: Option, final_opts: Option, @@ -112,7 +111,7 @@ fn create_path_at_do( None => return Ok(created), Some(Component::ParentDir) => { - at = Fd::openat( + at = crate::fd::openat( &at, unsafe { CStr::from_bytes_with_nul_unchecked(b"..\0") }, OFlag::O_DIRECTORY, @@ -138,7 +137,7 @@ fn create_path_at_do( Err(e) => return Err(e.into()), Ok(_) => true, }; - at = Fd::openat(&at, path, OFlag::O_DIRECTORY, stat::Mode::empty())?; + at = crate::fd::openat(&at, path, OFlag::O_DIRECTORY, stat::Mode::empty())?; if let (true, Some(opts)) = (created, opts) { if opts.owner.is_some() || opts.group.is_some() { diff --git a/proxmox-sys/src/lib.rs b/proxmox-sys/src/lib.rs index 00dadbd0..8a6b9ef3 100644 --- a/proxmox-sys/src/lib.rs +++ b/proxmox-sys/src/lib.rs @@ -32,6 +32,7 @@ macro_rules! identity { #[cfg(feature = "sortable-macro")] pub use proxmox_sortable_macro::sortable; +#[allow(deprecated)] use fd::Fd; /// Returns the hosts node name (UTS node name) @@ -57,6 +58,7 @@ pub fn nodename() -> &'static str { /// Safe wrapper for `nix::unistd::pipe2` defaulting to `O_CLOEXEC` /// and guarding the file descriptors. +#[allow(deprecated)] pub fn pipe() -> Result<(Fd, Fd), nix::Error> { let (pin, pout) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?; Ok((Fd(pin), Fd(pout))) diff --git a/proxmox-sys/src/linux/pid.rs b/proxmox-sys/src/linux/pid.rs index 9d708243..61ad8ca9 100644 --- a/proxmox-sys/src/linux/pid.rs +++ b/proxmox-sys/src/linux/pid.rs @@ -2,7 +2,7 @@ use std::fs::File; use std::io; -use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; use nix::fcntl::OFlag; use nix::sys::signal::Signal; @@ -14,7 +14,6 @@ use nix::NixPath; use proxmox_lang::{c_str, error::io_err_other}; use crate::error::SysResult; -use crate::fd::Fd; use crate::linux::procfs::{MountInfo, PidStat}; use crate::{c_result, c_try}; @@ -41,7 +40,7 @@ unsafe fn pidfd_send_signal( /// File descriptor refernce to a process. pub struct PidFd { - fd: Fd, + fd: OwnedFd, pid: Pid, } @@ -53,7 +52,7 @@ impl PidFd { /// Open a pidfd for the given process id. pub fn open(pid: Pid) -> io::Result { - let fd = unsafe { Fd::from_raw_fd(c_try!(pidfd_open(pid.as_raw(), 0)) as RawFd) }; + let fd = unsafe { OwnedFd::from_raw_fd(c_try!(pidfd_open(pid.as_raw(), 0)) as RawFd) }; Ok(Self { fd, pid }) } @@ -85,7 +84,7 @@ impl PidFd { /// Open a procfs file from. This is equivalent to opening `/proc//` using this /// process actual pid. This also works if the file descriptor has been sent over pub fn open_file(&self, path: &P) -> io::Result { - Fd::openat( + crate::fd::openat( self, path, OFlag::O_RDONLY | OFlag::O_CLOEXEC, @@ -124,7 +123,7 @@ impl PidFd { /// This will attempt to read the pid number via the file descriptor. pub fn try_from_raw_fd(fd: RawFd) -> io::Result { let mut this = Self { - fd: unsafe { Fd::from_raw_fd(fd) }, + fd: unsafe { OwnedFd::from_raw_fd(fd) }, pid: Pid::from_raw(1), }; // Simple check first: is it a valid pid file descriptor: diff --git a/proxmox-sys/src/linux/pty.rs b/proxmox-sys/src/linux/pty.rs index b62dc076..a8e8008f 100644 --- a/proxmox-sys/src/linux/pty.rs +++ b/proxmox-sys/src/linux/pty.rs @@ -10,8 +10,6 @@ use nix::sys::stat::Mode; use nix::unistd::{dup2, setsid}; use nix::{ioctl_write_int_bad, ioctl_write_ptr_bad, Result}; -use crate::fd::Fd; - ioctl_write_int_bad!(set_controlling_tty, libc::TIOCSCTTY); ioctl_write_ptr_bad!(set_size, libc::TIOCSWINSZ, nix::pty::Winsize); @@ -64,7 +62,7 @@ pub fn make_controlling_terminal(terminal: &str) -> Result<()> { | Mode::S_IWGRP | Mode::S_IROTH | Mode::S_IWOTH; // 0666 - let secondary_fd = Fd::open(terminal, OFlag::O_RDWR | OFlag::O_NOCTTY, mode)?; + let secondary_fd = crate::fd::open(terminal, OFlag::O_RDWR | OFlag::O_NOCTTY, mode)?; let s_raw_fd = secondary_fd.as_raw_fd(); unsafe { set_controlling_tty(s_raw_fd, 0) }?; dup2(s_raw_fd, 0)?; diff --git a/proxmox-sys/src/linux/tty.rs b/proxmox-sys/src/linux/tty.rs index 7795d7ec..fdea1629 100644 --- a/proxmox-sys/src/linux/tty.rs +++ b/proxmox-sys/src/linux/tty.rs @@ -1,6 +1,6 @@ use std::io::{self, Read, Write}; use std::mem::MaybeUninit; -use std::os::unix::io::AsRawFd; +use std::os::unix::io::{AsRawFd, OwnedFd}; use anyhow::{bail, format_err, Error}; use nix::fcntl::OFlag; @@ -9,7 +9,6 @@ use nix::sys::stat::Mode; use proxmox_lang::try_block; use crate::c_try; -use crate::fd::Fd; /// Get the current size of the terminal (for stdout). /// # Safety @@ -44,7 +43,7 @@ pub fn stdin_isatty() -> bool { pub enum TtyOutput { Stdout(std::io::Stdout), - DevTty(Fd), + DevTty(OwnedFd), } impl Write for TtyOutput { @@ -79,7 +78,7 @@ impl TtyOutput { if unsafe { libc::isatty(stdout.as_raw_fd()) } == 1 { Ok(Some(TtyOutput::Stdout(stdout))) } else { - match Fd::open( + match crate::fd::open( "/dev/tty", OFlag::O_WRONLY | OFlag::O_CLOEXEC | OFlag::O_NOCTTY, Mode::empty(),