2021-03-11 17:23:43 -05:00
|
|
|
#include <signal.h> /* signal */
|
|
|
|
#include <stdio.h> /* fprintf */
|
|
|
|
#include <string.h> /* memset */
|
2021-03-31 08:47:18 -04:00
|
|
|
|
|
|
|
#include <libircclient.h>
|
|
|
|
|
2021-03-11 17:23:43 -05:00
|
|
|
#include "threads.h" /* F_* */
|
2021-03-31 08:47:18 -04:00
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
#ifdef BUFSIZ
|
|
|
|
#undef BUFSIZ
|
|
|
|
#endif
|
|
|
|
#define BUFSIZ 127
|
|
|
|
|
2021-03-11 17:23:43 -05:00
|
|
|
extern irc_session_t *irc_session;
|
2021-03-31 08:47:18 -04:00
|
|
|
|
|
|
|
static const char string_signal_recv_fmt[] = _("Received %s, exiting.\n");
|
|
|
|
static const char string_signal_unknown_fmt[] = _("(Unknown Signal)");
|
|
|
|
|
2021-03-11 17:23:43 -05:00
|
|
|
static void SignalHandler(int sig) {
|
2021-03-31 08:47:18 -04:00
|
|
|
char buf[BUFSIZ+1];
|
2021-03-11 20:48:32 -05:00
|
|
|
const char *s = string_signal_unknown_fmt;
|
2021-03-11 17:23:43 -05:00
|
|
|
#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
|
2021-03-31 08:47:18 -04:00
|
|
|
|
|
|
|
memset(&buf, 0, BUFSIZ+1);
|
|
|
|
snprintf(buf, BUFSIZ, string_signal_recv_fmt, s);
|
2021-03-11 17:23:43 -05:00
|
|
|
fprintf(stderr, "%s", buf);
|
2021-03-31 08:47:18 -04:00
|
|
|
|
2021-03-11 17:23:43 -05:00
|
|
|
F_IRC_THREAD = STOPPED;
|
|
|
|
F_MAIN_THREAD = STOPPED;
|
|
|
|
F_SPAM_THREADS = STOPPED;
|
2021-03-31 08:47:18 -04:00
|
|
|
|
2021-03-11 17:23:43 -05:00
|
|
|
if (NULL != irc_session)
|
|
|
|
irc_cmd_quit(irc_session, buf);
|
|
|
|
/* TODO: clean exit */
|
|
|
|
}
|
2021-03-31 08:47:18 -04:00
|
|
|
|
2021-03-11 17:23:43 -05:00
|
|
|
void install_signal_handler(void) {
|
|
|
|
signal(SIGTERM, SignalHandler);
|
|
|
|
signal(SIGINT, SignalHandler);
|
|
|
|
#ifndef _WIN32
|
|
|
|
signal(SIGQUIT, SignalHandler);
|
|
|
|
signal(SIGKILL, SignalHandler);
|
|
|
|
signal(SIGHUP, SignalHandler);
|
|
|
|
#endif
|
|
|
|
}
|