fixed #162: optional sigabbrev_np()

This commit is contained in:
Maxim Devaev 2022-07-30 02:59:21 +03:00
parent b5f814d71e
commit 3d7fb8c8dd
3 changed files with 30 additions and 5 deletions

View File

@ -195,7 +195,9 @@ int main(int argc, char *argv[]) {
static void _signal_handler(int signum) {
US_LOG_INFO_NOLOCK("===== Stopping by SIG%s =====", us_signum_to_string(signum));
char *const name = us_signum_to_string(signum);
US_LOG_INFO_NOLOCK("===== Stopping by %s =====", name);
free(name);
_g_stop = true;
}

View File

@ -38,6 +38,12 @@
#include <sys/types.h>
#include <sys/file.h>
#if defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 32
# define HAS_SIGABBREV_NP
#else
# include <signal.h>
#endif
#ifdef NDEBUG
# error WTF dude? Asserts are good things!
@ -194,7 +200,22 @@ INLINE char *us_errno_to_string(int error, char *buf, size_t size) {
return buf;
}
INLINE const char *us_signum_to_string(int signum) {
const char *const str = sigabbrev_np(signum);
return (str == NULL ? "???" : str);
INLINE char *us_signum_to_string(int signum) {
# ifdef HAS_SIGABBREV_NP
const char *const name = sigabbrev_np(signum);
# else
const char *const name = (
signum == SIGTERM ? "TERM" :
signum == SIGINT ? "INT" :
signum == SIGPIPE ? "PIPE" :
NULL
);
# endif
char *buf;
if (name != NULL) {
US_ASPRINTF(buf, "SIG%s", name);
} else {
US_ASPRINTF(buf, "SIG[%d]", signum);
}
return buf;
}

View File

@ -67,7 +67,9 @@ static void *_server_loop_thread(UNUSED void *arg) {
}
static void _signal_handler(int signum) {
US_LOG_INFO_NOLOCK("===== Stopping by SIG%s =====", us_signum_to_string(signum));
char *const name = us_signum_to_string(signum);
US_LOG_INFO_NOLOCK("===== Stopping by %s =====", name);
free(name);
us_stream_loop_break(_g_stream);
us_server_loop_break(_g_server);
}