Skip to content

Commit 36ea95d

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 1b9d205 commit 36ea95d

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
@@ -38,6 +38,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3838
([#602](https://github.com/nix-rust/nix/pull/774))
3939

4040
### Changed
41+
- Remove obsolete `pipe2` emulation. All supported platforms support `pipe2`
42+
natively. Users should notice no difference.
43+
([#777](https://github.com/nix-rust/nix/pull/777))
4144
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
4245
- Marked `sys::ptrace::ptrace` as `unsafe`.
4346
- 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
@@ -855,10 +855,6 @@ pub fn pipe() -> Result<(RawFd, RawFd)> {
855855
}
856856
}
857857

858-
// libc only defines `pipe2` in `libc::notbsd`.
859-
#[cfg(any(target_os = "linux",
860-
target_os = "android",
861-
target_os = "emscripten"))]
862858
pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
863859
let mut fds: [c_int; 2] = unsafe { mem::uninitialized() };
864860

@@ -869,52 +865,6 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
869865
Ok((fds[0], fds[1]))
870866
}
871867

872-
#[cfg(not(any(target_os = "linux",
873-
target_os = "android",
874-
target_os = "emscripten")))]
875-
pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
876-
let mut fds: [c_int; 2] = unsafe { mem::uninitialized() };
877-
878-
let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
879-
880-
try!(Errno::result(res));
881-
882-
try!(pipe2_setflags(fds[0], fds[1], flags));
883-
884-
Ok((fds[0], fds[1]))
885-
}
886-
887-
#[cfg(not(any(target_os = "linux",
888-
target_os = "android",
889-
target_os = "emscripten")))]
890-
fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> {
891-
use fcntl::O_NONBLOCK;
892-
use fcntl::FcntlArg::F_SETFL;
893-
894-
let mut res = Ok(0);
895-
896-
if flags.contains(O_CLOEXEC) {
897-
res = res
898-
.and_then(|_| fcntl(fd1, F_SETFD(FD_CLOEXEC)))
899-
.and_then(|_| fcntl(fd2, F_SETFD(FD_CLOEXEC)));
900-
}
901-
902-
if flags.contains(O_NONBLOCK) {
903-
res = res
904-
.and_then(|_| fcntl(fd1, F_SETFL(O_NONBLOCK)))
905-
.and_then(|_| fcntl(fd2, F_SETFL(O_NONBLOCK)));
906-
}
907-
908-
match res {
909-
Ok(_) => Ok(()),
910-
Err(e) => {
911-
let _ = close(fd1);
912-
let _ = close(fd2);
913-
Err(e)
914-
}
915-
}
916-
}
917-
918868
/// Truncate a file to a specified length
919869
///
920870
/// See also

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};
@@ -291,3 +292,26 @@ fn test_sysconf_unsupported() {
291292
let open_max = sysconf(SysconfVar::_XOPEN_CRYPT);
292293
assert!(open_max.expect("sysconf failed").is_none())
293294
}
295+
296+
// Test that we can create a pair of pipes. No need to verify that they pass
297+
// data; that's the domain of the OS, not nix.
298+
#[test]
299+
fn test_pipe() {
300+
let (fd0, fd1) = pipe().unwrap();
301+
let m0 = stat::SFlag::from_bits_truncate(stat::fstat(fd0).unwrap().st_mode);
302+
// S_IFIFO means it's a pipe
303+
assert_eq!(m0, stat::S_IFIFO);
304+
let m1 = stat::SFlag::from_bits_truncate(stat::fstat(fd1).unwrap().st_mode);
305+
assert_eq!(m1, stat::S_IFIFO);
306+
}
307+
308+
// pipe2(2) is the same as pipe(2), except it allows setting some flags. Check
309+
// that we can set a flag.
310+
#[test]
311+
fn test_pipe2() {
312+
let (fd0, fd1) = pipe2(fcntl::O_CLOEXEC).unwrap();
313+
let f0 = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd0, fcntl::F_GETFD).unwrap());
314+
assert!(f0.contains(fcntl::FD_CLOEXEC));
315+
let f1 = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd1, fcntl::F_GETFD).unwrap());
316+
assert!(f1.contains(fcntl::FD_CLOEXEC));
317+
}

0 commit comments

Comments
 (0)