Skip to content

Commit f7a1ce1

Browse files
committed
Newtypes for uid_t, gid_t and pid_t.
Note about newtypes added to changelog. Changed passing by value to passing by reference in Uid::is_root(). Added documentation Improved entry about newtypes in CHANGELOG. Compare self with ROOT constant in `is_root()` method.
1 parent 274b09e commit f7a1ce1

File tree

7 files changed

+203
-77
lines changed

7 files changed

+203
-77
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3232
- Changed type signature of `sys::select::FdSet::contains` to make `self`
3333
immutable ([#564](https://github.com/nix-rust/nix/pull/564))
3434
- Changed type of `sched::sched_setaffinity`'s `pid` argument to `pid_t`
35+
- Introduced wrapper types for gid_t, pid_t, and uid_t as Gid, Pid, and Uid
36+
respectively. Various functions have been changed to use these new types as
37+
arguments. ([#629](https://github.com/nix-rust/nix/pull/629))
3538

3639
### Removed
3740
- Removed io::Error from nix::Error and conversion from nix::Error to Errno

src/sched.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::mem;
22
use std::os::unix::io::RawFd;
33
use std::option::Option;
4-
use libc::{self, c_int, c_void, pid_t};
4+
use libc::{self, c_int, c_void};
55
use {Errno, Error, Result};
6+
use ::unistd::Pid;
67

78
// For some functions taking with a parameter of type CloneFlags,
89
// only a subset of these flags have an effect.
@@ -91,9 +92,9 @@ mod ffi {
9192
}
9293
}
9394

94-
pub fn sched_setaffinity(pid: pid_t, cpuset: &CpuSet) -> Result<()> {
95+
pub fn sched_setaffinity(pid: Pid, cpuset: &CpuSet) -> Result<()> {
9596
let res = unsafe {
96-
libc::sched_setaffinity(pid,
97+
libc::sched_setaffinity(pid.into(),
9798
mem::size_of::<CpuSet>() as libc::size_t,
9899
mem::transmute(cpuset))
99100
};
@@ -105,7 +106,7 @@ pub fn clone(mut cb: CloneCb,
105106
stack: &mut [u8],
106107
flags: CloneFlags,
107108
signal: Option<c_int>)
108-
-> Result<pid_t> {
109+
-> Result<Pid> {
109110
extern "C" fn callback(data: *mut CloneCb) -> c_int {
110111
let cb: &mut CloneCb = unsafe { &mut *data };
111112
(*cb)() as c_int
@@ -121,7 +122,7 @@ pub fn clone(mut cb: CloneCb,
121122
&mut cb)
122123
};
123124

124-
Errno::result(res)
125+
Errno::result(res).map(Pid::from_raw)
125126
}
126127

127128
pub fn unshare(flags: CloneFlags) -> Result<()> {

src/sys/ptrace.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{mem, ptr};
22
use {Errno, Error, Result};
3-
use libc::{pid_t, c_void, c_long, siginfo_t};
3+
use libc::{c_void, c_long, siginfo_t};
4+
use ::unistd::Pid;
45

56
#[cfg(all(target_os = "linux",
67
any(target_arch = "x86",
@@ -74,7 +75,7 @@ mod ffi {
7475

7576
/// Performs a ptrace request. If the request in question is provided by a specialised function
7677
/// this function will return an unsupported operation error.
77-
pub fn ptrace(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
78+
pub fn ptrace(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
7879
use self::ptrace::*;
7980

8081
match request {
@@ -84,10 +85,10 @@ pub fn ptrace(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, dat
8485
}
8586
}
8687

87-
fn ptrace_peek(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
88+
fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
8889
let ret = unsafe {
8990
Errno::clear();
90-
ffi::ptrace(request, pid, addr, data)
91+
ffi::ptrace(request, pid.into(), addr, data)
9192
};
9293
match Errno::result(ret) {
9394
Ok(..) | Err(Error::Sys(Errno::UnknownErrno)) => Ok(ret),
@@ -99,7 +100,7 @@ fn ptrace_peek(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, da
99100
/// Some ptrace get requests populate structs or larger elements than c_long
100101
/// and therefore use the data field to return values. This function handles these
101102
/// requests.
102-
fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: pid_t) -> Result<T> {
103+
fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: Pid) -> Result<T> {
103104
// Creates an uninitialized pointer to store result in
104105
let data: Box<T> = Box::new(unsafe { mem::uninitialized() });
105106
let data: *mut c_void = unsafe { mem::transmute(data) };
@@ -109,36 +110,36 @@ fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: pid_t) -> Result<T> {
109110
Ok(*data)
110111
}
111112

112-
fn ptrace_other(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
113-
Errno::result(unsafe { ffi::ptrace(request, pid, addr, data) }).map(|_| 0)
113+
fn ptrace_other(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
114+
Errno::result(unsafe { ffi::ptrace(request, pid.into(), addr, data) }).map(|_| 0)
114115
}
115116

116117
/// Set options, as with `ptrace(PTRACE_SETOPTIONS,...)`.
117-
pub fn ptrace_setoptions(pid: pid_t, options: ptrace::PtraceOptions) -> Result<()> {
118+
pub fn ptrace_setoptions(pid: Pid, options: ptrace::PtraceOptions) -> Result<()> {
118119
use self::ptrace::*;
119120
use std::ptr;
120121

121122
ptrace(PTRACE_SETOPTIONS, pid, ptr::null_mut(), options as *mut c_void).map(drop)
122123
}
123124

124125
/// Gets a ptrace event as described by `ptrace(PTRACE_GETEVENTMSG,...)`
125-
pub fn ptrace_getevent(pid: pid_t) -> Result<c_long> {
126+
pub fn ptrace_getevent(pid: Pid) -> Result<c_long> {
126127
use self::ptrace::*;
127128
ptrace_get_data::<c_long>(PTRACE_GETEVENTMSG, pid)
128129
}
129130

130131
/// Get siginfo as with `ptrace(PTRACE_GETSIGINFO,...)`
131-
pub fn ptrace_getsiginfo(pid: pid_t) -> Result<siginfo_t> {
132+
pub fn ptrace_getsiginfo(pid: Pid) -> Result<siginfo_t> {
132133
use self::ptrace::*;
133134
ptrace_get_data::<siginfo_t>(PTRACE_GETSIGINFO, pid)
134135
}
135136

136137
/// Set siginfo as with `ptrace(PTRACE_SETSIGINFO,...)`
137-
pub fn ptrace_setsiginfo(pid: pid_t, sig: &siginfo_t) -> Result<()> {
138+
pub fn ptrace_setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
138139
use self::ptrace::*;
139140
let ret = unsafe{
140141
Errno::clear();
141-
ffi::ptrace(PTRACE_SETSIGINFO, pid, ptr::null_mut(), sig as *const _ as *const c_void)
142+
ffi::ptrace(PTRACE_SETSIGINFO, pid.into(), ptr::null_mut(), sig as *const _ as *const c_void)
142143
};
143144
match Errno::result(ret) {
144145
Ok(_) => Ok(()),

src/sys/signal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ pub fn pthread_sigmask(how: SigmaskHow,
409409
Errno::result(res).map(drop)
410410
}
411411

412-
pub fn kill<T: Into<Option<Signal>>>(pid: libc::pid_t, signal: T) -> Result<()> {
413-
let res = unsafe { libc::kill(pid,
412+
pub fn kill<T: Into<Option<Signal>>>(pid: ::unistd::Pid, signal: T) -> Result<()> {
413+
let res = unsafe { libc::kill(pid.into(),
414414
match signal.into() {
415415
Some(s) => s as libc::c_int,
416416
None => 0,

src/sys/wait.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use libc::{self, pid_t, c_int};
1+
use libc::{self, c_int};
22
use {Errno, Result};
3+
use unistd::Pid;
34

45
use sys::signal::Signal;
56

@@ -41,12 +42,12 @@ const WSTOPPED: WaitPidFlag = WUNTRACED;
4142

4243
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
4344
pub enum WaitStatus {
44-
Exited(pid_t, i8),
45-
Signaled(pid_t, Signal, bool),
46-
Stopped(pid_t, Signal),
45+
Exited(Pid, i8),
46+
Signaled(Pid, Signal, bool),
47+
Stopped(Pid, Signal),
4748
#[cfg(any(target_os = "linux", target_os = "android"))]
48-
PtraceEvent(pid_t, Signal, c_int),
49-
Continued(pid_t),
49+
PtraceEvent(Pid, Signal, c_int),
50+
Continued(Pid),
5051
StillAlive
5152
}
5253

@@ -185,15 +186,15 @@ mod status {
185186
}
186187
}
187188

188-
fn decode(pid : pid_t, status: i32) -> WaitStatus {
189+
fn decode(pid : Pid, status: i32) -> WaitStatus {
189190
if status::exited(status) {
190191
WaitStatus::Exited(pid, status::exit_status(status))
191192
} else if status::signaled(status) {
192193
WaitStatus::Signaled(pid, status::term_signal(status), status::dumped_core(status))
193194
} else if status::stopped(status) {
194195
cfg_if! {
195196
if #[cfg(any(target_os = "linux", target_os = "android"))] {
196-
fn decode_stopped(pid: pid_t, status: i32) -> WaitStatus {
197+
fn decode_stopped(pid: Pid, status: i32) -> WaitStatus {
197198
let status_additional = status::stop_additional(status);
198199
if status_additional == 0 {
199200
WaitStatus::Stopped(pid, status::stop_signal(status))
@@ -202,7 +203,7 @@ fn decode(pid : pid_t, status: i32) -> WaitStatus {
202203
}
203204
}
204205
} else {
205-
fn decode_stopped(pid: pid_t, status: i32) -> WaitStatus {
206+
fn decode_stopped(pid: Pid, status: i32) -> WaitStatus {
206207
WaitStatus::Stopped(pid, status::stop_signal(status))
207208
}
208209
}
@@ -214,7 +215,7 @@ fn decode(pid : pid_t, status: i32) -> WaitStatus {
214215
}
215216
}
216217

217-
pub fn waitpid(pid: pid_t, options: Option<WaitPidFlag>) -> Result<WaitStatus> {
218+
pub fn waitpid<P: Into<Option<Pid>>>(pid: P, options: Option<WaitPidFlag>) -> Result<WaitStatus> {
218219
use self::WaitStatus::*;
219220

220221
let mut status: i32 = 0;
@@ -224,14 +225,14 @@ pub fn waitpid(pid: pid_t, options: Option<WaitPidFlag>) -> Result<WaitStatus> {
224225
None => 0
225226
};
226227

227-
let res = unsafe { ffi::waitpid(pid as pid_t, &mut status as *mut c_int, option_bits) };
228+
let res = unsafe { ffi::waitpid(pid.into().unwrap_or(Pid::from_raw(-1)).into(), &mut status as *mut c_int, option_bits) };
228229

229230
Ok(match try!(Errno::result(res)) {
230231
0 => StillAlive,
231-
res => decode(res, status),
232+
res => decode(Pid::from_raw(res), status),
232233
})
233234
}
234235

235236
pub fn wait() -> Result<WaitStatus> {
236-
waitpid(-1, None)
237+
waitpid(None, None)
237238
}

0 commit comments

Comments
 (0)