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