quote-bot/signal.c

43 lines
1.1 KiB
C
Raw Normal View History

#include <signal.h> /* signal */
#include <stdio.h> /* fprintf */
#include <string.h> /* memset */
#include "libircclient.h"
#include "threads.h" /* F_* */
2021-03-11 20:48:32 -05:00
#include "strings.h"
extern irc_session_t *irc_session;
static void SignalHandler(int sig) {
char buf[512];
2021-03-11 20:48:32 -05:00
const char *s = string_signal_unknown_fmt;
#define CASE(x) case x: s = #x; break
switch (sig) {
CASE(SIGTERM);
CASE(SIGINT);
#ifndef _WIN32
CASE(SIGQUIT);
CASE(SIGKILL);
CASE(SIGHUP);
#endif
default:
break;
}
#undef CASE
memset(&buf, 0, sizeof buf);
2021-03-11 20:48:32 -05:00
snprintf(buf, (sizeof buf) - 1, string_signal_fmt, s);
fprintf(stderr, "%s", buf);
F_IRC_THREAD = STOPPED;
F_MAIN_THREAD = STOPPED;
F_SPAM_THREADS = STOPPED;
if (NULL != irc_session)
irc_cmd_quit(irc_session, buf);
/* TODO: clean exit */
}
void install_signal_handler(void) {
signal(SIGTERM, SignalHandler);
signal(SIGINT, SignalHandler);
#ifndef _WIN32
signal(SIGQUIT, SignalHandler);
signal(SIGKILL, SignalHandler);
signal(SIGHUP, SignalHandler);
#endif
}