Skip to content

Commit cf4b954

Browse files
committed
Use the real pipe2(2) on FreeBSD
FreeBSD added pipe2(2) in version 10.0.
1 parent 9f4db8a commit cf4b954

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/unistd.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,8 @@ pub fn pipe() -> Result<(RawFd, RawFd)> {
827827
// libc only defines `pipe2` in `libc::notbsd`.
828828
#[cfg(any(target_os = "linux",
829829
target_os = "android",
830-
target_os = "emscripten"))]
830+
target_os = "emscripten",
831+
target_os = "freebsd"))]
831832
pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
832833
let mut fds: [c_int; 2] = unsafe { mem::uninitialized() };
833834

@@ -840,7 +841,8 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
840841

841842
#[cfg(not(any(target_os = "linux",
842843
target_os = "android",
843-
target_os = "emscripten")))]
844+
target_os = "emscripten",
845+
target_os = "freebsd")))]
844846
pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
845847
let mut fds: [c_int; 2] = unsafe { mem::uninitialized() };
846848

@@ -855,7 +857,8 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
855857

856858
#[cfg(not(any(target_os = "linux",
857859
target_os = "android",
858-
target_os = "emscripten")))]
860+
target_os = "emscripten",
861+
target_os = "freebsd")))]
859862
fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> {
860863
use fcntl::O_NONBLOCK;
861864
use fcntl::FcntlArg::F_SETFL;

test/test_unistd.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate tempdir;
22

33
use nix::unistd::*;
44
use nix::unistd::ForkResult::*;
5+
use nix::fcntl;
56
use nix::sys::wait::*;
67
use nix::sys::stat;
78
use std::{env, iter};
@@ -273,3 +274,21 @@ fn test_sysconf_unsupported() {
273274
let open_max = sysconf(SysconfVar::_XOPEN_CRYPT);
274275
assert!(open_max.expect("sysconf failed").is_none())
275276
}
277+
278+
#[test]
279+
fn test_pipe() {
280+
let (fd0, fd1) = pipe().unwrap();
281+
let m0 = stat::SFlag::from_bits_truncate(stat::fstat(fd0).unwrap().st_mode);
282+
assert_eq!(m0, stat::S_IFIFO);
283+
let m1 = stat::SFlag::from_bits_truncate(stat::fstat(fd1).unwrap().st_mode);
284+
assert_eq!(m1, stat::S_IFIFO);
285+
}
286+
287+
#[test]
288+
fn test_pipe2() {
289+
let (fd0, fd1) = pipe2(fcntl::O_CLOEXEC).unwrap();
290+
let f0 = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd0, fcntl::F_GETFD).unwrap());
291+
assert!(f0.contains(fcntl::FD_CLOEXEC));
292+
let f1 = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd1, fcntl::F_GETFD).unwrap());
293+
assert!(f1.contains(fcntl::FD_CLOEXEC));
294+
}

0 commit comments

Comments
 (0)