| 1 | //! The unsafe `close` for raw file descriptors. |
| 2 | //! |
| 3 | //! # Safety |
| 4 | //! |
| 5 | //! Operating on raw file descriptors is unsafe. |
| 6 | #![allow (unsafe_code)] |
| 7 | |
| 8 | use crate::backend; |
| 9 | use backend::fd::RawFd; |
| 10 | |
| 11 | /// `close(raw_fd)`—Closes a `RawFd` directly. |
| 12 | /// |
| 13 | /// Most users won't need to use this, as `OwnedFd` automatically closes its |
| 14 | /// file descriptor on `Drop`. |
| 15 | /// |
| 16 | /// This function does not return a `Result`, as it is the [responsibility] of |
| 17 | /// filesystem designers to not return errors from `close`. Users who chose to |
| 18 | /// use NFS or similar filesystems should take care to monitor for problems |
| 19 | /// externally. |
| 20 | /// |
| 21 | /// [responsibility]: https://lwn.net/Articles/576518/ |
| 22 | /// |
| 23 | /// # References |
| 24 | /// - [Beej's Guide to Network Programming] |
| 25 | /// - [POSIX] |
| 26 | /// - [Linux] |
| 27 | /// - [Apple] |
| 28 | /// - [Winsock] |
| 29 | /// - [FreeBSD] |
| 30 | /// - [NetBSD] |
| 31 | /// - [OpenBSD] |
| 32 | /// - [DragonFly BSD] |
| 33 | /// - [illumos] |
| 34 | /// - [glibc] |
| 35 | /// |
| 36 | /// [Beej's Guide to Network Programming]: https://beej.us/guide/bgnet/html/split/system-calls-or-bust.html#close-and-shutdownget-outta-my-face |
| 37 | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/close.html |
| 38 | /// [Linux]: https://man7.org/linux/man-pages/man2/close.2.html |
| 39 | /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/close.2.html#//apple_ref/doc/man/2/close |
| 40 | /// [Winsock]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-closesocket |
| 41 | /// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=close&sektion=2 |
| 42 | /// [NetBSD]: https://man.netbsd.org/close.2 |
| 43 | /// [OpenBSD]: https://man.openbsd.org/close.2 |
| 44 | /// [DragonFly BSD]: https://man.dragonflybsd.org/?command=close§ion=2 |
| 45 | /// [illumos]: https://illumos.org/man/2/close |
| 46 | /// [glibc]: https://sourceware.org/glibc/manual/latest/html_node/Opening-and-Closing-Files.html#index-close |
| 47 | /// |
| 48 | /// # Safety |
| 49 | /// |
| 50 | /// This function takes a `RawFd`, which must be valid before the call, and is |
| 51 | /// not valid after the call. |
| 52 | #[inline ] |
| 53 | pub unsafe fn close(raw_fd: RawFd) { |
| 54 | backend::io::syscalls::close(raw_fd) |
| 55 | } |
| 56 | |
| 57 | /// `close(raw_fd)`—Closes a `RawFd` directly, and report any errors |
| 58 | /// returned by the OS. |
| 59 | /// |
| 60 | /// The rustix developers do not intend the existence of this feature to imply |
| 61 | /// that anyone should use it. |
| 62 | #[cfg (feature = "try_close" )] |
| 63 | pub unsafe fn try_close(raw_fd: RawFd) -> crate::io::Result<()> { |
| 64 | backend::io::syscalls::try_close(raw_fd) |
| 65 | } |
| 66 | |