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