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