axlibc/
fd_ops.rs

1use crate::{ctypes, utils::e};
2use arceos_posix_api::{sys_close, sys_dup, sys_dup2, sys_fcntl};
3use axerrno::LinuxError;
4use core::ffi::c_int;
5
6/// Close a file by `fd`.
7#[unsafe(no_mangle)]
8pub unsafe extern "C" fn close(fd: c_int) -> c_int {
9    e(sys_close(fd))
10}
11
12/// Duplicate a file descriptor.
13#[unsafe(no_mangle)]
14pub unsafe extern "C" fn dup(old_fd: c_int) -> c_int {
15    e(sys_dup(old_fd))
16}
17
18/// Duplicate a file descriptor, use file descriptor specified in `new_fd`.
19#[unsafe(no_mangle)]
20pub unsafe extern "C" fn dup2(old_fd: c_int, new_fd: c_int) -> c_int {
21    e(sys_dup2(old_fd, new_fd))
22}
23
24/// Duplicate a file descriptor, the caller can force the close-on-exec flag to
25/// be set for the new file descriptor by specifying `O_CLOEXEC` in flags.
26///
27/// If oldfd equals newfd, then `dup3()` fails with the error `EINVAL`.
28#[unsafe(no_mangle)]
29pub unsafe extern "C" fn dup3(old_fd: c_int, new_fd: c_int, flags: c_int) -> c_int {
30    if old_fd == new_fd {
31        return e((LinuxError::EINVAL as c_int).wrapping_neg());
32    }
33    let r = e(sys_dup2(old_fd, new_fd));
34    if r < 0 {
35        r
36    } else {
37        if flags as u32 & ctypes::O_CLOEXEC != 0 {
38            e(sys_fcntl(
39                new_fd,
40                ctypes::F_SETFD as c_int,
41                ctypes::FD_CLOEXEC as usize,
42            ));
43        }
44        new_fd
45    }
46}
47
48/// Manipulate file descriptor.
49///
50/// TODO: `SET/GET` command is ignored
51#[unsafe(no_mangle)]
52pub unsafe extern "C" fn ax_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int {
53    e(sys_fcntl(fd, cmd, arg))
54}