Skip to content

Commit

Permalink
[#96] ProcessState state method is const
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Jan 30, 2024
1 parent 96fb775 commit e0f7fbb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
80 changes: 47 additions & 33 deletions iceoryx2-bb/posix/src/process_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
//! }
//! ```
use std::fmt::Debug;

pub use iceoryx2_bb_container::semantic_string::SemanticString;
use iceoryx2_bb_elementary::enum_gen;
use iceoryx2_bb_log::{debug, error, fail, fatal_panic, trace, warn};
Expand Down Expand Up @@ -415,12 +417,22 @@ impl ProcessGuard {
/// ProcessState::DoesNotExist => (),
/// }
/// ```
#[derive(Debug)]
pub struct ProcessMonitor {
file: Option<File>,
file: core::cell::Cell<Option<File>>,
path: FilePath,
}

impl Debug for ProcessMonitor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"ProcessMonitor {{ file = {:?}, path = {:?}}}",
unsafe { &*self.file.as_ptr() },
self.path
)
}
}

impl ProcessMonitor {
/// Creates a new [`ProcessMonitor`] that can obtain the state of the process that will be
/// monitored.
Expand All @@ -435,8 +447,8 @@ impl ProcessMonitor {
/// let mut monitor = ProcessMonitor::new(&process_state_path).expect("");
/// ```
pub fn new(path: &FilePath) -> Result<Self, ProcessMonitorCreateError> {
let mut new_self = Self {
file: None,
let new_self = Self {
file: core::cell::Cell::new(None),
path: *path,
};

Expand Down Expand Up @@ -477,9 +489,9 @@ impl ProcessMonitor {
/// ProcessState::DoesNotExist => (),
/// }
/// ```
pub fn state(&mut self) -> Result<ProcessState, ProcessMonitorStateError> {
pub fn state(&self) -> Result<ProcessState, ProcessMonitorStateError> {
let msg = "Unable to acquire ProcessState";
match self.file {
match unsafe { &*self.file.as_ptr() } {
Some(_) => self.read_state_from_file(),
None => match File::does_exist(&self.path) {
Ok(true) => {
Expand All @@ -496,8 +508,8 @@ impl ProcessMonitor {
}
}

fn read_state_from_file(&mut self) -> Result<ProcessState, ProcessMonitorStateError> {
let file = match self.file {
fn read_state_from_file(&self) -> Result<ProcessState, ProcessMonitorStateError> {
let file = match unsafe { &*self.file.as_ptr() } {
Some(ref f) => f,
None => return Ok(ProcessState::InInitialization),
};
Expand Down Expand Up @@ -526,12 +538,12 @@ impl ProcessMonitor {
Ok(true) => match file.permission() {
Ok(INIT_PERMISSION) => Ok(ProcessState::InInitialization),
Err(_) | Ok(_) => {
self.file = None;
self.file.set(None);
Ok(ProcessState::Dead)
}
},
Ok(false) => {
self.file = None;
self.file.set(None);
Ok(ProcessState::DoesNotExist)
}
Err(v) => {
Expand All @@ -542,39 +554,41 @@ impl ProcessMonitor {
}
}

fn open_file(&mut self) -> Result<(), ProcessMonitorCreateError> {
fn open_file(&self) -> Result<(), ProcessMonitorCreateError> {
let origin = "ProcessMonitor::new()";
let msg = format!(
"Unable to open ProcessMonitor with the file \"{}\"",
self.path
);
self.file = match FileBuilder::new(&self.path).open_existing(AccessMode::Read) {
Ok(f) => Some(f),
Err(FileOpenError::FileDoesNotExist) => None,
Err(FileOpenError::IsDirectory) => {
fail!(from origin, with ProcessMonitorCreateError::IsDirectory,
self.file.set(
match FileBuilder::new(&self.path).open_existing(AccessMode::Read) {
Ok(f) => Some(f),
Err(FileOpenError::FileDoesNotExist) => None,
Err(FileOpenError::IsDirectory) => {
fail!(from origin, with ProcessMonitorCreateError::IsDirectory,
"{} since the path is a directory.", msg);
}
Err(FileOpenError::InsufficientPermissions) => {
if FileBuilder::new(&self.path)
.open_existing(AccessMode::Write)
.is_ok()
{
None
} else {
fail!(from origin, with ProcessMonitorCreateError::InsufficientPermissions,
}
Err(FileOpenError::InsufficientPermissions) => {
if FileBuilder::new(&self.path)
.open_existing(AccessMode::Write)
.is_ok()
{
None
} else {
fail!(from origin, with ProcessMonitorCreateError::InsufficientPermissions,
"{} due to insufficient permissions.", msg);
}
}
}
Err(FileOpenError::Interrupt) => {
fail!(from origin, with ProcessMonitorCreateError::Interrupt,
Err(FileOpenError::Interrupt) => {
fail!(from origin, with ProcessMonitorCreateError::Interrupt,
"{} since an interrupt signal was received.", msg);
}
Err(v) => {
fail!(from origin, with ProcessMonitorCreateError::UnknownError,
}
Err(v) => {
fail!(from origin, with ProcessMonitorCreateError::UnknownError,
"{} since an unknown failure occurred ({:?}).", msg, v);
}
};
}
},
);

Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions iceoryx2-bb/posix/tests/process_state_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn process_state_watcher_detects_alive_state_from_existing_process() {
let path = generate_file_path();

let guard = ProcessGuard::new(&path).unwrap();
let mut watcher = ProcessMonitor::new(&path).unwrap();
let watcher = ProcessMonitor::new(&path).unwrap();

assert_that!(watcher.state().unwrap(), eq ProcessState::Alive);
drop(guard);
Expand All @@ -109,7 +109,7 @@ pub fn process_state_watcher_detects_dead_state() {
.creation_mode(CreationMode::PurgeAndCreate)
.create()
.unwrap();
let mut watcher = ProcessMonitor::new(&path).unwrap();
let watcher = ProcessMonitor::new(&path).unwrap();

assert_that!(watcher.state().unwrap(), eq ProcessState::Dead);
file.remove_self().unwrap();
Expand All @@ -120,15 +120,15 @@ pub fn process_state_watcher_detects_dead_state() {
pub fn process_state_watcher_detects_non_existing_state() {
let path = generate_file_path();

let mut watcher = ProcessMonitor::new(&path).unwrap();
let watcher = ProcessMonitor::new(&path).unwrap();
assert_that!(watcher.state().unwrap(), eq ProcessState::DoesNotExist);
}

#[test]
pub fn process_state_watcher_transitions_work_starting_from_non_existing_process() {
let path = generate_file_path();

let mut watcher = ProcessMonitor::new(&path).unwrap();
let watcher = ProcessMonitor::new(&path).unwrap();
assert_that!(watcher.state().unwrap(), eq ProcessState::DoesNotExist);
let file = FileBuilder::new(&path)
.creation_mode(CreationMode::PurgeAndCreate)
Expand All @@ -148,7 +148,7 @@ pub fn process_state_watcher_transitions_work_starting_from_existing_process() {
.create()
.unwrap();

let mut watcher = ProcessMonitor::new(&path).unwrap();
let watcher = ProcessMonitor::new(&path).unwrap();
assert_that!(watcher.state().unwrap(), eq ProcessState::Dead);
file.remove_self().unwrap();
assert_that!(watcher.state().unwrap(), eq ProcessState::DoesNotExist);
Expand All @@ -172,7 +172,7 @@ pub fn process_state_watcher_detects_initialized_state() {
.create()
.unwrap();

let mut watcher = ProcessMonitor::new(&path).unwrap();
let watcher = ProcessMonitor::new(&path).unwrap();
assert_that!(watcher.state().unwrap(), eq ProcessState::InInitialization);
file.set_permission(Permission::OWNER_ALL).unwrap();
file.remove_self().unwrap();
Expand Down
1 change: 1 addition & 0 deletions iceoryx2-bb/system-types/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
pub use iceoryx2_bb_container::semantic_string::SemanticString;

use core::hash::{Hash, Hasher};
use iceoryx2_bb_container::byte_string::FixedSizeByteString;
use iceoryx2_bb_container::semantic_string;

Expand Down

0 comments on commit e0f7fbb

Please sign in to comment.