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
extern crate nix;
extern crate notice_core;

pub mod prelude {
    pub use super::notice_core::{Notify, Wait};
}

use prelude::*;

use nix::Error as NixError;
use nix::errno::Errno;
use nix::unistd;
use nix::fcntl::OFlag;

use notice_core::Unicast;

use std::os::unix::io::{AsRawFd, FromRawFd, RawFd, IntoRawFd};
use std::io;

struct Pipe(Option<RawFd>);

impl Pipe {
    pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
        unistd::read(self.as_raw_fd(), buf).map_err(nix2io)
    }

    pub fn write_all(&self, buf: &[u8]) -> io::Result<()> {
        let mut written = 0;
        loop {
            match unistd::write(self.as_raw_fd(), &buf[written..buf.len()]) {
                Ok(x) => written += x,
                Err(NixError::Sys(Errno::EINTR)) => (),
                Err(e) => return Err(nix2io(e)),
            }

            if written == buf.len() {
                break;
            }
        }

        Ok(())
    }
}

impl AsRawFd for Pipe {
    fn as_raw_fd(&self) -> RawFd {
        match self.0 {
            Some(x) => x,
            None => unreachable!(),
        }
    }
}

impl FromRawFd for Pipe {
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        Pipe(Some(fd))
    }
}

impl IntoRawFd for Pipe {
    fn into_raw_fd(mut self) -> RawFd {
        match self.0.take() {
            Some(x) => x,
            None => unreachable!(),
        }
    }
}

impl Drop for Pipe {
    fn drop(&mut self) {
        if let Some(x) = self.0.take() {
            unistd::close(x).ok();
        }
    }
}

pub struct Notifier(Pipe);

impl AsRawFd for Notifier {
    fn as_raw_fd(&self) -> RawFd {
        self.0.as_raw_fd()
    }
}

impl FromRawFd for Notifier {
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        Notifier(Pipe::from_raw_fd(fd))
    }
}

impl IntoRawFd for Notifier {
    fn into_raw_fd(self) -> RawFd {
        self.0.into_raw_fd()
    }
}

impl Unicast for Notifier {}

impl Notify for Notifier {
    type Error = io::Error;

    fn notify(&self) -> Result<(), io::Error> {
        self.0.write_all(&[1])
    }
}

pub struct Waiter {
    pipe: Pipe,
    buf_sz: usize,
}

impl Waiter {
    const DEFAULT_BUFFER_SIZE: usize = 64;

    pub fn set_buffer_len(&mut self, len: usize) {
        self.buf_sz = len;
    }

    pub fn buffer_len(&self) -> usize {
        self.buf_sz
    }
}

impl AsRawFd for Waiter {
    fn as_raw_fd(&self) -> RawFd {
        self.pipe.as_raw_fd()
    }
}

impl FromRawFd for Waiter {
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        Waiter {
            pipe: Pipe::from_raw_fd(fd),
            buf_sz: Self::DEFAULT_BUFFER_SIZE,
        }
    }
}

impl IntoRawFd for Waiter {
    fn into_raw_fd(self) -> RawFd {
        self.pipe.into_raw_fd()
    }
}

impl Unicast for Waiter {}

impl Wait for Waiter {
    type Error = io::Error;

    fn wait(&self) -> Result<usize, io::Error> {
        let mut buf = vec![0; self.buf_sz];
        self.pipe.read(&mut buf)
    }
}

fn nix2io(e: NixError) -> io::Error {
    match e {
        NixError::Sys(e) => io::Error::from_raw_os_error(e as i32),
        _ => io::Error::new(io::ErrorKind::Other, e),
    }
}

pub fn pair() -> Result<(Notifier, Waiter), io::Error> {
    let (r_fd, w_fd) = unistd::pipe2(OFlag::O_CLOEXEC).map_err(nix2io)?;

    let wr = Notifier(Pipe(Some(w_fd)));
    let rd = Waiter {
        pipe: Pipe(Some(r_fd)),
        buf_sz: Waiter::DEFAULT_BUFFER_SIZE,
    };

    Ok((wr, rd))
}