|
2 | 2 | #![allow(improper_ctypes)]
|
3 | 3 |
|
4 | 4 | use {Errno, Result};
|
| 5 | +use unistd::Pid; |
5 | 6 | use libc::{self, c_int, c_void, size_t, off_t};
|
6 | 7 | use std::marker::PhantomData;
|
7 | 8 | use std::os::unix::io::RawFd;
|
@@ -56,6 +57,84 @@ pub fn pread(fd: RawFd, buf: &mut [u8], offset: off_t) -> Result<usize>{
|
56 | 57 | Errno::result(res).map(|r| r as usize)
|
57 | 58 | }
|
58 | 59 |
|
| 60 | +/// A slice of memory in a remote process, starting at address `base` |
| 61 | +/// and consisting of `len` bytes. |
| 62 | +/// |
| 63 | +/// This is the same underlying C structure as [`IoVec`](struct.IoVec.html), |
| 64 | +/// except that it refers to memory in some other process, and is |
| 65 | +/// therefore not represented in Rust by an actual slice as IoVec is. It |
| 66 | +/// is used with [`process_vm_readv`](fn.process_vm_readv.html) |
| 67 | +/// and [`process_vm_writev`](fn.process_vm_writev.html). |
| 68 | +#[repr(C)] |
| 69 | +pub struct RemoteIoVec { |
| 70 | + /// The starting address of this slice (`iov_base`). |
| 71 | + pub base: usize, |
| 72 | + /// The number of bytes in this slice (`iov_len`). |
| 73 | + pub len: usize, |
| 74 | +} |
| 75 | + |
| 76 | +/// Write data directly to another process's virtual memory |
| 77 | +/// (see [`process_vm_writev`(2)]). |
| 78 | +/// |
| 79 | +/// `local_iov` is a list of [`IoVec`]s containing the data to be written, |
| 80 | +/// and `remote_iov` is a list of [`RemoteIoVec`]s identifying where the |
| 81 | +/// data should be written in the target process. On success, returns the |
| 82 | +/// number of bytes written, which will always be a whole |
| 83 | +/// number of remote_iov chunks. |
| 84 | +/// |
| 85 | +/// This requires the same permissions as debugging the process using |
| 86 | +/// [ptrace]: you must either be a privileged process (with |
| 87 | +/// `CAP_SYS_PTRACE`), or you must be running as the same user as the |
| 88 | +/// target process and the OS must have unprivileged debugging enabled. |
| 89 | +/// |
| 90 | +/// This function is only available on Linux. |
| 91 | +/// |
| 92 | +/// [`process_vm_writev`(2)]: http://man7.org/linux/man-pages/man2/process_vm_writev.2.html |
| 93 | +/// [ptrace]: ../ptrace/index.html |
| 94 | +/// [`IoVec`]: struct.IoVec.html |
| 95 | +/// [`RemoteIoVec`]: struct.RemoteIoVec.html |
| 96 | +#[cfg(target_os = "linux")] |
| 97 | +pub fn process_vm_writev(pid: Pid, local_iov: &[IoVec<&[u8]>], remote_iov: &[RemoteIoVec]) -> Result<usize> { |
| 98 | + let res = unsafe { |
| 99 | + libc::process_vm_writev(pid.into(), |
| 100 | + local_iov.as_ptr() as *const libc::iovec, local_iov.len() as libc::c_ulong, |
| 101 | + remote_iov.as_ptr() as *const libc::iovec, remote_iov.len() as libc::c_ulong, 0) |
| 102 | + }; |
| 103 | + |
| 104 | + Errno::result(res).map(|r| r as usize) |
| 105 | +} |
| 106 | + |
| 107 | +/// Read data directly from another process's virtual memory |
| 108 | +/// (see [`process_vm_readv`(2)]). |
| 109 | +/// |
| 110 | +/// `local_iov` is a list of [`IoVec`]s containing the buffer to copy |
| 111 | +/// data into, and `remote_iov` is a list of [`RemoteIoVec`]s identifying |
| 112 | +/// where the source data is in the target process. On success, |
| 113 | +/// returns the number of bytes written, which will always be a whole |
| 114 | +/// number of remote_iov chunks. |
| 115 | +/// |
| 116 | +/// This requires the same permissions as debugging the process using |
| 117 | +/// [ptrace]: you must either be a privileged process (with |
| 118 | +/// `CAP_SYS_PTRACE`), or you must be running as the same user as the |
| 119 | +/// target process and the OS must have unprivileged debugging enabled. |
| 120 | +/// |
| 121 | +/// This function is only available on Linux. |
| 122 | +/// |
| 123 | +/// [`process_vm_readv`(2)]: http://man7.org/linux/man-pages/man2/process_vm_readv.2.html |
| 124 | +/// [ptrace]: ../ptrace/index.html |
| 125 | +/// [`IoVec`]: struct.IoVec.html |
| 126 | +/// [`RemoteIoVec`]: struct.RemoteIoVec.html |
| 127 | +#[cfg(any(target_os = "linux"))] |
| 128 | +pub fn process_vm_readv(pid: Pid, local_iov: &[IoVec<&mut [u8]>], remote_iov: &[RemoteIoVec]) -> Result<usize> { |
| 129 | + let res = unsafe { |
| 130 | + libc::process_vm_readv(pid.into(), |
| 131 | + local_iov.as_ptr() as *const libc::iovec, local_iov.len() as libc::c_ulong, |
| 132 | + remote_iov.as_ptr() as *const libc::iovec, remote_iov.len() as libc::c_ulong, 0) |
| 133 | + }; |
| 134 | + |
| 135 | + Errno::result(res).map(|r| r as usize) |
| 136 | +} |
| 137 | + |
59 | 138 | #[repr(C)]
|
60 | 139 | pub struct IoVec<T>(libc::iovec, PhantomData<T>);
|
61 | 140 |
|
|
0 commit comments