diff --git a/src/dump/main.c b/src/dump/main.c index 0fcdd2f..4b790b8 100644 --- a/src/dump/main.c +++ b/src/dump/main.c @@ -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; } diff --git a/src/libs/tools.h b/src/libs/tools.h index 5feb639..162493d 100644 --- a/src/libs/tools.h +++ b/src/libs/tools.h @@ -38,6 +38,12 @@ #include #include +#if defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 32 +# define HAS_SIGABBREV_NP +#else +# include +#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; } diff --git a/src/ustreamer/main.c b/src/ustreamer/main.c index 6e8e7cc..c0406e7 100644 --- a/src/ustreamer/main.c +++ b/src/ustreamer/main.c @@ -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); }