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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License..

use super::*;
use core::mem;
use sgx_trts::trts::is_within_enclave;
use sgx_types::marker::ContiguousMemory;

pub unsafe fn poll(fds: &mut [pollfd], timeout: c_int) -> OCallResult<PolledOk> {
    let mut result: c_int = 0;
    let mut error: c_int = 0;

    let nfds = fds.len() as nfds_t;
    ensure!(nfds > 0, eos!(EINVAL));

    let status = u_poll_ocall(
        &mut result as *mut c_int,
        &mut error as *mut c_int,
        fds.as_mut_ptr(),
        nfds,
        timeout,
    );

    ensure!(status.is_success(), esgx!(status));
    match result {
        1..=i32::MAX => {
            if result as nfds_t > nfds {
                Err(ecust!("Malformed return value"))
            } else {
                Ok(PolledOk::ReadyFdCount(result as u32))
            }
        }
        0 => Ok(PolledOk::TimeLimitExpired),
        _ => Err(eos!(error)),
    }
}

pub unsafe fn epoll_create1(flags: c_int) -> OCallResult<c_int> {
    let mut result: c_int = 0;
    let mut error: c_int = 0;

    let status = u_epoll_create1_ocall(&mut result as *mut c_int, &mut error as *mut c_int, flags);

    ensure!(status.is_success(), esgx!(status));
    ensure!(result >= 0, eos!(error));
    Ok(result)
}

pub unsafe fn epoll_create(size: c_int) -> OCallResult<c_int> {
    ensure!(size > 0, eos!(EINVAL));
    epoll_create1(0)
}

pub unsafe fn epoll_ctl(
    epfd: c_int,
    op: c_int,
    fd: c_int,
    event: &mut epoll_event,
) -> OCallResult<()> {
    let mut result: c_int = 0;
    let mut error: c_int = 0;

    let status = u_epoll_ctl_ocall(
        &mut result as *mut c_int,
        &mut error as *mut c_int,
        epfd,
        op,
        fd,
        event,
    );

    ensure!(status.is_success(), esgx!(status));
    ensure!(result == 0, eos!(error));
    Ok(())
}

pub unsafe fn epoll_wait(
    epfd: c_int,
    events: &mut Vec<epoll_event>,
    timeout: c_int,
) -> OCallResult<PolledOk> {
    let mut result: c_int = 0;
    let mut error: c_int = 0;

    ensure!(
        is_within_enclave(events as *mut _ as *mut u8, mem::size_of_val(events)),
        eos!(EINVAL)
    );
    let maxevents = events.capacity();
    ensure!((1..=i32::MAX as usize).contains(&maxevents), eos!(EINVAL));

    let status = u_epoll_wait_ocall(
        &mut result as *mut c_int,
        &mut error as *mut c_int,
        epfd,
        events.as_mut_ptr(),
        maxevents as c_uint,
        timeout,
    );

    ensure!(status.is_success(), esgx!(status));
    match result {
        1..=i32::MAX => {
            if result > maxevents as c_int {
                Err(ecust!("Malformed return value"))
            } else {
                events.set_len(result as usize);
                Ok(PolledOk::ReadyFdCount(result as u32))
            }
        }
        0 => Ok(PolledOk::TimeLimitExpired),
        _ => Err(eos!(error)),
    }
}

#[derive(Clone, Copy, Debug)]
pub enum PolledOk {
    TimeLimitExpired,
    ReadyFdCount(u32),
}

unsafe impl ContiguousMemory for PolledOk {}