1 // Copyright 2020 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use std::fs::File; 6 use std::io::ErrorKind as IoErrorKind; 7 use std::os::fd::AsFd; 8 use std::os::fd::BorrowedFd; 9 use std::os::fd::OwnedFd; 10 use std::os::unix::io::AsRawFd; 11 use std::os::unix::io::FromRawFd; 12 use std::os::unix::io::IntoRawFd; 13 use std::os::unix::io::RawFd; 14 15 use crate::rutabaga_os::descriptor::AsRawDescriptor; 16 use crate::rutabaga_os::descriptor::FromRawDescriptor; 17 use crate::rutabaga_os::descriptor::IntoRawDescriptor; 18 use crate::rutabaga_os::DescriptorType; 19 20 pub type RawDescriptor = RawFd; 21 pub const DEFAULT_RAW_DESCRIPTOR: RawDescriptor = -1; 22 23 type Error = std::io::Error; 24 type Result<T> = std::result::Result<T, Error>; 25 26 pub struct OwnedDescriptor { 27 owned: OwnedFd, 28 } 29 30 impl OwnedDescriptor { try_clone(&self) -> Result<OwnedDescriptor>31 pub fn try_clone(&self) -> Result<OwnedDescriptor> { 32 let clone = self.owned.try_clone()?; 33 Ok(OwnedDescriptor { owned: clone }) 34 } 35 determine_type(&self) -> Result<DescriptorType>36 pub fn determine_type(&self) -> Result<DescriptorType> { 37 Err(Error::from(IoErrorKind::Unsupported)) 38 } 39 } 40 41 impl AsRawDescriptor for OwnedDescriptor { as_raw_descriptor(&self) -> RawDescriptor42 fn as_raw_descriptor(&self) -> RawDescriptor { 43 self.owned.as_raw_fd() 44 } 45 } 46 47 impl FromRawDescriptor for OwnedDescriptor { 48 // SAFETY: 49 // It is caller's responsibility to ensure that the descriptor is valid and 50 // stays valid for the lifetime of Self from_raw_descriptor(descriptor: RawDescriptor) -> Self51 unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self { 52 OwnedDescriptor { 53 owned: OwnedFd::from_raw_fd(descriptor), 54 } 55 } 56 } 57 58 impl IntoRawDescriptor for OwnedDescriptor { into_raw_descriptor(self) -> RawDescriptor59 fn into_raw_descriptor(self) -> RawDescriptor { 60 self.owned.into_raw_fd() 61 } 62 } 63 64 impl AsFd for OwnedDescriptor { as_fd(&self) -> BorrowedFd<'_>65 fn as_fd(&self) -> BorrowedFd<'_> { 66 self.owned.as_fd() 67 } 68 } 69 70 impl AsRawDescriptor for File { as_raw_descriptor(&self) -> RawDescriptor71 fn as_raw_descriptor(&self) -> RawDescriptor { 72 self.as_raw_fd() 73 } 74 } 75 76 impl FromRawDescriptor for File { 77 // SAFETY: 78 // It is caller's responsibility to ensure that the descriptor is valid and 79 // stays valid for the lifetime of Self from_raw_descriptor(descriptor: RawDescriptor) -> Self80 unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self { 81 File::from_raw_fd(descriptor) 82 } 83 } 84 85 impl IntoRawDescriptor for File { into_raw_descriptor(self) -> RawDescriptor86 fn into_raw_descriptor(self) -> RawDescriptor { 87 self.into_raw_fd() 88 } 89 } 90 91 impl From<File> for OwnedDescriptor { from(f: File) -> OwnedDescriptor92 fn from(f: File) -> OwnedDescriptor { 93 OwnedDescriptor { owned: f.into() } 94 } 95 } 96