| 1 |
/* |
| 2 |
* Copyright (C) 2018 Atheme Development Group (https://atheme.github.io/) |
| 3 |
* Rights to this code are documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* Log crash reason with backtrace on fatal abnormal signals. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "atheme-compat.h" |
| 9 |
|
| 10 |
#if (CURRENT_ABI_REVISION < 730000) |
| 11 |
# include "serno.h" |
| 12 |
#endif |
| 13 |
|
| 14 |
#if (defined(__linux__) || defined(__Linux__)) && defined(__GLIBC__) |
| 15 |
# if (__GLIBC__ == 2) && defined(__GLIBC_MINOR__) && (__GLIBC_MINOR__ >= 1) |
| 16 |
# define HAVE_BACKTRACE_SUPPORT 1 |
| 17 |
# else /* (__GLIBC__ == 2) && __GLIBC_MINOR__ && (__GLIBC_MINOR >= 1) */ |
| 18 |
# if (__GLIBC__ > 2) |
| 19 |
# define HAVE_BACKTRACE_SUPPORT 1 |
| 20 |
# endif /* (__GLIBC__ > 2) */ |
| 21 |
# endif /* !((__GLIBC__ == 2) && __GLIBC_MINOR__ && (__GLIBC_MINOR__ >= 1)) */ |
| 22 |
#endif /* (__linux__ || __Linux__) && __GLIBC__ */ |
| 23 |
|
| 24 |
#ifdef HAVE_BACKTRACE_SUPPORT |
| 25 |
|
| 26 |
#include <execinfo.h> |
| 27 |
#include <signal.h> |
| 28 |
#include <sys/resource.h> |
| 29 |
|
| 30 |
#define MIN_STACK_FRAMES 3 |
| 31 |
#define MAX_STACK_FRAMES 64 |
| 32 |
|
| 33 |
#ifdef DATADIR |
| 34 |
#define CRASHLOG_FILE DATADIR "/crash.log" |
| 35 |
#endif /* DATADIR */ |
| 36 |
|
| 37 |
#ifdef CRASHLOG_FILE |
| 38 |
static FILE *crashfp = NULL; |
| 39 |
#else /* CRASHLOG_FILE */ |
| 40 |
static FILE *const crashfp = stderr; |
| 41 |
#endif /* !CRASHLOG_FILE */ |
| 42 |
|
| 43 |
static struct sigaction oldbusaction; |
| 44 |
static struct sigaction oldfpeaction; |
| 45 |
static struct sigaction oldillaction; |
| 46 |
static struct sigaction oldsegvaction; |
| 47 |
|
| 48 |
static bool |
| 49 |
contrib_backtrace_signal_map(const int signum, const siginfo_t *const restrict info, |
| 50 |
const char **const restrict fault_type, |
| 51 |
const char **const restrict fault_code) |
| 52 |
{ |
| 53 |
if (signum == SIGBUS) |
| 54 |
{ |
| 55 |
*fault_type = "SIGBUS"; |
| 56 |
|
| 57 |
switch (info->si_code) |
| 58 |
{ |
| 59 |
#ifdef BUS_ADRALN |
| 60 |
case BUS_ADRALN: |
| 61 |
*fault_code = "BUS_ADRALN"; |
| 62 |
break; |
| 63 |
#endif /* BUS_ADRALN */ |
| 64 |
#ifdef BUS_ADDRERR |
| 65 |
case BUS_ADRERR: |
| 66 |
*fault_code = "BUS_ADRERR"; |
| 67 |
break; |
| 68 |
#endif /* BUS_ADRERR */ |
| 69 |
#ifdef BUS_OBJERR |
| 70 |
case BUS_OBJERR: |
| 71 |
*fault_code = "BUS_OBJERR"; |
| 72 |
break; |
| 73 |
#endif /* BUS_OBJERR */ |
| 74 |
#ifdef BUS_MCEERR_AR |
| 75 |
case BUS_MCEERR_AR: |
| 76 |
*fault_code = "BUS_MCEERR_AR"; |
| 77 |
break; |
| 78 |
#endif /* BUS_MCEERR_AR */ |
| 79 |
#ifdef BUS_MCEERR_AO |
| 80 |
case BUS_MCEERR_AO: |
| 81 |
*fault_code = "BUS_MCEERR_AO"; |
| 82 |
break; |
| 83 |
#endif /* BUS_MCEERR_AO */ |
| 84 |
#ifdef SI_KERNEL |
| 85 |
case SI_KERNEL: |
| 86 |
*fault_code = "<unknown>"; |
| 87 |
break; |
| 88 |
#endif /* SI_KERNEL */ |
| 89 |
default: |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
if (signum == SIGFPE) |
| 97 |
{ |
| 98 |
*fault_type = "SIGFPE"; |
| 99 |
|
| 100 |
switch (info->si_code) |
| 101 |
{ |
| 102 |
#ifdef FPE_INTDIV |
| 103 |
case FPE_INTDIV: |
| 104 |
*fault_code = "FPE_INTDIV"; |
| 105 |
break; |
| 106 |
#endif /* FPE_INTDIV */ |
| 107 |
#ifdef FPE_INTOVF |
| 108 |
case FPE_INTOVF: |
| 109 |
*fault_code = "FPE_INTOVF"; |
| 110 |
break; |
| 111 |
#endif /* FPE_INTOVF */ |
| 112 |
#ifdef FPE_FLTDIV |
| 113 |
case FPE_FLTDIV: |
| 114 |
*fault_code = "FPE_FLTDIV"; |
| 115 |
break; |
| 116 |
#endif /* FPE_FLTDIV */ |
| 117 |
#ifdef FPE_FLTOVF |
| 118 |
case FPE_FLTOVF: |
| 119 |
*fault_code = "FPE_FLTOVF"; |
| 120 |
break; |
| 121 |
#endif /* FPE_FLTOVF */ |
| 122 |
#ifdef FPE_FLTUND |
| 123 |
case FPE_FLTUND: |
| 124 |
*fault_code = "FPE_FLTUND"; |
| 125 |
break; |
| 126 |
#endif /* FPE_FLTUND */ |
| 127 |
#ifdef FPE_FLTRES |
| 128 |
case FPE_FLTRES: |
| 129 |
*fault_code = "FPE_FLTRES"; |
| 130 |
break; |
| 131 |
#endif /* FPE_FLTRES */ |
| 132 |
#ifdef FPE_FLTINV |
| 133 |
case FPE_FLTINV: |
| 134 |
*fault_code = "FPE_FLTINV"; |
| 135 |
break; |
| 136 |
#endif /* FPE_FLTINV */ |
| 137 |
#ifdef FPE_FLTSUB |
| 138 |
case FPE_FLTSUB: |
| 139 |
*fault_code = "FPE_FLTSUB"; |
| 140 |
break; |
| 141 |
#endif /* FPE_FLTSUB */ |
| 142 |
#ifdef SI_KERNEL |
| 143 |
case SI_KERNEL: |
| 144 |
*fault_code = "<unknown>"; |
| 145 |
break; |
| 146 |
#endif /* SI_KERNEL */ |
| 147 |
default: |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
if (signum == SIGILL) |
| 155 |
{ |
| 156 |
*fault_type = "SIGILL"; |
| 157 |
|
| 158 |
switch (info->si_code) |
| 159 |
{ |
| 160 |
#ifdef ILL_ILLOPC |
| 161 |
case ILL_ILLOPC: |
| 162 |
*fault_code = "ILL_ILLOPC"; |
| 163 |
break; |
| 164 |
#endif /* ILL_ILLOPC */ |
| 165 |
#ifdef ILL_ILLOPN |
| 166 |
case ILL_ILLOPN: |
| 167 |
*fault_code = "ILL_ILLOPN"; |
| 168 |
break; |
| 169 |
#endif /* ILL_ILLOPN */ |
| 170 |
#ifdef ILL_ILLADR |
| 171 |
case ILL_ILLADR: |
| 172 |
*fault_code = "ILL_ILLADR"; |
| 173 |
break; |
| 174 |
#endif /* ILL_ILLADR */ |
| 175 |
#ifdef ILL_ILLTRP |
| 176 |
case ILL_ILLTRP: |
| 177 |
*fault_code = "ILL_ILLTRP"; |
| 178 |
break; |
| 179 |
#endif /* ILL_ILLTRP */ |
| 180 |
#ifdef ILL_PRVOPC |
| 181 |
case ILL_PRVOPC: |
| 182 |
*fault_code = "ILL_PRVOPC"; |
| 183 |
break; |
| 184 |
#endif /* ILL_PRVOPC */ |
| 185 |
#ifdef ILL_PRVREG |
| 186 |
case ILL_PRVREG: |
| 187 |
*fault_code = "ILL_PRVREG"; |
| 188 |
break; |
| 189 |
#endif /* ILL_PRVREG */ |
| 190 |
#ifdef ILL_COPROC |
| 191 |
case ILL_COPROC: |
| 192 |
*fault_code = "ILL_COPROC"; |
| 193 |
break; |
| 194 |
#endif /* ILL_COPROC */ |
| 195 |
#ifdef ILL_BADSTK |
| 196 |
case ILL_BADSTK: |
| 197 |
*fault_code = "ILL_BADSTK"; |
| 198 |
break; |
| 199 |
#endif /* ILL_BADSTK */ |
| 200 |
#ifdef SI_KERNEL |
| 201 |
case SI_KERNEL: |
| 202 |
*fault_code = "<unknown>"; |
| 203 |
break; |
| 204 |
#endif /* SI_KERNEL */ |
| 205 |
default: |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
if (signum == SIGSEGV) |
| 213 |
{ |
| 214 |
*fault_type = "SIGSEGV"; |
| 215 |
|
| 216 |
switch (info->si_code) |
| 217 |
{ |
| 218 |
#ifdef SEGV_MAPERR |
| 219 |
case SEGV_MAPERR: |
| 220 |
*fault_code = "SEGV_MAPERR"; |
| 221 |
break; |
| 222 |
#endif /* SEGV_MAPERR */ |
| 223 |
#ifdef SEGV_ACCERR |
| 224 |
case SEGV_ACCERR: |
| 225 |
*fault_code = "SEGV_ACCERR"; |
| 226 |
break; |
| 227 |
#endif /* SEGV_ACCERR */ |
| 228 |
#ifdef SI_KERNEL |
| 229 |
case SI_KERNEL: |
| 230 |
*fault_code = "<unknown>"; |
| 231 |
break; |
| 232 |
#endif /* SI_KERNEL */ |
| 233 |
default: |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
(void) slog(LG_ERROR, "%s: unrecognised signal %d", __func__, signum); |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
static void ATHEME_FATTR_NORETURN |
| 245 |
contrib_backtrace_signal_handler(const int signum, siginfo_t *const restrict info, |
| 246 |
void ATHEME_VATTR_UNUSED *const restrict ucontext) |
| 247 |
{ |
| 248 |
const char *fault_type = "<unknown>"; |
| 249 |
const char *fault_code = "<unknown>"; |
| 250 |
|
| 251 |
if (! contrib_backtrace_signal_map(signum, info, &fault_type, &fault_code)) |
| 252 |
goto end; |
| 253 |
|
| 254 |
char *fault_null = "<NULL>"; |
| 255 |
char **fault_addr = (info->si_addr) ? backtrace_symbols(&info->si_addr, 1) : &fault_null; |
| 256 |
|
| 257 |
(void) fprintf(crashfp, "\n"); |
| 258 |
(void) fprintf(crashfp, "===8<===8<=== [ BEGIN CRASH REPORT ] ===>8===>8===\n"); |
| 259 |
(void) fprintf(crashfp, "\n"); |
| 260 |
(void) fprintf(crashfp, "Program Version ...: %s (%s)\n", PACKAGE_STRING, SERNO); |
| 261 |
(void) fprintf(crashfp, "Config Flags ......: %s\n", get_conf_opts()); |
| 262 |
(void) fprintf(crashfp, "Fault Type ........: %s (%d)\n", fault_type, signum); |
| 263 |
(void) fprintf(crashfp, "Fault Code ........: %s\n", fault_code); |
| 264 |
(void) fprintf(crashfp, "Fault Address .....: %s\n", (fault_addr) ? *fault_addr : fault_null); |
| 265 |
(void) fprintf(crashfp, "\n"); |
| 266 |
|
| 267 |
void *frames[MAX_STACK_FRAMES]; |
| 268 |
const int framecount = backtrace(frames, MAX_STACK_FRAMES); |
| 269 |
|
| 270 |
if (framecount >= MIN_STACK_FRAMES) |
| 271 |
{ |
| 272 |
(void) fprintf(crashfp, "Backtrace:\n"); |
| 273 |
(void) fprintf(crashfp, "\n"); |
| 274 |
|
| 275 |
char **symbols = backtrace_symbols(frames, framecount); |
| 276 |
|
| 277 |
if (symbols) |
| 278 |
{ |
| 279 |
for (int i = 0; i < framecount; i++) |
| 280 |
(void) fprintf(crashfp, " #%02d %s\n", i, symbols[i]); |
| 281 |
} |
| 282 |
else |
| 283 |
{ |
| 284 |
for (int i = 0; i < framecount; i++) |
| 285 |
(void) fprintf(crashfp, " #%02d %p\n", i, frames[i]); |
| 286 |
|
| 287 |
(void) fprintf(crashfp, "\n"); |
| 288 |
(void) fprintf(crashfp, "No symbols available.\n"); |
| 289 |
} |
| 290 |
} |
| 291 |
else |
| 292 |
(void) fprintf(crashfp, "No backtrace available.\n"); |
| 293 |
|
| 294 |
(void) fprintf(crashfp, "\n"); |
| 295 |
|
| 296 |
if (IS_TAINTED) |
| 297 |
{ |
| 298 |
(void) fprintf(crashfp, "Your installation is tainted; support is unavailable.\n"); |
| 299 |
} |
| 300 |
else |
| 301 |
{ |
| 302 |
#ifdef PACKAGE_BUGREPORT |
| 303 |
(void) fprintf(crashfp, "Please file a bug report for this crash:\n"); |
| 304 |
(void) fprintf(crashfp, " <%s>\n", PACKAGE_BUGREPORT); |
| 305 |
#else /* PACKAGE_BUGREPORT */ |
| 306 |
(void) fprintf(crashfp, "Please file a bug report for this crash.\n"); |
| 307 |
#endif /* !PACKAGE_BUGREPORT */ |
| 308 |
} |
| 309 |
|
| 310 |
(void) fprintf(crashfp, "\n"); |
| 311 |
(void) fprintf(crashfp, "===8<===8<==== [ END CRASH REPORT ] ====>8===>8===\n"); |
| 312 |
(void) fprintf(crashfp, "\n"); |
| 313 |
(void) fflush(crashfp); |
| 314 |
|
| 315 |
#ifdef CRASHLOG_FILE |
| 316 |
(void) fclose(crashfp); |
| 317 |
(void) fprintf(stderr, "Aborting; please see '%s'\n", CRASHLOG_FILE); |
| 318 |
(void) fflush(stderr); |
| 319 |
#endif /* CRASHLOG_FILE */ |
| 320 |
|
| 321 |
end: |
| 322 |
// This will cause a core dump (if enabled) and will result in the program exiting |
| 323 |
abort(); |
| 324 |
} |
| 325 |
|
| 326 |
static void |
| 327 |
mod_init(module_t *const restrict m) |
| 328 |
{ |
| 329 |
struct rlimit coredumps; |
| 330 |
|
| 331 |
if (getrlimit(RLIMIT_CORE, &coredumps) != 0) |
| 332 |
{ |
| 333 |
(void) slog(LG_ERROR, "%s: getrlimit(2) failed: %s", m->name, strerror(errno)); |
| 334 |
} |
| 335 |
else if (coredumps.rlim_max > 0) |
| 336 |
{ |
| 337 |
if (coredumps.rlim_cur < coredumps.rlim_max) |
| 338 |
{ |
| 339 |
coredumps.rlim_cur = coredumps.rlim_max; |
| 340 |
|
| 341 |
if (setrlimit(RLIMIT_CORE, &coredumps) != 0) |
| 342 |
(void) slog(LG_ERROR, "%s: setrlimit(2) failed: %s", m->name, strerror(errno)); |
| 343 |
else |
| 344 |
(void) slog(LG_INFO, "%s: coredumps enabled", m->name); |
| 345 |
} |
| 346 |
else |
| 347 |
(void) slog(LG_INFO, "%s: coredumps enabled", m->name); |
| 348 |
} |
| 349 |
else |
| 350 |
(void) slog(LG_INFO, "%s: coredumps cannot be enabled", m->name); |
| 351 |
|
| 352 |
sigset_t masked_signals; |
| 353 |
|
| 354 |
if (sigfillset(&masked_signals) != 0) |
| 355 |
{ |
| 356 |
(void) slog(LG_ERROR, "%s: sigfillset(3) failed: %s", m->name, strerror(errno)); |
| 357 |
|
| 358 |
m->mflags |= MODFLAG_FAIL; |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
const struct sigaction newsigaction = { |
| 363 |
.sa_sigaction = &contrib_backtrace_signal_handler, |
| 364 |
.sa_mask = masked_signals, |
| 365 |
.sa_flags = SA_SIGINFO, |
| 366 |
}; |
| 367 |
|
| 368 |
if (sigaction(SIGBUS, &newsigaction, &oldbusaction) != 0) |
| 369 |
{ |
| 370 |
(void) slog(LG_ERROR, "%s: sigaction(2) for SIGBUS failed: %s", m->name, strerror(errno)); |
| 371 |
|
| 372 |
m->mflags |= MODFLAG_FAIL; |
| 373 |
return; |
| 374 |
} |
| 375 |
if (sigaction(SIGFPE, &newsigaction, &oldfpeaction) != 0) |
| 376 |
{ |
| 377 |
(void) slog(LG_ERROR, "%s: sigaction(2) for SIGFPE failed: %s", m->name, strerror(errno)); |
| 378 |
|
| 379 |
(void) sigaction(SIGBUS, &oldbusaction, NULL); |
| 380 |
|
| 381 |
m->mflags |= MODFLAG_FAIL; |
| 382 |
return; |
| 383 |
} |
| 384 |
if (sigaction(SIGILL, &newsigaction, &oldillaction) != 0) |
| 385 |
{ |
| 386 |
(void) slog(LG_ERROR, "%s: sigaction(2) for SIGILL failed: %s", m->name, strerror(errno)); |
| 387 |
|
| 388 |
(void) sigaction(SIGBUS, &oldbusaction, NULL); |
| 389 |
(void) sigaction(SIGFPE, &oldfpeaction, NULL); |
| 390 |
|
| 391 |
m->mflags |= MODFLAG_FAIL; |
| 392 |
return; |
| 393 |
} |
| 394 |
if (sigaction(SIGSEGV, &newsigaction, &oldsegvaction) != 0) |
| 395 |
{ |
| 396 |
(void) slog(LG_ERROR, "%s: sigaction(2) for SIGSEGV failed: %s", m->name, strerror(errno)); |
| 397 |
|
| 398 |
(void) sigaction(SIGBUS, &oldbusaction, NULL); |
| 399 |
(void) sigaction(SIGFPE, &oldfpeaction, NULL); |
| 400 |
(void) sigaction(SIGILL, &oldillaction, NULL); |
| 401 |
|
| 402 |
m->mflags |= MODFLAG_FAIL; |
| 403 |
return; |
| 404 |
} |
| 405 |
|
| 406 |
#ifdef CRASHLOG_FILE |
| 407 |
if (! (crashfp = fopen(CRASHLOG_FILE, "w"))) |
| 408 |
{ |
| 409 |
(void) slog(LG_ERROR, "%s: fopen('%s') failed: %s", m->name, CRASHLOG_FILE, strerror(errno)); |
| 410 |
|
| 411 |
(void) sigaction(SIGBUS, &oldbusaction, NULL); |
| 412 |
(void) sigaction(SIGFPE, &oldfpeaction, NULL); |
| 413 |
(void) sigaction(SIGILL, &oldillaction, NULL); |
| 414 |
(void) sigaction(SIGSEGV, &oldsegvaction, NULL); |
| 415 |
|
| 416 |
m->mflags |= MODFLAG_FAIL; |
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
(void) slog(LG_INFO, "%s: logging to '%s'", m->name, CRASHLOG_FILE); |
| 421 |
#else /* CRASHLOG_FILE */ |
| 422 |
(void) slog(LG_INFO, "%s: logging to stderr", m->name); |
| 423 |
#endif /* !CRASHLOG_FILE */ |
| 424 |
} |
| 425 |
|
| 426 |
static void |
| 427 |
mod_deinit(const module_unload_intent_t ATHEME_VATTR_UNUSED intent) |
| 428 |
{ |
| 429 |
(void) sigaction(SIGBUS, &oldbusaction, NULL); |
| 430 |
(void) sigaction(SIGFPE, &oldfpeaction, NULL); |
| 431 |
(void) sigaction(SIGILL, &oldillaction, NULL); |
| 432 |
(void) sigaction(SIGSEGV, &oldsegvaction, NULL); |
| 433 |
|
| 434 |
#ifdef CRASHLOG_FILE |
| 435 |
(void) fclose(crashfp); |
| 436 |
#endif /* CRASHLOG_FILE */ |
| 437 |
} |
| 438 |
|
| 439 |
#else /* HAVE_BACKTRACE_SUPPORT */ |
| 440 |
|
| 441 |
static void |
| 442 |
mod_init(module_t *const restrict m) |
| 443 |
{ |
| 444 |
(void) slog(LG_ERROR, "%s: this module only supports Linux systems with GNU libc >= 2.1", m->name); |
| 445 |
(void) slog(LG_ERROR, "%s: please check your configuration and build environment", m->name); |
| 446 |
|
| 447 |
m->mflags |= MODFLAG_FAIL; |
| 448 |
} |
| 449 |
|
| 450 |
static void |
| 451 |
mod_deinit(const module_unload_intent_t ATHEME_VATTR_UNUSED intent) |
| 452 |
{ |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
#endif /* !HAVE_BACKTRACE_SUPPORT */ |
| 457 |
|
| 458 |
SIMPLE_DECLARE_MODULE_V1("contrib/backtrace", MODULE_UNLOAD_CAPABILITY_OK) |