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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
* atheme-services: A collection of minimalist IRC services
* signal.c: Signal-handling routines.
*
* Copyright (c) 2005-2007 Atheme Project (http://www.atheme.org)
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "atheme.h"
#include "uplink.h"
#include "conf.h"
#include "internal.h"
#include <signal.h>
#ifndef MOWGLI_OS_WIN
# include <sys/wait.h>
#endif
static void childproc_check(void);
static volatile sig_atomic_t got_sighup, got_sigint, got_sigterm, got_sigchld, got_sigusr2;
/*
* A version of signal(2) that works more reliably across different
* platforms.
*
* It restarts interrupted system calls, does not reset the handler,
* and blocks the same signal from within the handler.
*/
static void
signal_empty_handler(int signum)
{
/* do nothing */
}
static void
signal_hup_handler(int signum)
{
got_sighup = 1;
}
static void
signal_int_handler(int signum)
{
got_sigint = 1;
}
static void
signal_term_handler(int signum)
{
got_sigterm = 1;
}
static void
signal_chld_handler(int signum)
{
got_sigchld = 1;
}
static void
signal_usr2_handler(int signum)
{
got_sigusr2 = 1;
}
/* XXX */
static void
signal_usr1_handler(int signum)
{
int n;
if (me.connected && curr_uplink != NULL &&
curr_uplink->conn != NULL)
{
if (chansvs.nick != NULL)
{
n = send(curr_uplink->conn->fd, ":", 1, 0);
n = send(curr_uplink->conn->fd, chansvs.nick, strlen(chansvs.nick), 0);
n = send(curr_uplink->conn->fd, " QUIT :Out of memory!\r\n", 23, 0);
}
n = send(curr_uplink->conn->fd, "ERROR :Panic! Out of memory.\r\n", 30, 0);
}
if (runflags & (RF_LIVE | RF_STARTING))
n = write(2, "Out of memory!\n", 15);
abort();
}
void init_signal_handlers(void)
{
#ifndef MOWGLI_OS_WIN
#ifdef SIGHUP
mowgli_signal_install_handler(SIGHUP, signal_hup_handler);
#endif
#ifdef SIGINT
mowgli_signal_install_handler(SIGINT, signal_int_handler);
#endif
#ifdef SIGTERM
mowgli_signal_install_handler(SIGTERM, signal_term_handler);
#endif
#ifdef SIGPIPE
mowgli_signal_install_handler(SIGPIPE, signal_empty_handler);
#endif
#ifdef SIGCHLD
mowgli_signal_install_handler(SIGCHLD, signal_chld_handler);
#endif
#ifdef SIGUSR1
mowgli_signal_install_handler(SIGUSR1, signal_usr1_handler);
#endif
#ifdef SIGUSR2
mowgli_signal_install_handler(SIGUSR2, signal_usr2_handler);
#endif
#endif
}
void check_signals(void)
{
/* rehash */
if (got_sighup)
{
got_sighup = 0;
slog(LG_INFO, "sighandler(): got SIGHUP, rehashing \2%s\2", config_file);
wallops(_("Got SIGHUP; reloading \2%s\2."), config_file);
if (db_save && !readonly)
{
slog(LG_INFO, "UPDATE: \2%s\2", "system console");
wallops(_("Updating database by request of \2%s\2."), "system console");
db_save(NULL);
}
slog(LG_INFO, "REHASH: \2%s\2", "system console");
wallops(_("Rehashing \2%s\2 by request of \2%s\2."), config_file, "system console");
/* reload the config, opening other logs besides the core log if needed. */
if (!conf_rehash())
wallops(_("REHASH of \2%s\2 failed. Please correct any errors in the file and try again."), config_file);
return;
}
/* usually caused by ^C */
if (got_sigint && (runflags & RF_LIVE))
{
got_sigint = 0;
wallops(_("Exiting on signal %d."), SIGINT);
if (chansvs.me != NULL && chansvs.me->me != NULL)
quit_sts(chansvs.me->me, "caught interrupt");
me.connected = false;
slog(LG_INFO, "sighandler(): caught interrupt; exiting...");
runflags |= RF_SHUTDOWN;
}
else if (got_sigint && !(runflags & RF_LIVE))
{
got_sigint = 0;
wallops(_("Got SIGINT; restarting."));
slog(LG_INFO, "RESTART: \2%s\2", "system console");
wallops(_("Restarting by request of \2%s\2."), "system console");
runflags |= RF_RESTART;
}
if (got_sigterm)
{
got_sigterm = 0;
wallops(_("Exiting on signal %d."), SIGTERM);
slog(LG_INFO, "sighandler(): got SIGTERM; exiting...");
runflags |= RF_SHUTDOWN;
}
if (got_sigusr2)
{
got_sigusr2 = 0;
wallops(_("Got SIGUSR2; restarting."));
slog(LG_INFO, "RESTART: \2%s\2", "system console");
wallops(_("Restarting by request of \2%s\2."), "system console");
runflags |= RF_RESTART;
}
if (got_sigchld)
{
got_sigchld = 0;
childproc_check();
}
}
mowgli_list_t childproc_list;
struct childproc
{
mowgli_node_t node;
pid_t pid;
char *desc;
void (*cb)(pid_t pid, int status, void *data);
void *data;
};
/* Registers a child process.
* Will call cb(pid, status, data) after the process terminates
* where status is the status from waitpid(2).
*/
void childproc_add(pid_t pid, const char *desc, void (*cb)(pid_t pid, int status, void *data), void *data)
{
struct childproc *p;
p = smalloc(sizeof(*p));
p->pid = pid;
p->desc = sstrdup(desc);
p->cb = cb;
p->data = data;
mowgli_node_add(p, &p->node, &childproc_list);
}
static void childproc_free(struct childproc *p)
{
free(p->desc);
free(p);
}
/* Forgets about all child processes with the given callback.
* Useful if the callback is in a module which is being unloaded.
*/
void childproc_delete_all(void (*cb)(pid_t pid, int status, void *data))
{
mowgli_node_t *n, *tn;
struct childproc *p;
MOWGLI_ITER_FOREACH_SAFE(n, tn, childproc_list.head)
{
p = n->data;
if (p->cb == cb)
{
mowgli_node_delete(&p->node, &childproc_list);
childproc_free(p);
}
}
}
static void childproc_check(void)
{
#ifndef MOWGLI_OS_WIN
pid_t pid;
int status;
mowgli_node_t *n;
struct childproc *p;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
{
MOWGLI_ITER_FOREACH(n, childproc_list.head)
{
p = n->data;
if (p->pid == pid)
{
mowgli_node_delete(&p->node, &childproc_list);
if (p->cb)
p->cb(pid, status, p->data);
childproc_free(p);
break;
}
}
}
#endif
}
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
* vim:ts=8
* vim:sw=8
* vim:noexpandtab
*/
|