Skip to content

Commit

Permalink
ptrace: implement getsigmask and setsigmask
Browse files Browse the repository at this point in the history
  • Loading branch information
mbyzhang committed Mar 23, 2023
1 parent 9d7d122 commit c65d7eb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/sys/ptrace/linux.rs
Expand Up @@ -126,6 +126,8 @@ libc_enum! {
PTRACE_SYSEMU_SINGLESTEP,
#[cfg(all(target_os = "linux", target_env = "gnu"))]
PTRACE_GET_SYSCALL_INFO,
PTRACE_GETSIGMASK,
PTRACE_SETSIGMASK,
}
}

Expand Down Expand Up @@ -325,7 +327,7 @@ fn ptrace_get_data<T>(request: Request, pid: Pid) -> Result<T> {
libc::ptrace(
request as RequestType,
libc::pid_t::from(pid),
ptr::null_mut::<T>(),
mem::size_of::<T>(),
data.as_mut_ptr() as *const _ as *const c_void,
)
};
Expand Down Expand Up @@ -371,6 +373,11 @@ pub fn getsiginfo(pid: Pid) -> Result<siginfo_t> {
ptrace_get_data::<siginfo_t>(Request::PTRACE_GETSIGINFO, pid)
}

/// Get sigmask as with `ptrace(PTRACE_GETSIGMASK,...)`
pub fn getsigmask(pid: Pid) -> Result<u64> {
ptrace_get_data::<u64>(Request::PTRACE_GETSIGMASK, pid)
}

/// Get ptrace syscall info as with `ptrace(PTRACE_GET_SYSCALL_INFO,...)`
/// Only available on Linux 5.3+
#[cfg(all(target_os = "linux", target_env = "gnu"))]
Expand Down Expand Up @@ -404,6 +411,19 @@ pub fn setsiginfo(pid: Pid, sig: &siginfo_t) -> Result<()> {
}
}

/// Set sigmask as with `ptrace(PTRACE_SETSIGMASK,...)`
pub fn setsigmask(pid: Pid, mask: u64) -> Result<()> {
unsafe {
ptrace_other(
Request::PTRACE_SETSIGMASK,
pid,
mem::size_of::<u64>() as _,
&mask as *const _ as *mut c_void,
)
.map(drop)
}
}

/// Sets the process as traceable, as with `ptrace(PTRACE_TRACEME, ...)`
///
/// Indicates that this process is to be traced by its parent.
Expand Down
19 changes: 19 additions & 0 deletions test/sys/test_ptrace.rs
Expand Up @@ -66,6 +66,25 @@ fn test_ptrace_setsiginfo() {
}
}

// Just make sure ptrace_getsigmask can be called at all, for now.
#[test]
fn test_ptrace_getsigmask() {
require_capability!("test_ptrace_getsigmask", CAP_SYS_PTRACE);
if let Err(Errno::EOPNOTSUPP) = ptrace::getsigmask(getpid()) {
panic!("ptrace_getsigmask returns Errno::EOPNOTSUPP!");
}
}

// Just make sure ptrace_setsigmask can be called at all, for now.
#[test]
fn test_ptrace_setsigmask() {
require_capability!("test_ptrace_setsigmask", CAP_SYS_PTRACE);
let sigmask = unsafe { mem::zeroed() };
if let Err(Errno::EOPNOTSUPP) = ptrace::setsigmask(getpid(), &sigmask) {
panic!("ptrace_setsigmask returns Errno::EOPNOTSUPP!");
}
}

#[test]
fn test_ptrace_cont() {
use nix::sys::ptrace;
Expand Down

0 comments on commit c65d7eb

Please sign in to comment.