Skip to content

Instantly share code, notes, and snippets.

@krishnakumar4a4
Created October 12, 2018 15:30
Show Gist options
  • Save krishnakumar4a4/24f37239b3ea46fcf93c07d2e7d95dcc to your computer and use it in GitHub Desktop.
Save krishnakumar4a4/24f37239b3ea46fcf93c07d2e7d95dcc to your computer and use it in GitHub Desktop.
Run os commands with rust
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());
}
@krishnakumar4a4
Copy link
Author

Running OS commands with rust

Set UID on executable

  • An executable can be set UID bit to make it runner with owner user instead of caller.
  • This can be used where you want to execute a higher privileges program(like root) with lower privileges user.
  • chmod 4111 <executable>
  • libc::geteuid() gives you effective user id after set UID

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