Introduce namespace fd and related features
This commit is contained in:
committed by
Jeremy Soller
parent
896c40507f
commit
1bcb6dff78
Generated
+15
-6
@@ -232,6 +232,12 @@ dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ioslice"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e571352c8a3b89074d12e3ee5173ffe162159105352aaaf1fc5764da747e31b"
|
||||
|
||||
[[package]]
|
||||
name = "ld_so"
|
||||
version = "0.1.0"
|
||||
@@ -250,9 +256,9 @@ checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
|
||||
|
||||
[[package]]
|
||||
name = "libredox"
|
||||
version = "0.1.11"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df15f6eac291ed1cf25865b1ee60399f57e7c227e7f51bdbd4c5270396a9ed50"
|
||||
checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"libc",
|
||||
@@ -438,6 +444,7 @@ dependencies = [
|
||||
"bitflags",
|
||||
"generic-rt",
|
||||
"goblin",
|
||||
"ioslice",
|
||||
"plain",
|
||||
"redox-path",
|
||||
"redox_syscall",
|
||||
@@ -445,9 +452,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "redox_event"
|
||||
version = "0.4.2"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8eafe403549fab08a0c0862769493ad992625bb5a694aae2df6901e7e4fd05a"
|
||||
checksum = "c30b73c93693667b5a8e6f54bddcc5a57449ef17c99f4b55c12d5bae4ab79e82"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"libredox",
|
||||
@@ -456,8 +463,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.6.0"
|
||||
source = "git+https://gitlab.redox-os.org/redox-os/syscall.git?branch=master#1cf631ce3f36838ababdbd2b0190d93a6b64a31b"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49f3fe0889e69e2ae9e41f4d6c4c0181701d00e4697b356fb1f74173a5e0ee27"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
@@ -476,6 +484,7 @@ dependencies = [
|
||||
"chrono-tz",
|
||||
"dlmalloc",
|
||||
"generic-rt",
|
||||
"ioslice",
|
||||
"libc",
|
||||
"libm",
|
||||
"md-5",
|
||||
|
||||
+3
-3
@@ -62,12 +62,13 @@ features = ["c_api"]
|
||||
sc = "0.2.7"
|
||||
|
||||
[target.'cfg(target_os = "redox")'.dependencies]
|
||||
redox_syscall = "0.6.0"
|
||||
redox_syscall = "0.7"
|
||||
redox-rt = { path = "redox-rt" }
|
||||
redox-path = "0.3"
|
||||
redox_event = { version = "0.4.2", default-features = false, features = [
|
||||
redox_event = { version = "0.4.3", default-features = false, features = [
|
||||
"redox_syscall",
|
||||
] }
|
||||
ioslice = { version = "0.6", default-features = false }
|
||||
redox-ioctl = { path = "redox-ioctl" }
|
||||
|
||||
[features]
|
||||
@@ -83,4 +84,3 @@ panic = "abort"
|
||||
|
||||
[patch.crates-io]
|
||||
cc-11 = { git = "https://github.com/tea/cc-rs", branch = "riscv-abi-arch-fix", package = "cc" }
|
||||
redox_syscall = { git = "https://gitlab.redox-os.org/redox-os/syscall.git", branch = "master" }
|
||||
|
||||
@@ -8,6 +8,6 @@ description = "Ioctl definitions and (de)serialization for Redox"
|
||||
|
||||
[dependencies]
|
||||
drm-sys = "0.8.0"
|
||||
redox_syscall = "0.6.0"
|
||||
redox_syscall = "0.7"
|
||||
|
||||
[features]
|
||||
|
||||
+2
-1
@@ -12,7 +12,8 @@ description = "Libc-independent runtime for Redox"
|
||||
bitflags = "2"
|
||||
goblin = { version = "0.7", default-features = false, features = ["elf32", "elf64", "endian_fd"] }
|
||||
plain = "0.2"
|
||||
redox_syscall = "0.6.0"
|
||||
ioslice = { version = "0.6", default-features = false }
|
||||
redox_syscall = "0.7"
|
||||
redox-path = "0.3.0"
|
||||
|
||||
generic-rt = { path = "../generic-rt" }
|
||||
|
||||
+16
-1
@@ -186,7 +186,10 @@ pub(crate) fn read_proc_meta(proc: &FdGuardUpper) -> syscall::Result<ProcMeta> {
|
||||
proc.read(&mut bytes)?;
|
||||
Ok(*plain::from_bytes::<ProcMeta>(&bytes).unwrap())
|
||||
}
|
||||
pub unsafe fn initialize(#[cfg(feature = "proc")] proc_fd: FdGuardUpper) {
|
||||
pub unsafe fn initialize(
|
||||
#[cfg(feature = "proc")] proc_fd: FdGuardUpper,
|
||||
#[cfg(feature = "proc")] ns_fd: Option<FdGuardUpper>,
|
||||
) {
|
||||
#[cfg(feature = "proc")]
|
||||
let metadata = read_proc_meta(&proc_fd).unwrap();
|
||||
|
||||
@@ -223,6 +226,7 @@ pub unsafe fn initialize(#[cfg(feature = "proc")] proc_fd: FdGuardUpper) {
|
||||
egid: metadata.egid,
|
||||
rgid: metadata.rgid,
|
||||
sgid: metadata.sgid,
|
||||
ns_fd,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -241,6 +245,7 @@ pub struct DynamicProcInfo {
|
||||
pub egid: u32,
|
||||
pub rgid: u32,
|
||||
pub sgid: u32,
|
||||
pub ns_fd: Option<FdGuardUpper>,
|
||||
}
|
||||
|
||||
static DYNAMIC_PROC_INFO: Mutex<DynamicProcInfo> = Mutex::new(DynamicProcInfo {
|
||||
@@ -251,6 +256,7 @@ static DYNAMIC_PROC_INFO: Mutex<DynamicProcInfo> = Mutex::new(DynamicProcInfo {
|
||||
rgid: u32::MAX,
|
||||
egid: u32::MAX,
|
||||
sgid: u32::MAX,
|
||||
ns_fd: None,
|
||||
});
|
||||
|
||||
#[inline]
|
||||
@@ -263,6 +269,15 @@ pub fn current_proc_fd() -> &'static FdGuardUpper {
|
||||
assert!(info.has_proc_fd);
|
||||
unsafe { info.proc_fd.assume_init_ref() }
|
||||
}
|
||||
#[inline]
|
||||
pub fn current_namespace_fd() -> usize {
|
||||
DYNAMIC_PROC_INFO
|
||||
.lock()
|
||||
.ns_fd
|
||||
.as_ref()
|
||||
.map(|g| g.as_raw_fd())
|
||||
.unwrap_or(usize::MAX)
|
||||
}
|
||||
|
||||
struct ChildHookCommonArgs {
|
||||
new_thr_fd: FdGuard,
|
||||
|
||||
+27
-12
@@ -11,7 +11,7 @@ use crate::{
|
||||
auxv_defs::*,
|
||||
protocol::{ProcCall, ThreadCall},
|
||||
read_proc_meta,
|
||||
sys::{proc_call, thread_call},
|
||||
sys::{open, proc_call, thread_call},
|
||||
};
|
||||
|
||||
use alloc::{boxed::Box, vec};
|
||||
@@ -61,6 +61,8 @@ pub struct ExtraInfo<'a> {
|
||||
pub thr_fd: usize,
|
||||
/// Process handle
|
||||
pub proc_fd: usize,
|
||||
/// Namespace handle
|
||||
pub ns_fd: Option<usize>,
|
||||
}
|
||||
|
||||
pub fn fexec_impl(
|
||||
@@ -354,6 +356,8 @@ pub fn fexec_impl(
|
||||
push(AT_REDOX_THR_FD)?;
|
||||
push(extrainfo.proc_fd as usize)?;
|
||||
push(AT_REDOX_PROC_FD)?;
|
||||
push(extrainfo.ns_fd.unwrap_or(usize::MAX))?;
|
||||
push(AT_REDOX_NS_FD)?;
|
||||
|
||||
push(0)?;
|
||||
|
||||
@@ -710,7 +714,7 @@ impl FdGuard<false> {
|
||||
|
||||
#[inline]
|
||||
pub fn open<T: AsRef<str>>(path: T, flags: usize) -> Result<Self> {
|
||||
syscall::open(path, flags).map(Self::new)
|
||||
open(path, flags).map(Self::new)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -764,6 +768,21 @@ impl<const UPPER: bool> FdGuard<UPPER> {
|
||||
syscall::write(self.fd, buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn call_ro(&self, payload: &mut [u8], flags: CallFlags, metadata: &[u64]) -> Result<usize> {
|
||||
syscall::call_ro(self.fd, payload, flags, metadata)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn call_wo(&self, payload: &[u8], flags: CallFlags, metadata: &[u64]) -> Result<usize> {
|
||||
syscall::call_wo(self.fd, payload, flags, metadata)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn call_rw(&self, payload: &mut [u8], flags: CallFlags, metadata: &[u64]) -> Result<usize> {
|
||||
syscall::call_rw(self.fd, payload, flags, metadata)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_raw_fd(&self) -> usize {
|
||||
self.fd
|
||||
@@ -798,9 +817,7 @@ pub fn create_set_addr_space_buf(
|
||||
sp: usize,
|
||||
) -> [u8; size_of::<usize>() * 3] {
|
||||
let mut buf = [0u8; size_of::<usize>() * 3];
|
||||
|
||||
buf.copy_from_slice([space, sp, ip].map(usize::to_ne_bytes).as_flattened());
|
||||
|
||||
buf
|
||||
}
|
||||
pub fn create_set_addr_space_buf_for_fork(
|
||||
@@ -871,7 +888,6 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result<usize> {
|
||||
new_thr_fd: new_thr_fd.as_raw_fd(),
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(any(
|
||||
target_arch = "x86_64",
|
||||
target_arch = "aarch64",
|
||||
@@ -882,11 +898,9 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result<usize> {
|
||||
scratchpad_ptr as usize
|
||||
};
|
||||
#[cfg(target_arch = "x86")]
|
||||
{
|
||||
unsafe {
|
||||
let scratchpad_ptr = initial_rsp as *mut ForkScratchpad;
|
||||
unsafe {
|
||||
scratchpad_ptr.write(scratchpad);
|
||||
}
|
||||
scratchpad_ptr.write(scratchpad);
|
||||
}
|
||||
|
||||
// CoW-duplicate address space.
|
||||
@@ -1020,7 +1034,6 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result<usize> {
|
||||
}
|
||||
let start_fd = new_thr_fd.dup(b"start")?;
|
||||
start_fd.write(&[0])?;
|
||||
|
||||
Ok(new_pid)
|
||||
}
|
||||
|
||||
@@ -1079,12 +1092,13 @@ pub fn new_child_process(args: &ForkArgs<'_>) -> Result<NewChildProc> {
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn make_init() -> (&'static FdGuardUpper, &'static FdGuardUpper) {
|
||||
pub unsafe fn make_init(proc_cap: usize) -> (&'static FdGuardUpper, &'static FdGuardUpper) {
|
||||
let proc_fd = FdGuard::new(
|
||||
syscall::open("/scheme/proc/init", syscall::O_CLOEXEC).expect("failed to create init"),
|
||||
syscall::openat(proc_cap, "init", syscall::O_CLOEXEC, 0).expect("failed to create init"),
|
||||
)
|
||||
.to_upper()
|
||||
.unwrap();
|
||||
|
||||
syscall::sendfd(
|
||||
proc_fd.as_raw_fd(),
|
||||
RtTcb::current().thread_fd().dup(&[]).unwrap().take(),
|
||||
@@ -1116,6 +1130,7 @@ pub unsafe fn make_init() -> (&'static FdGuardUpper, &'static FdGuardUpper) {
|
||||
rgid: 0,
|
||||
egid: 0,
|
||||
sgid: 0,
|
||||
ns_fd: None,
|
||||
};
|
||||
(
|
||||
unsafe { (*STATIC_PROC_INFO.get()).proc_fd.assume_init_ref() },
|
||||
|
||||
@@ -250,3 +250,35 @@ pub const SIGWINCH: usize = 28;
|
||||
pub const SIGIO: usize = 29;
|
||||
pub const SIGPWR: usize = 30;
|
||||
pub const SIGSYS: usize = 31;
|
||||
|
||||
bitflags! {
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, Ord, Hash, PartialEq, PartialOrd)]
|
||||
pub struct NsPermissions: usize {
|
||||
/// List schemes in the namespace
|
||||
const LIST = 1 << 0;
|
||||
/// Register a new scheme in the namespace
|
||||
const INSERT = 1 << 1;
|
||||
/// Delete a scheme from the namespace
|
||||
const DELETE = 1 << 2;
|
||||
/// Get scheme creation capabilities of the namespace
|
||||
const SCHEME_CREATE = 1 << 3;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
#[repr(usize)]
|
||||
pub enum NsDup {
|
||||
ForkNs = 0,
|
||||
ShrinkPermissions = 1,
|
||||
IssueRegister = 2,
|
||||
}
|
||||
impl NsDup {
|
||||
pub fn try_from_raw(raw: usize) -> Option<Self> {
|
||||
Some(match raw {
|
||||
0 => Self::ForkNs,
|
||||
1 => Self::ShrinkPermissions,
|
||||
2 => Self::IssueRegister,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+65
-36
@@ -1,21 +1,24 @@
|
||||
use core::{
|
||||
mem::size_of,
|
||||
mem::{replace, size_of},
|
||||
ptr::addr_of,
|
||||
sync::atomic::{AtomicU32, Ordering},
|
||||
};
|
||||
|
||||
use ioslice::IoSlice;
|
||||
use syscall::{
|
||||
CallFlags, EINVAL, ERESTART, TimeSpec,
|
||||
error::{self, EINTR, Error, Result},
|
||||
error::{self, EINTR, ENODEV, Error, Result},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
DYNAMIC_PROC_INFO, DynamicProcInfo, RtTcb, Tcb,
|
||||
arch::manually_enter_trampoline,
|
||||
protocol::{ProcCall, ProcKillTarget, RtSigInfo, ThreadCall, WaitFlags},
|
||||
proc::{FdGuard, FdGuardUpper},
|
||||
protocol::{NsDup, ProcCall, ProcKillTarget, RtSigInfo, ThreadCall, WaitFlags},
|
||||
read_proc_meta,
|
||||
signal::tmp_disable_signals,
|
||||
};
|
||||
use alloc::vec::Vec;
|
||||
|
||||
#[inline]
|
||||
fn wrapper<T>(restart: bool, erestart: bool, mut f: impl FnMut() -> Result<T>) -> Result<T> {
|
||||
@@ -39,31 +42,6 @@ fn wrapper<T>(restart: bool, erestart: bool, mut f: impl FnMut() -> Result<T>) -
|
||||
return res;
|
||||
}
|
||||
}
|
||||
pub fn unlink<T: AsRef<str>>(path: T, flags: usize) -> Result<usize> {
|
||||
let redox_path = redox_path::RedoxPath::from_absolute(path.as_ref())
|
||||
.expect("path must be canonicalized beforehand");
|
||||
let (scheme, reference) = redox_path.as_parts().unwrap();
|
||||
let root_path = if scheme.as_ref().is_empty() {
|
||||
alloc::string::String::from(":")
|
||||
} else {
|
||||
alloc::format!("/scheme/{}", scheme)
|
||||
};
|
||||
// TODO: Temporary workaround to remove unlink and rmdir
|
||||
let root_fd = crate::proc::FdGuard::open(
|
||||
&root_path,
|
||||
syscall::O_DIRECTORY | syscall::O_RDONLY | syscall::O_CLOEXEC,
|
||||
)?;
|
||||
let path = reference.as_ref();
|
||||
unsafe {
|
||||
syscall::syscall4(
|
||||
syscall::SYS_UNLINKAT,
|
||||
root_fd.as_raw_fd(),
|
||||
path.as_ptr() as usize,
|
||||
path.len(),
|
||||
flags,
|
||||
)
|
||||
}
|
||||
}
|
||||
// TODO: uninitialized memory?
|
||||
#[inline]
|
||||
pub fn posix_read(fd: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
@@ -368,14 +346,6 @@ pub fn posix_exit(status: i32) -> ! {
|
||||
let _ = syscall::write(1, b"redox-rt: ProcCall::Exit FAILED, abort()ing!\n");
|
||||
core::intrinsics::abort();
|
||||
}
|
||||
pub fn setrens(rns: usize, ens: usize) -> Result<()> {
|
||||
this_proc_call(
|
||||
&mut [],
|
||||
CallFlags::empty(),
|
||||
&[ProcCall::Setrens as u64, rns as u64, ens as u64],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn posix_getpgid(pid: usize) -> Result<usize> {
|
||||
this_proc_call(
|
||||
&mut [],
|
||||
@@ -409,3 +379,62 @@ pub fn posix_nanosleep(rqtp: &TimeSpec, rmtp: &mut TimeSpec) -> Result<()> {
|
||||
wrapper(false, false, || syscall::nanosleep(rqtp, rmtp))?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn setns(fd: usize) -> Option<FdGuardUpper> {
|
||||
let mut info = DYNAMIC_PROC_INFO.lock();
|
||||
let new_fd_guard = FdGuard::new(fd).to_upper().unwrap();
|
||||
let old_fd_guard = replace(&mut info.ns_fd, Some(new_fd_guard));
|
||||
old_fd_guard
|
||||
}
|
||||
pub fn getns() -> Result<usize> {
|
||||
let cur_ns = crate::current_namespace_fd();
|
||||
if cur_ns == usize::MAX {
|
||||
Err(Error::new(ENODEV))
|
||||
} else {
|
||||
Ok(cur_ns)
|
||||
}
|
||||
}
|
||||
pub fn open<T: AsRef<str>>(path: T, flags: usize) -> Result<usize> {
|
||||
let path = path.as_ref();
|
||||
let fcntl_flags = flags & syscall::O_FCNTL_MASK;
|
||||
unsafe {
|
||||
syscall::syscall5(
|
||||
syscall::SYS_OPENAT,
|
||||
crate::current_namespace_fd(),
|
||||
path.as_ptr() as usize,
|
||||
path.len(),
|
||||
flags,
|
||||
fcntl_flags,
|
||||
)
|
||||
}
|
||||
}
|
||||
pub fn unlink<T: AsRef<str>>(path: T, flags: usize) -> Result<usize> {
|
||||
let path = path.as_ref();
|
||||
unsafe {
|
||||
syscall::syscall4(
|
||||
syscall::SYS_UNLINKAT,
|
||||
crate::current_namespace_fd(),
|
||||
path.as_ptr() as usize,
|
||||
path.len(),
|
||||
flags,
|
||||
)
|
||||
}
|
||||
}
|
||||
pub fn mkns(names: &[IoSlice]) -> Result<FdGuardUpper> {
|
||||
let mut buf = Vec::from((NsDup::ForkNs as usize).to_ne_bytes());
|
||||
for name in names {
|
||||
let name_bytes = name.as_slice();
|
||||
let len = name_bytes.len();
|
||||
let _scheme_name = core::str::from_utf8(name_bytes).map_err(|_| Error::new(EINVAL))?;
|
||||
buf.extend_from_slice(&len.to_ne_bytes());
|
||||
buf.extend_from_slice(name_bytes);
|
||||
}
|
||||
FdGuard::new(syscall::dup(crate::current_namespace_fd(), &buf)?).to_upper()
|
||||
}
|
||||
pub fn register_scheme_to_ns(ns_fd: usize, name: &str, cap_fd: usize) -> Result<()> {
|
||||
let mut buf = alloc::vec::Vec::from((NsDup::IssueRegister as usize).to_ne_bytes());
|
||||
buf.extend_from_slice(name.as_bytes());
|
||||
let ns_this_scheme = FdGuard::new(syscall::dup(ns_fd, &buf)?);
|
||||
let cap_bytes = cap_fd.to_ne_bytes();
|
||||
ns_this_scheme.call_wo(&cap_bytes, CallFlags::FD, &[])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+14
-1
@@ -175,10 +175,22 @@ pub unsafe extern "C" fn relibc_ld_so_start(sp: &'static mut Stack, ld_entry: us
|
||||
)
|
||||
.expect_notls("no proc fd present");
|
||||
|
||||
let ns_fd = crate::platform::get_auxv_raw(
|
||||
sp.auxv().cast(),
|
||||
redox_rt::auxv_defs::AT_REDOX_NS_FD,
|
||||
)
|
||||
.filter(|&fd| fd != usize::MAX)
|
||||
.map(|fd| {
|
||||
redox_rt::proc::FdGuard::new(fd)
|
||||
.to_upper()
|
||||
.expect_notls("failed to move ns fd to upper table")
|
||||
});
|
||||
|
||||
redox_rt::initialize(
|
||||
redox_rt::proc::FdGuard::new(proc_fd)
|
||||
.to_upper()
|
||||
.expect_notls("failed to move proc fd to upper table"),
|
||||
ns_fd,
|
||||
);
|
||||
redox_rt::signal::setup_sighandler(&tcb.os_specific, true);
|
||||
}
|
||||
@@ -224,8 +236,9 @@ pub unsafe extern "C" fn relibc_ld_so_start(sp: &'static mut Stack, ld_entry: us
|
||||
_r_debug.lock().r_ldbase = ld_entry;
|
||||
|
||||
// TODO: Fix memory leak, although minimal.
|
||||
#[cfg(target_os = "redox")]
|
||||
unsafe {
|
||||
crate::platform::init(auxv.clone());
|
||||
crate::platform::init_inner(auxv.clone());
|
||||
}
|
||||
|
||||
let name_or_path = if is_manual {
|
||||
|
||||
@@ -52,3 +52,6 @@ pub const AT_REDOX_PROC_FD: usize = 41;
|
||||
|
||||
#[cfg(target_os = "redox")]
|
||||
pub const AT_REDOX_THR_FD: usize = 42;
|
||||
|
||||
#[cfg(target_os = "redox")]
|
||||
pub const AT_REDOX_NS_FD: usize = 43;
|
||||
|
||||
+19
-3
@@ -328,16 +328,32 @@ pub fn get_auxv(auxvs: &[[usize; 2]], key: usize) -> Option<usize> {
|
||||
// SAFETY: Must only be called when only one thread exists.
|
||||
pub unsafe fn init(auxvs: Box<[[usize; 2]]>) {
|
||||
use self::auxv_defs::*;
|
||||
use crate::header::sys_stat::S_ISVTX;
|
||||
use redox_rt::proc::FdGuard;
|
||||
use syscall::MODE_PERM;
|
||||
|
||||
let Some(proc_fd) = get_auxv(&auxvs, AT_REDOX_PROC_FD) else {
|
||||
panic!("Missing proc and thread fd!");
|
||||
};
|
||||
let Some(ns_fd) = get_auxv(&auxvs, AT_REDOX_NS_FD) else {
|
||||
panic!("Missing namespace fd!");
|
||||
};
|
||||
unsafe {
|
||||
redox_rt::initialize(FdGuard::new(proc_fd).to_upper().unwrap());
|
||||
redox_rt::initialize(
|
||||
FdGuard::new(proc_fd).to_upper().unwrap(),
|
||||
if ns_fd == usize::MAX {
|
||||
None
|
||||
} else {
|
||||
Some(FdGuard::new(ns_fd).to_upper().unwrap())
|
||||
},
|
||||
);
|
||||
init_inner(auxvs)
|
||||
}
|
||||
}
|
||||
#[cold]
|
||||
#[cfg(target_os = "redox")]
|
||||
pub unsafe fn init_inner(auxvs: Box<[[usize; 2]]>) {
|
||||
use self::auxv_defs::*;
|
||||
use crate::header::sys_stat::S_ISVTX;
|
||||
use syscall::MODE_PERM;
|
||||
|
||||
// TODO: Is it safe to assume setup_sighandler has been called at this point?
|
||||
redox_rt::sys::this_proc_call(
|
||||
|
||||
@@ -204,7 +204,9 @@ pub fn execve(
|
||||
umask: redox_rt::sys::get_umask(),
|
||||
thr_fd: RtTcb::current().thread_fd().as_raw_fd(),
|
||||
proc_fd: redox_rt::current_proc_fd().as_raw_fd(),
|
||||
ns_fd: Some(redox_rt::current_namespace_fd()),
|
||||
};
|
||||
|
||||
fexec_impl(
|
||||
exec_fd_guard,
|
||||
arg0,
|
||||
|
||||
@@ -44,7 +44,7 @@ pub fn open(path: &str, flags: usize) -> Result<usize> {
|
||||
"stdout" => syscall::dup(1, &[]),
|
||||
"tty" => {
|
||||
if let Some(tty) = env_str!("TTY") {
|
||||
return syscall::open(tty, flags);
|
||||
return redox_rt::sys::open(tty, flags);
|
||||
}
|
||||
Err(Error::new(ENOENT))
|
||||
}
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
use core::{slice, str};
|
||||
use core::{
|
||||
cell::LazyCell,
|
||||
slice, str,
|
||||
sync::atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use ioslice::IoSlice;
|
||||
use redox_rt::{
|
||||
protocol::{ProcKillTarget, SocketCall, WaitFlags},
|
||||
sys::{WaitpidTarget, posix_read, posix_write},
|
||||
};
|
||||
use syscall::{EMFILE, Error, Result};
|
||||
use syscall::{EMFILE, ENOSYS, Error, Result};
|
||||
|
||||
use crate::{
|
||||
header::{
|
||||
@@ -140,12 +146,13 @@ pub unsafe extern "C" fn redox_openat_v1(
|
||||
path_base: *const u8,
|
||||
path_len: usize,
|
||||
flags: u32,
|
||||
fcntl_flags: u32,
|
||||
) -> RawResult {
|
||||
Error::mux(syscall::openat(
|
||||
fd,
|
||||
unsafe { str::from_utf8_unchecked(slice::from_raw_parts(path_base, path_len)) },
|
||||
flags as usize,
|
||||
0, //TODO: openat fcntl_flags
|
||||
fcntl_flags as usize,
|
||||
))
|
||||
}
|
||||
#[unsafe(no_mangle)]
|
||||
@@ -253,6 +260,10 @@ pub unsafe extern "C" fn redox_get_ens_v0() -> RawResult {
|
||||
Error::mux(redox_rt::sys::getens())
|
||||
}
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn redox_get_ns_v0() -> RawResult {
|
||||
Error::mux(redox_rt::sys::getns())
|
||||
}
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn redox_get_proc_credentials_v1(
|
||||
cap_fd: usize,
|
||||
target_pid: usize,
|
||||
@@ -263,7 +274,16 @@ pub unsafe extern "C" fn redox_get_proc_credentials_v1(
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn redox_setrens_v1(rns: usize, ens: usize) -> RawResult {
|
||||
Error::mux(redox_rt::sys::setrens(rns, ens).map(|()| 0))
|
||||
let _ = if ens == 0 {
|
||||
let null_namespace: [IoSlice; 2] = [IoSlice::new(b"memory"), IoSlice::new(b"pipe")];
|
||||
match redox_rt::sys::mkns(&null_namespace) {
|
||||
Ok(new_ns_fd) => redox_rt::sys::setns(new_ns_fd.take()),
|
||||
Err(e) => return Error::mux(Err(e)),
|
||||
}
|
||||
} else {
|
||||
redox_rt::sys::setns(ens)
|
||||
};
|
||||
0
|
||||
}
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn redox_waitpid_v1(pid: usize, status: *mut i32, options: u32) -> RawResult {
|
||||
@@ -387,8 +407,16 @@ pub unsafe extern "C" fn redox_mkns_v1(
|
||||
if flags != 0 {
|
||||
return Err(Error::new(EINVAL));
|
||||
}
|
||||
// Kernel does the UTF-8 validation.
|
||||
syscall::mkns(unsafe { core::slice::from_raw_parts(names.cast(), num_names) })
|
||||
let raw_iovecs = unsafe { slice::from_raw_parts(names, num_names) };
|
||||
let names_ioslice: Vec<IoSlice> = raw_iovecs
|
||||
.iter()
|
||||
.map(|iov| {
|
||||
IoSlice::new(unsafe {
|
||||
slice::from_raw_parts(iov.iov_base as *const u8, iov.iov_len)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
redox_rt::sys::mkns(&names_ioslice).map(|fd| fd.take())
|
||||
})())
|
||||
}
|
||||
|
||||
@@ -434,3 +462,30 @@ pub unsafe extern "C" fn redox_get_socket_token_v0(
|
||||
&metadata,
|
||||
))
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn redox_setns_v0(fd: usize) -> RawResult {
|
||||
match redox_rt::sys::setns(fd) {
|
||||
Some(guard) => guard.take(),
|
||||
None => usize::MAX,
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn redox_register_scheme_to_ns_v0(
|
||||
ns_fd: usize,
|
||||
name_base: *const u8,
|
||||
name_len: usize,
|
||||
cap_fd: usize,
|
||||
) -> RawResult {
|
||||
Error::mux(
|
||||
redox_rt::sys::register_scheme_to_ns(
|
||||
ns_fd,
|
||||
unsafe {
|
||||
str::from_utf8_unchecked(unsafe { slice::from_raw_parts(name_base, name_len) })
|
||||
},
|
||||
cap_fd,
|
||||
)
|
||||
.map(|()| 0),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ pub fn chdir(path: &str) -> Result<()> {
|
||||
let canon = canonicalize_using_cwd(cwd_guard.as_deref(), path).ok_or(Error::new(ENOENT))?;
|
||||
let canon_with_scheme = canonicalize_with_cwd_internal(cwd_guard.as_deref(), path)?;
|
||||
|
||||
let fd = syscall::open(&canon_with_scheme, O_STAT | O_CLOEXEC)?;
|
||||
let fd = redox_rt::sys::open(&canon_with_scheme, O_STAT | O_CLOEXEC)?;
|
||||
let mut stat = Stat::default();
|
||||
if syscall::fstat(fd, &mut stat).is_err() || (stat.st_mode & MODE_TYPE) != MODE_DIR {
|
||||
return Err(Error::new(ENOTDIR));
|
||||
@@ -116,14 +116,14 @@ pub fn open(path: &str, flags: usize) -> Result<usize> {
|
||||
let open_res = if canon.starts_with(libcscheme::LIBC_SCHEME) {
|
||||
libcscheme::open(&canon, flags)
|
||||
} else {
|
||||
syscall::open(&*canon, flags)
|
||||
redox_rt::sys::open(&*canon, flags)
|
||||
};
|
||||
|
||||
match open_res {
|
||||
Ok(fd) => return Ok(fd),
|
||||
Err(error) if error == Error::new(EXDEV) => {
|
||||
let resolve_flags = O_CLOEXEC | O_SYMLINK | O_RDONLY;
|
||||
let resolve_fd = FdGuard::new(syscall::open(&*canon, resolve_flags)?);
|
||||
let resolve_fd = FdGuard::new(redox_rt::sys::open(&*canon, resolve_flags)?);
|
||||
|
||||
let bytes_read = resolve_fd.read(&mut resolve_buf)?;
|
||||
// TODO: make resolve_buf PATH_MAX + 1 bytes?
|
||||
|
||||
@@ -53,7 +53,7 @@ impl PalSignal for Sys {
|
||||
_ => return Err(Errno(EINVAL)),
|
||||
};
|
||||
// TODO: implement setitimer
|
||||
// let fd = FdGuard::new(syscall::open(path, syscall::O_RDONLY | syscall::O_CLOEXEC)?);
|
||||
// let fd = FdGuard::new(redox_rt::sys::open(path, syscall::O_RDONLY | syscall::O_CLOEXEC)?);
|
||||
// let count = syscall::read(*fd, &mut spec)?;
|
||||
|
||||
let mut spec = syscall::ITimerSpec::default();
|
||||
|
||||
@@ -1069,12 +1069,14 @@ impl PalSocket for Sys {
|
||||
// The tcp: and udp: schemes allow using no path,
|
||||
// and later specifying one using `dup`.
|
||||
Ok(match (domain, kind) {
|
||||
(AF_INET, SOCK_STREAM) => syscall::open("/scheme/tcp", flags)? as c_int,
|
||||
(AF_INET, SOCK_DGRAM) => syscall::open("/scheme/udp", flags)? as c_int,
|
||||
(AF_INET, SOCK_STREAM) => redox_rt::sys::open("/scheme/tcp", flags)? as c_int,
|
||||
(AF_INET, SOCK_DGRAM) => redox_rt::sys::open("/scheme/udp", flags)? as c_int,
|
||||
(AF_UNIX, SOCK_STREAM) => {
|
||||
syscall::open("/scheme/uds_stream", flags | O_CREAT)? as c_int
|
||||
redox_rt::sys::open("/scheme/uds_stream", flags | O_CREAT)? as c_int
|
||||
}
|
||||
(AF_UNIX, SOCK_DGRAM) => {
|
||||
redox_rt::sys::open("/scheme/uds_dgram", flags | O_CREAT)? as c_int
|
||||
}
|
||||
(AF_UNIX, SOCK_DGRAM) => syscall::open("/scheme/uds_dgram", flags | O_CREAT)? as c_int,
|
||||
_ => return Err(Errno(EPROTONOSUPPORT)),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user