Skip to content

Provide fork() + error tweaks #3

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

Merged
merged 1 commit into from
Sep 16, 2014
Merged
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
4 changes: 4 additions & 0 deletions src/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 35 additions & 1 deletion src/unistd.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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
Expand Down Expand Up @@ -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) };
Expand Down