libamxp  1.4.0
Patterns C Implementation
System Signal Handling

Provides functionality to translate Linux System Signals to Ambiorix signal/slot mechanism. More...

Macros

#define AMXP_SYSSIG_MAX   32
 Defines highest possible system signal. More...
 

Functions

int amxp_syssig_enable (const int sigid, const bool enable)
 Enables or disables monitoring of a system signal. More...
 
bool amxp_syssig_is_enabled (const int sigid)
 Checks if a system signal is being monitored. More...
 
int amxp_syssig_get_fd (void)
 Returns a file descriptor that can be used in an eventloop. More...
 
int amxp_syssig_read (void)
 Reads from the file descriptor. More...
 

Detailed Description

Provides functionality to translate Linux System Signals to Ambiorix signal/slot mechanism.

Handling system signals using the system provided methods have some limitations. In a system signal handler the set of possible system calls that can be done is very limited.

The Ambiorix System Signal API translates a system signal to an Ambiorix signal. To such a signal you can connect many slots (callback functions).

For more information about Ambiorix Signal/Slots

See also
Signal/Slots.

Macro Definition Documentation

◆ AMXP_SYSSIG_MAX

#define AMXP_SYSSIG_MAX   32

Defines highest possible system signal.

Definition at line 95 of file amxp_syssig.h.

Function Documentation

◆ amxp_syssig_enable()

int amxp_syssig_enable ( const int  sigid,
const bool  enable 
)

Enables or disables monitoring of a system signal.

When a system signal gets enabled, the signal name is added to the global Ambiorix signal manager. The name of the signal is retrieved using strsignal function.

Note
When enabling a system signal it is possible that other parts of the application that rely on the signal stop working, as the signal is handeld by Ambiorix. So use with caution.

After enabling a signal it is possible to connect slots (callback) functions to it using amxp_slot_connect.

The name of the signal can be retrieved using "strsignal(sigid)".

Parameters
sigidid of the system signal example SIGUSR1
enabletrue to enable monitoring, false to stop monitoring
Returns
0 when signal is successfully enabled and is being monitored

Definition at line 70 of file amxp_syssig.c.

70  {
71  int retval = -1;
72  amxp_signal_t* signal = NULL;
73  sigset_t change_mask;
74  when_true(sigid > AMXP_SYSSIG_MAX, exit);
75  when_true(sigid == SIGKILL || sigid == SIGSTOP, exit);
76 
77  sigemptyset(&change_mask);
78  sigaddset(&change_mask, sigid);
79 
80  amxp_signal_new(NULL, &signal, strsignal(sigid));
81 
82  if(enable) {
83  when_failed(sigprocmask(SIG_BLOCK, &change_mask, NULL), exit);
84  sigaddset(&mask, sigid);
85  } else {
86  when_failed(sigprocmask(SIG_UNBLOCK, &change_mask, NULL), exit);
87  sigdelset(&mask, sigid);
88  }
89 
90  sig_fd = signalfd(sig_fd, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
91  retval = 0;
92 
93 exit:
94  return retval;
95 }
static int sig_fd
Definition: amxp_syssig.c:67
static sigset_t mask
Definition: amxp_syssig.c:68
int amxp_signal_new(amxp_signal_mngr_t *sig_mngr, amxp_signal_t **signal, const char *name)
Constructor function, creates a new signal.
Definition: amxp_signal.c:620
#define AMXP_SYSSIG_MAX
Defines highest possible system signal.
Definition: amxp_syssig.h:95
Structure containing the signal information.
Definition: amxp_signal.h:119

◆ amxp_syssig_get_fd()

int amxp_syssig_get_fd ( void  )

Returns a file descriptor that can be used in an eventloop.

System signal monitoring is done using "signalfd". The filedescriptor must be added to your eventloop to make it work. When data is available on this filedescriptor the function amxp_syssig_read must be called to make sure the correct dispatching is done (calling of the slots)

Returns
A file descriptor or -1 if non is available

Definition at line 110 of file amxp_syssig.c.

110  {
111  return sig_fd;
112 }

◆ amxp_syssig_is_enabled()

bool amxp_syssig_is_enabled ( const int  sigid)

Checks if a system signal is being monitored.

If a system signal is enabled for monitoring this function will return true.

Parameters
sigidid of the system signal example SIGUSR1
Returns
true when the signal was enabled for being monitored

Definition at line 97 of file amxp_syssig.c.

97  {
98  bool enabled = false;
99  int inset = -1;
100  when_true(sigid > AMXP_SYSSIG_MAX, exit);
101 
102  inset = sigismember(&mask, sigid);
103  when_true(inset < 0, exit);
104 
105  enabled = (inset == 1);
106 exit:
107  return enabled;
108 }

◆ amxp_syssig_read()

int amxp_syssig_read ( void  )

Reads from the file descriptor.

Use this function in an eventloop. When a system signal is received data will become available for read on the signal fd. Use amxp_syssig_get_fd to get the file descriptor and add it to your eventloop.

This function will make sure that all data is read from the file descriptor and that the correct slots are called.

Returns
0 when reading and dispatching is done successful, any other value indicates an error condition.

Definition at line 114 of file amxp_syssig.c.

114  {
115  int retval = -1;
116  struct signalfd_siginfo si;
117  ssize_t res = 0;
118  amxc_var_t sig_var;
119  int len = sizeof(amxp_siginfo_t);
120  amxp_signal_t* signal = NULL;
121 
122  amxc_var_init(&sig_var);
123 
124  res = read(sig_fd, &si, len);
125  when_true(res != len, exit);
126 
127  amxc_var_set(amxp_siginfo_t, &sig_var, &si);
128 
129  signal = amxp_sigmngr_find_signal(NULL, strsignal(si.ssi_signo));
130  when_null(signal, exit);
131  amxp_signal_trigger(signal, &sig_var);
132 
133  retval = 0;
134 
135 exit:
136  amxc_var_clean(&sig_var);
137  return retval;
138 }
amxp_signal_t * amxp_sigmngr_find_signal(const amxp_signal_mngr_t *const sig_mngr, const char *name)
Get the pointer to the signal.
Definition: amxp_signal.c:475
void amxp_signal_trigger(amxp_signal_t *const signal, const amxc_var_t *const data)
Triggers a signal.
Definition: amxp_signal.c:691
struct signalfd_siginfo amxp_siginfo_t