55 lines
1.2 KiB
C
55 lines
1.2 KiB
C
|
#include <signal.h> /* signal */
|
||
|
#include <stdio.h> /* fprintf */
|
||
|
#include <stdlib.h> /* exit */
|
||
|
#include <string.h> /* memset */
|
||
|
|
||
|
#include "libircclient.h"
|
||
|
|
||
|
#include "threads.h" /* F_* */
|
||
|
|
||
|
extern irc_session_t *irc_session;
|
||
|
|
||
|
const char signal_fmt[] = "Received %s, exiting.\n";
|
||
|
const char signal_unknown_fmt[] = "(Unknown Signal)";
|
||
|
|
||
|
static void SignalHandler(int sig) {
|
||
|
char buf[512];
|
||
|
const char *s = 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);
|
||
|
snprintf(buf, (sizeof buf) - 1, 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
|
||
|
}
|