Use Utf8CStr for logging

This commit is contained in:
topjohnwu 2023-09-12 17:35:20 -07:00
parent 89aee6ffa7
commit 0a2a590ab7
4 changed files with 30 additions and 31 deletions

View File

@ -15,7 +15,7 @@ static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) {
buf[0] = '\0';
// Fortify logs when a fatal error occurs. Do not run through fortify again
int len = std::min(__call_bypassing_fortify(vsnprintf)(buf, sz, fmt, ap), sz - 1);
log_with_rs(level, byte_view(buf, len));
log_with_rs(level, byte_view(buf, len + 1));
return len;
}

View File

@ -5,7 +5,7 @@ use std::panic::Location;
use std::process::exit;
use crate::ffi::LogLevel;
use crate::Utf8CStrArr;
use crate::{Utf8CStr, Utf8CStrArr};
// Error handling and logging throughout the Rust codebase in Magisk:
//
@ -37,7 +37,7 @@ pub static mut LOGGER: Logger = Logger {
flags: 0,
};
type LogWriter = fn(level: LogLevel, msg: &[u8]);
type LogWriter = fn(level: LogLevel, msg: &Utf8CStr);
type Formatter<'a> = &'a mut dyn fmt::Write;
#[derive(Copy, Clone)]
@ -91,6 +91,8 @@ fn log_with_writer<F: FnOnce(LogWriter)>(level: LogLevel, f: F) {
}
pub fn log_from_cxx(level: LogLevel, msg: &[u8]) {
// SAFETY: The null termination is handled on the C++ side
let msg = unsafe { Utf8CStr::from_bytes_unchecked(msg) };
log_with_writer(level, |write| write(level, msg));
}
@ -98,7 +100,7 @@ pub fn log_with_formatter<F: FnOnce(Formatter) -> fmt::Result>(level: LogLevel,
log_with_writer(level, |write| {
let mut buf = Utf8CStrArr::default();
f(&mut buf).ok();
write(level, buf.as_bytes_with_nul());
write(level, &buf);
});
}
@ -107,11 +109,11 @@ pub fn log_with_args(level: LogLevel, args: Arguments) {
}
pub fn cmdline_logging() {
fn cmdline_write(level: LogLevel, msg: &[u8]) {
fn cmdline_write(level: LogLevel, msg: &Utf8CStr) {
if level == LogLevel::Info {
stdout().write_all(msg).ok();
stdout().write_all(msg.as_bytes()).ok();
} else {
stderr().write_all(msg).ok();
stderr().write_all(msg.as_bytes()).ok();
}
}

View File

@ -40,7 +40,7 @@ enum ALogPriority {
type ThreadEntry = extern "C" fn(*mut c_void) -> *mut c_void;
extern "C" {
fn __android_log_write(prio: i32, tag: *const c_char, msg: *const u8);
fn __android_log_write(prio: i32, tag: *const c_char, msg: *const c_char);
fn strftime(buf: *mut c_char, len: usize, fmt: *const c_char, tm: *const tm) -> usize;
fn zygisk_fetch_logd() -> RawFd;
@ -58,7 +58,7 @@ fn level_to_prio(level: LogLevel) -> i32 {
}
pub fn android_logging() {
fn android_log_write(level: LogLevel, msg: &[u8]) {
fn android_log_write(level: LogLevel, msg: &Utf8CStr) {
unsafe {
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
}
@ -75,15 +75,15 @@ pub fn android_logging() {
}
pub fn magisk_logging() {
fn magisk_write(level: LogLevel, msg: &[u8]) {
fn magisk_log_write(level: LogLevel, msg: &Utf8CStr) {
unsafe {
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
}
magisk_log_write(level_to_prio(level), msg);
magisk_log_to_pipe(level_to_prio(level), msg);
}
let logger = Logger {
write: magisk_write,
write: magisk_log_write,
flags: 0,
};
exit_on_error(false);
@ -93,15 +93,15 @@ pub fn magisk_logging() {
}
pub fn zygisk_logging() {
fn zygisk_write(level: LogLevel, msg: &[u8]) {
fn zygisk_log_write(level: LogLevel, msg: &Utf8CStr) {
unsafe {
__android_log_write(level_to_prio(level), raw_cstr!("Magisk"), msg.as_ptr());
}
zygisk_log_write(level_to_prio(level), msg);
zygisk_log_to_pipe(level_to_prio(level), msg);
}
let logger = Logger {
write: zygisk_write,
write: zygisk_log_write,
flags: 0,
};
exit_on_error(false);
@ -121,10 +121,10 @@ struct LogMeta {
const MAX_MSG_LEN: usize = PIPE_BUF - std::mem::size_of::<LogMeta>();
fn do_magisk_log_write(logd: &mut File, prio: i32, msg: &[u8]) -> io::Result<usize> {
fn write_log_to_pipe(logd: &mut File, prio: i32, msg: &Utf8CStr) -> io::Result<usize> {
// Truncate message if needed
let len = min(MAX_MSG_LEN, msg.len());
let msg = &msg[..len];
let msg = &msg.as_bytes()[..len];
let meta = LogMeta {
prio,
@ -138,7 +138,7 @@ fn do_magisk_log_write(logd: &mut File, prio: i32, msg: &[u8]) -> io::Result<usi
logd.write_vectored(&[io1, io2])
}
fn magisk_log_write(prio: i32, msg: &[u8]) {
fn magisk_log_to_pipe(prio: i32, msg: &Utf8CStr) {
let magiskd = match MAGISKD.get() {
None => return,
Some(s) => s,
@ -151,7 +151,7 @@ fn magisk_log_write(prio: i32, msg: &[u8]) {
Some(s) => s,
};
let result = do_magisk_log_write(logd, prio, msg);
let result = write_log_to_pipe(logd, prio, msg);
// If any error occurs, shut down the logd pipe
if result.is_err() {
@ -159,7 +159,7 @@ fn magisk_log_write(prio: i32, msg: &[u8]) {
}
}
fn zygisk_log_write(prio: i32, msg: &[u8]) {
fn zygisk_log_to_pipe(prio: i32, msg: &Utf8CStr) {
let magiskd = match MAGISKD.get() {
None => return,
Some(s) => s,
@ -191,7 +191,7 @@ fn zygisk_log_write(prio: i32, msg: &[u8]) {
pthread_sigmask(SIG_BLOCK, &mask, &mut orig_mask);
}
let result = do_magisk_log_write(logd, prio, msg);
let result = write_log_to_pipe(logd, prio, msg);
// Consume SIGPIPE if exists, then restore mask
unsafe {

View File

@ -1,6 +1,6 @@
use std::fs;
use std::fs::File;
use std::io::Write;
use std::io::{IoSlice, Write};
use std::sync::OnceLock;
use base::ffi::LogLevel;
@ -8,7 +8,7 @@ use base::libc::{
close, makedev, mknod, open, syscall, unlink, SYS_dup3, O_CLOEXEC, O_RDWR, STDERR_FILENO,
STDIN_FILENO, STDOUT_FILENO, S_IFCHR,
};
use base::*;
use base::{cstr, exit_on_error, raw_cstr, Logger, Utf8CStr, LOGGER};
static KMSG: OnceLock<File> = OnceLock::new();
@ -49,19 +49,16 @@ pub fn setup_klog() {
writeln!(rate, "on").ok();
}
fn klog_write_impl(_: LogLevel, msg: &[u8]) {
fn kmsg_log_write(_: LogLevel, msg: &Utf8CStr) {
if let Some(kmsg) = KMSG.get().as_mut() {
let mut buf = Utf8CStrArr::default();
buf.append("magiskinit: ");
unsafe {
buf.append_unchecked(msg);
}
kmsg.write_all(buf.as_bytes()).ok();
let io1 = IoSlice::new("magiskinit: ".as_bytes());
let io2 = IoSlice::new(msg.as_bytes());
kmsg.write_vectored(&[io1, io2]).ok();
}
}
let logger = Logger {
write: klog_write_impl,
write: kmsg_log_write,
flags: 0,
};
exit_on_error(false);