Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle binary for shebangs #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ clap = "2.25.1"
syscall = "0.2.1"
lazy_static = "0.2.8"
byteorder = "1.1.0"
bstr = "0.1"

[build-dependencies]
gcc = { git = "https://github.com/vincenthage/gcc-rs", branch = "master" }
gcc = { git = "https://github.com/vincenthage/gcc-rs", branch = "master" }
49 changes: 31 additions & 18 deletions src/kernel/execve/shebang.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::path::{Path, PathBuf};
use std::fs::File;
use std::io::{BufRead, BufReader};
extern crate bstr;

use self::bstr::BString;
use errors::{Error, Result};
use filesystem::{FileSystem, Translator};
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Path, PathBuf};

/// Expand in argv[] the shebang of `user_path`, if any. This function
/// returns -errno if an error occurred, 1 if a shebang was found and
Expand Down Expand Up @@ -134,20 +137,32 @@ pub fn translate_and_check_exec(fs: &FileSystem, guest_path: &Path) -> Result<Pa
/// string can include white space.
//const char *host_path, char user_path[PATH_MAX], char argument[BINPRM_BUF_SIZE]
fn extract(host_path: &Path) -> Result<Option<PathBuf>> {
let file = File::open(host_path)?;
let mut first_line = String::new();
BufReader::new(file).read_line(&mut first_line)?;
if !first_line.starts_with("#!") {
return Ok(None);
let mut bytes = BufReader::new(File::open(host_path)?).bytes();
match (bytes.next(), bytes.next()) {
(Some(Err(err)), _) | (_, Some(Err(err))) => return Err(Error::from(err)),
(Some(Ok(b'#')), Some(Ok(b'!'))) => {}
_ => return Ok(None),
}

let mut arguments = first_line[2..]
.split(" ")
.map(|s| s.trim());
match arguments.next() {
None => Err(Error::InvalidPath("Cannot have empty shebang")),
Some(path) => Ok(Some(Path::new(path).to_path_buf()))
let first_line = bytes
.take_while(|c| match c {
Ok(b'\n') => true,
_ => false,
})
.collect::<std::result::Result<Vec<u8>, _>>()?;
let path: Vec<u8> = first_line
.clone()
.into_iter()
.take_while(|c| !c.is_ascii_whitespace())
.collect();
if path.is_empty() {
return Err(Error::InvalidPath("Cannot have empty shebang"));
}
// NOTE: this unwrap may fail on non-UNIX systems (a.k.a Windows)
// where paths may not be arbitrary bytes
let arg = first_line[path.len()..].to_vec();
let mut argv = PathBuf::from(BString::from(path).to_path().unwrap());
argv.push(BString::from_vec(arg).to_path().unwrap());
Ok(Some(argv))
//
// /* Skip leading spaces. */
// do {
Expand Down Expand Up @@ -258,13 +273,11 @@ fn extract(host_path: &Path) -> Result<Option<PathBuf>> {
// return 1;
}



#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
use filesystem::FileSystem;
use std::path::PathBuf;

#[test]
fn test_extract_shebang_not_script() {
Expand Down