nix/sys/uio.rs
1//! Vectored I/O
2
3use crate::Result;
4use crate::errno::Errno;
5use libc::{self, c_int, c_void, size_t, off_t};
6use std::marker::PhantomData;
7use std::os::unix::io::RawFd;
8
9/// Low-level vectored write to a raw file descriptor
10///
11/// See also [writev(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/writev.html)
12pub fn writev(fd: RawFd, iov: &[IoVec<&[u8]>]) -> Result<usize> {
13 let res = unsafe { libc::writev(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int) };
14
15 Errno::result(res).map(|r| r as usize)
16}
17
18/// Low-level vectored read from a raw file descriptor
19///
20/// See also [readv(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/readv.html)
21pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result<usize> {
22 let res = unsafe { libc::readv(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int) };
23
24 Errno::result(res).map(|r| r as usize)
25}
26
27/// Write to `fd` at `offset` from buffers in `iov`.
28///
29/// Buffers in `iov` will be written in order until all buffers have been written
30/// or an error occurs. The file offset is not changed.
31///
32/// See also: [`writev`](fn.writev.html) and [`pwrite`](fn.pwrite.html)
33#[cfg(not(target_os = "redox"))]
34pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>],
35 offset: off_t) -> Result<usize> {
36 let res = unsafe {
37 libc::pwritev(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset)
38 };
39
40 Errno::result(res).map(|r| r as usize)
41}
42
43/// Read from `fd` at `offset` filling buffers in `iov`.
44///
45/// Buffers in `iov` will be filled in order until all buffers have been filled,
46/// no more bytes are available, or an error occurs. The file offset is not
47/// changed.
48///
49/// See also: [`readv`](fn.readv.html) and [`pread`](fn.pread.html)
50#[cfg(not(target_os = "redox"))]
51pub fn preadv(fd: RawFd, iov: &[IoVec<&mut [u8]>],
52 offset: off_t) -> Result<usize> {
53 let res = unsafe {
54 libc::preadv(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset)
55 };
56
57 Errno::result(res).map(|r| r as usize)
58}
59
60/// Low-level write to a file, with specified offset.
61///
62/// See also [pwrite(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html)
63// TODO: move to unistd
64pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result<usize> {
65 let res = unsafe {
66 libc::pwrite(fd, buf.as_ptr() as *const c_void, buf.len() as size_t,
67 offset)
68 };
69
70 Errno::result(res).map(|r| r as usize)
71}
72
73/// Low-level write to a file, with specified offset.
74///
75/// See also [pread(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html)
76// TODO: move to unistd
77pub fn pread(fd: RawFd, buf: &mut [u8], offset: off_t) -> Result<usize>{
78 let res = unsafe {
79 libc::pread(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t,
80 offset)
81 };
82
83 Errno::result(res).map(|r| r as usize)
84}
85
86/// A slice of memory in a remote process, starting at address `base`
87/// and consisting of `len` bytes.
88///
89/// This is the same underlying C structure as [`IoVec`](struct.IoVec.html),
90/// except that it refers to memory in some other process, and is
91/// therefore not represented in Rust by an actual slice as `IoVec` is. It
92/// is used with [`process_vm_readv`](fn.process_vm_readv.html)
93/// and [`process_vm_writev`](fn.process_vm_writev.html).
94#[cfg(target_os = "linux")]
95#[repr(C)]
96#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
97pub struct RemoteIoVec {
98 /// The starting address of this slice (`iov_base`).
99 pub base: usize,
100 /// The number of bytes in this slice (`iov_len`).
101 pub len: usize,
102}
103
104/// Write data directly to another process's virtual memory
105/// (see [`process_vm_writev`(2)]).
106///
107/// `local_iov` is a list of [`IoVec`]s containing the data to be written,
108/// and `remote_iov` is a list of [`RemoteIoVec`]s identifying where the
109/// data should be written in the target process. On success, returns the
110/// number of bytes written, which will always be a whole
111/// number of `remote_iov` chunks.
112///
113/// This requires the same permissions as debugging the process using
114/// [ptrace]: you must either be a privileged process (with
115/// `CAP_SYS_PTRACE`), or you must be running as the same user as the
116/// target process and the OS must have unprivileged debugging enabled.
117///
118/// This function is only available on Linux.
119///
120/// [`process_vm_writev`(2)]: https://man7.org/linux/man-pages/man2/process_vm_writev.2.html
121/// [ptrace]: ../ptrace/index.html
122/// [`IoVec`]: struct.IoVec.html
123/// [`RemoteIoVec`]: struct.RemoteIoVec.html
124#[cfg(target_os = "linux")]
125pub fn process_vm_writev(
126 pid: crate::unistd::Pid,
127 local_iov: &[IoVec<&[u8]>],
128 remote_iov: &[RemoteIoVec]) -> Result<usize>
129{
130 let res = unsafe {
131 libc::process_vm_writev(pid.into(),
132 local_iov.as_ptr() as *const libc::iovec, local_iov.len() as libc::c_ulong,
133 remote_iov.as_ptr() as *const libc::iovec, remote_iov.len() as libc::c_ulong, 0)
134 };
135
136 Errno::result(res).map(|r| r as usize)
137}
138
139/// Read data directly from another process's virtual memory
140/// (see [`process_vm_readv`(2)]).
141///
142/// `local_iov` is a list of [`IoVec`]s containing the buffer to copy
143/// data into, and `remote_iov` is a list of [`RemoteIoVec`]s identifying
144/// where the source data is in the target process. On success,
145/// returns the number of bytes written, which will always be a whole
146/// number of `remote_iov` chunks.
147///
148/// This requires the same permissions as debugging the process using
149/// [`ptrace`]: you must either be a privileged process (with
150/// `CAP_SYS_PTRACE`), or you must be running as the same user as the
151/// target process and the OS must have unprivileged debugging enabled.
152///
153/// This function is only available on Linux.
154///
155/// [`process_vm_readv`(2)]: https://man7.org/linux/man-pages/man2/process_vm_readv.2.html
156/// [`ptrace`]: ../ptrace/index.html
157/// [`IoVec`]: struct.IoVec.html
158/// [`RemoteIoVec`]: struct.RemoteIoVec.html
159#[cfg(any(target_os = "linux"))]
160pub fn process_vm_readv(
161 pid: crate::unistd::Pid,
162 local_iov: &[IoVec<&mut [u8]>],
163 remote_iov: &[RemoteIoVec]) -> Result<usize>
164{
165 let res = unsafe {
166 libc::process_vm_readv(pid.into(),
167 local_iov.as_ptr() as *const libc::iovec, local_iov.len() as libc::c_ulong,
168 remote_iov.as_ptr() as *const libc::iovec, remote_iov.len() as libc::c_ulong, 0)
169 };
170
171 Errno::result(res).map(|r| r as usize)
172}
173
174/// A vector of buffers.
175///
176/// Vectored I/O methods like [`writev`] and [`readv`] use this structure for
177/// both reading and writing. Each `IoVec` specifies the base address and
178/// length of an area in memory.
179#[repr(transparent)]
180#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
181pub struct IoVec<T>(pub(crate) libc::iovec, PhantomData<T>);
182
183impl<T> IoVec<T> {
184 /// View the `IoVec` as a Rust slice.
185 #[inline]
186 pub fn as_slice(&self) -> &[u8] {
187 use std::slice;
188
189 unsafe {
190 slice::from_raw_parts(
191 self.0.iov_base as *const u8,
192 self.0.iov_len)
193 }
194 }
195}
196
197impl<'a> IoVec<&'a [u8]> {
198 #[cfg(target_os = "freebsd")]
199 pub(crate) fn from_raw_parts(base: *mut c_void, len: usize) -> Self {
200 IoVec(libc::iovec {
201 iov_base: base,
202 iov_len: len
203 }, PhantomData)
204 }
205
206 /// Create an `IoVec` from a Rust slice.
207 pub fn from_slice(buf: &'a [u8]) -> IoVec<&'a [u8]> {
208 IoVec(libc::iovec {
209 iov_base: buf.as_ptr() as *mut c_void,
210 iov_len: buf.len() as size_t,
211 }, PhantomData)
212 }
213}
214
215impl<'a> IoVec<&'a mut [u8]> {
216 /// Create an `IoVec` from a mutable Rust slice.
217 pub fn from_mut_slice(buf: &'a mut [u8]) -> IoVec<&'a mut [u8]> {
218 IoVec(libc::iovec {
219 iov_base: buf.as_ptr() as *mut c_void,
220 iov_len: buf.len() as size_t,
221 }, PhantomData)
222 }
223}