Skip to content

Commit

Permalink
Use upstream libc ptrace FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
Susurrus committed Aug 10, 2017
1 parent 087aece commit c25b9f6
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/sys/ptrace.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
use std::{mem, ptr};
use {Errno, Error, Result};
use libc::{c_void, c_long, siginfo_t};
use libc::{self, c_void, c_long, siginfo_t};
use ::unistd::Pid;

pub mod ptrace {
use libc::c_int;

pub type PtraceRequest = c_int;
/// The datatype used for the `request` argument to `ptrace`
#[cfg(any(all(target_os = "linux", arch = "s390x"),
all(target_os = "linux", target_env = "gnu")))]
#[doc(hidden)]
pub type ptrace_request_type = ::libc::c_uint;

#[cfg(not(any(all(target_os = "linux", arch = "s390x"),
all(target_os = "linux", target_env = "gnu"))))]
#[doc(hidden)]
pub type ptrace_request_type = ::libc::c_int;

pub type PtraceRequest = ptrace_request_type;

pub const PTRACE_TRACEME: PtraceRequest = 0;
pub const PTRACE_PEEKTEXT: PtraceRequest = 1;
Expand Down Expand Up @@ -60,14 +71,6 @@ pub mod ptrace {
pub const PTRACE_O_TRACESECCOMP: PtraceOptions = (1 << PTRACE_EVENT_SECCOMP);
}

mod ffi {
use libc::{pid_t, c_int, c_long, c_void};

extern {
pub fn ptrace(request: c_int, pid: pid_t, addr: * const c_void, data: * const c_void) -> c_long;
}
}

/// 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, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
Expand All @@ -83,7 +86,7 @@ pub fn ptrace(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data:
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.into(), addr, data)
libc::ptrace(request, Into::<libc::pid_t>::into(pid), addr, data)
};
match Errno::result(ret) {
Ok(..) | Err(Error::Sys(Errno::UnknownErrno)) => Ok(ret),
Expand All @@ -98,21 +101,21 @@ fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data
fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: Pid) -> Result<T> {
// Creates an uninitialized pointer to store result in
let data: T = unsafe { mem::uninitialized() };
let res = unsafe { ffi::ptrace(request, pid.into(), ptr::null_mut(), &data as *const _ as *const c_void) };
let res = unsafe { libc::ptrace(request, Into::<libc::pid_t>::into(pid), ptr::null_mut::<T>(), &data as *const _ as *const c_void) };
Errno::result(res)?;
Ok(data)
}

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)
Errno::result(unsafe { libc::ptrace(request, Into::<libc::pid_t>::into(pid), addr, data) }).map(|_| 0)
}

/// Set options, as with `ptrace(PTRACE_SETOPTIONS,...)`.
pub fn setoptions(pid: Pid, options: ptrace::PtraceOptions) -> Result<()> {
use self::ptrace::*;
use std::ptr;

let res = unsafe { ffi::ptrace(PTRACE_SETOPTIONS, pid.into(), ptr::null_mut(), options as *mut c_void) };
let res = unsafe { libc::ptrace(PTRACE_SETOPTIONS, Into::<libc::pid_t>::into(pid), ptr::null_mut::<libc::c_void>(), options as *mut c_void) };
Errno::result(res).map(|_| ())
}

Expand All @@ -133,7 +136,7 @@ pub fn setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
use self::ptrace::*;
let ret = unsafe{
Errno::clear();
ffi::ptrace(PTRACE_SETSIGINFO, pid.into(), ptr::null_mut(), sig as *const _ as *const c_void)
libc::ptrace(PTRACE_SETSIGINFO, Into::<libc::pid_t>::into(pid), ptr::null_mut::<libc::c_void>(), sig as *const _ as *const c_void)
};
match Errno::result(ret) {
Ok(_) => Ok(()),
Expand Down

0 comments on commit c25b9f6

Please sign in to comment.