1use crate::*;
2use std::mem::MaybeUninit;
3
4#[man(eventfd(2))]
5pub fn eventfd(initval: c::c_uint, flags: c::c_int) -> Result<OwnedFd> {
6 let res = unsafe { c::eventfd(initval, flags) };
7 map_err!(res).map(OwnedFd::new)
8}
9
10pub fn eventfd_read(fd: c::c_int) -> Result<u64> {
12 let mut num = 0;
13 let res = read(fd, &mut num)?.len();
14 if res < 8 {
15 Err(Errno(c::EBADF))
16 } else {
17 Ok(num)
18 }
19}
20
21pub fn eventfd_write(fd: c::c_int, num: u64) -> Result<()> {
23 let res = write(fd, &num)?;
24 if res < 8 {
25 Err(Errno(c::EBADF))
26 } else {
27 Ok(())
28 }
29}
30
31#[man(memfd_create(2))]
32pub fn memfd_create<'a>(name: impl IntoUstr<'a>, flags: c::c_uint) -> Result<OwnedFd> {
33 let name = name.into_ustr();
34 let res = unsafe {
35 c::syscall(c::SYS_memfd_create, name.as_ptr() as usize, flags as usize)
36 };
37 map_err!(res).map(|val| OwnedFd::new(val as _))
38}
39
40#[man(sysinfo(2))]
41pub fn sysinfo() -> Result<c::sysinfo> {
42 let mut sysinfo = MaybeUninit::uninit();
43 let res = unsafe { c::sysinfo(sysinfo.as_mut_ptr()) };
44 map_err!(res).map(|_| unsafe { sysinfo.assume_init() })
45}
46
47#[man(pipe2(2))]
48pub fn pipe2(flags: c::c_int) -> Result<(OwnedFd, OwnedFd)> {
49 let mut buf = [0; 2];
50 let res = unsafe { c::pipe2(buf.as_mut_ptr(), flags) };
51 map_err!(res).map(|_| (OwnedFd::new(buf[0]), OwnedFd::new(buf[1])))
52}
53
54#[man(syncfs(2))]
55pub fn syncfs(fd: c::c_int) -> Result<()> {
56 let res = unsafe { libc::syscall(c::SYS_syncfs, fd as usize) };
57 map_err!(res).map(drop)
58}