1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![unstable(reason = "not public", issue = "0", feature = "fd")]
16 use libc::{self, c_int, size_t, c_void};
18 use sync::atomic::{AtomicBool, Ordering};
20 use sys_common::AsInner;
21 use sys_common::io::read_to_end_uninitialized;
28 pub fn new(fd: c_int) -> FileDesc {
32 pub fn raw(&self) -> c_int { self.fd }
34 /// Extracts the actual filedescriptor without closing it.
35 pub fn into_raw(self) -> c_int {
41 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
42 let ret = cvt(unsafe {
44 buf.as_mut_ptr() as *mut c_void,
50 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
52 (&mut me).read_to_end(buf)
55 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
56 let ret = cvt(unsafe {
58 buf.as_ptr() as *const c_void,
64 #[cfg(not(any(target_env = "newlib", target_os = "solaris", target_os = "emscripten")))]
65 pub fn set_cloexec(&self) -> io::Result<()> {
67 cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
71 #[cfg(any(target_env = "newlib", target_os = "solaris", target_os = "emscripten"))]
72 pub fn set_cloexec(&self) -> io::Result<()> {
74 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
75 cvt(libc::fcntl(self.fd, libc::F_SETFD, previous | libc::FD_CLOEXEC))?;
80 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
82 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
83 let new = if nonblocking {
84 previous | libc::O_NONBLOCK
86 previous & !libc::O_NONBLOCK
88 cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
93 pub fn duplicate(&self) -> io::Result<FileDesc> {
94 // We want to atomically duplicate this file descriptor and set the
95 // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
96 // flag, however, isn't supported on older Linux kernels (earlier than
99 // To detect this and ensure that CLOEXEC is still set, we
100 // follow a strategy similar to musl [1] where if passing
101 // F_DUPFD_CLOEXEC causes `fcntl` to return EINVAL it means it's not
102 // supported (the third parameter, 0, is always valid), so we stop
105 // Also note that Android doesn't have F_DUPFD_CLOEXEC, but get it to
106 // resolve so we at least compile this.
108 // [1]: http://comments.gmane.org/gmane.linux.lib.musl.general/2963
109 #[cfg(target_os = "android")]
110 use libc::F_DUPFD as F_DUPFD_CLOEXEC;
111 #[cfg(not(target_os = "android"))]
112 use libc::F_DUPFD_CLOEXEC;
114 let make_filedesc = |fd| {
115 let fd = FileDesc::new(fd);
119 static TRY_CLOEXEC: AtomicBool =
120 AtomicBool::new(!cfg!(target_os = "android"));
122 if TRY_CLOEXEC.load(Ordering::Relaxed) {
123 match cvt(unsafe { libc::fcntl(fd, F_DUPFD_CLOEXEC, 0) }) {
124 // We *still* call the `set_cloexec` method as apparently some
125 // linux kernel at some point stopped setting CLOEXEC even
126 // though it reported doing so on F_DUPFD_CLOEXEC.
128 return Ok(if cfg!(target_os = "linux") {
134 Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {
135 TRY_CLOEXEC.store(false, Ordering::Relaxed);
137 Err(e) => return Err(e),
140 cvt(unsafe { libc::fcntl(fd, libc::F_DUPFD, 0) }).and_then(make_filedesc)
144 impl<'a> Read for &'a FileDesc {
145 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
149 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
150 unsafe { read_to_end_uninitialized(self, buf) }
154 impl AsInner<c_int> for FileDesc {
155 fn as_inner(&self) -> &c_int { &self.fd }
158 impl Drop for FileDesc {
160 // Note that errors are ignored when closing a file descriptor. The
161 // reason for this is that if an error occurs we don't actually know if
162 // the file descriptor was closed or not, and if we retried (for
163 // something like EINTR), we might close another valid file descriptor
164 // (opened after we closed ours.
165 let _ = unsafe { libc::close(self.fd) };