Remove dependency on signal() function

Replaces uses of signal() with sigaction() which should be far
more portable.
This commit is contained in:
matt335672
2023-10-02 16:29:06 +01:00
parent bce303c3a8
commit d11617adbe
6 changed files with 191 additions and 10 deletions
+126 -9
View File
@@ -2987,10 +2987,28 @@ g_set_alarm(void (*func)(int), unsigned int secs)
#if defined(_WIN32)
return 0;
#else
struct sigaction action;
/* Cancel any previous alarm to prevent a race */
unsigned int rv = alarm(0);
signal(SIGALRM, func);
(void)alarm(secs);
if (func == NULL)
{
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
}
else
{
action.sa_handler = func;
action.sa_flags = SA_RESTART;
}
sigemptyset (&action.sa_mask);
sigaction(SIGALRM, &action, NULL);
if (func != NULL && secs > 0)
{
(void)alarm(secs);
}
return rv;
#endif
}
@@ -3002,7 +3020,22 @@ g_signal_child_stop(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGCHLD, func);
struct sigaction action;
if (func == NULL)
{
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
}
else
{
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
}
@@ -3013,7 +3046,21 @@ g_signal_segfault(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGSEGV, func);
struct sigaction action;
if (func == NULL)
{
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
}
else
{
action.sa_handler = func;
action.sa_flags = SA_RESETHAND; // This is a one-shot
}
sigemptyset (&action.sa_mask);
sigaction(SIGSEGV, &action, NULL);
#endif
}
@@ -3024,7 +3071,21 @@ g_signal_hang_up(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGHUP, func);
struct sigaction action;
if (func == NULL)
{
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
}
else
{
action.sa_handler = func;
action.sa_flags = SA_RESTART;
}
sigemptyset (&action.sa_mask);
sigaction(SIGHUP, &action, NULL);
#endif
}
@@ -3035,7 +3096,21 @@ g_signal_user_interrupt(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGINT, func);
struct sigaction action;
if (func == NULL)
{
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
}
else
{
action.sa_handler = func;
action.sa_flags = SA_RESTART;
}
sigemptyset (&action.sa_mask);
sigaction(SIGINT, &action, NULL);
#endif
}
@@ -3046,7 +3121,21 @@ g_signal_terminate(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGTERM, func);
struct sigaction action;
if (func == NULL)
{
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
}
else
{
action.sa_handler = func;
action.sa_flags = SA_RESTART;
}
sigemptyset (&action.sa_mask);
sigaction(SIGTERM, &action, NULL);
#endif
}
@@ -3057,7 +3146,21 @@ g_signal_pipe(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGPIPE, func);
struct sigaction action;
if (func == NULL)
{
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
}
else
{
action.sa_handler = func;
action.sa_flags = SA_RESTART;
}
sigemptyset (&action.sa_mask);
sigaction(SIGPIPE, &action, NULL);
#endif
}
@@ -3068,7 +3171,21 @@ g_signal_usr1(void (*func)(int))
{
#if defined(_WIN32)
#else
signal(SIGUSR1, func);
struct sigaction action;
if (func == NULL)
{
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
}
else
{
action.sa_handler = func;
action.sa_flags = SA_RESTART;
}
sigemptyset (&action.sa_mask);
sigaction(SIGUSR1, &action, NULL);
#endif
}
+51
View File
@@ -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]);
@@ -275,13 +283,56 @@ int g_execvp(const char *p1, char *args[]);
*/
int g_execvp_list(const char *file, struct list *argv);
int g_execlp3(const char *a1, const char *a2, const char *a3);
/**
* Set an alarm using SIGALRM
* @param func Signal handler, or NULL to cancel an alarm
* @param secs Number of seconds until an alarm is raised
* @return Number of seconds remaining before a previously requested
* alarm is raised
*/
unsigned int g_set_alarm(void (*func)(int), unsigned int secs);
/**
* Set a handler up for SIGCHLD
* @param func signal handler, or NULL to restore the default handler
* The handler remains in place until explicitly replaced.
*/
void g_signal_child_stop(void (*func)(int));
/**
* Set a handler up for SIGSEGV
* @param func signal handler, or NULL to restore the default handler
* The handler can only be called once, at which point the
* default handler is restored. This is to avoid infinite loops
*/
void g_signal_segfault(void (*func)(int));
/**
* Set a handler up for SIGHUP
* @param func signal handler, or NULL to restore the default handler
* The handler remains in place until explicitly replaced.
*/
void g_signal_hang_up(void (*func)(int));
/**
* Set a handler up for SIGINT
* @param func signal handler, or NULL to restore the default handler
* The handler remains in place until explicitly replaced.
*/
void g_signal_user_interrupt(void (*func)(int));
/**
* Set a handler up for SIGTERM
* @param func signal handler, or NULL to restore the default handler
* The handler remains in place until explicitly replaced.
*/
void g_signal_terminate(void (*func)(int));
/**
* Set a handler up for SIGPIPE
* @param func signal handler, or NULL to restore the default handler
* The handler remains in place until explicitly replaced.
*/
void g_signal_pipe(void (*func)(int));
/**
* Set a handler up for SIGUSR1
* @param func signal handler, or NULL to restore the default handler
* The handler remains in place until explicitly replaced.
*/
void g_signal_usr1(void (*func)(int));
int g_fork(void);
int g_setgid(int pid);