Skip to content

Commit

Permalink
Remove dependency on signal() function
Browse files Browse the repository at this point in the history
Replaces uses of signal() with sigaction() which should be far
more portable.
  • Loading branch information
matt335672 committed Oct 2, 2023
1 parent 89ceaf0 commit b906b60
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
65 changes: 57 additions & 8 deletions common/os_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -2987,9 +2987,15 @@ g_set_alarm(void (*func)(int), unsigned int secs)
#if defined(_WIN32)
return 0;
#else
struct sigaction action;

action.sa_handler = func;
action.sa_flags = SA_RESTART;
sigemptyset (&action.sa_mask);

/* Cancel any previous alarm to prevent a race */
unsigned int rv = alarm(0);
signal(SIGALRM, func);
sigaction(SIGALRM, &action, NULL);
(void)alarm(secs);
return rv;
#endif
Expand All @@ -3002,7 +3008,14 @@ g_signal_child_stop(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGCHLD, func);
struct sigaction action;

action.sa_handler = func;
// Don't need to know when children are stopped or started
action.sa_flags = (SA_RESTART | SA_NOCLDSTOP);
sigemptyset (&action.sa_mask);

sigaction(SIGCHLD, &action, NULL);
#endif
}

Expand All @@ -3013,7 +3026,13 @@ g_signal_segfault(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGSEGV, func);
struct sigaction action;

action.sa_handler = func;
action.sa_flags = 0;
sigemptyset (&action.sa_mask);

sigaction(SIGSEGV, &action, NULL);
#endif
}

Expand All @@ -3024,7 +3043,13 @@ g_signal_hang_up(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGHUP, func);
struct sigaction action;

action.sa_handler = func;
action.sa_flags = SA_RESTART;
sigemptyset (&action.sa_mask);

sigaction(SIGHUP, &action, NULL);
#endif
}

Expand All @@ -3035,7 +3060,13 @@ g_signal_user_interrupt(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGINT, func);
struct sigaction action;

action.sa_handler = func;
action.sa_flags = SA_RESTART;
sigemptyset (&action.sa_mask);

sigaction(SIGINT, &action, NULL);
#endif
}

Expand All @@ -3046,7 +3077,13 @@ g_signal_terminate(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGTERM, func);
struct sigaction action;

action.sa_handler = func;
action.sa_flags = SA_RESTART;
sigemptyset (&action.sa_mask);

sigaction(SIGTERM, &action, NULL);
#endif
}

Expand All @@ -3057,7 +3094,13 @@ g_signal_pipe(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGPIPE, func);
struct sigaction action;

action.sa_handler = func;
action.sa_flags = SA_RESTART;
sigemptyset (&action.sa_mask);

sigaction(SIGPIPE, &action, NULL);
#endif
}

Expand All @@ -3068,7 +3111,13 @@ g_signal_usr1(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGUSR1, func);
struct sigaction action;

action.sa_handler = func;
action.sa_flags = SA_RESTART;
sigemptyset (&action.sa_mask);

sigaction(SIGUSR1, &action, NULL);
#endif
}

Expand Down
8 changes: 8 additions & 0 deletions common/os_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ g_sck_get_peer_ip_address(int sck,
const char *
g_sck_get_peer_description(int sck,
char *desc, unsigned int bytes);
/**
* Sleep for the specified number of milli-seconds
* @param msecs Milli-seconds
*
* If a signal is processed, it is possible that this call will
* sleep for less than the specified number of milli-seconds. This
* is platform-specific
*/
void g_sleep(int msecs);
int g_pipe(int fd[2]);

Expand Down

0 comments on commit b906b60

Please sign in to comment.