1//! Owned and borrowed Unix-like file descriptors.
23#![stable(feature = "io_safety", since = "1.63.0")]
4#![deny(unsafe_op_in_unsafe_fn)]
56#[cfg(target_os = "motor")]
7use moto_rt::libc;
89use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
10#[cfg(not(target_os = "trusty"))]
11use crate::fs;
12use crate::marker::PhantomData;
13use crate::mem::ManuallyDrop;
14#[cfg(not(any(
15 target_arch = "wasm32",
16 target_env = "sgx",
17 target_os = "hermit",
18 target_os = "trusty",
19 target_os = "motor"
20)))]
21use crate::sys::cvt;
22#[cfg(not(target_os = "trusty"))]
23use crate::sys::{AsInner, FromInner, IntoInner};
24use crate::{fmt, io};
2526type ValidRawFd = core::num::niche_types::NotAllOnes<RawFd>;
2728/// A borrowed file descriptor.
29///
30/// This has a lifetime parameter to tie it to the lifetime of something that owns the file
31/// descriptor. For the duration of that lifetime, it is guaranteed that nobody will close the file
32/// descriptor.
33///
34/// This uses `repr(transparent)` and has the representation of a host file
35/// descriptor, so it can be used in FFI in places where a file descriptor is
36/// passed as an argument, it is not captured or consumed, and it never has the
37/// value `-1`.
38///
39/// This type does not have a [`ToOwned`][crate::borrow::ToOwned]
40/// implementation. Calling `.to_owned()` on a variable of this type will call
41/// it on `&BorrowedFd` and use `Clone::clone()` like `ToOwned` does for all
42/// types implementing `Clone`. The result will be descriptor borrowed under
43/// the same lifetime.
44///
45/// To obtain an [`OwnedFd`], you can use [`BorrowedFd::try_clone_to_owned`]
46/// instead, but this is not supported on all platforms.
47#[derive(#[automatically_derived]
#[stable(feature = "io_safety", since = "1.63.0")]
impl<'fd> ::core::marker::Copy for BorrowedFd<'fd> { }Copy, #[automatically_derived]
#[stable(feature = "io_safety", since = "1.63.0")]
impl<'fd> ::core::clone::Clone for BorrowedFd<'fd> {
#[inline]
fn clone(&self) -> BorrowedFd<'fd> {
let _: ::core::clone::AssertParamIsClone<ValidRawFd>;
let _: ::core::clone::AssertParamIsClone<PhantomData<&'fd OwnedFd>>;
*self
}
}Clone)]
48#[repr(transparent)]
49#[rustc_nonnull_optimization_guaranteed]
50#[stable(feature = "io_safety", since = "1.63.0")]
51pub struct BorrowedFd<'fd> {
52 fd: ValidRawFd,
53 _phantom: PhantomData<&'fd OwnedFd>,
54}
5556/// An owned file descriptor.
57///
58/// This closes the file descriptor on drop. It is guaranteed that nobody else will close the file
59/// descriptor.
60///
61/// This uses `repr(transparent)` and has the representation of a host file
62/// descriptor, so it can be used in FFI in places where a file descriptor is
63/// passed as a consumed argument or returned as an owned value, and it never
64/// has the value `-1`.
65///
66/// You can use [`AsFd::as_fd`] to obtain a [`BorrowedFd`].
67#[repr(transparent)]
68#[rustc_nonnull_optimization_guaranteed]
69#[stable(feature = "io_safety", since = "1.63.0")]
70pub struct OwnedFd {
71 fd: ValidRawFd,
72}
7374impl BorrowedFd<'_> {
75/// Returns a `BorrowedFd` holding the given raw file descriptor.
76 ///
77 /// # Safety
78 ///
79 /// The resource pointed to by `fd` must remain open for the duration of
80 /// the returned `BorrowedFd`.
81 ///
82 /// # Panics
83 ///
84 /// Panics if the raw file descriptor has the value `-1`.
85#[inline]
86 #[track_caller]
87 #[rustc_const_stable(feature = "io_safety", since = "1.63.0")]
88 #[stable(feature = "io_safety", since = "1.63.0")]
89pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
90Self { fd: ValidRawFd::new(fd).expect("fd != -1"), _phantom: PhantomData }
91 }
92}
9394impl OwnedFd {
95/// Creates a new `OwnedFd` instance that shares the same underlying file
96 /// description as the existing `OwnedFd` instance.
97#[stable(feature = "io_safety", since = "1.63.0")]
98pub fn try_clone(&self) -> io::Result<Self> {
99self.as_fd().try_clone_to_owned()
100 }
101}
102103impl BorrowedFd<'_> {
104/// Creates a new `OwnedFd` instance that shares the same underlying file
105 /// description as the existing `BorrowedFd` instance.
106#[cfg(not(any(
107 target_arch = "wasm32",
108 target_os = "hermit",
109 target_os = "trusty",
110 target_os = "motor"
111)))]
112 #[stable(feature = "io_safety", since = "1.63.0")]
113pub fn try_clone_to_owned(&self) -> io::Result<OwnedFd> {
114// We want to atomically duplicate this file descriptor and set the
115 // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
116 // is a POSIX flag that was added to Linux in 2.6.24.
117#[cfg(not(any(target_os = "espidf", target_os = "vita")))]
118let cmd = libc::F_DUPFD_CLOEXEC;
119120// For ESP-IDF, F_DUPFD is used instead, because the CLOEXEC semantics
121 // will never be supported, as this is a bare metal framework with
122 // no capabilities for multi-process execution. While F_DUPFD is also
123 // not supported yet, it might be (currently it returns ENOSYS).
124#[cfg(any(target_os = "espidf", target_os = "vita"))]
125let cmd = libc::F_DUPFD;
126127// Avoid using file descriptors below 3 as they are used for stdio
128let fd = cvt(unsafe { libc::fcntl(self.as_raw_fd(), cmd, 3) })?;
129Ok(unsafe { OwnedFd::from_raw_fd(fd) })
130 }
131132/// Creates a new `OwnedFd` instance that shares the same underlying file
133 /// description as the existing `BorrowedFd` instance.
134#[cfg(any(target_arch = "wasm32", target_os = "hermit", target_os = "trusty"))]
135 #[stable(feature = "io_safety", since = "1.63.0")]
136pub fn try_clone_to_owned(&self) -> io::Result<OwnedFd> {
137Err(io::Error::UNSUPPORTED_PLATFORM)
138 }
139140/// Creates a new `OwnedFd` instance that shares the same underlying file
141 /// description as the existing `BorrowedFd` instance.
142#[cfg(target_os = "motor")]
143 #[stable(feature = "io_safety", since = "1.63.0")]
144pub fn try_clone_to_owned(&self) -> io::Result<OwnedFd> {
145let fd = moto_rt::fs::duplicate(self.as_raw_fd()).map_err(crate::sys::map_motor_error)?;
146Ok(unsafe { OwnedFd::from_raw_fd(fd) })
147 }
148}
149150#[stable(feature = "io_safety", since = "1.63.0")]
151impl AsRawFdfor BorrowedFd<'_> {
152#[inline]
153fn as_raw_fd(&self) -> RawFd {
154self.fd.as_inner()
155 }
156}
157158#[stable(feature = "io_safety", since = "1.63.0")]
159impl AsRawFdfor OwnedFd {
160#[inline]
161fn as_raw_fd(&self) -> RawFd {
162self.fd.as_inner()
163 }
164}
165166#[stable(feature = "io_safety", since = "1.63.0")]
167impl IntoRawFdfor OwnedFd {
168#[inline]
169fn into_raw_fd(self) -> RawFd {
170ManuallyDrop::new(self).fd.as_inner()
171 }
172}
173174#[stable(feature = "io_safety", since = "1.63.0")]
175impl FromRawFdfor OwnedFd {
176/// Constructs a new instance of `Self` from the given raw file descriptor.
177 ///
178 /// # Safety
179 ///
180 /// The resource pointed to by `fd` must be open and suitable for assuming
181 /// [ownership][io-safety]. The resource must not require any cleanup other than `close`.
182 ///
183 /// [io-safety]: io#io-safety
184 ///
185 /// # Panics
186 ///
187 /// Panics if the raw file descriptor has the value `-1`.
188#[inline]
189 #[track_caller]
190unsafe fn from_raw_fd(fd: RawFd) -> Self {
191Self { fd: ValidRawFd::new(fd).expect("fd != -1") }
192 }
193}
194195#[stable(feature = "io_safety", since = "1.63.0")]
196impl Dropfor OwnedFd {
197#[inline]
198fn drop(&mut self) {
199unsafe {
200// Note that errors are ignored when closing a file descriptor. According to POSIX 2024,
201 // we can and indeed should retry `close` on `EINTR`
202 // (https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/close.html),
203 // but it is not clear yet how well widely-used implementations are conforming with this
204 // mandate since older versions of POSIX left the state of the FD after an `EINTR`
205 // unspecified. Ignoring errors is "fine" because some of the major Unices (in
206 // particular, Linux) do make sure to always close the FD, even when `close()` is
207 // interrupted, and the scenario is rare to begin with. If we retried on a
208 // not-POSIX-compliant implementation, the consequences could be really bad since we may
209 // close the wrong FD. Helpful link to an epic discussion by POSIX workgroup that led to
210 // the latest POSIX wording: http://austingroupbugs.net/view.php?id=529
211#[cfg(not(target_os = "hermit"))]
212{
213#[cfg(unix)]
214crate::sys::fs::debug_assert_fd_is_open(self.fd.as_inner());
215216let _ = libc::close(self.fd.as_inner());
217 }
218#[cfg(target_os = "hermit")]
219let _ = hermit_abi::close(self.fd.as_inner());
220 }
221 }
222}
223224#[stable(feature = "io_safety", since = "1.63.0")]
225impl fmt::Debugfor BorrowedFd<'_> {
226fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
227f.debug_struct("BorrowedFd").field("fd", &self.fd).finish()
228 }
229}
230231#[stable(feature = "io_safety", since = "1.63.0")]
232impl fmt::Debugfor OwnedFd {
233fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234f.debug_struct("OwnedFd").field("fd", &self.fd).finish()
235 }
236}
237238macro_rules!impl_is_terminal {
239 ($($t:ty),*$(,)?) => {$(
240#[unstable(feature = "sealed", issue = "none")]
241impl crate::sealed::Sealed for $t {}
242243#[stable(feature = "is_terminal", since = "1.70.0")]
244impl io::IsTerminal for $t {
245#[inline]
246fn is_terminal(&self) -> bool {
247crate::sys::io::is_terminal(self)
248 }
249 }
250 )*}
251}
252253#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for OwnedFd { }
#[stable(feature = "is_terminal", since = "1.70.0")]
impl io::IsTerminal for OwnedFd {
#[inline]
fn is_terminal(&self) -> bool { crate::sys::io::is_terminal(self) }
}impl_is_terminal!(BorrowedFd<'_>, OwnedFd);
254255/// A trait to borrow the file descriptor from an underlying object.
256///
257/// This is only available on unix platforms and must be imported in order to
258/// call the method. Windows platforms have a corresponding `AsHandle` and
259/// `AsSocket` set of traits.
260#[stable(feature = "io_safety", since = "1.63.0")]
261pub trait AsFd {
262/// Borrows the file descriptor.
263 ///
264 /// # Example
265 ///
266 /// ```rust,no_run
267 /// use std::fs::File;
268 /// # use std::io;
269 /// # #[cfg(any(unix, target_os = "wasi"))]
270 /// # use std::os::fd::{AsFd, BorrowedFd};
271 ///
272 /// let mut f = File::open("foo.txt")?;
273 /// # #[cfg(any(unix, target_os = "wasi"))]
274 /// let borrowed_fd: BorrowedFd<'_> = f.as_fd();
275 /// # Ok::<(), io::Error>(())
276 /// ```
277#[stable(feature = "io_safety", since = "1.63.0")]
278fn as_fd(&self) -> BorrowedFd<'_>;
279}
280281#[stable(feature = "io_safety", since = "1.63.0")]
282impl<T: AsFd + ?Sized> AsFdfor &T {
283#[inline]
284fn as_fd(&self) -> BorrowedFd<'_> {
285 T::as_fd(self)
286 }
287}
288289#[stable(feature = "io_safety", since = "1.63.0")]
290impl<T: AsFd + ?Sized> AsFdfor &mut T {
291#[inline]
292fn as_fd(&self) -> BorrowedFd<'_> {
293 T::as_fd(self)
294 }
295}
296297#[stable(feature = "io_safety", since = "1.63.0")]
298impl AsFdfor BorrowedFd<'_> {
299#[inline]
300fn as_fd(&self) -> BorrowedFd<'_> {
301*self302 }
303}
304305#[stable(feature = "io_safety", since = "1.63.0")]
306impl AsFdfor OwnedFd {
307#[inline]
308fn as_fd(&self) -> BorrowedFd<'_> {
309// Safety: `OwnedFd` and `BorrowedFd` have the same validity
310 // invariants, and the `BorrowedFd` is bounded by the lifetime
311 // of `&self`.
312unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
313 }
314}
315316#[stable(feature = "io_safety", since = "1.63.0")]
317#[cfg(not(target_os = "trusty"))]
318impl AsFdfor fs::File {
319#[inline]
320fn as_fd(&self) -> BorrowedFd<'_> {
321self.as_inner().as_fd()
322 }
323}
324325#[stable(feature = "io_safety", since = "1.63.0")]
326#[cfg(not(target_os = "trusty"))]
327impl From<fs::File> for OwnedFd {
328/// Takes ownership of a [`File`](fs::File)'s underlying file descriptor.
329#[inline]
330fn from(file: fs::File) -> OwnedFd {
331file.into_inner().into_inner().into_inner()
332 }
333}
334335#[stable(feature = "io_safety", since = "1.63.0")]
336#[cfg(not(target_os = "trusty"))]
337impl From<OwnedFd> for fs::File {
338/// Returns a [`File`](fs::File) that takes ownership of the given
339 /// file descriptor.
340#[inline]
341fn from(owned_fd: OwnedFd) -> Self {
342Self::from_inner(FromInner::from_inner(FromInner::from_inner(owned_fd)))
343 }
344}
345346#[stable(feature = "io_safety", since = "1.63.0")]
347#[cfg(not(target_os = "trusty"))]
348impl AsFdfor crate::net::TcpStream {
349#[inline]
350fn as_fd(&self) -> BorrowedFd<'_> {
351self.as_inner().socket().as_fd()
352 }
353}
354355#[stable(feature = "io_safety", since = "1.63.0")]
356#[cfg(not(target_os = "trusty"))]
357impl From<crate::net::TcpStream> for OwnedFd {
358/// Takes ownership of a [`TcpStream`](crate::net::TcpStream)'s socket file descriptor.
359#[inline]
360fn from(tcp_stream: crate::net::TcpStream) -> OwnedFd {
361tcp_stream.into_inner().into_socket().into_inner().into_inner().into()
362 }
363}
364365#[stable(feature = "io_safety", since = "1.63.0")]
366#[cfg(not(target_os = "trusty"))]
367impl From<OwnedFd> for crate::net::TcpStream {
368#[inline]
369fn from(owned_fd: OwnedFd) -> Self {
370Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner(
371owned_fd,
372 ))))
373 }
374}
375376#[stable(feature = "io_safety", since = "1.63.0")]
377#[cfg(not(target_os = "trusty"))]
378impl AsFdfor crate::net::TcpListener {
379#[inline]
380fn as_fd(&self) -> BorrowedFd<'_> {
381self.as_inner().socket().as_fd()
382 }
383}
384385#[stable(feature = "io_safety", since = "1.63.0")]
386#[cfg(not(target_os = "trusty"))]
387impl From<crate::net::TcpListener> for OwnedFd {
388/// Takes ownership of a [`TcpListener`](crate::net::TcpListener)'s socket file descriptor.
389#[inline]
390fn from(tcp_listener: crate::net::TcpListener) -> OwnedFd {
391tcp_listener.into_inner().into_socket().into_inner().into_inner().into()
392 }
393}
394395#[stable(feature = "io_safety", since = "1.63.0")]
396#[cfg(not(target_os = "trusty"))]
397impl From<OwnedFd> for crate::net::TcpListener {
398#[inline]
399fn from(owned_fd: OwnedFd) -> Self {
400Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner(
401owned_fd,
402 ))))
403 }
404}
405406#[stable(feature = "io_safety", since = "1.63.0")]
407#[cfg(not(target_os = "trusty"))]
408impl AsFdfor crate::net::UdpSocket {
409#[inline]
410fn as_fd(&self) -> BorrowedFd<'_> {
411self.as_inner().socket().as_fd()
412 }
413}
414415#[stable(feature = "io_safety", since = "1.63.0")]
416#[cfg(not(target_os = "trusty"))]
417impl From<crate::net::UdpSocket> for OwnedFd {
418/// Takes ownership of a [`UdpSocket`](crate::net::UdpSocket)'s file descriptor.
419#[inline]
420fn from(udp_socket: crate::net::UdpSocket) -> OwnedFd {
421udp_socket.into_inner().into_socket().into_inner().into_inner().into()
422 }
423}
424425#[stable(feature = "io_safety", since = "1.63.0")]
426#[cfg(not(target_os = "trusty"))]
427impl From<OwnedFd> for crate::net::UdpSocket {
428#[inline]
429fn from(owned_fd: OwnedFd) -> Self {
430Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner(
431owned_fd,
432 ))))
433 }
434}
435436#[stable(feature = "asfd_ptrs", since = "1.64.0")]
437/// This impl allows implementing traits that require `AsFd` on Arc.
438/// ```
439/// # #[cfg(any(unix, target_os = "wasi"))] mod group_cfg {
440/// # #[cfg(target_os = "wasi")]
441/// # use std::os::wasi::io::AsFd;
442/// # #[cfg(unix)]
443/// # use std::os::unix::io::AsFd;
444/// use std::net::UdpSocket;
445/// use std::sync::Arc;
446///
447/// trait MyTrait: AsFd {}
448/// impl MyTrait for Arc<UdpSocket> {}
449/// impl MyTrait for Box<UdpSocket> {}
450/// # }
451/// ```
452impl<T: AsFd + ?Sized> AsFdfor crate::sync::Arc<T> {
453#[inline]
454fn as_fd(&self) -> BorrowedFd<'_> {
455 (**self).as_fd()
456 }
457}
458459#[stable(feature = "asfd_rc", since = "1.69.0")]
460impl<T: AsFd + ?Sized> AsFdfor crate::rc::Rc<T> {
461#[inline]
462fn as_fd(&self) -> BorrowedFd<'_> {
463 (**self).as_fd()
464 }
465}
466467#[unstable(feature = "unique_rc_arc", issue = "112566")]
468impl<T: AsFd + ?Sized> AsFdfor crate::rc::UniqueRc<T> {
469#[inline]
470fn as_fd(&self) -> BorrowedFd<'_> {
471 (**self).as_fd()
472 }
473}
474475#[stable(feature = "asfd_ptrs", since = "1.64.0")]
476impl<T: AsFd + ?Sized> AsFdfor Box<T> {
477#[inline]
478fn as_fd(&self) -> BorrowedFd<'_> {
479 (**self).as_fd()
480 }
481}
482483#[stable(feature = "io_safety", since = "1.63.0")]
484impl AsFdfor io::Stdin {
485#[inline]
486fn as_fd(&self) -> BorrowedFd<'_> {
487unsafe { BorrowedFd::borrow_raw(0) }
488 }
489}
490491#[stable(feature = "io_safety", since = "1.63.0")]
492impl<'a> AsFdfor io::StdinLock<'a> {
493#[inline]
494fn as_fd(&self) -> BorrowedFd<'_> {
495// SAFETY: user code should not close stdin out from under the standard library
496unsafe { BorrowedFd::borrow_raw(0) }
497 }
498}
499500#[stable(feature = "io_safety", since = "1.63.0")]
501impl AsFdfor io::Stdout {
502#[inline]
503fn as_fd(&self) -> BorrowedFd<'_> {
504unsafe { BorrowedFd::borrow_raw(1) }
505 }
506}
507508#[stable(feature = "io_safety", since = "1.63.0")]
509impl<'a> AsFdfor io::StdoutLock<'a> {
510#[inline]
511fn as_fd(&self) -> BorrowedFd<'_> {
512// SAFETY: user code should not close stdout out from under the standard library
513unsafe { BorrowedFd::borrow_raw(1) }
514 }
515}
516517#[stable(feature = "io_safety", since = "1.63.0")]
518impl AsFdfor io::Stderr {
519#[inline]
520fn as_fd(&self) -> BorrowedFd<'_> {
521unsafe { BorrowedFd::borrow_raw(2) }
522 }
523}
524525#[stable(feature = "io_safety", since = "1.63.0")]
526impl<'a> AsFdfor io::StderrLock<'a> {
527#[inline]
528fn as_fd(&self) -> BorrowedFd<'_> {
529// SAFETY: user code should not close stderr out from under the standard library
530unsafe { BorrowedFd::borrow_raw(2) }
531 }
532}
533534#[stable(feature = "anonymous_pipe", since = "1.87.0")]
535#[cfg(not(target_os = "trusty"))]
536impl AsFdfor io::PipeReader {
537fn as_fd(&self) -> BorrowedFd<'_> {
538self.0.as_fd()
539 }
540}
541542#[stable(feature = "anonymous_pipe", since = "1.87.0")]
543#[cfg(not(target_os = "trusty"))]
544impl From<io::PipeReader> for OwnedFd {
545fn from(pipe: io::PipeReader) -> Self {
546pipe.0.into_inner()
547 }
548}
549550#[stable(feature = "anonymous_pipe", since = "1.87.0")]
551#[cfg(not(target_os = "trusty"))]
552impl AsFdfor io::PipeWriter {
553fn as_fd(&self) -> BorrowedFd<'_> {
554self.0.as_fd()
555 }
556}
557558#[stable(feature = "anonymous_pipe", since = "1.87.0")]
559#[cfg(not(target_os = "trusty"))]
560impl From<io::PipeWriter> for OwnedFd {
561fn from(pipe: io::PipeWriter) -> Self {
562pipe.0.into_inner()
563 }
564}
565566#[stable(feature = "anonymous_pipe", since = "1.87.0")]
567#[cfg(not(target_os = "trusty"))]
568impl From<OwnedFd> for io::PipeReader {
569fn from(owned_fd: OwnedFd) -> Self {
570Self(FromInner::from_inner(owned_fd))
571 }
572}
573574#[stable(feature = "anonymous_pipe", since = "1.87.0")]
575#[cfg(not(target_os = "trusty"))]
576impl From<OwnedFd> for io::PipeWriter {
577fn from(owned_fd: OwnedFd) -> Self {
578Self(FromInner::from_inner(owned_fd))
579 }
580}