Skip to content

Commit f50aa34

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

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3232
([#663](https://github.com/nix-rust/nix/pull/663))
3333

3434
### Changed
35+
- On FreeBSD, bind directly to `pipe2` instead of emulating it. Users should
36+
notice no difference. ([#777](https://github.com/nix-rust/nix/pull/777))
3537
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
3638
- Marked `sys::ptrace::ptrace` as `unsafe`.
3739
- Changed function signature of `socket()` and `socketpair()`. The `protocol` argument

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: 24 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,26 @@ 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 that we can create a pair of pipes. No need to verify that they pass
279+
// data; that's the domain of the OS, not nix.
280+
#[test]
281+
fn test_pipe() {
282+
let (fd0, fd1) = pipe().unwrap();
283+
let m0 = stat::SFlag::from_bits_truncate(stat::fstat(fd0).unwrap().st_mode);
284+
// S_IFIFO means it's a pipe
285+
assert_eq!(m0, stat::S_IFIFO);
286+
let m1 = stat::SFlag::from_bits_truncate(stat::fstat(fd1).unwrap().st_mode);
287+
assert_eq!(m1, stat::S_IFIFO);
288+
}
289+
290+
// pipe2(2) is the same as pipe(2), except it allows setting some flags. Check
291+
// that we can set a flag.
292+
#[test]
293+
fn test_pipe2() {
294+
let (fd0, fd1) = pipe2(fcntl::O_CLOEXEC).unwrap();
295+
let f0 = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd0, fcntl::F_GETFD).unwrap());
296+
assert!(f0.contains(fcntl::FD_CLOEXEC));
297+
let f1 = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd1, fcntl::F_GETFD).unwrap());
298+
assert!(f1.contains(fcntl::FD_CLOEXEC));
299+
}

0 commit comments

Comments
 (0)