Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file panic.c |
| 2 | * Routines for panic handling. */ |
Sylvain Munaut | ac3e61a | 2010-07-25 18:08:54 +0200 | [diff] [blame] | 3 | /* |
| 4 | * (C) 2010 by Sylvain Munaut <tnt@246tNt.com> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Sylvain Munaut | ac3e61a | 2010-07-25 18:08:54 +0200 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
Harald Welte | a523d14 | 2011-08-17 16:09:19 +0200 | [diff] [blame] | 26 | /*! \addtogroup utils |
| 27 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 28 | * \file panic.c */ |
Harald Welte | a523d14 | 2011-08-17 16:09:19 +0200 | [diff] [blame] | 29 | |
Harald Welte | bc568d0 | 2018-06-29 20:23:29 +0200 | [diff] [blame] | 30 | #include <unistd.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 31 | #include <osmocom/core/panic.h> |
| 32 | #include <osmocom/core/backtrace.h> |
Sylvain Munaut | ac3e61a | 2010-07-25 18:08:54 +0200 | [diff] [blame] | 33 | |
| 34 | #include "../config.h" |
| 35 | |
| 36 | |
| 37 | static osmo_panic_handler_t osmo_panic_handler = (void*)0; |
| 38 | |
| 39 | |
| 40 | #ifndef PANIC_INFLOOP |
| 41 | |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | |
| 45 | static void osmo_panic_default(const char *fmt, va_list args) |
| 46 | { |
| 47 | vfprintf(stderr, fmt, args); |
Pablo Neira Ayuso | 619b8b3 | 2011-05-07 12:45:47 +0200 | [diff] [blame] | 48 | osmo_generate_backtrace(); |
Sylvain Munaut | ac3e61a | 2010-07-25 18:08:54 +0200 | [diff] [blame] | 49 | abort(); |
| 50 | } |
| 51 | |
| 52 | #else |
| 53 | |
| 54 | static void osmo_panic_default(const char *fmt, va_list args) |
| 55 | { |
| 56 | while (1); |
| 57 | } |
| 58 | |
| 59 | #endif |
| 60 | |
| 61 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 62 | /*! Terminate the current program with a panic |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 63 | * |
| 64 | * You can call this function in case some severely unexpected situation |
| 65 | * is detected and the program is supposed to terminate in a way that |
| 66 | * reports the fact that it terminates. |
| 67 | * |
| 68 | * The application can register a panic handler function using \ref |
| 69 | * osmo_set_panic_handler. If it doesn't, a default panic handler |
| 70 | * function is called automatically. |
| 71 | * |
| 72 | * The default function on most systems will generate a backtrace and |
| 73 | * then abort() the process. |
| 74 | */ |
Sylvain Munaut | ac3e61a | 2010-07-25 18:08:54 +0200 | [diff] [blame] | 75 | void osmo_panic(const char *fmt, ...) |
| 76 | { |
| 77 | va_list args; |
| 78 | |
| 79 | va_start(args, fmt); |
| 80 | |
| 81 | if (osmo_panic_handler) |
| 82 | osmo_panic_handler(fmt, args); |
| 83 | else |
| 84 | osmo_panic_default(fmt, args); |
| 85 | |
| 86 | va_end(args); |
Harald Welte | bc568d0 | 2018-06-29 20:23:29 +0200 | [diff] [blame] | 87 | |
| 88 | /* not reached, but make compiler believe we really never return */ |
| 89 | #ifndef PANIC_INFLOOP |
| 90 | exit(2342); |
| 91 | #else |
| 92 | while (1) ; |
| 93 | #endif |
Sylvain Munaut | ac3e61a | 2010-07-25 18:08:54 +0200 | [diff] [blame] | 94 | } |
Sylvain Munaut | ac3e61a | 2010-07-25 18:08:54 +0200 | [diff] [blame] | 95 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 96 | /*! Set the panic handler |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 97 | * \param[in] h New panic handler function |
| 98 | * |
| 99 | * This changes the panic handling function from the currently active |
| 100 | * function to a new call-back function supplied by the caller. |
| 101 | */ |
Sylvain Munaut | c91d17b | 2010-11-13 18:00:25 +0100 | [diff] [blame] | 102 | void osmo_set_panic_handler(osmo_panic_handler_t h) |
Sylvain Munaut | ac3e61a | 2010-07-25 18:08:54 +0200 | [diff] [blame] | 103 | { |
| 104 | osmo_panic_handler = h; |
| 105 | } |
| 106 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 107 | /*! @} */ |