Skip to content

Commit

Permalink
Use the real pipe2(2) on FreeBSD
Browse files Browse the repository at this point in the history
FreeBSD added pipe2(2) in version 10.0.
  • Loading branch information
asomers committed Oct 8, 2017
1 parent 9f4db8a commit cf4b954
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,8 @@ pub fn pipe() -> Result<(RawFd, RawFd)> {
// libc only defines `pipe2` in `libc::notbsd`.
#[cfg(any(target_os = "linux",
target_os = "android",
target_os = "emscripten"))]
target_os = "emscripten",
target_os = "freebsd"))]
pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
let mut fds: [c_int; 2] = unsafe { mem::uninitialized() };

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

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

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

#[cfg(not(any(target_os = "linux",
target_os = "android",
target_os = "emscripten")))]
target_os = "emscripten",
target_os = "freebsd")))]
fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> {
use fcntl::O_NONBLOCK;
use fcntl::FcntlArg::F_SETFL;
Expand Down
19 changes: 19 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate tempdir;

use nix::unistd::*;
use nix::unistd::ForkResult::*;
use nix::fcntl;
use nix::sys::wait::*;
use nix::sys::stat;
use std::{env, iter};
Expand Down Expand Up @@ -273,3 +274,21 @@ fn test_sysconf_unsupported() {
let open_max = sysconf(SysconfVar::_XOPEN_CRYPT);
assert!(open_max.expect("sysconf failed").is_none())
}

#[test]
fn test_pipe() {
let (fd0, fd1) = pipe().unwrap();
let m0 = stat::SFlag::from_bits_truncate(stat::fstat(fd0).unwrap().st_mode);
assert_eq!(m0, stat::S_IFIFO);
let m1 = stat::SFlag::from_bits_truncate(stat::fstat(fd1).unwrap().st_mode);
assert_eq!(m1, stat::S_IFIFO);
}

#[test]
fn test_pipe2() {
let (fd0, fd1) = pipe2(fcntl::O_CLOEXEC).unwrap();
let f0 = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd0, fcntl::F_GETFD).unwrap());
assert!(f0.contains(fcntl::FD_CLOEXEC));
let f1 = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd1, fcntl::F_GETFD).unwrap());
assert!(f1.contains(fcntl::FD_CLOEXEC));
}

0 comments on commit cf4b954

Please sign in to comment.