Skip to content

Commit c65d7eb

Browse files
committed
ptrace: implement getsigmask and setsigmask
1 parent 9d7d122 commit c65d7eb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/sys/ptrace/linux.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ libc_enum! {
126126
PTRACE_SYSEMU_SINGLESTEP,
127127
#[cfg(all(target_os = "linux", target_env = "gnu"))]
128128
PTRACE_GET_SYSCALL_INFO,
129+
PTRACE_GETSIGMASK,
130+
PTRACE_SETSIGMASK,
129131
}
130132
}
131133

@@ -325,7 +327,7 @@ fn ptrace_get_data<T>(request: Request, pid: Pid) -> Result<T> {
325327
libc::ptrace(
326328
request as RequestType,
327329
libc::pid_t::from(pid),
328-
ptr::null_mut::<T>(),
330+
mem::size_of::<T>(),
329331
data.as_mut_ptr() as *const _ as *const c_void,
330332
)
331333
};
@@ -371,6 +373,11 @@ pub fn getsiginfo(pid: Pid) -> Result<siginfo_t> {
371373
ptrace_get_data::<siginfo_t>(Request::PTRACE_GETSIGINFO, pid)
372374
}
373375

376+
/// Get sigmask as with `ptrace(PTRACE_GETSIGMASK,...)`
377+
pub fn getsigmask(pid: Pid) -> Result<u64> {
378+
ptrace_get_data::<u64>(Request::PTRACE_GETSIGMASK, pid)
379+
}
380+
374381
/// Get ptrace syscall info as with `ptrace(PTRACE_GET_SYSCALL_INFO,...)`
375382
/// Only available on Linux 5.3+
376383
#[cfg(all(target_os = "linux", target_env = "gnu"))]
@@ -404,6 +411,19 @@ pub fn setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
404411
}
405412
}
406413

414+
/// Set sigmask as with `ptrace(PTRACE_SETSIGMASK,...)`
415+
pub fn setsigmask(pid: Pid, mask: u64) -> Result<()> {
416+
unsafe {
417+
ptrace_other(
418+
Request::PTRACE_SETSIGMASK,
419+
pid,
420+
mem::size_of::<u64>() as _,
421+
&mask as *const _ as *mut c_void,
422+
)
423+
.map(drop)
424+
}
425+
}
426+
407427
/// Sets the process as traceable, as with `ptrace(PTRACE_TRACEME, ...)`
408428
///
409429
/// Indicates that this process is to be traced by its parent.

test/sys/test_ptrace.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ fn test_ptrace_setsiginfo() {
6666
}
6767
}
6868

69+
// Just make sure ptrace_getsigmask can be called at all, for now.
70+
#[test]
71+
fn test_ptrace_getsigmask() {
72+
require_capability!("test_ptrace_getsigmask", CAP_SYS_PTRACE);
73+
if let Err(Errno::EOPNOTSUPP) = ptrace::getsigmask(getpid()) {
74+
panic!("ptrace_getsigmask returns Errno::EOPNOTSUPP!");
75+
}
76+
}
77+
78+
// Just make sure ptrace_setsigmask can be called at all, for now.
79+
#[test]
80+
fn test_ptrace_setsigmask() {
81+
require_capability!("test_ptrace_setsigmask", CAP_SYS_PTRACE);
82+
let sigmask = unsafe { mem::zeroed() };
83+
if let Err(Errno::EOPNOTSUPP) = ptrace::setsigmask(getpid(), &sigmask) {
84+
panic!("ptrace_setsigmask returns Errno::EOPNOTSUPP!");
85+
}
86+
}
87+
6988
#[test]
7089
fn test_ptrace_cont() {
7190
use nix::sys::ptrace;

0 commit comments

Comments
 (0)