Skip to content

Commit ff4484d

Browse files
committed
Auto merge of #449 - posborne:documentation-improvements, r=kamalmarhubi
Documentation improvements This series of commits starts to fill in documentation that was not previously present for various calls, mostly in unistd for this pass. r? @kamalmarhubi
2 parents b05c1e4 + fdf8ab6 commit ff4484d

File tree

2 files changed

+128
-7
lines changed

2 files changed

+128
-7
lines changed

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,40 @@ use std::fmt;
7777
use std::error;
7878
use libc::PATH_MAX;
7979

80+
/// Nix Result Type
8081
pub type Result<T> = result::Result<T, Error>;
8182

83+
/// Nix Error Type
84+
///
85+
/// The nix error type provides a common way of dealing with
86+
/// various system system/libc calls that might fail. Each
87+
/// error has a corresponding errno (usually the one from the
88+
/// underlying OS) to which it can be mapped in addition to
89+
/// implementing other common traits.
8290
#[derive(Clone, Copy, Debug, PartialEq)]
8391
pub enum Error {
8492
Sys(errno::Errno),
8593
InvalidPath,
8694
}
8795

8896
impl Error {
97+
98+
/// Create a nix Error from a given errno
8999
pub fn from_errno(errno: errno::Errno) -> Error {
90100
Error::Sys(errno)
91101
}
92102

103+
/// Get the current errno and convert it to a nix Error
93104
pub fn last() -> Error {
94105
Error::Sys(errno::Errno::last())
95106
}
96107

108+
/// Create a new invalid argument error (`EINVAL`)
97109
pub fn invalid_argument() -> Error {
98110
Error::Sys(errno::EINVAL)
99111
}
100112

113+
/// Get the errno associated with this error
101114
pub fn errno(&self) -> errno::Errno {
102115
match *self {
103116
Error::Sys(errno) => errno,

src/unistd.rs

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ use sys::stat::Mode;
1515
#[cfg(any(target_os = "linux", target_os = "android"))]
1616
pub use self::linux::*;
1717

18+
/// Represents the successful result of calling `fork`
19+
///
20+
/// When `fork` is called, the process continues execution in the parent process
21+
/// and in the new child. This return type can be examined to determine whether
22+
/// you are now executing in the parent process or in the child.
1823
#[derive(Clone, Copy)]
1924
pub enum ForkResult {
20-
Parent {
21-
child: pid_t
22-
},
23-
Child
25+
Parent { child: pid_t },
26+
Child,
2427
}
2528

2629
impl ForkResult {
30+
31+
/// Return `true` if this is the child process of the `fork()`
2732
#[inline]
2833
pub fn is_child(&self) -> bool {
2934
match *self {
@@ -32,31 +37,80 @@ impl ForkResult {
3237
}
3338
}
3439

40+
/// Returns `true` if this is the parent process of the `fork()`
3541
#[inline]
3642
pub fn is_parent(&self) -> bool {
3743
!self.is_child()
3844
}
3945
}
4046

47+
/// Create a new child process duplicating the parent process ([see
48+
/// fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html)).
49+
///
50+
/// After calling the fork system call (successfully) two processes will
51+
/// be created that are identical with the exception of their pid and the
52+
/// return value of this function. As an example:
53+
///
54+
/// ```no_run
55+
/// use nix::unistd::{fork, ForkResult};
56+
///
57+
/// match fork() {
58+
/// Ok(ForkResult::Parent { child, .. }) => {
59+
/// println!("Continuing execution in parent process, new child has pid: {}", child);
60+
/// }
61+
/// Ok(ForkResult::Child) => println!("I'm a new child process"),
62+
/// Err(e) => println!("Fork failed"),
63+
/// }
64+
/// ```
65+
///
66+
/// This will print something like the following (order indeterministic). The
67+
/// thing to note is that you end up with two processes continuing execution
68+
/// immediately after the fork call but with different match arms.
69+
///
70+
/// ```text
71+
/// Continuing execution in parent process, new child has pid: 1234
72+
/// I'm a new child process
73+
/// ```
4174
#[inline]
4275
pub fn fork() -> Result<ForkResult> {
4376
use self::ForkResult::*;
4477
let res = unsafe { libc::fork() };
4578

4679
Errno::result(res).map(|res| match res {
4780
0 => Child,
48-
res => Parent { child: res }
81+
res => Parent { child: res },
4982
})
5083
}
5184

85+
/// Get the pid of this process (see
86+
/// [getpid(2)](http://man7.org/linux/man-pages/man2/getpid.2.html)).
87+
///
88+
/// Since you are running code, there is always a pid to return, so there
89+
/// is no error case that needs to be handled.
5290
#[inline]
5391
pub fn getpid() -> pid_t {
54-
unsafe { libc::getpid() } // no error handling, according to man page: "These functions are always successful."
92+
unsafe { libc::getpid() }
5593
}
94+
95+
/// Get the pid of this processes' parent (see
96+
/// [getpid(2)](http://man7.org/linux/man-pages/man2/getpid.2.html)).
97+
///
98+
/// There is always a parent pid to return, so there is no error case that needs
99+
/// to be handled.
56100
#[inline]
57101
pub fn getppid() -> pid_t {
58102
unsafe { libc::getppid() } // no error handling, according to man page: "These functions are always successful."
59103
}
104+
105+
/// Set a process group ID (see
106+
/// [setpgid(2)](http://man7.org/linux/man-pages/man2/setpgid.2.html)).
107+
///
108+
/// Set the process group id (PGID) of a particular process. If a pid of zero
109+
/// is specified, then the pid of the calling process is used. Process groups
110+
/// may be used to group together a set of processes in order for the OS to
111+
/// apply some operations across the group.
112+
///
113+
/// `setsid()` may be used to create a new process group.
60114
#[inline]
61115
pub fn setpgid(pid: pid_t, pgid: pid_t) -> Result<()> {
62116
let res = unsafe { libc::setpgid(pid, pgid) };
@@ -68,26 +122,64 @@ pub fn getpgid(pid: Option<pid_t>) -> Result<pid_t> {
68122
Errno::result(res)
69123
}
70124

125+
/// Create new session and set process group id (see
126+
/// [setsid(2)](http://man7.org/linux/man-pages/man2/setsid.2.html)).
127+
#[inline]
128+
pub fn setsid() -> Result<pid_t> {
129+
Errno::result(unsafe { libc::setsid() })
130+
}
131+
132+
/// Get the caller's thread ID (see
133+
/// [gettid(2)](http://man7.org/linux/man-pages/man2/gettid.2.html).
134+
///
135+
/// This function is only available on Linux based systems. In a single
136+
/// threaded process, the main thread will have the same ID as the process. In
137+
/// a multithreaded process, each thread will have a unique thread id but the
138+
/// same process ID.
139+
///
140+
/// No error handling is required as a thread id should always exist for any
141+
/// process, even if threads are not being used.
71142
#[cfg(any(target_os = "linux", target_os = "android"))]
72143
#[inline]
73144
pub fn gettid() -> pid_t {
74-
unsafe { libc::syscall(libc::SYS_gettid) as pid_t } // no error handling, according to man page: "These functions are always successful."
145+
unsafe { libc::syscall(libc::SYS_gettid) as pid_t }
75146
}
76147

148+
/// Create a copy of the specified file descriptor (see
149+
/// [dup(2)](http://man7.org/linux/man-pages/man2/dup.2.html)).
150+
///
151+
/// The new file descriptor will be have a new index but refer to the same
152+
/// resource as the old file descriptor and the old and new file descriptors may
153+
/// be used interchangeably. The new and old file descriptor share the same
154+
/// underlying resource, offset, and file status flags. The actual index used
155+
/// for the file descriptor will be the lowest fd index that is available.
156+
///
157+
/// The two file descriptors do not share file descriptor flags (e.g. `FD_CLOEXEC`).
77158
#[inline]
78159
pub fn dup(oldfd: RawFd) -> Result<RawFd> {
79160
let res = unsafe { libc::dup(oldfd) };
80161

81162
Errno::result(res)
82163
}
83164

165+
/// Create a copy of the specified file descriptor using the specified fd (see
166+
/// [dup(2)](http://man7.org/linux/man-pages/man2/dup.2.html)).
167+
///
168+
/// This function behaves similar to `dup()` except that it will try to use the
169+
/// specified fd instead of allocating a new one. See the man pages for more
170+
/// detail on the exact behavior of this function.
84171
#[inline]
85172
pub fn dup2(oldfd: RawFd, newfd: RawFd) -> Result<RawFd> {
86173
let res = unsafe { libc::dup2(oldfd, newfd) };
87174

88175
Errno::result(res)
89176
}
90177

178+
/// Create a new copy of the specified file descriptor using the specified fd
179+
/// and flags (see [dup(2)](http://man7.org/linux/man-pages/man2/dup.2.html)).
180+
///
181+
/// This function behaves similar to `dup2()` but allows for flags to be
182+
/// specified.
91183
pub fn dup3(oldfd: RawFd, newfd: RawFd, flags: OFlag) -> Result<RawFd> {
92184
dup3_polyfill(oldfd, newfd, flags)
93185
}
@@ -110,6 +202,11 @@ fn dup3_polyfill(oldfd: RawFd, newfd: RawFd, flags: OFlag) -> Result<RawFd> {
110202
Ok(fd)
111203
}
112204

205+
/// Change the current working directory of the calling process (see
206+
/// [chdir(2)](http://man7.org/linux/man-pages/man2/chdir.2.html)).
207+
///
208+
/// This function may fail in a number of different scenarios. See the man
209+
/// pages for additional details on possible failure cases.
113210
#[inline]
114211
pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> {
115212
let res = try!(path.with_nix_path(|cstr| {
@@ -213,6 +310,17 @@ pub fn getcwd() -> Result<PathBuf> {
213310
}
214311
}
215312

313+
/// Change the ownership of the file at `path` to be owned by the specified
314+
/// `owner` (user) and `group` (see
315+
/// [chown(2)](http://man7.org/linux/man-pages/man2/lchown.2.html)).
316+
///
317+
/// The owner/group for the provided path name will not be modified if `None` is
318+
/// provided for that argument. Ownership change will be attempted for the path
319+
/// only if `Some` owner/group is provided.
320+
///
321+
/// This call may fail under a number of different situations. See [the man
322+
/// pages](http://man7.org/linux/man-pages/man2/lchown.2.html#ERRORS) for
323+
/// additional details.
216324
#[inline]
217325
pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<uid_t>, group: Option<gid_t>) -> Result<()> {
218326
let res = try!(path.with_nix_path(|cstr| {

0 commit comments

Comments
 (0)