rest-server: update to OwnedFd

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2022-10-19 12:13:03 +02:00
parent 8420c266af
commit 8bd961acdc
2 changed files with 15 additions and 14 deletions

View File

@ -5,7 +5,7 @@ use std::future::Future;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::os::raw::{c_char, c_int, c_uchar}; use std::os::raw::{c_char, c_int, c_uchar};
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use std::panic::UnwindSafe; use std::panic::UnwindSafe;
use std::path::PathBuf; use std::path::PathBuf;
@ -14,7 +14,7 @@ use futures::future::{self, Either};
use nix::unistd::{fork, ForkResult}; use nix::unistd::{fork, ForkResult};
use proxmox_io::{ReadExt, WriteExt}; use proxmox_io::{ReadExt, WriteExt};
use proxmox_sys::fd::{fd_change_cloexec, Fd}; use proxmox_sys::fd::fd_change_cloexec;
use proxmox_sys::fs::CreateOptions; use proxmox_sys::fs::CreateOptions;
// Unfortunately FnBox is nightly-only and Box<FnOnce> is unusable, so just use Box<Fn>... // Unfortunately FnBox is nightly-only and Box<FnOnce> is unusable, so just use Box<Fn>...
@ -103,8 +103,7 @@ impl Reloader {
// At this point we call pre-exec helpers. We must be certain that if they fail for // At this point we call pre-exec helpers. We must be certain that if they fail for
// whatever reason we can still call `_exit()`, so use catch_unwind. // whatever reason we can still call `_exit()`, so use catch_unwind.
match std::panic::catch_unwind(move || { match std::panic::catch_unwind(move || {
let mut pnew = let mut pnew = std::fs::File::from(pnew);
unsafe { std::fs::File::from_raw_fd(pnew.into_raw_fd()) };
let pid = nix::unistd::Pid::this(); let pid = nix::unistd::Pid::this();
if let Err(e) = unsafe { pnew.write_host_value(pid.as_raw()) } { if let Err(e) = unsafe { pnew.write_host_value(pid.as_raw()) } {
log::error!("failed to send new server PID to parent: {}", e); log::error!("failed to send new server PID to parent: {}", e);
@ -131,7 +130,7 @@ impl Reloader {
let fd = let fd =
unsafe { sd_journal_stream_fd(ident.as_ptr(), libc::LOG_INFO, 1) }; unsafe { sd_journal_stream_fd(ident.as_ptr(), libc::LOG_INFO, 1) };
if fd >= 0 && fd != 1 { if fd >= 0 && fd != 1 {
let fd = proxmox_sys::fd::Fd(fd); // add drop handler let fd = unsafe { OwnedFd::from_raw_fd(fd) }; // add drop handler
nix::unistd::dup2(fd.as_raw_fd(), 1)?; nix::unistd::dup2(fd.as_raw_fd(), 1)?;
} else { } else {
log::error!("failed to update STDOUT journal redirection ({})", fd); log::error!("failed to update STDOUT journal redirection ({})", fd);
@ -139,7 +138,7 @@ impl Reloader {
let fd = let fd =
unsafe { sd_journal_stream_fd(ident.as_ptr(), libc::LOG_ERR, 1) }; unsafe { sd_journal_stream_fd(ident.as_ptr(), libc::LOG_ERR, 1) };
if fd >= 0 && fd != 2 { if fd >= 0 && fd != 2 {
let fd = proxmox_sys::fd::Fd(fd); // add drop handler let fd = unsafe { OwnedFd::from_raw_fd(fd) }; // add drop handler
nix::unistd::dup2(fd.as_raw_fd(), 2)?; nix::unistd::dup2(fd.as_raw_fd(), 2)?;
} else { } else {
log::error!("failed to update STDERR journal redirection ({})", fd); log::error!("failed to update STDERR journal redirection ({})", fd);
@ -167,7 +166,7 @@ impl Reloader {
child child
); );
std::mem::drop(pnew); std::mem::drop(pnew);
let mut pold = unsafe { std::fs::File::from_raw_fd(pold.into_raw_fd()) }; let mut pold = std::fs::File::from(pold);
let child = nix::unistd::Pid::from_raw(match unsafe { pold.read_le_value() } { let child = nix::unistd::Pid::from_raw(match unsafe { pold.read_le_value() } {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
@ -229,10 +228,12 @@ impl Reloadable for tokio::net::TcpListener {
// FIXME: We could become "independent" of the TcpListener and its reference to the file // FIXME: We could become "independent" of the TcpListener and its reference to the file
// descriptor by `dup()`ing it (and check if the listener still exists via kcmp()?) // descriptor by `dup()`ing it (and check if the listener still exists via kcmp()?)
fn get_store_func(&self) -> Result<BoxedStoreFunc, Error> { fn get_store_func(&self) -> Result<BoxedStoreFunc, Error> {
let mut fd_opt = Some(Fd(nix::fcntl::fcntl( let mut fd_opt = Some(unsafe {
self.as_raw_fd(), OwnedFd::from_raw_fd(nix::fcntl::fcntl(
nix::fcntl::FcntlArg::F_DUPFD_CLOEXEC(0), self.as_raw_fd(),
)?)); nix::fcntl::FcntlArg::F_DUPFD_CLOEXEC(0),
)?)
});
Ok(Box::new(move || { Ok(Box::new(move || {
let fd = fd_opt.take().unwrap(); let fd = fd_opt.take().unwrap();
fd_change_cloexec(fd.as_raw_fd(), false)?; fd_change_cloexec(fd.as_raw_fd(), false)?;

View File

@ -16,6 +16,7 @@
//! * generic interface to authenticate user //! * generic interface to authenticate user
use std::future::Future; use std::future::Future;
use std::os::unix::io::{FromRawFd, OwnedFd};
use std::pin::Pin; use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
@ -26,7 +27,6 @@ use hyper::{Body, Method, Response};
use nix::unistd::Pid; use nix::unistd::Pid;
use proxmox_router::UserInformation; use proxmox_router::UserInformation;
use proxmox_sys::fd::Fd;
use proxmox_sys::fs::CreateOptions; use proxmox_sys::fs::CreateOptions;
use proxmox_sys::linux::procfs::PidStat; use proxmox_sys::linux::procfs::PidStat;
@ -175,7 +175,7 @@ pub fn fail_on_shutdown() -> Result<(), Error> {
/// safe wrapper for `nix::sys::socket::socketpair` defaulting to `O_CLOEXEC` and guarding the file /// safe wrapper for `nix::sys::socket::socketpair` defaulting to `O_CLOEXEC` and guarding the file
/// descriptors. /// descriptors.
pub fn socketpair() -> Result<(Fd, Fd), Error> { pub fn socketpair() -> Result<(OwnedFd, OwnedFd), Error> {
use nix::sys::socket; use nix::sys::socket;
let (pa, pb) = socket::socketpair( let (pa, pb) = socket::socketpair(
socket::AddressFamily::Unix, socket::AddressFamily::Unix,
@ -183,7 +183,7 @@ pub fn socketpair() -> Result<(Fd, Fd), Error> {
None, None,
socket::SockFlag::SOCK_CLOEXEC, socket::SockFlag::SOCK_CLOEXEC,
)?; )?;
Ok((Fd(pa), Fd(pb))) Ok(unsafe { (OwnedFd::from_raw_fd(pa), OwnedFd::from_raw_fd(pb)) })
} }
/// Extract a specific cookie from cookie header. /// Extract a specific cookie from cookie header.