summaryrefslogtreecommitdiff
path: root/src/sys
diff options
context:
space:
mode:
authorAlan Somers <asomers@gmail.com>2017-07-04 10:36:24 -0600
committerGitHub <noreply@github.com>2017-07-04 10:36:24 -0600
commit9e51647095af4612870c0e4582b739c8147784dc (patch)
tree15ea8a126502b4a70ef08a28638d3fed1ec91b47 /src/sys
parente12ff7725e6911ad3ed2112bbc21a21643ee40da (diff)
parentb62ab099c8aa3ae59a9298fee342cd316f14a8b1 (diff)
downloadnix-9e51647095af4612870c0e4582b739c8147784dc.zip
Merge pull request #629 from Kixunil/newtypes
Newtypes for uid_t, gid_t and pid_t.
Diffstat (limited to 'src/sys')
-rw-r--r--src/sys/ptrace.rs25
-rw-r--r--src/sys/signal.rs4
-rw-r--r--src/sys/wait.rs27
3 files changed, 29 insertions, 27 deletions
diff --git a/src/sys/ptrace.rs b/src/sys/ptrace.rs
index bf6ee8bb..17dfee34 100644
--- a/src/sys/ptrace.rs
+++ b/src/sys/ptrace.rs
@@ -1,6 +1,7 @@
use std::{mem, ptr};
use {Errno, Error, Result};
-use libc::{pid_t, c_void, c_long, siginfo_t};
+use libc::{c_void, c_long, siginfo_t};
+use ::unistd::Pid;
#[cfg(all(target_os = "linux",
any(target_arch = "x86",
@@ -74,7 +75,7 @@ mod ffi {
/// Performs a ptrace request. If the request in question is provided by a specialised function
/// this function will return an unsupported operation error.
-pub fn ptrace(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
+pub fn ptrace(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
use self::ptrace::*;
match request {
@@ -84,10 +85,10 @@ pub fn ptrace(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, dat
}
}
-fn ptrace_peek(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
+fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
let ret = unsafe {
Errno::clear();
- ffi::ptrace(request, pid, addr, data)
+ ffi::ptrace(request, pid.into(), addr, data)
};
match Errno::result(ret) {
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
/// Some ptrace get requests populate structs or larger elements than c_long
/// and therefore use the data field to return values. This function handles these
/// requests.
-fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: pid_t) -> Result<T> {
+fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: Pid) -> Result<T> {
// Creates an uninitialized pointer to store result in
let data: Box<T> = Box::new(unsafe { mem::uninitialized() });
let data: *mut c_void = unsafe { mem::transmute(data) };
@@ -109,12 +110,12 @@ fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: pid_t) -> Result<T> {
Ok(*data)
}
-fn ptrace_other(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
- Errno::result(unsafe { ffi::ptrace(request, pid, addr, data) }).map(|_| 0)
+fn ptrace_other(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
+ Errno::result(unsafe { ffi::ptrace(request, pid.into(), addr, data) }).map(|_| 0)
}
/// Set options, as with `ptrace(PTRACE_SETOPTIONS,...)`.
-pub fn ptrace_setoptions(pid: pid_t, options: ptrace::PtraceOptions) -> Result<()> {
+pub fn ptrace_setoptions(pid: Pid, options: ptrace::PtraceOptions) -> Result<()> {
use self::ptrace::*;
use std::ptr;
@@ -122,23 +123,23 @@ pub fn ptrace_setoptions(pid: pid_t, options: ptrace::PtraceOptions) -> Result<(
}
/// Gets a ptrace event as described by `ptrace(PTRACE_GETEVENTMSG,...)`
-pub fn ptrace_getevent(pid: pid_t) -> Result<c_long> {
+pub fn ptrace_getevent(pid: Pid) -> Result<c_long> {
use self::ptrace::*;
ptrace_get_data::<c_long>(PTRACE_GETEVENTMSG, pid)
}
/// Get siginfo as with `ptrace(PTRACE_GETSIGINFO,...)`
-pub fn ptrace_getsiginfo(pid: pid_t) -> Result<siginfo_t> {
+pub fn ptrace_getsiginfo(pid: Pid) -> Result<siginfo_t> {
use self::ptrace::*;
ptrace_get_data::<siginfo_t>(PTRACE_GETSIGINFO, pid)
}
/// Set siginfo as with `ptrace(PTRACE_SETSIGINFO,...)`
-pub fn ptrace_setsiginfo(pid: pid_t, sig: &siginfo_t) -> Result<()> {
+pub fn ptrace_setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
use self::ptrace::*;
let ret = unsafe{
Errno::clear();
- ffi::ptrace(PTRACE_SETSIGINFO, pid, ptr::null_mut(), sig as *const _ as *const c_void)
+ ffi::ptrace(PTRACE_SETSIGINFO, pid.into(), ptr::null_mut(), sig as *const _ as *const c_void)
};
match Errno::result(ret) {
Ok(_) => Ok(()),
diff --git a/src/sys/signal.rs b/src/sys/signal.rs
index 887c24f3..e7978052 100644
--- a/src/sys/signal.rs
+++ b/src/sys/signal.rs
@@ -409,8 +409,8 @@ pub fn pthread_sigmask(how: SigmaskHow,
Errno::result(res).map(drop)
}
-pub fn kill<T: Into<Option<Signal>>>(pid: libc::pid_t, signal: T) -> Result<()> {
- let res = unsafe { libc::kill(pid,
+pub fn kill<T: Into<Option<Signal>>>(pid: ::unistd::Pid, signal: T) -> Result<()> {
+ let res = unsafe { libc::kill(pid.into(),
match signal.into() {
Some(s) => s as libc::c_int,
None => 0,
diff --git a/src/sys/wait.rs b/src/sys/wait.rs
index 35b94790..ee0beade 100644
--- a/src/sys/wait.rs
+++ b/src/sys/wait.rs
@@ -1,5 +1,6 @@
-use libc::{self, pid_t, c_int};
+use libc::{self, c_int};
use {Errno, Result};
+use unistd::Pid;
use sys::signal::Signal;
@@ -41,12 +42,12 @@ const WSTOPPED: WaitPidFlag = WUNTRACED;
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
pub enum WaitStatus {
- Exited(pid_t, i8),
- Signaled(pid_t, Signal, bool),
- Stopped(pid_t, Signal),
+ Exited(Pid, i8),
+ Signaled(Pid, Signal, bool),
+ Stopped(Pid, Signal),
#[cfg(any(target_os = "linux", target_os = "android"))]
- PtraceEvent(pid_t, Signal, c_int),
- Continued(pid_t),
+ PtraceEvent(Pid, Signal, c_int),
+ Continued(Pid),
StillAlive
}
@@ -185,7 +186,7 @@ mod status {
}
}
-fn decode(pid : pid_t, status: i32) -> WaitStatus {
+fn decode(pid : Pid, status: i32) -> WaitStatus {
if status::exited(status) {
WaitStatus::Exited(pid, status::exit_status(status))
} else if status::signaled(status) {
@@ -193,7 +194,7 @@ fn decode(pid : pid_t, status: i32) -> WaitStatus {
} else if status::stopped(status) {
cfg_if! {
if #[cfg(any(target_os = "linux", target_os = "android"))] {
- fn decode_stopped(pid: pid_t, status: i32) -> WaitStatus {
+ fn decode_stopped(pid: Pid, status: i32) -> WaitStatus {
let status_additional = status::stop_additional(status);
if status_additional == 0 {
WaitStatus::Stopped(pid, status::stop_signal(status))
@@ -202,7 +203,7 @@ fn decode(pid : pid_t, status: i32) -> WaitStatus {
}
}
} else {
- fn decode_stopped(pid: pid_t, status: i32) -> WaitStatus {
+ fn decode_stopped(pid: Pid, status: i32) -> WaitStatus {
WaitStatus::Stopped(pid, status::stop_signal(status))
}
}
@@ -214,7 +215,7 @@ fn decode(pid : pid_t, status: i32) -> WaitStatus {
}
}
-pub fn waitpid(pid: pid_t, options: Option<WaitPidFlag>) -> Result<WaitStatus> {
+pub fn waitpid<P: Into<Option<Pid>>>(pid: P, options: Option<WaitPidFlag>) -> Result<WaitStatus> {
use self::WaitStatus::*;
let mut status: i32 = 0;
@@ -224,14 +225,14 @@ pub fn waitpid(pid: pid_t, options: Option<WaitPidFlag>) -> Result<WaitStatus> {
None => 0
};
- let res = unsafe { ffi::waitpid(pid as pid_t, &mut status as *mut c_int, option_bits) };
+ let res = unsafe { ffi::waitpid(pid.into().unwrap_or(Pid::from_raw(-1)).into(), &mut status as *mut c_int, option_bits) };
Ok(match try!(Errno::result(res)) {
0 => StillAlive,
- res => decode(res, status),
+ res => decode(Pid::from_raw(res), status),
})
}
pub fn wait() -> Result<WaitStatus> {
- waitpid(-1, None)
+ waitpid(None, None)
}