Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call pipe2 directly on Linux #427

Merged
merged 4 commits into from
Sep 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
- `pipe2` now calls `libc::pipe2` where available. Previously it was emulated
using `pipe`, which meant that setting `O_CLOEXEC` was not atomic.
([#427](https://github.com/nix-rust/nix/pull/427))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

([#427](https://github.com/nix-rust/nix/pull/427))

## [0.7.0] 2016-09-09

### Added
Expand Down
39 changes: 30 additions & 9 deletions src/unistd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Standard symbolic constants and types
//!
use {Errno, Error, Result, NixPath};
use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};
use fcntl::{fcntl, OFlag, O_CLOEXEC, FD_CLOEXEC};
use fcntl::FcntlArg::F_SETFD;
use libc::{self, c_char, c_void, c_int, c_uint, size_t, pid_t, off_t, uid_t, gid_t, mode_t};
use std::mem;
use std::ffi::{CString, CStr, OsString};
Expand Down Expand Up @@ -360,21 +360,42 @@ pub fn pipe() -> Result<(RawFd, RawFd)> {
}
}

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

let res = libc::pipe(fds.as_mut_ptr());
let res = unsafe { libc::pipe2(fds.as_mut_ptr(), flags.bits()) };

try!(Errno::result(res));
try!(Errno::result(res));

Ok((fds[0], fds[1]))
}

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

Ok((fds[0], fds[1]))
}
let res = unsafe { libc::pipe(fds.as_mut_ptr()) };

try!(Errno::result(res));

try!(pipe2_setflags(fds[0], fds[1], flags));

Ok((fds[0], fds[1]))
}

#[cfg(not(any(target_os = "linux",
target_os = "android",
target_os = "emscripten")))]
fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> {
use fcntl::O_NONBLOCK;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you pull these use statements down from the file header?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is the only function that uses these imports, and it's no longer building on Linux, we get "unused import" warnings if we leave these at the top. Is there another good way to avoid those?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

use fcntl::FcntlArg::F_SETFL;

let mut res = Ok(0);

if flags.contains(O_CLOEXEC) {
Expand Down