Add some additional documentation for core AMI events
[asterisk/asterisk.git] / main / logger.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Asterisk Logger
22  *
23  * Logging routines
24  *
25  * \author Mark Spencer <markster@digium.com>
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 /* When we include logger.h again it will trample on some stuff in syslog.h, but
37  * nothing we care about in here. */
38 #include <syslog.h>
39
40 #include "asterisk/_private.h"
41 #include "asterisk/paths.h"     /* use ast_config_AST_LOG_DIR */
42 #include "asterisk/logger.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/config.h"
46 #include "asterisk/term.h"
47 #include "asterisk/cli.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/manager.h"
50 #include "asterisk/astobj2.h"
51 #include "asterisk/threadstorage.h"
52 #include "asterisk/strings.h"
53 #include "asterisk/pbx.h"
54 #include "asterisk/app.h"
55 #include "asterisk/syslog.h"
56 #include "asterisk/buildinfo.h"
57 #include "asterisk/ast_version.h"
58
59 #include <signal.h>
60 #include <time.h>
61 #include <sys/stat.h>
62 #include <fcntl.h>
63 #ifdef HAVE_BKTR
64 #include <execinfo.h>
65 #define MAX_BACKTRACE_FRAMES 20
66 #  if defined(HAVE_DLADDR) && defined(HAVE_BFD) && defined(BETTER_BACKTRACES)
67 #    include <dlfcn.h>
68 #    include <bfd.h>
69 #  endif
70 #endif
71
72 static char dateformat[256] = "%b %e %T";               /* Original Asterisk Format */
73
74 static char queue_log_name[256] = QUEUELOG;
75 static char exec_after_rotate[256] = "";
76
77 static int filesize_reload_needed;
78 static unsigned int global_logmask = 0xFFFF;
79 static int queuelog_init;
80 static int logger_initialized;
81 static volatile int next_unique_callid; /* Used to assign unique call_ids to calls */
82 static int display_callids;
83 static void unique_callid_cleanup(void *data);
84
85 struct ast_callid {
86     int call_identifier; /* Numerical value of the call displayed in the logs */
87 };
88
89 AST_THREADSTORAGE_CUSTOM(unique_callid, NULL, unique_callid_cleanup);
90
91 static enum rotatestrategy {
92         SEQUENTIAL = 1 << 0,     /* Original method - create a new file, in order */
93         ROTATE = 1 << 1,         /* Rotate all files, such that the oldest file has the highest suffix */
94         TIMESTAMP = 1 << 2,      /* Append the epoch timestamp onto the end of the archived file */
95 } rotatestrategy = SEQUENTIAL;
96
97 static struct {
98         unsigned int queue_log:1;
99         unsigned int queue_log_to_file:1;
100         unsigned int queue_adaptive_realtime:1;
101 } logfiles = { 1 };
102
103 static char hostname[MAXHOSTNAMELEN];
104
105 enum logtypes {
106         LOGTYPE_SYSLOG,
107         LOGTYPE_FILE,
108         LOGTYPE_CONSOLE,
109 };
110
111 struct logchannel {
112         /*! What to log to this channel */
113         unsigned int logmask;
114         /*! If this channel is disabled or not */
115         int disabled;
116         /*! syslog facility */
117         int facility;
118         /*! Verbosity level */
119         int verbosity;
120         /*! Type of log channel */
121         enum logtypes type;
122         /*! logfile logging file pointer */
123         FILE *fileptr;
124         /*! Filename */
125         char filename[PATH_MAX];
126         /*! field for linking to list */
127         AST_LIST_ENTRY(logchannel) list;
128         /*! Line number from configuration file */
129         int lineno;
130         /*! Components (levels) from last config load */
131         char components[0];
132 };
133
134 static AST_RWLIST_HEAD_STATIC(logchannels, logchannel);
135
136 enum logmsgtypes {
137         LOGMSG_NORMAL = 0,
138         LOGMSG_VERBOSE,
139 };
140
141 struct logmsg {
142         enum logmsgtypes type;
143         int level;
144         int line;
145         int lwp;
146         struct ast_callid *callid;
147         AST_DECLARE_STRING_FIELDS(
148                 AST_STRING_FIELD(date);
149                 AST_STRING_FIELD(file);
150                 AST_STRING_FIELD(function);
151                 AST_STRING_FIELD(message);
152                 AST_STRING_FIELD(level_name);
153         );
154         AST_LIST_ENTRY(logmsg) list;
155 };
156
157 static void logmsg_free(struct logmsg *msg)
158 {
159         if (msg->callid) {
160                 ast_callid_unref(msg->callid);
161         }
162         ast_free(msg);
163 }
164
165 static AST_LIST_HEAD_STATIC(logmsgs, logmsg);
166 static pthread_t logthread = AST_PTHREADT_NULL;
167 static ast_cond_t logcond;
168 static int close_logger_thread = 0;
169
170 static FILE *qlog;
171
172 /*! \brief Logging channels used in the Asterisk logging system
173  *
174  * The first 16 levels are reserved for system usage, and the remaining
175  * levels are reserved for usage by dynamic levels registered via
176  * ast_logger_register_level.
177  */
178
179 /* Modifications to this array are protected by the rwlock in the
180  * logchannels list.
181  */
182
183 static char *levels[NUMLOGLEVELS] = {
184         "DEBUG",
185         "---EVENT---",          /* no longer used */
186         "NOTICE",
187         "WARNING",
188         "ERROR",
189         "VERBOSE",
190         "DTMF",
191 };
192
193 /*! \brief Colors used in the console for logging */
194 static const int colors[NUMLOGLEVELS] = {
195         COLOR_BRGREEN,
196         COLOR_BRBLUE,           /* no longer used */
197         COLOR_YELLOW,
198         COLOR_BRRED,
199         COLOR_RED,
200         COLOR_GREEN,
201         COLOR_BRGREEN,
202         0,
203         0,
204         0,
205         0,
206         0,
207         0,
208         0,
209         0,
210         0,
211         COLOR_BRBLUE,
212         COLOR_BRBLUE,
213         COLOR_BRBLUE,
214         COLOR_BRBLUE,
215         COLOR_BRBLUE,
216         COLOR_BRBLUE,
217         COLOR_BRBLUE,
218         COLOR_BRBLUE,
219         COLOR_BRBLUE,
220         COLOR_BRBLUE,
221         COLOR_BRBLUE,
222         COLOR_BRBLUE,
223         COLOR_BRBLUE,
224         COLOR_BRBLUE,
225         COLOR_BRBLUE,
226         COLOR_BRBLUE,
227 };
228
229 AST_THREADSTORAGE(verbose_buf);
230 #define VERBOSE_BUF_INIT_SIZE   256
231
232 AST_THREADSTORAGE(log_buf);
233 #define LOG_BUF_INIT_SIZE       256
234
235 static void logger_queue_init(void);
236
237 static unsigned int make_components(const char *s, int lineno, int *verbosity)
238 {
239         char *w;
240         unsigned int res = 0;
241         char *stringp = ast_strdupa(s);
242         unsigned int x;
243
244         *verbosity = 3;
245
246         while ((w = strsep(&stringp, ","))) {
247                 w = ast_skip_blanks(w);
248
249                 if (!strcmp(w, "*")) {
250                         res = 0xFFFFFFFF;
251                         break;
252                 } else if (!strncasecmp(w, "verbose(", 8) && sscanf(w + 8, "%d)", verbosity) == 1) {
253                         res |= (1 << __LOG_VERBOSE);
254                         break;
255                 } else for (x = 0; x < ARRAY_LEN(levels); x++) {
256                         if (levels[x] && !strcasecmp(w, levels[x])) {
257                                 res |= (1 << x);
258                                 break;
259                         }
260                 }
261         }
262
263         return res;
264 }
265
266 static struct logchannel *make_logchannel(const char *channel, const char *components, int lineno)
267 {
268         struct logchannel *chan;
269         char *facility;
270         struct ast_tm tm;
271         struct timeval now = ast_tvnow();
272         char datestring[256];
273
274         if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan) + strlen(components) + 1)))
275                 return NULL;
276
277         strcpy(chan->components, components);
278         chan->lineno = lineno;
279
280         if (!strcasecmp(channel, "console")) {
281                 chan->type = LOGTYPE_CONSOLE;
282         } else if (!strncasecmp(channel, "syslog", 6)) {
283                 /*
284                 * syntax is:
285                 *  syslog.facility => level,level,level
286                 */
287                 facility = strchr(channel, '.');
288                 if (!facility++ || !facility) {
289                         facility = "local0";
290                 }
291
292                 chan->facility = ast_syslog_facility(facility);
293
294                 if (chan->facility < 0) {
295                         fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
296                         ast_free(chan);
297                         return NULL;
298                 }
299
300                 chan->type = LOGTYPE_SYSLOG;
301                 ast_copy_string(chan->filename, channel, sizeof(chan->filename));
302                 openlog("asterisk", LOG_PID, chan->facility);
303         } else {
304                 if (!ast_strlen_zero(hostname)) {
305                         snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",
306                                  channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel, hostname);
307                 } else {
308                         snprintf(chan->filename, sizeof(chan->filename), "%s/%s",
309                                  channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel);
310                 }
311                 if (!(chan->fileptr = fopen(chan->filename, "a"))) {
312                         /* Can't do real logging here since we're called with a lock
313                          * so log to any attached consoles */
314                         ast_console_puts_mutable("ERROR: Unable to open log file '", __LOG_ERROR);
315                         ast_console_puts_mutable(chan->filename, __LOG_ERROR);
316                         ast_console_puts_mutable("': ", __LOG_ERROR);
317                         ast_console_puts_mutable(strerror(errno), __LOG_ERROR);
318                         ast_console_puts_mutable("'\n", __LOG_ERROR);
319                         ast_free(chan);
320                         return NULL;
321                 } else {
322                         /* Create our date/time */
323                         ast_localtime(&now, &tm, NULL);
324                         ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
325
326                         fprintf(chan->fileptr, "[%s] Asterisk %s built by %s @ %s on a %s running %s on %s\n",
327                                 datestring, ast_get_version(), ast_build_user, ast_build_hostname,
328                                 ast_build_machine, ast_build_os, ast_build_date);
329                         fflush(chan->fileptr);
330                 }
331                 chan->type = LOGTYPE_FILE;
332         }
333         chan->logmask = make_components(chan->components, lineno, &chan->verbosity);
334
335         return chan;
336 }
337
338 static void init_logger_chain(int locked, const char *altconf)
339 {
340         struct logchannel *chan;
341         struct ast_config *cfg;
342         struct ast_variable *var;
343         const char *s;
344         struct ast_flags config_flags = { 0 };
345
346         display_callids = 1;
347
348         if (!(cfg = ast_config_load2(S_OR(altconf, "logger.conf"), "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
349                 return;
350         }
351
352         /* delete our list of log channels */
353         if (!locked) {
354                 AST_RWLIST_WRLOCK(&logchannels);
355         }
356         while ((chan = AST_RWLIST_REMOVE_HEAD(&logchannels, list))) {
357                 ast_free(chan);
358         }
359         global_logmask = 0;
360         if (!locked) {
361                 AST_RWLIST_UNLOCK(&logchannels);
362         }
363
364         errno = 0;
365         /* close syslog */
366         closelog();
367
368         /* If no config file, we're fine, set default options. */
369         if (!cfg) {
370                 if (errno) {
371                         fprintf(stderr, "Unable to open logger.conf: %s; default settings will be used.\n", strerror(errno));
372                 } else {
373                         fprintf(stderr, "Errors detected in logger.conf: see above; default settings will be used.\n");
374                 }
375                 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
376                         return;
377                 }
378                 chan->type = LOGTYPE_CONSOLE;
379                 chan->logmask = __LOG_WARNING | __LOG_NOTICE | __LOG_ERROR;
380                 if (!locked) {
381                         AST_RWLIST_WRLOCK(&logchannels);
382                 }
383                 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
384                 global_logmask |= chan->logmask;
385                 if (!locked) {
386                         AST_RWLIST_UNLOCK(&logchannels);
387                 }
388                 return;
389         }
390
391         if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
392                 if (ast_true(s)) {
393                         if (gethostname(hostname, sizeof(hostname) - 1)) {
394                                 ast_copy_string(hostname, "unknown", sizeof(hostname));
395                                 fprintf(stderr, "What box has no hostname???\n");
396                         }
397                 } else
398                         hostname[0] = '\0';
399         } else
400                 hostname[0] = '\0';
401         if ((s = ast_variable_retrieve(cfg, "general", "display_callids"))) {
402                 display_callids = ast_true(s);
403         }
404         if ((s = ast_variable_retrieve(cfg, "general", "dateformat")))
405                 ast_copy_string(dateformat, s, sizeof(dateformat));
406         else
407                 ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
408         if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
409                 logfiles.queue_log = ast_true(s);
410         }
411         if ((s = ast_variable_retrieve(cfg, "general", "queue_log_to_file"))) {
412                 logfiles.queue_log_to_file = ast_true(s);
413         }
414         if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name"))) {
415                 ast_copy_string(queue_log_name, s, sizeof(queue_log_name));
416         }
417         if ((s = ast_variable_retrieve(cfg, "general", "exec_after_rotate"))) {
418                 ast_copy_string(exec_after_rotate, s, sizeof(exec_after_rotate));
419         }
420         if ((s = ast_variable_retrieve(cfg, "general", "rotatestrategy"))) {
421                 if (strcasecmp(s, "timestamp") == 0) {
422                         rotatestrategy = TIMESTAMP;
423                 } else if (strcasecmp(s, "rotate") == 0) {
424                         rotatestrategy = ROTATE;
425                 } else if (strcasecmp(s, "sequential") == 0) {
426                         rotatestrategy = SEQUENTIAL;
427                 } else {
428                         fprintf(stderr, "Unknown rotatestrategy: %s\n", s);
429                 }
430         } else {
431                 if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp"))) {
432                         rotatestrategy = ast_true(s) ? TIMESTAMP : SEQUENTIAL;
433                         fprintf(stderr, "rotatetimestamp option has been deprecated.  Please use rotatestrategy instead.\n");
434                 }
435         }
436
437         if (!locked) {
438                 AST_RWLIST_WRLOCK(&logchannels);
439         }
440         var = ast_variable_browse(cfg, "logfiles");
441         for (; var; var = var->next) {
442                 if (!(chan = make_logchannel(var->name, var->value, var->lineno))) {
443                         /* Print error message directly to the consoles since the lock is held
444                          * and we don't want to unlock with the list partially built */
445                         ast_console_puts_mutable("ERROR: Unable to create log channel '", __LOG_ERROR);
446                         ast_console_puts_mutable(var->name, __LOG_ERROR);
447                         ast_console_puts_mutable("'\n", __LOG_ERROR);
448                         continue;
449                 }
450                 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
451                 global_logmask |= chan->logmask;
452         }
453
454         if (qlog) {
455                 fclose(qlog);
456                 qlog = NULL;
457         }
458
459         if (!locked) {
460                 AST_RWLIST_UNLOCK(&logchannels);
461         }
462
463         ast_config_destroy(cfg);
464 }
465
466 void ast_child_verbose(int level, const char *fmt, ...)
467 {
468         char *msg = NULL, *emsg = NULL, *sptr, *eptr;
469         va_list ap, aq;
470         int size;
471
472         va_start(ap, fmt);
473         va_copy(aq, ap);
474         if ((size = vsnprintf(msg, 0, fmt, ap)) < 0) {
475                 va_end(ap);
476                 va_end(aq);
477                 return;
478         }
479         va_end(ap);
480
481         if (!(msg = ast_malloc(size + 1))) {
482                 va_end(aq);
483                 return;
484         }
485
486         vsnprintf(msg, size + 1, fmt, aq);
487         va_end(aq);
488
489         if (!(emsg = ast_malloc(size * 2 + 1))) {
490                 ast_free(msg);
491                 return;
492         }
493
494         for (sptr = msg, eptr = emsg; ; sptr++) {
495                 if (*sptr == '"') {
496                         *eptr++ = '\\';
497                 }
498                 *eptr++ = *sptr;
499                 if (*sptr == '\0') {
500                         break;
501                 }
502         }
503         ast_free(msg);
504
505         fprintf(stdout, "verbose \"%s\" %d\n", emsg, level);
506         fflush(stdout);
507         ast_free(emsg);
508 }
509
510 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
511 {
512         va_list ap;
513         struct timeval tv;
514         struct ast_tm tm;
515         char qlog_msg[8192];
516         int qlog_len;
517         char time_str[30];
518
519         if (!logger_initialized) {
520                 /* You are too early.  We are not open yet! */
521                 return;
522         }
523         if (!queuelog_init) {
524                 AST_RWLIST_WRLOCK(&logchannels);
525                 if (!queuelog_init) {
526                         /*
527                          * We have delayed initializing the queue logging system so
528                          * preloaded realtime modules can get up.  We must initialize
529                          * now since someone is trying to log something.
530                          */
531                         logger_queue_init();
532                         queuelog_init = 1;
533                         AST_RWLIST_UNLOCK(&logchannels);
534                         ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
535                 } else {
536                         AST_RWLIST_UNLOCK(&logchannels);
537                 }
538         }
539
540         if (ast_check_realtime("queue_log")) {
541                 tv = ast_tvnow();
542                 ast_localtime(&tv, &tm, NULL);
543                 ast_strftime(time_str, sizeof(time_str), "%F %T.%6q", &tm);
544                 va_start(ap, fmt);
545                 vsnprintf(qlog_msg, sizeof(qlog_msg), fmt, ap);
546                 va_end(ap);
547                 if (logfiles.queue_adaptive_realtime) {
548                         AST_DECLARE_APP_ARGS(args,
549                                 AST_APP_ARG(data)[5];
550                         );
551                         AST_NONSTANDARD_APP_ARGS(args, qlog_msg, '|');
552                         /* Ensure fields are large enough to receive data */
553                         ast_realtime_require_field("queue_log",
554                                 "data1", RQ_CHAR, strlen(S_OR(args.data[0], "")),
555                                 "data2", RQ_CHAR, strlen(S_OR(args.data[1], "")),
556                                 "data3", RQ_CHAR, strlen(S_OR(args.data[2], "")),
557                                 "data4", RQ_CHAR, strlen(S_OR(args.data[3], "")),
558                                 "data5", RQ_CHAR, strlen(S_OR(args.data[4], "")),
559                                 SENTINEL);
560
561                         /* Store the log */
562                         ast_store_realtime("queue_log", "time", time_str,
563                                 "callid", callid,
564                                 "queuename", queuename,
565                                 "agent", agent,
566                                 "event", event,
567                                 "data1", S_OR(args.data[0], ""),
568                                 "data2", S_OR(args.data[1], ""),
569                                 "data3", S_OR(args.data[2], ""),
570                                 "data4", S_OR(args.data[3], ""),
571                                 "data5", S_OR(args.data[4], ""),
572                                 SENTINEL);
573                 } else {
574                         ast_store_realtime("queue_log", "time", time_str,
575                                 "callid", callid,
576                                 "queuename", queuename,
577                                 "agent", agent,
578                                 "event", event,
579                                 "data", qlog_msg,
580                                 SENTINEL);
581                 }
582
583                 if (!logfiles.queue_log_to_file) {
584                         return;
585                 }
586         }
587
588         if (qlog) {
589                 va_start(ap, fmt);
590                 qlog_len = snprintf(qlog_msg, sizeof(qlog_msg), "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
591                 vsnprintf(qlog_msg + qlog_len, sizeof(qlog_msg) - qlog_len, fmt, ap);
592                 va_end(ap);
593                 AST_RWLIST_RDLOCK(&logchannels);
594                 if (qlog) {
595                         fprintf(qlog, "%s\n", qlog_msg);
596                         fflush(qlog);
597                 }
598                 AST_RWLIST_UNLOCK(&logchannels);
599         }
600 }
601
602 static int rotate_file(const char *filename)
603 {
604         char old[PATH_MAX];
605         char new[PATH_MAX];
606         int x, y, which, found, res = 0, fd;
607         char *suffixes[4] = { "", ".gz", ".bz2", ".Z" };
608
609         switch (rotatestrategy) {
610         case SEQUENTIAL:
611                 for (x = 0; ; x++) {
612                         snprintf(new, sizeof(new), "%s.%d", filename, x);
613                         fd = open(new, O_RDONLY);
614                         if (fd > -1)
615                                 close(fd);
616                         else
617                                 break;
618                 }
619                 if (rename(filename, new)) {
620                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
621                         res = -1;
622                 } else {
623                         filename = new;
624                 }
625                 break;
626         case TIMESTAMP:
627                 snprintf(new, sizeof(new), "%s.%ld", filename, (long)time(NULL));
628                 if (rename(filename, new)) {
629                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
630                         res = -1;
631                 } else {
632                         filename = new;
633                 }
634                 break;
635         case ROTATE:
636                 /* Find the next empty slot, including a possible suffix */
637                 for (x = 0; ; x++) {
638                         found = 0;
639                         for (which = 0; which < ARRAY_LEN(suffixes); which++) {
640                                 snprintf(new, sizeof(new), "%s.%d%s", filename, x, suffixes[which]);
641                                 fd = open(new, O_RDONLY);
642                                 if (fd > -1) {
643                                         close(fd);
644                                         found = 1;
645                                         break;
646                                 }
647                         }
648                         if (!found) {
649                                 break;
650                         }
651                 }
652
653                 /* Found an empty slot */
654                 for (y = x; y > 0; y--) {
655                         for (which = 0; which < ARRAY_LEN(suffixes); which++) {
656                                 snprintf(old, sizeof(old), "%s.%d%s", filename, y - 1, suffixes[which]);
657                                 fd = open(old, O_RDONLY);
658                                 if (fd > -1) {
659                                         /* Found the right suffix */
660                                         close(fd);
661                                         snprintf(new, sizeof(new), "%s.%d%s", filename, y, suffixes[which]);
662                                         if (rename(old, new)) {
663                                                 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
664                                                 res = -1;
665                                         }
666                                         break;
667                                 }
668                         }
669                 }
670
671                 /* Finally, rename the current file */
672                 snprintf(new, sizeof(new), "%s.0", filename);
673                 if (rename(filename, new)) {
674                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
675                         res = -1;
676                 } else {
677                         filename = new;
678                 }
679         }
680
681         if (!ast_strlen_zero(exec_after_rotate)) {
682                 struct ast_channel *c = ast_dummy_channel_alloc();
683                 char buf[512];
684
685                 pbx_builtin_setvar_helper(c, "filename", filename);
686                 pbx_substitute_variables_helper(c, exec_after_rotate, buf, sizeof(buf));
687                 if (c) {
688                         c = ast_channel_unref(c);
689                 }
690                 if (ast_safe_system(buf) == -1) {
691                         ast_log(LOG_WARNING, "error executing '%s'\n", buf);
692                 }
693         }
694         return res;
695 }
696
697 /*!
698  * \internal
699  * \brief Start the realtime queue logging if configured.
700  *
701  * \retval TRUE if not to open queue log file.
702  */
703 static int logger_queue_rt_start(void)
704 {
705         if (ast_check_realtime("queue_log")) {
706                 if (!ast_realtime_require_field("queue_log",
707                         "time", RQ_DATETIME, 26,
708                         "data1", RQ_CHAR, 20,
709                         "data2", RQ_CHAR, 20,
710                         "data3", RQ_CHAR, 20,
711                         "data4", RQ_CHAR, 20,
712                         "data5", RQ_CHAR, 20,
713                         SENTINEL)) {
714                         logfiles.queue_adaptive_realtime = 1;
715                 } else {
716                         logfiles.queue_adaptive_realtime = 0;
717                 }
718
719                 if (!logfiles.queue_log_to_file) {
720                         /* Don't open the log file. */
721                         return 1;
722                 }
723         }
724         return 0;
725 }
726
727 /*!
728  * \internal
729  * \brief Rotate the queue log file and restart.
730  *
731  * \param queue_rotate Log queue rotation mode.
732  *
733  * \note Assumes logchannels is write locked on entry.
734  *
735  * \retval 0 on success.
736  * \retval -1 on error.
737  */
738 static int logger_queue_restart(int queue_rotate)
739 {
740         int res = 0;
741         char qfname[PATH_MAX];
742
743         if (logger_queue_rt_start()) {
744                 return res;
745         }
746
747         snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
748         if (qlog) {
749                 /* Just in case it was still open. */
750                 fclose(qlog);
751                 qlog = NULL;
752         }
753         if (queue_rotate) {
754                 rotate_file(qfname);
755         }
756
757         /* Open the log file. */
758         qlog = fopen(qfname, "a");
759         if (!qlog) {
760                 ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
761                 res = -1;
762         }
763         return res;
764 }
765
766 static int reload_logger(int rotate, const char *altconf)
767 {
768         int queue_rotate = rotate;
769         struct logchannel *f;
770         int res = 0;
771
772         AST_RWLIST_WRLOCK(&logchannels);
773
774         if (qlog) {
775                 if (rotate < 0) {
776                         /* Check filesize - this one typically doesn't need an auto-rotate */
777                         if (ftello(qlog) > 0x40000000) { /* Arbitrarily, 1 GB */
778                                 fclose(qlog);
779                                 qlog = NULL;
780                         } else {
781                                 queue_rotate = 0;
782                         }
783                 } else {
784                         fclose(qlog);
785                         qlog = NULL;
786                 }
787         } else {
788                 queue_rotate = 0;
789         }
790
791         ast_mkdir(ast_config_AST_LOG_DIR, 0777);
792
793         AST_RWLIST_TRAVERSE(&logchannels, f, list) {
794                 if (f->disabled) {
795                         f->disabled = 0;        /* Re-enable logging at reload */
796                         /*** DOCUMENTATION
797                                 <managerEventInstance>
798                                         <synopsis>Raised when a logging channel is re-enabled after a reload operation.</synopsis>
799                                         <syntax>
800                                                 <parameter name="Channel">
801                                                         <para>The name of the logging channel.</para>
802                                                 </parameter>
803                                         </syntax>
804                                 </managerEventInstance>
805                         ***/
806                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
807                 }
808                 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
809                         int rotate_this = 0;
810                         if (ftello(f->fileptr) > 0x40000000) { /* Arbitrarily, 1 GB */
811                                 /* Be more proactive about rotating massive log files */
812                                 rotate_this = 1;
813                         }
814                         fclose(f->fileptr);     /* Close file */
815                         f->fileptr = NULL;
816                         if (rotate || rotate_this) {
817                                 rotate_file(f->filename);
818                         }
819                 }
820         }
821
822         filesize_reload_needed = 0;
823
824         init_logger_chain(1 /* locked */, altconf);
825
826         ast_unload_realtime("queue_log");
827         if (logfiles.queue_log) {
828                 res = logger_queue_restart(queue_rotate);
829                 AST_RWLIST_UNLOCK(&logchannels);
830                 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
831                 ast_verb(1, "Asterisk Queue Logger restarted\n");
832         } else {
833                 AST_RWLIST_UNLOCK(&logchannels);
834         }
835
836         return res;
837 }
838
839 /*! \brief Reload the logger module without rotating log files (also used from loader.c during
840         a full Asterisk reload) */
841 int logger_reload(void)
842 {
843         if (reload_logger(0, NULL)) {
844                 return RESULT_FAILURE;
845         }
846         return RESULT_SUCCESS;
847 }
848
849 static char *handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
850 {
851         switch (cmd) {
852         case CLI_INIT:
853                 e->command = "logger reload";
854                 e->usage =
855                         "Usage: logger reload [<alt-conf>]\n"
856                         "       Reloads the logger subsystem state.  Use after restarting syslogd(8) if you are using syslog logging.\n";
857                 return NULL;
858         case CLI_GENERATE:
859                 return NULL;
860         }
861         if (reload_logger(0, a->argc == 3 ? a->argv[2] : NULL)) {
862                 ast_cli(a->fd, "Failed to reload the logger\n");
863                 return CLI_FAILURE;
864         }
865         return CLI_SUCCESS;
866 }
867
868 static char *handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
869 {
870         switch (cmd) {
871         case CLI_INIT:
872                 e->command = "logger rotate";
873                 e->usage =
874                         "Usage: logger rotate\n"
875                         "       Rotates and Reopens the log files.\n";
876                 return NULL;
877         case CLI_GENERATE:
878                 return NULL;
879         }
880         if (reload_logger(1, NULL)) {
881                 ast_cli(a->fd, "Failed to reload the logger and rotate log files\n");
882                 return CLI_FAILURE;
883         }
884         return CLI_SUCCESS;
885 }
886
887 static char *handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
888 {
889         int x;
890         int state;
891         int level = -1;
892
893         switch (cmd) {
894         case CLI_INIT:
895                 e->command = "logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}";
896                 e->usage =
897                         "Usage: logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}\n"
898                         "       Set a specific log level to enabled/disabled for this console.\n";
899                 return NULL;
900         case CLI_GENERATE:
901                 return NULL;
902         }
903
904         if (a->argc < 5)
905                 return CLI_SHOWUSAGE;
906
907         AST_RWLIST_WRLOCK(&logchannels);
908
909         for (x = 0; x < ARRAY_LEN(levels); x++) {
910                 if (levels[x] && !strcasecmp(a->argv[3], levels[x])) {
911                         level = x;
912                         break;
913                 }
914         }
915
916         AST_RWLIST_UNLOCK(&logchannels);
917
918         state = ast_true(a->argv[4]) ? 1 : 0;
919
920         if (level != -1) {
921                 ast_console_toggle_loglevel(a->fd, level, state);
922                 ast_cli(a->fd, "Logger status for '%s' has been set to '%s'.\n", levels[level], state ? "on" : "off");
923         } else
924                 return CLI_SHOWUSAGE;
925
926         return CLI_SUCCESS;
927 }
928
929 /*! \brief CLI command to show logging system configuration */
930 static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
931 {
932 #define FORMATL "%-35.35s %-8.8s %-9.9s "
933         struct logchannel *chan;
934         switch (cmd) {
935         case CLI_INIT:
936                 e->command = "logger show channels";
937                 e->usage =
938                         "Usage: logger show channels\n"
939                         "       List configured logger channels.\n";
940                 return NULL;
941         case CLI_GENERATE:
942                 return NULL;
943         }
944         ast_cli(a->fd, FORMATL, "Channel", "Type", "Status");
945         ast_cli(a->fd, "Configuration\n");
946         ast_cli(a->fd, FORMATL, "-------", "----", "------");
947         ast_cli(a->fd, "-------------\n");
948         AST_RWLIST_RDLOCK(&logchannels);
949         AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
950                 unsigned int level;
951
952                 ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
953                         chan->disabled ? "Disabled" : "Enabled");
954                 ast_cli(a->fd, " - ");
955                 for (level = 0; level < ARRAY_LEN(levels); level++) {
956                         if ((chan->logmask & (1 << level)) && levels[level]) {
957                                 ast_cli(a->fd, "%s ", levels[level]);
958                         }
959                 }
960                 ast_cli(a->fd, "\n");
961         }
962         AST_RWLIST_UNLOCK(&logchannels);
963         ast_cli(a->fd, "\n");
964
965         return CLI_SUCCESS;
966 }
967
968 struct verb {
969         void (*verboser)(const char *string);
970         AST_LIST_ENTRY(verb) list;
971 };
972
973 static AST_RWLIST_HEAD_STATIC(verbosers, verb);
974
975 static struct ast_cli_entry cli_logger[] = {
976         AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
977         AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
978         AST_CLI_DEFINE(handle_logger_rotate, "Rotates and reopens the log files"),
979         AST_CLI_DEFINE(handle_logger_set_level, "Enables/Disables a specific logging level for this console")
980 };
981
982 static void _handle_SIGXFSZ(int sig)
983 {
984         /* Indicate need to reload */
985         filesize_reload_needed = 1;
986 }
987
988 static struct sigaction handle_SIGXFSZ = {
989         .sa_handler = _handle_SIGXFSZ,
990         .sa_flags = SA_RESTART,
991 };
992
993 static void ast_log_vsyslog(struct logmsg *msg)
994 {
995         char buf[BUFSIZ];
996         int syslog_level = ast_syslog_priority_from_loglevel(msg->level);
997
998         if (syslog_level < 0) {
999                 /* we are locked here, so cannot ast_log() */
1000                 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", msg->level);
1001                 return;
1002         }
1003
1004         snprintf(buf, sizeof(buf), "%s[%d]: %s:%d in %s: %s",
1005                  levels[msg->level], msg->lwp, msg->file, msg->line, msg->function, msg->message);
1006
1007         term_strip(buf, buf, strlen(buf) + 1);
1008         syslog(syslog_level, "%s", buf);
1009 }
1010
1011 /* These gymnastics are due to platforms which designate char as unsigned by
1012  * default. Level is the negative character -- offset by 1, because \0 is the
1013  * EOS delimiter. */
1014 #define VERBOSE_MAGIC2LEVEL(x) (((char) -*(signed char *) (x)) - 1)
1015 #define VERBOSE_HASMAGIC(x)     (*(signed char *) (x) < 0)
1016
1017 /*! \brief Print a normal log message to the channels */
1018 static void logger_print_normal(struct logmsg *logmsg)
1019 {
1020         struct logchannel *chan = NULL;
1021         char buf[BUFSIZ];
1022         struct verb *v = NULL;
1023         int level = 0;
1024
1025         if (logmsg->level == __LOG_VERBOSE) {
1026                 char *tmpmsg = ast_strdupa(logmsg->message + 1);
1027                 level = VERBOSE_MAGIC2LEVEL(logmsg->message);
1028                 /* Iterate through the list of verbosers and pass them the log message string */
1029                 AST_RWLIST_RDLOCK(&verbosers);
1030                 AST_RWLIST_TRAVERSE(&verbosers, v, list)
1031                         v->verboser(logmsg->message);
1032                 AST_RWLIST_UNLOCK(&verbosers);
1033                 ast_string_field_set(logmsg, message, tmpmsg);
1034         }
1035
1036         AST_RWLIST_RDLOCK(&logchannels);
1037
1038         if (!AST_RWLIST_EMPTY(&logchannels)) {
1039                 AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
1040                         char call_identifier_str[13];
1041
1042                         if (logmsg->callid) {
1043                                 snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", logmsg->callid->call_identifier);
1044                         } else {
1045                                 call_identifier_str[0] = '\0';
1046                         }
1047
1048
1049                         /* If the channel is disabled, then move on to the next one */
1050                         if (chan->disabled) {
1051                                 continue;
1052                         }
1053                         if (logmsg->level == __LOG_VERBOSE && level > chan->verbosity) {
1054                                 continue;
1055                         }
1056
1057                         /* Check syslog channels */
1058                         if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << logmsg->level))) {
1059                                 ast_log_vsyslog(logmsg);
1060                         /* Console channels */
1061                         } else if (chan->type == LOGTYPE_CONSOLE && (chan->logmask & (1 << logmsg->level))) {
1062                                 char linestr[128];
1063                                 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
1064
1065                                 /* If the level is verbose, then skip it */
1066                                 if (logmsg->level == __LOG_VERBOSE)
1067                                         continue;
1068
1069                                 /* Turn the numerical line number into a string */
1070                                 snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
1071                                 /* Build string to print out */
1072                                 snprintf(buf, sizeof(buf), "[%s] %s[%d]%s: %s:%s %s: %s",
1073                                          logmsg->date,
1074                                          term_color(tmp1, logmsg->level_name, colors[logmsg->level], 0, sizeof(tmp1)),
1075                                          logmsg->lwp,
1076                                          call_identifier_str,
1077                                          term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
1078                                          term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
1079                                          term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
1080                                          logmsg->message);
1081                                 /* Print out */
1082                                 ast_console_puts_mutable(buf, logmsg->level);
1083                         /* File channels */
1084                         } else if (chan->type == LOGTYPE_FILE && (chan->logmask & (1 << logmsg->level))) {
1085                                 int res = 0;
1086
1087                                 /* If no file pointer exists, skip it */
1088                                 if (!chan->fileptr) {
1089                                         continue;
1090                                 }
1091
1092                                 /* Print out to the file */
1093                                 res = fprintf(chan->fileptr, "[%s] %s[%d]%s %s: %s",
1094                                               logmsg->date, logmsg->level_name, logmsg->lwp, call_identifier_str,
1095                                               logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
1096                                 if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
1097                                         fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
1098                                         if (errno == ENOMEM || errno == ENOSPC)
1099                                                 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
1100                                         else
1101                                                 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
1102                                         /*** DOCUMENTATION
1103                                                 <managerEventInstance>
1104                                                         <synopsis>Raised when a logging channel is disabled.</synopsis>
1105                                                         <syntax>
1106                                                                 <parameter name="Channel">
1107                                                                         <para>The name of the logging channel.</para>
1108                                                                 </parameter>
1109                                                         </syntax>
1110                                                 </managerEventInstance>
1111                                         ***/
1112                                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
1113                                         chan->disabled = 1;
1114                                 } else if (res > 0) {
1115                                         fflush(chan->fileptr);
1116                                 }
1117                         }
1118                 }
1119         } else if (logmsg->level != __LOG_VERBOSE) {
1120                 fputs(logmsg->message, stdout);
1121         }
1122
1123         AST_RWLIST_UNLOCK(&logchannels);
1124
1125         /* If we need to reload because of the file size, then do so */
1126         if (filesize_reload_needed) {
1127                 reload_logger(-1, NULL);
1128                 ast_verb(1, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
1129         }
1130
1131         return;
1132 }
1133
1134 /*! \brief Actual logging thread */
1135 static void *logger_thread(void *data)
1136 {
1137         struct logmsg *next = NULL, *msg = NULL;
1138
1139         for (;;) {
1140                 /* We lock the message list, and see if any message exists... if not we wait on the condition to be signalled */
1141                 AST_LIST_LOCK(&logmsgs);
1142                 if (AST_LIST_EMPTY(&logmsgs)) {
1143                         if (close_logger_thread) {
1144                                 AST_LIST_UNLOCK(&logmsgs);
1145                                 break;
1146                         } else {
1147                                 ast_cond_wait(&logcond, &logmsgs.lock);
1148                         }
1149                 }
1150                 next = AST_LIST_FIRST(&logmsgs);
1151                 AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
1152                 AST_LIST_UNLOCK(&logmsgs);
1153
1154                 /* Otherwise go through and process each message in the order added */
1155                 while ((msg = next)) {
1156                         /* Get the next entry now so that we can free our current structure later */
1157                         next = AST_LIST_NEXT(msg, list);
1158
1159                         /* Depending on the type, send it to the proper function */
1160                         logger_print_normal(msg);
1161
1162                         /* Free the data since we are done */
1163                         logmsg_free(msg);
1164                 }
1165
1166                 /* If we should stop, then stop */
1167                 if (close_logger_thread)
1168                         break;
1169         }
1170
1171         return NULL;
1172 }
1173
1174 /*!
1175  * \internal
1176  * \brief Initialize the logger queue.
1177  *
1178  * \note Assumes logchannels is write locked on entry.
1179  *
1180  * \return Nothing
1181  */
1182 static void logger_queue_init(void)
1183 {
1184         ast_unload_realtime("queue_log");
1185         if (logfiles.queue_log) {
1186                 char qfname[PATH_MAX];
1187
1188                 if (logger_queue_rt_start()) {
1189                         return;
1190                 }
1191
1192                 /* Open the log file. */
1193                 snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR,
1194                         queue_log_name);
1195                 if (qlog) {
1196                         /* Just in case it was already open. */
1197                         fclose(qlog);
1198                 }
1199                 qlog = fopen(qfname, "a");
1200                 if (!qlog) {
1201                         ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
1202                 }
1203         }
1204 }
1205
1206 int init_logger(void)
1207 {
1208         /* auto rotate if sig SIGXFSZ comes a-knockin */
1209         sigaction(SIGXFSZ, &handle_SIGXFSZ, NULL);
1210
1211         /* start logger thread */
1212         ast_cond_init(&logcond, NULL);
1213         if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
1214                 ast_cond_destroy(&logcond);
1215                 return -1;
1216         }
1217
1218         /* register the logger cli commands */
1219         ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
1220
1221         ast_mkdir(ast_config_AST_LOG_DIR, 0777);
1222
1223         /* create log channels */
1224         init_logger_chain(0 /* locked */, NULL);
1225         logger_initialized = 1;
1226
1227         return 0;
1228 }
1229
1230 void close_logger(void)
1231 {
1232         struct logchannel *f = NULL;
1233
1234         logger_initialized = 0;
1235
1236         /* Stop logger thread */
1237         AST_LIST_LOCK(&logmsgs);
1238         close_logger_thread = 1;
1239         ast_cond_signal(&logcond);
1240         AST_LIST_UNLOCK(&logmsgs);
1241
1242         if (logthread != AST_PTHREADT_NULL)
1243                 pthread_join(logthread, NULL);
1244
1245         AST_RWLIST_WRLOCK(&logchannels);
1246
1247         if (qlog) {
1248                 fclose(qlog);
1249                 qlog = NULL;
1250         }
1251
1252         AST_RWLIST_TRAVERSE(&logchannels, f, list) {
1253                 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
1254                         fclose(f->fileptr);
1255                         f->fileptr = NULL;
1256                 }
1257         }
1258
1259         closelog(); /* syslog */
1260
1261         AST_RWLIST_UNLOCK(&logchannels);
1262 }
1263
1264 void ast_callid_strnprint(char *buffer, size_t buffer_size, struct ast_callid *callid)
1265 {
1266         snprintf(buffer, buffer_size, "[C-%08x]", callid->call_identifier);
1267 }
1268
1269 struct ast_callid *ast_create_callid(void)
1270 {
1271         struct ast_callid *call;
1272         int using;
1273
1274         if (!(call = ao2_alloc(sizeof(struct ast_callid), NULL))) {
1275                 ast_log(LOG_ERROR, "Could not allocate callid struct.\n");
1276                 return NULL;
1277         }
1278
1279         using = ast_atomic_fetchadd_int(&next_unique_callid, +1);
1280
1281         call->call_identifier = using;
1282         ast_debug(3, "CALL_ID [C-%08x] created by thread.\n", call->call_identifier);
1283         return call;
1284 }
1285
1286 struct ast_callid *ast_read_threadstorage_callid(void)
1287 {
1288         struct ast_callid **callid;
1289         callid = ast_threadstorage_get(&unique_callid, sizeof(struct ast_callid **));
1290         if (callid && *callid) {
1291                 ast_callid_ref(*callid);
1292                 return *callid;
1293         }
1294
1295         return NULL;
1296
1297 }
1298
1299 int ast_callid_threadassoc_add(struct ast_callid *callid)
1300 {
1301         struct ast_callid **pointing;
1302         pointing = ast_threadstorage_get(&unique_callid, sizeof(struct ast_callid **));
1303         if (!(pointing)) {
1304                 ast_log(LOG_ERROR, "Failed to allocate thread storage.\n");
1305                 return -1;
1306         }
1307
1308         if (!(*pointing)) {
1309                 /* callid will be unreffed at thread destruction */
1310                 ast_callid_ref(callid);
1311                 *pointing = callid;
1312                 ast_debug(3, "CALL_ID [C-%08x] bound to thread.\n", callid->call_identifier);
1313         } else {
1314                 ast_log(LOG_WARNING, "Attempted to ast_callid_threadassoc_add on thread already associated with a callid.\n");
1315                 return 1;
1316         }
1317
1318         return 0;
1319 }
1320
1321 int ast_callid_threadassoc_remove()
1322 {
1323         struct ast_callid **pointing;
1324         pointing = ast_threadstorage_get(&unique_callid, sizeof(struct ast_callid **));
1325         if (!(pointing)) {
1326                 ast_log(LOG_ERROR, "Failed to allocate thread storage.\n");
1327                 return -1;
1328         }
1329
1330         if (!(*pointing)) {
1331                 ast_log(LOG_ERROR, "Tried to clean callid thread storage with no callid in thread storage.\n");
1332                 return -1;
1333         } else {
1334                 ast_debug(3, "Call_ID [C-%08x] being removed from thread.\n", (*pointing)->call_identifier);
1335                 *pointing = ast_callid_unref(*pointing);
1336                 return 0;
1337         }
1338 }
1339
1340 int ast_callid_threadstorage_auto(struct ast_callid **callid)
1341 {
1342         struct ast_callid *tmp;
1343
1344         /* Start by trying to see if a callid is available from thread storage */
1345         tmp = ast_read_threadstorage_callid();
1346         if (tmp) {
1347                 *callid = tmp;
1348                 return 0;
1349         }
1350
1351         /* If that failed, try to create a new one and bind it. */
1352         tmp = ast_create_callid();
1353         if (tmp) {
1354                 ast_callid_threadassoc_add(tmp);
1355                 *callid = tmp;
1356                 return 1;
1357         }
1358
1359         /* If neither worked, then something must have gone wrong. */
1360         return -1;
1361 }
1362
1363 void ast_callid_threadstorage_auto_clean(struct ast_callid *callid, int callid_created)
1364 {
1365         if (callid) {
1366                 /* If the callid was created rather than simply grabbed from the thread storage, we need to unbind here. */
1367                 if (callid_created == 1) {
1368                         ast_callid_threadassoc_remove();
1369                 }
1370                 callid = ast_callid_unref(callid);
1371         }
1372 }
1373
1374 /*!
1375  * \internal
1376  * \brief thread storage cleanup function for unique_callid
1377  */
1378 static void unique_callid_cleanup(void *data)
1379 {
1380         struct ast_callid **callid = data;
1381
1382         if (*callid) {
1383                 ast_callid_unref(*callid);
1384         }
1385
1386         ast_free(data);
1387 }
1388
1389 /*!
1390  * \brief send log messages to syslog and/or the console
1391  */
1392 static void __attribute__((format(printf, 6, 0))) ast_log_full(int level, const char *file, int line, const char *function, struct ast_callid *callid, const char *fmt, va_list ap)
1393 {
1394         struct logmsg *logmsg = NULL;
1395         struct ast_str *buf = NULL;
1396         struct ast_tm tm;
1397         struct timeval now = ast_tvnow();
1398         int res = 0;
1399         char datestring[256];
1400
1401         if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
1402                 return;
1403
1404         if (level != __LOG_VERBOSE && AST_RWLIST_EMPTY(&logchannels)) {
1405                 /*
1406                  * we don't have the logger chain configured yet,
1407                  * so just log to stdout
1408                  */
1409                 int result;
1410                 result = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
1411                 if (result != AST_DYNSTR_BUILD_FAILED) {
1412                         term_filter_escapes(ast_str_buffer(buf));
1413                         fputs(ast_str_buffer(buf), stdout);
1414                 }
1415                 return;
1416         }
1417
1418         /* Ignore anything that never gets logged anywhere */
1419         if (level != __LOG_VERBOSE && !(global_logmask & (1 << level)))
1420                 return;
1421
1422         /* Build string */
1423         res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
1424
1425         /* If the build failed, then abort and free this structure */
1426         if (res == AST_DYNSTR_BUILD_FAILED)
1427                 return;
1428
1429         /* Create a new logging message */
1430         if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128)))
1431                 return;
1432
1433         /* Copy string over */
1434         ast_string_field_set(logmsg, message, ast_str_buffer(buf));
1435
1436         /* Set type */
1437         if (level == __LOG_VERBOSE) {
1438                 logmsg->type = LOGMSG_VERBOSE;
1439         } else {
1440                 logmsg->type = LOGMSG_NORMAL;
1441         }
1442
1443         if (display_callids && callid) {
1444                 logmsg->callid = ast_callid_ref(callid);
1445                 /* callid will be unreffed at logmsg destruction */
1446         }
1447
1448         /* Create our date/time */
1449         ast_localtime(&now, &tm, NULL);
1450         ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
1451         ast_string_field_set(logmsg, date, datestring);
1452
1453         /* Copy over data */
1454         logmsg->level = level;
1455         logmsg->line = line;
1456         ast_string_field_set(logmsg, level_name, levels[level]);
1457         ast_string_field_set(logmsg, file, file);
1458         ast_string_field_set(logmsg, function, function);
1459         logmsg->lwp = ast_get_tid();
1460
1461         /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
1462         if (logthread != AST_PTHREADT_NULL) {
1463                 AST_LIST_LOCK(&logmsgs);
1464                 if (close_logger_thread) {
1465                         /* Logger is either closing or closed.  We cannot log this message. */
1466                         ast_free(logmsg);
1467                 } else {
1468                         AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
1469                         ast_cond_signal(&logcond);
1470                 }
1471                 AST_LIST_UNLOCK(&logmsgs);
1472         } else {
1473                 logger_print_normal(logmsg);
1474                 logmsg_free(logmsg);
1475         }
1476 }
1477
1478 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
1479 {
1480         struct ast_callid *callid;
1481         va_list ap;
1482
1483         callid = ast_read_threadstorage_callid();
1484
1485         va_start(ap, fmt);
1486         ast_log_full(level, file, line, function, callid, fmt, ap);
1487         va_end(ap);
1488
1489         if (callid) {
1490                 ast_callid_unref(callid);
1491         }
1492 }
1493
1494 void ast_log_callid(int level, const char *file, int line, const char *function, struct ast_callid *callid, const char *fmt, ...)
1495 {
1496         va_list ap;
1497         va_start(ap, fmt);
1498         ast_log_full(level, file, line, function, callid, fmt, ap);
1499         va_end(ap);
1500 }
1501
1502 #ifdef HAVE_BKTR
1503
1504 struct ast_bt *ast_bt_create(void)
1505 {
1506         struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
1507         if (!bt) {
1508                 ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
1509                 return NULL;
1510         }
1511
1512         bt->alloced = 1;
1513
1514         ast_bt_get_addresses(bt);
1515
1516         return bt;
1517 }
1518
1519 int ast_bt_get_addresses(struct ast_bt *bt)
1520 {
1521         bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
1522
1523         return 0;
1524 }
1525
1526 void *ast_bt_destroy(struct ast_bt *bt)
1527 {
1528         if (bt->alloced) {
1529                 ast_free(bt);
1530         }
1531
1532         return NULL;
1533 }
1534
1535 char **ast_bt_get_symbols(void **addresses, size_t num_frames)
1536 {
1537         char **strings = NULL;
1538 #if defined(BETTER_BACKTRACES)
1539         int stackfr;
1540         bfd *bfdobj;           /* bfd.h */
1541         Dl_info dli;           /* dlfcn.h */
1542         long allocsize;
1543         asymbol **syms = NULL; /* bfd.h */
1544         bfd_vma offset;        /* bfd.h */
1545         const char *lastslash;
1546         asection *section;
1547         const char *file, *func;
1548         unsigned int line;
1549         char address_str[128];
1550         char msg[1024];
1551         size_t strings_size;
1552         size_t *eachlen;
1553 #endif
1554
1555 #if defined(BETTER_BACKTRACES)
1556         strings_size = num_frames * sizeof(*strings);
1557         eachlen = ast_calloc(num_frames, sizeof(*eachlen));
1558
1559         if (!(strings = ast_calloc(num_frames, sizeof(*strings)))) {
1560                 return NULL;
1561         }
1562
1563         for (stackfr = 0; stackfr < num_frames; stackfr++) {
1564                 int found = 0, symbolcount;
1565
1566                 msg[0] = '\0';
1567
1568                 if (!dladdr(addresses[stackfr], &dli)) {
1569                         continue;
1570                 }
1571
1572                 if (strcmp(dli.dli_fname, "asterisk") == 0) {
1573                         char asteriskpath[256];
1574                         if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
1575                                 /* This will fail to find symbols */
1576                                 ast_debug(1, "Failed to find asterisk binary for debug symbols.\n");
1577                                 dli.dli_fname = "asterisk";
1578                         }
1579                 }
1580
1581                 lastslash = strrchr(dli.dli_fname, '/');
1582                 if (    (bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
1583                                 bfd_check_format(bfdobj, bfd_object) &&
1584                                 (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
1585                                 (syms = ast_malloc(allocsize)) &&
1586                                 (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
1587
1588                         if (bfdobj->flags & DYNAMIC) {
1589                                 offset = addresses[stackfr] - dli.dli_fbase;
1590                         } else {
1591                                 offset = addresses[stackfr] - (void *) 0;
1592                         }
1593
1594                         for (section = bfdobj->sections; section; section = section->next) {
1595                                 if (    !bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
1596                                                 section->vma > offset ||
1597                                                 section->size + section->vma < offset) {
1598                                         continue;
1599                                 }
1600
1601                                 if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
1602                                         continue;
1603                                 }
1604
1605                                 /* file can possibly be null even with a success result from bfd_find_nearest_line */
1606                                 file = file ? file : "";
1607
1608                                 /* Stack trace output */
1609                                 found++;
1610                                 if ((lastslash = strrchr(file, '/'))) {
1611                                         const char *prevslash;
1612                                         for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--);
1613                                         if (prevslash >= file) {
1614                                                 lastslash = prevslash;
1615                                         }
1616                                 }
1617                                 if (dli.dli_saddr == NULL) {
1618                                         address_str[0] = '\0';
1619                                 } else {
1620                                         snprintf(address_str, sizeof(address_str), " (%p+%lX)",
1621                                                 dli.dli_saddr,
1622                                                 (unsigned long) (addresses[stackfr] - dli.dli_saddr));
1623                                 }
1624                                 snprintf(msg, sizeof(msg), "%s:%u %s()%s",
1625                                         lastslash ? lastslash + 1 : file, line,
1626                                         S_OR(func, "???"),
1627                                         address_str);
1628
1629                                 break; /* out of section iteration */
1630                         }
1631                 }
1632                 if (bfdobj) {
1633                         bfd_close(bfdobj);
1634                         if (syms) {
1635                                 ast_free(syms);
1636                         }
1637                 }
1638
1639                 /* Default output, if we cannot find the information within BFD */
1640                 if (!found) {
1641                         if (dli.dli_saddr == NULL) {
1642                                 address_str[0] = '\0';
1643                         } else {
1644                                 snprintf(address_str, sizeof(address_str), " (%p+%lX)",
1645                                         dli.dli_saddr,
1646                                         (unsigned long) (addresses[stackfr] - dli.dli_saddr));
1647                         }
1648                         snprintf(msg, sizeof(msg), "%s %s()%s",
1649                                 lastslash ? lastslash + 1 : dli.dli_fname,
1650                                 S_OR(dli.dli_sname, "<unknown>"),
1651                                 address_str);
1652                 }
1653
1654                 if (!ast_strlen_zero(msg)) {
1655                         char **tmp;
1656                         eachlen[stackfr] = strlen(msg);
1657                         if (!(tmp = ast_realloc(strings, strings_size + eachlen[stackfr] + 1))) {
1658                                 ast_free(strings);
1659                                 strings = NULL;
1660                                 break; /* out of stack frame iteration */
1661                         }
1662                         strings = tmp;
1663                         strings[stackfr] = (char *) strings + strings_size;
1664                         ast_copy_string(strings[stackfr], msg, eachlen[stackfr] + 1);
1665                         strings_size += eachlen[stackfr] + 1;
1666                 }
1667         }
1668
1669         if (strings) {
1670                 /* Recalculate the offset pointers */
1671                 strings[0] = (char *) strings + num_frames * sizeof(*strings);
1672                 for (stackfr = 1; stackfr < num_frames; stackfr++) {
1673                         strings[stackfr] = strings[stackfr - 1] + eachlen[stackfr - 1] + 1;
1674                 }
1675         }
1676 #else /* !defined(BETTER_BACKTRACES) */
1677         strings = backtrace_symbols(addresses, num_frames);
1678 #endif /* defined(BETTER_BACKTRACES) */
1679         return strings;
1680 }
1681
1682 #endif /* HAVE_BKTR */
1683
1684 void ast_backtrace(void)
1685 {
1686 #ifdef HAVE_BKTR
1687         struct ast_bt *bt;
1688         int i = 0;
1689         char **strings;
1690
1691         if (!(bt = ast_bt_create())) {
1692                 ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
1693                 return;
1694         }
1695
1696         if ((strings = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {
1697                 ast_debug(1, "Got %d backtrace record%c\n", bt->num_frames, bt->num_frames != 1 ? 's' : ' ');
1698                 for (i = 3; i < bt->num_frames - 2; i++) {
1699                         ast_debug(1, "#%d: [%p] %s\n", i - 3, bt->addresses[i], strings[i]);
1700                 }
1701
1702                 /* MALLOC_DEBUG will erroneously report an error here, unless we undef the macro. */
1703 #undef free
1704                 free(strings);
1705         } else {
1706                 ast_debug(1, "Could not allocate memory for backtrace\n");
1707         }
1708         ast_bt_destroy(bt);
1709 #else
1710         ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
1711 #endif /* defined(HAVE_BKTR) */
1712 }
1713
1714 void __ast_verbose_ap(const char *file, int line, const char *func, int level, struct ast_callid *callid, const char *fmt, va_list ap)
1715 {
1716         struct ast_str *buf = NULL;
1717         int res = 0;
1718         const char *prefix = level >= 4 ? VERBOSE_PREFIX_4 : level == 3 ? VERBOSE_PREFIX_3 : level == 2 ? VERBOSE_PREFIX_2 : level == 1 ? VERBOSE_PREFIX_1 : "";
1719         signed char magic = level > 127 ? -128 : -level - 1; /* 0 => -1, 1 => -2, etc.  Can't pass NUL, as it is EOS-delimiter */
1720
1721         /* For compatibility with modules still calling ast_verbose() directly instead of using ast_verb() */
1722         if (level < 0) {
1723                 if (!strncmp(fmt, VERBOSE_PREFIX_4, strlen(VERBOSE_PREFIX_4))) {
1724                         magic = -5;
1725                 } else if (!strncmp(fmt, VERBOSE_PREFIX_3, strlen(VERBOSE_PREFIX_3))) {
1726                         magic = -4;
1727                 } else if (!strncmp(fmt, VERBOSE_PREFIX_2, strlen(VERBOSE_PREFIX_2))) {
1728                         magic = -3;
1729                 } else if (!strncmp(fmt, VERBOSE_PREFIX_1, strlen(VERBOSE_PREFIX_1))) {
1730                         magic = -2;
1731                 } else {
1732                         magic = -1;
1733                 }
1734         }
1735
1736         if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE))) {
1737                 return;
1738         }
1739
1740         if (ast_opt_timestamp) {
1741                 struct timeval now;
1742                 struct ast_tm tm;
1743                 char date[40];
1744                 char *datefmt;
1745
1746                 now = ast_tvnow();
1747                 ast_localtime(&now, &tm, NULL);
1748                 ast_strftime(date, sizeof(date), dateformat, &tm);
1749                 datefmt = alloca(strlen(date) + 3 + strlen(prefix) + strlen(fmt) + 1);
1750                 sprintf(datefmt, "%c[%s] %s%s", (char) magic, date, prefix, fmt);
1751                 fmt = datefmt;
1752         } else {
1753                 char *tmp = alloca(strlen(prefix) + strlen(fmt) + 2);
1754                 sprintf(tmp, "%c%s%s", (char) magic, prefix, fmt);
1755                 fmt = tmp;
1756         }
1757
1758         /* Build string */
1759         res = ast_str_set_va(&buf, 0, fmt, ap);
1760
1761         /* If the build failed then we can drop this allocated message */
1762         if (res == AST_DYNSTR_BUILD_FAILED) {
1763                 return;
1764         }
1765
1766         ast_log_callid(__LOG_VERBOSE, file, line, func, callid, "%s", ast_str_buffer(buf));
1767 }
1768
1769 void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...)
1770 {
1771         struct ast_callid *callid;
1772         va_list ap;
1773
1774         callid = ast_read_threadstorage_callid();
1775
1776         va_start(ap, fmt);
1777         __ast_verbose_ap(file, line, func, level, callid, fmt, ap);
1778         va_end(ap);
1779
1780         if (callid) {
1781                 ast_callid_unref(callid);
1782         }
1783 }
1784
1785 void __ast_verbose_callid(const char *file, int line, const char *func, int level, struct ast_callid *callid, const char *fmt, ...)
1786 {
1787         va_list ap;
1788         va_start(ap, fmt);
1789         __ast_verbose_ap(file, line, func, level, callid, fmt, ap);
1790         va_end(ap);
1791 }
1792
1793 /* No new code should use this directly, but we have the ABI for backwards compat */
1794 #undef ast_verbose
1795 void __attribute__((format(printf, 1,2))) ast_verbose(const char *fmt, ...);
1796 void ast_verbose(const char *fmt, ...)
1797 {
1798         struct ast_callid *callid;
1799         va_list ap;
1800
1801         callid = ast_read_threadstorage_callid();
1802
1803         va_start(ap, fmt);
1804         __ast_verbose_ap("", 0, "", 0, callid, fmt, ap);
1805         va_end(ap);
1806
1807         if (callid) {
1808                 ast_callid_unref(callid);
1809         }
1810 }
1811
1812 int ast_register_verbose(void (*v)(const char *string))
1813 {
1814         struct verb *verb;
1815
1816         if (!(verb = ast_malloc(sizeof(*verb))))
1817                 return -1;
1818
1819         verb->verboser = v;
1820
1821         AST_RWLIST_WRLOCK(&verbosers);
1822         AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
1823         AST_RWLIST_UNLOCK(&verbosers);
1824
1825         return 0;
1826 }
1827
1828 int ast_unregister_verbose(void (*v)(const char *string))
1829 {
1830         struct verb *cur;
1831
1832         AST_RWLIST_WRLOCK(&verbosers);
1833         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
1834                 if (cur->verboser == v) {
1835                         AST_RWLIST_REMOVE_CURRENT(list);
1836                         ast_free(cur);
1837                         break;
1838                 }
1839         }
1840         AST_RWLIST_TRAVERSE_SAFE_END;
1841         AST_RWLIST_UNLOCK(&verbosers);
1842
1843         return cur ? 0 : -1;
1844 }
1845
1846 static void update_logchannels(void)
1847 {
1848         struct logchannel *cur;
1849
1850         AST_RWLIST_WRLOCK(&logchannels);
1851
1852         global_logmask = 0;
1853
1854         AST_RWLIST_TRAVERSE(&logchannels, cur, list) {
1855                 cur->logmask = make_components(cur->components, cur->lineno, &cur->verbosity);
1856                 global_logmask |= cur->logmask;
1857         }
1858
1859         AST_RWLIST_UNLOCK(&logchannels);
1860 }
1861
1862 int ast_logger_register_level(const char *name)
1863 {
1864         unsigned int level;
1865         unsigned int available = 0;
1866
1867         AST_RWLIST_WRLOCK(&logchannels);
1868
1869         for (level = 0; level < ARRAY_LEN(levels); level++) {
1870                 if ((level >= 16) && !available && !levels[level]) {
1871                         available = level;
1872                         continue;
1873                 }
1874
1875                 if (levels[level] && !strcasecmp(levels[level], name)) {
1876                         ast_log(LOG_WARNING,
1877                                 "Unable to register dynamic logger level '%s': a standard logger level uses that name.\n",
1878                                 name);
1879                         AST_RWLIST_UNLOCK(&logchannels);
1880
1881                         return -1;
1882                 }
1883         }
1884
1885         if (!available) {
1886                 ast_log(LOG_WARNING,
1887                         "Unable to register dynamic logger level '%s'; maximum number of levels registered.\n",
1888                         name);
1889                 AST_RWLIST_UNLOCK(&logchannels);
1890
1891                 return -1;
1892         }
1893
1894         levels[available] = ast_strdup(name);
1895
1896         AST_RWLIST_UNLOCK(&logchannels);
1897
1898         ast_debug(1, "Registered dynamic logger level '%s' with index %d.\n", name, available);
1899
1900         update_logchannels();
1901
1902         return available;
1903 }
1904
1905 void ast_logger_unregister_level(const char *name)
1906 {
1907         unsigned int found = 0;
1908         unsigned int x;
1909
1910         AST_RWLIST_WRLOCK(&logchannels);
1911
1912         for (x = 16; x < ARRAY_LEN(levels); x++) {
1913                 if (!levels[x]) {
1914                         continue;
1915                 }
1916
1917                 if (strcasecmp(levels[x], name)) {
1918                         continue;
1919                 }
1920
1921                 found = 1;
1922                 break;
1923         }
1924
1925         if (found) {
1926                 /* take this level out of the global_logmask, to ensure that no new log messages
1927                  * will be queued for it
1928                  */
1929
1930                 global_logmask &= ~(1 << x);
1931
1932                 ast_free(levels[x]);
1933                 levels[x] = NULL;
1934                 AST_RWLIST_UNLOCK(&logchannels);
1935
1936                 ast_debug(1, "Unregistered dynamic logger level '%s' with index %d.\n", name, x);
1937
1938                 update_logchannels();
1939         } else {
1940                 AST_RWLIST_UNLOCK(&logchannels);
1941         }
1942 }
1943