summaryrefslogtreecommitdiff
path: root/src/unistd.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/unistd.rs')
-rw-r--r--src/unistd.rs36
1 files changed, 35 insertions, 1 deletions
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) };