From b0850134c535c12802827e5d538c1ca3f31aae06 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 16 Sep 2014 00:28:33 -0700 Subject: Provide fork() + error tweaks --- src/errno.rs | 4 ++++ src/unistd.rs | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/errno.rs b/src/errno.rs index 2591ab17..ebc1852a 100644 --- a/src/errno.rs +++ b/src/errno.rs @@ -24,6 +24,10 @@ impl SysError { SysError { kind: kind } } + pub fn errno(&self) -> uint { + self.kind as uint + } + pub fn desc(&self) -> &'static str { match self.kind { UnknownErrno => "Unknown errno", diff --git a/src/unistd.rs b/src/unistd.rs index 69f34556..7fbb0d54 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -1,6 +1,6 @@ use std::{mem, ptr}; use std::c_str::{CString, ToCStr}; -use libc::{c_char, c_void, c_int, size_t}; +use libc::{c_char, c_void, c_int, size_t, pid_t}; use fcntl::{fcntl, Fd, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC, F_SETFD, F_SETFL}; use errno::{SysResult, SysError, from_ffi}; @@ -10,6 +10,7 @@ pub use self::linux::*; mod ffi { use libc::{c_char, c_int, size_t}; pub use libc::{close, read, write, pipe}; + pub use libc::funcs::posix88::unistd::fork; extern { // duplicate a file descriptor @@ -42,6 +43,39 @@ mod ffi { } } +pub enum Fork { + Parent(pid_t), + Child +} + +impl Fork { + pub fn is_child(&self) -> bool { + match *self { + Child => true, + _ => false + } + } + + pub fn is_parent(&self) -> bool { + match *self { + Parent(_) => true, + _ => false + } + } +} + +pub fn fork() -> SysResult { + let res = unsafe { ffi::fork() }; + + if res < 0 { + return Err(SysError::last()); + } else if res == 0 { + Ok(Child) + } else { + Ok(Parent(res)) + } +} + #[inline] pub fn dup(oldfd: Fd) -> SysResult { let res = unsafe { ffi::dup(oldfd) }; -- cgit v1.2.3