Improved and portable ast_log recursion avoidance

This introduces a new logger routine ast_log_safe.  This routine should be
used for all error messages in code that can be run as a result of ast_log.
ast_log_safe does nothing if run recursively.  All error logging in
astobj2.c, strings.c and utils.h have been switched to ast_log_safe.

This required adding support for raw threadstorage.  This provides direct
access to the void* pointer in threadstorage.  In ast_log_safe, NULL is used
to signify that this thread is not already running ast_log_safe, (void*)1 when
it is already running.  This was done since it's critical that ast_log_safe
do nothing that could log during recursion checking.

ASTERISK-24155 #close
Reported by: Timo Teräs
Review: https://reviewboard.asterisk.org/r/4502/
........

Merged revisions 433522 from http://svn.asterisk.org/svn/asterisk/branches/11


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@433523 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Corey Farrell
2015-03-27 07:09:26 +00:00
parent 4b225e2104
commit d01706ce1e
7 changed files with 87 additions and 28 deletions

View File

@@ -25,7 +25,6 @@
#include "asterisk/network.h"
#include <execinfo.h>
#include <time.h> /* we want to override localtime_r */
#include <unistd.h>
#include <string.h>
@@ -526,26 +525,8 @@ long int ast_random(void);
#define ast_free free
#define ast_free_ptr ast_free
/*
* This buffer is in static memory. We never intend to read it,
* nor do we care about multiple threads writing to it at the
* same time. We only want to know if we're recursing too deep
* already. 60 entries should be more than enough. Function
* call depth rarely exceeds 20 or so.
*/
#define _AST_MEM_BACKTRACE_BUFLEN 60
extern void *_ast_mem_backtrace_buffer[_AST_MEM_BACKTRACE_BUFLEN];
/*
* Ok, this sucks. But if we're already out of mem, we don't
* want the logger to create infinite recursion (and a crash).
*/
#define MALLOC_FAILURE_MSG \
do { \
if (backtrace(_ast_mem_backtrace_buffer, _AST_MEM_BACKTRACE_BUFLEN) < _AST_MEM_BACKTRACE_BUFLEN) { \
ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file); \
} \
} while (0)
ast_log_safe(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file)
/*!
* \brief A wrapper for malloc()