Debian Codegraph @

third_party/rust/rustix/src/backend/libc/event/syscalls.rs

raw
1 //! libc syscalls supporting `rustix::event`.
2
3 use crate::backend::c;
4 #[cfg(any(linux_kernel, solarish, target_os = "redox"))]
5 use crate::backend::conv::ret;
6 use crate::backend::conv::ret_c_int;
7 #[cfg(feature = "alloc")]
8 #[cfg(any(linux_kernel, solarish, target_os = "redox"))]
9 use crate::backend::conv::ret_u32;
10 #[cfg(solarish)]
11 use crate::event::port::Event;
12 #[cfg(any(
13 linux_kernel,
14 target_os = "freebsd",
15 target_os = "illumos",
16 target_os = "espidf"
17 ))]
18 use crate::event::EventfdFlags;
19 #[cfg(any(bsd, linux_kernel, target_os = "wasi"))]
20 use crate::event::FdSetElement;
21 use crate::event::PollFd;
22 use crate::io;
23 #[cfg(solarish)]
24 use crate::utils::as_mut_ptr;
25 #[cfg(any(linux_kernel, solarish, target_os = "redox"))]
26 use crate::utils::as_ptr;
27 #[cfg(any(
28 all(feature = "alloc", bsd),
29 solarish,
30 all(feature = "alloc", any(linux_kernel, target_os = "redox")),
31 ))]
32 use core::mem::MaybeUninit;
33 #[cfg(any(bsd, linux_kernel, target_os = "wasi"))]
34 use core::ptr::null;
35 #[cfg(any(bsd, linux_kernel, solarish, target_os = "redox", target_os = "wasi"))]
36 use core::ptr::null_mut;
37 #[cfg(any(
38 linux_kernel,
39 solarish,
40 target_os = "redox",
41 all(feature = "alloc", bsd)
42 ))]
43 use {crate::backend::conv::borrowed_fd, crate::fd::BorrowedFd};
44 #[cfg(any(
45 linux_kernel,
46 solarish,
47 target_os = "freebsd",
48 target_os = "illumos",
49 target_os = "espidf",
50 target_os = "redox",
51 all(feature = "alloc", bsd)
52 ))]
53 use {crate::backend::conv::ret_owned_fd, crate::fd::OwnedFd};
54 #[cfg(all(feature = "alloc", bsd))]
55 use {crate::event::kqueue::Event, crate::utils::as_ptr};
56
57 #[cfg(any(
58 linux_kernel,
59 target_os = "freebsd",
60 target_os = "illumos",
61 target_os = "espidf"
62 ))]
63 pub(crate) fn eventfd(initval: u32, flags: EventfdFlags) -> io::Result<OwnedFd> {
64 #[cfg(linux_kernel)]
65 unsafe {
66 syscall! {
67 fn eventfd2(
68 initval: c::c_uint,
69 flags: c::c_int
70 ) via SYS_eventfd2 -> c::c_int
71 }
72 ret_owned_fd(eventfd2(initval, bitflags_bits!(flags)))
73 }
74
75 // `eventfd` was added in FreeBSD 13, so it isn't available on FreeBSD 12.
76 #[cfg(target_os = "freebsd")]
77 unsafe {
78 weakcall! {
79 fn eventfd(
80 initval: c::c_uint,
81 flags: c::c_int
82 ) -> c::c_int
83 }
84 ret_owned_fd(eventfd(initval, bitflags_bits!(flags)))
85 }
86
87 #[cfg(any(target_os = "illumos", target_os = "espidf"))]
88 unsafe {
89 ret_owned_fd(c::eventfd(initval, bitflags_bits!(flags)))
90 }
91 }
92
93 #[cfg(all(feature = "alloc", bsd))]
94 pub(crate) fn kqueue() -> io::Result<OwnedFd> {
95 unsafe { ret_owned_fd(c::kqueue()) }
96 }
97
98 #[cfg(all(feature = "alloc", bsd))]
99 pub(crate) unsafe fn kevent(
100 kq: BorrowedFd<'_>,
101 changelist: &[Event],
102 eventlist: &mut [MaybeUninit<Event>],
103 timeout: Option<&c::timespec>,
104 ) -> io::Result<c::c_int> {
105 ret_c_int(c::kevent(
106 borrowed_fd(kq),
107 changelist.as_ptr().cast(),
108 changelist
109 .len()
110 .try_into()
111 .map_err(|_| io::Errno::OVERFLOW)?,
112 eventlist.as_mut_ptr().cast(),
113 eventlist
114 .len()
115 .try_into()
116 .map_err(|_| io::Errno::OVERFLOW)?,
117 timeout.map_or(null(), as_ptr),
118 ))
119 }
120
121 #[inline]
122 pub(crate) fn poll(fds: &mut [PollFd<'_>], timeout: c::c_int) -> io::Result<usize> {
123 let nfds = fds
124 .len()
125 .try_into()
126 .map_err(|_convert_err| io::Errno::INVAL)?;
127
128 ret_c_int(unsafe { c::poll(fds.as_mut_ptr().cast(), nfds, timeout) })
129 .map(|nready| nready as usize)
130 }
131
132 #[cfg(any(bsd, linux_kernel))]
133 pub(crate) unsafe fn select(
134 nfds: i32,
135 readfds: Option<&mut [FdSetElement]>,
136 writefds: Option<&mut [FdSetElement]>,
137 exceptfds: Option<&mut [FdSetElement]>,
138 timeout: Option<&crate::timespec::Timespec>,
139 ) -> io::Result<i32> {
140 let len = crate::event::fd_set_num_elements_for_bitvector(nfds);
141
142 let readfds = match readfds {
143 Some(readfds) => {
144 assert!(readfds.len() >= len);
145 readfds.as_mut_ptr()
146 }
147 None => null_mut(),
148 };
149 let writefds = match writefds {
150 Some(writefds) => {
151 assert!(writefds.len() >= len);
152 writefds.as_mut_ptr()
153 }
154 None => null_mut(),
155 };
156 let exceptfds = match exceptfds {
157 Some(exceptfds) => {
158 assert!(exceptfds.len() >= len);
159 exceptfds.as_mut_ptr()
160 }
161 None => null_mut(),
162 };
163
164 let timeout_data;
165 let timeout_ptr = match timeout {
166 Some(timeout) => {
167 // Convert from `Timespec` to `c::timeval`.
168 timeout_data = c::timeval {
169 tv_sec: timeout.tv_sec.try_into().map_err(|_| io::Errno::OVERFLOW)?,
170 tv_usec: ((timeout.tv_nsec + 999) / 1000) as _,
171 };
172 &timeout_data
173 }
174 None => null(),
175 };
176
177 // On Apple platforms, use the specially mangled `select` which doesn't
178 // have an `FD_SETSIZE` limitation.
179 #[cfg(apple)]
180 {
181 extern "C" {
182 #[link_name = "select$DARWIN_EXTSN$NOCANCEL"]
183 fn select(
184 nfds: c::c_int,
185 readfds: *mut FdSetElement,
186 writefds: *mut FdSetElement,
187 errorfds: *mut FdSetElement,
188 timeout: *const c::timeval,
189 ) -> c::c_int;
190 }
191
192 ret_c_int(select(nfds, readfds, writefds, exceptfds, timeout_ptr))
193 }
194
195 // Otherwise just use the normal `select`.
196 #[cfg(not(apple))]
197 {
198 ret_c_int(c::select(
199 nfds,
200 readfds.cast(),
201 writefds.cast(),
202 exceptfds.cast(),
203 timeout_ptr as *mut c::timeval,
204 ))
205 }
206 }
207
208 // WASI uses a count + array instead of a bitvector.
209 #[cfg(target_os = "wasi")]
210 pub(crate) unsafe fn select(
211 nfds: i32,
212 readfds: Option<&mut [FdSetElement]>,
213 writefds: Option<&mut [FdSetElement]>,
214 exceptfds: Option<&mut [FdSetElement]>,
215 timeout: Option<&crate::timespec::Timespec>,
216 ) -> io::Result<i32> {
217 let len = crate::event::fd_set_num_elements_for_fd_array(nfds as usize);
218
219 let readfds = match readfds {
220 Some(readfds) => {
221 assert!(readfds.len() >= len);
222 readfds.as_mut_ptr()
223 }
224 None => null_mut(),
225 };
226 let writefds = match writefds {
227 Some(writefds) => {
228 assert!(writefds.len() >= len);
229 writefds.as_mut_ptr()
230 }
231 None => null_mut(),
232 };
233 let exceptfds = match exceptfds {
234 Some(exceptfds) => {
235 assert!(exceptfds.len() >= len);
236 exceptfds.as_mut_ptr()
237 }
238 None => null_mut(),
239 };
240
241 let timeout_data;
242 let timeout_ptr = match timeout {
243 Some(timeout) => {
244 // Convert from `Timespec` to `c::timeval`.
245 timeout_data = c::timeval {
246 tv_sec: timeout.tv_sec.try_into().map_err(|_| io::Errno::OVERFLOW)?,
247 tv_usec: ((timeout.tv_nsec + 999) / 1000) as _,
248 };
249 &timeout_data
250 }
251 None => null(),
252 };
253
254 ret_c_int(c::select(
255 nfds,
256 readfds.cast(),
257 writefds.cast(),
258 exceptfds.cast(),
259 timeout_ptr as *mut c::timeval,
260 ))
261 }
262
263 #[cfg(solarish)]
264 pub(crate) fn port_create() -> io::Result<OwnedFd> {
265 unsafe { ret_owned_fd(c::port_create()) }
266 }
267
268 #[cfg(solarish)]
269 pub(crate) unsafe fn port_associate(
270 port: BorrowedFd<'_>,
271 source: c::c_int,
272 object: c::uintptr_t,
273 events: c::c_int,
274 user: *mut c::c_void,
275 ) -> io::Result<()> {
276 ret(c::port_associate(
277 borrowed_fd(port),
278 source,
279 object,
280 events,
281 user,
282 ))
283 }
284
285 #[cfg(solarish)]
286 pub(crate) unsafe fn port_dissociate(
287 port: BorrowedFd<'_>,
288 source: c::c_int,
289 object: c::uintptr_t,
290 ) -> io::Result<()> {
291 ret(c::port_dissociate(borrowed_fd(port), source, object))
292 }
293
294 #[cfg(solarish)]
295 pub(crate) fn port_get(
296 port: BorrowedFd<'_>,
297 timeout: Option<&mut c::timespec>,
298 ) -> io::Result<Event> {
299 let mut event = MaybeUninit::<c::port_event>::uninit();
300 let timeout = timeout.map_or(null_mut(), as_mut_ptr);
301
302 unsafe {
303 ret(c::port_get(borrowed_fd(port), event.as_mut_ptr(), timeout))?;
304 }
305
306 // If we're done, initialize the event and return it.
307 Ok(Event(unsafe { event.assume_init() }))
308 }
309
310 #[cfg(all(feature = "alloc", solarish))]
311 pub(crate) fn port_getn(
312 port: BorrowedFd<'_>,
313 timeout: Option<&mut c::timespec>,
314 events: &mut Vec<Event>,
315 mut nget: u32,
316 ) -> io::Result<()> {
317 let timeout = timeout.map_or(null_mut(), as_mut_ptr);
318 unsafe {
319 ret(c::port_getn(
320 borrowed_fd(port),
321 events.as_mut_ptr().cast(),
322 events.len().try_into().unwrap(),
323 &mut nget,
324 timeout,
325 ))?;
326 }
327
328 // Update the vector length.
329 unsafe {
330 events.set_len(nget.try_into().unwrap());
331 }
332
333 Ok(())
334 }
335
336 #[cfg(solarish)]
337 pub(crate) fn port_send(
338 port: BorrowedFd<'_>,
339 events: c::c_int,
340 userdata: *mut c::c_void,
341 ) -> io::Result<()> {
342 unsafe { ret(c::port_send(borrowed_fd(port), events, userdata)) }
343 }
344
345 #[cfg(not(any(target_os = "redox", target_os = "wasi")))]
346 pub(crate) fn pause() {
347 let r = unsafe { c::pause() };
348 let errno = libc_errno::errno().0;
349 debug_assert_eq!(r, -1);
350 debug_assert_eq!(errno, c::EINTR);
351 }
352
353 #[inline]
354 #[cfg(any(linux_kernel, solarish, target_os = "redox"))]
355 pub(crate) fn epoll_create(flags: super::epoll::CreateFlags) -> io::Result<OwnedFd> {
356 unsafe { ret_owned_fd(c::epoll_create1(bitflags_bits!(flags))) }
357 }
358
359 #[inline]
360 #[cfg(any(linux_kernel, solarish, target_os = "redox"))]
361 pub(crate) fn epoll_add(
362 epoll: BorrowedFd<'_>,
363 source: BorrowedFd<'_>,
364 event: &crate::event::epoll::Event,
365 ) -> io::Result<()> {
366 // We use our own `Event` struct instead of libc's because
367 // ours preserves pointer provenance instead of just using a `u64`,
368 // and we have tests elsewhere for layout equivalence.
369 unsafe {
370 ret(c::epoll_ctl(
371 borrowed_fd(epoll),
372 c::EPOLL_CTL_ADD,
373 borrowed_fd(source),
374 // The event is read-only even though libc has a non-const pointer.
375 as_ptr(event) as *mut c::epoll_event,
376 ))
377 }
378 }
379
380 #[inline]
381 #[cfg(any(linux_kernel, solarish, target_os = "redox"))]
382 pub(crate) fn epoll_mod(
383 epoll: BorrowedFd<'_>,
384 source: BorrowedFd<'_>,
385 event: &crate::event::epoll::Event,
386 ) -> io::Result<()> {
387 unsafe {
388 ret(c::epoll_ctl(
389 borrowed_fd(epoll),
390 c::EPOLL_CTL_MOD,
391 borrowed_fd(source),
392 // The event is read-only even though libc has a non-const pointer.
393 as_ptr(event) as *mut c::epoll_event,
394 ))
395 }
396 }
397
398 #[inline]
399 #[cfg(any(linux_kernel, solarish, target_os = "redox"))]
400 pub(crate) fn epoll_del(epoll: BorrowedFd<'_>, source: BorrowedFd<'_>) -> io::Result<()> {
401 unsafe {
402 ret(c::epoll_ctl(
403 borrowed_fd(epoll),
404 c::EPOLL_CTL_DEL,
405 borrowed_fd(source),
406 null_mut(),
407 ))
408 }
409 }
410
411 #[inline]
412 #[cfg(feature = "alloc")]
413 #[cfg(any(linux_kernel, solarish, target_os = "redox"))]
414 pub(crate) fn epoll_wait(
415 epoll: BorrowedFd<'_>,
416 events: &mut [MaybeUninit<crate::event::epoll::Event>],
417 timeout: c::c_int,
418 ) -> io::Result<usize> {
419 unsafe {
420 ret_u32(c::epoll_wait(
421 borrowed_fd(epoll),
422 events.as_mut_ptr().cast::<c::epoll_event>(),
423 events.len().try_into().unwrap_or(i32::MAX),
424 timeout,
425 ))
426 .map(|i| i.try_into().unwrap_or(usize::MAX))
427 }
428 }