quote-bot/signal.c
2021-03-31 08:47:18 -04:00

59 lines
1.3 KiB
C

#include <signal.h> /* signal */
#include <stdio.h> /* fprintf */
#include <string.h> /* memset */
#include <libircclient.h>
#include "threads.h" /* F_* */
#include "i18n.h"
#ifdef BUFSIZ
#undef BUFSIZ
#endif
#define BUFSIZ 127
extern irc_session_t *irc_session;
static const char string_signal_recv_fmt[] = _("Received %s, exiting.\n");
static const char string_signal_unknown_fmt[] = _("(Unknown Signal)");
static void SignalHandler(int sig) {
char buf[BUFSIZ+1];
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, BUFSIZ+1);
snprintf(buf, BUFSIZ, string_signal_recv_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
}