Skip to content

Commit

Permalink
Add some functions and modularize thread (#129)
Browse files Browse the repository at this point in the history
* Add some functions

* Modularize rwlock

* Modularize mutex

* Reorder and format

* Modularize once

* Use atomic for spinlock

* Write `eaccess` in terms of `euidaccess`
  • Loading branch information
carbotaniuman committed May 18, 2024
1 parent 593322f commit 7cac7ff
Show file tree
Hide file tree
Showing 14 changed files with 727 additions and 552 deletions.
12 changes: 12 additions & 0 deletions c-scape/src/fs/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ unsafe extern "C" fn faccessat(
None => -1,
}
}

#[no_mangle]
unsafe extern "C" fn euidaccess(pathname: *const c_char, amode: c_int) -> c_int {
libc!(libc::euidaccess(pathname, amode));
faccessat(libc::AT_FDCWD, pathname, amode, libc::AT_EACCESS)
}

#[no_mangle]
unsafe extern "C" fn eaccess(pathname: *const c_char, amode: c_int) -> c_int {
libc!(libc::eaccess(pathname, amode));
euidaccess(pathname, amode)
}
20 changes: 19 additions & 1 deletion c-scape/src/fs/fallocate.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use crate::convert_res;
use libc::{c_int, off_t};
use libc::{c_int, off64_t, off_t};
use rustix::fd::BorrowedFd;
use rustix::fs::FallocateFlags;

#[no_mangle]
unsafe extern "C" fn fallocate(fd: c_int, mode: c_int, offset: off_t, len: off_t) -> c_int {
libc!(libc::fallocate(fd, mode, offset, len));
fallocate64(fd, mode, offset as _, len as _)
}

#[no_mangle]
unsafe extern "C" fn fallocate64(fd: c_int, mode: c_int, offset: off64_t, len: off64_t) -> c_int {
libc!(libc::fallocate64(fd, mode, offset, len));

let fd = BorrowedFd::borrow_raw(fd);
let mode = FallocateFlags::from_bits_retain(mode as _);
Expand All @@ -14,3 +20,15 @@ unsafe extern "C" fn fallocate(fd: c_int, mode: c_int, offset: off_t, len: off_t
None => -1,
}
}

#[no_mangle]
unsafe extern "C" fn posix_fallocate(fd: c_int, offset: off_t, len: off_t) -> c_int {
libc!(libc::posix_fallocate(fd, offset, len));
fallocate64(fd, 0, offset as _, len as _)
}

#[no_mangle]
unsafe extern "C" fn posix_fallocate64(fd: c_int, offset: off64_t, len: off64_t) -> c_int {
libc!(libc::posix_fallocate64(fd, offset, len));
fallocate64(fd, 0, offset, len)
}
24 changes: 23 additions & 1 deletion c-scape/src/fs/rename.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::ffi::CStr;
use rustix::fd::BorrowedFd;

use libc::{c_char, c_int};
use libc::{c_char, c_int, c_uint};

use crate::convert_res;

Expand Down Expand Up @@ -31,3 +31,25 @@ unsafe extern "C" fn renameat(
None => -1,
}
}

#[no_mangle]
unsafe extern "C" fn renameat2(
old_fd: c_int,
old: *const c_char,
new_fd: c_int,
new: *const c_char,
flags: c_uint,
) -> c_int {
libc!(libc::renameat2(old_fd, old, new_fd, new, flags));

match convert_res(rustix::fs::renameat_with(
BorrowedFd::borrow_raw(old_fd),
CStr::from_ptr(old.cast()),
BorrowedFd::borrow_raw(new_fd),
CStr::from_ptr(new.cast()),
rustix::fs::RenameFlags::from_bits_retain(flags),
)) {
Some(()) => 0,
None => -1,
}
}
43 changes: 43 additions & 0 deletions c-scape/src/io/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ unsafe extern "C" fn preadv(fd: c_int, iov: *const iovec, iovcnt: c_int, offset:
preadv64(fd, iov, iovcnt, offset as off64_t)
}

#[no_mangle]
unsafe extern "C" fn preadv2(
fd: c_int,
iov: *const iovec,
iovcnt: c_int,
offset: off_t,
flags: c_int,
) -> isize {
libc!(libc::preadv2(fd, iov, iovcnt, offset, flags));
preadv64v2(fd, iov, iovcnt, offset as off64_t, flags)
}

#[no_mangle]
unsafe extern "C" fn preadv64(
fd: c_int,
Expand Down Expand Up @@ -114,6 +126,37 @@ unsafe extern "C" fn preadv64(
}
}

#[no_mangle]
unsafe extern "C" fn preadv64v2(
fd: c_int,
iov: *const iovec,
iovcnt: c_int,
offset: off64_t,
flags: c_int,
) -> isize {
libc!(libc::preadv64v2(fd, iov, iovcnt, offset, flags));

if fd == -1 {
set_errno(Errno(libc::EBADF));
return -1;
}

let iov: *const IoSliceMut<'_> = checked_cast!(iov);

// Note that rustix's `readv` takes a `&mut`, however it doesn't
// mutate the `IoSliceMut` instances themselves, so it's safe to
// cast away the `const` here.
match convert_res(rustix::io::preadv2(
BorrowedFd::borrow_raw(fd),
slice::from_raw_parts_mut(iov.cast_mut(), iovcnt as usize),
offset as u64,
rustix::io::ReadWriteFlags::from_bits_retain(flags as _),
)) {
Some(nwritten) => nwritten as isize,
None => -1,
}
}

// `__*_chk` functions that have to live in c-gull because they depend on
// C functions not in the libc crate, due to `VaList` being unstable.

Expand Down
33 changes: 33 additions & 0 deletions c-scape/src/io/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ unsafe extern "C" fn pwritev(fd: c_int, iov: *const iovec, iovcnt: c_int, offset
pwritev64(fd, iov, iovcnt, offset as off64_t)
}

#[no_mangle]
unsafe extern "C" fn pwritev2(
fd: c_int,
iov: *const iovec,
iovcnt: c_int,
offset: off_t,
flags: c_int,
) -> isize {
libc!(libc::pwritev2(fd, iov, iovcnt, offset, flags));
pwritev64v2(fd, iov, iovcnt, offset as off64_t, flags)
}

#[no_mangle]
unsafe extern "C" fn pwritev64(
fd: c_int,
Expand All @@ -86,3 +98,24 @@ unsafe extern "C" fn pwritev64(
None => -1,
}
}

#[no_mangle]
unsafe extern "C" fn pwritev64v2(
fd: c_int,
iov: *const iovec,
iovcnt: c_int,
offset: off64_t,
flags: c_int,
) -> isize {
libc!(libc::pwritev64v2(fd, iov, iovcnt, offset, flags));

match convert_res(rustix::io::pwritev2(
BorrowedFd::borrow_raw(fd),
slice::from_raw_parts(checked_cast!(iov), iovcnt as usize),
offset as u64,
rustix::io::ReadWriteFlags::from_bits_retain(flags as _),
)) {
Some(nwritten) => nwritten as isize,
None => -1,
}
}
1 change: 1 addition & 0 deletions c-scape/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ mod stdio;
mod strtod;
mod strtol;
mod syscall;
mod system;
mod time;

#[cfg(feature = "deprecated-and-unimplemented")]
Expand Down
28 changes: 28 additions & 0 deletions c-scape/src/system/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use errno::{set_errno, Errno};
use libc::c_int;

use crate::convert_res;

#[no_mangle]
unsafe extern "C" fn reboot(cmd: c_int) -> c_int {
libc!(libc::reboot(cmd));

let arg = match cmd {
libc::LINUX_REBOOT_CMD_CAD_OFF => rustix::system::RebootCommand::CadOff,
libc::LINUX_REBOOT_CMD_CAD_ON => rustix::system::RebootCommand::CadOn,
libc::LINUX_REBOOT_CMD_HALT => rustix::system::RebootCommand::Halt,
libc::LINUX_REBOOT_CMD_KEXEC => rustix::system::RebootCommand::Kexec,
libc::LINUX_REBOOT_CMD_POWER_OFF => rustix::system::RebootCommand::PowerOff,
libc::LINUX_REBOOT_CMD_RESTART => rustix::system::RebootCommand::Restart,
libc::LINUX_REBOOT_CMD_SW_SUSPEND => rustix::system::RebootCommand::SwSuspend,
_ => {
set_errno(Errno(libc::EINVAL));
return -1;
}
};

match convert_res(rustix::system::reboot(arg)) {
Some(()) => 0,
None => -1,
}
}
Loading

0 comments on commit 7cac7ff

Please sign in to comment.