Skip to content
This repository has been archived by the owner on Dec 9, 2018. It is now read-only.

Commit

Permalink
Merge pull request #83 from tbu-/pr_basic_command
Browse files Browse the repository at this point in the history
Add very bare-bones `process::Command`
  • Loading branch information
Jorge Aparicio committed Feb 2, 2017
2 parents 61fc606 + afb395b commit fea5ca4
Show file tree
Hide file tree
Showing 19 changed files with 951 additions and 18 deletions.
11 changes: 11 additions & 0 deletions examples/execve.rs
@@ -0,0 +1,11 @@
use std::process::Command;

fn main() {
let result = Command::new("/bin/echo")
.arg("Execution of /bin/echo successful")
.spawn()
.unwrap()
.wait()
.unwrap();
assert!(result.success());
}
2 changes: 2 additions & 0 deletions src/linux/aarch64.rs
Expand Up @@ -13,6 +13,8 @@ pub const O_TRUNC: c_int = 0o00001000;

pub const FIOCLEX: c_uint = 0x5451;

pub const SIGCHLD: c_ulong = 17;

#[derive(Clone, Copy)]
#[repr(C)]
pub struct stat64 {
Expand Down
2 changes: 2 additions & 0 deletions src/linux/arm.rs
Expand Up @@ -13,6 +13,8 @@ pub const O_TRUNC: c_int = 0o00001000;

pub const FIOCLEX: c_uint = 0x5451;

pub const SIGCHLD: c_ulong = 17;

// include/linux/types.h
pub type ino_t = __kernel_ino_t;
// include/uapi/asm-generic/posix_types.h
Expand Down
19 changes: 19 additions & 0 deletions src/linux/libc.rs
@@ -0,0 +1,19 @@
use super::c_int;

// Rust 1.14.0: src/liblibc/src/unix/notbsd/mod.rs
#[allow(non_snake_case)]
pub fn WTERMSIG(status: c_int) -> c_int {
status & 0x7f
}

// Rust 1.14.0: src/liblibc/src/unix/notbsd/mod.rs
#[allow(non_snake_case)]
pub fn WIFEXITED(status: c_int) -> bool {
(status & 0x7f) == 0
}

// Rust 1.14.0: src/liblibc/src/unix/notbsd/mod.rs
#[allow(non_snake_case)]
pub fn WEXITSTATUS(status: c_int) -> c_int {
(status >> 8) & 0xff
}
2 changes: 2 additions & 0 deletions src/linux/mips.rs
Expand Up @@ -13,6 +13,8 @@ pub const O_TRUNC: c_int = 0x0200;

pub const FIOCLEX: c_uint = 0x6601;

pub const SIGCHLD: c_ulong = 18;

#[derive(Clone, Copy)]
#[repr(C)]
pub struct stat64 {
Expand Down
2 changes: 2 additions & 0 deletions src/linux/mips64.rs
Expand Up @@ -13,6 +13,8 @@ pub const O_TRUNC: c_int = 0x0200;

pub const FIOCLEX: c_uint = 0x6601;

pub const SIGCHLD: c_ulong = 18;

#[derive(Clone, Copy)]
#[repr(C)]
pub struct stat64 {
Expand Down
95 changes: 94 additions & 1 deletion src/linux/mod.rs
Expand Up @@ -34,12 +34,15 @@ mod arch;
#[path = "x86_64.rs"]
mod arch;

pub mod types;
mod libc;
mod types;

use core::intrinsics;
use ctypes::*;
use ptr;

pub use self::arch::*;
pub use self::libc::*;
pub use self::types::*;

// include/uapi/linux/fcntl.h
Expand Down Expand Up @@ -397,8 +400,98 @@ pub unsafe fn mkdir(pathname: *const c_char, mode: umode_t) -> ssize_t {
}

// fs/readdir.c
#[inline(always)]
pub unsafe fn getdents64(fd: c_int, dirent: *mut linux_dirent64, count: c_uint)
-> ssize_t
{
syscall!(GETDENTS64, fd, dirent, count) as ssize_t
}

// kernel/fork.c
#[inline(always)]
pub unsafe fn clone(clone_flags: c_ulong,
newsp: c_ulong,
parent_tidptr: *mut c_int,
tls: c_ulong,
child_tidptr: *mut c_int)
-> ssize_t
{
#[cfg(any(target_arch = "aarch64",
target_arch = "arm",
target_arch = "mips",
target_arch = "mips64",
target_arch = "powerpc",
target_arch = "powerpc64",
target_arch = "x86"))]
#[inline(always)]
unsafe fn clone(clone_flags: c_ulong,
newsp: c_ulong,
parent_tidptr: *mut c_int,
tls: c_ulong,
child_tidptr: *mut c_int)
-> ssize_t
{
syscall!(CLONE, clone_flags, newsp, parent_tidptr, tls, child_tidptr) as ssize_t
}
#[cfg(any(target_arch = "x86_64"))]
#[inline(always)]
unsafe fn clone(clone_flags: c_ulong,
newsp: c_ulong,
parent_tidptr: *mut c_int,
tls: c_ulong,
child_tidptr: *mut c_int)
-> ssize_t
{
syscall!(CLONE, clone_flags, newsp, parent_tidptr, child_tidptr, tls) as ssize_t
}
clone(clone_flags, newsp, parent_tidptr, tls, child_tidptr)
}

// kernel/fork.c
#[inline(always)]
pub unsafe fn fork() -> ssize_t {
clone(SIGCHLD, 0, ptr::null_mut(), 0, ptr::null_mut())
}

// fs/exec.c
#[inline(always)]
pub unsafe fn execve(filename: *const c_char,
argv: *const *const c_char,
envp: *const *const c_char)
-> ssize_t
{
syscall!(EXECVE, filename, argv, envp) as ssize_t
}

// fs/pipe.c
#[inline(always)]
pub unsafe fn pipe2(filedes: *mut c_int, flags: c_int) -> ssize_t {
syscall!(PIPE2, filedes, flags) as ssize_t
}

// fs/pipe.c
#[inline(always)]
pub unsafe fn pipe(filedes: *mut c_int) -> ssize_t {
#[inline(always)]
#[cfg(not(target_arch = "aarch64"))]
unsafe fn pipe(filedes: *mut c_int) -> ssize_t {
syscall!(PIPE, filedes) as ssize_t
}
#[inline(always)]
#[cfg(target_arch = "aarch64")]
unsafe fn pipe(filedes: *mut c_int) -> ssize_t {
pipe2(filedes, 0)
}
pipe(filedes)
}

// kernel/exit.c
#[inline(always)]
pub unsafe fn wait4(upid: pid_t,
stat_addr: *mut c_int,
options: c_int,
ru: *mut rusage)
-> ssize_t
{
syscall!(WAIT4, upid, stat_addr, options, ru) as ssize_t
}
2 changes: 2 additions & 0 deletions src/linux/powerpc.rs
Expand Up @@ -13,6 +13,8 @@ pub const O_TRUNC: c_int = 0o00001000;

pub const FIOCLEX: c_uint = 0x20006601;

pub const SIGCHLD: c_ulong = 17;

#[derive(Clone, Copy)]
#[repr(C)]
pub struct stat64 {
Expand Down
2 changes: 2 additions & 0 deletions src/linux/powerpc64.rs
Expand Up @@ -13,6 +13,8 @@ pub const O_TRUNC: c_int = 0o00001000;

pub const FIOCLEX: c_uint = 0x20006601;

pub const SIGCHLD: c_ulong = 17;

#[derive(Clone, Copy)]
#[repr(C)]
pub struct stat64 {
Expand Down
2 changes: 2 additions & 0 deletions src/linux/sparc64.rs
Expand Up @@ -12,3 +12,5 @@ pub const O_DIRECTORY: c_int = 0o00200000;
pub const O_EXCL: c_int = 0x0800;
pub const O_PATH: c_int = 0x1000000;
pub const O_TRUNC: c_int = 0x0400;

pub const SIGCHLD: c_ulong = 17;
6 changes: 6 additions & 0 deletions src/linux/types.rs
Expand Up @@ -10,6 +10,7 @@ pub type clockid_t = __kernel_clockid_t;
pub type loff_t = __kernel_loff_t;
pub type mode_t = __kernel_mode_t;
pub type nlink_t = u32;
pub type pid_t = __kernel_pid_t;
pub type time_t = __kernel_time_t;
pub type umode_t = c_ushort;

Expand All @@ -21,6 +22,7 @@ type __kernel_loff_t = c_longlong;
type __kernel_long_t = c_long;
type __kernel_mode_t = c_uint;
type __kernel_off64_t = c_longlong;
type __kernel_pid_t = c_int;
type __kernel_time_t = __kernel_long_t;
type __kernel_uid_t = c_uint;

Expand All @@ -43,6 +45,10 @@ pub struct linux_dirent64 {
pub d_name: [c_char; 0],
}

pub struct rusage {
_unimplemented: (),
}

// Where from?
pub type blkcnt64_t = i64;
#[cfg(not(any(target_arch = "mips", target_arch = "mips64")))] pub type dev_t = u64;
Expand Down
2 changes: 2 additions & 0 deletions src/linux/x86.rs
Expand Up @@ -13,6 +13,8 @@ pub const O_TRUNC: c_int = 0o00001000;

pub const FIOCLEX: c_uint = 0x5451;

pub const SIGCHLD: c_ulong = 17;

// include/linux/types.h
pub type ino_t = __kernel_ino_t;
// include/uapi/asm-generic/posix_types.h
Expand Down
2 changes: 2 additions & 0 deletions src/linux/x86_64.rs
Expand Up @@ -13,6 +13,8 @@ pub const O_TRUNC: c_int = 0o00001000;

pub const FIOCLEX: c_uint = 0x5451;

pub const SIGCHLD: c_ulong = 17;

pub type blksize_t = i64;

#[derive(Clone, Copy)]
Expand Down

0 comments on commit fea5ca4

Please sign in to comment.