Skip to content

Commit 509da51

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

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/sys/ptrace.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
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+
cfg_if! {
10+
if #[cfg(any(all(target_os = "linux", arch = "s390x"),
11+
all(target_os = "linux", target_env = "gnu")))] {
12+
pub type PtraceRequest = ::libc::c_uint;
13+
} else {
14+
pub type PtraceRequest = c_int;
15+
}
16+
}
1017

1118
pub const PTRACE_TRACEME: PtraceRequest = 0;
1219
pub const PTRACE_PEEKTEXT: PtraceRequest = 1;
@@ -60,14 +67,6 @@ pub mod ptrace {
6067
pub const PTRACE_O_TRACESECCOMP: PtraceOptions = (1 << PTRACE_EVENT_SECCOMP);
6168
}
6269

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-
7170
/// Performs a ptrace request. If the request in question is provided by a specialised function
7271
/// this function will return an unsupported operation error.
7372
pub fn ptrace(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
@@ -83,7 +82,7 @@ pub fn ptrace(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data:
8382
fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
8483
let ret = unsafe {
8584
Errno::clear();
86-
ffi::ptrace(request, pid.into(), addr, data)
85+
libc::ptrace(request, libc::pid_t::from(pid), addr, data)
8786
};
8887
match Errno::result(ret) {
8988
Ok(..) | Err(Error::Sys(Errno::UnknownErrno)) => Ok(ret),
@@ -98,21 +97,21 @@ fn ptrace_peek(request: ptrace::PtraceRequest, pid: Pid, addr: *mut c_void, data
9897
fn ptrace_get_data<T>(request: ptrace::PtraceRequest, pid: Pid) -> Result<T> {
9998
// Creates an uninitialized pointer to store result in
10099
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) };
100+
let res = unsafe { libc::ptrace(request, libc::pid_t::from(pid), ptr::null_mut::<T>(), &data as *const _ as *const c_void) };
102101
Errno::result(res)?;
103102
Ok(data)
104103
}
105104

106105
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)
106+
Errno::result(unsafe { libc::ptrace(request, libc::pid_t::from(pid), addr, data) }).map(|_| 0)
108107
}
109108

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

115-
let res = unsafe { ffi::ptrace(PTRACE_SETOPTIONS, pid.into(), ptr::null_mut(), options as *mut c_void) };
114+
let res = unsafe { libc::ptrace(PTRACE_SETOPTIONS, libc::pid_t::from(pid), ptr::null_mut::<libc::c_void>(), options as *mut c_void) };
116115
Errno::result(res).map(|_| ())
117116
}
118117

@@ -133,7 +132,7 @@ pub fn setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
133132
use self::ptrace::*;
134133
let ret = unsafe{
135134
Errno::clear();
136-
ffi::ptrace(PTRACE_SETSIGINFO, pid.into(), ptr::null_mut(), sig as *const _ as *const c_void)
135+
libc::ptrace(PTRACE_SETSIGINFO, libc::pid_t::from(pid), ptr::null_mut::<libc::c_void>(), sig as *const _ as *const c_void)
137136
};
138137
match Errno::result(ret) {
139138
Ok(_) => Ok(()),

0 commit comments

Comments
 (0)