|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! SGX-specific extensions to general I/O primitives |
| 12 | +//! |
| 13 | +//! SGX file descriptors behave differently from Unix file descriptors. See the |
| 14 | +//! description of [`TryIntoRawFd`](trait.TryIntoRawFd.html) for more details. |
| 15 | +#![unstable(feature = "sgx_platform", issue = "56975")] |
| 16 | + |
| 17 | +pub use sys::abi::usercalls::raw::Fd as RawFd; |
| 18 | +use net; |
| 19 | +use sys::{self, AsInner, FromInner, IntoInner, TryIntoInner}; |
| 20 | + |
| 21 | +/// A trait to extract the raw SGX file descriptor from an underlying |
| 22 | +/// object. |
| 23 | +#[unstable(feature = "sgx_platform", issue = "56975")] |
| 24 | +pub trait AsRawFd { |
| 25 | + /// Extracts the raw file descriptor. |
| 26 | + /// |
| 27 | + /// This method does **not** pass ownership of the raw file descriptor |
| 28 | + /// to the caller. The descriptor is only guaranteed to be valid while |
| 29 | + /// the original object has not yet been destroyed. |
| 30 | + #[unstable(feature = "sgx_platform", issue = "56975")] |
| 31 | + fn as_raw_fd(&self) -> RawFd; |
| 32 | +} |
| 33 | + |
| 34 | +/// A trait to express the ability to construct an object from a raw file |
| 35 | +/// descriptor. |
| 36 | +#[unstable(feature = "sgx_platform", issue = "56975")] |
| 37 | +pub trait FromRawFd { |
| 38 | + /// Constructs a new instance of `Self` from the given raw file |
| 39 | + /// descriptor. |
| 40 | + /// |
| 41 | + /// This function **consumes ownership** of the specified file |
| 42 | + /// descriptor. The returned object will take responsibility for closing |
| 43 | + /// it when the object goes out of scope. |
| 44 | + /// |
| 45 | + /// This function is also unsafe as the primitives currently returned |
| 46 | + /// have the contract that they are the sole owner of the file |
| 47 | + /// descriptor they are wrapping. Usage of this function could |
| 48 | + /// accidentally allow violating this contract which can cause memory |
| 49 | + /// unsafety in code that relies on it being true. |
| 50 | + #[unstable(feature = "sgx_platform", issue = "56975")] |
| 51 | + unsafe fn from_raw_fd(fd: RawFd) -> Self; |
| 52 | +} |
| 53 | + |
| 54 | +/// A trait to express the ability to consume an object and acquire ownership of |
| 55 | +/// its raw file descriptor. |
| 56 | +#[unstable(feature = "sgx_platform", issue = "56975")] |
| 57 | +pub trait TryIntoRawFd: Sized { |
| 58 | + /// Consumes this object, returning the raw underlying file descriptor, if |
| 59 | + /// this object is not cloned. |
| 60 | + /// |
| 61 | + /// This function **transfers ownership** of the underlying file descriptor |
| 62 | + /// to the caller. Callers are then the unique owners of the file descriptor |
| 63 | + /// and must close the descriptor once it's no longer needed. |
| 64 | + /// |
| 65 | + /// Unlike other platforms, on SGX, the file descriptor is shared between |
| 66 | + /// all clones of an object. To avoid race conditions, this function will |
| 67 | + /// only return `Ok` when called on the final clone. |
| 68 | + #[unstable(feature = "sgx_platform", issue = "56975")] |
| 69 | + fn try_into_raw_fd(self) -> Result<RawFd, Self>; |
| 70 | +} |
| 71 | + |
| 72 | +impl AsRawFd for net::TcpStream { |
| 73 | + fn as_raw_fd(&self) -> RawFd { *self.as_inner().as_inner().as_inner().as_inner() } |
| 74 | +} |
| 75 | + |
| 76 | +impl AsRawFd for net::TcpListener { |
| 77 | + fn as_raw_fd(&self) -> RawFd { *self.as_inner().as_inner().as_inner().as_inner() } |
| 78 | +} |
| 79 | + |
| 80 | +impl FromRawFd for net::TcpStream { |
| 81 | + unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream { |
| 82 | + let fd = sys::fd::FileDesc::from_inner(fd); |
| 83 | + let socket = sys::net::Socket::from_inner(fd); |
| 84 | + net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, None))) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl FromRawFd for net::TcpListener { |
| 89 | + unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener { |
| 90 | + let fd = sys::fd::FileDesc::from_inner(fd); |
| 91 | + let socket = sys::net::Socket::from_inner(fd); |
| 92 | + net::TcpListener::from_inner(sys::net::TcpListener::from_inner(socket)) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl TryIntoRawFd for net::TcpStream { |
| 97 | + fn try_into_raw_fd(self) -> Result<RawFd, Self> { |
| 98 | + let (socket, peer_addr) = self.into_inner().into_inner(); |
| 99 | + match socket.try_into_inner() { |
| 100 | + Ok(fd) => Ok(fd.into_inner()), |
| 101 | + Err(socket) => { |
| 102 | + let sys = sys::net::TcpStream::from_inner((socket, peer_addr)); |
| 103 | + Err(net::TcpStream::from_inner(sys)) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl TryIntoRawFd for net::TcpListener { |
| 110 | + fn try_into_raw_fd(self) -> Result<RawFd, Self> { |
| 111 | + match self.into_inner().into_inner().try_into_inner() { |
| 112 | + Ok(fd) => Ok(fd.into_inner()), |
| 113 | + Err(socket) => { |
| 114 | + let sys = sys::net::TcpListener::from_inner(socket); |
| 115 | + Err(net::TcpListener::from_inner(sys)) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments