Skip to content
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

Newtypes for uid_t, gid_t and pid_t. #629

Merged
merged 1 commit into from
Jul 4, 2017
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Changed type signature of `sys::select::FdSet::contains` to make `self`
immutable ([#564](https://github.com/nix-rust/nix/pull/564))
- Changed type of `sched::sched_setaffinity`'s `pid` argument to `pid_t`
- Introduced wrapper types for gid_t, pid_t, and uid_t as Gid, Pid, and Uid
respectively. Various functions have been changed to use these new types as
arguments. ([#629](https://github.com/nix-rust/nix/pull/629))

### Removed
- Removed io::Error from nix::Error and conversion from nix::Error to Errno
Expand Down
11 changes: 6 additions & 5 deletions src/sched.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::mem;
use std::os::unix::io::RawFd;
use std::option::Option;
use libc::{self, c_int, c_void, pid_t};
use libc::{self, c_int, c_void};
use {Errno, Error, Result};
use ::unistd::Pid;

// For some functions taking with a parameter of type CloneFlags,
// only a subset of these flags have an effect.
Expand Down Expand Up @@ -91,9 +92,9 @@ mod ffi {
}
}

pub fn sched_setaffinity(pid: pid_t, cpuset: &CpuSet) -> Result<()> {
pub fn sched_setaffinity(pid: Pid, cpuset: &CpuSet) -> Result<()> {
let res = unsafe {
libc::sched_setaffinity(pid,
libc::sched_setaffinity(pid.into(),
mem::size_of::<CpuSet>() as libc::size_t,
mem::transmute(cpuset))
};
Expand All @@ -105,7 +106,7 @@ pub fn clone(mut cb: CloneCb,
stack: &mut [u8],
flags: CloneFlags,
signal: Option<c_int>)
-> Result<pid_t> {
-> Result<Pid> {
extern "C" fn callback(data: *mut CloneCb) -> c_int {
let cb: &mut CloneCb = unsafe { &mut *data };
(*cb)() as c_int
Expand All @@ -121,7 +122,7 @@ pub fn clone(mut cb: CloneCb,
&mut cb)
};

Errno::result(res)
Errno::result(res).map(Pid::from_raw)
}

pub fn unshare(flags: CloneFlags) -> Result<()> {
Expand Down
25 changes: 13 additions & 12 deletions src/sys/ptrace.rs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
Expand All @@ -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) };
Expand All @@ -109,36 +110,36 @@ 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;

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

/// 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(()),
Expand Down
4 changes: 2 additions & 2 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 14 additions & 13 deletions src/sys/wait.rs
Original file line number Diff line number Diff line change
@@ -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;

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

Expand Down Expand Up @@ -185,15 +186,15 @@ 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) {
WaitStatus::Signaled(pid, status::term_signal(status), status::dumped_core(status))
} 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))
Expand All @@ -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))
}
}
Expand All @@ -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;
Expand All @@ -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)
}
Loading