Skip to content

Commit 474f42e

Browse files
committed
Eliminate obsolete pipe2 emulation.
All supported platforms now use the native syscall. It was added in: * DragonflyBSD 4.2 * FreeBSD 10.0 * NetBSD 6.0 * OpenBSD 5.7
1 parent 9f4db8a commit 474f42e

File tree

3 files changed

+27
-50
lines changed

3 files changed

+27
-50
lines changed

CHANGELOG.md

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

3434
### Changed
35+
- Remove obsolete `pipe2` emulation. All supported platforms support `pipe2`
36+
natively. Users should notice no difference.
37+
([#777](https://github.com/nix-rust/nix/pull/777))
3538
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
3639
- Marked `sys::ptrace::ptrace` as `unsafe`.
3740
- Changed function signature of `socket()` and `socketpair()`. The `protocol` argument

src/unistd.rs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,6 @@ pub fn pipe() -> Result<(RawFd, RawFd)> {
824824
}
825825
}
826826

827-
// libc only defines `pipe2` in `libc::notbsd`.
828-
#[cfg(any(target_os = "linux",
829-
target_os = "android",
830-
target_os = "emscripten"))]
831827
pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
832828
let mut fds: [c_int; 2] = unsafe { mem::uninitialized() };
833829

@@ -838,52 +834,6 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
838834
Ok((fds[0], fds[1]))
839835
}
840836

841-
#[cfg(not(any(target_os = "linux",
842-
target_os = "android",
843-
target_os = "emscripten")))]
844-
pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
845-
let mut fds: [c_int; 2] = unsafe { mem::uninitialized() };
846-
847-
let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
848-
849-
try!(Errno::result(res));
850-
851-
try!(pipe2_setflags(fds[0], fds[1], flags));
852-
853-
Ok((fds[0], fds[1]))
854-
}
855-
856-
#[cfg(not(any(target_os = "linux",
857-
target_os = "android",
858-
target_os = "emscripten")))]
859-
fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> {
860-
use fcntl::O_NONBLOCK;
861-
use fcntl::FcntlArg::F_SETFL;
862-
863-
let mut res = Ok(0);
864-
865-
if flags.contains(O_CLOEXEC) {
866-
res = res
867-
.and_then(|_| fcntl(fd1, F_SETFD(FD_CLOEXEC)))
868-
.and_then(|_| fcntl(fd2, F_SETFD(FD_CLOEXEC)));
869-
}
870-
871-
if flags.contains(O_NONBLOCK) {
872-
res = res
873-
.and_then(|_| fcntl(fd1, F_SETFL(O_NONBLOCK)))
874-
.and_then(|_| fcntl(fd2, F_SETFL(O_NONBLOCK)));
875-
}
876-
877-
match res {
878-
Ok(_) => Ok(()),
879-
Err(e) => {
880-
let _ = close(fd1);
881-
let _ = close(fd2);
882-
Err(e)
883-
}
884-
}
885-
}
886-
887837
pub fn ftruncate(fd: RawFd, len: off_t) -> Result<()> {
888838
Errno::result(unsafe { libc::ftruncate(fd, len) }).map(drop)
889839
}

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)