xref: /aosp_15_r20/external/crosvm/base/src/sys/macos/mod.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2023 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 
7 use crate::descriptor::FromRawDescriptor;
8 use crate::sys::unix::RawDescriptor;
9 use crate::unix::set_descriptor_cloexec;
10 use crate::unix::Pid;
11 use crate::MmapError;
12 
13 mod event;
14 pub(in crate::sys::macos) mod kqueue;
15 mod net;
16 mod timer;
17 
18 pub(crate) use event::PlatformEvent;
19 pub(in crate::sys) use libc::sendmsg;
20 pub(in crate::sys) use net::sockaddr_un;
21 pub(in crate::sys) use net::sockaddrv4_to_lib_c;
22 pub(in crate::sys) use net::sockaddrv6_to_lib_c;
23 
set_thread_name(_name: &str) -> crate::errno::Result<()>24 pub fn set_thread_name(_name: &str) -> crate::errno::Result<()> {
25     todo!();
26 }
27 
get_cpu_affinity() -> crate::errno::Result<Vec<usize>>28 pub fn get_cpu_affinity() -> crate::errno::Result<Vec<usize>> {
29     todo!();
30 }
31 
getpid() -> Pid32 pub fn getpid() -> Pid {
33     todo!();
34 }
35 
open_file_or_duplicate<P: AsRef<std::path::Path>>( _path: P, _options: &std::fs::OpenOptions, ) -> crate::Result<std::fs::File>36 pub fn open_file_or_duplicate<P: AsRef<std::path::Path>>(
37     _path: P,
38     _options: &std::fs::OpenOptions,
39 ) -> crate::Result<std::fs::File> {
40     todo!();
41 }
42 
43 pub mod platform_timer_resolution {
44     pub struct UnixSetTimerResolution {}
45     impl crate::EnabledHighResTimer for UnixSetTimerResolution {}
46 
enable_high_res_timers() -> crate::Result<Box<dyn crate::EnabledHighResTimer>>47     pub fn enable_high_res_timers() -> crate::Result<Box<dyn crate::EnabledHighResTimer>> {
48         todo!();
49     }
50 }
51 
set_cpu_affinity<I: IntoIterator<Item = usize>>(_cpus: I) -> crate::errno::Result<()>52 pub fn set_cpu_affinity<I: IntoIterator<Item = usize>>(_cpus: I) -> crate::errno::Result<()> {
53     todo!();
54 }
55 
56 pub struct EventContext<T: crate::EventToken> {
57     p: std::marker::PhantomData<T>,
58 }
59 
60 impl<T: crate::EventToken> EventContext<T> {
new() -> crate::errno::Result<EventContext<T>>61     pub fn new() -> crate::errno::Result<EventContext<T>> {
62         todo!();
63     }
build_with( _fd_tokens: &[(&dyn crate::AsRawDescriptor, T)], ) -> crate::errno::Result<EventContext<T>>64     pub fn build_with(
65         _fd_tokens: &[(&dyn crate::AsRawDescriptor, T)],
66     ) -> crate::errno::Result<EventContext<T>> {
67         todo!();
68     }
add_for_event( &self, _descriptor: &dyn crate::AsRawDescriptor, _event_type: crate::EventType, _token: T, ) -> crate::errno::Result<()>69     pub fn add_for_event(
70         &self,
71         _descriptor: &dyn crate::AsRawDescriptor,
72         _event_type: crate::EventType,
73         _token: T,
74     ) -> crate::errno::Result<()> {
75         todo!();
76     }
modify( &self, _fd: &dyn crate::AsRawDescriptor, _event_type: crate::EventType, _token: T, ) -> crate::errno::Result<()>77     pub fn modify(
78         &self,
79         _fd: &dyn crate::AsRawDescriptor,
80         _event_type: crate::EventType,
81         _token: T,
82     ) -> crate::errno::Result<()> {
83         todo!();
84     }
delete(&self, _fd: &dyn crate::AsRawDescriptor) -> crate::errno::Result<()>85     pub fn delete(&self, _fd: &dyn crate::AsRawDescriptor) -> crate::errno::Result<()> {
86         todo!();
87     }
wait(&self) -> crate::errno::Result<smallvec::SmallVec<[crate::TriggeredEvent<T>; 16]>>88     pub fn wait(&self) -> crate::errno::Result<smallvec::SmallVec<[crate::TriggeredEvent<T>; 16]>> {
89         todo!();
90     }
wait_timeout( &self, _timeout: std::time::Duration, ) -> crate::errno::Result<smallvec::SmallVec<[crate::TriggeredEvent<T>; 16]>>91     pub fn wait_timeout(
92         &self,
93         _timeout: std::time::Duration,
94     ) -> crate::errno::Result<smallvec::SmallVec<[crate::TriggeredEvent<T>; 16]>> {
95         todo!();
96     }
97 }
98 
99 impl<T: crate::EventToken> crate::AsRawDescriptor for EventContext<T> {
as_raw_descriptor(&self) -> RawDescriptor100     fn as_raw_descriptor(&self) -> RawDescriptor {
101         todo!();
102     }
103 }
104 
105 pub struct MemoryMappingArena {}
106 
107 #[derive(Debug)]
108 pub struct MemoryMapping {}
109 
110 impl MemoryMapping {
size(&self) -> usize111     pub fn size(&self) -> usize {
112         todo!();
113     }
range_end(&self, _offset: usize, _count: usize) -> Result<usize, MmapError>114     pub(crate) fn range_end(&self, _offset: usize, _count: usize) -> Result<usize, MmapError> {
115         todo!();
116     }
msync(&self) -> Result<(), MmapError>117     pub fn msync(&self) -> Result<(), MmapError> {
118         todo!();
119     }
new_protection_fixed( _addr: *mut u8, _size: usize, _prot: crate::Protection, ) -> Result<MemoryMapping, MmapError>120     pub fn new_protection_fixed(
121         _addr: *mut u8,
122         _size: usize,
123         _prot: crate::Protection,
124     ) -> Result<MemoryMapping, MmapError> {
125         todo!();
126     }
127     /// # Safety
128     ///
129     /// unimplemented, always aborts
from_descriptor_offset_protection_fixed( _addr: *mut u8, _fd: &dyn crate::AsRawDescriptor, _size: usize, _offset: u64, _prot: crate::Protection, ) -> Result<MemoryMapping, MmapError>130     pub unsafe fn from_descriptor_offset_protection_fixed(
131         _addr: *mut u8,
132         _fd: &dyn crate::AsRawDescriptor,
133         _size: usize,
134         _offset: u64,
135         _prot: crate::Protection,
136     ) -> Result<MemoryMapping, MmapError> {
137         todo!();
138     }
139 }
140 
141 // SAFETY: Unimplemented, always aborts
142 unsafe impl crate::MappedRegion for MemoryMapping {
as_ptr(&self) -> *mut u8143     fn as_ptr(&self) -> *mut u8 {
144         todo!();
145     }
size(&self) -> usize146     fn size(&self) -> usize {
147         todo!();
148     }
149 }
150 
151 pub mod ioctl {
152     pub type IoctlNr = std::ffi::c_ulong;
153     /// # Safety
154     ///
155     /// unimplemented, always aborts
ioctl<F: crate::AsRawDescriptor>( _descriptor: &F, _nr: IoctlNr, ) -> std::ffi::c_int156     pub unsafe fn ioctl<F: crate::AsRawDescriptor>(
157         _descriptor: &F,
158         _nr: IoctlNr,
159     ) -> std::ffi::c_int {
160         todo!();
161     }
162     /// # Safety
163     ///
164     /// unimplemented, always aborts
ioctl_with_val( _descriptor: &dyn crate::AsRawDescriptor, _nr: IoctlNr, _arg: std::ffi::c_ulong, ) -> std::ffi::c_int165     pub unsafe fn ioctl_with_val(
166         _descriptor: &dyn crate::AsRawDescriptor,
167         _nr: IoctlNr,
168         _arg: std::ffi::c_ulong,
169     ) -> std::ffi::c_int {
170         todo!();
171     }
172     /// # Safety
173     ///
174     /// unimplemented, always aborts
ioctl_with_ref<T>( _descriptor: &dyn crate::AsRawDescriptor, _nr: IoctlNr, _arg: &T, ) -> std::ffi::c_int175     pub unsafe fn ioctl_with_ref<T>(
176         _descriptor: &dyn crate::AsRawDescriptor,
177         _nr: IoctlNr,
178         _arg: &T,
179     ) -> std::ffi::c_int {
180         todo!();
181     }
182     /// # Safety
183     ///
184     /// unimplemented, always aborts
ioctl_with_mut_ref<T>( _descriptor: &dyn crate::AsRawDescriptor, _nr: IoctlNr, _arg: &mut T, ) -> std::ffi::c_int185     pub unsafe fn ioctl_with_mut_ref<T>(
186         _descriptor: &dyn crate::AsRawDescriptor,
187         _nr: IoctlNr,
188         _arg: &mut T,
189     ) -> std::ffi::c_int {
190         todo!();
191     }
192     /// # Safety
193     ///
194     /// unimplemented, always aborts
ioctl_with_ptr<T>( _descriptor: &dyn crate::AsRawDescriptor, _nr: IoctlNr, _arg: *const T, ) -> std::ffi::c_int195     pub unsafe fn ioctl_with_ptr<T>(
196         _descriptor: &dyn crate::AsRawDescriptor,
197         _nr: IoctlNr,
198         _arg: *const T,
199     ) -> std::ffi::c_int {
200         todo!();
201     }
202     /// # Safety
203     ///
204     /// unimplemented, always aborts
ioctl_with_mut_ptr<T>( _descriptor: &dyn crate::AsRawDescriptor, _nr: IoctlNr, _arg: *mut T, ) -> std::ffi::c_int205     pub unsafe fn ioctl_with_mut_ptr<T>(
206         _descriptor: &dyn crate::AsRawDescriptor,
207         _nr: IoctlNr,
208         _arg: *mut T,
209     ) -> std::ffi::c_int {
210         todo!();
211     }
212 }
213 
file_punch_hole(_file: &std::fs::File, _offset: u64, _length: u64) -> std::io::Result<()>214 pub fn file_punch_hole(_file: &std::fs::File, _offset: u64, _length: u64) -> std::io::Result<()> {
215     todo!();
216 }
217 
file_write_zeroes_at( _file: &std::fs::File, _offset: u64, _length: usize, ) -> std::io::Result<usize>218 pub fn file_write_zeroes_at(
219     _file: &std::fs::File,
220     _offset: u64,
221     _length: usize,
222 ) -> std::io::Result<usize> {
223     todo!();
224 }
225 
226 pub mod syslog {
227     pub struct PlatformSyslog {}
228 
229     impl crate::syslog::Syslog for PlatformSyslog {
new( _proc_name: String, _facility: crate::syslog::Facility, ) -> Result< ( Option<Box<dyn crate::syslog::Log + Send>>, Option<crate::RawDescriptor>, ), crate::syslog::Error, >230         fn new(
231             _proc_name: String,
232             _facility: crate::syslog::Facility,
233         ) -> Result<
234             (
235                 Option<Box<dyn crate::syslog::Log + Send>>,
236                 Option<crate::RawDescriptor>,
237             ),
238             crate::syslog::Error,
239         > {
240             todo!();
241         }
242     }
243 }
244 
245 impl PartialEq for crate::SafeDescriptor {
eq(&self, _other: &Self) -> bool246     fn eq(&self, _other: &Self) -> bool {
247         todo!();
248     }
249 }
250 
251 impl crate::shm::PlatformSharedMemory for crate::SharedMemory {
new(_debug_name: &std::ffi::CStr, _size: u64) -> crate::Result<crate::SharedMemory>252     fn new(_debug_name: &std::ffi::CStr, _size: u64) -> crate::Result<crate::SharedMemory> {
253         todo!();
254     }
from_safe_descriptor( _descriptor: crate::SafeDescriptor, _size: u64, ) -> crate::Result<crate::SharedMemory>255     fn from_safe_descriptor(
256         _descriptor: crate::SafeDescriptor,
257         _size: u64,
258     ) -> crate::Result<crate::SharedMemory> {
259         todo!();
260     }
261 }
262 
263 pub(crate) use libc::off_t;
264 pub(crate) use libc::pread;
265 pub(crate) use libc::preadv;
266 pub(crate) use libc::pwrite;
267 pub(crate) use libc::pwritev;
268 
269 /// Spawns a pipe pair where the first pipe is the read end and the second pipe is the write end.
270 ///
271 /// The `O_CLOEXEC` flag will be applied after pipe creation.
pipe() -> crate::errno::Result<(File, File)>272 pub fn pipe() -> crate::errno::Result<(File, File)> {
273     let mut pipe_fds = [-1; 2];
274     // SAFETY:
275     // Safe because pipe will only write 2 element array of i32 to the given pointer, and we check
276     // for error.
277     let ret = unsafe { libc::pipe(pipe_fds.as_mut_ptr()) };
278     if ret == -1 {
279         return crate::errno::errno_result();
280     }
281 
282     // SAFETY:
283     // Safe because both fds must be valid for pipe to have returned sucessfully and we have
284     // exclusive ownership of them.
285     let pipes = unsafe {
286         (
287             File::from_raw_descriptor(pipe_fds[0]),
288             File::from_raw_descriptor(pipe_fds[1]),
289         )
290     };
291 
292     set_descriptor_cloexec(&pipes.0)?;
293     set_descriptor_cloexec(&pipes.1)?;
294 
295     Ok(pipes)
296 }
297