Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion c-gull/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ libc = { version = "0.2.155", default-features = false }
c-scape = { path = "../c-scape", version = "0.21.0", default-features = false }
errno = { version = "0.3.3", default-features = false, optional = true }
tz-rs = { version = "0.7.0", default-features = false, optional = true }
rustix = { version = "0.38.31", default-features = false, optional = true, features = ["fs", "itoa", "net", "param", "process", "procfs", "rand", "termios", "thread", "time"] }
rustix = { version = "1.0.0", default-features = false, optional = true, features = ["fs", "net", "param", "process", "rand", "termios", "thread", "time"] }

[features]
default = ["thread", "std", "coexist-with-libc", "threadsafe-setenv"]
Expand Down
24 changes: 11 additions & 13 deletions c-gull/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::process::Command;
use errno::{errno, set_errno, Errno};
use libc::{c_char, c_int, size_t};
use rustix::net::{
IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrAny, SocketAddrStorage, SocketAddrV4,
SocketAddrV6,
addr::SocketAddrArg, addr::SocketAddrStorage, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr,
SocketAddrV4, SocketAddrV6,
};

// TODO: Upstream this.
Expand Down Expand Up @@ -141,21 +141,20 @@ unsafe extern "C" fn getaddrinfo(
} else {
Ipv4Addr::LOCALHOST
};
SocketAddrAny::V4(SocketAddrV4::new(addr, port))
SocketAddrV4::new(addr, port).write_sockaddr(storage)
}
libc::AF_INET6 => {
let addr = if is_passive {
Ipv6Addr::UNSPECIFIED
} else {
Ipv6Addr::LOCALHOST
};
SocketAddrAny::V6(SocketAddrV6::new(addr, port, 0, 0))
SocketAddrV6::new(addr, port, 0, 0).write_sockaddr(storage)
}
_ => unreachable!(),
}
.write(storage);
};
info.ai_addr = storage.cast();
info.ai_addrlen = len.try_into().unwrap();
info.ai_addrlen = len;

if !prev.is_null() {
(*prev).ai_next = ptr;
Expand Down Expand Up @@ -215,9 +214,9 @@ unsafe extern "C" fn getaddrinfo(
let info = &mut *ptr;

let storage = alloc::alloc::alloc(addr_layout).cast::<SocketAddrStorage>();
let len = SocketAddrAny::from(SocketAddr::new(addr, port)).write(storage);
let len = SocketAddr::new(addr, port).write_sockaddr(storage);
info.ai_addr = storage.cast();
info.ai_addrlen = len.try_into().unwrap();
info.ai_addrlen = len;
*res = ptr;
return 0;
}
Expand All @@ -237,9 +236,9 @@ unsafe extern "C" fn getaddrinfo(
{
let storage =
alloc::alloc::alloc(addr_layout).cast::<SocketAddrStorage>();
let len = SocketAddrAny::V4(SocketAddrV4::new(v4, port)).write(storage);
let len = SocketAddrV4::new(v4, port).write_sockaddr(storage);
info.ai_addr = storage.cast();
info.ai_addrlen = len.try_into().unwrap();
info.ai_addrlen = len;
info.ai_family = libc::AF_INET;
}
}
Expand All @@ -249,8 +248,7 @@ unsafe extern "C" fn getaddrinfo(
{
let storage =
alloc::alloc::alloc(addr_layout).cast::<SocketAddrStorage>();
let len =
SocketAddrAny::V6(SocketAddrV6::new(v6, port, 0, 0)).write(storage);
let len = SocketAddrV6::new(v6, port, 0, 0).write_sockaddr(storage);
info.ai_addr = storage.cast();
info.ai_addrlen = len.try_into().unwrap();
info.ai_family = libc::AF_INET6;
Expand Down
16 changes: 8 additions & 8 deletions c-scape/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ cc = { version = "1.0.68", optional = true }

[dependencies]
libm = "0.2.1"
rustix = { version = "0.38.35", default-features = false, features = ["event", "fs", "itoa", "mm", "net", "param", "pipe", "process", "rand", "runtime", "shm", "stdio", "system", "termios", "thread", "time"] }
rustix-futex-sync = { version = "0.2.1", features = ["atomic_usize"] }
rustix = { version = "1.0.0", default-features = false, features = ["event", "fs", "mm", "net", "param", "pipe", "process", "pty", "rand", "runtime", "shm", "stdio", "system", "termios", "thread", "time"] }
rustix-futex-sync = { version = "0.3.0", features = ["atomic_usize"] }
memoffset = "0.9.0"
realpath-ext = { version = "0.1.0", default-features = false }
origin = { version = "0.24.0", default-features = false, features = ["init-fini-arrays", "program-at-exit", "thread-at-exit", "nightly", "getauxval"] }
origin = { version = "0.25.0", default-features = false, features = ["init-fini-arrays", "program-at-exit", "thread-at-exit", "nightly", "getauxval"] }
# We use the libc crate for C ABI types and constants, but we don't depend on
# the actual platform libc.
libc = { version = "0.2.155", default-features = false }
errno = { version = "0.3.3", default-features = false }
rand_pcg = "0.3.1"
rand_core = { version = "0.6.4", features = ["getrandom"] }
rand = { version = "0.8.5", default-features = false }
rustix-dlmalloc = { version = "0.1.0", optional = true }
rustix-openpty = "0.1.1"
rand_pcg = "0.9.0"
rand_core = { version = "0.9.3", features = ["os_rng"] }
rand = { version = "0.9.0", default-features = false }
rustix-dlmalloc = { version = "0.2.1", optional = true }
rustix-openpty = "0.2.0"
bitflags = { version = "2.4.1", default-features = false }
printf-compat = { version = "0.1.1", default-features = false }
num-complex = { version = "0.4.4", default-features = false, features = ["libm"] }
Expand Down
17 changes: 17 additions & 0 deletions c-scape/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ fn main() {
link_in_empty(name);
}
}

let os = var("CARGO_CFG_TARGET_OS").unwrap();

if os == "linux" || os == "l4re" || os == "android" || os == "emscripten" {
use_feature("linux_like");
}

// Add some additional common target combinations.

// Android and "regular" Linux both use the Linux kernel.
if os == "android" || os == "linux" {
use_feature("linux_kernel");
}
}

fn link_in_empty(name: &str) {
Expand Down Expand Up @@ -63,3 +76,7 @@ fn link_in_empty(name: &str) {
);
}
}

fn use_feature(feature: &str) {
println!("cargo:rustc-cfg={}", feature);
}
10 changes: 5 additions & 5 deletions c-scape/src/at_fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ pub(crate) fn at_fork(
///
/// # Safety
///
/// Wildly unsafe. See the documentation comment for [`rustix::runtime::fork`].
/// On top of that, this calls the unsafe functions registered with
/// [`at_fork`].
/// Wildly unsafe. See the documentation comment for
/// [`rustix::runtime::kernel_fork`]. On top of that, this calls the unsafe
/// functions registered with [`at_fork`].
pub(crate) unsafe fn fork() -> rustix::io::Result<Option<rustix::process::Pid>> {
let funcs = FORK_FUNCS.lock();

// Callbacks before calling `fork`.
funcs.prepare.iter().rev().for_each(|func| func());

// Call `fork`.
match rustix::runtime::fork()? {
match rustix::runtime::kernel_fork()? {
rustix::runtime::Fork::Child(pid) => {
// The child's thread record is copied from the parent;
// update it with the child's current-thread-id.
Expand All @@ -72,7 +72,7 @@ pub(crate) unsafe fn fork() -> rustix::io::Result<Option<rustix::process::Pid>>
funcs.child.iter().for_each(|func| func());
Ok(None)
}
rustix::runtime::Fork::Parent(child_pid) => {
rustix::runtime::Fork::ParentOf(child_pid) => {
// Callbacks after calling `fork`, in the parent.
funcs.parent.iter().for_each(|func| func());
Ok(Some(child_pid))
Expand Down
6 changes: 3 additions & 3 deletions c-scape/src/brk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static mut CURRENT: *mut c_void = null_mut();
unsafe extern "C" fn brk(ptr: *mut c_void) -> c_int {
libc!(libc::brk(ptr));

let new = match convert_res(rustix::runtime::brk(ptr)) {
let new = match convert_res(rustix::runtime::kernel_brk(ptr)) {
Some(new) => new,
None => return -1,
};
Expand All @@ -33,7 +33,7 @@ unsafe extern "C" fn sbrk(increment: intptr_t) -> *mut c_void {

if old.is_null() {
// Read the current value from the OS.
old = match convert_res(rustix::runtime::brk(null_mut())) {
old = match convert_res(rustix::runtime::kernel_brk(null_mut())) {
Some(old) => old,
None => return without_provenance_mut(!0),
};
Expand Down Expand Up @@ -61,7 +61,7 @@ unsafe extern "C" fn sbrk(increment: intptr_t) -> *mut c_void {
}

// Install the new address.
let new = match convert_res(rustix::runtime::brk(want)) {
let new = match convert_res(rustix::runtime::kernel_brk(want)) {
Some(new) => new,
None => {
CURRENT = old;
Expand Down
2 changes: 1 addition & 1 deletion c-scape/src/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ unsafe extern "C" fn exit(status: c_int) -> ! {
#[no_mangle]
unsafe extern "C" fn _Exit(status: c_int) -> ! {
//libc!(libc::_Exit(status));
origin::program::exit_immediately(status)
origin::program::immediate_exit(status)
}

/// POSIX-compatible `_exit`.
Expand Down
3 changes: 2 additions & 1 deletion c-scape/src/fs/fadvise.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::convert_res;
use core::num::NonZeroU64;
use errno::{set_errno, Errno};
use libc::{c_int, off64_t, off_t};
use rustix::fd::BorrowedFd;
Expand Down Expand Up @@ -28,7 +29,7 @@ unsafe extern "C" fn posix_fadvise64(
match convert_res(rustix::fs::fadvise(
BorrowedFd::borrow_raw(fd),
offset as u64,
len as u64,
NonZeroU64::new(len as u64),
advice,
)) {
Some(()) => 0,
Expand Down
9 changes: 5 additions & 4 deletions c-scape/src/fs/fcntl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::ffi::VaList;
use errno::{set_errno, Errno};
use rustix::fd::{BorrowedFd, IntoRawFd};
use rustix::fs::{FdFlags, FlockOperation, OFlags};
use rustix::fs::{FlockOperation, OFlags};
use rustix::io::FdFlags;

use libc::c_int;

Expand Down Expand Up @@ -44,7 +45,7 @@ unsafe fn _fcntl<FlockTy: Flock>(fd: c_int, cmd: c_int, mut args: VaList<'_, '_>
libc::F_GETFD => {
libc!(libc::fcntl(fd, libc::F_GETFD));
let fd = BorrowedFd::borrow_raw(fd);
match convert_res(rustix::fs::fcntl_getfd(fd)) {
match convert_res(rustix::io::fcntl_getfd(fd)) {
Some(flags) => flags.bits() as _,
None => -1,
}
Expand All @@ -53,7 +54,7 @@ unsafe fn _fcntl<FlockTy: Flock>(fd: c_int, cmd: c_int, mut args: VaList<'_, '_>
let flags = args.arg::<c_int>();
libc!(libc::fcntl(fd, libc::F_SETFD, flags));
let fd = BorrowedFd::borrow_raw(fd);
match convert_res(rustix::fs::fcntl_setfd(
match convert_res(rustix::io::fcntl_setfd(
fd,
FdFlags::from_bits(flags as _).unwrap(),
)) {
Expand Down Expand Up @@ -100,7 +101,7 @@ unsafe fn _fcntl<FlockTy: Flock>(fd: c_int, cmd: c_int, mut args: VaList<'_, '_>
let arg = args.arg::<c_int>();
libc!(libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, arg));
let fd = BorrowedFd::borrow_raw(fd);
match convert_res(rustix::fs::fcntl_dupfd_cloexec(fd, arg)) {
match convert_res(rustix::io::fcntl_dupfd_cloexec(fd, arg)) {
Some(fd) => fd.into_raw_fd(),
None => -1,
}
Expand Down
6 changes: 3 additions & 3 deletions c-scape/src/fs/inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ unsafe extern "C" fn inotify_init1(flags: c_int) -> c_int {
libc!(libc::inotify_init1(flags));

let flags = CreateFlags::from_bits(flags as _).unwrap();
match convert_res(inotify::inotify_init(flags)) {
match convert_res(inotify::init(flags)) {
Some(fd) => fd.into_raw_fd(),
None => -1,
}
Expand All @@ -28,7 +28,7 @@ unsafe extern "C" fn inotify_add_watch(fd: c_int, pathname: *const c_char, mask:
let fd = BorrowedFd::borrow_raw(fd);
let pathname = CStr::from_ptr(pathname);
let mask = WatchFlags::from_bits(mask).unwrap();
match convert_res(inotify::inotify_add_watch(fd, pathname, mask)) {
match convert_res(inotify::add_watch(fd, pathname, mask)) {
Some(wd) => wd,
None => -1,
}
Expand All @@ -39,7 +39,7 @@ unsafe extern "C" fn inotify_rm_watch(fd: c_int, wd: c_int) -> c_int {
libc!(libc::inotify_rm_watch(fd, wd));

let fd = BorrowedFd::borrow_raw(fd);
match convert_res(inotify::inotify_remove_watch(fd, wd)) {
match convert_res(inotify::remove_watch(fd, wd)) {
Some(()) => 0,
None => -1,
}
Expand Down
4 changes: 2 additions & 2 deletions c-scape/src/fs/lseek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ unsafe extern "C" fn lseek64(fd: c_int, offset: off64_t, whence: c_int) -> off64
libc::SEEK_CUR => rustix::fs::SeekFrom::Current(offset),
libc::SEEK_END => rustix::fs::SeekFrom::End(offset),
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
libc::SEEK_DATA => rustix::fs::SeekFrom::Data(offset),
libc::SEEK_DATA => rustix::fs::SeekFrom::Data(offset as u64),
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
libc::SEEK_HOLE => rustix::fs::SeekFrom::Hole(offset),
libc::SEEK_HOLE => rustix::fs::SeekFrom::Hole(offset as u64),
_ => {
set_errno(Errno(libc::EINVAL));
return -1;
Expand Down
8 changes: 4 additions & 4 deletions c-scape/src/fs/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ptr::{addr_of, addr_of_mut, copy_nonoverlapping};
use errno::{set_errno, Errno};
use libc::{c_char, c_int, time_t};
use rustix::fd::BorrowedFd;
use rustix::fs::{AtFlags, StatExt};
use rustix::fs::AtFlags;

use crate::convert_res;

Expand All @@ -23,11 +23,11 @@ fn rustix_stat_to_libc_stat(
stat.st_size = rustix_stat.st_size.try_into()?;
stat.st_blksize = rustix_stat.st_blksize.try_into()?;
stat.st_blocks = rustix_stat.st_blocks.try_into()?;
stat.st_atime = rustix_stat.atime().try_into()?;
stat.st_atime = rustix_stat.st_atime.try_into()?;
stat.st_atime_nsec = rustix_stat.st_atime_nsec.try_into()?;
stat.st_mtime = rustix_stat.mtime().try_into()?;
stat.st_mtime = rustix_stat.st_mtime.try_into()?;
stat.st_mtime_nsec = rustix_stat.st_mtime_nsec.try_into()?;
stat.st_ctime = rustix_stat.ctime() as time_t;
stat.st_ctime = rustix_stat.st_ctime as time_t;
stat.st_ctime_nsec = rustix_stat.st_ctime_nsec.try_into()?;
Ok(stat)
}
Expand Down
33 changes: 20 additions & 13 deletions c-scape/src/io/epoll.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use rustix::event::epoll::{
add, delete, modify, CreateFlags, Event, EventData, EventFlags, EventVec,
};
use rustix::fd::{BorrowedFd, IntoRawFd};

use crate::convert_res;
use alloc::vec::Vec;
use errno::{set_errno, Errno};
use libc::c_int;

use crate::convert_res;
use rustix::buffer::spare_capacity;
use rustix::event::epoll::{add, delete, modify, CreateFlags, Event, EventData, EventFlags};
use rustix::fd::{BorrowedFd, IntoRawFd};

#[no_mangle]
unsafe extern "C" fn epoll_create(size: c_int) -> c_int {
Expand Down Expand Up @@ -82,18 +80,27 @@ unsafe extern "C" fn epoll_wait(
return -1;
}

// TODO: We should add an `EventVec::from_raw_parts` to allow `epoll_wait`
let timeout = if timeout < 0 {
None
} else {
Some(rustix::event::Timespec {
tv_sec: i64::from(timeout) / 1000,
tv_nsec: (i64::from(timeout) % 1000) * 1_000_000,
})
};

// TODO: We should use `Vec::from_raw_parts` to allow `epoll_wait`
// to write events directly into the user's buffer, rather then allocating
// and copying here.
let mut events_vec = EventVec::with_capacity(maxevents as usize);
let mut events_vec = Vec::with_capacity(maxevents as usize);
match convert_res(rustix::event::epoll::wait(
BorrowedFd::borrow_raw(epfd),
&mut events_vec,
timeout,
spare_capacity(&mut events_vec),
timeout.as_ref(),
)) {
Some(()) => {
Some(_) => {
let mut events = events;
for Event { flags, data } in events_vec.iter() {
for Event { flags, data } in events_vec.iter().copied() {
events.write(libc::epoll_event {
events: flags.bits(),
r#u64: data.u64(),
Expand Down
10 changes: 9 additions & 1 deletion c-scape/src/io/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ unsafe extern "C" fn poll(fds: *mut libc::pollfd, nfds: libc::nfds_t, timeout: c
let pollfds: *mut rustix::event::PollFd<'_> = checked_cast!(fds);

let fds = slice::from_raw_parts_mut(pollfds, nfds.try_into().unwrap());
match convert_res(rustix::event::poll(fds, timeout)) {
let timeout = if timeout < 0 {
None
} else {
Some(rustix::event::Timespec {
tv_sec: i64::from(timeout) / 1000,
tv_nsec: (i64::from(timeout) % 1000) * 1_000_000,
})
};
match convert_res(rustix::event::poll(fds, timeout.as_ref())) {
Some(num) => num.try_into().unwrap(),
None => -1,
}
Expand Down
Loading