Resolve clippy errors and warnings

This commit is contained in:
topjohnwu 2023-05-30 22:23:11 -07:00
parent 665c6bdc4b
commit 9ea9f01933
7 changed files with 246 additions and 282 deletions

View File

@ -21,7 +21,7 @@ pub mod unsafe_impl {
if r >= 0 { if r >= 0 {
*buf.offset(r) = b'\0'; *buf.offset(r) = b'\0';
} }
return r; r
} }
#[no_mangle] #[no_mangle]
@ -42,11 +42,13 @@ pub fn __open_fd_impl(path: &CStr, flags: i32, mode: mode_t) -> Option<OwnedFd>
} }
pub fn __xopen_fd_impl(path: &CStr, flags: i32, mode: mode_t) -> Option<OwnedFd> { pub fn __xopen_fd_impl(path: &CStr, flags: i32, mode: mode_t) -> Option<OwnedFd> {
let fd = xopen(path.as_ptr(), flags, mode); unsafe {
if fd >= 0 { let fd = xopen(path.as_ptr(), flags, mode);
unsafe { Some(OwnedFd::from_raw_fd(fd)) } if fd >= 0 {
} else { Some(OwnedFd::from_raw_fd(fd))
None } else {
None
}
} }
} }
@ -87,7 +89,7 @@ pub fn realpath(path: &CStr, buf: &mut [u8]) -> isize {
let mut st2: libc::stat; let mut st2: libc::stat;
let mut skip_check = false; let mut skip_check = false;
unsafe { unsafe {
st1 = std::mem::zeroed(); st1 = mem::zeroed();
if libc::fstat(fd.as_raw_fd(), &mut st1) < 0 { if libc::fstat(fd.as_raw_fd(), &mut st1) < 0 {
// This shall only fail on Linux < 3.6 // This shall only fail on Linux < 3.6
skip_check = true; skip_check = true;
@ -95,7 +97,7 @@ pub fn realpath(path: &CStr, buf: &mut [u8]) -> isize {
} }
let len = fd_path(fd.as_raw_fd(), buf); let len = fd_path(fd.as_raw_fd(), buf);
unsafe { unsafe {
st2 = std::mem::zeroed(); st2 = mem::zeroed();
if libc::stat(buf.as_ptr().cast(), &mut st2) < 0 if libc::stat(buf.as_ptr().cast(), &mut st2) < 0
|| (!skip_check && (st2.st_dev != st1.st_dev || st2.st_ino != st1.st_ino)) || (!skip_check && (st2.st_dev != st1.st_dev || st2.st_ino != st1.st_ino))
{ {
@ -103,7 +105,7 @@ pub fn realpath(path: &CStr, buf: &mut [u8]) -> isize {
return -1; return -1;
} }
} }
return len; len
} else { } else {
*errno() = ENOENT; *errno() = ENOENT;
-1 -1
@ -115,25 +117,23 @@ extern "C" {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn mkdirs(path: *const c_char, mode: mode_t) -> i32 { pub unsafe extern "C" fn mkdirs(path: *const c_char, mode: mode_t) -> i32 {
unsafe { let mut buf = [0_u8; 4096];
let mut buf = [0 as u8; 4096]; let ptr: *mut c_char = buf.as_mut_ptr().cast();
let ptr: *mut c_char = buf.as_mut_ptr().cast(); let len = strscpy(ptr, path, buf.len());
let len = strscpy(ptr, path, buf.len()); let mut curr = &mut buf[1..len];
let mut curr = &mut buf[1..len]; while let Some(p) = curr.iter().position(|c| *c == b'/') {
while let Some(p) = curr.iter().position(|c| *c == b'/') { curr[p] = b'\0';
curr[p] = b'\0';
if libc::mkdir(ptr, mode) < 0 && *errno() != EEXIST {
return -1;
}
curr[p] = b'/';
curr = &mut curr[(p + 1)..];
}
if libc::mkdir(ptr, mode) < 0 && *errno() != EEXIST { if libc::mkdir(ptr, mode) < 0 && *errno() != EEXIST {
return -1; return -1;
} }
0 curr[p] = b'/';
curr = &mut curr[(p + 1)..];
} }
if libc::mkdir(ptr, mode) < 0 && *errno() != EEXIST {
return -1;
}
0
} }
pub trait ReadExt { pub trait ReadExt {
@ -201,7 +201,7 @@ impl<T: BufRead> BufReadExt for T {
if let Some((key, value)) = line.split_once('=') { if let Some((key, value)) = line.split_once('=') {
return f(key, value); return f(key, value);
} }
return true; true
}); });
} }
} }
@ -212,10 +212,10 @@ pub trait WriteExt {
impl<T: Write> WriteExt for T { impl<T: Write> WriteExt for T {
fn write_zeros(&mut self, mut len: usize) -> io::Result<()> { fn write_zeros(&mut self, mut len: usize) -> io::Result<()> {
let mut buf = [0 as u8; 4096]; let buf = [0_u8; 4096];
while len > 0 { while len > 0 {
let l = min(buf.len(), len); let l = min(buf.len(), len);
self.write_all(&mut buf[..l])?; self.write_all(&buf[..l])?;
len -= l; len -= l;
} }
Ok(()) Ok(())

View File

@ -1,3 +1,4 @@
#![allow(clippy::missing_safety_doc)]
#![feature(format_args_nl)] #![feature(format_args_nl)]
#![feature(io_error_more)] #![feature(io_error_more)]

View File

@ -39,7 +39,7 @@ pub fn exit_on_error(b: bool) {
} }
impl LogLevel { impl LogLevel {
fn to_disable_flag(&self) -> u32 { fn as_disable_flag(&self) -> u32 {
match *self { match *self {
LogLevel::Error => LogFlag::DisableError, LogLevel::Error => LogFlag::DisableError,
LogLevel::Warn => LogFlag::DisableWarn, LogLevel::Warn => LogFlag::DisableWarn,
@ -51,7 +51,7 @@ impl LogLevel {
} }
pub fn set_log_level_state(level: LogLevel, enabled: bool) { pub fn set_log_level_state(level: LogLevel, enabled: bool) {
let flag = level.to_disable_flag(); let flag = level.as_disable_flag();
unsafe { unsafe {
if enabled { if enabled {
LOGGER.flags &= !flag LOGGER.flags &= !flag
@ -63,7 +63,7 @@ pub fn set_log_level_state(level: LogLevel, enabled: bool) {
pub fn log_with_rs(level: LogLevel, msg: &[u8]) { pub fn log_with_rs(level: LogLevel, msg: &[u8]) {
let logger = unsafe { LOGGER }; let logger = unsafe { LOGGER };
if (logger.flags & level.to_disable_flag()) != 0 { if (logger.flags & level.as_disable_flag()) != 0 {
return; return;
} }
(logger.write)(level, msg); (logger.write)(level, msg);
@ -74,7 +74,7 @@ pub fn log_with_rs(level: LogLevel, msg: &[u8]) {
pub fn log_impl(level: LogLevel, args: Arguments) { pub fn log_impl(level: LogLevel, args: Arguments) {
let logger = unsafe { LOGGER }; let logger = unsafe { LOGGER };
if (logger.flags & level.to_disable_flag()) != 0 { if (logger.flags & level.as_disable_flag()) != 0 {
return; return;
} }
let mut buf: [u8; 4096] = [0; 4096]; let mut buf: [u8; 4096] = [0; 4096];
@ -108,16 +108,16 @@ macro_rules! perror {
($fmt:expr) => { ($fmt:expr) => {
$crate::log_impl($crate::ffi::LogLevel::Error, format_args_nl!( $crate::log_impl($crate::ffi::LogLevel::Error, format_args_nl!(
concat!($fmt, " failed with {}: {}"), concat!($fmt, " failed with {}: {}"),
crate::errno(), $crate::errno(),
crate::error_str() $crate::error_str()
)) ))
}; };
($fmt:expr, $($args:tt)*) => { ($fmt:expr, $($args:tt)*) => {
$crate::log_impl($crate::ffi::LogLevel::Error, format_args_nl!( $crate::log_impl($crate::ffi::LogLevel::Error, format_args_nl!(
concat!($fmt, " failed with {}: {}"), concat!($fmt, " failed with {}: {}"),
$($args)*, $($args)*,
crate::errno(), $crate::errno(),
crate::error_str() $crate::error_str()
)) ))
}; };
} }

View File

@ -95,7 +95,7 @@ pub fn ptr_to_str_result<'a, T>(ptr: *const T) -> Result<&'a str, StrErr> {
} else { } else {
unsafe { CStr::from_ptr(ptr.cast()) } unsafe { CStr::from_ptr(ptr.cast()) }
.to_str() .to_str()
.map_err(|e| StrErr::from(e)) .map_err(StrErr::from)
} }
} }

View File

@ -7,7 +7,7 @@ use libc::{
ssize_t, SYS_dup3, ssize_t, SYS_dup3,
}; };
use crate::{cstr, errno, error, mkdirs, perror, ptr_to_str, realpath}; use crate::{cstr, errno, error, mkdirs, perror, ptr_to_str, raw_cstr, realpath};
mod unsafe_impl { mod unsafe_impl {
use std::ffi::CStr; use std::ffi::CStr;
@ -45,7 +45,7 @@ mod unsafe_impl {
if r < 0 { if r < 0 {
perror!("readlink"); perror!("readlink");
} }
return r; r
} }
#[no_mangle] #[no_mangle]
@ -73,7 +73,7 @@ mod unsafe_impl {
} }
} }
} }
return r; r
} }
#[no_mangle] #[no_mangle]
@ -82,7 +82,7 @@ mod unsafe_impl {
if r < 0 { if r < 0 {
perror!("poll"); perror!("poll");
} }
return r; r
} }
#[no_mangle] #[no_mangle]
@ -96,52 +96,49 @@ mod unsafe_impl {
if r < 0 { if r < 0 {
perror!("sendfile"); perror!("sendfile");
} }
return r; r
} }
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xfopen(path: *const c_char, mode: *const c_char) -> *mut libc::FILE { pub unsafe extern "C" fn xfopen(path: *const c_char, mode: *const c_char) -> *mut libc::FILE {
unsafe { let fp = libc::fopen(path, mode);
let fp = libc::fopen(path, mode); if fp.is_null() {
if fp.is_null() { perror!("fopen {}", ptr_to_str(path));
perror!("fopen {}", ptr_to_str(path));
}
return fp;
} }
fp
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xfdopen(fd: RawFd, mode: *const c_char) -> *mut libc::FILE { pub unsafe extern "C" fn xfdopen(fd: RawFd, mode: *const c_char) -> *mut libc::FILE {
unsafe { let fp = libc::fdopen(fd, mode);
let fp = libc::fdopen(fd, mode); if fp.is_null() {
if fp.is_null() { perror!("fdopen");
perror!("fdopen");
}
return fp;
} }
fp
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) -> RawFd { pub unsafe extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) -> RawFd {
unsafe { let r = libc::open(path, flags, mode as c_uint);
let r = libc::open(path, flags, mode as c_uint); if r < 0 {
if r < 0 { perror!("open {}", ptr_to_str(path));
perror!("open {}", ptr_to_str(path));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xopenat(dirfd: RawFd, path: *const c_char, flags: i32, mode: mode_t) -> RawFd { pub unsafe extern "C" fn xopenat(
unsafe { dirfd: RawFd,
let r = libc::openat(dirfd, path, flags, mode as c_uint); path: *const c_char,
if r < 0 { flags: i32,
perror!("openat {}", ptr_to_str(path)); mode: mode_t,
} ) -> RawFd {
return r; let r = libc::openat(dirfd, path, flags, mode as c_uint);
if r < 0 {
perror!("openat {}", ptr_to_str(path));
} }
r
} }
// Fully write data slice // Fully write data slice
@ -157,19 +154,19 @@ pub fn xwrite(fd: RawFd, data: &[u8]) -> isize {
continue; continue;
} }
perror!("write"); perror!("write");
return r as isize; return r;
} }
let r = r as usize; let r = r as usize;
write_sz += r; write_sz += r;
remain = &remain[r..]; remain = &remain[r..];
if r == 0 || remain.len() == 0 { if r == 0 || remain.is_empty() {
break; break;
} }
} }
if remain.len() != 0 { if !remain.is_empty() {
error!("write ({} != {})", write_sz, data.len()) error!("write ({} != {})", write_sz, data.len())
} }
return write_sz as isize; write_sz as isize
} }
} }
@ -179,7 +176,7 @@ pub fn xread(fd: RawFd, data: &mut [u8]) -> isize {
if r < 0 { if r < 0 {
perror!("read"); perror!("read");
} }
return r; r
} }
} }
@ -196,19 +193,19 @@ pub fn xxread(fd: RawFd, data: &mut [u8]) -> isize {
continue; continue;
} }
perror!("read"); perror!("read");
return r as isize; return r;
} }
let r = r as usize; let r = r as usize;
read_sz += r; read_sz += r;
remain = &mut remain[r..]; remain = &mut remain[r..];
if r == 0 || remain.len() == 0 { if r == 0 || remain.is_empty() {
break; break;
} }
} }
if remain.len() != 0 { if !remain.is_empty() {
error!("read ({} != {})", read_sz, data.len()) error!("read ({} != {})", read_sz, data.len())
} }
return read_sz as isize; read_sz as isize
} }
} }
@ -219,7 +216,7 @@ pub extern "C" fn xlseek64(fd: RawFd, offset: i64, whence: i32) -> i64 {
if r < 0 { if r < 0 {
perror!("lseek64"); perror!("lseek64");
} }
return r; r
} }
} }
@ -229,7 +226,7 @@ pub fn xpipe2(fds: &mut [i32; 2], flags: i32) -> i32 {
if r < 0 { if r < 0 {
perror!("pipe2"); perror!("pipe2");
} }
return r; r
} }
} }
@ -240,7 +237,7 @@ pub extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 {
if r < 0 { if r < 0 {
perror!("setns"); perror!("setns");
} }
return r; r
} }
} }
@ -251,19 +248,17 @@ pub extern "C" fn xunshare(flags: i32) -> i32 {
if r < 0 { if r < 0 {
perror!("unshare"); perror!("unshare");
} }
return r; r
} }
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR { pub unsafe extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR {
unsafe { let dp = libc::opendir(path);
let dp = libc::opendir(path); if dp.is_null() {
if dp.is_null() { perror!("opendir {}", ptr_to_str(path));
perror!("opendir {}", ptr_to_str(path));
}
return dp;
} }
dp
} }
#[no_mangle] #[no_mangle]
@ -273,30 +268,27 @@ pub extern "C" fn xfdopendir(fd: RawFd) -> *mut libc::DIR {
if dp.is_null() { if dp.is_null() {
perror!("fdopendir"); perror!("fdopendir");
} }
return dp; dp
} }
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xreaddir(dirp: *mut libc::DIR) -> *mut libc::dirent { pub unsafe extern "C" fn xreaddir(dirp: *mut libc::DIR) -> *mut libc::dirent {
#[allow(unused_unsafe)] *errno() = 0;
unsafe { loop {
*errno() = 0; let e = libc::readdir(dirp);
loop { if e.is_null() {
let e = libc::readdir(dirp); if *errno() != 0 {
if e.is_null() { perror!("readdir")
if *errno() != 0 { }
perror!("readdir") } else {
} // Filter out . and ..
} else { let s = (*e).d_name.as_ptr();
// Filter out . and .. if libc::strcmp(s, raw_cstr!(".")) == 0 || libc::strcmp(s, raw_cstr!("..")) == 0 {
let s = CStr::from_ptr((*e).d_name.as_ptr()); continue;
if s == cstr!(".") || s == cstr!("..") { }
continue; };
} return e;
};
return e;
}
} }
} }
@ -307,7 +299,7 @@ pub extern "C" fn xsetsid() -> i32 {
if r < 0 { if r < 0 {
perror!("setsid"); perror!("setsid");
} }
return r; r
} }
} }
@ -318,19 +310,17 @@ pub extern "C" fn xsocket(domain: i32, ty: i32, protocol: i32) -> RawFd {
if fd < 0 { if fd < 0 {
perror!("socket"); perror!("socket");
} }
return fd; fd
} }
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xbind(socket: i32, address: *const sockaddr, len: socklen_t) -> i32 { pub unsafe extern "C" fn xbind(socket: i32, address: *const sockaddr, len: socklen_t) -> i32 {
unsafe { let r = libc::bind(socket, address, len);
let r = libc::bind(socket, address, len); if r < 0 {
if r < 0 { perror!("bind");
perror!("bind");
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
@ -340,122 +330,109 @@ pub extern "C" fn xlisten(socket: i32, backlog: i32) -> i32 {
if r < 0 { if r < 0 {
perror!("listen"); perror!("listen");
} }
return r; r
} }
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xaccept4( pub unsafe extern "C" fn xaccept4(
sockfd: RawFd, sockfd: RawFd,
addr: *mut sockaddr, addr: *mut sockaddr,
len: *mut socklen_t, len: *mut socklen_t,
flg: i32, flg: i32,
) -> RawFd { ) -> RawFd {
unsafe { let fd = libc::accept4(sockfd, addr, len, flg);
let fd = libc::accept4(sockfd, addr, len, flg); if fd < 0 {
if fd < 0 { perror!("accept4");
perror!("accept4");
}
return fd;
} }
fd
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xsendmsg(fd: RawFd, msg: *const libc::msghdr, flags: i32) -> ssize_t { pub unsafe extern "C" fn xsendmsg(fd: RawFd, msg: *const libc::msghdr, flags: i32) -> ssize_t {
unsafe { let r = libc::sendmsg(fd, msg, flags);
let r = libc::sendmsg(fd, msg, flags); if r < 0 {
if r < 0 { perror!("sendmsg");
perror!("sendmsg");
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xrecvmsg(fd: RawFd, msg: *mut libc::msghdr, flags: i32) -> ssize_t { pub unsafe extern "C" fn xrecvmsg(fd: RawFd, msg: *mut libc::msghdr, flags: i32) -> ssize_t {
unsafe { let r = libc::recvmsg(fd, msg, flags);
let r = libc::recvmsg(fd, msg, flags); if r < 0 {
if r < 0 { perror!("recvmsg");
perror!("recvmsg");
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xaccess(path: *const c_char, mode: i32) -> i32 { pub unsafe extern "C" fn xaccess(path: *const c_char, mode: i32) -> i32 {
unsafe { let r = libc::access(path, mode);
let r = libc::access(path, mode); if r < 0 {
if r < 0 { perror!("access {}", ptr_to_str(path));
perror!("access {}", ptr_to_str(path));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xfaccessat(dirfd: RawFd, path: *const c_char, mode: i32, flags: i32) -> i32 { pub unsafe extern "C" fn xfaccessat(
unsafe { dirfd: RawFd,
#[allow(unused_mut)] path: *const c_char,
let mut r = libc::faccessat(dirfd, path, mode, flags); mode: i32,
if r < 0 { flags: i32,
perror!("faccessat {}", ptr_to_str(path)); ) -> i32 {
} #[allow(unused_mut)]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] let mut r = libc::faccessat(dirfd, path, mode, flags);
if r > 0 && *errno() == 0 { if r < 0 {
r = 0 perror!("faccessat {}", ptr_to_str(path));
}
return r;
} }
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
if r > 0 && *errno() == 0 {
r = 0
}
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xstat(path: *const c_char, buf: *mut libc::stat) -> i32 { pub unsafe extern "C" fn xstat(path: *const c_char, buf: *mut libc::stat) -> i32 {
unsafe { let r = libc::stat(path, buf);
let r = libc::stat(path, buf); if r < 0 {
if r < 0 { perror!("stat {}", ptr_to_str(path));
perror!("stat {}", ptr_to_str(path));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xlstat(path: *const c_char, buf: *mut libc::stat) -> i32 { pub unsafe extern "C" fn xlstat(path: *const c_char, buf: *mut libc::stat) -> i32 {
unsafe { let r = libc::lstat(path, buf);
let r = libc::lstat(path, buf); if r < 0 {
if r < 0 { perror!("lstat {}", ptr_to_str(path));
perror!("lstat {}", ptr_to_str(path));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xfstat(fd: RawFd, buf: *mut libc::stat) -> i32 { pub unsafe extern "C" fn xfstat(fd: RawFd, buf: *mut libc::stat) -> i32 {
unsafe { let r = libc::fstat(fd, buf);
let r = libc::fstat(fd, buf); if r < 0 {
if r < 0 { perror!("fstat");
perror!("fstat");
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xfstatat( pub unsafe extern "C" fn xfstatat(
dirfd: RawFd, dirfd: RawFd,
path: *const c_char, path: *const c_char,
buf: *mut libc::stat, buf: *mut libc::stat,
flags: i32, flags: i32,
) -> i32 { ) -> i32 {
unsafe { let r = libc::fstatat(dirfd, path, buf, flags);
let r = libc::fstatat(dirfd, path, buf, flags); if r < 0 {
if r < 0 { perror!("fstatat {}", ptr_to_str(path));
perror!("fstatat {}", ptr_to_str(path));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
@ -465,7 +442,7 @@ pub extern "C" fn xdup(oldfd: RawFd) -> RawFd {
if fd < 0 { if fd < 0 {
perror!("dup"); perror!("dup");
} }
return fd; fd
} }
} }
@ -476,7 +453,7 @@ pub extern "C" fn xdup2(oldfd: RawFd, newfd: RawFd) -> RawFd {
if fd < 0 { if fd < 0 {
perror!("dup2"); perror!("dup2");
} }
return fd; fd
} }
} }
@ -487,7 +464,7 @@ pub extern "C" fn xdup3(oldfd: RawFd, newfd: RawFd, flags: i32) -> RawFd {
if fd < 0 { if fd < 0 {
perror!("dup3"); perror!("dup3");
} }
return fd; fd
} }
} }
@ -502,127 +479,113 @@ pub fn xreadlinkat(dirfd: RawFd, path: &CStr, data: &mut [u8]) -> isize {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xsymlink(target: *const c_char, linkpath: *const c_char) -> i32 { pub unsafe extern "C" fn xsymlink(target: *const c_char, linkpath: *const c_char) -> i32 {
unsafe { let r = libc::symlink(target, linkpath);
let r = libc::symlink(target, linkpath); if r < 0 {
if r < 0 { perror!("symlink {} -> {}", ptr_to_str(target), ptr_to_str(linkpath));
perror!("symlink {} -> {}", ptr_to_str(target), ptr_to_str(linkpath));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xsymlinkat(target: *const c_char, dirfd: RawFd, linkpath: *const c_char) -> i32 { pub unsafe extern "C" fn xsymlinkat(
unsafe { target: *const c_char,
let r = libc::symlinkat(target, dirfd, linkpath); dirfd: RawFd,
if r < 0 { linkpath: *const c_char,
perror!( ) -> i32 {
"symlinkat {} -> {}", let r = libc::symlinkat(target, dirfd, linkpath);
ptr_to_str(target), if r < 0 {
ptr_to_str(linkpath) perror!(
); "symlinkat {} -> {}",
} ptr_to_str(target),
return r; ptr_to_str(linkpath)
);
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xlinkat( pub unsafe extern "C" fn xlinkat(
olddirfd: RawFd, olddirfd: RawFd,
target: *const c_char, target: *const c_char,
newdirfd: RawFd, newdirfd: RawFd,
linkpath: *const c_char, linkpath: *const c_char,
flags: i32, flags: i32,
) -> i32 { ) -> i32 {
unsafe { let r = libc::linkat(olddirfd, target, newdirfd, linkpath, flags);
let r = libc::linkat(olddirfd, target, newdirfd, linkpath, flags); if r < 0 {
if r < 0 { perror!("linkat {} -> {}", ptr_to_str(target), ptr_to_str(linkpath));
perror!("linkat {} -> {}", ptr_to_str(target), ptr_to_str(linkpath));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xmount( pub unsafe extern "C" fn xmount(
src: *const c_char, src: *const c_char,
target: *const c_char, target: *const c_char,
fstype: *const c_char, fstype: *const c_char,
flags: c_ulong, flags: c_ulong,
data: *const c_void, data: *const c_void,
) -> i32 { ) -> i32 {
unsafe { let r = libc::mount(src, target, fstype, flags, data);
let r = libc::mount(src, target, fstype, flags, data); if r < 0 {
if r < 0 { perror!("mount {} -> {}", ptr_to_str(src), ptr_to_str(target));
perror!("mount {} -> {}", ptr_to_str(src), ptr_to_str(target));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xumount(target: *const c_char) -> i32 { pub unsafe extern "C" fn xumount(target: *const c_char) -> i32 {
unsafe { let r = libc::umount(target);
let r = libc::umount(target); if r < 0 {
if r < 0 { perror!("umount {}", ptr_to_str(target));
perror!("umount {}", ptr_to_str(target));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 { pub unsafe extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 {
unsafe { let r = libc::umount2(target, flags);
let r = libc::umount2(target, flags); if r < 0 {
if r < 0 { perror!("umount2 {}", ptr_to_str(target));
perror!("umount2 {}", ptr_to_str(target));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xrename(oldname: *const c_char, newname: *const c_char) -> i32 { pub unsafe extern "C" fn xrename(oldname: *const c_char, newname: *const c_char) -> i32 {
unsafe { let r = libc::rename(oldname, newname);
let r = libc::rename(oldname, newname); if r < 0 {
if r < 0 { perror!("rename {} -> {}", ptr_to_str(oldname), ptr_to_str(newname));
perror!("rename {} -> {}", ptr_to_str(oldname), ptr_to_str(newname));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xmkdir(path: *const c_char, mode: mode_t) -> i32 { pub unsafe extern "C" fn xmkdir(path: *const c_char, mode: mode_t) -> i32 {
unsafe { let r = libc::mkdir(path, mode);
let r = libc::mkdir(path, mode); if r < 0 && *errno() != libc::EEXIST {
if r < 0 && *errno() != libc::EEXIST { perror!("mkdir {}", ptr_to_str(path));
perror!("mkdir {}", ptr_to_str(path));
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xmkdirs(path: *const c_char, mode: mode_t) -> i32 { pub unsafe extern "C" fn xmkdirs(path: *const c_char, mode: mode_t) -> i32 {
let r = mkdirs(path, mode); let r = mkdirs(path, mode);
if r < 0 { if r < 0 {
perror!("mkdirs {}", ptr_to_str(path)); perror!("mkdirs {}", ptr_to_str(path));
} }
return r; r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xmkdirat(dirfd: RawFd, path: *const c_char, mode: mode_t) -> i32 { pub unsafe extern "C" fn xmkdirat(dirfd: RawFd, path: *const c_char, mode: mode_t) -> i32 {
unsafe { let r = libc::mkdirat(dirfd, path, mode);
let r = libc::mkdirat(dirfd, path, mode); if r < 0 && *errno() != libc::EEXIST {
if r < 0 && *errno() != libc::EEXIST { perror!("mkdirat {}", ptr_to_str(path));
perror!("mkdirat {}", ptr_to_str(path));
}
return r;
} }
r
} }
#[inline] #[inline]
@ -634,7 +597,7 @@ pub fn xsendfile(out_fd: RawFd, in_fd: RawFd, offset: Option<&mut off_t>, count:
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xmmap( pub unsafe extern "C" fn xmmap(
addr: *mut c_void, addr: *mut c_void,
len: usize, len: usize,
prot: i32, prot: i32,
@ -642,14 +605,12 @@ pub extern "C" fn xmmap(
fd: RawFd, fd: RawFd,
offset: off_t, offset: off_t,
) -> *mut c_void { ) -> *mut c_void {
unsafe { let r = libc::mmap(addr, len, prot, flags, fd, offset);
let r = libc::mmap(addr, len, prot, flags, fd, offset); if r == libc::MAP_FAILED {
if r == libc::MAP_FAILED { perror!("mmap");
perror!("mmap"); return ptr::null_mut();
return ptr::null_mut();
}
return r;
} }
r
} }
#[no_mangle] #[no_mangle]
@ -659,7 +620,7 @@ pub extern "C" fn xfork() -> i32 {
if r < 0 { if r < 0 {
perror!("fork"); perror!("fork");
} }
return r; r
} }
} }
@ -673,16 +634,14 @@ pub fn xrealpath(path: &CStr, buf: &mut [u8]) -> isize {
if r < 0 { if r < 0 {
perror!("realpath {}", path.to_str().unwrap_or("")) perror!("realpath {}", path.to_str().unwrap_or(""))
} }
return r; r
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xmknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> i32 { pub unsafe extern "C" fn xmknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> i32 {
unsafe { let r = libc::mknod(pathname, mode, dev);
let r = libc::mknod(pathname, mode, dev); if r < 0 {
if r < 0 { perror!("mknod {}", ptr_to_str(pathname));
perror!("mknod {}", ptr_to_str(pathname));
}
return r;
} }
r
} }

View File

@ -1,3 +1,5 @@
#![allow(clippy::missing_safety_doc)]
use daemon::*; use daemon::*;
use logging::*; use logging::*;
use std::ffi::CStr; use std::ffi::CStr;

View File

@ -78,7 +78,7 @@ pub fn magisk_logging() {
unsafe { unsafe {
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr()); __android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
} }
magisk_log_write(level_to_prio(level), &msg); magisk_log_write(level_to_prio(level), msg);
} }
let logger = Logger { let logger = Logger {
@ -96,7 +96,7 @@ pub fn zygisk_logging() {
unsafe { unsafe {
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr()); __android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
} }
zygisk_log_write(level_to_prio(level), &msg); zygisk_log_write(level_to_prio(level), msg);
} }
let logger = Logger { let logger = Logger {
@ -150,7 +150,7 @@ fn magisk_log_write(prio: i32, msg: &[u8]) {
Some(s) => s, Some(s) => s,
}; };
let result = do_magisk_log_write(logd, prio, &msg); let result = do_magisk_log_write(logd, prio, msg);
// If any error occurs, shut down the logd pipe // If any error occurs, shut down the logd pipe
if result.is_err() { if result.is_err() {
@ -190,7 +190,7 @@ fn zygisk_log_write(prio: i32, msg: &[u8]) {
pthread_sigmask(SIG_BLOCK, &mask, &mut orig_mask); pthread_sigmask(SIG_BLOCK, &mask, &mut orig_mask);
} }
let result = do_magisk_log_write(logd, prio, &msg); let result = do_magisk_log_write(logd, prio, msg);
// Consume SIGPIPE if exists, then restore mask // Consume SIGPIPE if exists, then restore mask
unsafe { unsafe {
@ -278,7 +278,7 @@ extern "C" fn logfile_writer(arg: *mut c_void) -> *mut c_void {
let mut ts: timespec = std::mem::zeroed(); let mut ts: timespec = std::mem::zeroed();
let mut tm: tm = std::mem::zeroed(); let mut tm: tm = std::mem::zeroed();
if clock_gettime(CLOCK_REALTIME, &mut ts) < 0 if clock_gettime(CLOCK_REALTIME, &mut ts) < 0
|| localtime_r(&ts.tv_sec, &mut tm) == null_mut() || localtime_r(&ts.tv_sec, &mut tm).is_null()
{ {
continue; continue;
} }
@ -301,7 +301,9 @@ extern "C" fn logfile_writer(arg: *mut c_void) -> *mut c_void {
let io1 = IoSlice::new(&aux[..aux_len]); let io1 = IoSlice::new(&aux[..aux_len]);
let io2 = IoSlice::new(msg); let io2 = IoSlice::new(msg);
logfile.as_write().write_vectored(&[io1, io2])?; // We don't need to care the written len because we are writing less than PIPE_BUF
// It's guaranteed to always write the whole thing atomically
let _ = logfile.as_write().write_vectored(&[io1, io2])?;
} }
} }