summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2014-09-16 00:28:33 -0700
committerCarl Lerche <me@carllerche.com>2014-09-16 00:28:33 -0700
commitb0850134c535c12802827e5d538c1ca3f31aae06 (patch)
treec5a698cae9fb8b2d2282f80ffb20414bff3cb51e
parent027e6a162a1c9d8fe0c5fdf086e7dfbe699e2330 (diff)
downloadnix-b0850134c535c12802827e5d538c1ca3f31aae06.zip
Provide fork() + error tweaks
-rw-r--r--src/errno.rs4
-rw-r--r--src/unistd.rs36
2 files changed, 39 insertions, 1 deletions
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<Fork> {
+ 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<Fd> {
let res = unsafe { ffi::dup(oldfd) };