I want to override the fopen function in libc so that for example I can print something before the main functionality. Consider this code:
use libc::FILE;
use libc::c_char;
use libc::fopen as fopen_c;
#[no_mangle]
#[link(name="fopen")]
unsafe fn fopen(filename: *const c_char, mode: *const c_char) {
println!("made some change");
}
if I Use LD_PRELOAD and run a c file that uses fopen , the result of fopen would not be a returned file, but the printed string. But what if I want to keep the main functionality of it, and also print the string ?
something like this:
use libc::fopen as fopen_c;
unsafe fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE {
println!("made some chnge");
fopen_c(filename, mode)
}
Running the code above will result in an infinite recursion.