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
use std::ffi::c_int;

extern "C" {
    #[cfg(not(any(target_os = "dragonfly", target_os = "vxworks")))]
    #[cfg_attr(
        any(
            target_os = "linux",
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "l4re"
        ),
        link_name = "__errno_location"
    )]
    #[cfg_attr(
        any(
            target_os = "netbsd",
            target_os = "openbsd",
            target_os = "android",
            target_os = "redox",
            target_env = "newlib"
        ),
        link_name = "__errno"
    )]
    #[cfg_attr(
        any(target_os = "solaris", target_os = "illumos"),
        link_name = "___errno"
    )]
    #[cfg_attr(
        any(
            target_os = "macos",
            target_os = "ios",
            target_os = "freebsd",
            target_os = "watchos"
        ),
        link_name = "__error"
    )]
    #[cfg_attr(target_os = "haiku", link_name = "_errnop")]
    fn errno_location() -> *mut c_int;
}

pub extern "C" fn reset_errno() {
    set_errno(0);
}

pub extern "C" fn set_errno(errno: c_int) {
    unsafe { errno_location().write(errno) }
}

/// # Panics
/// if set fails.
pub extern "C" fn set_non_blocking(socket: c_int) {
    assert!(
        set_non_blocking_flag(socket, true),
        "set_non_blocking failed !"
    );
}

/// # Panics
/// if set fails.
pub extern "C" fn set_blocking(socket: c_int) {
    assert!(
        set_non_blocking_flag(socket, false),
        "set_blocking failed !"
    );
}

extern "C" fn set_non_blocking_flag(socket: c_int, on: bool) -> bool {
    let flags = unsafe { libc::fcntl(socket, libc::F_GETFL) };
    if flags < 0 {
        return false;
    }
    unsafe {
        libc::fcntl(
            socket,
            libc::F_SETFL,
            if on {
                flags | libc::O_NONBLOCK
            } else {
                flags & !libc::O_NONBLOCK
            },
        ) == 0
    }
}

#[must_use]
pub extern "C" fn is_blocking(socket: c_int) -> bool {
    !is_non_blocking(socket)
}

#[must_use]
pub extern "C" fn is_non_blocking(socket: c_int) -> bool {
    let flags = unsafe { libc::fcntl(socket, libc::F_GETFL) };
    if flags < 0 {
        return false;
    }
    (flags & libc::O_NONBLOCK) != 0
}

#[macro_export]
macro_rules! log_syscall {
    ( $socket:expr, $done:expr, $once_result:expr ) => {
        #[cfg(feature = "logs")]
        if let Some(coroutine) = $crate::scheduler::SchedulableCoroutine::current() {
            $crate::info!(
                "{} {} {} {} {} {}",
                coroutine.get_name(),
                coroutine.state(),
                $socket,
                $done,
                $once_result,
                std::io::Error::last_os_error(),
            );
        }
    };
}

#[macro_export]
macro_rules! impl_non_blocking {
    ( $socket:expr, $impls:expr ) => {{
        let socket = $socket;
        let blocking = $crate::syscall::common::is_blocking(socket);
        if blocking {
            $crate::syscall::common::set_non_blocking(socket);
        }
        let r = $impls;
        if blocking {
            $crate::syscall::common::set_blocking(socket);
        }
        return r;
    }};
}