Coverage Report

Created: 2026-02-14 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustix-1.0.7/src/pipe.rs
Line
Count
Source
1
//! `pipe` and related APIs.
2
3
#![allow(unsafe_code)]
4
5
use crate::fd::OwnedFd;
6
use crate::{backend, io};
7
#[cfg(not(any(
8
    solarish,
9
    windows,
10
    target_os = "espidf",
11
    target_os = "haiku",
12
    target_os = "redox",
13
    target_os = "vita",
14
    target_os = "wasi",
15
)))]
16
use backend::c;
17
#[cfg(linux_kernel)]
18
use backend::fd::AsFd;
19
20
#[cfg(not(apple))]
21
pub use backend::pipe::types::PipeFlags;
22
23
#[cfg(linux_kernel)]
24
pub use backend::pipe::types::{IoSliceRaw, SpliceFlags};
25
26
/// `PIPE_BUF`—The maximum length at which writes to a pipe are atomic.
27
///
28
/// # References
29
///  - [Linux]
30
///  - [POSIX]
31
///
32
/// [Linux]: https://man7.org/linux/man-pages/man7/pipe.7.html
33
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/write.html
34
#[cfg(not(any(
35
    solarish,
36
    windows,
37
    target_os = "espidf",
38
    target_os = "haiku",
39
    target_os = "horizon",
40
    target_os = "hurd",
41
    target_os = "redox",
42
    target_os = "vita",
43
    target_os = "wasi",
44
)))]
45
pub const PIPE_BUF: usize = c::PIPE_BUF;
46
47
/// `pipe()`—Creates a pipe.
48
///
49
/// This function creates a pipe and returns two file descriptors, for the
50
/// reading and writing ends of the pipe, respectively.
51
///
52
/// See [`pipe_with`] to pass additional flags.
53
///
54
/// # References
55
///  - [POSIX]
56
///  - [Linux]
57
///  - [Apple]
58
///  - [FreeBSD]
59
///  - [NetBSD]
60
///  - [OpenBSD]
61
///  - [DragonFly BSD]
62
///  - [illumos]
63
///  - [glibc]
64
///
65
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/pipe.html
66
/// [Linux]: https://man7.org/linux/man-pages/man2/pipe.2.html
67
/// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/pipe.2.html
68
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=pipe&sektion=2
69
/// [NetBSD]: https://man.netbsd.org/pipe.2
70
/// [OpenBSD]: https://man.openbsd.org/pipe.2
71
/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=pipe&section=2
72
/// [illumos]: https://illumos.org/man/2/pipe
73
/// [glibc]: https://sourceware.org/glibc/manual/latest/html_node/Creating-a-Pipe.html
74
#[inline]
75
0
pub fn pipe() -> io::Result<(OwnedFd, OwnedFd)> {
76
0
    backend::pipe::syscalls::pipe()
77
0
}
Unexecuted instantiation: rustix::pipe::pipe
Unexecuted instantiation: rustix::pipe::pipe
78
79
/// `pipe2(flags)`—Creates a pipe, with flags.
80
///
81
/// `pipe_with` is the same as [`pipe`] but adds an additional flags operand.
82
///
83
/// This function creates a pipe and returns two file descriptors, for the
84
/// reading and writing ends of the pipe, respectively.
85
///
86
/// # References
87
///  - [Linux]
88
///  - [FreeBSD]
89
///  - [NetBSD]
90
///  - [OpenBSD]
91
///  - [DragonFly BSD]
92
///  - [illumos]
93
///
94
/// [Linux]: https://man7.org/linux/man-pages/man2/pipe2.2.html
95
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=pipe2&sektion=2
96
/// [NetBSD]: https://man.netbsd.org/pipe2.2
97
/// [OpenBSD]: https://man.openbsd.org/pipe2.2
98
/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=pipe2&section=2
99
/// [illumos]: https://illumos.org/man/2/pipe2
100
#[cfg(not(any(
101
    apple,
102
    target_os = "aix",
103
    target_os = "espidf",
104
    target_os = "haiku",
105
    target_os = "horizon",
106
    target_os = "nto"
107
)))]
108
#[inline]
109
#[doc(alias = "pipe2")]
110
0
pub fn pipe_with(flags: PipeFlags) -> io::Result<(OwnedFd, OwnedFd)> {
111
0
    backend::pipe::syscalls::pipe_with(flags)
112
0
}
113
114
/// `splice(fd_in, off_in, fd_out, off_out, len, flags)`—Transfer data
115
/// between a file and a pipe.
116
///
117
/// This function transfers up to `len` bytes of data from the file descriptor
118
/// `fd_in` to the file descriptor `fd_out`, where one of the file descriptors
119
/// must refer to a pipe.
120
///
121
/// `off_*` must be `None` if the corresponding fd refers to a pipe. Otherwise
122
/// its value points to the starting offset to the file, from which the data is
123
/// read/written. On success, the number of bytes read/written is added to the
124
/// offset.
125
///
126
/// Passing `None` causes the read/write to start from the file offset, and the
127
/// file offset is adjusted appropriately.
128
///
129
/// # References
130
///  - [Linux]
131
///
132
/// [Linux]: https://man7.org/linux/man-pages/man2/splice.2.html
133
#[cfg(linux_kernel)]
134
#[inline]
135
0
pub fn splice<FdIn: AsFd, FdOut: AsFd>(
136
0
    fd_in: FdIn,
137
0
    off_in: Option<&mut u64>,
138
0
    fd_out: FdOut,
139
0
    off_out: Option<&mut u64>,
140
0
    len: usize,
141
0
    flags: SpliceFlags,
142
0
) -> io::Result<usize> {
143
0
    backend::pipe::syscalls::splice(fd_in.as_fd(), off_in, fd_out.as_fd(), off_out, len, flags)
144
0
}
145
146
/// `vmsplice(fd, bufs, flags)`—Transfer data between memory and a pipe.
147
///
148
/// If `fd` is the write end of the pipe, the function maps the memory pointer
149
/// at by `bufs` to the pipe.
150
///
151
/// If `fd` is the read end of the pipe, the function writes data from the pipe
152
/// to said memory.
153
///
154
/// # Safety
155
///
156
/// If the memory must not be mutated (such as when `bufs` were originally
157
/// immutable slices), it is up to the caller to ensure that the write end of
158
/// the pipe is placed in `fd`.
159
///
160
/// Additionally if `SpliceFlags::GIFT` is set, the caller must also ensure
161
/// that the contents of `bufs` in never modified following the call, and that
162
/// all of the pointers in `bufs` are page aligned, and the lengths are
163
/// multiples of a page size in bytes.
164
///
165
/// # References
166
///  - [Linux]
167
///
168
/// [Linux]: https://man7.org/linux/man-pages/man2/vmsplice.2.html
169
#[cfg(linux_kernel)]
170
#[inline]
171
0
pub unsafe fn vmsplice<PipeFd: AsFd>(
172
0
    fd: PipeFd,
173
0
    bufs: &[IoSliceRaw<'_>],
174
0
    flags: SpliceFlags,
175
0
) -> io::Result<usize> {
176
0
    backend::pipe::syscalls::vmsplice(fd.as_fd(), bufs, flags)
177
0
}
178
179
/// `tee(fd_in, fd_out, len, flags)`—Copy data between pipes without
180
/// consuming it.
181
///
182
/// This reads up to `len` bytes from `in_fd` without consuming them, and
183
/// writes them to `out_fd`.
184
///
185
/// # References
186
///  - [Linux]
187
///
188
/// [Linux]: https://man7.org/linux/man-pages/man2/tee.2.html
189
#[cfg(linux_kernel)]
190
#[inline]
191
0
pub fn tee<FdIn: AsFd, FdOut: AsFd>(
192
0
    fd_in: FdIn,
193
0
    fd_out: FdOut,
194
0
    len: usize,
195
0
    flags: SpliceFlags,
196
0
) -> io::Result<usize> {
197
0
    backend::pipe::syscalls::tee(fd_in.as_fd(), fd_out.as_fd(), len, flags)
198
0
}
199
200
/// `fnctl(fd, F_GETPIPE_SZ)`—Return the buffer capacity of a pipe.
201
///
202
/// # References
203
///  - [Linux]
204
///
205
/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
206
#[cfg(linux_kernel)]
207
#[inline]
208
0
pub fn fcntl_getpipe_size<Fd: AsFd>(fd: Fd) -> io::Result<usize> {
209
0
    backend::pipe::syscalls::fcntl_getpipe_size(fd.as_fd())
210
0
}
211
212
/// `fnctl(fd, F_SETPIPE_SZ)`—Set the buffer capacity of a pipe.
213
///
214
/// # References
215
///  - [Linux]
216
///
217
/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
218
#[cfg(linux_kernel)]
219
#[inline]
220
0
pub fn fcntl_setpipe_size<Fd: AsFd>(fd: Fd, size: usize) -> io::Result<usize> {
221
0
    backend::pipe::syscalls::fcntl_setpipe_size(fd.as_fd(), size)
222
0
}