      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302

    #![unstable(reason = "not public", issue = "0", feature = "fd")]

    use crate::cmp;
    use crate::io::{self, Read, Initializer, IoSlice, IoSliceMut};
    use crate::mem;
    use crate::sync::atomic::{AtomicBool, Ordering};
    use crate::sys::cvt;
    use crate::sys_common::AsInner;

    use libc::{c_int, c_void, ssize_t};

    #[derive(Debug)]
    pub struct FileDesc {
        fd: c_int,
    }

    fn max_len() -> usize {
        // The maximum read limit on most posix-like systems is `SSIZE_MAX`,
        // with the man page quoting that if the count of bytes to read is
        // greater than `SSIZE_MAX` the result is "unspecified".
        //
        // On macOS, however, apparently the 64-bit libc is either buggy or
        // intentionally showing odd behavior by rejecting any read with a size
        // larger than or equal to INT_MAX. To handle both of these the read
        // size is capped on both platforms.
        if cfg!(target_os = "macos") {
            <c_int>::max_value() as usize - 1
        } else {
            <ssize_t>::max_value() as usize
        }
    }

    impl FileDesc {
        pub fn new(fd: c_int) -> FileDesc {
            FileDesc { fd }
        }

        pub fn raw(&self) -> c_int { self.fd }

        /// Extracts the actual file descriptor without closing it.
        pub fn into_raw(self) -> c_int {
            let fd = self.fd;
            mem::forget(self);
            fd
        }

        pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
            let ret = cvt(unsafe {
                libc::read(self.fd,
                           buf.as_mut_ptr() as *mut c_void,
                           cmp::min(buf.len(), max_len()))
            })?;
            Ok(ret as usize)
        }

        pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
            let ret = cvt(unsafe {
                libc::readv(self.fd,
                            bufs.as_ptr() as *const libc::iovec,
                            cmp::min(bufs.len(), c_int::max_value() as usize) as c_int)
            })?;
            Ok(ret as usize)
        }

        pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
            let mut me = self;
            (&mut me).read_to_end(buf)
        }

        pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
            #[cfg(target_os = "android")]
            use super::android::cvt_pread64;

            #[cfg(target_os = "emscripten")]
            unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: usize, offset: i64)
                -> io::Result<isize>
            {
                use crate::convert::TryInto;
                use libc::pread64;
                // pread64 on emscripten actually takes a 32 bit offset
                if let Ok(o) = offset.try_into() {
                    cvt(pread64(fd, buf, count, o))
                } else {
                    Err(io::Error::new(io::ErrorKind::InvalidInput,
                                       "cannot pread >2GB"))
                }
            }

            #[cfg(not(any(target_os = "android", target_os = "emscripten")))]
            unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: usize, offset: i64)
                -> io::Result<isize>
            {
                #[cfg(target_os = "linux")]
                use libc::pread64;
                #[cfg(not(target_os = "linux"))]
                use libc::pread as pread64;
                cvt(pread64(fd, buf, count, offset))
            }

            unsafe {
                cvt_pread64(self.fd,
                            buf.as_mut_ptr() as *mut c_void,
                            cmp::min(buf.len(), max_len()),
                            offset as i64)
                    .map(|n| n as usize)
            }
        }

        pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
            let ret = cvt(unsafe {
                libc::write(self.fd,
                            buf.as_ptr() as *const c_void,
                            cmp::min(buf.len(), max_len()))
            })?;
            Ok(ret as usize)
        }

        pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
            let ret = cvt(unsafe {
                libc::writev(self.fd,
                             bufs.as_ptr() as *const libc::iovec,
                             cmp::min(bufs.len(), c_int::max_value() as usize) as c_int)
            })?;
            Ok(ret as usize)
        }

        pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
            #[cfg(target_os = "android")]
            use super::android::cvt_pwrite64;

            #[cfg(target_os = "emscripten")]
            unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: usize, offset: i64)
                -> io::Result<isize>
            {
                use crate::convert::TryInto;
                use libc::pwrite64;
                // pwrite64 on emscripten actually takes a 32 bit offset
                if let Ok(o) = offset.try_into() {
                    cvt(pwrite64(fd, buf, count, o))
                } else {
                    Err(io::Error::new(io::ErrorKind::InvalidInput,
                                       "cannot pwrite >2GB"))
                }
            }

            #[cfg(not(any(target_os = "android", target_os = "emscripten")))]
            unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: usize, offset: i64)
                -> io::Result<isize>
            {
                #[cfg(target_os = "linux")]
                use libc::pwrite64;
                #[cfg(not(target_os = "linux"))]
                use libc::pwrite as pwrite64;
                cvt(pwrite64(fd, buf, count, offset))
            }

            unsafe {
                cvt_pwrite64(self.fd,
                             buf.as_ptr() as *const c_void,
                             cmp::min(buf.len(), max_len()),
                             offset as i64)
                    .map(|n| n as usize)
            }
        }

        #[cfg(target_os = "linux")]
        pub fn get_cloexec(&self) -> io::Result<bool> {
            unsafe {
                Ok((cvt(libc::fcntl(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0)
            }
        }

        #[cfg(not(any(target_env = "newlib",
                      target_os = "solaris",
                      target_os = "emscripten",
                      target_os = "fuchsia",
                      target_os = "l4re",
                      target_os = "haiku")))]
        pub fn set_cloexec(&self) -> io::Result<()> {
            unsafe {
                cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
                Ok(())
            }
        }
        #[cfg(any(target_env = "newlib",
                  target_os = "solaris",
                  target_os = "emscripten",
                  target_os = "fuchsia",
                  target_os = "l4re",
                  target_os = "haiku"))]
        pub fn set_cloexec(&self) -> io::Result<()> {
            unsafe {
                let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
                let new = previous | libc::FD_CLOEXEC;
                if new != previous {
                    cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
                }
                Ok(())
            }
        }

        #[cfg(target_os = "linux")]
        pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
            unsafe {
                let v = nonblocking as c_int;
                cvt(libc::ioctl(self.fd, libc::FIONBIO, &v))?;
                Ok(())
            }
        }

        #[cfg(not(target_os = "linux"))]
        pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
            unsafe {
                let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
                let new = if nonblocking {
                    previous | libc::O_NONBLOCK
                } else {
                    previous & !libc::O_NONBLOCK
                };
                if new != previous {
                    cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
                }
                Ok(())
            }
        }

        pub fn duplicate(&self) -> io::Result<FileDesc> {
            // We want to atomically duplicate this file descriptor and set the
            // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
            // flag, however, isn't supported on older Linux kernels (earlier than
            // 2.6.24).
            //
            // To detect this and ensure that CLOEXEC is still set, we
            // follow a strategy similar to musl [1] where if passing
            // F_DUPFD_CLOEXEC causes `fcntl` to return EINVAL it means it's not
            // supported (the third parameter, 0, is always valid), so we stop
            // trying that.
            //
            // Also note that Android doesn't have F_DUPFD_CLOEXEC, but get it to
            // resolve so we at least compile this.
            //
            // [1]: http://comments.gmane.org/gmane.linux.lib.musl.general/2963
            #[cfg(any(target_os = "android", target_os = "haiku"))]
            use libc::F_DUPFD as F_DUPFD_CLOEXEC;
            #[cfg(not(any(target_os = "android", target_os="haiku")))]
            use libc::F_DUPFD_CLOEXEC;

            let make_filedesc = |fd| {
                let fd = FileDesc::new(fd);
                fd.set_cloexec()?;
                Ok(fd)
            };
            static TRY_CLOEXEC: AtomicBool =
                AtomicBool::new(!cfg!(target_os = "android"));
            let fd = self.raw();
            if TRY_CLOEXEC.load(Ordering::Relaxed) {
                match cvt(unsafe { libc::fcntl(fd, F_DUPFD_CLOEXEC, 0) }) {
                    // We *still* call the `set_cloexec` method as apparently some
                    // linux kernel at some point stopped setting CLOEXEC even
                    // though it reported doing so on F_DUPFD_CLOEXEC.
                    Ok(fd) => {
                        return Ok(if cfg!(target_os = "linux") {
                            make_filedesc(fd)?
                        } else {
                            FileDesc::new(fd)
                        })
                    }
                    Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {
                        TRY_CLOEXEC.store(false, Ordering::Relaxed);
                    }
                    Err(e) => return Err(e),
                }
            }
            cvt(unsafe { libc::fcntl(fd, libc::F_DUPFD, 0) }).and_then(make_filedesc)
        }
    }

    impl<'a> Read for &'a FileDesc {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            (**self).read(buf)
        }

        #[inline]
        unsafe fn initializer(&self) -> Initializer {
            Initializer::nop()
        }
    }

    impl AsInner<c_int> for FileDesc {
        fn as_inner(&self) -> &c_int { &self.fd }
    }

    impl Drop for FileDesc {
        fn drop(&mut self) {
            // Note that errors are ignored when closing a file descriptor. The
            // reason for this is that if an error occurs we don't actually know if
            // the file descriptor was closed or not, and if we retried (for
            // something like EINTR), we might close another valid file descriptor
            // opened after we closed ours.
            let _ = unsafe { libc::close(self.fd) };
        }
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)