OODuck  0.5
C Oriented Object framework with duck-typing support
e4c_signal_mapping Struct Reference

Public Attributes

int signal_number
 
const e4c_exception_type *const exception_type
 

Detailed Description

Represents a map between a signal and an exception

A signal is an asynchronous notification sent by the operating system to a process in order to notify it of an event that occurred. Most of the signals will, by default, crash the program as soon as they are raised. exceptions4c can convert signals to exceptions, so they can be easily handled.

For example, a suspicious or dangerous part of the program could be wrapped up with try blocks and then catch segmentation faults or divisions by zero. Then the program would clean up and continue normally:

int * pointer = NULL;
try{
int oops = *pointer;
}catch(BadPointerException){
printf("No problem ;-)");
}finally{
// clean up...
}
}

In order to perform the conversion, exceptions4c maps signals to exceptions.

The simplest way to get this working is by calling the function e4c_context_begin. This function will set up the default mappings for the available signals in the platform, when passed handle_signals=true.

If you need to be more specific about which signals get converted to exceptions, you can define an array of signal mappings and then pass it to the function e4c_context_set_signal_mappings.

An array of signal mappings is defined through three macros:

While E4C_SIGNAL_MAPPING tells the system to convert a specific signal to a given exception, E4C_IGNORE_SIGNAL allows you to disregard the signal and continue (even if unmeaningful).

Every array of signal mappings needs to be terminated with the E4C_NULL_SIGNAL_MAPPING element, so the system finds out how many mappings are there in a given array.

const e4c_signal_mapping my_signal_mappings[] = {
E4C_SIGNAL_MAPPING(SIGABRT, Exception1),
E4C_SIGNAL_MAPPING(SIGINT, Exception2),
...
}

Once the array is properly defined, it can be passed to the function e4c_context_set_signal_mappings. This way, only the specified signals will be handled as exceptions, and they will be converted to the specified exceptions.

e4c_using_context(E4C_FALSE){
e4c_context_set_signal_mappings(my_signal_mappings);
...
}

These are some of the signals you can handle:

  • SIGFPE when dividing by zero.
  • SIGSEGV when dereferencing an invalid pointer.
  • SIGINT when a user interrupts the process.
  • SIGTERM when a process is requested to be terminated.
See also
e4c_context_begin
e4c_context_set_signal_mappings
e4c_context_get_signal_mappings
E4C_SIGNAL_MAPPING
E4C_IGNORE_SIGNAL
e4c_default_signal_mappings

Member Data Documentation

const e4c_exception_type* const e4c_signal_mapping::exception_type

The exception representing the signal

int e4c_signal_mapping::signal_number

The signal to be converted