Files
syscall-stream-rs/src/lib.rs
T
2025-08-18 16:45:17 -06:00

127 lines
2.6 KiB
Rust

mod host;
#[allow(dead_code)]
mod syscall_intercept;
use syscall_lib::Syscall;
use libc::exit;
use std::{
cell::Cell,
io::{BufWriter, Write},
net::TcpStream,
sync::Mutex,
};
use syscall_intercept::*;
use crate::host::Host;
static mut HOST: Option<Mutex<Host>> = None;
#[ctor::ctor]
fn start() {
// unsafe {
// HOST = Some({
// match Host::new() {
// Ok(host) => Mutex::new(host),
// Err(e) => {
// eprintln!("Failed to connect to server: {}", e);
// exit(1);
// }
// }
// });
// }
unsafe { set_hook_fn(hook) };
}
// fn print_direct(str: &str)
thread_local! {
/// A flag indicating whether the current thread is in an intercept context.
static INTERCEPTED: Cell<bool> = Cell::new(false);
}
// lazy_static! {
// }
struct InterceptGuard;
impl InterceptGuard {
fn try_lock() -> Option<Self> {
INTERCEPTED.with(|x| {
if x.get() {
None
} else {
x.set(true);
Some(InterceptGuard)
}
})
}
}
impl Drop for InterceptGuard {
fn drop(&mut self) {
INTERCEPTED.with(|x| x.set(false));
}
}
extern "C" fn hook(
syscall_num: libc::c_long,
arg0: libc::c_ulong,
arg1: libc::c_ulong,
arg2: libc::c_ulong,
arg3: libc::c_ulong,
arg4: libc::c_ulong,
arg5: libc::c_ulong,
result: &mut isize,
) -> InterceptResult {
// detect and avoid recursive interception
let _guard = match InterceptGuard::try_lock() {
Some(g) => g,
None => {
println!("Disabled!");
return InterceptResult::Forward;
}
};
// if syscall_should_proxy(syscall_num) {
// return InterceptResult::Forward;
// }
unsafe {
unset_hook_fn();
}
let args = [arg0, arg1, arg2, arg3, arg4, arg5];
let syscall = Syscall::from_syscall(syscall_num as libc::c_long, args).unwrap();
// if !syscall_should_proxy(&syscall) {
// return InterceptResult::Forward;
// }
// Return if is print
// if syscall_num == libc::SYS_write as _ && arg0 == 1 {
// return InterceptResult::Forward;
// }
// if syscall_num == libc::SYS_close as _ && arg0 == 1 {
// return InterceptResult::Forward;
// }
println!("{:?}", syscall);
// unsafe {
// #[allow(static_mut_refs)]
// if let Some(host) = HOST.as_ref() {
// *result = host.lock().unwrap().execute(&syscall);
// }
// }
unsafe {
set_hook_fn(hook);
}
InterceptResult::Forward
}