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
use std::convert::TryInto;
use std::io;
use std::time::Duration;

use bitflags::bitflags;

use super::{Int, Short};

bitflags! {
    pub struct Events: Short {
        const IN = libc::POLLIN;
        const RDNORM = libc::POLLRDNORM;
        const RDBAND = libc::POLLRDBAND;
        const PRI = libc::POLLPRI;
        const OUT = libc::POLLOUT;
        const WRNORM  = libc::POLLWRNORM;
        const WRBAND = libc::POLLWRBAND;
        const ERR = libc::POLLERR;
        const HUP = libc::POLLHUP;
        const NVAL = libc::POLLNVAL;
    }
}

#[repr(C)]
pub struct PollFd {
    pub fd: Int,
    pub events: Events,
    pub revents: Events,
}

pub fn poll(fds: &mut [PollFd], timeout: Option<Duration>) -> io::Result<usize> {
    let raw_timeout: Int = match timeout {
        Some(t) => t.as_millis().try_into().unwrap_or(Int::MAX),
        None => -1,
    };

    super::error::convert_neg_ret(unsafe {
        libc::poll(
            fds.as_mut_ptr() as *mut libc::pollfd,
            fds.len() as libc::nfds_t,
            raw_timeout,
        )
    })
    .map(|n| n as usize)
}

#[cfg(target_os = "netbsd")]
const LIBC_PPOLL: unsafe extern "C" fn(
    *mut libc::pollfd,
    libc::nfds_t,
    *const libc::timespec,
    *const libc::sigset_t,
) -> libc::c_int = super::externs::pollts;
#[cfg(any(
    target_os = "linux",
    target_os = "freebsd",
    target_os = "openbsd",
    target_os = "dragonfly",
))]
const LIBC_PPOLL: unsafe extern "C" fn(
    *mut libc::pollfd,
    libc::nfds_t,
    *const libc::timespec,
    *const libc::sigset_t,
) -> libc::c_int = libc::ppoll;

#[cfg(any(
    target_os = "linux",
    target_os = "freebsd",
    target_os = "openbsd",
    target_os = "netbsd",
))]
pub fn ppoll(
    fds: &mut [PollFd],
    timeout: Option<Duration>,
    sigmask: Option<super::signal::Sigset>,
) -> io::Result<usize> {
    let raw_timeout = match timeout {
        Some(t) => &libc::timespec {
            tv_sec: t.as_secs().try_into().unwrap_or(libc::time_t::MAX),
            tv_nsec: t.subsec_nanos() as super::Long,
        },
        None => std::ptr::null(),
    };

    let raw_sigmask = match sigmask {
        Some(s) => &s.raw_set(),
        None => std::ptr::null(),
    };

    super::error::convert_neg_ret(unsafe {
        LIBC_PPOLL(
            fds.as_mut_ptr() as *mut libc::pollfd,
            fds.len() as libc::nfds_t,
            raw_timeout,
            raw_sigmask,
        )
    })
    .map(|n| n as usize)
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::fs;
    use std::io::Write;
    use std::os::unix::io::AsRawFd;

    #[cfg(any(
        target_os = "linux",
        target_os = "freebsd",
        target_os = "openbsd",
        target_os = "netbsd",
        target_os = "dragonfly",
    ))]
    fn pipe_cloexec() -> io::Result<(fs::File, fs::File)> {
        super::super::pipe2(libc::O_CLOEXEC)
    }

    #[cfg(target_os = "macos")]
    fn pipe_cloexec() -> io::Result<(fs::File, fs::File)> {
        let (r, w) = super::super::pipe()?;
        super::super::fcntl::set_inheritable(r.as_raw_fd(), false);
        super::super::fcntl::set_inheritable(w.as_raw_fd(), false);
        Ok((r, w))
    }

    #[test]
    fn test_poll() {
        let (r1, mut w1) = pipe_cloexec().unwrap();
        let (r2, mut w2) = pipe_cloexec().unwrap();

        let mut fds = [
            PollFd {
                fd: r1.as_raw_fd(),
                events: Events::IN,
                revents: Events::empty(),
            },
            PollFd {
                fd: r2.as_raw_fd(),
                events: Events::IN,
                revents: Events::empty(),
            },
        ];

        // Nothing to start
        assert_eq!(poll(&mut fds, Some(Duration::from_secs(0))).unwrap(), 0);

        // Now we write some data and test again
        w1.write(b"a").unwrap();
        assert_eq!(poll(&mut fds, Some(Duration::from_secs(0))).unwrap(), 1);
        assert_eq!(fds[0].fd, r1.as_raw_fd());
        assert_eq!(fds[0].revents, Events::IN);

        // Now make sure reading two files works
        w2.write(b"a").unwrap();
        assert_eq!(poll(&mut fds, Some(Duration::from_secs(0))).unwrap(), 2);
        assert_eq!(fds[0].fd, r1.as_raw_fd());
        assert_eq!(fds[0].revents, Events::IN);
        assert_eq!(fds[1].fd, r2.as_raw_fd());
        assert_eq!(fds[1].revents, Events::IN);
    }

    #[cfg(any(
        target_os = "linux",
        target_os = "freebsd",
        target_os = "openbsd",
        target_os = "netbsd",
    ))]
    #[test]
    fn test_ppoll() {
        let (r1, mut w1) = pipe_cloexec().unwrap();
        let (r2, mut w2) = pipe_cloexec().unwrap();

        let mut fds = [
            PollFd {
                fd: r1.as_raw_fd(),
                events: Events::IN,
                revents: Events::empty(),
            },
            PollFd {
                fd: r2.as_raw_fd(),
                events: Events::IN,
                revents: Events::empty(),
            },
        ];

        // Nothing to start
        assert_eq!(
            ppoll(&mut fds, Some(Duration::from_secs(0)), None).unwrap(),
            0,
        );

        // Now we write some data and test again
        w1.write(b"a").unwrap();
        assert_eq!(
            ppoll(&mut fds, Some(Duration::from_secs(0)), None).unwrap(),
            1,
        );
        assert_eq!(fds[0].fd, r1.as_raw_fd());
        assert_eq!(fds[0].revents, Events::IN);

        // Now make sure reading two files works
        w2.write(b"a").unwrap();
        assert_eq!(
            ppoll(&mut fds, Some(Duration::from_secs(0)), None).unwrap(),
            2,
        );
        assert_eq!(fds[0].fd, r1.as_raw_fd());
        assert_eq!(fds[0].revents, Events::IN);
        assert_eq!(fds[1].fd, r2.as_raw_fd());
        assert_eq!(fds[1].revents, Events::IN);
    }
}