Skip to content

Commit

Permalink
Very rough very experimental very preliminary NuttX proof-of-concept …
Browse files Browse the repository at this point in the history
…port.
  • Loading branch information
sunfishcode committed Aug 16, 2022
1 parent a2c6c7a commit 1bf77b0
Show file tree
Hide file tree
Showing 31 changed files with 237 additions and 31 deletions.
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include = ["src", "build.rs", "Cargo.toml", "COPYRIGHT", "LICENSE*", "/*.md"]
cc = { version = "1.0.68", optional = true }

[dependencies]
bitflags = "1.2.1"
bitflags = { version = "1.2.1", default-features = false }
itoa = { version = "1.0.1", default-features = false, optional = true }
io-lifetimes = { version = "0.7.0", default-features = false, optional = true }

Expand All @@ -27,6 +27,9 @@ core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core
alloc = { version = "1.0.0", optional = true, package = "rustc-std-workspace-alloc" }
compiler_builtins = { version = '0.1.49', optional = true }

# The NuttX API.
nuttx-sys = { git = "https://github.com/sunfishcode/nuttx-sys", features = ["build_flat", "pipes", "dev_pipe", "sched_user_identity", "sched_have_parent"] }

# The procfs feature needs once_cell.
[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
once_cell = { version = "1.5.2", optional = true }
Expand All @@ -39,16 +42,16 @@ once_cell = { version = "1.5.2", optional = true }
# `RUSTFLAGS` or enabling the `use-libc` cargo feature.
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", any(target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"), all(target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "mips", target_arch = "mips64")))))'.dependencies]
linux-raw-sys = { version = "0.0.46", default-features = false, features = ["general", "errno", "ioctl", "no_std"] }
libc_errno = { package = "errno", version = "0.2.8", default-features = false, optional = true }
libc = { version = "0.2.126", features = ["extra_traits"], optional = true }
#libc_errno = { package = "errno", version = "0.2.8", default-features = false, optional = true }
#libc = { version = "0.2.126", features = ["extra_traits"], optional = true }

# Dependencies for platforms where only libc is supported:
#
# On all other Unix-family platforms, and under Miri, we always use the libc
# backend, so enable its dependencies unconditionally.
[target.'cfg(any(rustix_use_libc, miri, not(all(target_os = "linux", any(target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"), all(target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "mips", target_arch = "mips64")))))))'.dependencies]
libc_errno = { package = "errno", version = "0.2.8", default-features = false }
libc = { version = "0.2.126", features = ["extra_traits"] }
#libc_errno = { package = "errno", version = "0.2.8", default-features = false }
#libc = { version = "0.2.126", features = ["extra_traits"] }

# Additional dependencies for Linux with the libc backend:
#
Expand Down Expand Up @@ -122,7 +125,7 @@ rustc-dep-of-std = [
]

# Enable this to request the libc backend.
use-libc = ["libc_errno", "libc"]
use-libc = []

# Enable `rustix::fs::*`.
fs = []
Expand Down Expand Up @@ -179,7 +182,7 @@ all-apis = [
# When using the linux_raw backend, and not using Mustang, should we use libc
# for reading the aux vectors, instead of reading them ourselves from
# /proc/self/auxv?
use-libc-auxv = ["libc"]
use-libc-auxv = []

# Expose io-lifetimes' features for third-party crate impls.
async-std = ["io-lifetimes/async-std"]
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {

// Check for `--features=use-libc`. This allows crate users to enable the
// libc backend.
let feature_use_libc = var("CARGO_FEATURE_USE_LIBC").is_ok();
let feature_use_libc = true; // For now, always enable this on the NuttX branch.

// Check for `--features=rustc-dep-of-std`. This is used when rustix is
// being used to build std, in which case `can_compile` doesn't work
Expand Down
2 changes: 2 additions & 0 deletions src/backend/libc/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
#[cfg(not(target_os = "redox"))]
#[cfg(any(feature = "fs", feature = "procfs"))]
pub(crate) mod dir;
Expand All @@ -13,6 +14,7 @@ pub(crate) mod dir;
target_os = "wasi",
)))]
pub(crate) mod makedev;
*/
#[cfg(not(windows))]
pub(crate) mod syscalls;
pub(crate) mod types;
24 changes: 22 additions & 2 deletions src/backend/libc/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::super::conv::{
};
#[cfg(any(target_os = "android", target_os = "linux"))]
use super::super::conv::{syscall_ret, syscall_ret_owned_fd, syscall_ret_ssize_t};
/*
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
use super::super::offset::libc_fallocate;
#[cfg(not(any(
Expand All @@ -31,16 +32,19 @@ use super::super::offset::libc_posix_fadvise;
target_os = "redox",
)))]
use super::super::offset::libc_posix_fallocate;
use super::super::offset::{libc_fstat, libc_fstatat, libc_ftruncate, libc_lseek, libc_off_t};
*/
use super::super::offset::{libc_fstat, /*libc_fstatat,*/ libc_ftruncate, libc_lseek, libc_off_t};
#[cfg(not(any(
target_os = "illumos",
target_os = "netbsd",
target_os = "redox",
target_os = "wasi",
)))]
use super::super::offset::{libc_fstatfs, libc_statfs};
/*
#[cfg(not(any(target_os = "illumos", target_os = "redox", target_os = "wasi")))]
use super::super::offset::{libc_fstatvfs, libc_statvfs};
*/
#[cfg(all(
any(target_arch = "arm", target_arch = "mips", target_arch = "x86"),
target_env = "gnu",
Expand All @@ -54,6 +58,7 @@ use crate::ffi::CStr;
use crate::ffi::CString;
#[cfg(not(target_os = "illumos"))]
use crate::fs::Access;
/*
#[cfg(not(any(
target_os = "dragonfly",
target_os = "illumos",
Expand All @@ -74,6 +79,7 @@ use crate::fs::Advice;
use crate::fs::FallocateFlags;
#[cfg(not(target_os = "wasi"))]
use crate::fs::FlockOperation;
*/
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
use crate::fs::MemfdFlags;
#[cfg(any(
Expand All @@ -100,8 +106,10 @@ use crate::fs::{cwd, RenameFlags, ResolveFlags, Statx, StatxFlags};
)))]
use crate::fs::{Dev, FileType};
use crate::fs::{FdFlags, Mode, OFlags, Stat, Timestamps};
/*
#[cfg(not(any(target_os = "illumos", target_os = "redox", target_os = "wasi")))]
use crate::fs::{StatVfs, StatVfsMountFlags};
*/
use crate::io::{self, OwnedFd, SeekFrom};
#[cfg(not(target_os = "wasi"))]
use crate::process::{Gid, Uid};
Expand Down Expand Up @@ -133,6 +141,7 @@ use {
super::super::conv::nonnegative_ret,
crate::fs::{copyfile_state_t, CloneFlags, CopyfileFlags},
};
/*
#[cfg(not(target_os = "redox"))]
use {super::super::offset::libc_openat, crate::fs::AtFlags};
Expand Down Expand Up @@ -197,6 +206,7 @@ pub(crate) fn openat(
))
}
}
*/

#[cfg(not(any(
target_os = "illumos",
Expand All @@ -213,6 +223,7 @@ pub(crate) fn statfs(filename: &CStr) -> io::Result<StatFs> {
}
}

/*
#[cfg(not(any(target_os = "illumos", target_os = "redox", target_os = "wasi")))]
#[inline]
pub(crate) fn statvfs(filename: &CStr) -> io::Result<StatVfs> {
Expand Down Expand Up @@ -806,6 +817,7 @@ pub(crate) fn fadvise(fd: BorrowedFd<'_>, offset: u64, len: u64, advice: Advice)
Err(io::Errno(err))
}
}
*/

pub(crate) fn fcntl_getfd(fd: BorrowedFd<'_>) -> io::Result<FdFlags> {
unsafe { ret_c_int(c::fcntl(borrowed_fd(fd), c::F_GETFD)).map(FdFlags::from_bits_truncate) }
Expand Down Expand Up @@ -846,6 +858,7 @@ pub(crate) fn fcntl_add_seals(fd: BorrowedFd<'_>, seals: SealFlags) -> io::Resul
unsafe { ret(c::fcntl(borrowed_fd(fd), c::F_ADD_SEALS, seals.bits())) }
}

/*
#[cfg(not(target_os = "wasi"))]
pub(crate) fn fcntl_dupfd_cloexec(fd: BorrowedFd<'_>, min: RawFd) -> io::Result<OwnedFd> {
unsafe { ret_owned_fd(c::fcntl(borrowed_fd(fd), c::F_DUPFD_CLOEXEC, min)) }
Expand All @@ -864,6 +877,7 @@ pub(crate) fn seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> {
let offset = unsafe { ret_off_t(libc_lseek(borrowed_fd(fd), offset, whence))? };
Ok(offset as u64)
}
*/

pub(crate) fn tell(fd: BorrowedFd<'_>) -> io::Result<u64> {
let offset = unsafe { ret_off_t(libc_lseek(borrowed_fd(fd), 0, c::SEEK_CUR))? };
Expand Down Expand Up @@ -910,10 +924,12 @@ pub(crate) fn fchown(fd: BorrowedFd<'_>, owner: Option<Uid>, group: Option<Gid>)
}
}

/*
#[cfg(not(target_os = "wasi"))]
pub(crate) fn flock(fd: BorrowedFd<'_>, operation: FlockOperation) -> io::Result<()> {
unsafe { ret(c::flock(borrowed_fd(fd), operation as c::c_int)) }
}
*/

pub(crate) fn fstat(fd: BorrowedFd<'_>) -> io::Result<Stat> {
// 32-bit and mips64 Linux: `struct stat64` is not y2038 compatible; use
Expand Down Expand Up @@ -969,6 +985,7 @@ pub(crate) fn fstatfs(fd: BorrowedFd<'_>) -> io::Result<StatFs> {
}
}

/*
#[cfg(not(any(target_os = "illumos", target_os = "redox", target_os = "wasi")))]
pub(crate) fn fstatvfs(fd: BorrowedFd<'_>) -> io::Result<StatVfs> {
let mut statvfs = MaybeUninit::<libc_statvfs>::uninit();
Expand All @@ -994,6 +1011,7 @@ fn libc_statvfs_to_statvfs(from: libc_statvfs) -> StatVfs {
f_namemax: from.f_namemax as u64,
}
}
*/

pub(crate) fn futimens(fd: BorrowedFd<'_>, times: &Timestamps) -> io::Result<()> {
// 32-bit gnu version: libc has `futimens` but it is not y2038 safe by default.
Expand Down Expand Up @@ -1096,6 +1114,7 @@ unsafe fn futimens_old(fd: BorrowedFd<'_>, times: &Timestamps) -> io::Result<()>
ret(c::futimens(borrowed_fd(fd), old_times.as_ptr()))
}

/*
#[cfg(not(any(
target_os = "dragonfly",
target_os = "illumos",
Expand Down Expand Up @@ -1133,6 +1152,7 @@ pub(crate) fn fallocate(
}
}
}
*/

#[cfg(any(target_os = "ios", target_os = "macos"))]
pub(crate) fn fallocate(
Expand Down Expand Up @@ -1174,7 +1194,7 @@ pub(crate) fn fsync(fd: BorrowedFd<'_>) -> io::Result<()> {
target_os = "redox",
)))]
pub(crate) fn fdatasync(fd: BorrowedFd<'_>) -> io::Result<()> {
unsafe { ret(c::fdatasync(borrowed_fd(fd))) }
unsafe { ret(c::fsync(borrowed_fd(fd))) }
}

pub(crate) fn ftruncate(fd: BorrowedFd<'_>, length: u64) -> io::Result<()> {
Expand Down
14 changes: 13 additions & 1 deletion src/backend/libc/fs/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ bitflags! {
}
}

/*
#[cfg(not(target_os = "redox"))]
bitflags! {
/// `AT_*` constants for use with [`openat`], [`statat`], and other `*at`
Expand Down Expand Up @@ -73,6 +74,7 @@ bitflags! {
const STATX_DONT_SYNC = c::AT_STATX_DONT_SYNC;
}
}
*/

bitflags! {
/// `S_I*` constants for use with [`openat`], [`chmodat`], and [`fchmod`].
Expand Down Expand Up @@ -204,8 +206,10 @@ bitflags! {
))]
const FSYNC = c::O_FSYNC;

/*
/// `O_NOFOLLOW`
const NOFOLLOW = c::O_NOFOLLOW;
*/

/// `O_NONBLOCK`
const NONBLOCK = c::O_NONBLOCK;
Expand Down Expand Up @@ -456,6 +460,7 @@ impl FileType {
}
}

/*
/// `POSIX_FADV_*` constants for use with [`fadvise`].
///
/// [`fadvise`]: crate::fs::fadvise
Expand Down Expand Up @@ -489,6 +494,7 @@ pub enum Advice {
/// `POSIX_FADV_DONTNEED`
DontNeed = c::POSIX_FADV_DONTNEED as c::c_uint,
}
*/

#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
bitflags! {
Expand Down Expand Up @@ -680,6 +686,7 @@ bitflags! {
}
}

/*
#[cfg(not(any(
target_os = "illumos",
target_os = "netbsd",
Expand Down Expand Up @@ -777,7 +784,9 @@ bitflags! {
const UNSHARE_RANGE = c::FALLOC_FL_UNSHARE_RANGE;
}
}
*/

/*
#[cfg(not(any(target_os = "illumos", target_os = "redox", target_os = "wasi",)))]
bitflags! {
/// `ST_*` constants for use with [`StatVfs`].
Expand Down Expand Up @@ -838,6 +847,7 @@ pub enum FlockOperation {
/// `LOCK_UN | LOCK_NB`
NonBlockingUnlock = c::LOCK_UN | c::LOCK_NB,
}
*/

/// `struct stat` for use with [`statat`] and [`fstat`].
///
Expand Down Expand Up @@ -943,7 +953,7 @@ pub struct StatVfs {
pub f_ffree: u64,
pub f_favail: u64,
pub f_fsid: u64,
pub f_flag: StatVfsMountFlags,
/*pub f_flag: StatVfsMountFlags, */
pub f_namemax: u64,
}

Expand Down Expand Up @@ -1055,8 +1065,10 @@ pub type FsWord = u64;
#[cfg(all(target_os = "linux", target_arch = "s390x"))]
pub type FsWord = u32;

/*
#[cfg(not(target_os = "redox"))]
pub use c::{UTIME_NOW, UTIME_OMIT};
*/

/// `PROC_SUPER_MAGIC`—The magic number for the procfs filesystem.
#[cfg(all(
Expand Down
Loading

0 comments on commit 1bf77b0

Please sign in to comment.