summaryrefslogtreecommitdiff
path: root/main/logger.c
diff options
context:
space:
mode:
authorCorey Farrell <git@cfware.com>2015-03-27 07:09:26 +0000
committerCorey Farrell <git@cfware.com>2015-03-27 07:09:26 +0000
commitd01706ce1ee518118456d5673f529204bdac73bb (patch)
tree426f775451dc84e5e7b65d8a57d0b2be1fcbd378 /main/logger.c
parent4b225e210485fa41b8cf00f6955a22101ff7ef6b (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 git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@433523 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/logger.c')
-rw-r--r--main/logger.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/main/logger.c b/main/logger.c
index 8ab75876c..183e75d08 100644
--- a/main/logger.c
+++ b/main/logger.c
@@ -107,6 +107,7 @@ static struct {
} logfiles = { 1 };
static char hostname[MAXHOSTNAMELEN];
+AST_THREADSTORAGE_RAW(in_safe_log);
enum logtypes {
LOGTYPE_SYSLOG,
@@ -1766,6 +1767,36 @@ void ast_log(int level, const char *file, int line, const char *function, const
}
}
+void ast_log_safe(int level, const char *file, int line, const char *function, const char *fmt, ...)
+{
+ va_list ap;
+ void *recursed = ast_threadstorage_get_ptr(&in_safe_log);
+ struct ast_callid *callid;
+
+ if (recursed) {
+ return;
+ }
+
+ if (ast_threadstorage_set_ptr(&in_safe_log, (void*)1)) {
+ /* We've failed to set the flag that protects against
+ * recursion, so bail. */
+ return;
+ }
+
+ callid = ast_read_threadstorage_callid();
+
+ va_start(ap, fmt);
+ ast_log_full(level, file, line, function, callid, fmt, ap);
+ va_end(ap);
+
+ if (callid) {
+ ast_callid_unref(callid);
+ }
+
+ /* Clear flag so the next allocation failure can be logged. */
+ ast_threadstorage_set_ptr(&in_safe_log, NULL);
+}
+
void ast_log_callid(int level, const char *file, int line, const char *function, struct ast_callid *callid, const char *fmt, ...)
{
va_list ap;