Last active
September 30, 2015 10:12
-
-
Save matklad/8013540174cc23bdb70f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example of an FFI call in Rust, for execvp and setns.