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
use IntoInner;
use io::FileDesc;
use mio::{Evented, Poll, PollOpt, Ready, Token};
use mio::unix::EventedFd;
use std::io::{ErrorKind, Read, Result, Write};
use std::os::unix::io::AsRawFd;
use tokio_core::reactor::{Handle, PollEvented};

/// Represents an attempt to register a file descriptor with a tokio event loop.
#[derive(Debug)]
pub enum MaybeEventedFd {
    /// A regular file that cannot be registered in an event loop.
    RegularFile(FileDesc),
    /// A file descriptor that was successfully registered with tokio
    Registered(PollEvented<EventedFileDesc>),
}

/// Unix-specific extensions for a `FileDesc`.
///
/// To make use of this extension, make sure this trait is imported into
/// the appropriate module.
///
/// ```rust,no_run
/// extern crate conch_runtime;
/// # extern crate tokio_core;
///
/// use conch_runtime::io::FileDesc;
/// use conch_runtime::os::unix::io::FileDescExt;
/// # use std::fs::File;
/// # use tokio_core::reactor::Core;
///
/// # fn main() {
/// let file = File::open("/dev/null").unwrap();
/// let fd = FileDesc::from(file);
///
/// let core = Core::new().unwrap();
/// fd.into_evented(&core.handle()).unwrap();
/// # }
/// ```
pub trait FileDescExt {
    /// Registers the underlying primitive OS handle with a `tokio` event loop.
    ///
    /// The resulting type is "futures" aware meaning that it is (a) nonblocking,
    /// (b) will notify the appropriate task when data is ready to be read or written
    /// and (c) will panic if use off of a future's task.
    ///
    /// Note: two identical file descriptors (which have identical file descriptions)
    /// must *NOT* be registered on the same event loop at the same time (e.g.
    /// `unsafe`ly coping raw file descriptors and registering both copies with
    /// the same `Handle`). Doing so may end up starving one of the copies from
    /// receiving notifications from the event loop.
    #[deprecated(note = "does not handle regular files, use `into_evented2` instead")]
    fn into_evented(self, handle: &Handle) -> Result<PollEvented<EventedFileDesc>>;

    /// Attempts to register the underlying primitive OS handle with a `tokio` event loop.
    ///
    /// The resulting type is "futures" aware meaning that it is (a) nonblocking,
    /// (b) will notify the appropriate task when data is ready to be read or written
    /// and (c) will panic if use off of a future's task.
    ///
    /// Note: two identical file descriptors (which have identical file descriptions)
    /// must *NOT* be registered on the same event loop at the same time (e.g.
    /// `unsafe`ly coping raw file descriptors and registering both copies with
    /// the same `Handle`). Doing so may end up starving one of the copies from
    /// receiving notifications from the event loop.
    ///
    /// Note: regular files are not supported by the OS primitives which power tokio
    /// event loops, and will result in an error on registration. However, since
    /// regular files can be assumed to always be ready for read/write operations,
    /// we can handle this usecase by not registering those file descriptors within tokio.
    fn into_evented2(self, handle: &Handle) -> Result<MaybeEventedFd> where Self: Sized {
        // FIXME(breaking): remove this default
        self.into_evented(handle).map(MaybeEventedFd::Registered)
    }

    /// Sets the `O_NONBLOCK` flag on the descriptor to the desired state.
    ///
    /// Specifiying `true` will set the file descriptor in non-blocking mode,
    /// while specifying `false` will set it to blocking mode.
    fn set_nonblock(&self, set: bool) -> Result<()>;
}

impl FileDescExt for FileDesc {
    fn into_evented(self, handle: &Handle) -> Result<PollEvented<EventedFileDesc>> {
        try!(self.set_nonblock(true));
        PollEvented::new(EventedFileDesc(self), handle)
    }

    fn into_evented2(self, handle: &Handle) -> Result<MaybeEventedFd> {
        let ret = if is_regular_file(&self)? {
            MaybeEventedFd::RegularFile(self)
        } else {
            self.set_nonblock(true)?;
            let evented = PollEvented::new(EventedFileDesc(self), handle)?;
            MaybeEventedFd::Registered(evented)
        };

        Ok(ret)
    }

    fn set_nonblock(&self, set: bool) -> Result<()> {
        self.inner().set_nonblock(set)
    }
}

/// A `FileDesc` which has been registered with a `tokio` event loop.
///
/// This version is "futures aware" meaning that it is both (a) nonblocking
/// and (b) will panic if use off of a future's task.
#[derive(Debug, PartialEq, Eq)]
pub struct EventedFileDesc(FileDesc);

impl Evented for EventedFileDesc {
    fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> Result<()> {
        match EventedFd(&self.0.as_raw_fd()).register(poll, token, interest, opts) {
            ret@Ok(_) => ret,
            Err(e) => if e.kind() == ErrorKind::AlreadyExists {
                self.reregister(poll, token, interest, opts)
            } else {
                Err(e)
            },
        }
    }

    fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> Result<()> {
        EventedFd(&self.0.as_raw_fd()).reregister(poll, token, interest, opts)
    }

    fn deregister(&self, poll: &Poll) -> Result<()> {
        EventedFd(&self.0.as_raw_fd()).deregister(poll)
    }
}

impl Read for EventedFileDesc {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        self.0.read(buf)
    }
}

impl Write for EventedFileDesc {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        self.0.write(buf)
    }

    fn flush(&mut self) -> Result<()> {
        self.0.flush()
    }
}

fn is_regular_file(fd: &FileDesc) -> Result<bool> {
    use libc;
    use std::mem;
    use sys::cvt_r;

    #[cfg(not(linux))]
    fn get_mode(fd: &FileDesc) -> Result<libc::mode_t> {
        unsafe {
            let mut stat: libc::stat = mem::zeroed();
            cvt_r(|| libc::fstat(fd.as_raw_fd(), &mut stat))
                .map(|_| stat.st_mode)
        }
    }

    #[cfg(linux)]
    fn get_mode(fd: &FileDesc) -> Result<libc::mode_t> {
        unsafe {
            let mut stat: libc::stat64 = mem::zeroed();
            cvt_r(|| libc::fstat64(fd.as_raw_fd(), &mut stat))
                .map(|_| stat.st_mode)
        }
    }

    get_mode(&fd).map(|mode| mode & libc::S_IFMT == libc::S_IFREG)
}