Skip to content

Commit 1255748

Browse files
committed
Fix breaking changes from nix update.
1 parent f8c5ecd commit 1255748

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ version = "1.0.0"
44
edition = "2021"
55

66
[dependencies]
7-
nix = "0.28"
7+
nix = { version = "0.28", features = ["ptrace", "sched", "personality", "signal", "fs"] }

examples/spawning.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::io::Write;
2+
use std::os::fd::{AsRawFd, BorrowedFd};
23
use std::sync::Arc;
34
use std::{ffi::CString, path::Path};
45

@@ -20,19 +21,20 @@ fn spawn<P: AsRef<Path>>(tracer: &Tracer, path: P) -> Result<Pid, nix::Error> {
2021

2122
// Create a communication pipe for error handling.
2223
let (pipe_read, pipe_write) = nix::unistd::pipe()?;
24+
let (pipe_read, pipe_write) = (pipe_read.as_raw_fd(), pipe_write.as_raw_fd());
2325

2426
// Tell write pipe to close on exec (required for checking successful execution).
25-
fcntl(pipe_write, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC))?;
27+
fcntl(pipe_write.as_raw_fd(), FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC))?;
2628

2729
let child = move || unsafe {
2830
let _ = nix::unistd::close(pipe_read);
2931

30-
let report_error = |err| {
32+
let report_error = move |err| {
3133
// Convert error to bytes.
3234
let errno = (err as i32).to_ne_bytes();
3335

3436
// Write error status to pipe.
35-
let _ = nix::unistd::write(pipe_write, &errno);
37+
let _ = nix::unistd::write(std::mem::transmute::<_, BorrowedFd>(pipe_write), &errno);
3638

3739
// Explicitly close the write end of the pipe to ensure the parent can read EOF
3840
// if exec hasn't been called.
@@ -69,11 +71,11 @@ fn spawn<P: AsRef<Path>>(tracer: &Tracer, path: P) -> Result<Pid, nix::Error> {
6971
let _ = nix::unistd::close(pipe_write);
7072

7173
let mut errno = [0; 4];
72-
match nix::unistd::read(pipe_read, &mut errno) {
74+
match nix::unistd::read(pipe_read.as_raw_fd(), &mut errno) {
7375
// Child ran into error.
7476
Ok(4) => {
7577
let errno = i32::from_ne_bytes(errno);
76-
let errno = nix::Error::from_i32(errno);
78+
let errno = nix::Error::from_raw(errno);
7779
return Err(errno);
7880
}
7981
// Child ran successfully.

src/lib.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use nix::errno::Errno;
1+
use nix::Error;
22
use nix::libc;
33
use nix::sys::ptrace::{Request, RequestType};
44
use nix::sys::signal::Signal;
@@ -31,15 +31,15 @@ impl PtraceRequest {
3131
);
3232

3333
if ret == -1 {
34-
-(nix::errno::errno() as c_long)
34+
-(Error::last() as c_long)
3535
} else {
3636
ret
3737
}
3838
}
3939

4040
unsafe fn peek(self) -> c_long {
4141
let ret = unsafe {
42-
Errno::clear();
42+
Error::clear();
4343
libc::ptrace(
4444
self.ty as RequestType,
4545
libc::pid_t::from(self.pid),
@@ -49,12 +49,12 @@ impl PtraceRequest {
4949
};
5050

5151
if ret == -1 {
52-
let errno = nix::errno::errno();
52+
let errno = Error::last();
5353

54-
if errno == Errno::UnknownErrno as i32 {
54+
if errno == Error::UnknownErrno {
5555
ret
5656
} else {
57-
-errno as c_long
57+
-(errno as c_long)
5858
}
5959
} else {
6060
ret
@@ -126,7 +126,7 @@ impl Tracer {
126126
*lock = c_long::MIN;
127127

128128
if result < 0 {
129-
Err(Errno::from_i32(-result as i32))
129+
Err(Error::from_raw(-result as i32))
130130
} else {
131131
Ok(result)
132132
}
@@ -145,7 +145,7 @@ impl Tracer {
145145
*lock = c_long::MIN;
146146

147147
if result < 0 {
148-
Err(Errno::from_i32(-result as i32))
148+
Err(Error::from_raw(-result as i32))
149149
} else {
150150
Ok(Pid::from_raw(result as i32))
151151
}
@@ -170,6 +170,25 @@ impl Tracer {
170170
.map(drop)
171171
}
172172

173+
/// Continue execution until the next syscall, as with `ptrace(PTRACE_SYSCALL, ...)`
174+
///
175+
/// Arranges for the tracee to be stopped at the next entry to or exit from a system call,
176+
/// optionally delivering a signal specified by `sig`.
177+
pub fn syscall<T: Into<Option<Signal>>>(&self, pid: Pid, sig: T) -> Result<()> {
178+
let data = match sig.into() {
179+
Some(s) => s as i32 as usize,
180+
None => 0,
181+
};
182+
183+
self.send(PtraceRequest {
184+
ty: Request::PTRACE_SYSCALL,
185+
pid,
186+
addr: 0,
187+
data,
188+
})
189+
.map(drop)
190+
}
191+
173192
/// Sets the process as traceable, as with `ptrace(PTRACE_TRACEME, ...)`
174193
///
175194
/// Indicates that this process is to be traced by its parent.

0 commit comments

Comments
 (0)