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
use crate::{ctypes, utils::e};
use arceos_posix_api::{sys_close, sys_dup, sys_dup2, sys_fcntl};
use axerrno::LinuxError;
use core::ffi::c_int;

/// Close a file by `fd`.
#[no_mangle]
pub unsafe extern "C" fn close(fd: c_int) -> c_int {
    e(sys_close(fd))
}

/// Duplicate a file descriptor.
#[no_mangle]
pub unsafe extern "C" fn dup(old_fd: c_int) -> c_int {
    e(sys_dup(old_fd))
}

/// Duplicate a file descriptor, use file descriptor specified in `new_fd`.
#[no_mangle]
pub unsafe extern "C" fn dup2(old_fd: c_int, new_fd: c_int) -> c_int {
    e(sys_dup2(old_fd, new_fd))
}

/// Duplicate a file descriptor, the caller can force the close-on-exec flag to
/// be set for the new file descriptor by specifying `O_CLOEXEC` in flags.
///
/// If oldfd equals newfd, then `dup3()` fails with the error `EINVAL`.
#[no_mangle]
pub unsafe extern "C" fn dup3(old_fd: c_int, new_fd: c_int, flags: c_int) -> c_int {
    if old_fd == new_fd {
        return e((LinuxError::EINVAL as c_int).wrapping_neg());
    }
    let r = e(sys_dup2(old_fd, new_fd));
    if r < 0 {
        r
    } else {
        if flags as u32 & ctypes::O_CLOEXEC != 0 {
            e(sys_fcntl(
                new_fd,
                ctypes::F_SETFD as c_int,
                ctypes::FD_CLOEXEC as usize,
            ));
        }
        new_fd
    }
}

/// Manipulate file descriptor.
///
/// TODO: `SET/GET` command is ignored
#[no_mangle]
pub unsafe extern "C" fn ax_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int {
    e(sys_fcntl(fd, cmd, arg))
}