mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-22 16:07:39 +00:00
Use Utf8CStr for logging
This commit is contained in:
parent
89aee6ffa7
commit
0a2a590ab7
@ -15,7 +15,7 @@ static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) {
|
|||||||
buf[0] = '\0';
|
buf[0] = '\0';
|
||||||
// Fortify logs when a fatal error occurs. Do not run through fortify again
|
// 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);
|
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;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ use std::panic::Location;
|
|||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
use crate::ffi::LogLevel;
|
use crate::ffi::LogLevel;
|
||||||
use crate::Utf8CStrArr;
|
use crate::{Utf8CStr, Utf8CStrArr};
|
||||||
|
|
||||||
// Error handling and logging throughout the Rust codebase in Magisk:
|
// Error handling and logging throughout the Rust codebase in Magisk:
|
||||||
//
|
//
|
||||||
@ -37,7 +37,7 @@ pub static mut LOGGER: Logger = Logger {
|
|||||||
flags: 0,
|
flags: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
type LogWriter = fn(level: LogLevel, msg: &[u8]);
|
type LogWriter = fn(level: LogLevel, msg: &Utf8CStr);
|
||||||
type Formatter<'a> = &'a mut dyn fmt::Write;
|
type Formatter<'a> = &'a mut dyn fmt::Write;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[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]) {
|
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));
|
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| {
|
log_with_writer(level, |write| {
|
||||||
let mut buf = Utf8CStrArr::default();
|
let mut buf = Utf8CStrArr::default();
|
||||||
f(&mut buf).ok();
|
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() {
|
pub fn cmdline_logging() {
|
||||||
fn cmdline_write(level: LogLevel, msg: &[u8]) {
|
fn cmdline_write(level: LogLevel, msg: &Utf8CStr) {
|
||||||
if level == LogLevel::Info {
|
if level == LogLevel::Info {
|
||||||
stdout().write_all(msg).ok();
|
stdout().write_all(msg.as_bytes()).ok();
|
||||||
} else {
|
} else {
|
||||||
stderr().write_all(msg).ok();
|
stderr().write_all(msg.as_bytes()).ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ enum ALogPriority {
|
|||||||
type ThreadEntry = extern "C" fn(*mut c_void) -> *mut c_void;
|
type ThreadEntry = extern "C" fn(*mut c_void) -> *mut c_void;
|
||||||
|
|
||||||
extern "C" {
|
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 strftime(buf: *mut c_char, len: usize, fmt: *const c_char, tm: *const tm) -> usize;
|
||||||
|
|
||||||
fn zygisk_fetch_logd() -> RawFd;
|
fn zygisk_fetch_logd() -> RawFd;
|
||||||
@ -58,7 +58,7 @@ fn level_to_prio(level: LogLevel) -> i32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn android_logging() {
|
pub fn android_logging() {
|
||||||
fn android_log_write(level: LogLevel, msg: &[u8]) {
|
fn android_log_write(level: LogLevel, msg: &Utf8CStr) {
|
||||||
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());
|
||||||
}
|
}
|
||||||
@ -75,15 +75,15 @@ pub fn android_logging() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn magisk_logging() {
|
pub fn magisk_logging() {
|
||||||
fn magisk_write(level: LogLevel, msg: &[u8]) {
|
fn magisk_log_write(level: LogLevel, msg: &Utf8CStr) {
|
||||||
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_to_pipe(level_to_prio(level), msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
let logger = Logger {
|
let logger = Logger {
|
||||||
write: magisk_write,
|
write: magisk_log_write,
|
||||||
flags: 0,
|
flags: 0,
|
||||||
};
|
};
|
||||||
exit_on_error(false);
|
exit_on_error(false);
|
||||||
@ -93,15 +93,15 @@ pub fn magisk_logging() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn zygisk_logging() {
|
pub fn zygisk_logging() {
|
||||||
fn zygisk_write(level: LogLevel, msg: &[u8]) {
|
fn zygisk_log_write(level: LogLevel, msg: &Utf8CStr) {
|
||||||
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_to_pipe(level_to_prio(level), msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
let logger = Logger {
|
let logger = Logger {
|
||||||
write: zygisk_write,
|
write: zygisk_log_write,
|
||||||
flags: 0,
|
flags: 0,
|
||||||
};
|
};
|
||||||
exit_on_error(false);
|
exit_on_error(false);
|
||||||
@ -121,10 +121,10 @@ struct LogMeta {
|
|||||||
|
|
||||||
const MAX_MSG_LEN: usize = PIPE_BUF - std::mem::size_of::<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
|
// Truncate message if needed
|
||||||
let len = min(MAX_MSG_LEN, msg.len());
|
let len = min(MAX_MSG_LEN, msg.len());
|
||||||
let msg = &msg[..len];
|
let msg = &msg.as_bytes()[..len];
|
||||||
|
|
||||||
let meta = LogMeta {
|
let meta = LogMeta {
|
||||||
prio,
|
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])
|
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() {
|
let magiskd = match MAGISKD.get() {
|
||||||
None => return,
|
None => return,
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
@ -151,7 +151,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 = write_log_to_pipe(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() {
|
||||||
@ -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() {
|
let magiskd = match MAGISKD.get() {
|
||||||
None => return,
|
None => return,
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
@ -191,7 +191,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 = write_log_to_pipe(logd, prio, msg);
|
||||||
|
|
||||||
// Consume SIGPIPE if exists, then restore mask
|
// Consume SIGPIPE if exists, then restore mask
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
use std::io::{IoSlice, Write};
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
use base::ffi::LogLevel;
|
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,
|
close, makedev, mknod, open, syscall, unlink, SYS_dup3, O_CLOEXEC, O_RDWR, STDERR_FILENO,
|
||||||
STDIN_FILENO, STDOUT_FILENO, S_IFCHR,
|
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();
|
static KMSG: OnceLock<File> = OnceLock::new();
|
||||||
|
|
||||||
@ -49,19 +49,16 @@ pub fn setup_klog() {
|
|||||||
writeln!(rate, "on").ok();
|
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() {
|
if let Some(kmsg) = KMSG.get().as_mut() {
|
||||||
let mut buf = Utf8CStrArr::default();
|
let io1 = IoSlice::new("magiskinit: ".as_bytes());
|
||||||
buf.append("magiskinit: ");
|
let io2 = IoSlice::new(msg.as_bytes());
|
||||||
unsafe {
|
kmsg.write_vectored(&[io1, io2]).ok();
|
||||||
buf.append_unchecked(msg);
|
|
||||||
}
|
|
||||||
kmsg.write_all(buf.as_bytes()).ok();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let logger = Logger {
|
let logger = Logger {
|
||||||
write: klog_write_impl,
|
write: kmsg_log_write,
|
||||||
flags: 0,
|
flags: 0,
|
||||||
};
|
};
|
||||||
exit_on_error(false);
|
exit_on_error(false);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user