lazypoline_rs/
lib.rs

1//! lazypoline-rs - A framework for building syscall interposers
2//!
3//! This framework provides tools for intercepting and handling system calls
4//! in user-space Linux applications using Syscall User Dispatch (SUD)
5//! and binary rewriting for maximum efficiency.
6//!
7//! # Getting Started
8//!
9//! ```rust
10//! use lazypoline::{self, syscall, SyscallContext, SyscallAction};
11//!
12//! #[lazypoline::syscall_handler]
13//! fn handle_open(ctx: &mut SyscallContext) -> SyscallAction {
14//!     println!("Open syscall: {}", unsafe { std::ffi::CStr::from_ptr(ctx.args.rdi as *const i8).to_string_lossy() });
15//!     SyscallAction::Allow
16//! }
17//!
18//! fn main() -> Result<(), Box<dyn std::error::Error>> {
19//!     // Initialize the interposer
20//!     let interposer = lazypoline::new()
21//!         .handler(handle_open())
22//!         .trace(true)
23//!         .build()?
24//!         .init()?;
25//!
26//!     // Your application code here
27//!     
28//!     // The interposer is automatically cleaned up when dropped
29//!     Ok(())
30//! }
31//! ```
32
33pub mod core;
34pub mod ffi;
35pub mod interposer;
36pub mod syscall;
37pub mod util;
38
39pub use lazypoline_macros::{syscall_enum, syscall_handler};
40
41pub use crate::interposer::SyscallHandler;
42pub use interposer::{Interposer, InterposerBuilder, InterposerError};
43pub use syscall::{Syscall, SyscallAction, SyscallArgs, SyscallContext};
44
45/// Create a new interposer builder
46#[must_use] pub fn new() -> InterposerBuilder {
47	InterposerBuilder::new()
48}
49
50/// Initialize lazypoline with default settings
51///
52/// This is equivalent to `new().build().init()`
53pub fn init() -> Result<Interposer, InterposerError> {
54	new().build()?.init()
55}
56
57/// Shorthand for setting up a simple syscall tracer
58pub fn trace() -> Result<Interposer, InterposerError> {
59	new().trace(true).build()?.init()
60}
61
62// Export for FFI (used by bootstrap)
63#[unsafe(no_mangle)]
64pub extern "C" fn bootstrap_lazypoline() {
65	if let Err(e) = init() {
66		eprintln!("Failed to initialize lazypoline: {e}");
67		std::process::exit(1);
68	}
69}