Skip to content

Instantly share code, notes, and snippets.

@matklad
Last active September 30, 2015 10:12
Show Gist options
  • Save matklad/8013540174cc23bdb70f to your computer and use it in GitHub Desktop.
Save matklad/8013540174cc23bdb70f to your computer and use it in GitHub Desktop.
mod ffi {
use libc::{c_int, c_char};
extern {
pub fn setns(fd: c_int, nstype: c_int) -> c_int;
pub fn execvp(path: *const c_char, arg: *const *const c_char) -> c_int;
}
}
fn exec(args: &[String]) -> Result<(), String> {
let mut cargs = Vec::new();
let mut tmp = Vec::new();
for arg in args.iter() {
let s: &str = arg;
let p = CString::new(s).unwrap();
tmp.push(p);
cargs.push(tmp[tmp.len()-1].as_ptr());
}
cargs.push(ptr::null());
let ret = unsafe {
ffi::execvp(cargs[0], cargs.as_ptr())
};
Err(format!("execv error: {}", ret))
}
@matklad
Copy link
Author

matklad commented Sep 30, 2015

example of an FFI call in Rust, for execvp and setns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment