From 7d027b3e2886b3c7daaaf778e3a1ffc4b14f55a6 Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Fri, 29 Dec 2023 13:44:03 +0100 Subject: [PATCH] [#61] Fix undefined behavior in FixedSizeString by removing const from new_unchecked --- doc/release-notes/iceoryx2-unreleased.md | 1 + iceoryx2-bb/container/src/byte_string.rs | 15 +----- iceoryx2-bb/container/src/semantic_string.rs | 2 +- iceoryx2-bb/posix/src/config.rs | 17 ++++--- iceoryx2-bb/posix/tests/directory_tests.rs | 4 +- .../posix/tests/file_descriptor_set_tests.rs | 2 +- .../posix/tests/file_descriptor_tests.rs | 2 +- iceoryx2-bb/posix/tests/file_lock_tests.rs | 4 +- iceoryx2-bb/posix/tests/file_tests.rs | 2 +- iceoryx2-bb/posix/tests/metadata_tests.rs | 4 +- .../posix/tests/socket_ancillary_tests.rs | 4 +- .../posix/tests/unix_datagram_socket_tests.rs | 4 +- .../communication_channel/message_queue.rs | 10 ++-- iceoryx2-cal/src/communication_channel/mod.rs | 15 ++---- .../posix_shared_memory.rs | 6 +-- .../communication_channel/process_local.rs | 6 +-- .../communication_channel/unix_datagram.rs | 10 ++-- iceoryx2-cal/src/dynamic_storage/mod.rs | 16 ++----- .../dynamic_storage/posix_shared_memory.rs | 10 ++-- .../src/dynamic_storage/process_local.rs | 10 ++-- iceoryx2-cal/src/event/mod.rs | 15 ++---- iceoryx2-cal/src/event/process_local.rs | 42 +++++++++------- .../src/event/unix_datagram_socket.rs | 48 +++++++++++-------- iceoryx2-cal/src/named_concept.rs | 10 ++++ iceoryx2-cal/src/shared_memory/mod.rs | 16 ++----- iceoryx2-cal/src/shared_memory/posix.rs | 36 ++++++++++---- .../src/shared_memory/process_local.rs | 35 +++++++++----- .../file_reference_set.rs | 12 ++--- .../src/shared_memory_directory/mod.rs | 22 ++++----- iceoryx2-cal/src/static_storage/file.rs | 6 +-- iceoryx2-cal/src/static_storage/mod.rs | 16 ++----- .../src/static_storage/process_local.rs | 6 +-- iceoryx2-cal/src/zero_copy_connection/mod.rs | 15 ++---- .../posix_shared_memory.rs | 6 +-- .../src/zero_copy_connection/process_local.rs | 6 +-- .../communication_channel_trait_tests.rs | 5 +- .../tests/dynamic_storage_trait_tests.rs | 5 +- iceoryx2-cal/tests/event_tests.rs | 9 ++-- .../tests/shared_memory_trait_tests.rs | 5 +- .../tests/static_storage_file_tests.rs | 16 ++++--- .../tests/static_storage_trait_tests.rs | 5 +- .../tests/zero_copy_connection_trait_tests.rs | 5 +- iceoryx2/src/config.rs | 11 +++-- iceoryx2/src/service/process_local.rs | 2 +- iceoryx2/src/service/zero_copy.rs | 2 +- 45 files changed, 264 insertions(+), 236 deletions(-) diff --git a/doc/release-notes/iceoryx2-unreleased.md b/doc/release-notes/iceoryx2-unreleased.md index 5653380bd..9353e2ce7 100644 --- a/doc/release-notes/iceoryx2-unreleased.md +++ b/doc/release-notes/iceoryx2-unreleased.md @@ -11,6 +11,7 @@ ### Bugfixes + * Fix undefined behavior in `FixedSizeByteString::new_unchecked` [#61](https://github.com/eclipse-iceoryx/iceoryx2/issues/61) * Fix suffix of static config [#66](https://github.com/eclipse-iceoryx/iceoryx2/issues/66) ### Refactoring diff --git a/iceoryx2-bb/container/src/byte_string.rs b/iceoryx2-bb/container/src/byte_string.rs index 77b465ca7..db9e8b937 100644 --- a/iceoryx2-bb/container/src/byte_string.rs +++ b/iceoryx2-bb/container/src/byte_string.rs @@ -207,23 +207,12 @@ impl FixedSizeByteString { /// /// * `bytes` len must be smaller or equal than [`FixedSizeByteString::capacity()`] /// - pub const unsafe fn new_unchecked(bytes: &[u8]) -> Self { + pub unsafe fn new_unchecked(bytes: &[u8]) -> Self { if CAPACITY < bytes.len() { panic!("Insufficient capacity to store bytes."); } - let mut new_self = Self::new(); - new_self.len = bytes.len(); - std::ptr::copy( - bytes.as_ptr(), - new_self.data.as_ptr() as *mut u8, - bytes.len(), - ); - - let zero = 0u8; - std::ptr::copy(&zero, new_self.data.as_ptr().add(bytes.len()) as *mut u8, 1); - - new_self + Self::from_bytes_truncated(bytes) } /// Creates a new [`FixedSizeByteString`] from a byte slice diff --git a/iceoryx2-bb/container/src/semantic_string.rs b/iceoryx2-bb/container/src/semantic_string.rs index ea3abf34f..a1400fdcd 100644 --- a/iceoryx2-bb/container/src/semantic_string.rs +++ b/iceoryx2-bb/container/src/semantic_string.rs @@ -295,7 +295,7 @@ macro_rules! semantic_string { } impl $string_name { - pub const unsafe fn new_unchecked(bytes: &[u8]) -> Self { + pub unsafe fn new_unchecked(bytes: &[u8]) -> Self { Self { value: iceoryx2_bb_container::byte_string::FixedSizeByteString::new_unchecked(bytes), } diff --git a/iceoryx2-bb/posix/src/config.rs b/iceoryx2-bb/posix/src/config.rs index fd929b49d..2bcdd432f 100644 --- a/iceoryx2-bb/posix/src/config.rs +++ b/iceoryx2-bb/posix/src/config.rs @@ -38,12 +38,17 @@ pub const ADAPTIVE_WAIT_INITIAL_WAITING_TIME: Duration = Duration::from_micros(1 pub const ADAPTIVE_WAIT_FINAL_WAITING_TIME: Duration = Duration::from_millis(10); // directories -pub const TEMP_DIRECTORY: Path = - unsafe { Path::new_unchecked(iceoryx2_pal_configuration::TEMP_DIRECTORY) }; -pub const TEST_DIRECTORY: Path = - unsafe { Path::new_unchecked(iceoryx2_pal_configuration::TEST_DIRECTORY) }; -pub const SHARED_MEMORY_DIRECTORY: Path = - unsafe { Path::new_unchecked(iceoryx2_pal_configuration::SHARED_MEMORY_DIRECTORY) }; +pub fn temp_directory() -> Path { + unsafe { Path::new_unchecked(iceoryx2_pal_configuration::TEMP_DIRECTORY) } +} + +pub fn test_directory() -> Path { + unsafe { Path::new_unchecked(iceoryx2_pal_configuration::TEST_DIRECTORY) } +} + +pub fn shared_memory_directory() -> Path { + unsafe { Path::new_unchecked(iceoryx2_pal_configuration::SHARED_MEMORY_DIRECTORY) } +} // TODO unable to verify? pub const ACL_LIST_CAPACITY: u32 = 25; diff --git a/iceoryx2-bb/posix/tests/directory_tests.rs b/iceoryx2-bb/posix/tests/directory_tests.rs index 47fc957ac..1b59b8ce3 100644 --- a/iceoryx2-bb/posix/tests/directory_tests.rs +++ b/iceoryx2-bb/posix/tests/directory_tests.rs @@ -87,7 +87,7 @@ impl TestFixture { } fn generate_directory_name(&mut self) -> Path { - let mut directory = TEST_DIRECTORY; + let mut directory = test_directory(); directory.push(PATH_SEPARATOR).unwrap(); directory.push_bytes(b"dir_tests_").unwrap(); directory @@ -107,7 +107,7 @@ impl TestFixture { #[test] fn directory_temp_directory_does_exist() { - assert_that!(Directory::does_exist(&TEST_DIRECTORY).unwrap(), eq true); + assert_that!(Directory::does_exist(&test_directory()).unwrap(), eq true); } #[test] diff --git a/iceoryx2-bb/posix/tests/file_descriptor_set_tests.rs b/iceoryx2-bb/posix/tests/file_descriptor_set_tests.rs index 7b09fcaec..3b2bd42e8 100644 --- a/iceoryx2-bb/posix/tests/file_descriptor_set_tests.rs +++ b/iceoryx2-bb/posix/tests/file_descriptor_set_tests.rs @@ -38,7 +38,7 @@ fn generate_socket_name() -> FilePath { ) .unwrap(); - FilePath::from_path_and_file(&TEST_DIRECTORY, &file).unwrap() + FilePath::from_path_and_file(&test_directory(), &file).unwrap() } #[test] diff --git a/iceoryx2-bb/posix/tests/file_descriptor_tests.rs b/iceoryx2-bb/posix/tests/file_descriptor_tests.rs index d326a6201..a9f2bd4c1 100644 --- a/iceoryx2-bb/posix/tests/file_descriptor_tests.rs +++ b/iceoryx2-bb/posix/tests/file_descriptor_tests.rs @@ -53,7 +53,7 @@ trait GenericTestBuilder { impl GenericTestBuilder for File { fn sut() -> Self { - let name = FilePath::from_path_and_file(&TEST_DIRECTORY, &generate_name()).unwrap(); + let name = FilePath::from_path_and_file(&test_directory(), &generate_name()).unwrap(); let file_content = [170u8; 2048]; diff --git a/iceoryx2-bb/posix/tests/file_lock_tests.rs b/iceoryx2-bb/posix/tests/file_lock_tests.rs index cac8bbe28..ebf28cbc0 100644 --- a/iceoryx2-bb/posix/tests/file_lock_tests.rs +++ b/iceoryx2-bb/posix/tests/file_lock_tests.rs @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT use iceoryx2_bb_container::semantic_string::SemanticString; -use iceoryx2_bb_posix::config::*; +use iceoryx2_bb_posix::config; use iceoryx2_bb_posix::file::*; use iceoryx2_bb_posix::file_lock::*; use iceoryx2_bb_posix::process::*; @@ -38,7 +38,7 @@ fn generate_file_name() -> FilePath { ) .unwrap(); - FilePath::from_path_and_file(&TEST_DIRECTORY, &file).unwrap() + FilePath::from_path_and_file(&config::test_directory(), &file).unwrap() } const TIMEOUT: Duration = Duration::from_millis(10); diff --git a/iceoryx2-bb/posix/tests/file_tests.rs b/iceoryx2-bb/posix/tests/file_tests.rs index f3e095a3f..1f5b48f54 100644 --- a/iceoryx2-bb/posix/tests/file_tests.rs +++ b/iceoryx2-bb/posix/tests/file_tests.rs @@ -33,7 +33,7 @@ fn generate_file_name() -> FilePath { ) .unwrap(); - FilePath::from_path_and_file(&TEST_DIRECTORY, &file).unwrap() + FilePath::from_path_and_file(&test_directory(), &file).unwrap() } struct TestFixture { diff --git a/iceoryx2-bb/posix/tests/metadata_tests.rs b/iceoryx2-bb/posix/tests/metadata_tests.rs index d9d92f66e..c34c55643 100644 --- a/iceoryx2-bb/posix/tests/metadata_tests.rs +++ b/iceoryx2-bb/posix/tests/metadata_tests.rs @@ -28,7 +28,7 @@ use iceoryx2_pal_posix::posix::POSIX_SUPPORT_USERS_AND_GROUPS; #[test] fn metadata_reads_basic_stats_correctly() { let file_name = - FilePath::from_path_and_file(&TEST_DIRECTORY, &FileName::new(b"meta_test").unwrap()) + FilePath::from_path_and_file(&test_directory(), &FileName::new(b"meta_test").unwrap()) .unwrap(); let mut file = FileBuilder::new(&file_name) @@ -50,7 +50,7 @@ fn metadata_reads_owner_and_permission_stats_correctly() { test_requires!(POSIX_SUPPORT_USERS_AND_GROUPS && POSIX_SUPPORT_PERMISSIONS); let file_name = - FilePath::from_path_and_file(&TEST_DIRECTORY, &FileName::new(b"meta_test_123").unwrap()) + FilePath::from_path_and_file(&test_directory(), &FileName::new(b"meta_test_123").unwrap()) .unwrap(); let mut file = FileBuilder::new(&file_name) diff --git a/iceoryx2-bb/posix/tests/socket_ancillary_tests.rs b/iceoryx2-bb/posix/tests/socket_ancillary_tests.rs index b0eff3f4a..a82f2969f 100644 --- a/iceoryx2-bb/posix/tests/socket_ancillary_tests.rs +++ b/iceoryx2-bb/posix/tests/socket_ancillary_tests.rs @@ -12,7 +12,7 @@ use iceoryx2_bb_container::semantic_string::SemanticString; use iceoryx2_bb_elementary::unique_id::*; -use iceoryx2_bb_posix::config::*; +use iceoryx2_bb_posix::config; use iceoryx2_bb_posix::file::*; use iceoryx2_bb_posix::file_descriptor::*; use iceoryx2_bb_posix::process::ProcessId; @@ -29,7 +29,7 @@ fn generate_file_name() -> FilePath { file.push_bytes(UniqueId::new().value().to_string().as_bytes()) .unwrap(); - FilePath::from_path_and_file(&TEST_DIRECTORY, &file).unwrap() + FilePath::from_path_and_file(&config::test_directory(), &file).unwrap() } struct TestFixture { diff --git a/iceoryx2-bb/posix/tests/unix_datagram_socket_tests.rs b/iceoryx2-bb/posix/tests/unix_datagram_socket_tests.rs index e26abeeda..d4cb05ece 100644 --- a/iceoryx2-bb/posix/tests/unix_datagram_socket_tests.rs +++ b/iceoryx2-bb/posix/tests/unix_datagram_socket_tests.rs @@ -43,7 +43,7 @@ fn generate_socket_name() -> FilePath { ) .unwrap(); - FilePath::from_path_and_file(&TEST_DIRECTORY, &file).unwrap() + FilePath::from_path_and_file(&test_directory(), &file).unwrap() } fn generate_file_name() -> FilePath { @@ -57,7 +57,7 @@ fn generate_file_name() -> FilePath { ) .unwrap(); - FilePath::from_path_and_file(&TEST_DIRECTORY, &file).unwrap() + FilePath::from_path_and_file(&test_directory(), &file).unwrap() } struct TestFixture { diff --git a/iceoryx2-cal/src/communication_channel/message_queue.rs b/iceoryx2-cal/src/communication_channel/message_queue.rs index 5b5c730de..f9abd4293 100644 --- a/iceoryx2-cal/src/communication_channel/message_queue.rs +++ b/iceoryx2-cal/src/communication_channel/message_queue.rs @@ -147,9 +147,9 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Self { - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, - path_hint: DEFAULT_PATH_HINT, + suffix: Channel::<()>::default_suffix(), + prefix: Channel::<()>::default_prefix(), + path_hint: Channel::<()>::default_path_hint(), } } } @@ -190,7 +190,7 @@ struct SharedConfiguration { } #[derive(Debug)] -pub struct Creator { +pub struct Creator { channel_name: FileName, enable_safe_overflow: bool, buffer_size: usize, @@ -291,7 +291,7 @@ impl CommunicationChannelCreator> for Creator } #[derive(Debug)] -pub struct Connector { +pub struct Connector { channel_name: FileName, config: Configuration, _phantom_data: PhantomData, diff --git a/iceoryx2-cal/src/communication_channel/mod.rs b/iceoryx2-cal/src/communication_channel/mod.rs index 31add38da..7517a98dc 100644 --- a/iceoryx2-cal/src/communication_channel/mod.rs +++ b/iceoryx2-cal/src/communication_channel/mod.rs @@ -72,7 +72,6 @@ pub mod unix_datagram; use std::fmt::Debug; -use iceoryx2_bb_posix::config::TEMP_DIRECTORY; use iceoryx2_bb_system_types::file_name::FileName; use iceoryx2_bb_system_types::path::Path; @@ -81,15 +80,6 @@ use crate::named_concept::{NamedConcept, NamedConceptBuilder, NamedConceptMgmt}; /// The buffer size which the receiver has at least by default pub const DEFAULT_RECEIVER_BUFFER_SIZE: usize = 8; -/// The default suffix of every communication channel -pub const DEFAULT_SUFFIX: FileName = unsafe { FileName::new_unchecked(b".com") }; - -/// The default prefix of every communication channel -pub const DEFAULT_PREFIX: FileName = unsafe { FileName::new_unchecked(b"iox2_") }; - -/// The default path hint for every communication channel -pub const DEFAULT_PATH_HINT: Path = TEMP_DIRECTORY; - /// Describes failures when sending data #[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)] pub enum CommunicationChannelSendError { @@ -207,4 +197,9 @@ pub trait CommunicationChannel: Sized + Debug + NamedConceptMgmt { fn has_configurable_buffer_size() -> bool { false } + + /// The default suffix of every communication channel + fn default_suffix() -> FileName { + unsafe { FileName::new_unchecked(b".com") } + } } diff --git a/iceoryx2-cal/src/communication_channel/posix_shared_memory.rs b/iceoryx2-cal/src/communication_channel/posix_shared_memory.rs index 4ba42505a..d4e27984a 100644 --- a/iceoryx2-cal/src/communication_channel/posix_shared_memory.rs +++ b/iceoryx2-cal/src/communication_channel/posix_shared_memory.rs @@ -79,9 +79,9 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Self { - suffix: DEFAULT_SUFFIX, - path_hint: DEFAULT_PATH_HINT, - prefix: DEFAULT_PREFIX, + suffix: Channel::default_suffix(), + path_hint: Channel::default_path_hint(), + prefix: Channel::default_prefix(), } } } diff --git a/iceoryx2-cal/src/communication_channel/process_local.rs b/iceoryx2-cal/src/communication_channel/process_local.rs index 6ad4f2918..5719c128e 100644 --- a/iceoryx2-cal/src/communication_channel/process_local.rs +++ b/iceoryx2-cal/src/communication_channel/process_local.rs @@ -69,9 +69,9 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Self { - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, - path_hint: DEFAULT_PATH_HINT, + suffix: Channel::default_suffix(), + prefix: Channel::default_prefix(), + path_hint: Channel::default_path_hint(), } } } diff --git a/iceoryx2-cal/src/communication_channel/unix_datagram.rs b/iceoryx2-cal/src/communication_channel/unix_datagram.rs index ae391e85a..e62cdeb10 100644 --- a/iceoryx2-cal/src/communication_channel/unix_datagram.rs +++ b/iceoryx2-cal/src/communication_channel/unix_datagram.rs @@ -37,9 +37,9 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Self { - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, - path_hint: DEFAULT_PATH_HINT, + suffix: Channel::<()>::default_suffix(), + prefix: Channel::<()>::default_prefix(), + path_hint: Channel::<()>::default_path_hint(), } } } @@ -165,7 +165,7 @@ impl CommunicationChannel for Channel { } #[derive(Debug)] -pub struct Creator { +pub struct Creator { channel_name: FileName, enable_safe_overflow: bool, buffer_size: usize, @@ -240,7 +240,7 @@ impl CommunicationChannelCreator> for Creator } #[derive(Debug)] -pub struct Connector { +pub struct Connector { channel_name: FileName, config: Configuration, _phantom_data: PhantomData, diff --git a/iceoryx2-cal/src/dynamic_storage/mod.rs b/iceoryx2-cal/src/dynamic_storage/mod.rs index 1fc28fd4c..3f26ab7da 100644 --- a/iceoryx2-cal/src/dynamic_storage/mod.rs +++ b/iceoryx2-cal/src/dynamic_storage/mod.rs @@ -57,24 +57,13 @@ use std::fmt::Debug; use iceoryx2_bb_memory::bump_allocator::BumpAllocator; -use iceoryx2_bb_posix::config::TEMP_DIRECTORY; use iceoryx2_bb_system_types::file_name::FileName; -use iceoryx2_bb_system_types::path::Path; use crate::static_storage::file::{NamedConcept, NamedConceptBuilder, NamedConceptMgmt}; pub mod posix_shared_memory; pub mod process_local; -/// The default suffix of every dynamic storage -pub const DEFAULT_SUFFIX: FileName = unsafe { FileName::new_unchecked(b".dyn") }; - -/// The default prefix of every dynamic storage -pub const DEFAULT_PREFIX: FileName = unsafe { FileName::new_unchecked(b"iox2_") }; - -/// The default path hint for every dynamic storage -pub const DEFAULT_PATH_HINT: Path = TEMP_DIRECTORY; - /// Describes failures when creating a new [`DynamicStorage`] #[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)] pub enum DynamicStorageCreateError { @@ -155,4 +144,9 @@ pub trait DynamicStorage: Sized + Debug + NamedConceptMgmt + Nam /// can be accessed by multiple processes concurrently therefore it must be constant or /// thread-safe. fn get(&self) -> &T; + + /// The default suffix of every dynamic storage + fn default_suffix() -> FileName { + unsafe { FileName::new_unchecked(b".dyn") } + } } diff --git a/iceoryx2-cal/src/dynamic_storage/posix_shared_memory.rs b/iceoryx2-cal/src/dynamic_storage/posix_shared_memory.rs index 05a07518b..0a28ec328 100644 --- a/iceoryx2-cal/src/dynamic_storage/posix_shared_memory.rs +++ b/iceoryx2-cal/src/dynamic_storage/posix_shared_memory.rs @@ -59,7 +59,7 @@ const IS_INITIALIZED_STATE_VALUE: u64 = 0xbeefaffedeadbeef; /// The builder of [`Storage`]. #[derive(Debug)] -pub struct Builder { +pub struct Builder { storage_name: FileName, supplementary_size: usize, has_ownership: bool, @@ -67,7 +67,7 @@ pub struct Builder { _phantom_data: PhantomData, } -#[derive(Clone, Debug)] +#[derive(Debug, Clone)] pub struct Configuration { suffix: FileName, prefix: FileName, @@ -83,9 +83,9 @@ struct Data { impl Default for Configuration { fn default() -> Self { Self { - path: DEFAULT_PATH_HINT, - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, + path: Storage::<()>::default_path_hint(), + suffix: Storage::<()>::default_suffix(), + prefix: Storage::<()>::default_prefix(), } } } diff --git a/iceoryx2-cal/src/dynamic_storage/process_local.rs b/iceoryx2-cal/src/dynamic_storage/process_local.rs index cfd665628..857cad362 100644 --- a/iceoryx2-cal/src/dynamic_storage/process_local.rs +++ b/iceoryx2-cal/src/dynamic_storage/process_local.rs @@ -67,7 +67,7 @@ struct StorageDetails { layout: Layout, } -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct Configuration { suffix: FileName, prefix: FileName, @@ -77,9 +77,9 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Self { - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, - path_hint: DEFAULT_PATH_HINT, + suffix: Storage::<()>::default_suffix(), + prefix: Storage::<()>::default_prefix(), + path_hint: Storage::<()>::default_path_hint(), } } } @@ -273,7 +273,7 @@ impl Drop for Storage { } #[derive(Debug)] -pub struct Builder { +pub struct Builder { name: FileName, supplementary_size: usize, has_ownership: bool, diff --git a/iceoryx2-cal/src/event/mod.rs b/iceoryx2-cal/src/event/mod.rs index 121f3ee1f..72374a3f8 100644 --- a/iceoryx2-cal/src/event/mod.rs +++ b/iceoryx2-cal/src/event/mod.rs @@ -16,7 +16,6 @@ pub mod unix_datagram_socket; use std::{fmt::Debug, time::Duration}; pub use crate::named_concept::{NamedConcept, NamedConceptBuilder, NamedConceptMgmt}; -use iceoryx2_bb_posix::config::TEMP_DIRECTORY; pub use iceoryx2_bb_system_types::file_name::FileName; pub use iceoryx2_bb_system_types::path::Path; @@ -78,15 +77,6 @@ impl std::fmt::Display for ListenerCreateError { impl std::error::Error for ListenerCreateError {} -/// The default suffix of every event -pub const DEFAULT_SUFFIX: FileName = unsafe { FileName::new_unchecked(b".event") }; - -/// The default prefix of every event -pub const DEFAULT_PREFIX: FileName = unsafe { FileName::new_unchecked(b"iox2_") }; - -/// The default path hint for every event -pub const DEFAULT_PATH_HINT: Path = TEMP_DIRECTORY; - pub trait TriggerId: Debug + Copy {} impl TriggerId for u64 {} @@ -117,4 +107,9 @@ pub trait Event: Sized + NamedConceptMgmt + Debug { type NotifierBuilder: NotifierBuilder; type Listener: Listener; type ListenerBuilder: ListenerBuilder; + + /// The default suffix of every event + fn default_suffix() -> FileName { + unsafe { FileName::new_unchecked(b".event") } + } } diff --git a/iceoryx2-cal/src/event/process_local.rs b/iceoryx2-cal/src/event/process_local.rs index d9f839191..2300b29de 100644 --- a/iceoryx2-cal/src/event/process_local.rs +++ b/iceoryx2-cal/src/event/process_local.rs @@ -64,23 +64,25 @@ static PROCESS_LOCAL_STORAGE: Lazy>> = Laz }); #[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub struct Configuration { +pub struct Configuration { suffix: FileName, prefix: FileName, path: Path, + _phantom: PhantomData, } -impl Default for Configuration { +impl Default for Configuration { fn default() -> Self { Self { - path: DEFAULT_PATH_HINT, - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, + path: EventImpl::::default_path_hint(), + suffix: EventImpl::::default_suffix(), + prefix: EventImpl::::default_prefix(), + _phantom: PhantomData, } } } -impl NamedConceptConfiguration for Configuration { +impl NamedConceptConfiguration for Configuration { fn prefix(mut self, value: FileName) -> Self { self.prefix = value; self @@ -114,7 +116,7 @@ pub struct Duplex { name: FileName, management: Arc>, has_ownership: bool, - config: Configuration, + config: Configuration, } impl NamedConcept for Duplex { @@ -150,7 +152,7 @@ impl Notifier for Duplex { impl Drop for Duplex { fn drop(&mut self) { if self.has_ownership { - fatal_panic!(from self, when unsafe { Event::::remove_cfg(&self.name, &self.config) }, + fatal_panic!(from self, when unsafe { EventImpl::::remove_cfg(&self.name, &self.config) }, "This should never happen! Unable to remove resources."); } } @@ -206,11 +208,13 @@ impl Listener for Duplex { #[derive(Debug)] pub struct Builder { name: FileName, - config: Configuration, + config: Configuration, _data: PhantomData, } -impl NamedConceptBuilder> for Builder { +impl NamedConceptBuilder> + for Builder +{ fn new(name: &FileName) -> Self { Self { name: *name, @@ -219,13 +223,15 @@ impl NamedConceptBuilder> for Buil } } - fn config(mut self, config: &Configuration) -> Self { + fn config(mut self, config: &Configuration) -> Self { self.config = *config; self } } -impl NotifierBuilder> for Builder { +impl NotifierBuilder> + for Builder +{ fn open(self) -> Result, NotifierCreateError> { let msg = "Failed to open event"; @@ -255,7 +261,9 @@ impl NotifierBuilder } } -impl ListenerBuilder> for Builder { +impl ListenerBuilder> + for Builder +{ fn create(self) -> Result, ListenerCreateError> { let msg = "Failed to create event"; @@ -322,19 +330,19 @@ impl ListenerBuilder } #[derive(Debug)] -pub struct Event { +pub struct EventImpl { _data: PhantomData, } -impl crate::event::Event for Event { +impl crate::event::Event for EventImpl { type Notifier = Duplex; type Listener = Duplex; type NotifierBuilder = Builder; type ListenerBuilder = Builder; } -impl NamedConceptMgmt for Event { - type Configuration = Configuration; +impl NamedConceptMgmt for EventImpl { + type Configuration = Configuration; fn does_exist_cfg( name: &FileName, diff --git a/iceoryx2-cal/src/event/unix_datagram_socket.rs b/iceoryx2-cal/src/event/unix_datagram_socket.rs index d44a24b4d..699d37ece 100644 --- a/iceoryx2-cal/src/event/unix_datagram_socket.rs +++ b/iceoryx2-cal/src/event/unix_datagram_socket.rs @@ -22,23 +22,25 @@ use iceoryx2_bb_posix::{ pub use iceoryx2_bb_system_types::file_name::FileName; #[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub struct Configuration { +pub struct Configuration { suffix: FileName, prefix: FileName, path: Path, + _phantom: PhantomData, } -impl Default for Configuration { +impl Default for Configuration { fn default() -> Self { Self { - path: DEFAULT_PATH_HINT, - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, + path: EventImpl::::default_path_hint(), + suffix: EventImpl::::default_suffix(), + prefix: EventImpl::::default_prefix(), + _phantom: PhantomData, } } } -impl NamedConceptConfiguration for Configuration { +impl NamedConceptConfiguration for Configuration { fn prefix(mut self, value: FileName) -> Self { self.prefix = value; self @@ -67,19 +69,21 @@ impl NamedConceptConfiguration for Configuration { } } -impl From for crate::communication_channel::unix_datagram::Configuration { - fn from(value: Configuration) -> Self { +impl From> + for crate::communication_channel::unix_datagram::Configuration +{ + fn from(value: Configuration) -> Self { Self::default().suffix(value.suffix).path_hint(value.path) } } #[derive(Debug)] -pub struct Event { +pub struct EventImpl { _data: PhantomData, } -impl NamedConceptMgmt for Event { - type Configuration = Configuration; +impl NamedConceptMgmt for EventImpl { + type Configuration = Configuration; fn does_exist_cfg( name: &FileName, @@ -105,7 +109,7 @@ impl NamedConceptMgmt for Event { } } -impl crate::event::Event for Event { +impl crate::event::Event for EventImpl { type Notifier = Notifier; type Listener = Listener; type NotifierBuilder = NotifierBuilder; @@ -147,11 +151,13 @@ impl crate::event::Notifier for Notifier #[derive(Debug)] pub struct NotifierBuilder { name: FileName, - config: Configuration, + config: Configuration, _data: PhantomData, } -impl NamedConceptBuilder> for NotifierBuilder { +impl NamedConceptBuilder> + for NotifierBuilder +{ fn new(name: &FileName) -> Self { Self { name: *name, @@ -160,13 +166,13 @@ impl NamedConceptBuilder> for Noti } } - fn config(mut self, config: &Configuration) -> Self { + fn config(mut self, config: &Configuration) -> Self { self.config = *config; self } } -impl crate::event::NotifierBuilder> +impl crate::event::NotifierBuilder> for NotifierBuilder { fn open(self) -> Result, NotifierCreateError> { @@ -275,11 +281,13 @@ impl crate::event::Listener for Listener #[derive(Debug)] pub struct ListenerBuilder { name: FileName, - config: Configuration, + config: Configuration, _data: PhantomData, } -impl NamedConceptBuilder> for ListenerBuilder { +impl NamedConceptBuilder> + for ListenerBuilder +{ fn new(name: &FileName) -> Self { Self { name: *name, @@ -288,13 +296,13 @@ impl NamedConceptBuilder> for List } } - fn config(mut self, config: &Configuration) -> Self { + fn config(mut self, config: &Configuration) -> Self { self.config = *config; self } } -impl crate::event::ListenerBuilder> +impl crate::event::ListenerBuilder> for ListenerBuilder { fn create(self) -> Result, ListenerCreateError> { diff --git a/iceoryx2-cal/src/named_concept.rs b/iceoryx2-cal/src/named_concept.rs index 2dfdf4cab..820e752ef 100644 --- a/iceoryx2-cal/src/named_concept.rs +++ b/iceoryx2-cal/src/named_concept.rs @@ -178,4 +178,14 @@ pub trait NamedConceptMgmt { /// Returns a list of all available concepts with a custom configuration. fn list_cfg(cfg: &Self::Configuration) -> Result, NamedConceptListError>; + + /// The default prefix of every zero copy connection + fn default_prefix() -> FileName { + unsafe { FileName::new_unchecked(b"iox2_") } + } + + /// The default path hint for every zero copy connection + fn default_path_hint() -> Path { + iceoryx2_bb_posix::config::temp_directory() + } } diff --git a/iceoryx2-cal/src/shared_memory/mod.rs b/iceoryx2-cal/src/shared_memory/mod.rs index 96b56c61e..d9e819d53 100644 --- a/iceoryx2-cal/src/shared_memory/mod.rs +++ b/iceoryx2-cal/src/shared_memory/mod.rs @@ -60,21 +60,10 @@ pub mod process_local; use std::fmt::Debug; use iceoryx2_bb_elementary::allocator::DeallocationError; -use iceoryx2_bb_posix::config::TEMP_DIRECTORY; pub use crate::shm_allocator::*; use crate::static_storage::file::{NamedConcept, NamedConceptBuilder, NamedConceptMgmt}; use iceoryx2_bb_system_types::file_name::FileName; -use iceoryx2_bb_system_types::path::Path; - -/// The default suffix of every shared memory -pub const DEFAULT_SUFFIX: FileName = unsafe { FileName::new_unchecked(b".shm") }; - -/// The default prefix of every shared memory -pub const DEFAULT_PREFIX: FileName = unsafe { FileName::new_unchecked(b"iox2_") }; - -/// The default path hint for every shared memory -pub const DEFAULT_PATH_HINT: Path = TEMP_DIRECTORY; /// Failure returned by [`SharedMemoryBuilder::create()`] #[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)] @@ -159,4 +148,9 @@ pub trait SharedMemory: /// Releases the ownership of the [`SharedMemory`] meaning when it goes out of scope the /// underlying resource will not be removed. fn release_ownership(&mut self); + + /// The default suffix of every shared memory + fn default_suffix() -> FileName { + unsafe { FileName::new_unchecked(b".shm") } + } } diff --git a/iceoryx2-cal/src/shared_memory/posix.rs b/iceoryx2-cal/src/shared_memory/posix.rs index 8e0ef2f2a..345b7904f 100644 --- a/iceoryx2-cal/src/shared_memory/posix.rs +++ b/iceoryx2-cal/src/shared_memory/posix.rs @@ -32,30 +32,46 @@ use crate::static_storage::file::{ const IS_INITIALIZED_STATE_VALUE: u64 = 0xbeefaffedeadbeef; -#[derive(Clone, Debug)] -pub struct Configuration { +#[derive(Debug)] +pub struct Configuration { pub is_memory_locked: bool, pub permission: Permission, pub zero_memory: bool, path: Path, suffix: FileName, prefix: FileName, + _phantom: PhantomData, } -impl Default for Configuration { +impl Default for Configuration { fn default() -> Self { Self { is_memory_locked: false, permission: Permission::OWNER_ALL, zero_memory: true, - path: DEFAULT_PATH_HINT, - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, + path: Memory::::default_path_hint(), + suffix: Memory::::default_suffix(), + prefix: Memory::::default_prefix(), + _phantom: PhantomData, + } + } +} + +impl Clone for Configuration { + fn clone(&self) -> Self { + Self { + is_memory_locked: self.is_memory_locked, + permission: self.permission, + zero_memory: self.zero_memory, + path: self.path, + suffix: self.suffix, + prefix: self.prefix, + _phantom: self._phantom, } } } -impl NamedConceptConfiguration for Configuration { +impl NamedConceptConfiguration for Configuration { fn prefix(mut self, value: FileName) -> Self { self.prefix = value; self @@ -88,7 +104,7 @@ impl NamedConceptConfiguration for Configuration { pub struct Builder { name: FileName, size: usize, - config: Configuration, + config: Configuration, _phantom_allocator: PhantomData, } @@ -116,7 +132,7 @@ impl NamedConceptBuilder> } } - fn config(mut self, config: &Configuration) -> Self { + fn config(mut self, config: &Configuration) -> Self { self.config = config.clone(); self } @@ -305,7 +321,7 @@ impl NamedConcept for Memory { } impl NamedConceptMgmt for Memory { - type Configuration = Configuration; + type Configuration = Configuration; fn does_exist_cfg( name: &FileName, diff --git a/iceoryx2-cal/src/shared_memory/process_local.rs b/iceoryx2-cal/src/shared_memory/process_local.rs index 7f1b37c79..a12bcae04 100644 --- a/iceoryx2-cal/src/shared_memory/process_local.rs +++ b/iceoryx2-cal/src/shared_memory/process_local.rs @@ -69,24 +69,37 @@ static PROCESS_LOCAL_STORAGE: Lazy { suffix: FileName, prefix: FileName, path: Path, + _phantom: PhantomData, } -impl Default for Configuration { +impl Default for Configuration { fn default() -> Self { Self { - path: DEFAULT_PATH_HINT, - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, + path: Memory::::default_path_hint(), + suffix: Memory::::default_suffix(), + prefix: Memory::::default_prefix(), + _phantom: PhantomData, + } + } +} + +impl Clone for Configuration { + fn clone(&self) -> Self { + Self { + suffix: self.suffix, + prefix: self.prefix, + path: self.path, + _phantom: self._phantom, } } } -impl NamedConceptConfiguration for Configuration { +impl NamedConceptConfiguration for Configuration { fn prefix(mut self, value: FileName) -> Self { self.prefix = value; self @@ -119,7 +132,7 @@ impl NamedConceptConfiguration for Configuration { pub struct Builder { name: FileName, size: usize, - config: Configuration, + config: Configuration, _phantom_allocator: PhantomData, } @@ -135,7 +148,7 @@ impl NamedConceptBuilder> } } - fn config(mut self, config: &Configuration) -> Self { + fn config(mut self, config: &Configuration) -> Self { self.config = config.clone(); self } @@ -260,7 +273,7 @@ pub struct Memory { name: FileName, shm: Arc, has_ownership: bool, - config: Configuration, + config: Configuration, _phantom_data: PhantomData, } @@ -288,7 +301,7 @@ impl NamedConcept for Memory { } impl NamedConceptMgmt for Memory { - type Configuration = Configuration; + type Configuration = Configuration; fn list_cfg( config: &Self::Configuration, diff --git a/iceoryx2-cal/src/shared_memory_directory/file_reference_set.rs b/iceoryx2-cal/src/shared_memory_directory/file_reference_set.rs index 878b9d2e9..9c36c98bc 100644 --- a/iceoryx2-cal/src/shared_memory_directory/file_reference_set.rs +++ b/iceoryx2-cal/src/shared_memory_directory/file_reference_set.rs @@ -27,7 +27,7 @@ pub(crate) struct FileReferenceSetId(usize); #[derive(Debug, Clone, Copy)] #[repr(C)] struct Entry { - name: FileName, + name: Option, offset: usize, len: usize, } @@ -35,7 +35,7 @@ struct Entry { impl Entry { const fn default() -> Self { Self { - name: unsafe { FileName::new_unchecked(b"empty") }, + name: None, offset: 0, len: 0, } @@ -94,7 +94,7 @@ impl FileReferenceSet { unsafe { self.entries[id].get().write(Entry { - name: *name, + name: Some(*name), offset, len, }) @@ -115,7 +115,7 @@ impl FileReferenceSet { } if self.counter[i].increment_ref_counter_when_exist() { - if unsafe { &*self.entries[i].get() }.name == *name + if unsafe { &*self.entries[i].get() }.name == Some(*name) && !self.decision_counter[i].does_value_win(current_decision_count) { if self.counter[i].is_initialized() { @@ -194,7 +194,7 @@ impl FileReferenceSet { } pub(crate) fn get_name(&self, id: FileReferenceSetId) -> FileName { - unsafe { &*self.entries[id.0].get() }.name + unsafe { &*self.entries[id.0].get() }.name.unwrap() } pub(crate) fn get_payload(&self, id: FileReferenceSetId, base_address: usize) -> &[u8] { @@ -223,7 +223,7 @@ impl FileReferenceSet { fn find_entry(&self, name: &FileName) -> Option { for id in 0..self.ids.capacity() as usize { if self.counter[id].increment_ref_counter_when_initialized() { - if unsafe { *self.entries[id].get() }.name == *name { + if unsafe { *self.entries[id].get() }.name == Some(*name) { return Some(FileReferenceSetId(id)); } diff --git a/iceoryx2-cal/src/shared_memory_directory/mod.rs b/iceoryx2-cal/src/shared_memory_directory/mod.rs index d1e8c3538..2b25a25c3 100644 --- a/iceoryx2-cal/src/shared_memory_directory/mod.rs +++ b/iceoryx2-cal/src/shared_memory_directory/mod.rs @@ -26,8 +26,8 @@ use std::{alloc::Layout, fmt::Debug, marker::PhantomData}; use crate::shared_memory_directory::file::{File, FileCreator}; const MAX_NUMBER_OF_ENTRIES: usize = 512; -const MGMT_SHM_SUFFIX: FileName = unsafe { FileName::new_unchecked(b".dm") }; -const DATA_SHM_SUFFIX: FileName = unsafe { FileName::new_unchecked(b".dd") }; +const MGMT_SHM_SUFFIX: &[u8] = b".dm"; +const DATA_SHM_SUFFIX: &[u8] = b".dd"; #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum SharedMemoryDirectoryCreateFileError { @@ -75,7 +75,7 @@ impl SharedMemoryDirectoryCreator { when MgmtShm::Builder::new(&self.name) .config( &MgmtShm::Configuration::default() - .suffix(MGMT_SHM_SUFFIX), + .suffix(unsafe {FileName::new_unchecked(MGMT_SHM_SUFFIX)}), ) .size(core::mem::size_of::() + core::mem::align_of::() - 1) .create(&::Configuration::default()), @@ -92,7 +92,7 @@ impl SharedMemoryDirectoryCreator { let mut data_shm = fail!(from self, when DataShm::Builder::new(&self.name).config( &DataShm::Configuration::default() - .suffix(DATA_SHM_SUFFIX), + .suffix(unsafe{FileName::new_unchecked(DATA_SHM_SUFFIX)}), ).size(self.size).create(allocator_config), "{} since the data segment could not be created.", msg); @@ -120,7 +120,7 @@ impl SharedMemoryDirectoryCreator { let data_shm = fail!(from self, when DataShm::Builder::new(&self.name) .config( &DataShm::Configuration::default() - .suffix(DATA_SHM_SUFFIX), + .suffix(unsafe{FileName::new_unchecked(DATA_SHM_SUFFIX)}), ) .open(), "{} since the data segment could not be opened.", msg); @@ -128,7 +128,7 @@ impl SharedMemoryDirectoryCreator { let mgmt_shm = fail!(from self, when MgmtShm::Builder::new(&self.name) .config( &MgmtShm::Configuration::default() - .suffix(MGMT_SHM_SUFFIX), + .suffix(unsafe{FileName::new_unchecked(MGMT_SHM_SUFFIX)}), ) .open(), "{} since the management segment could not be opened.", msg); @@ -217,14 +217,14 @@ impl< if !fail!(from origin, when DataShm::does_exist_cfg( name, - &DataShm::Configuration::default().suffix(DATA_SHM_SUFFIX)), + &DataShm::Configuration::default().suffix(unsafe{FileName::new_unchecked(DATA_SHM_SUFFIX)})), "{} \"{}\" exists due to a failure while checking the data segment.", msg, name) { return Ok(false); } let mgmt_result = fail!(from origin, - when MgmtShm::does_exist_cfg(name, &MgmtShm::Configuration::default().suffix(MGMT_SHM_SUFFIX)), + when MgmtShm::does_exist_cfg(name, &MgmtShm::Configuration::default().suffix(unsafe{FileName::new_unchecked(MGMT_SHM_SUFFIX)})), "{} \"{}\" exists due to a failure while checking the management segment.", msg, name ); @@ -236,7 +236,7 @@ impl< let origin = "SharedMemoryDirectory::list()"; Ok(fail!(from origin, when DataShm::list_cfg( - &DataShm::Configuration::default().suffix(DATA_SHM_SUFFIX) + &DataShm::Configuration::default().suffix(unsafe{FileName::new_unchecked(DATA_SHM_SUFFIX)}) ), "{} since the data segments could not be listed.", msg)) } @@ -250,14 +250,14 @@ impl< if !fail!(from origin, when DataShm::remove_cfg( name, - &DataShm::Configuration::default().suffix(DATA_SHM_SUFFIX)), + &DataShm::Configuration::default().suffix(unsafe{FileName::new_unchecked(DATA_SHM_SUFFIX)})), "{} \"{}\" since the data segment could not be removed.", msg, name) { return Ok(false); } let mgmt_result = fail!(from origin, - when MgmtShm::remove_cfg(name, &MgmtShm::Configuration::default().suffix(MGMT_SHM_SUFFIX)), + when MgmtShm::remove_cfg(name, &MgmtShm::Configuration::default().suffix(unsafe{FileName::new_unchecked(MGMT_SHM_SUFFIX)})), "{} \"{}\" since the management segment could not be removed.", msg, name ); diff --git a/iceoryx2-cal/src/static_storage/file.rs b/iceoryx2-cal/src/static_storage/file.rs index 4799accf1..a922548a4 100644 --- a/iceoryx2-cal/src/static_storage/file.rs +++ b/iceoryx2-cal/src/static_storage/file.rs @@ -64,9 +64,9 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Configuration { - path: DEFAULT_PATH_HINT, - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, + path: Storage::default_path_hint(), + suffix: Storage::default_suffix(), + prefix: Storage::default_prefix(), } } } diff --git a/iceoryx2-cal/src/static_storage/mod.rs b/iceoryx2-cal/src/static_storage/mod.rs index e07349c44..a00072c9e 100644 --- a/iceoryx2-cal/src/static_storage/mod.rs +++ b/iceoryx2-cal/src/static_storage/mod.rs @@ -19,23 +19,12 @@ pub mod process_local; use std::fmt::Debug; use iceoryx2_bb_log::fail; -use iceoryx2_bb_posix::config::TEMP_DIRECTORY; use iceoryx2_bb_system_types::file_name::FileName; -use iceoryx2_bb_system_types::path::Path; use crate::named_concept::{ NamedConcept, NamedConceptBuilder, NamedConceptConfiguration, NamedConceptMgmt, }; -/// The default suffix of every static storage -pub const DEFAULT_SUFFIX: FileName = unsafe { FileName::new_unchecked(b".static_storage") }; - -/// The default prefix of every static storage -pub const DEFAULT_PREFIX: FileName = unsafe { FileName::new_unchecked(b"iox2_") }; - -/// The default path hint for every static storage -pub const DEFAULT_PATH_HINT: Path = TEMP_DIRECTORY; - #[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)] pub enum StaticStorageCreateError { AlreadyExists, @@ -131,4 +120,9 @@ pub trait StaticStorage: Debug + Sized + NamedConceptMgmt + NamedConcept { /// Acquires the ownership of the static storage. If the object goes out of scope the /// underlying resources are removed. fn acquire_ownership(&mut self); + + /// The default suffix of every static storage + fn default_suffix() -> FileName { + unsafe { FileName::new_unchecked(b".static_storage") } + } } diff --git a/iceoryx2-cal/src/static_storage/process_local.rs b/iceoryx2-cal/src/static_storage/process_local.rs index bb1b2f095..5f399bace 100644 --- a/iceoryx2-cal/src/static_storage/process_local.rs +++ b/iceoryx2-cal/src/static_storage/process_local.rs @@ -79,9 +79,9 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Self { - path: DEFAULT_PATH_HINT, - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, + path: Storage::default_path_hint(), + suffix: Storage::default_suffix(), + prefix: Storage::default_prefix(), } } } diff --git a/iceoryx2-cal/src/zero_copy_connection/mod.rs b/iceoryx2-cal/src/zero_copy_connection/mod.rs index 8a5f90da2..b8e655dbf 100644 --- a/iceoryx2-cal/src/zero_copy_connection/mod.rs +++ b/iceoryx2-cal/src/zero_copy_connection/mod.rs @@ -17,7 +17,6 @@ use std::fmt::Debug; pub use crate::shared_memory::PointerOffset; use crate::static_storage::file::{NamedConcept, NamedConceptBuilder, NamedConceptMgmt}; -use iceoryx2_bb_posix::config::TEMP_DIRECTORY; pub use iceoryx2_bb_system_types::file_name::FileName; pub use iceoryx2_bb_system_types::path::Path; @@ -94,15 +93,6 @@ pub const DEFAULT_BUFFER_SIZE: usize = 4; pub const DEFAULT_ENABLE_SAFE_OVERFLOW: bool = false; pub const DEFAULT_MAX_BORROWED_SAMPLES: usize = 4; -/// The default suffix of every zero copy connection -pub const DEFAULT_SUFFIX: FileName = unsafe { FileName::new_unchecked(b".rx") }; - -/// The default prefix of every zero copy connection -pub const DEFAULT_PREFIX: FileName = unsafe { FileName::new_unchecked(b"iox2_") }; - -/// The default path hint for every zero copy connection -pub const DEFAULT_PATH_HINT: Path = TEMP_DIRECTORY; - pub trait ZeroCopyConnectionBuilder: NamedConceptBuilder { fn buffer_size(self, value: usize) -> Self; fn enable_safe_overflow(self, value: bool) -> Self; @@ -147,4 +137,9 @@ pub trait ZeroCopyConnection: Sized + NamedConceptMgmt { fn has_configurable_buffer_size() -> bool { false } + + /// The default suffix of every zero copy connection + fn default_suffix() -> FileName { + unsafe { FileName::new_unchecked(b".rx") } + } } diff --git a/iceoryx2-cal/src/zero_copy_connection/posix_shared_memory.rs b/iceoryx2-cal/src/zero_copy_connection/posix_shared_memory.rs index 7a4b39eb6..4de1d2238 100644 --- a/iceoryx2-cal/src/zero_copy_connection/posix_shared_memory.rs +++ b/iceoryx2-cal/src/zero_copy_connection/posix_shared_memory.rs @@ -43,9 +43,9 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Self { - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, - path_hint: DEFAULT_PATH_HINT, + suffix: Connection::default_suffix(), + prefix: Connection::default_prefix(), + path_hint: Connection::default_path_hint(), } } } diff --git a/iceoryx2-cal/src/zero_copy_connection/process_local.rs b/iceoryx2-cal/src/zero_copy_connection/process_local.rs index 9ff5bb362..401d55013 100644 --- a/iceoryx2-cal/src/zero_copy_connection/process_local.rs +++ b/iceoryx2-cal/src/zero_copy_connection/process_local.rs @@ -57,9 +57,9 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Self { - suffix: DEFAULT_SUFFIX, - prefix: DEFAULT_PREFIX, - path_hint: DEFAULT_PATH_HINT, + suffix: Connection::default_suffix(), + prefix: Connection::default_prefix(), + path_hint: Connection::default_path_hint(), } } } diff --git a/iceoryx2-cal/tests/communication_channel_trait_tests.rs b/iceoryx2-cal/tests/communication_channel_trait_tests.rs index 7517c0bb2..9c04c5e92 100644 --- a/iceoryx2-cal/tests/communication_channel_trait_tests.rs +++ b/iceoryx2-cal/tests/communication_channel_trait_tests.rs @@ -416,8 +416,9 @@ mod communication_channel { #[test] fn defaults_for_configuration_are_set_correctly>() { let config = ::Configuration::default(); - assert_that!(*config.get_suffix(), eq DEFAULT_SUFFIX); - assert_that!(*config.get_path_hint(), eq DEFAULT_PATH_HINT); + assert_that!(*config.get_suffix(), eq Sut::default_suffix()); + assert_that!(*config.get_path_hint(), eq Sut::default_path_hint()); + assert_that!(*config.get_prefix(), eq Sut::default_prefix()); } //#[cfg(not(any(target_os = "windows")))] diff --git a/iceoryx2-cal/tests/dynamic_storage_trait_tests.rs b/iceoryx2-cal/tests/dynamic_storage_trait_tests.rs index 4f6b385a2..c0a367d53 100644 --- a/iceoryx2-cal/tests/dynamic_storage_trait_tests.rs +++ b/iceoryx2-cal/tests/dynamic_storage_trait_tests.rs @@ -438,8 +438,9 @@ mod dynamic_storage { #[test] fn defaults_for_configuration_are_set_correctly>() { let config = ::Configuration::default(); - assert_that!(*config.get_suffix(), eq DEFAULT_SUFFIX); - assert_that!(*config.get_path_hint(), eq DEFAULT_PATH_HINT); + assert_that!(*config.get_suffix(), eq Sut::default_suffix()); + assert_that!(*config.get_path_hint(), eq Sut::default_path_hint()); + assert_that!(*config.get_prefix(), eq Sut::default_prefix()); } #[instantiate_tests(>)] diff --git a/iceoryx2-cal/tests/event_tests.rs b/iceoryx2-cal/tests/event_tests.rs index c2c64e146..438786e61 100644 --- a/iceoryx2-cal/tests/event_tests.rs +++ b/iceoryx2-cal/tests/event_tests.rs @@ -384,13 +384,14 @@ mod event { #[test] fn defaults_for_configuration_are_set_correctly>() { let config = ::Configuration::default(); - assert_that!(*config.get_suffix(), eq DEFAULT_SUFFIX); - assert_that!(*config.get_path_hint(), eq DEFAULT_PATH_HINT); + assert_that!(*config.get_suffix(), eq Sut::default_suffix()); + assert_that!(*config.get_path_hint(), eq Sut::default_path_hint()); + assert_that!(*config.get_prefix(), eq Sut::default_prefix()); } - #[instantiate_tests(>)] + #[instantiate_tests(>)] mod unix_datagram {} - #[instantiate_tests(>)] + #[instantiate_tests(>)] mod process_local {} } diff --git a/iceoryx2-cal/tests/shared_memory_trait_tests.rs b/iceoryx2-cal/tests/shared_memory_trait_tests.rs index 673f82a8e..c5f9ba5d2 100644 --- a/iceoryx2-cal/tests/shared_memory_trait_tests.rs +++ b/iceoryx2-cal/tests/shared_memory_trait_tests.rs @@ -299,8 +299,9 @@ mod shared_memory { #[test] fn defaults_for_configuration_are_set_correctly>() { let config = ::Configuration::default(); - assert_that!(*config.get_suffix(), eq DEFAULT_SUFFIX); - assert_that!(*config.get_path_hint(), eq DEFAULT_PATH_HINT); + assert_that!(*config.get_suffix(), eq Sut::default_suffix()); + assert_that!(*config.get_path_hint(), eq Sut::default_path_hint()); + assert_that!(*config.get_prefix(), eq Sut::default_prefix()); } #[instantiate_tests(>)] diff --git a/iceoryx2-cal/tests/static_storage_file_tests.rs b/iceoryx2-cal/tests/static_storage_file_tests.rs index 7cfe77398..91ccca7fc 100644 --- a/iceoryx2-cal/tests/static_storage_file_tests.rs +++ b/iceoryx2-cal/tests/static_storage_file_tests.rs @@ -40,7 +40,7 @@ fn static_storage_file_custom_path_and_suffix_works() { let content = "some storage content".to_string(); let config = Configuration::default() .suffix(unsafe { FileName::new_unchecked(b".blubbme") }) - .path_hint(TEST_DIRECTORY); + .path_hint(test_directory()); let storage_guard = Builder::new(&storage_name) .config(&config) @@ -66,7 +66,7 @@ fn static_storage_file_path_is_created_when_it_does_not_exist() { let storage_name = generate_name(); let content = "some more funky content".to_string(); let non_existing_path = - FilePath::from_path_and_file(&TEST_DIRECTORY, &generate_name()).unwrap(); + FilePath::from_path_and_file(&test_directory(), &generate_name()).unwrap(); Directory::remove(&non_existing_path.into()).ok(); let config = Configuration::default() @@ -97,9 +97,12 @@ fn static_storage_file_custom_path_and_suffix_list_storage_works() { let config = Configuration::default() .suffix(unsafe { FileName::new_unchecked(b".blubbme") }) .path_hint( - FilePath::from_path_and_file(&TEST_DIRECTORY, &FileName::new(b"non_existing").unwrap()) - .unwrap() - .into(), + FilePath::from_path_and_file( + &test_directory(), + &FileName::new(b"non_existing").unwrap(), + ) + .unwrap() + .into(), ); let content = "some storage content".to_string(); @@ -117,7 +120,8 @@ fn static_storage_file_custom_path_and_suffix_list_storage_works() { let mut some_files = vec![]; for _i in 0..NUMBER_OF_STORAGES { - let storage_name = FilePath::from_path_and_file(&TEST_DIRECTORY, &generate_name()).unwrap(); + let storage_name = + FilePath::from_path_and_file(&test_directory(), &generate_name()).unwrap(); FileBuilder::new(&storage_name) .creation_mode(CreationMode::PurgeAndCreate) .create() diff --git a/iceoryx2-cal/tests/static_storage_trait_tests.rs b/iceoryx2-cal/tests/static_storage_trait_tests.rs index c6d764a32..4481451bb 100644 --- a/iceoryx2-cal/tests/static_storage_trait_tests.rs +++ b/iceoryx2-cal/tests/static_storage_trait_tests.rs @@ -456,8 +456,9 @@ mod static_storage { #[test] fn defaults_for_configuration_are_set_correctly() { let config = ::Configuration::default(); - assert_that!(*config.get_suffix(), eq DEFAULT_SUFFIX); - assert_that!(*config.get_path_hint(), eq DEFAULT_PATH_HINT); + assert_that!(*config.get_suffix(), eq Sut::default_suffix()); + assert_that!(*config.get_path_hint(), eq Sut::default_path_hint()); + assert_that!(*config.get_prefix(), eq Sut::default_prefix()); } #[instantiate_tests()] diff --git a/iceoryx2-cal/tests/zero_copy_connection_trait_tests.rs b/iceoryx2-cal/tests/zero_copy_connection_trait_tests.rs index c8d5eaa19..e41dc0099 100644 --- a/iceoryx2-cal/tests/zero_copy_connection_trait_tests.rs +++ b/iceoryx2-cal/tests/zero_copy_connection_trait_tests.rs @@ -497,8 +497,9 @@ mod zero_copy_connection { #[test] fn defaults_for_configuration_are_set_correctly() { let config = ::Configuration::default(); - assert_that!(*config.get_suffix(), eq DEFAULT_SUFFIX); - assert_that!(*config.get_path_hint(), eq DEFAULT_PATH_HINT); + assert_that!(*config.get_suffix(), eq Sut::default_suffix()); + assert_that!(*config.get_path_hint(), eq Sut::default_path_hint()); + assert_that!(*config.get_prefix(), eq Sut::default_prefix()); } #[instantiate_tests()] diff --git a/iceoryx2/src/config.rs b/iceoryx2/src/config.rs index fbdc843db..60c793f75 100644 --- a/iceoryx2/src/config.rs +++ b/iceoryx2/src/config.rs @@ -102,13 +102,11 @@ use crate::service::port_factory::publisher::UnableToDeliverStrategy; /// Path to the default config file #[cfg(target_os = "windows")] -pub const DEFAULT_CONFIG_FILE: FilePath = - unsafe { FilePath::new_unchecked(b"config\\iceoryx2_win.toml") }; +pub const DEFAULT_CONFIG_FILE: &[u8] = b"config\\iceoryx2_win.toml"; /// Path to the default config file #[cfg(not(target_os = "windows"))] -pub const DEFAULT_CONFIG_FILE: FilePath = - unsafe { FilePath::new_unchecked(b"config/iceoryx2.toml") }; +pub const DEFAULT_CONFIG_FILE: &[u8] = b"config/iceoryx2.toml"; /// Failures occurring while creating a new [`Config`] object with [`Config::from_file()`] or /// [`Config::setup_global_config_from_file()`] @@ -331,7 +329,10 @@ impl Config { /// config was already populated. pub fn get_global_config() -> &'static Config { if !ICEORYX2_CONFIG.is_initialized() - && Config::setup_global_config_from_file(&DEFAULT_CONFIG_FILE).is_err() + && Config::setup_global_config_from_file(unsafe { + &FilePath::new_unchecked(DEFAULT_CONFIG_FILE) + }) + .is_err() { warn!(from "Config::get_global_config()", "Unable to load default config file, populate config with default values."); ICEORYX2_CONFIG.set_value(Config::default()); diff --git a/iceoryx2/src/service/process_local.rs b/iceoryx2/src/service/process_local.rs index c37baa5c9..9adc7708c 100644 --- a/iceoryx2/src/service/process_local.rs +++ b/iceoryx2/src/service/process_local.rs @@ -58,7 +58,7 @@ impl<'config> crate::service::Details<'config> for Service<'config> { type ServiceNameHasher = hash::sha1::Sha1; type SharedMemory = shared_memory::process_local::Memory; type Connection = zero_copy_connection::process_local::Connection; - type Event = event::process_local::Event; + type Event = event::process_local::EventImpl; fn from_state(state: ServiceState<'config, Self::StaticStorage, Self::DynamicStorage>) -> Self { Self { state } diff --git a/iceoryx2/src/service/zero_copy.rs b/iceoryx2/src/service/zero_copy.rs index ca93e991e..cfd63279d 100644 --- a/iceoryx2/src/service/zero_copy.rs +++ b/iceoryx2/src/service/zero_copy.rs @@ -58,7 +58,7 @@ impl<'config> crate::service::Details<'config> for Service<'config> { type ServiceNameHasher = hash::sha1::Sha1; type SharedMemory = shared_memory::posix::Memory; type Connection = zero_copy_connection::posix_shared_memory::Connection; - type Event = event::unix_datagram_socket::Event; + type Event = event::unix_datagram_socket::EventImpl; fn from_state(state: ServiceState<'config, Self::StaticStorage, Self::DynamicStorage>) -> Self { Self { state }