forked from kofany/psotnic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals.cpp
145 lines (125 loc) · 4.1 KB
/
signals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/***************************************************************************
* Copyright (C) 2003-2005 by Grzegorz Rusin *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "prots.h"
#include "global-var.h"
void sigChild()
{
int status;
while(waitpid(-1, &status, WNOHANG) > 0) ;
}
void sigTerm()
{
signal(SIGTERM, SIG_IGN);
if(net.irc.fd > 0) net.irc.send("QUIT :", (const char *) config.quitreason, NULL);
net.send(HAS_N, "[!] Got termination signal", NULL);
stopPsotnic = 1;
}
void sigInt()
{
signal(SIGINT, SIG_IGN);
if(net.irc.fd > 0) net.irc.send("QUIT :", (const char *) config.quitreason, NULL);
net.send(HAS_N, "[!] Terminated by user", NULL);
stopPsotnic = 1;
}
void sigHup()
{
userlist.save(config.userlist_file);
HOOK(receivedSigHup, receivedSigHup());
}
void sigSegv()
{
signal(SIGSEGV, SIG_IGN);
net.send(HAS_N, "[!] Got segmentation fault signal; please report this crash", NULL);
net.irc.send("QUIT :Got segmentation fault signal; please report this crash", NULL);
#ifdef HAVE_DEBUG
char gdb[MAX_LEN], cmdlist[256], btfile[256];
int bt = 0;
unsigned int core = 0;
snprintf(cmdlist, 256, "/tmp/.psotnic-%d", (int) getpid());
FILE *f = fopen(cmdlist, "w");
if(f)
{
fprintf(f, "attach %d\n", getpid());
fprintf(f, "bt 100\n");
fprintf(f, "detach\n");
fprintf(f, "q\n");
fclose(f);
snprintf(btfile, 256, ".gdb-backtrace-%d", (int) getpid());
snprintf(gdb, MAX_LEN, "gdb -q -x %s > %s 2>&1", cmdlist, btfile);
if(!system(gdb))
bt = 1;
unlink(cmdlist);
}
//enabling core dumps
struct rlimit limit;
if(!getrlimit(RLIMIT_CORE, &limit))
{
limit.rlim_cur = limit.rlim_max;
if(!setrlimit(RLIMIT_CORE, &limit))
core = limit.rlim_cur;
}
#endif
dumpIrcBacktrace();
}
void safeExit()
{
net.send(HAS_N, "[*] Abnormal program termination", NULL);
stopPsotnic = 1;
}
void sigCpuTime()
{
net.send(HAS_N, "[*] Cpu time limit reached, terminating", NULL);
if(net.irc.fd > 0) net.irc.send("QUIT :Cpu time limit reached, terminating", NULL);
stopPsotnic = 1;
}
void sigUpdateFailed()
{
DEBUG(printf("[D] got: SIGUSR2\n"));
psotget.end();
}
void sigUpdated()
{
DEBUG(printf("[D] got: SIGUSR1\n"));
psotget.end();
#ifdef RESTART_AFTER_UPDATE
ME.restart();
#endif
}
#define register_signal(x, y) \
{ \
memset(&sv, 0, sizeof(sv)); \
sv.sa_handler = y; \
sigaction(x, &sv, NULL); \
}
void signalHandling()
{
struct sigaction sv;
register_signal(SIGPIPE, SIG_IGN);
register_signal(SIGCHLD, (sighandler_t) sigChild);
register_signal(SIGTERM, (sighandler_t) sigTerm);
register_signal(SIGINT, (sighandler_t) sigInt);
register_signal(SIGHUP, (sighandler_t) sigHup);
register_signal(SIGUSR1, (sighandler_t) sigUpdated);
register_signal(SIGUSR2, (sighandler_t) sigUpdateFailed);
#ifndef HAVE_DEBUG
register_signal(SIGSEGV, (sighandler_t) sigSegv);
#endif
register_signal(SIGXCPU, (sighandler_t) sigCpuTime);
}