diff options
author | Corey Farrell <git@cfware.com> | 2015-03-27 07:12:25 +0000 |
---|---|---|
committer | Corey Farrell <git@cfware.com> | 2015-03-27 07:12:25 +0000 |
commit | 28e3bd0af7d15f0b1e9dae5f0422ae0fe627b9cd (patch) | |
tree | b55a657449dd559453dc3fd7cab6f0f08a06c9de /include/asterisk | |
parent | 554eb7451638f8384f5150c3adc202194b5fafc0 (diff) |
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
........
Merged revisions 433523 from http://svn.asterisk.org/svn/asterisk/branches/13
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@433524 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'include/asterisk')
-rw-r--r-- | include/asterisk/logger.h | 11 | ||||
-rw-r--r-- | include/asterisk/threadstorage.h | 40 | ||||
-rw-r--r-- | include/asterisk/utils.h | 21 |
3 files changed, 52 insertions, 20 deletions
diff --git a/include/asterisk/logger.h b/include/asterisk/logger.h index feb9c7eda..efaac4887 100644 --- a/include/asterisk/logger.h +++ b/include/asterisk/logger.h @@ -62,6 +62,17 @@ extern "C" { void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf, 5, 6))); +/*! + * \brief Used for sending a log message with protection against recursion. + * + * \note This function should be used by all error messages that might be directly + * or indirectly caused by logging. + * + * \see ast_log for documentation on the parameters. + */ +void ast_log_safe(int level, const char *file, int line, const char *function, const char *fmt, ...) + __attribute__((format(printf, 5, 6))); + /* XXX needs documentation */ typedef unsigned int ast_callid; diff --git a/include/asterisk/threadstorage.h b/include/asterisk/threadstorage.h index e3ece8b67..4d587a5c7 100644 --- a/include/asterisk/threadstorage.h +++ b/include/asterisk/threadstorage.h @@ -84,6 +84,8 @@ void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len AST_THREADSTORAGE_CUSTOM_SCOPE(name, NULL, ast_free_ptr,) #define AST_THREADSTORAGE_EXTERNAL(name) \ extern struct ast_threadstorage name +#define AST_THREADSTORAGE_RAW(name) \ + AST_THREADSTORAGE_CUSTOM_SCOPE(name, NULL, NULL,) /*! * \brief Define a thread storage variable, with custom initialization and cleanup @@ -216,4 +218,42 @@ void *__ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size, co #define ast_threadstorage_get(ts, init_size) __ast_threadstorage_get(ts, init_size, __FILE__, __PRETTY_FUNCTION__, __LINE__) #endif /* defined(DEBUG_THREADLOCALS) */ +/*! + * \brief Retrieve a raw pointer from threadstorage. + * \param ts Threadstorage object to operate on. + * + * \return A pointer associated with the current thread, NULL + * if no pointer is associated yet. + * + * \note This should only be used on threadstorage declared + * by AST_THREADSTORAGE_RAW unless you really know what + * you are doing. + */ +AST_INLINE_API( +void *ast_threadstorage_get_ptr(struct ast_threadstorage *ts), +{ + pthread_once(&ts->once, ts->key_init); + return pthread_getspecific(ts->key); +} +) + +/*! + * \brief Set a raw pointer from threadstorage. + * \param ts Threadstorage object to operate on. + * + * \retval 0 Success + * \retval non-zero Failure + * + * \note This should only be used on threadstorage declared + * by AST_THREADSTORAGE_RAW unless you really know what + * you are doing. + */ +AST_INLINE_API( +int ast_threadstorage_set_ptr(struct ast_threadstorage *ts, void *ptr), +{ + pthread_once(&ts->once, ts->key_init); + return pthread_setspecific(ts->key, ptr); +} +) + #endif /* ASTERISK_THREADSTORAGE_H */ diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h index e4c4c8bea..e48ca02c0 100644 --- a/include/asterisk/utils.h +++ b/include/asterisk/utils.h @@ -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() |