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 //! Safe, cross-platform-compatible wrappers for system interfaces.
6
7 mod alloc;
8 mod clock;
9 pub mod custom_serde;
10 pub mod descriptor;
11 pub mod descriptor_reflection;
12 mod errno;
13 mod event;
14 mod file_traits;
15 mod mmap;
16 mod notifiers;
17 mod shm;
18 pub mod syslog;
19 mod timer;
20 mod tube;
21 mod wait_context;
22 mod worker_thread;
23 mod write_zeroes;
24
25 pub mod sys;
26 pub use alloc::LayoutAllocation;
27
28 pub use clock::Clock;
29 pub use clock::FakeClock;
30 pub use errno::errno_result;
31 pub use errno::Error;
32 pub use errno::Result;
33 pub use event::Event;
34 pub use event::EventWaitResult;
35 pub use file_traits::FileAllocate;
36 pub use file_traits::FileGetLen;
37 pub use file_traits::FileReadWriteAtVolatile;
38 pub use file_traits::FileReadWriteVolatile;
39 pub use file_traits::FileSetLen;
40 pub use file_traits::FileSync;
41 pub use mmap::ExternalMapping;
42 pub use mmap::MappedRegion;
43 pub use mmap::MemoryMapping;
44 pub use mmap::MemoryMappingBuilder;
45 pub use notifiers::CloseNotifier;
46 pub use notifiers::ReadNotifier;
47 pub use platform::ioctl::ioctl;
48 pub use platform::ioctl::ioctl_with_mut_ptr;
49 pub use platform::ioctl::ioctl_with_mut_ref;
50 pub use platform::ioctl::ioctl_with_ptr;
51 pub use platform::ioctl::ioctl_with_ref;
52 pub use platform::ioctl::ioctl_with_val;
53 pub use platform::ioctl::IoctlNr;
54 pub use shm::SharedMemory;
55 pub use sys::platform;
56 pub use timer::FakeTimer;
57 pub use timer::Timer;
58 pub use tube::Error as TubeError;
59 pub use tube::RecvTube;
60 pub use tube::Result as TubeResult;
61 pub use tube::SendTube;
62 pub use tube::Tube;
63 pub use wait_context::EventToken;
64 pub use wait_context::EventType;
65 pub use wait_context::TriggeredEvent;
66 pub use wait_context::WaitContext;
67 pub use worker_thread::WorkerThread;
68 pub use write_zeroes::PunchHole;
69 pub use write_zeroes::WriteZeroesAt;
70
71 // TODO(b/233233301): reorganize platform specific exports under platform
72 // namespaces instead of exposing them directly in base::.
73 cfg_if::cfg_if! {
74 if #[cfg(unix)] {
75 pub use sys::unix;
76
77 pub use unix::net;
78
79 // File related exports.
80 pub use platform::{FileFlags, get_max_open_files};
81
82 // memory/mmap related exports.
83 pub use platform::{
84 MemfdSeals, MemoryMappingBuilderUnix, Unix as MemoryMappingUnix,
85 SharedMemoryUnix,
86 };
87
88 // descriptor/fd related exports.
89 pub use platform::{
90 add_fd_flags, clear_fd_flags, clone_descriptor, safe_descriptor_from_path,
91 validate_raw_descriptor, clear_descriptor_cloexec,
92 };
93
94 // Event/signal related exports.
95 pub use platform::{
96 block_signal, clear_signal, get_blocked_signals, new_pipe_full,
97 register_rt_signal_handler, signal, unblock_signal, Killable, SIGRTMIN,
98 AcpiNotifyEvent, NetlinkGenericSocket, SignalFd, Terminal,
99 };
100
101 pub use platform::{
102 chown, drop_capabilities, iov_max, pipe, read_raw_stdin
103 };
104 pub use platform::{enable_core_scheduling, set_rt_prio_limit, set_rt_round_robin};
105 pub use platform::{flock, FlockOperation};
106 pub use platform::{getegid, geteuid};
107 pub use platform::{gettid, kill_process_group, reap_child};
108 pub use platform::{
109 net::{UnixSeqpacket, UnixSeqpacketListener, UnlinkUnixSeqpacketListener},
110 ScmSocket, UnlinkUnixListener, SCM_SOCKET_MAX_FD_COUNT,
111 };
112 pub use platform::EventExt;
113 } else if #[cfg(windows)] {
114 pub use platform::{EventTrigger, EventExt, WaitContextExt};
115 pub use platform::MemoryMappingBuilderWindows;
116 pub use platform::set_thread_priority;
117 pub use platform::{give_foregrounding_permission, Console};
118 pub use platform::{named_pipes, named_pipes::PipeConnection};
119 pub use platform::{SafeMultimediaHandle, MAXIMUM_WAIT_OBJECTS};
120 pub use crate::platform::win::{
121 measure_timer_resolution, nt_query_timer_resolution, nt_set_timer_resolution,
122 set_sparse_file, set_time_period,
123 };
124 pub use platform::ioctl::ioctl_with_ptr_sized;
125
126 pub use tube::{
127 deserialize_and_recv, serialize_and_send, set_alias_pid, set_duplicate_handle_tube,
128 DuplicateHandleRequest, DuplicateHandleResponse, DuplicateHandleTube
129 };
130 pub use tube::ProtoTube;
131 pub use platform::{set_audio_thread_priorities, thread};
132 pub use platform::Terminal;
133 } else {
134 compile_error!("Unsupported platform");
135 }
136 }
137
138 pub use log::debug;
139 pub use log::error;
140 pub use log::info;
141 pub use log::trace;
142 pub use log::warn;
143 pub use mmap::Protection;
144 pub use platform::deserialize_with_descriptors;
145 pub use platform::get_cpu_affinity;
146 pub use platform::get_filesystem_type;
147 pub use platform::getpid;
148 pub use platform::number_of_logical_cores;
149 pub use platform::open_file;
150 pub use platform::pagesize;
151 pub use platform::platform_timer_resolution::enable_high_res_timers;
152 pub use platform::round_up_to_page_size;
153 pub use platform::set_cpu_affinity;
154 pub use platform::with_as_descriptor;
155 pub use platform::with_raw_descriptor;
156 pub use platform::BlockingMode;
157 pub use platform::EventContext;
158 pub use platform::FileSerdeWrapper;
159 pub use platform::FramingMode;
160 pub use platform::MemoryMappingArena;
161 pub use platform::MmapError;
162 pub use platform::RawDescriptor;
163 pub use platform::SerializeDescriptors;
164 pub use platform::StreamChannel;
165 pub use platform::INVALID_DESCRIPTOR;
166 use uuid::Uuid;
167
168 pub use crate::descriptor::AsRawDescriptor;
169 pub use crate::descriptor::AsRawDescriptors;
170 pub use crate::descriptor::Descriptor;
171 pub use crate::descriptor::FromRawDescriptor;
172 pub use crate::descriptor::IntoRawDescriptor;
173 pub use crate::descriptor::SafeDescriptor;
174
175 /// An empty trait that helps reset timer resolution to its previous state.
176 // TODO(b:232103460): Maybe this needs to be thought through.
177 pub trait EnabledHighResTimer {}
178
179 /// Creates a UUID.
generate_uuid() -> String180 pub fn generate_uuid() -> String {
181 let mut buf = Uuid::encode_buffer();
182 Uuid::new_v4()
183 .as_hyphenated()
184 .encode_lower(&mut buf)
185 .to_owned()
186 }
187
188 use serde::Deserialize;
189 use serde::Serialize;
190 #[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
191 pub enum VmEventType {
192 Exit,
193 Reset,
194 Crash,
195 Panic(u8),
196 WatchdogReset,
197 }
198