Skip to content

Commit c25b9f6

Browse files
committed
Use upstream libc ptrace FFI
1 parent 087aece commit c25b9f6

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/sys/ptrace.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
use std::{mem, ptr};
22
use {Errno, Error, Result};
3-
use libc::{c_void, c_long, siginfo_t};
3+
use libc::{self, c_void, c_long, siginfo_t};
44
use ::unistd::Pid;
55

66
pub mod ptrace {
77
use libc::c_int;
88

9-
pub type PtraceRequest = c_int;
9+
/// The datatype used for the `request` argument to `ptrace`
10+
#[cfg(any(all(target_os = "linux", arch = "s390x"),
11+
all(target_os = "linux", target_env = "gnu")))]
12+
#[doc(hidden)]
13+
pub type ptrace_request_type = ::libc::c_uint;
14+
15+
#[cfg(not(any(all(target_os = "linux", arch = "s390x"),
16+
all(target_os = "linux", target_env = "gnu"))))]
17+
#[doc(hidden)]
18+
pub type ptrace_request_type = ::libc::c_int;
19+
20+
pub type PtraceRequest = ptrace_request_type;
1021

1122
pub const PTRACE_TRACEME: PtraceRequest = 0;
1223
pub const PTRACE_PEEKTEXT: PtraceRequest = 1;
@@ -60,14 +71,6 @@ pub mod ptrace {
6071
pub const PTRACE_O_TRACESECCOMP: PtraceOptions = (1 << PTRACE_EVENT_SECCOMP);
6172
}
6273

63-
mod ffi {
64-
use libc::{pid_t, c_int, c_long, c_void};
65-
66-
extern {
67-
pub fn ptrace(request: c_int, pid: pid_t, addr: * const c_void, data: * const c_void) -> c_long;
68-
}
69-
}
70-
7174
/// Performs a ptrace request. If the request in question is provided by a specialised function
7275
/// this function will return an unsupported operation error.
7376
pub fn ptrace(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
@@ -83,7 +86,7 @@ pub fn ptrace(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data:
8386
fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
8487
let ret = unsafe {
8588
Errno::clear();
86-
ffi::ptrace(request, pid.into(), addr, data)
89+
libc::ptrace(request, Into::<libc::pid_t>::into(pid), addr, data)
8790
};
8891
match Errno::result(ret) {
8992
Ok(..) | Err(Error::Sys(Errno::UnknownErrno)) => Ok(ret),
@@ -98,21 +101,21 @@ fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data
98101
fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: Pid) -> Result<T> {
99102
// Creates an uninitialized pointer to store result in
100103
let data: T = unsafe { mem::uninitialized() };
101-
let res = unsafe { ffi::ptrace(request, pid.into(), ptr::null_mut(), &data as *const _ as *const c_void) };
104+
let res = unsafe { libc::ptrace(request, Into::<libc::pid_t>::into(pid), ptr::null_mut::<T>(), &data as *const _ as *const c_void) };
102105
Errno::result(res)?;
103106
Ok(data)
104107
}
105108

106109
fn ptrace_other(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
107-
Errno::result(unsafe { ffi::ptrace(request, pid.into(), addr, data) }).map(|_| 0)
110+
Errno::result(unsafe { libc::ptrace(request, Into::<libc::pid_t>::into(pid), addr, data) }).map(|_| 0)
108111
}
109112

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

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

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

0 commit comments

Comments
 (0)