Merge branch 'cstr' into 'master'

Switch to a lightweight CStr wrapper

See merge request redox-os/relibc!431
This commit is contained in:
Jeremy Soller
2023-11-04 18:29:38 +00:00
24 changed files with 374 additions and 1590 deletions
+16 -16
View File
@@ -89,7 +89,7 @@ impl Sys {
}
impl Pal for Sys {
fn access(path: &CStr, mode: c_int) -> c_int {
fn access(path: CStr, mode: c_int) -> c_int {
e(unsafe { syscall!(ACCESS, path.as_ptr(), mode) }) as c_int
}
@@ -97,15 +97,15 @@ impl Pal for Sys {
unsafe { syscall!(BRK, addr) as *mut c_void }
}
fn chdir(path: &CStr) -> c_int {
fn chdir(path: CStr) -> c_int {
e(unsafe { syscall!(CHDIR, path.as_ptr()) }) as c_int
}
fn chmod(path: &CStr, mode: mode_t) -> c_int {
fn chmod(path: CStr, mode: mode_t) -> c_int {
e(unsafe { syscall!(FCHMODAT, AT_FDCWD, path.as_ptr(), mode, 0) }) as c_int
}
fn chown(path: &CStr, owner: uid_t, group: gid_t) -> c_int {
fn chown(path: CStr, owner: uid_t, group: gid_t) -> c_int {
e(unsafe {
syscall!(
FCHOWNAT,
@@ -141,7 +141,7 @@ impl Pal for Sys {
e(unsafe { syscall!(DUP3, fildes, fildes2, 0) }) as c_int
}
unsafe fn execve(path: &CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
unsafe fn execve(path: CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
e(syscall!(EXECVE, path.as_ptr(), argv, envp)) as c_int
}
unsafe fn fexecve(fildes: c_int, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
@@ -252,7 +252,7 @@ impl Pal for Sys {
e(unsafe { syscall!(UTIMENSAT, fd, ptr::null::<c_char>(), times, 0) }) as c_int
}
fn utimens(path: &CStr, times: *const timespec) -> c_int {
fn utimens(path: CStr, times: *const timespec) -> c_int {
e(unsafe { syscall!(UTIMENSAT, AT_FDCWD, path.as_ptr(), times, 0) }) as c_int
}
@@ -332,11 +332,11 @@ impl Pal for Sys {
e(unsafe { syscall!(GETUID) }) as uid_t
}
fn lchown(path: &CStr, owner: uid_t, group: gid_t) -> c_int {
fn lchown(path: CStr, owner: uid_t, group: gid_t) -> c_int {
e(unsafe { syscall!(LCHOWN, path.as_ptr(), owner, group) }) as c_int
}
fn link(path1: &CStr, path2: &CStr) -> c_int {
fn link(path1: CStr, path2: CStr) -> c_int {
e(unsafe {
syscall!(
LINKAT,
@@ -353,11 +353,11 @@ impl Pal for Sys {
e(unsafe { syscall!(LSEEK, fildes, offset, whence) }) as off_t
}
fn mkdir(path: &CStr, mode: mode_t) -> c_int {
fn mkdir(path: CStr, mode: mode_t) -> c_int {
e(unsafe { syscall!(MKDIRAT, AT_FDCWD, path.as_ptr(), mode) }) as c_int
}
fn mkfifo(path: &CStr, mode: mode_t) -> c_int {
fn mkfifo(path: CStr, mode: mode_t) -> c_int {
e(unsafe { syscall!(MKNODAT, AT_FDCWD, path.as_ptr(), mode | S_IFIFO, 0) }) as c_int
}
@@ -408,7 +408,7 @@ impl Pal for Sys {
e(unsafe { syscall!(NANOSLEEP, rqtp, rmtp) }) as c_int
}
fn open(path: &CStr, oflag: c_int, mode: mode_t) -> c_int {
fn open(path: CStr, oflag: c_int, mode: mode_t) -> c_int {
e(unsafe { syscall!(OPENAT, AT_FDCWD, path.as_ptr(), oflag, mode) }) as c_int
}
@@ -491,7 +491,7 @@ impl Pal for Sys {
e(unsafe { syscall!(READ, fildes, buf.as_mut_ptr(), buf.len()) }) as ssize_t
}
fn readlink(pathname: &CStr, out: &mut [u8]) -> ssize_t {
fn readlink(pathname: CStr, out: &mut [u8]) -> ssize_t {
e(unsafe {
syscall!(
READLINKAT,
@@ -503,11 +503,11 @@ impl Pal for Sys {
}) as ssize_t
}
fn rename(old: &CStr, new: &CStr) -> c_int {
fn rename(old: CStr, new: CStr) -> c_int {
e(unsafe { syscall!(RENAMEAT, AT_FDCWD, old.as_ptr(), AT_FDCWD, new.as_ptr()) }) as c_int
}
fn rmdir(path: &CStr) -> c_int {
fn rmdir(path: CStr) -> c_int {
e(unsafe { syscall!(UNLINKAT, AT_FDCWD, path.as_ptr(), AT_REMOVEDIR) }) as c_int
}
@@ -539,7 +539,7 @@ impl Pal for Sys {
e(unsafe { syscall!(SETSID) }) as c_int
}
fn symlink(path1: &CStr, path2: &CStr) -> c_int {
fn symlink(path1: CStr, path2: CStr) -> c_int {
e(unsafe { syscall!(SYMLINKAT, path1.as_ptr(), AT_FDCWD, path2.as_ptr()) }) as c_int
}
@@ -555,7 +555,7 @@ impl Pal for Sys {
e(unsafe { syscall!(UNAME, utsname, 0) }) as c_int
}
fn unlink(path: &CStr) -> c_int {
fn unlink(path: CStr) -> c_int {
e(unsafe { syscall!(UNLINKAT, AT_FDCWD, path.as_ptr(), 0) }) as c_int
}
+16 -16
View File
@@ -25,15 +25,15 @@ pub use self::socket::PalSocket;
mod socket;
pub trait Pal {
fn access(path: &CStr, mode: c_int) -> c_int;
fn access(path: CStr, mode: c_int) -> c_int;
fn brk(addr: *mut c_void) -> *mut c_void;
fn chdir(path: &CStr) -> c_int;
fn chdir(path: CStr) -> c_int;
fn chmod(path: &CStr, mode: mode_t) -> c_int;
fn chmod(path: CStr, mode: mode_t) -> c_int;
fn chown(path: &CStr, owner: uid_t, group: gid_t) -> c_int;
fn chown(path: CStr, owner: uid_t, group: gid_t) -> c_int;
fn clock_getres(clk_id: clockid_t, tp: *mut timespec) -> c_int;
@@ -47,7 +47,7 @@ pub trait Pal {
fn dup2(fildes: c_int, fildes2: c_int) -> c_int;
unsafe fn execve(path: &CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int;
unsafe fn execve(path: CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int;
unsafe fn fexecve(fildes: c_int, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int;
fn exit(status: c_int) -> !;
@@ -87,7 +87,7 @@ pub trait Pal {
fn futimens(fd: c_int, times: *const timespec) -> c_int;
fn utimens(path: &CStr, times: *const timespec) -> c_int;
fn utimens(path: CStr, times: *const timespec) -> c_int;
fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char;
@@ -128,15 +128,15 @@ pub trait Pal {
fn getuid() -> uid_t;
fn lchown(path: &CStr, owner: uid_t, group: gid_t) -> c_int;
fn lchown(path: CStr, owner: uid_t, group: gid_t) -> c_int;
fn link(path1: &CStr, path2: &CStr) -> c_int;
fn link(path1: CStr, path2: CStr) -> c_int;
fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t;
fn mkdir(path: &CStr, mode: mode_t) -> c_int;
fn mkdir(path: CStr, mode: mode_t) -> c_int;
fn mkfifo(path: &CStr, mode: mode_t) -> c_int;
fn mkfifo(path: CStr, mode: mode_t) -> c_int;
unsafe fn mlock(addr: *const c_void, len: usize) -> c_int;
@@ -165,7 +165,7 @@ pub trait Pal {
fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int;
fn open(path: &CStr, oflag: c_int, mode: mode_t) -> c_int;
fn open(path: CStr, oflag: c_int, mode: mode_t) -> c_int;
fn pipe2(fildes: &mut [c_int], flags: c_int) -> c_int;
@@ -179,11 +179,11 @@ pub trait Pal {
fn read(fildes: c_int, buf: &mut [u8]) -> ssize_t;
fn readlink(pathname: &CStr, out: &mut [u8]) -> ssize_t;
fn readlink(pathname: CStr, out: &mut [u8]) -> ssize_t;
fn rename(old: &CStr, new: &CStr) -> c_int;
fn rename(old: CStr, new: CStr) -> c_int;
fn rmdir(path: &CStr) -> c_int;
fn rmdir(path: CStr) -> c_int;
fn sched_yield() -> c_int;
@@ -199,7 +199,7 @@ pub trait Pal {
fn setsid() -> c_int;
fn symlink(path1: &CStr, path2: &CStr) -> c_int;
fn symlink(path1: CStr, path2: CStr) -> c_int;
fn sync() -> c_int;
@@ -207,7 +207,7 @@ pub trait Pal {
fn uname(utsname: *mut utsname) -> c_int;
fn unlink(path: &CStr) -> c_int;
fn unlink(path: CStr) -> c_int;
fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t;
+3 -2
View File
@@ -81,7 +81,7 @@ pub enum ArgEnv<'a> {
}
pub enum Executable<'a> {
AtPath(&'a CStr),
AtPath(CStr<'a>),
InFd { file: File, arg0: &'a [u8] },
}
@@ -187,7 +187,8 @@ pub fn execve(
interpreter.pop().unwrap();
}
let cstring = CString::new(interpreter).map_err(|_| Error::new(ENOEXEC))?;
image_file = File::open(&cstring, O_RDONLY as c_int).map_err(|_| Error::new(ENOENT))?;
image_file = File::open(CStr::borrow(&cstring), O_RDONLY as c_int)
.map_err(|_| Error::new(ENOENT))?;
// Make sure path is kept alive long enough, and push it to the arguments
_interpreter_path = Some(cstring);
+18 -21
View File
@@ -80,7 +80,7 @@ pub fn e(sys: Result<usize>) -> usize {
pub struct Sys;
impl Pal for Sys {
fn access(path: &CStr, mode: c_int) -> c_int {
fn access(path: CStr, mode: c_int) -> c_int {
let fd = match File::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC) {
Ok(fd) => fd,
Err(_) => return -1,
@@ -165,19 +165,19 @@ impl Pal for Sys {
}
}
fn chdir(path: &CStr) -> c_int {
fn chdir(path: CStr) -> c_int {
let path = path_from_c_str!(path);
e(path::chdir(path).map(|()| 0)) as c_int
}
fn chmod(path: &CStr, mode: mode_t) -> c_int {
fn chmod(path: CStr, mode: mode_t) -> c_int {
match File::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC) {
Ok(file) => Self::fchmod(*file, mode),
Err(_) => -1,
}
}
fn chown(path: &CStr, owner: uid_t, group: gid_t) -> c_int {
fn chown(path: CStr, owner: uid_t, group: gid_t) -> c_int {
match File::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC) {
Ok(file) => Self::fchown(*file, owner, group),
Err(_) => -1,
@@ -232,7 +232,7 @@ impl Pal for Sys {
loop {}
}
unsafe fn execve(path: &CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
unsafe fn execve(path: CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
e(self::exec::execve(
Executable::AtPath(path),
self::exec::ArgEnv::C { argv, envp },
@@ -257,7 +257,7 @@ impl Pal for Sys {
!0
} else {
match str::from_utf8(&buf[..res]) {
Ok(path) => Sys::chdir(&CString::new(path).unwrap()),
Ok(path) => e(path::chdir(path).map(|()| 0)) as c_int,
Err(_) => {
unsafe { errno = EINVAL };
return -1;
@@ -387,7 +387,7 @@ impl Pal for Sys {
e(syscall::futimens(fd as usize, &times)) as c_int
}
fn utimens(path: &CStr, times: *const timespec) -> c_int {
fn utimens(path: CStr, times: *const timespec) -> c_int {
match File::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC) {
Ok(file) => Self::futimens(*file, times),
Err(_) => -1,
@@ -637,7 +637,7 @@ impl Pal for Sys {
e(syscall::getuid()) as pid_t
}
fn lchown(path: &CStr, owner: uid_t, group: gid_t) -> c_int {
fn lchown(path: CStr, owner: uid_t, group: gid_t) -> c_int {
// TODO: Is it correct for regular chown to use O_PATH? On Linux the meaning of that flag
// is to forbid file operations, including fchown.
@@ -648,7 +648,7 @@ impl Pal for Sys {
}
}
fn link(path1: &CStr, path2: &CStr) -> c_int {
fn link(path1: CStr, path2: CStr) -> c_int {
e(unsafe { syscall::link(path1.as_ptr() as *const u8, path2.as_ptr() as *const u8) })
as c_int
}
@@ -661,7 +661,7 @@ impl Pal for Sys {
)) as off_t
}
fn mkdir(path: &CStr, mode: mode_t) -> c_int {
fn mkdir(path: CStr, mode: mode_t) -> c_int {
match File::create(
path,
fcntl::O_DIRECTORY | fcntl::O_EXCL | fcntl::O_CLOEXEC,
@@ -672,7 +672,7 @@ impl Pal for Sys {
}
}
fn mkfifo(path: &CStr, mode: mode_t) -> c_int {
fn mkfifo(path: CStr, mode: mode_t) -> c_int {
match File::create(
path,
fcntl::O_CREAT | fcntl::O_CLOEXEC,
@@ -788,7 +788,7 @@ impl Pal for Sys {
}
}
fn open(path: &CStr, oflag: c_int, mode: mode_t) -> c_int {
fn open(path: CStr, oflag: c_int, mode: mode_t) -> c_int {
let path = path_from_c_str!(path);
e(libredox::open(path, oflag, mode)) as c_int
@@ -828,7 +828,7 @@ impl Pal for Sys {
e(syscall::fpath(fildes as usize, out)) as ssize_t
}
fn readlink(pathname: &CStr, out: &mut [u8]) -> ssize_t {
fn readlink(pathname: CStr, out: &mut [u8]) -> ssize_t {
match File::open(
pathname,
fcntl::O_RDONLY | fcntl::O_SYMLINK | fcntl::O_CLOEXEC,
@@ -838,7 +838,7 @@ impl Pal for Sys {
}
}
fn rename(oldpath: &CStr, newpath: &CStr) -> c_int {
fn rename(oldpath: CStr, newpath: CStr) -> c_int {
let newpath = path_from_c_str!(newpath);
match File::open(oldpath, fcntl::O_PATH | fcntl::O_CLOEXEC) {
Ok(file) => e(syscall::frename(*file as usize, newpath)) as c_int,
@@ -846,7 +846,7 @@ impl Pal for Sys {
}
}
fn rmdir(path: &CStr) -> c_int {
fn rmdir(path: CStr) -> c_int {
let path = path_from_c_str!(path);
e(canonicalize(path).and_then(|path| syscall::rmdir(&path))) as c_int
}
@@ -891,7 +891,7 @@ impl Pal for Sys {
e(syscall::setreuid(ruid as usize, euid as usize)) as c_int
}
fn symlink(path1: &CStr, path2: &CStr) -> c_int {
fn symlink(path1: CStr, path2: CStr) -> c_int {
let mut file = match File::create(
path2,
fcntl::O_WRONLY | fcntl::O_SYMLINK | fcntl::O_CLOEXEC,
@@ -922,10 +922,7 @@ impl Pal for Sys {
return Ok(());
}
let mut file = File::open(
&CString::new("/etc/hostname").unwrap(),
fcntl::O_RDONLY | fcntl::O_CLOEXEC,
)?;
let mut file = File::open(c_str!("/etc/hostname"), fcntl::O_RDONLY | fcntl::O_CLOEXEC)?;
let mut read = 0;
let name_len = name.len();
@@ -1001,7 +998,7 @@ impl Pal for Sys {
}
}
fn unlink(path: &CStr) -> c_int {
fn unlink(path: CStr) -> c_int {
let path = path_from_c_str!(path);
e(canonicalize(path).and_then(|path| syscall::unlink(&path))) as c_int
}
+7 -7
View File
@@ -10,7 +10,7 @@ use crate::header::arch_aarch64_user::user_regs_struct;
#[cfg(target_arch = "x86_64")]
use crate::header::arch_x64_user::user_regs_struct;
use crate::{
c_str::CString,
c_str::{CStr, CString},
fs::File,
header::{errno as errnoh, fcntl, signal, sys_ptrace},
io::{self, prelude::*},
@@ -57,7 +57,7 @@ pub fn is_traceme(pid: pid_t) -> bool {
return false;
}
File::open(
&CString::new(format!("chan:ptrace-relibc/{}/traceme", pid)).unwrap(),
CStr::borrow(&CString::new(format!("chan:ptrace-relibc/{}/traceme", pid)).unwrap()),
fcntl::O_PATH,
)
.is_ok()
@@ -74,19 +74,19 @@ pub fn get_session(
Ok(entry.insert(Session {
first: true,
tracer: File::open(
&CString::new(format!("proc:{}/trace", pid)).unwrap(),
CStr::borrow(&CString::new(format!("proc:{}/trace", pid)).unwrap()),
NEW_FLAGS,
)?,
mem: File::open(
&CString::new(format!("proc:{}/mem", pid)).unwrap(),
CStr::borrow(&CString::new(format!("proc:{}/mem", pid)).unwrap()),
NEW_FLAGS,
)?,
regs: File::open(
&CString::new(format!("proc:{}/regs/int", pid)).unwrap(),
CStr::borrow(&CString::new(format!("proc:{}/regs/int", pid)).unwrap()),
NEW_FLAGS,
)?,
fpregs: File::open(
&CString::new(format!("proc:{}/regs/float", pid)).unwrap(),
CStr::borrow(&CString::new(format!("proc:{}/regs/float", pid)).unwrap()),
NEW_FLAGS,
)?,
}))
@@ -136,7 +136,7 @@ fn inner_ptrace(
// Mark this child as traced, parent will check for this marker file
let pid = Sys::getpid();
mem::forget(File::open(
&CString::new(format!("chan:ptrace-relibc/{}/traceme", pid)).unwrap(),
CStr::borrow(&CString::new(format!("chan:ptrace-relibc/{}/traceme", pid)).unwrap()),
fcntl::O_CREAT | fcntl::O_PATH | fcntl::O_EXCL,
)?);
return Ok(0);
+1 -2
View File
@@ -9,6 +9,5 @@ license = "MIT"
[dependencies]
redox_syscall = "0.4"
# TODO: Update
goblin = { version = "0.0.21", default-features = false, features = ["elf32", "elf64", "endian_fd"] }
goblin = { version = "0.7", default-features = false, features = ["elf32", "elf64", "endian_fd"] }
plain = "0.2"