+#![allow(deprecated)]
+
use std::marker::PhantomData;
use std::os::unix::io::{AsRawFd, RawFd};
/// `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
}
}
+#[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<P>(path: &P, oflag: OFlag, mode: Mode) -> Result<Self, nix::Error>
where
P: ?Sized + NixPath,
nix::fcntl::open(path, oflag, mode).map(Self)
}
+ #[deprecated(note = "use proxmox_sys::fd::openat instead")]
pub fn openat<D, P>(dirfd: &D, path: &P, oflag: OFlag, mode: Mode) -> Result<Self, nix::Error>
where
D: AsRawFd,
}
}
+#[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;
}
}
+#[allow(deprecated)]
impl AsRef<FdRef> for Fd {
fn as_ref(&self) -> &FdRef {
self.as_fd_ref()
}
}
+#[allow(deprecated)]
impl Borrow<FdRef> for Fd {
fn borrow(&self) -> &FdRef {
self.as_fd_ref()
}
}
+#[allow(deprecated)]
impl std::ops::Deref for Fd {
type Target = FdRef;
//! 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::*;
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<P>(path: &P, oflag: OFlag, mode: Mode) -> Result<OwnedFd, nix::Error>
+where
+ P: ?Sized + NixPath,
+{
+ nix::fcntl::open(path, oflag, mode).map(|fd| unsafe { OwnedFd::from_raw_fd(fd) })
+}
+
+pub fn openat<D, P>(dirfd: &D, path: &P, oflag: OFlag, mode: Mode) -> Result<OwnedFd, nix::Error>
+where
+ D: AsRawFd,
+ P: ?Sized + NixPath,
+{
+ nix::fcntl::openat(dirfd.as_raw_fd(), path, oflag, mode)
+ .map(|fd| unsafe { OwnedFd::from_raw_fd(fd) })
+}
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};
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.
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(),
}
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(),
}
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?"),
};
}
fn create_path_at_do(
- mut at: Fd,
+ mut at: OwnedFd,
mut iter: std::iter::Peekable<std::path::Components>,
intermediate_opts: Option<CreateOptions>,
final_opts: Option<CreateOptions>,
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,
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() {
#[cfg(feature = "sortable-macro")]
pub use proxmox_sortable_macro::sortable;
+#[allow(deprecated)]
use fd::Fd;
/// Returns the hosts node name (UTS node name)
/// 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)))
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;
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};
/// File descriptor refernce to a process.
pub struct PidFd {
- fd: Fd,
+ fd: OwnedFd,
pid: Pid,
}
/// Open a pidfd for the given process id.
pub fn open(pid: Pid) -> io::Result<Self> {
- 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 })
}
/// Open a procfs file from. This is equivalent to opening `/proc/<pid>/<file>` using this
/// process actual pid. This also works if the file descriptor has been sent over
pub fn open_file<P: ?Sized + NixPath>(&self, path: &P) -> io::Result<File> {
- Fd::openat(
+ crate::fd::openat(
self,
path,
OFlag::O_RDONLY | OFlag::O_CLOEXEC,
/// This will attempt to read the pid number via the file descriptor.
pub fn try_from_raw_fd(fd: RawFd) -> io::Result<Self> {
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:
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);
| 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)?;
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;
use proxmox_lang::try_block;
use crate::c_try;
-use crate::fd::Fd;
/// Get the current size of the terminal (for stdout).
/// # Safety
pub enum TtyOutput {
Stdout(std::io::Stdout),
- DevTty(Fd),
+ DevTty(OwnedFd),
}
impl Write for 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(),