blob: 6c92522927beee7ac4853d3ce01d4fce5d5f869a [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file panic.c
2 * Routines for panic handling. */
Sylvain Munautac3e61a2010-07-25 18:08:54 +02003/*
4 * (C) 2010 by Sylvain Munaut <tnt@246tNt.com>
5 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Sylvain Munautac3e61a2010-07-25 18:08:54 +020010 * 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 *
Sylvain Munautac3e61a2010-07-25 18:08:54 +020020 */
21
Harald Weltea523d142011-08-17 16:09:19 +020022/*! \addtogroup utils
23 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020024 * \file panic.c */
Harald Weltea523d142011-08-17 16:09:19 +020025
Harald Weltebc568d02018-06-29 20:23:29 +020026#include <unistd.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010027#include <osmocom/core/panic.h>
28#include <osmocom/core/backtrace.h>
Sylvain Munautac3e61a2010-07-25 18:08:54 +020029
30#include "../config.h"
31
32
33static osmo_panic_handler_t osmo_panic_handler = (void*)0;
34
35
36#ifndef PANIC_INFLOOP
37
38#include <stdio.h>
39#include <stdlib.h>
40
41static void osmo_panic_default(const char *fmt, va_list args)
42{
43 vfprintf(stderr, fmt, args);
Pablo Neira Ayuso619b8b32011-05-07 12:45:47 +020044 osmo_generate_backtrace();
Sylvain Munautac3e61a2010-07-25 18:08:54 +020045 abort();
46}
47
48#else
49
50static void osmo_panic_default(const char *fmt, va_list args)
51{
52 while (1);
53}
54
55#endif
56
57
Neels Hofmeyr87e45502017-06-20 00:17:59 +020058/*! Terminate the current program with a panic
Harald Welte2d2e2cc2016-04-25 12:11:20 +020059 *
60 * You can call this function in case some severely unexpected situation
61 * is detected and the program is supposed to terminate in a way that
62 * reports the fact that it terminates.
63 *
64 * The application can register a panic handler function using \ref
65 * osmo_set_panic_handler. If it doesn't, a default panic handler
66 * function is called automatically.
67 *
68 * The default function on most systems will generate a backtrace and
69 * then abort() the process.
70 */
Sylvain Munautac3e61a2010-07-25 18:08:54 +020071void osmo_panic(const char *fmt, ...)
72{
73 va_list args;
74
75 va_start(args, fmt);
76
77 if (osmo_panic_handler)
78 osmo_panic_handler(fmt, args);
79 else
80 osmo_panic_default(fmt, args);
81
82 va_end(args);
Harald Weltebc568d02018-06-29 20:23:29 +020083
84 /* not reached, but make compiler believe we really never return */
85#ifndef PANIC_INFLOOP
86 exit(2342);
87#else
88 while (1) ;
89#endif
Sylvain Munautac3e61a2010-07-25 18:08:54 +020090}
Sylvain Munautac3e61a2010-07-25 18:08:54 +020091
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092/*! Set the panic handler
Harald Welte2d2e2cc2016-04-25 12:11:20 +020093 * \param[in] h New panic handler function
94 *
95 * This changes the panic handling function from the currently active
96 * function to a new call-back function supplied by the caller.
97 */
Sylvain Munautc91d17b2010-11-13 18:00:25 +010098void osmo_set_panic_handler(osmo_panic_handler_t h)
Sylvain Munautac3e61a2010-07-25 18:08:54 +020099{
100 osmo_panic_handler = h;
101}
102
Harald Weltede6e4982012-12-06 21:25:27 +0100103/*! @} */