Skip to content

Commit b085013

Browse files
committed
Provide fork() + error tweaks
1 parent 027e6a1 commit b085013

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/errno.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ impl SysError {
2424
SysError { kind: kind }
2525
}
2626

27+
pub fn errno(&self) -> uint {
28+
self.kind as uint
29+
}
30+
2731
pub fn desc(&self) -> &'static str {
2832
match self.kind {
2933
UnknownErrno => "Unknown errno",

src/unistd.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{mem, ptr};
22
use std::c_str::{CString, ToCStr};
3-
use libc::{c_char, c_void, c_int, size_t};
3+
use libc::{c_char, c_void, c_int, size_t, pid_t};
44
use fcntl::{fcntl, Fd, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC, F_SETFD, F_SETFL};
55
use errno::{SysResult, SysError, from_ffi};
66

@@ -10,6 +10,7 @@ pub use self::linux::*;
1010
mod ffi {
1111
use libc::{c_char, c_int, size_t};
1212
pub use libc::{close, read, write, pipe};
13+
pub use libc::funcs::posix88::unistd::fork;
1314

1415
extern {
1516
// duplicate a file descriptor
@@ -42,6 +43,39 @@ mod ffi {
4243
}
4344
}
4445

46+
pub enum Fork {
47+
Parent(pid_t),
48+
Child
49+
}
50+
51+
impl Fork {
52+
pub fn is_child(&self) -> bool {
53+
match *self {
54+
Child => true,
55+
_ => false
56+
}
57+
}
58+
59+
pub fn is_parent(&self) -> bool {
60+
match *self {
61+
Parent(_) => true,
62+
_ => false
63+
}
64+
}
65+
}
66+
67+
pub fn fork() -> SysResult<Fork> {
68+
let res = unsafe { ffi::fork() };
69+
70+
if res < 0 {
71+
return Err(SysError::last());
72+
} else if res == 0 {
73+
Ok(Child)
74+
} else {
75+
Ok(Parent(res))
76+
}
77+
}
78+
4579
#[inline]
4680
pub fn dup(oldfd: Fd) -> SysResult<Fd> {
4781
let res = unsafe { ffi::dup(oldfd) };

0 commit comments

Comments
 (0)