Skip to content

Commit

Permalink
run formatter, add stub test
Browse files Browse the repository at this point in the history
  • Loading branch information
thundergolfer committed May 19, 2024
1 parent 78571e8 commit 733ab41
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 171 deletions.
7 changes: 7 additions & 0 deletions operating_systems/linux/strace_rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions operating_systems/linux/strace_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.86"
bitflags = "2.5.0"
libc = "0.2.139"
num = "0.4.3"
Expand Down
28 changes: 15 additions & 13 deletions operating_systems/linux/strace_rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[macro_use]
extern crate num_derive;
#[macro_use]
Expand All @@ -13,28 +12,29 @@ use tracing::{debug, error};
pub mod ptrace;

unsafe fn do_child<T>(args: T) -> i32
where T: IntoIterator<Item = String> {
let cstrings: Vec<CString> = args.into_iter()
where
T: IntoIterator<Item = String>,
{
let cstrings: Vec<CString> = args
.into_iter()
.map(|arg| CString::new(arg.as_str()).expect("CString::new failed"))
.collect();

let child_prog = cstrings.get(0).unwrap().clone();
debug!(?child_prog, "starting child");
let child_prog = child_prog.into_raw();
let mut c_pointers: Vec<*const libc::c_char> = cstrings
.iter()
.map(|cstr| cstr.as_ptr())
.collect();
let mut c_pointers: Vec<*const libc::c_char> =
cstrings.iter().map(|cstr| cstr.as_ptr()).collect();
// Ensure null termination for C-style argv array
c_pointers.push(ptr::null());

let argv: *const *const libc::c_char = c_pointers.as_ptr();

let mut envp: Vec<*const libc::c_char> = std::env::vars()
.into_iter()
.map(|(k,v)| CString::new(format!("{k}={v}")).expect("cannot fail"))
.map(|cstr| cstr.as_ptr())
.collect();
.into_iter()
.map(|(k, v)| CString::new(format!("{k}={v}")).expect("cannot fail"))
.map(|cstr| cstr.as_ptr())
.collect();
envp.push(ptr::null());
let envp: *const *const libc::c_char = envp.as_ptr();
libc::execve(child_prog, argv, envp);
Expand All @@ -52,8 +52,10 @@ fn do_trace(child: i32) -> i32 {
0
}

pub unsafe fn trace_command<T>(args: T) -> i32
where T: IntoIterator<Item = String> {
pub unsafe fn trace_command<T>(args: T) -> i32
where
T: IntoIterator<Item = String>,
{
unsafe {
let child = libc::fork();
debug!(?child, "after fork");
Expand Down
4 changes: 1 addition & 3 deletions operating_systems/linux/strace_rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ fn main() {
// and then we fork() to create two processes –
// one to execute the program to be traced, and
// the other to trace it.
let exit_code = unsafe {
trace_command(args.into_iter())
};
let exit_code = unsafe { trace_command(args.into_iter()) };
process::exit(exit_code);
}
Loading

0 comments on commit 733ab41

Please sign in to comment.