iceoryx2-cal 0.9.0

iceoryx2: [internal] high-level traits and implementations that represents OS primitives in an exchangeable fashion
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use alloc::format;
use alloc::vec;
use alloc::vec::Vec;
use core::ptr::NonNull;

use iceoryx2_bb_elementary_traits::non_null::NonNullCompat;
use iceoryx2_bb_elementary_traits::testing::abandonable::Abandonable;
use iceoryx2_bb_posix::file::Permission;
use iceoryx2_bb_posix::process_state::ProcessGuardBuilder;
use iceoryx2_bb_posix::process_state::ProcessMonitorOpenError;
use iceoryx2_bb_posix::{
    directory::{Directory, DirectoryOpenError, DirectoryReadError},
    file::{File, FileRemoveError},
    file_type::FileType,
    process_state::{
        ProcessCleaner, ProcessCleanerCreateError, ProcessGuard, ProcessGuardCreateError,
        ProcessMonitor, ProcessMonitorCreateError, ProcessMonitorStateError, ProcessState,
    },
};
use iceoryx2_bb_system_types::{file_name::FileName, path::Path};
use iceoryx2_log::fail;

use crate::{
    monitoring::{MonitoringCreateCleanerError, MonitoringCreateMonitorError, State},
    named_concept::{
        NamedConcept, NamedConceptBuilder, NamedConceptConfiguration, NamedConceptDoesExistError,
        NamedConceptListError, NamedConceptMgmt, NamedConceptRemoveError,
    },
};

use super::{
    Monitoring, MonitoringBuilder, MonitoringCleaner, MonitoringCreateTokenError,
    MonitoringMonitor, MonitoringStateError, MonitoringToken,
};

#[cfg(not(feature = "dev_permissions"))]
const GUARD_PERMISSIONS: Permission = Permission::OWNER_ALL;

#[cfg(not(feature = "dev_permissions"))]
const DIR_PERMISSIONS: Permission = Permission::OWNER_ALL
    .const_bitor(Permission::GROUP_READ)
    .const_bitor(Permission::GROUP_EXEC)
    .const_bitor(Permission::OTHERS_READ)
    .const_bitor(Permission::OTHERS_EXEC);

#[cfg(feature = "dev_permissions")]
const GUARD_PERMISSIONS: Permission = Permission::ALL;

#[cfg(feature = "dev_permissions")]
const DIR_PERMISSIONS: Permission = Permission::ALL;

#[derive(Debug)]
pub struct FileLockMonitoring {}

impl NamedConceptMgmt for FileLockMonitoring {
    type Configuration = Configuration;

    fn list_cfg(
        cfg: &Self::Configuration,
    ) -> Result<Vec<FileName>, crate::named_concept::NamedConceptListError> {
        let path = cfg.get_path_hint();
        let origin = "FileLockMonitoring::list_cfg()";
        let msg = format!("Unable to list all FileLockMonitoring instances in \"{path}\"");
        let directory = match Directory::new(path) {
            Ok(directory) => directory,
            Err(DirectoryOpenError::InsufficientPermissions) => {
                fail!(from origin, with NamedConceptListError::InsufficientPermissions,
                    "{} due to insufficient permissions to read the directory.", msg);
            }
            Err(DirectoryOpenError::DoesNotExist) => {
                return Ok(vec![]);
            }
            Err(v) => {
                fail!(from origin, with NamedConceptListError::InternalError,
                    "{} due to failure ({:?}) while reading the directory.", msg, v);
            }
        };

        let entries = fail!(from origin,
                            when directory.contents(),
                            map DirectoryReadError::InsufficientPermissions => NamedConceptListError::InsufficientPermissions,
                            unmatched NamedConceptListError::InternalError,
                            "{} due to a failure while reading the directory contents.", msg);

        Ok(entries
            .iter()
            .filter(|entry| {
                let metadata = entry.metadata();
                metadata.file_type() == FileType::File
            })
            .filter_map(|entry| cfg.extract_name_from_file(entry.name()))
            .collect())
    }

    fn does_exist_cfg(
        name: &FileName,
        cfg: &Self::Configuration,
    ) -> Result<bool, crate::named_concept::NamedConceptDoesExistError> {
        let process_state_path = cfg.path_for(name);
        let msg =
            format!("Unable to check if the FileLockMonitoring \"{process_state_path}\" exists");
        let origin = "FileLockMonitoring::does_exist_cfg()";

        match File::does_exist(&process_state_path) {
            Ok(v) => Ok(v),
            Err(e) => {
                fail!(from origin, with NamedConceptDoesExistError::InternalError,
                    "{} due to an internal failure ({:?}).", msg, e);
            }
        }
    }

    unsafe fn remove_cfg(
        name: &FileName,
        cfg: &Self::Configuration,
    ) -> Result<bool, crate::named_concept::NamedConceptRemoveError> {
        let process_state_path = cfg.path_for(name);
        let msg = format!("Unable to remove FileLockMonitoring \"{process_state_path}\"");
        let origin = "FileLockMonitoring::remove_cfg()";
        match unsafe { ProcessGuard::remove(&process_state_path) } {
            Ok(v) => Ok(v),
            Err(FileRemoveError::InsufficientPermissions) => {
                fail!(from origin, with NamedConceptRemoveError::InsufficientPermissions,
                        "{} due to insufficient permissions.", msg);
            }
            Err(v) => {
                fail!(from origin, with NamedConceptRemoveError::InternalError,
                        "{} due to an internal failure ({:?}).", msg, v);
            }
        }
    }

    fn remove_path_hint(
        value: &Path,
    ) -> Result<(), crate::named_concept::NamedConceptPathHintRemoveError> {
        crate::named_concept::remove_path_hint(value)
    }
}

#[derive(Debug)]
pub struct Cleaner {
    cleaner: ProcessCleaner,
    name: FileName,
}

impl Abandonable for Cleaner {
    unsafe fn abandon_in_place(mut this: NonNull<Self>) {
        let this = unsafe { this.as_mut() };
        unsafe { ProcessCleaner::abandon_in_place(NonNull::iox2_from_mut(&mut this.cleaner)) };
    }
}

impl NamedConcept for Cleaner {
    fn name(&self) -> &FileName {
        &self.name
    }
}

impl MonitoringCleaner for Cleaner {
    fn relinquish(self) {
        self.cleaner.abandon()
    }
}

#[derive(Debug)]
pub struct Token {
    guard: ProcessGuard,
    name: FileName,
}

impl Abandonable for Token {
    unsafe fn abandon_in_place(mut this: NonNull<Self>) {
        let this = unsafe { this.as_mut() };
        unsafe { ProcessGuard::abandon_in_place(NonNull::iox2_from_mut(&mut this.guard)) };
    }
}

impl NamedConcept for Token {
    fn name(&self) -> &FileName {
        &self.name
    }
}

impl MonitoringToken for Token {}

#[derive(Debug)]
pub struct Monitor {
    monitor: ProcessMonitor,
    name: FileName,
}

impl NamedConcept for Monitor {
    fn name(&self) -> &FileName {
        &self.name
    }
}

impl MonitoringMonitor for Monitor {
    fn state(&self) -> Result<super::State, MonitoringStateError> {
        let msg = "Unable to acquire monitor state";

        match self.monitor.state() {
            Ok(ProcessState::Alive) => Ok(State::Alive),
            Ok(ProcessState::Dead) | Ok(ProcessState::CleaningUp) => Ok(State::Dead),
            Ok(ProcessState::DoesNotExist) | Ok(ProcessState::Starting) => Ok(State::DoesNotExist),
            Err(ProcessMonitorStateError::Interrupt)
            | Err(ProcessMonitorStateError::ProcessMonitorOpenError(
                ProcessMonitorOpenError::Interrupt,
            )) => {
                fail!(from self, with MonitoringStateError::Interrupt,
                    "{} since an interrupt signal was received.", msg);
            }
            Err(ProcessMonitorStateError::ProcessMonitorOpenError(
                ProcessMonitorOpenError::InsufficientPermissions,
            )) => {
                fail!(from self, with MonitoringStateError::InsufficientPermissions,
                    "{} due to insufficient permissions.", msg);
            }
            Err(v) => {
                fail!(from self, with MonitoringStateError::InternalError,
                    "{} since an internal failure occurred ({:?}).", msg, v);
            }
        }
    }
}

#[derive(Debug)]
pub struct Builder {
    name: FileName,
    config: Configuration,
}

impl NamedConceptBuilder<FileLockMonitoring> for Builder {
    fn new(name: &FileName) -> Self {
        Self {
            name: *name,
            config: Configuration::default(),
        }
    }

    fn config(mut self, config: &<FileLockMonitoring as NamedConceptMgmt>::Configuration) -> Self {
        self.config = config.clone();
        self
    }
}

impl MonitoringBuilder<FileLockMonitoring> for Builder {
    fn token(
        self,
    ) -> Result<<FileLockMonitoring as super::Monitoring>::Token, super::MonitoringCreateTokenError>
    {
        let msg = "Unable to create FileLockMonitoring token";
        let process_state_path = self.config.path_for(&self.name);
        match ProcessGuardBuilder::new()
            .guard_permissions(GUARD_PERMISSIONS)
            .directory_permissions(DIR_PERMISSIONS)
            .create(&process_state_path)
        {
            Ok(guard) => Ok(Token {
                guard,
                name: self.name,
            }),
            Err(ProcessGuardCreateError::InsufficientPermissions) => {
                fail!(from self, with MonitoringCreateTokenError::InsufficientPermissions,
                    "{} due to insufficient permissions.", msg);
            }
            Err(ProcessGuardCreateError::AlreadyExists) => {
                fail!(from self, with MonitoringCreateTokenError::AlreadyExists,
                    "{} since it already exists.", msg);
            }
            Err(ProcessGuardCreateError::SystemCorrupted) => {
                fail!(from self, with MonitoringCreateTokenError::SystemCorrupted,
                    "{} since another process corrupted the process guard while it was created.", msg);
            }
            Err(v) => {
                fail!(from self, with MonitoringCreateTokenError::InternalError,
                    "{} due to an internal failure ({:?}).", msg, v);
            }
        }
    }

    fn monitor(
        self,
    ) -> Result<
        <FileLockMonitoring as super::Monitoring>::Monitor,
        super::MonitoringCreateMonitorError,
    > {
        let msg = "Unable to acquire monitor";
        let process_state_path = self.config.path_for(&self.name);
        match ProcessMonitor::new(&process_state_path) {
            Ok(monitor) => Ok(Monitor {
                monitor,
                name: self.name,
            }),
            Err(ProcessMonitorCreateError::InvalidCleanerPathName) => {
                fail!(from self, with MonitoringCreateMonitorError::ConceptNameNotSupportedOnPlatform,
                    "{} since the concept name \"{}\" results in a concept name that is not supported on this platform.", msg, self.name);
            }
        }
    }

    fn cleaner(
        self,
    ) -> Result<<FileLockMonitoring as Monitoring>::Cleaner, super::MonitoringCreateCleanerError>
    {
        let msg = "Unable to acquire cleaner";
        let process_state_path = self.config.path_for(&self.name);
        match ProcessCleaner::new(&process_state_path) {
            Ok(cleaner) => Ok(Cleaner {
                cleaner,
                name: self.name,
            }),
            Err(ProcessCleanerCreateError::Interrupt) => {
                fail!(from self, with MonitoringCreateCleanerError::Interrupt,
                    "{} since an interrupt signal was received.", msg);
            }
            Err(ProcessCleanerCreateError::ProcessIsStillAlive) => {
                fail!(from self, with MonitoringCreateCleanerError::InstanceStillAlive,
                    "{} since the instance is still alive.", msg);
            }
            Err(ProcessCleanerCreateError::OwnedByAnotherProcess) => {
                fail!(from self, with MonitoringCreateCleanerError::AlreadyOwnedByAnotherInstance,
                    "{} since another instance already acquired the cleaner.", msg);
            }
            Err(ProcessCleanerCreateError::ProcessIsBeingCleanedUpOrCrashedDuringCleanup) => {
                fail!(from self, with MonitoringCreateCleanerError::IsBeingCleanedUpOrAnotherCleanerCrashedDuringCleanup,
                    "{} since the process is currently being cleaned up (or crashed during cleanup).", msg);
            }
            Err(ProcessCleanerCreateError::DoesNotExist) => {
                fail!(from self, with MonitoringCreateCleanerError::DoesNotExist,
                    "{} since it does not exist.", msg);
            }
            Err(e) => {
                fail!(from self, with MonitoringCreateCleanerError::InternalError,
                    "{} due to an internal failure ({:?}).", msg, e);
            }
        }
    }
}

impl crate::monitoring::Monitoring for FileLockMonitoring {
    type Token = Token;
    type Monitor = Monitor;
    type Builder = Builder;
    type Cleaner = Cleaner;
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Configuration {
    suffix: FileName,
    prefix: FileName,
    path_hint: Path,
}

impl Default for Configuration {
    fn default() -> Self {
        Self {
            suffix: FileLockMonitoring::default_suffix(),
            prefix: FileLockMonitoring::default_prefix(),
            path_hint: FileLockMonitoring::default_path_hint(),
        }
    }
}

impl NamedConceptConfiguration for Configuration {
    fn prefix(mut self, value: &FileName) -> Self {
        self.prefix = *value;
        self
    }

    fn get_prefix(&self) -> &FileName {
        &self.prefix
    }

    fn suffix(mut self, value: &FileName) -> Self {
        self.suffix = *value;
        self
    }

    fn get_suffix(&self) -> &FileName {
        &self.suffix
    }

    fn path_hint(mut self, value: &Path) -> Self {
        self.path_hint = *value;
        self
    }

    fn get_path_hint(&self) -> &Path {
        &self.path_hint
    }
}