chiark / gitweb /
nix: Provide a compat version of pipe for 0.28
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 25 Feb 2024 02:06:00 +0000 (02:06 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 25 Feb 2024 17:40:44 +0000 (17:40 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
server/daemon.rs
src/compat.rs

index 45a8f81d626e78a4e806bad08b399e20fe6bd0ef..48b664a6f31ea1b87c8272f16faf7234e489c5fd 100644 (file)
@@ -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 => { }
index f02d9818384173e9951027d4d4f38f72174fd53d..220ab11be7250aa1494a88223e2d28c2acd24913 100644 (file)
@@ -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<usize> {
     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)))
+}