| 1 |
/* |
| 2 |
This file is part of libmicrohttpd |
| 3 |
Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff |
| 4 |
Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Lesser General Public |
| 8 |
License as published by the Free Software Foundation; either |
| 9 |
version 2.1 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public |
| 17 |
License along with this library; if not, write to the Free Software |
| 18 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
*/ |
| 20 |
|
| 21 |
/** |
| 22 |
* @file microhttpd/mhd_panic.h |
| 23 |
* @brief MHD_panic() function and helpers |
| 24 |
* @author Daniel Pittman |
| 25 |
* @author Christian Grothoff |
| 26 |
* @author Karlson2k (Evgeny Grin) |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "mhd_panic.h" |
| 30 |
#include "platform.h" |
| 31 |
#include "microhttpd.h" |
| 32 |
|
| 33 |
/** |
| 34 |
* Handler for fatal errors. |
| 35 |
*/ |
| 36 |
MHD_PanicCallback mhd_panic = (MHD_PanicCallback) NULL; |
| 37 |
|
| 38 |
/** |
| 39 |
* Closure argument for #mhd_panic. |
| 40 |
*/ |
| 41 |
void *mhd_panic_cls = NULL; |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Default implementation of the panic function, |
| 46 |
* prints an error message and aborts. |
| 47 |
* |
| 48 |
* @param cls unused |
| 49 |
* @param file name of the file with the problem |
| 50 |
* @param line line number with the problem |
| 51 |
* @param reason error message with details |
| 52 |
*/ |
| 53 |
_MHD_NORETURN static void |
| 54 |
mhd_panic_std (void *cls, |
| 55 |
const char *file, |
| 56 |
unsigned int line, |
| 57 |
const char *reason) |
| 58 |
{ |
| 59 |
(void) cls; /* Mute compiler warning. */ |
| 60 |
#ifdef HAVE_MESSAGES |
| 61 |
fprintf (stderr, |
| 62 |
_ ("Fatal error in GNU libmicrohttpd %s:%u: %s\n"), |
| 63 |
file, |
| 64 |
line, |
| 65 |
reason); |
| 66 |
#else /* ! HAVE_MESSAGES */ |
| 67 |
(void) file; /* Mute compiler warning. */ |
| 68 |
(void) line; /* Mute compiler warning. */ |
| 69 |
(void) reason; /* Mute compiler warning. */ |
| 70 |
#endif |
| 71 |
abort (); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Sets the global error handler to a different implementation. |
| 77 |
* |
| 78 |
* @a cb will only be called in the case of typically fatal, serious internal |
| 79 |
* consistency issues or serious system failures like failed lock of mutex. |
| 80 |
* |
| 81 |
* These issues should only arise in the case of serious memory corruption or |
| 82 |
* similar problems with the architecture, there is no safe way to continue |
| 83 |
* even for closing of the application. |
| 84 |
* |
| 85 |
* The default implementation that is used if no panic function is set simply |
| 86 |
* prints an error message and calls `abort()`. |
| 87 |
* Alternative implementations might call `exit()` or other similar functions. |
| 88 |
* |
| 89 |
* @param cb new error handler or NULL to use default handler |
| 90 |
* @param cls passed to @a cb |
| 91 |
* @ingroup logging |
| 92 |
*/ |
| 93 |
_MHD_EXTERN void |
| 94 |
MHD_set_panic_func (MHD_PanicCallback cb, |
| 95 |
void *cls) |
| 96 |
{ |
| 97 |
if ((MHD_PanicCallback) NULL != cb) |
| 98 |
mhd_panic = cb; |
| 99 |
else |
| 100 |
mhd_panic = &mhd_panic_std; |
| 101 |
|
| 102 |
mhd_panic_cls = cls; |
| 103 |
} |