From c4250d10861259c2c45c12af03b5c82da3b639f1 Mon Sep 17 00:00:00 2001 From: Arran Cudbard-Bell Date: Tue, 24 Jun 2014 18:07:12 +0100 Subject: [PATCH] Move exit functions to debug.c --- src/include/libradius.h | 23 ++++----- src/lib/debug.c | 121 +++++++++++++++++++++++++++++++++++++----------- src/lib/heap.c | 11 +---- src/lib/log.c | 51 -------------------- 4 files changed, 108 insertions(+), 98 deletions(-) diff --git a/src/include/libradius.h b/src/include/libradius.h index 06e691e..fef66a0 100644 --- a/src/include/libradius.h +++ b/src/include/libradius.h @@ -476,7 +476,6 @@ DICT_VENDOR *dict_vendorbyvalue(int vendor); #endif /* md5.c */ - void fr_md5_calc(uint8_t *, uint8_t const *, unsigned int); /* radius.c */ @@ -621,14 +620,6 @@ int readvp2(VALUE_PAIR **out, TALLOC_CTX *ctx, FILE *fp, bool *pfiledone); void fr_strerror_printf(char const *, ...) CC_HINT(format (printf, 1, 2)); void fr_perror(char const *, ...) CC_HINT(format (printf, 1, 2)); -extern bool fr_assert_cond(char const *file, int line, char const *expr, bool cond); -#define fr_assert(_x) fr_assert_cond(__FILE__, __LINE__, #_x, (_x)) - -extern void NEVER_RETURNS _fr_exit(char const *file, int line, int status); -#define fr_exit(_x) _fr_exit(__FILE__, __LINE__, (_x)) - -extern void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status); -#define fr_exit_now(_x) _fr_exit_now(__FILE__, __LINE__, (_x)) extern char const *fr_strerror(void); extern char const *fr_syserror(int num); @@ -725,6 +716,7 @@ void fr_rand_seed(void const *, size_t ); /* seed the random pool */ int fr_crypt_check(char const *key, char const *salt); /* cbuff.c */ + typedef struct fr_cbuff fr_cbuff_t; fr_cbuff_t *fr_cbuff_alloc(TALLOC_CTX *ctx, uint32_t size, bool lock); @@ -762,10 +754,19 @@ void fr_fault_set_cb(fr_fault_cb_t func); void fr_fault_set_log_fn(fr_fault_log_t func); void fr_fault_set_log_fd(int fd); -#ifdef WITH_VERIFY_PTR +# ifdef WITH_VERIFY_PTR void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp); void fr_verify_list(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR *vps); -#endif +# endif + +bool fr_assert_cond(char const *file, int line, char const *expr, bool cond); +# define fr_assert(_x) fr_assert_cond(__FILE__, __LINE__, #_x, (_x)) + +void NEVER_RETURNS _fr_exit(char const *file, int line, int status); +# define fr_exit(_x) _fr_exit(__FILE__, __LINE__, (_x)) + +void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status); +# define fr_exit_now(_x) _fr_exit_now(__FILE__, __LINE__, (_x)) /* rbtree.c */ typedef struct rbtree_t rbtree_t; diff --git a/src/lib/debug.c b/src/lib/debug.c index 3b13472..e1f0125 100644 --- a/src/lib/debug.c +++ b/src/lib/debug.c @@ -510,7 +510,7 @@ void fr_fault(int sig) /* * Here we temporarily enable the dumpable flag so if GBD or LLDB - * is called in the panic_action, they can pattach tot he running + * is called in the panic_action, they can pattach to the running * process. */ if (fr_get_dumpable_flag() == 0) { @@ -620,7 +620,7 @@ int fr_log_talloc_report(TALLOC_CTX *ctx) */ static void _fr_fault_mem_report(int sig) { - fr_fault_log("CAUGHT SIGNAL: %s\n", strsignal(sig)); + FR_FAULT_LOG("CAUGHT SIGNAL: %s", strsignal(sig)); if (fr_log_talloc_report(NULL) < 0) fr_perror("memreport"); } @@ -809,7 +809,7 @@ void fr_fault_set_log_fd(int fd) inline void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp) { if (!vp) { - fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR pointer was NULL", file, line); + FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR pointer was NULL", file, line); fr_assert(0); fr_exit_now(0); } @@ -824,25 +824,25 @@ inline void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp) TALLOC_CTX *parent; if (!talloc_get_type(vp->data.ptr, uint8_t)) { - fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be " - "uint8_t but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr)); + FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be " + "uint8_t but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr)); (void) talloc_get_type_abort(vp->data.ptr, uint8_t); } len = talloc_array_length(vp->vp_octets); if (vp->length > len) { - fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than " - "uint8_t data buffer length %zu\n", file, line, vp->da->name, vp->length, len); + FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than " + "uint8_t data buffer length %zu\n", file, line, vp->da->name, vp->length, len); fr_assert(0); fr_exit_now(1); } parent = talloc_parent(vp->data.ptr); if (parent != vp) { - fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer is not " - "parented by VALUE_PAIR %p, instead parented by %p (%s)\n", - file, line, vp->da->name, - vp, parent, parent ? talloc_get_name(parent) : "NULL"); + FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer is not " + "parented by VALUE_PAIR %p, instead parented by %p (%s)\n", + file, line, vp->da->name, + vp, parent, parent ? talloc_get_name(parent) : "NULL"); fr_assert(0); fr_exit_now(1); } @@ -855,32 +855,32 @@ inline void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp) TALLOC_CTX *parent; if (!talloc_get_type(vp->data.ptr, char)) { - fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be " - "char but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr)); + FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be " + "char but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr)); (void) talloc_get_type_abort(vp->data.ptr, char); } len = (talloc_array_length(vp->vp_strvalue) - 1); if (vp->length > len) { - fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than " - "char buffer length %zu\n", file, line, vp->da->name, vp->length, len); + FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than " + "char buffer length %zu\n", file, line, vp->da->name, vp->length, len); fr_assert(0); fr_exit_now(1); } if (vp->vp_strvalue[vp->length] != '\0') { - fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer not \\0 " - "terminated\n", file, line, vp->da->name); + FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer not \\0 " + "terminated\n", file, line, vp->da->name); fr_assert(0); fr_exit_now(1); } parent = talloc_parent(vp->data.ptr); if (parent != vp) { - fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" uint8_t buffer is not " - "parented by VALUE_PAIR %p, instead parented by %p (%s)\n", - file, line, vp->da->name, - vp, parent, parent ? talloc_get_name(parent) : "NULL"); + FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" uint8_t buffer is not " + "parented by VALUE_PAIR %p, instead parented by %p (%s)\n", + file, line, vp->da->name, + vp, parent, parent ? talloc_get_name(parent) : "NULL"); fr_assert(0); fr_exit_now(1); } @@ -908,11 +908,11 @@ void fr_verify_list(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR parent = talloc_parent(vp); if (expected && (parent != expected)) { - fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: Expected VALUE_PAIR \"%s\" to be parented " - "by %p (%s), instead parented by %p (%s)\n", - file, line, vp->da->name, - expected, talloc_get_name(expected), - parent, parent ? talloc_get_name(parent) : "NULL"); + FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: Expected VALUE_PAIR \"%s\" to be parented " + "by %p (%s), instead parented by %p (%s)\n", + file, line, vp->da->name, + expected, talloc_get_name(expected), + parent, parent ? talloc_get_name(parent) : "NULL"); fr_log_talloc_report(expected); if (parent) fr_log_talloc_report(parent); @@ -923,3 +923,72 @@ void fr_verify_list(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR } } #endif + +bool fr_assert_cond(char const *file, int line, char const *expr, bool cond) +{ + if (!cond) { + FR_FAULT_LOG("SOFT ASSERT FAILED %s[%u]: %s", file, line, expr); +#if !defined(NDEBUG) && defined(SIGUSR1) + fr_fault(SIGUSR1); +#endif + return false; + } + + return cond; +} + +/** Exit possibly printing a message about why we're exiting. + * + * Use the fr_exit(status) macro instead of calling this function + * directly. + * + * @param file where fr_exit() was called. + * @param line where fr_exit() was called. + * @param status we're exiting with. + */ +void NEVER_RETURNS _fr_exit(char const *file, int line, int status) +{ +#ifndef NDEBUG + char const *error = fr_strerror(); + + if (error && (status != 0)) { + FR_FAULT_LOG("EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error); + } else { + FR_FAULT_LOG("EXIT(%i) CALLED %s[%u]", status, file, line); + } +#endif + fprintf(stderr, "If running under a debugger it should break <>"); + fflush(stderr); + + fr_debug_break(); /* If running under GDB we'll break here */ + + exit(status); +} + +/** Exit possibly printing a message about why we're exiting. + * + * Use the fr_exit_now(status) macro instead of calling this function + * directly. + * + * @param file where fr_exit_now() was called. + * @param line where fr_exit_now() was called. + * @param status we're exiting with. + */ +void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status) +{ +#ifndef NDEBUG + char const *error = fr_strerror(); + + if (error && (status != 0)) { + FR_FAULT_LOG("_EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error); + } else { + FR_FAULT_LOG("_EXIT(%i) CALLED %s[%u]", status, file, line); + } +#endif + fprintf(stderr, "If running under a debugger it should break <>\n"); + fflush(stderr); + + fr_debug_break(); /* If running under GDB we'll break here */ + + _exit(status); +} diff --git a/src/lib/heap.c b/src/lib/heap.c index e3675cf..6da68ac 100644 --- a/src/lib/heap.c +++ b/src/lib/heap.c @@ -263,15 +263,6 @@ static int heap_cmp(void const *one, void const *two) #define ARRAY_SIZE (1024) -void NEVER_RETURNS _fr_exit(char const *file, int line, int status) -{ -#ifndef NDEBUG - fprintf(stderr, "EXIT CALLED %s[%u]: %i: ", file, line, status); -#endif - fflush(stderr); - exit(status); -} - int main(int argc, char **argv) { fr_heap_t *hp; @@ -296,7 +287,7 @@ int main(int argc, char **argv) fprintf(stderr, "Failed inserting %d\n", i); fr_exit(1); } - + if (!fr_heap_check(hp, &array[i])) { fprintf(stderr, "Inserted but not in heap %d\n", i); fr_exit(1); diff --git a/src/lib/log.c b/src/lib/log.c index 8b38888..b0a978e 100644 --- a/src/lib/log.c +++ b/src/lib/log.c @@ -198,54 +198,3 @@ void fr_perror(char const *fmt, ...) va_end(ap); } - -bool fr_assert_cond(char const *file, int line, char const *expr, bool cond) -{ - if (!cond) { - fr_perror("SOFT ASSERT FAILED %s[%u]: %s", file, line, expr); -#if !defined(NDEBUG) && defined(SIGUSR1) - fr_fault(SIGUSR1); -#endif - return false; - } - - return cond; -} - -void NEVER_RETURNS _fr_exit(char const *file, int line, int status) -{ -#ifndef NDEBUG - char const *error = fr_strerror(); - - if (error && (status != 0)) { - fr_perror("EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error); - } else { - fr_perror("EXIT(%i) CALLED %s[%u]", status, file, line); - } -#endif - fr_perror("If running under a debugger it should break <>"); - fflush(stderr); - - fr_debug_break(); /* If running under GDB we'll break here */ - - exit(status); -} - -void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status) -{ -#ifndef NDEBUG - char const *error = fr_strerror(); - - if (error && (status != 0)) { - fr_perror("_EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error); - } else { - fr_perror("_EXIT(%i) CALLED %s[%u]", status, file, line); - } -#endif - fr_perror("If running under a debugger it should break <>"); - fflush(stderr); - - fr_debug_break(); /* If running under GDB we'll break here */ - - _exit(status); -} -- 2.1.4