• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 iobuf;
16 mod mmap;
17 mod notifiers;
18 mod periodic_logger;
19 mod shm;
20 pub mod syslog;
21 pub mod test_utils;
22 mod timer;
23 mod tube;
24 mod volatile_memory;
25 mod wait_context;
26 mod worker_thread;
27 mod write_zeroes;
28 
29 pub mod sys;
30 pub use alloc::LayoutAllocation;
31 
32 pub use clock::Clock;
33 pub use clock::FakeClock;
34 pub use errno::errno_result;
35 pub use errno::Error;
36 pub use errno::Result;
37 pub use event::Event;
38 pub use event::EventWaitResult;
39 pub use file_traits::FileAllocate;
40 pub use file_traits::FileGetLen;
41 pub use file_traits::FileReadWriteAtVolatile;
42 pub use file_traits::FileReadWriteVolatile;
43 pub use file_traits::FileSetLen;
44 pub use file_traits::FileSync;
45 pub use iobuf::IoBufMut;
46 pub use mmap::Error as MmapError;
47 pub use mmap::ExternalMapping;
48 pub use mmap::MappedRegion;
49 pub use mmap::MemoryMapping;
50 pub use mmap::MemoryMappingBuilder;
51 pub use mmap::Result as MmapResult;
52 pub use notifiers::CloseNotifier;
53 pub use notifiers::ReadNotifier;
54 pub use platform::ioctl::ioctl;
55 pub use platform::ioctl::ioctl_with_mut_ptr;
56 pub use platform::ioctl::ioctl_with_mut_ref;
57 pub use platform::ioctl::ioctl_with_ptr;
58 pub use platform::ioctl::ioctl_with_ref;
59 pub use platform::ioctl::ioctl_with_val;
60 pub use platform::ioctl::IoctlNr;
61 pub use shm::SharedMemory;
62 use sys::platform;
63 pub use timer::FakeTimer;
64 pub use timer::Timer;
65 pub use timer::TimerTrait;
66 pub use tube::Error as TubeError;
67 #[cfg(any(windows, feature = "proto_tube"))]
68 pub use tube::ProtoTube;
69 pub use tube::RecvTube;
70 pub use tube::Result as TubeResult;
71 pub use tube::SendTube;
72 pub use tube::Tube;
73 pub use volatile_memory::VolatileMemory;
74 pub use volatile_memory::VolatileMemoryError;
75 pub use volatile_memory::VolatileMemoryResult;
76 pub use volatile_memory::VolatileSlice;
77 pub use wait_context::EventToken;
78 pub use wait_context::EventType;
79 pub use wait_context::TriggeredEvent;
80 pub use wait_context::WaitContext;
81 pub use worker_thread::WorkerThread;
82 pub use write_zeroes::PunchHole;
83 pub use write_zeroes::PunchHoleMut;
84 pub use write_zeroes::WriteZeroesAt;
85 
86 // TODO(b/233233301): reorganize platform specific exports under platform
87 // namespaces instead of exposing them directly in base::.
88 cfg_if::cfg_if! {
89     if #[cfg(any(target_os = "android", target_os = "linux"))] {
90         pub use sys::linux;
91 
92         // descriptor/fd related exports.
93         pub use linux::{
94             clone_descriptor, safe_descriptor_from_path,
95             validate_raw_descriptor, clear_descriptor_cloexec,
96         };
97 
98         // Event/signal related exports.
99         pub use linux::{
100             block_signal, clear_signal, get_blocked_signals, new_pipe_full,
101             register_rt_signal_handler, signal, unblock_signal, Killable, SIGRTMIN,
102             AcpiNotifyEvent, NetlinkGenericSocket, SignalFd, Terminal,
103         };
104 
105         pub use linux::{
106             drop_capabilities, pipe, read_raw_stdin
107         };
108         pub use linux::{enable_core_scheduling, set_rt_prio_limit, set_rt_round_robin};
109         pub use linux::{flock, FlockOperation};
110         pub use linux::{getegid, geteuid};
111         pub use linux::{gettid, kill_process_group, reap_child};
112         pub use linux::logical_core_capacity;
113         pub use linux::logical_core_cluster_id;
114         pub use linux::logical_core_frequencies_khz;
115         pub use linux::sched_attr;
116         pub use linux::sched_setattr;
117         pub use linux::UnlinkUnixListener;
118         pub use linux::EventExt;
119         pub use linux::Gid;
120     }
121 }
122 
123 cfg_if::cfg_if! {
124      if #[cfg(windows)] {
125         pub use sys::windows;
126 
127         pub use windows::{EventTrigger, EventExt, WaitContextExt};
128         pub use windows::IoBuf;
129         pub use windows::MemoryMappingBuilderWindows;
130         pub use windows::set_thread_priority;
131         pub use windows::{give_foregrounding_permission, Console};
132         pub use windows::{named_pipes, named_pipes::PipeConnection};
133         pub use windows::{SafeMultimediaHandle, MAXIMUM_WAIT_OBJECTS};
134         pub use windows::set_sparse_file;
135         pub use windows::ioctl::ioctl_with_ptr_sized;
136         pub use windows::create_overlapped;
137         pub use windows::device_io_control;
138         pub use windows::number_of_logical_cores;
139         pub use windows::pagesize;
140         pub use windows::read_overlapped_blocking;
141 
142         pub use tube::{
143             deserialize_and_recv, serialize_and_send, set_alias_pid, set_duplicate_handle_tube,
144             DuplicateHandleRequest, DuplicateHandleResponse, DuplicateHandleTube
145         };
146         pub use tube::PipeTube;
147         pub use tube::FlushOnDropTube;
148         pub use windows::{set_audio_thread_priority, thread};
149         pub use windows::Pid;
150         pub use windows::Terminal;
151     }
152 }
153 
154 cfg_if::cfg_if! {
155     if #[cfg(unix)] {
156         pub use sys::unix;
157 
158         pub use unix::IoBuf;
159         pub use unix::net::UnixSeqpacket;
160         pub use unix::net::UnixSeqpacketListener;
161         pub use unix::net::UnlinkUnixSeqpacketListener;
162         pub use unix::ScmSocket;
163         pub use unix::SCM_SOCKET_MAX_FD_COUNT;
164         pub use unix::add_fd_flags;
165         pub use unix::clear_fd_flags;
166         pub use unix::number_of_logical_cores;
167         pub use unix::pagesize;
168         pub use unix::Pid;
169     }
170 }
171 
172 pub use descriptor_reflection::deserialize_with_descriptors;
173 pub use descriptor_reflection::with_as_descriptor;
174 pub use descriptor_reflection::with_raw_descriptor;
175 pub use descriptor_reflection::FileSerdeWrapper;
176 pub use descriptor_reflection::SerializeDescriptors;
177 pub use log::debug;
178 pub use log::error;
179 pub use log::info;
180 pub use log::trace;
181 pub use log::warn;
182 pub use mmap::Protection;
183 pub use platform::get_cpu_affinity;
184 pub use platform::get_filesystem_type;
185 pub use platform::getpid;
186 pub use platform::open_file_or_duplicate;
187 pub use platform::platform_timer_resolution::enable_high_res_timers;
188 pub use platform::set_cpu_affinity;
189 pub use platform::BlockingMode;
190 pub use platform::EventContext;
191 pub use platform::FramingMode;
192 pub use platform::MemoryMappingArena;
193 pub use platform::RawDescriptor;
194 pub use platform::StreamChannel;
195 pub use platform::INVALID_DESCRIPTOR;
196 use uuid::Uuid;
197 
198 pub use crate::descriptor::AsRawDescriptor;
199 pub use crate::descriptor::AsRawDescriptors;
200 pub use crate::descriptor::Descriptor;
201 pub use crate::descriptor::FromRawDescriptor;
202 pub use crate::descriptor::IntoRawDescriptor;
203 pub use crate::descriptor::SafeDescriptor;
204 
205 /// An empty trait that helps reset timer resolution to its previous state.
206 // TODO(b:232103460): Maybe this needs to be thought through.
207 pub trait EnabledHighResTimer {}
208 
209 /// Creates a UUID.
generate_uuid() -> String210 pub fn generate_uuid() -> String {
211     let mut buf = Uuid::encode_buffer();
212     Uuid::new_v4()
213         .as_hyphenated()
214         .encode_lower(&mut buf)
215         .to_owned()
216 }
217 
218 use serde::Deserialize;
219 use serde::Serialize;
220 #[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
221 pub enum VmEventType {
222     Exit,
223     Reset,
224     Crash,
225     Panic(u8),
226     WatchdogReset,
227 }
228 
229 /// Uses the system's page size in bytes to round the given value up to the nearest page boundary.
230 #[inline(always)]
round_up_to_page_size(v: usize) -> usize231 pub fn round_up_to_page_size(v: usize) -> usize {
232     let page_mask = pagesize() - 1;
233     (v + page_mask) & !page_mask
234 }
235