Safety#

Functions

struct safety *safety_create(void)#

Create a new safety instance.

Allocates and initializes an empty safety context.

Returns:

Pointer to a new safety instance, or NULL on failure.

void safety_destroy(struct safety *self)#

Destroy a safety instance and all of its entries.

This function frees all resources associated with the given safety context. All entries registered with the context will be destroyed automatically by calling their destroy() method.

Parameters:

self[in] Pointer to the safety instance to destroy.

int safety_add(struct safety *self, struct safety_entry *entry)#

Add an entry to the safety context.

Registers a new entry to be included in future safety checks.

Parameters:
  • self[in] Safety context.

  • entry[in] Entry to be added.

Returns:

0 on success, or a negative error code on failure. -EINVAL: null pointer.

-EALREADY: entry already added.

-ENOMEM / -ENOSPC: capacity exceeded.

int safety_add_and_enable(struct safety *self, struct safety_entry *entry)#

Add and enable an entry in one operation.

Registers an entry to the safety context and immediately enables it.

Parameters:
  • self[in] Safety context.

  • entry[in] Entry to add and enable.

Returns:

0 on success, or negative error code on failure. -EINVAL: null pointer. -EALREADY: already added. -ENOMEM / -ENOSPC: out of memory. -EIO: enable() failed after add, entry will be removed again.

int safety_remove(struct safety *self, struct safety_entry *entry)#

Remove an entry from the safety context.

Deregisters the given entry, if present.

Parameters:
  • self[in] Safety context.

  • entry[in] Entry to remove.

Returns:

0 on success, or a negative error code on failure. -EINVAL: null pointer.

-ENOENT: entry not found.

int safety_check(struct safety *self, safety_error_callback_t cb, void *cb_ctx)#

Perform safety checks on all registered entries.

Iterates through all registered entries and invokes their check() method. For each entry that returns a non-OK status, the callback is invoked.

Parameters:
  • self[in] Safety context.

  • cb[in] Callback for reporting failed entries (nullable).

  • cb_ctx[in] User-defined context pointer for callback.

Returns:

0 if all entries returned OK status (i.e. system is safe).

Otherwise, returns the number of failed entries (> 0).

Returns a negative error code on failure: -EINVAL: null pointer.

int safety_iterate(struct safety *self, safety_iterator_t cb, void *cb_ctx)#

Iterate over all registered entries.

Invokes the given callback once per registered entry.

Parameters:
  • self[in] Safety context.

  • cb[in] Callback to invoke for each entry.

  • cb_ctx[in] User-defined context pointer for callback.

Returns:

0 on success, or a negative error code on failure. -EINVAL: null pointer.

Typedefs

typedef void (*safety_error_callback_t)(struct safety_entry *entry, safety_entry_status_t status, void *ctx)#

Error callback type for safety checks.

Called when an entry returns a non-OK status during safety_check().

Param entry:

[in] The entry that failed the check.

Param status:

[in] The status returned by the entry.

Param ctx:

[in] User-defined context pointer.

typedef void (*safety_iterator_t)(struct safety_entry *entry, void *ctx)#

Iterator callback type for iterating all entries.

Called once per registered entry in safety_iterate().

Param entry:

[in] The current entry.

Param ctx:

[in] User-defined context pointer.