Skip to content

Commit

Permalink
implement a simple shell in linux using nix
Browse files Browse the repository at this point in the history
  • Loading branch information
droundy committed Nov 30, 2015
1 parent d409e27 commit 4554013
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ repository = "https://github.com/droundy/bigbro"
documentation = "https://droundy.github.io/bigbro"

[dependencies]
nix = "0.4"
# nix = "0.4"
nix = { git = "https://github.com/carllerche/nix-rust" }
libc = "*"

[dev-dependencies]
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! This allows you to track file accesses by child processes.

extern crate nix;

use std::path;
use std::collections::HashSet;

Expand Down Expand Up @@ -31,7 +33,7 @@ mod linux;
pub use linux::shell;

#[cfg(not(target_os = "linux"))]
pub fn shell(command_line: &str) -> io::Result<Accesses> {
pub fn shell(command_line: &str) -> Result<Accesses, Box<Error>> {
let r = try!(try!(process::Command::new("sh").arg("-c")
.arg(command_line).spawn()).wait());
Ok(Accesses {
Expand Down
57 changes: 46 additions & 11 deletions src/linux.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
use super::{Accesses, ExitStatus};

use std::process;
use std::collections::HashSet;
use std::io;
use super::{Accesses};

use std::io;
use nix;
use std::ffi::{CString};
use std::collections::HashSet;

pub fn shell(command_line: &str) -> io::Result<Accesses> {
let r = try!(try!(process::Command::new("sh").arg("-c")
.arg(command_line).spawn()).wait());
Ok(Accesses {
status: ExitStatus { exit_code: r.code() },
read_files: HashSet::new(),
wrote_files: HashSet::new(),
})
match try!(nix::unistd::fork()) {
nix::unistd::Fork::Parent(pid) => {
use nix::sys::wait::WaitStatus::*;
use super::ExitStatus;
println!("I am parent of {}", pid);
match try!(nix::sys::wait::waitpid(pid, None)) {
Exited(_,ii) => {
Ok(Accesses {
status: ExitStatus { exit_code: Some(ii as i32) },
read_files: HashSet::new(),
wrote_files: HashSet::new(),
})
},
Signaled(_,_,_) => {
Ok(Accesses {
status: ExitStatus { exit_code: None },
read_files: HashSet::new(),
wrote_files: HashSet::new(),
})
},
Stopped(_,_) => unreachable!(),
Continued(_) => unreachable!(),
StillAlive => unreachable!(),
}
},
nix::unistd::Fork::Child => {
println!("Hello world");
nix::unistd::execvp(&CString::new("sh").unwrap(),
&[CString::new("sh").unwrap(),
CString::new("-c").unwrap(),
CString::new(command_line).unwrap()]);
unreachable!()
},
}
}

#[test]
fn test_into_io_error() {
use nix::errno::Errno;
use std;
std::io::Error::from(nix::Error::InvalidPath);
}

0 comments on commit 4554013

Please sign in to comment.