Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement From<RawFd> for Stdio #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions daemonize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use std::fs::File;
use std::mem::transmute;
use std::os::unix::ffi::OsStringExt;
use std::os::unix::io::AsRawFd;
use std::os::unix::io::RawFd;
use std::path::{Path, PathBuf};
use std::process::exit;

Expand Down Expand Up @@ -135,6 +136,7 @@ impl From<u32> for Mask {
enum StdioImpl {
Devnull,
RedirectToFile(File),
RedirectToRawFd(RawFd),
Keep,
}

Expand Down Expand Up @@ -166,6 +168,14 @@ impl From<File> for Stdio {
}
}

impl From<RawFd> for Stdio {
fn from(fd: RawFd) -> Self {
Self {
inner: StdioImpl::RedirectToRawFd(fd),
}
}
}
Comment on lines +171 to +177

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to look at preferring the safe equivalents that are available now? sunfishcode/io-lifetimes#38

If a type implements AsRawFd, FromRawFd, and/or IntoRawFd, it usually wants AsFd, From<OwnedFd>, and/or From<T> for OwnedFd, respectively.

Since Rust 1.63.0 (Aug 2022), OwnedFd became available, which provides as_fd() and as_raw_fd() if you need to get BorrowedFd or RawFd types (original RFC for further context).

Suggested change
impl From<RawFd> for Stdio {
fn from(fd: RawFd) -> Self {
Self {
inner: StdioImpl::RedirectToRawFd(fd),
}
}
}
impl From<OwnedFd> for Stdio {
fn from(fd: OwnedFd) -> Self {
Self {
inner: StdioImpl::RedirectToFd(fd),
}
}
}
  • You would also need to change the import from use std::os::unix::io::RawFd; to use std::os::fd::OwnedFd;
  • There's also a AsRawFd import and a as_raw_fd() call for the RedirectToFile enum variant, that can get an OwnedFd since 1.63.0 in a similar way.

That said, I'm not quite sure why Stdio struct exists here instead of using the std::process::Stdio one? It already supports From for file and fd, while there is a separate function call null() for getting the equivalent /dev/null and the Keep enum variant seems to be for a No-op.

Ah.. so this is for redirection of the streams handled in the redirect_standard_streams() call further down. So at the time Rust didn't have an equivalent built-in for Stdio but it does now since Rust 1.74 (Nov 2023). So if raising the MSRV were viable, one could switch to that to simplify 😎


/// Parent process execution outcome.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
Expand Down Expand Up @@ -457,6 +467,9 @@ unsafe fn redirect_standard_streams(
let raw_fd = file.as_raw_fd();
check_err(libc::dup2(raw_fd, fd), ErrorKind::RedirectStreams)?;
}
StdioImpl::RedirectToRawFd(raw_fd) => {
check_err(libc::dup2(raw_fd, fd), ErrorKind::RedirectStreams)?;
}
StdioImpl::Keep => (),
};
Ok(())
Expand Down