From: Ian Jackson Date: Sun, 25 Feb 2024 02:06:00 +0000 (+0000) Subject: nix: Provide a compat version of pipe for 0.28 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=494632bd10d605725f499eac4dbb3790fe8e3f9e;p=hippotat.git nix: Provide a compat version of pipe for 0.28 Signed-off-by: Ian Jackson --- diff --git a/server/daemon.rs b/server/daemon.rs index 45a8f81..48b664a 100644 --- a/server/daemon.rs +++ b/server/daemon.rs @@ -154,7 +154,7 @@ impl Daemoniser { .context("open /dev/null"); mdup2(null_fd, 0, "null onto stdin"); - let (st_rfd, st_wfd) = pipe().context("pipe"); + let (st_rfd, st_wfd) = compat::pipe().context("pipe"); match fork().context("fork (1)") { ForkResult::Child => { } diff --git a/src/compat.rs b/src/compat.rs index f02d981..220ab11 100644 --- a/src/compat.rs +++ b/src/compat.rs @@ -1,4 +1,6 @@ +#![allow(unused_imports)] +use std::os::fd::IntoRawFd; use cfg_if::cfg_if; use crate::prelude::*; @@ -35,3 +37,19 @@ pub unsafe fn write(fd: c_int, buf: &[u8]) -> nix::Result { buf, ) } + +/// Version of [`nix::unistd::pipe`] with a fixed type for the fd +// +/// * nix <=0.27 returns a pair of `c_int` +/// * nix >=0.28 returns a pair of `OwnedFd` +pub fn pipe() -> nix::Result<(c_int, c_int)> { + let (a, b) = nix::unistd::pipe()?; + let map = |fd| { cfg_if! { + if #[cfg(nix_ge_0_28)] { + OwnedFd::into_raw_fd(fd) + } else { + fd + } + }}; + Ok((map(a), map(b))) +}