mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-10-05 21:15:45 +00:00
event_monitor: add safety comments
Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
parent
b9ad3eda27
commit
b1efa5b26b
@ -16,8 +16,10 @@ static mut MONITOR: Option<(File, Instant)> = None;
|
|||||||
/// This function must only be called once from the main process before any threads
|
/// This function must only be called once from the main process before any threads
|
||||||
/// are created to avoid race conditions
|
/// are created to avoid race conditions
|
||||||
pub fn set_monitor(file: File) -> Result<(), std::io::Error> {
|
pub fn set_monitor(file: File) -> Result<(), std::io::Error> {
|
||||||
|
// SAFETY: there is only one caller of this function, so MONITOR is written to only once
|
||||||
assert!(unsafe { MONITOR.is_none() });
|
assert!(unsafe { MONITOR.is_none() });
|
||||||
let fd = file.as_raw_fd();
|
let fd = file.as_raw_fd();
|
||||||
|
// SAFETY: FFI call to configure the fd
|
||||||
let ret = unsafe {
|
let ret = unsafe {
|
||||||
let mut flags = libc::fcntl(fd, libc::F_GETFL);
|
let mut flags = libc::fcntl(fd, libc::F_GETFL);
|
||||||
flags |= libc::O_NONBLOCK;
|
flags |= libc::O_NONBLOCK;
|
||||||
@ -26,6 +28,7 @@ pub fn set_monitor(file: File) -> Result<(), std::io::Error> {
|
|||||||
if ret < 0 {
|
if ret < 0 {
|
||||||
return Err(std::io::Error::last_os_error());
|
return Err(std::io::Error::last_os_error());
|
||||||
}
|
}
|
||||||
|
// SAFETY: MONITOR is None. Nobody else can hold a reference to it.
|
||||||
unsafe {
|
unsafe {
|
||||||
MONITOR = Some((file, Instant::now()));
|
MONITOR = Some((file, Instant::now()));
|
||||||
};
|
};
|
||||||
@ -41,6 +44,7 @@ struct Event<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn event_log(source: &str, event: &str, properties: Option<&HashMap<Cow<str>, Cow<str>>>) {
|
pub fn event_log(source: &str, event: &str, properties: Option<&HashMap<Cow<str>, Cow<str>>>) {
|
||||||
|
// SAFETY: MONITOR is always in a valid state (None or Some).
|
||||||
if let Some((file, start)) = unsafe { MONITOR.as_ref() } {
|
if let Some((file, start)) = unsafe { MONITOR.as_ref() } {
|
||||||
let e = Event {
|
let e = Event {
|
||||||
timestamp: start.elapsed(),
|
timestamp: start.elapsed(),
|
||||||
|
Loading…
Reference in New Issue
Block a user