Skip to content
Snippets Groups Projects
extconf.c 170 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
    /*  
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 2006, Digium, Inc.
     *
     * Steve Murphy <murf@digium.com>
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
    /*
     *
     * A condensation of the pbx_config stuff, to read into exensions.conf, and provide an interface to the data there,
     * for operations outside of asterisk. A huge, awful hack.
     *
     */
    
    #include "asterisk/autoconfig.h"
    
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <time.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <sys/resource.h>
    #include <sys/wait.h>
    #include <stdarg.h>
    #include <string.h>
    #include <locale.h>
    #include <ctype.h>
    #if !defined(SOLARIS) && !defined(__CYGWIN__)
    #include <err.h>
    #endif
    #include <regex.h>
    #include <limits.h>
    #include <pthread.h>
    #include <netdb.h>
    #include <sys/param.h>
    #define ASINCLUDE_GLOB 1
    #ifdef AST_INCLUDE_GLOB
    #if defined(__Darwin__) || defined(__CYGWIN__)
    #define GLOB_ABORTED GLOB_ABEND
    #endif
    # include <glob.h>
    #endif
    
    static char ast_config_AST_CONFIG_DIR[PATH_MAX] = {"/etc/asterisk"};
    #define AST_API_MODULE  1 /* gimme the inline defs! */
    struct ast_channel 
    {
    	char x; /* basically empty! */
    };
    
    
    
    #include "asterisk/inline_api.h"
    #include "asterisk/compat.h"
    #include "asterisk/compiler.h"
    #include "asterisk/endian.h"
    #include "asterisk/ast_expr.h"
    #include "asterisk/ael_structs.h"
    #include "asterisk/pval.h"
    
    /* logger.h */
    #define EVENTLOG "event_log"
    #define	QUEUELOG	"queue_log"
    
    #define DEBUG_M(a) { \
    	a; \
    }
    
    #define VERBOSE_PREFIX_1 " "
    #define VERBOSE_PREFIX_2 "  == "
    #define VERBOSE_PREFIX_3 "    -- "
    #define VERBOSE_PREFIX_4 "       > "
    
    /* IN CONFLICT: void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
       __attribute__ ((format (printf, 5, 6))); */
    
    static void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
    
    
    void ast_backtrace(void);
    
    void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
    	__attribute__ ((format (printf, 5, 6)));
    
    /* IN CONFLICT: void ast_verbose(const char *fmt, ...)
       __attribute__ ((format (printf, 1, 2))); */
    
    int ast_register_verbose(void (*verboser)(const char *string));
    int ast_unregister_verbose(void (*verboser)(const char *string));
    
    void ast_console_puts(const char *string);
    
    void ast_console_puts_mutable(const char *string);
    void ast_console_toggle_mute(int fd);
    
    #define _A_ __FILE__, __LINE__, __PRETTY_FUNCTION__
    
    #ifdef LOG_DEBUG
    #undef LOG_DEBUG
    #endif
    #define __LOG_DEBUG    0
    #define LOG_DEBUG      __LOG_DEBUG, _A_
    
    #ifdef LOG_EVENT
    #undef LOG_EVENT
    #endif
    #define __LOG_EVENT    1
    #define LOG_EVENT      __LOG_EVENT, _A_
    
    #ifdef LOG_NOTICE
    #undef LOG_NOTICE
    #endif
    #define __LOG_NOTICE   2
    #define LOG_NOTICE     __LOG_NOTICE, _A_
    
    #ifdef LOG_WARNING
    #undef LOG_WARNING
    #endif
    #define __LOG_WARNING  3
    #define LOG_WARNING    __LOG_WARNING, _A_
    
    #ifdef LOG_ERROR
    #undef LOG_ERROR
    #endif
    #define __LOG_ERROR    4
    #define LOG_ERROR      __LOG_ERROR, _A_
    
    #ifdef LOG_VERBOSE
    #undef LOG_VERBOSE
    #endif
    #define __LOG_VERBOSE  5
    #define LOG_VERBOSE    __LOG_VERBOSE, _A_
    
    #ifdef LOG_DTMF
    #undef LOG_DTMF
    #endif
    #define __LOG_DTMF  6
    #define LOG_DTMF    __LOG_DTMF, _A_
    
    /* from utils.h */
    
    static unsigned int __unsigned_int_flags_dummy;
    
    struct ast_flags {  /* stolen from utils.h */
    	unsigned int flags;
    };
    #define ast_test_flag(p,flag) 		({ \
    					typeof ((p)->flags) __p = (p)->flags; \
    					typeof (__unsigned_int_flags_dummy) __x = 0; \
    					(void) (&__p == &__x); \
    					((p)->flags & (flag)); \
    					})
    
    #define ast_set2_flag(p,value,flag)	do { \
    					typeof ((p)->flags) __p = (p)->flags; \
    					typeof (__unsigned_int_flags_dummy) __x = 0; \
    					(void) (&__p == &__x); \
    					if (value) \
    						(p)->flags |= (flag); \
    					else \
    						(p)->flags &= ~(flag); \
    					} while (0)
    
    
    #ifdef __AST_DEBUG_MALLOC
    static void ast_free(void *ptr) attribute_unused;
    static void ast_free(void *ptr)
    {
    	free(ptr);
    }
    #else
    #define ast_free free
    #endif
    
    #ifndef __AST_DEBUG_MALLOC
    
    #define MALLOC_FAILURE_MSG \
    	ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file);
    /*!
     * \brief A wrapper for malloc()
     *
     * ast_malloc() is a wrapper for malloc() that will generate an Asterisk log
     * message in the case that the allocation fails.
     *
     * The argument and return value are the same as malloc()
     */
    #define ast_malloc(len) \
    	_ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
    
    AST_INLINE_API(
    void * attribute_malloc _ast_malloc(size_t len, const char *file, int lineno, const char *func),
    {
    	void *p;
    
    	if (!(p = malloc(len)))
    		MALLOC_FAILURE_MSG;
    
    	return p;
    }
    )
    
    /*!
     * \brief A wrapper for calloc()
     *
     * ast_calloc() is a wrapper for calloc() that will generate an Asterisk log
     * message in the case that the allocation fails.
     *
     * The arguments and return value are the same as calloc()
     */
    #define ast_calloc(num, len) \
    	_ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
    
    AST_INLINE_API(
    void * attribute_malloc _ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func),
    {
    	void *p;
    
    	if (!(p = calloc(num, len)))
    		MALLOC_FAILURE_MSG;
    
    	return p;
    }
    )
    
    /*!
     * \brief A wrapper for calloc() for use in cache pools
     *
     * ast_calloc_cache() is a wrapper for calloc() that will generate an Asterisk log
     * message in the case that the allocation fails. When memory debugging is in use,
     * the memory allocated by this function will be marked as 'cache' so it can be
     * distinguished from normal memory allocations.
     *
     * The arguments and return value are the same as calloc()
     */
    #define ast_calloc_cache(num, len) \
    	_ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
    
    /*!
     * \brief A wrapper for realloc()
     *
     * ast_realloc() is a wrapper for realloc() that will generate an Asterisk log
     * message in the case that the allocation fails.
     *
     * The arguments and return value are the same as realloc()
     */
    #define ast_realloc(p, len) \
    	_ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
    
    AST_INLINE_API(
    void * attribute_malloc _ast_realloc(void *p, size_t len, const char *file, int lineno, const char *func),
    {
    	void *newp;
    
    	if (!(newp = realloc(p, len)))
    		MALLOC_FAILURE_MSG;
    
    	return newp;
    }
    )
    
    /*!
     * \brief A wrapper for strdup()
     *
     * ast_strdup() is a wrapper for strdup() that will generate an Asterisk log
     * message in the case that the allocation fails.
     *
     * ast_strdup(), unlike strdup(), can safely accept a NULL argument. If a NULL
     * argument is provided, ast_strdup will return NULL without generating any
     * kind of error log message.
     *
     * The argument and return value are the same as strdup()
     */
    #define ast_strdup(str) \
    	_ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)
    
    AST_INLINE_API(
    char * attribute_malloc _ast_strdup(const char *str, const char *file, int lineno, const char *func),
    {
    	char *newstr = NULL;
    
    	if (str) {
    		if (!(newstr = strdup(str)))
    			MALLOC_FAILURE_MSG;
    	}
    
    	return newstr;
    }
    )
    
    /*!
     * \brief A wrapper for strndup()
     *
     * ast_strndup() is a wrapper for strndup() that will generate an Asterisk log
     * message in the case that the allocation fails.
     *
     * ast_strndup(), unlike strndup(), can safely accept a NULL argument for the
     * string to duplicate. If a NULL argument is provided, ast_strdup will return  
     * NULL without generating any kind of error log message.
     *
     * The arguments and return value are the same as strndup()
     */
    #define ast_strndup(str, len) \
    	_ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
    
    AST_INLINE_API(
    char * attribute_malloc _ast_strndup(const char *str, size_t len, const char *file, int lineno, const char *func),
    {
    	char *newstr = NULL;
    
    	if (str) {
    		if (!(newstr = strndup(str, len)))
    			MALLOC_FAILURE_MSG;
    	}
    
    	return newstr;
    }
    )
    
    /*!
     * \brief A wrapper for asprintf()
     *
     * ast_asprintf() is a wrapper for asprintf() that will generate an Asterisk log
     * message in the case that the allocation fails.
     *
     * The arguments and return value are the same as asprintf()
     */
    #define ast_asprintf(ret, fmt, ...) \
    	_ast_asprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, __VA_ARGS__)
    
    AST_INLINE_API(
    int _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, ...),
    {
    	int res;
    	va_list ap;
    
    	va_start(ap, fmt);
    	if ((res = vasprintf(ret, fmt, ap)) == -1)
    		MALLOC_FAILURE_MSG;
    	va_end(ap);
    
    	return res;
    }
    )
    
    /*!
     * \brief A wrapper for vasprintf()
     *
     * ast_vasprintf() is a wrapper for vasprintf() that will generate an Asterisk log
     * message in the case that the allocation fails.
     *
     * The arguments and return value are the same as vasprintf()
     */
    #define ast_vasprintf(ret, fmt, ap) \
    	_ast_vasprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, (fmt), (ap))
    
    AST_INLINE_API(
    int _ast_vasprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, va_list ap),
    {
    	int res;
    
    	if ((res = vasprintf(ret, fmt, ap)) == -1)
    		MALLOC_FAILURE_MSG;
    
    	return res;
    }
    )
    
    #else
    
    /* If astmm is in use, let it handle these.  Otherwise, it will report that
       all allocations are coming from this header file */
    
    #define ast_malloc(a)		malloc(a)
    #define ast_calloc(a,b)		calloc(a,b)
    #define ast_realloc(a,b)	realloc(a,b)
    #define ast_strdup(a)		strdup(a)
    #define ast_strndup(a,b)	strndup(a,b)
    #define ast_asprintf(a,b,c)	asprintf(a,b,c)
    #define ast_vasprintf(a,b,c)	vasprintf(a,b,c)
    
    #endif /* AST_DEBUG_MALLOC */
    
    #if !defined(ast_strdupa) && defined(__GNUC__)
    /*!
      \brief duplicate a string in memory from the stack
      \param s The string to duplicate
    
      This macro will duplicate the given string.  It returns a pointer to the stack
      allocatted memory for the new string.
    */
    #define ast_strdupa(s)                                                    \
    	(__extension__                                                    \
    	({                                                                \
    		const char *__old = (s);                                  \
    		size_t __len = strlen(__old) + 1;                         \
    		char *__new = __builtin_alloca(__len);                    \
    		memcpy (__new, __old, __len);                             \
    		__new;                                                    \
    	}))
    #endif
    
    
    /* from config.c */
    
    #define MAX_NESTED_COMMENTS 128
    #define COMMENT_START ";--"
    #define COMMENT_END "--;"
    #define COMMENT_META ';'
    #define COMMENT_TAG '-'
    
    static char *extconfig_conf = "extconfig.conf";
    
    /*! Growable string buffer */
    static char *comment_buffer;   /*!< this will be a comment collector.*/
    static int   comment_buffer_size;  /*!< the amount of storage so far alloc'd for the comment_buffer */
    
    static char *lline_buffer;    /*!< A buffer for stuff behind the ; */
    static int  lline_buffer_size;
    
    #define CB_INCR 250
    
    struct ast_comment {
    	struct ast_comment *next;
    	char cmt[0];
    };
    
    static void CB_INIT(void)
    {
    	if (!comment_buffer) {
    		comment_buffer = ast_malloc(CB_INCR);
    		if (!comment_buffer)
    			return;
    		comment_buffer[0] = 0;
    		comment_buffer_size = CB_INCR;
    		lline_buffer = ast_malloc(CB_INCR);
    		if (!lline_buffer)
    			return;
    		lline_buffer[0] = 0;
    		lline_buffer_size = CB_INCR;
    	} else {
    		comment_buffer[0] = 0;
    		lline_buffer[0] = 0;
    	}
    }
    
    static void  CB_ADD(char *str)
    {
    	int rem = comment_buffer_size - strlen(comment_buffer) - 1;
    	int siz = strlen(str);
    	if (rem < siz+1) {
    		comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + siz + 1);
    		if (!comment_buffer)
    			return;
    		comment_buffer_size += CB_INCR+siz+1;
    	}
    	strcat(comment_buffer,str);
    }
    
    static void  CB_ADD_LEN(char *str, int len)
    {
    	int cbl = strlen(comment_buffer) + 1;
    	int rem = comment_buffer_size - cbl;
    	if (rem < len+1) {
    		comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + len + 1);
    		if (!comment_buffer)
    			return;
    		comment_buffer_size += CB_INCR+len+1;
    	}
    	strncat(comment_buffer,str,len);
    	comment_buffer[cbl+len-1] = 0;
    }
    
    static void  LLB_ADD(char *str)
    {
    	int rem = lline_buffer_size - strlen(lline_buffer) - 1;
    	int siz = strlen(str);
    	if (rem < siz+1) {
    		lline_buffer = ast_realloc(lline_buffer, lline_buffer_size + CB_INCR + siz + 1);
    		if (!lline_buffer) 
    			return;
    		lline_buffer_size += CB_INCR + siz + 1;
    	}
    	strcat(lline_buffer,str);
    }
    
    static void CB_RESET(void )  
    { 
    	comment_buffer[0] = 0; 
    	lline_buffer[0] = 0;
    }
    		
    /*! \brief Keep track of how many threads are currently trying to wait*() on
     *  a child process */
    static unsigned int safe_system_level = 0;
    static void *safe_system_prev_handler;
    
    /*! \brief NULL handler so we can collect the child exit status */
    static void null_sig_handler(int signal)
    {
    
    }
    
    void ast_replace_sigchld(void);
    
    void ast_replace_sigchld(void)
    {
    	unsigned int level;
    
    	level = safe_system_level++;
    
    	/* only replace the handler if it has not already been done */
    	if (level == 0)
    		safe_system_prev_handler = signal(SIGCHLD, null_sig_handler);
    
    }
    
    void ast_unreplace_sigchld(void);
    
    void ast_unreplace_sigchld(void)
    {
    	unsigned int level;
    
    	level = --safe_system_level;
    
    	/* only restore the handler if we are the last one */
    	if (level == 0)
    		signal(SIGCHLD, safe_system_prev_handler);
    
    }
    
    int ast_safe_system(const char *s);
    
    int ast_safe_system(const char *s)
    {
    	pid_t pid;
    #ifdef HAVE_WORKING_FORK
    	int x;
    #endif
    	int res;
    	struct rusage rusage;
    	int status;
    
    #if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
    	ast_replace_sigchld();
    
    #ifdef HAVE_WORKING_FORK
    	pid = fork();
    #else
    	pid = vfork();
    #endif	
    
    	if (pid == 0) {
    #ifdef HAVE_WORKING_FORK
    		/* Close file descriptors and launch system command */
    		for (x = STDERR_FILENO + 1; x < 4096; x++)
    			close(x);
    #endif
    		execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
    		_exit(1);
    	} else if (pid > 0) {
    		for(;;) {
    			res = wait4(pid, &status, 0, &rusage);
    			if (res > -1) {
    				res = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
    				break;
    			} else if (errno != EINTR) 
    				break;
    		}
    	} else {
    		ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
    		res = -1;
    	}
    
    	ast_unreplace_sigchld();
    #else
    	res = -1;
    #endif
    
    	return res;
    }
    
    static struct ast_comment *ALLOC_COMMENT(const char *buffer)
    { 
    	struct ast_comment *x = ast_calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
    	strcpy(x->cmt, buffer);
    	return x;
    }
    
    static struct ast_config_map {
    	struct ast_config_map *next;
    	char *name;
    	char *driver;
    	char *database;
    	char *table;
    	char stuff[0];
    } *config_maps = NULL;
    
    static struct ast_config_engine *config_engine_list;
    
    #define MAX_INCLUDE_LEVEL 10
    
    
    struct ast_category {
    	char name[80];
    	int ignored;			/*!< do not let user of the config see this category */
    	int include_level;	
    	struct ast_comment *precomments;
    	struct ast_comment *sameline;
    	struct ast_variable *root;
    	struct ast_variable *last;
    	struct ast_category *next;
    };
    
    struct ast_config {
    	struct ast_category *root;
    	struct ast_category *last;
    	struct ast_category *current;
    	struct ast_category *last_browse;		/*!< used to cache the last category supplied via category_browse */
    	int include_level;
    	int max_include_level;
    };
    
    typedef struct ast_config *config_load_func(const char *database, const char *table, const char *configfile, struct ast_config *config, int withcomments);
    typedef struct ast_variable *realtime_var_get(const char *database, const char *table, va_list ap);
    typedef struct ast_config *realtime_multi_get(const char *database, const char *table, va_list ap);
    typedef int realtime_update(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap);
    
    /*! \brief Configuration engine structure, used to define realtime drivers */
    struct ast_config_engine {
    	char *name;
    	config_load_func *load_func;
    	realtime_var_get *realtime_func;
    	realtime_multi_get *realtime_multi_func;
    	realtime_update *update_func;
    	struct ast_config_engine *next;
    };
    
    static struct ast_config_engine *config_engine_list;
    
    /* from config.h */
    
    struct ast_variable {
    	char *name;
    	char *value;
    	int lineno;
    	int object;		/*!< 0 for variable, 1 for object */
    	int blanklines; 	/*!< Number of blanklines following entry */
    	struct ast_comment *precomments;
    	struct ast_comment *sameline;
    	struct ast_variable *next;
    	char stuff[0];
    };
    
    static const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable);
    static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments);
    
    struct ast_config *localized_config_load_with_comments(const char *filename);
    static char *ast_category_browse(struct ast_config *config, const char *prev);
    static struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category);
    static void ast_variables_destroy(struct ast_variable *v);
    static void ast_config_destroy(struct ast_config *cfg);
    
    static struct ast_variable *ast_variable_new(const char *name, const char *value);
    
    static struct ast_variable *ast_variable_new(const char *name, const char *value) 
    {
    	struct ast_variable *variable;
    	int name_len = strlen(name) + 1;	
    
    	if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
    		variable->name = variable->stuff;
    		variable->value = variable->stuff + name_len;		
    		strcpy(variable->name,name);
    		strcpy(variable->value,value);
    	}
    
    	return variable;
    }
    
    static void ast_variable_append(struct ast_category *category, struct ast_variable *variable);
    
    static void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
    {
    	if (!variable)
    		return;
    	if (category->last)
    		category->last->next = variable;
    	else
    		category->root = variable;
    	category->last = variable;
    	while (category->last->next)
    		category->last = category->last->next;
    }
    
    static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored);
    
    static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
    {
    	struct ast_category *cat;
    
    	/* try exact match first, then case-insensitive match */
    	for (cat = config->root; cat; cat = cat->next) {
    		if (cat->name == category_name && (ignored || !cat->ignored))
    			return cat;
    	}
    
    	for (cat = config->root; cat; cat = cat->next) {
    		if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
    			return cat;
    	}
    
    	return NULL;
    }
    
    static struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
    {
    	return category_get(config, category_name, 0);
    }
    
    static struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
    {
    	struct ast_category *cat = NULL;
    
    	if (category && config->last_browse && (config->last_browse->name == category))
    		cat = config->last_browse;
    	else
    		cat = ast_category_get(config, category);
    
    	return (cat) ? cat->root : NULL;
    }
    
    static const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
    {
    	struct ast_variable *v;
    
    	if (category) {
    		for (v = ast_variable_browse(config, category); v; v = v->next) {
    			if (!strcasecmp(variable, v->name))
    				return v->value;
    		}
    	} else {
    		struct ast_category *cat;
    
    		for (cat = config->root; cat; cat = cat->next)
    			for (v = cat->root; v; v = v->next)
    				if (!strcasecmp(variable, v->name))
    					return v->value;
    	}
    
    	return NULL;
    }
    
    static struct ast_variable *variable_clone(const struct ast_variable *old)
    {
    	struct ast_variable *new = ast_variable_new(old->name, old->value);
    
    	if (new) {
    		new->lineno = old->lineno;
    		new->object = old->object;
    		new->blanklines = old->blanklines;
    		/* TODO: clone comments? */
    	}
    
    	return new;
    }
     
    static void ast_variables_destroy(struct ast_variable *v)
    {
    	struct ast_variable *vn;
    
    	while (v) {
    		vn = v;
    		v = v->next;
    		free(vn);
    	}
    }
    
    static void ast_config_destroy(struct ast_config *cfg)
    {
    	struct ast_category *cat, *catn;
    
    	if (!cfg)
    		return;
    
    	cat = cfg->root;
    	while (cat) {
    		ast_variables_destroy(cat->root);
    		catn = cat;
    		cat = cat->next;
    		free(catn);
    	}
    	free(cfg);
    }
    
    
    /* options.h declars ast_options extern; I need it static? */
    
    #define AST_CACHE_DIR_LEN 	512
    #define AST_FILENAME_MAX	80
    
    /*! \ingroup main_options */
    enum ast_option_flags {
    	/*! Allow \#exec in config files */
    	AST_OPT_FLAG_EXEC_INCLUDES = (1 << 0),
    	/*! Do not fork() */
    	AST_OPT_FLAG_NO_FORK = (1 << 1),
    	/*! Keep quiet */
    	AST_OPT_FLAG_QUIET = (1 << 2),
    	/*! Console mode */
    	AST_OPT_FLAG_CONSOLE = (1 << 3),
    	/*! Run in realtime Linux priority */
    	AST_OPT_FLAG_HIGH_PRIORITY = (1 << 4),
    	/*! Initialize keys for RSA authentication */
    	AST_OPT_FLAG_INIT_KEYS = (1 << 5),
    	/*! Remote console */
    	AST_OPT_FLAG_REMOTE = (1 << 6),
    	/*! Execute an asterisk CLI command upon startup */
    	AST_OPT_FLAG_EXEC = (1 << 7),
    	/*! Don't use termcap colors */
    	AST_OPT_FLAG_NO_COLOR = (1 << 8),
    	/*! Are we fully started yet? */
    	AST_OPT_FLAG_FULLY_BOOTED = (1 << 9),
    	/*! Trascode via signed linear */
    	AST_OPT_FLAG_TRANSCODE_VIA_SLIN = (1 << 10),
    	/*! Enable priority jumping in applications */
    	AST_OPT_FLAG_PRIORITY_JUMPING = (1 << 11),
    	/*! Dump core on a seg fault */
    	AST_OPT_FLAG_DUMP_CORE = (1 << 12),
    	/*! Cache sound files */
    	AST_OPT_FLAG_CACHE_RECORD_FILES = (1 << 13),
    	/*! Display timestamp in CLI verbose output */
    	AST_OPT_FLAG_TIMESTAMP = (1 << 14),
    	/*! Override config */
    	AST_OPT_FLAG_OVERRIDE_CONFIG = (1 << 15),
    	/*! Reconnect */
    	AST_OPT_FLAG_RECONNECT = (1 << 16),
    	/*! Transmit Silence during Record() */
    	AST_OPT_FLAG_TRANSMIT_SILENCE = (1 << 17),
    	/*! Suppress some warnings */
    	AST_OPT_FLAG_DONT_WARN = (1 << 18),
    	/*! End CDRs before the 'h' extension */
    	AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN = (1 << 19),
    	/*! Use Zaptel Timing for generators if available */
    	AST_OPT_FLAG_INTERNAL_TIMING = (1 << 20),
    	/*! Always fork, even if verbose or debug settings are non-zero */
    	AST_OPT_FLAG_ALWAYS_FORK = (1 << 21),
    	/*! Disable log/verbose output to remote consoles */
    	AST_OPT_FLAG_MUTE = (1 << 22)
    };
    
    /*! These are the options that set by default when Asterisk starts */
    #define AST_DEFAULT_OPTIONS AST_OPT_FLAG_TRANSCODE_VIA_SLIN
    
    #define ast_opt_exec_includes		ast_test_flag(&ast_options, AST_OPT_FLAG_EXEC_INCLUDES)
    #define ast_opt_no_fork			ast_test_flag(&ast_options, AST_OPT_FLAG_NO_FORK)
    #define ast_opt_quiet			ast_test_flag(&ast_options, AST_OPT_FLAG_QUIET)
    #define ast_opt_console			ast_test_flag(&ast_options, AST_OPT_FLAG_CONSOLE)
    #define ast_opt_high_priority		ast_test_flag(&ast_options, AST_OPT_FLAG_HIGH_PRIORITY)
    #define ast_opt_init_keys		ast_test_flag(&ast_options, AST_OPT_FLAG_INIT_KEYS)
    #define ast_opt_remote			ast_test_flag(&ast_options, AST_OPT_FLAG_REMOTE)
    #define ast_opt_exec			ast_test_flag(&ast_options, AST_OPT_FLAG_EXEC)
    #define ast_opt_no_color		ast_test_flag(&ast_options, AST_OPT_FLAG_NO_COLOR)
    #define ast_fully_booted		ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)
    #define ast_opt_transcode_via_slin	ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSCODE_VIA_SLIN)
    #define ast_opt_priority_jumping	ast_test_flag(&ast_options, AST_OPT_FLAG_PRIORITY_JUMPING)
    #define ast_opt_dump_core		ast_test_flag(&ast_options, AST_OPT_FLAG_DUMP_CORE)
    #define ast_opt_cache_record_files	ast_test_flag(&ast_options, AST_OPT_FLAG_CACHE_RECORD_FILES)
    #define ast_opt_timestamp		ast_test_flag(&ast_options, AST_OPT_FLAG_TIMESTAMP)
    #define ast_opt_override_config		ast_test_flag(&ast_options, AST_OPT_FLAG_OVERRIDE_CONFIG)
    #define ast_opt_reconnect		ast_test_flag(&ast_options, AST_OPT_FLAG_RECONNECT)
    #define ast_opt_transmit_silence	ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSMIT_SILENCE)
    #define ast_opt_dont_warn		ast_test_flag(&ast_options, AST_OPT_FLAG_DONT_WARN)
    #define ast_opt_end_cdr_before_h_exten	ast_test_flag(&ast_options, AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN)
    #define ast_opt_internal_timing		ast_test_flag(&ast_options, AST_OPT_FLAG_INTERNAL_TIMING)
    #define ast_opt_always_fork		ast_test_flag(&ast_options, AST_OPT_FLAG_ALWAYS_FORK)
    #define ast_opt_mute			ast_test_flag(&ast_options, AST_OPT_FLAG_MUTE)
    
    /*  IN CONFLICT: extern int option_verbose; */
    /*  IN CONFLICT: extern int option_debug;	*/	/*!< Debugging */
    extern int option_maxcalls;		/*!< Maximum number of simultaneous channels */
    extern double option_maxload;
    extern char defaultlanguage[];
    
    extern time_t ast_startuptime;
    extern time_t ast_lastreloadtime;
    extern pid_t ast_mainpid;
    
    extern char record_cache_dir[AST_CACHE_DIR_LEN];
    extern char debug_filename[AST_FILENAME_MAX];
    
    extern int ast_language_is_prefix;
    
    
    
    /* lock.h */
    
    #ifndef	HAVE_MTX_PROFILE
    #define	__MTX_PROF(a)	return pthread_mutex_lock((a))
    #else
    #define	__MTX_PROF(a)	do {			\
    	int i;					\
    	/* profile only non-blocking events */	\
    	ast_mark(mtx_prof, 1);			\
    	i = pthread_mutex_trylock((a));		\
    	ast_mark(mtx_prof, 0);			\
    	if (!i)					\
    		return i;			\
    	else					\
    		return pthread_mutex_lock((a)); \
    	} while (0)
    #endif	/* HAVE_MTX_PROFILE */
    
    #define AST_PTHREADT_NULL (pthread_t) -1
    #define AST_PTHREADT_STOP (pthread_t) -2
    
    #if defined(SOLARIS) || defined(BSD)
    #define AST_MUTEX_INIT_W_CONSTRUCTORS
    #endif /* SOLARIS || BSD */
    
    /* Asterisk REQUIRES recursive (not error checking) mutexes
       and will not run without them. */
    #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
    #define PTHREAD_MUTEX_INIT_VALUE	PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
    #define AST_MUTEX_KIND			PTHREAD_MUTEX_RECURSIVE_NP
    #else
    #define PTHREAD_MUTEX_INIT_VALUE	PTHREAD_MUTEX_INITIALIZER
    #define AST_MUTEX_KIND			PTHREAD_MUTEX_RECURSIVE
    #endif /* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
    
    #ifdef DEBUG_THREADS
    
    #define __ast_mutex_logger(...)  do { if (canlog) ast_log(LOG_ERROR, __VA_ARGS__); else fprintf(stderr, __VA_ARGS__); } while (0)
    
    #ifdef THREAD_CRASH
    #define DO_THREAD_CRASH do { *((int *)(0)) = 1; } while(0)
    #else
    #define DO_THREAD_CRASH do { } while (0)
    #endif
    
    #define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, { NULL }, { 0 }, 0, { NULL }, { 0 } }
    
    #define AST_MAX_REENTRANCY 10
    
    struct ast_mutex_info {
    	pthread_mutex_t mutex;
    
    	/*! Track which thread holds this lock */
    	unsigned int track:1;
    
    	const char *file[AST_MAX_REENTRANCY];
    	int lineno[AST_MAX_REENTRANCY];
    	int reentrancy;
    	const char *func[AST_MAX_REENTRANCY];
    	pthread_t thread[AST_MAX_REENTRANCY];
    };
    
    typedef struct ast_mutex_info ast_mutex_t;
    
    typedef pthread_cond_t ast_cond_t;
    
    static pthread_mutex_t empty_mutex;
    
    static void __attribute__((constructor)) init_empty_mutex(void)
    {
    	memset(&empty_mutex, 0, sizeof(empty_mutex));
    }
    
    static inline int __ast_pthread_mutex_init_attr(const char *filename, int lineno, const char *func,
    						const char *mutex_name, ast_mutex_t *t,
    						pthread_mutexattr_t *attr) 
    {
    #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
    	int canlog = strcmp(filename, "logger.c");
    
    	if ((t->mutex) != ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
    		if ((t->mutex) != (empty_mutex)) {
    			__ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is already initialized.\n",
    					   filename, lineno, func, mutex_name);
    			__ast_mutex_logger("%s line %d (%s): Error: previously initialization of mutex '%s'.\n",
    					   t->file[0], t->lineno[0], t->func[0], mutex_name);
    			DO_THREAD_CRASH;
    			return 0;
    		}
    	}
    #endif