Created
October 12, 2018 15:30
-
-
Save krishnakumar4a4/24f37239b3ea46fcf93c07d2e7d95dcc to your computer and use it in GitHub Desktop.
Run os commands with rust
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
extern crate libc; | |
use std::process::{Command,Stdio}; | |
fn main() { | |
println!("Hello, world!"); | |
unsafe { | |
println!("Euid is {}",libc::geteuid()); | |
} | |
println!("ps output {:?}",Command::new("ps").arg("-efl").output().unwrap()); | |
let ps_cmd = Command::new("ps").arg("-efl").stdout(Stdio::piped()).spawn().expect("Error ps cmd"); | |
let ps_out = ps_cmd.stdout.expect("error ps out"); | |
let grep_cmd = Command::new("grep").arg("docker").stdin(ps_out).stdout(Stdio::piped()).spawn().expect("Error grep cmd"); | |
let out = grep_cmd.wait_with_output().expect("Error out"); | |
println!("Final output {:?}",out.stdout.as_slice()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running OS commands with rust
Set UID on executable
chmod 4111 <executable>
libc::geteuid()
gives you effective user id after set UID