Thu Aug 30 20:42:59 2012

Asterisk developer's documentation


asterisk.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2012, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 
00020 /* Doxygenified Copyright Header */
00021 /*!
00022  * \mainpage Asterisk -- The Open Source Telephony Project
00023  *
00024  * \par Developer Documentation for Asterisk
00025  *
00026  * This is the main developer documentation for Asterisk. It is 
00027  * generated by running "make progdocs" from the Asterisk source tree.  
00028  *
00029  * In addition to the information available on the Asterisk source code, 
00030  * please see the appendices for information on coding guidelines, 
00031  * release management, commit policies, and more.
00032  *
00033  * \arg \ref AsteriskArchitecture
00034  *
00035  * \par Additional documentation
00036  * \arg \ref Licensing
00037  * \arg \ref DevDoc 
00038  * \arg \ref ConfigFiles
00039  *
00040  * \section copyright Copyright and Author
00041  *
00042  * Copyright (C) 1999 - 2012, Digium, Inc.
00043  * Asterisk is a <a href="http://www.digium.com/en/company/view-policy.php?id=Trademark-Policy">registered trademark</a>
00044  * of <a href="http://www.digium.com">Digium, Inc</a>.
00045  *
00046  * \author Mark Spencer <markster@digium.com>
00047  * Also see \ref AstCREDITS
00048  *
00049  * See http://www.asterisk.org for more information about
00050  * the Asterisk project. Please do not directly contact
00051  * any of the maintainers of this project for assistance;
00052  * the project provides a web site, mailing lists, and IRC
00053  * channels for your use.
00054  */
00055 
00056 /*! \file
00057   \brief Top level source file for Asterisk  - the Open Source PBX. Implementation
00058   of PBX core functions and CLI interface.
00059   
00060  */
00061 
00062 /*** MODULEINFO
00063    <support_level>core</support_level>
00064  ***/
00065 
00066 #include "asterisk.h"
00067 
00068 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00069 
00070 #include "asterisk/_private.h"
00071 
00072 #undef sched_setscheduler
00073 #undef setpriority
00074 #include <sys/time.h>
00075 #include <fcntl.h>
00076 #include <signal.h>
00077 #include <sched.h>
00078 #include <sys/un.h>
00079 #include <sys/wait.h>
00080 #include <ctype.h>
00081 #include <sys/resource.h>
00082 #include <grp.h>
00083 #include <pwd.h>
00084 #include <sys/stat.h>
00085 #if defined(HAVE_SYSINFO)
00086 #include <sys/sysinfo.h>
00087 #elif defined(HAVE_SYSCTL)
00088 #include <sys/param.h>
00089 #include <sys/sysctl.h>
00090 #if !defined(__OpenBSD__)
00091 #include <sys/vmmeter.h>
00092 #if defined(__FreeBSD__)
00093 #include <vm/vm_param.h>
00094 #endif
00095 #endif
00096 #if defined(HAVE_SWAPCTL)
00097 #include <sys/swap.h>
00098 #endif
00099 #endif
00100 #include <regex.h>
00101 
00102 #if defined(SOLARIS)
00103 int daemon(int, int);  /* defined in libresolv of all places */
00104 #include <sys/loadavg.h>
00105 #endif
00106 
00107 #ifdef linux
00108 #include <sys/prctl.h>
00109 #ifdef HAVE_CAP
00110 #include <sys/capability.h>
00111 #endif /* HAVE_CAP */
00112 #endif /* linux */
00113 
00114 #include "asterisk/paths.h"   /* we define here the variables so better agree on the prototype */
00115 #include "asterisk/network.h"
00116 #include "asterisk/cli.h"
00117 #include "asterisk/channel.h"
00118 #include "asterisk/features.h"
00119 #include "asterisk/ulaw.h"
00120 #include "asterisk/alaw.h"
00121 #include "asterisk/callerid.h"
00122 #include "asterisk/image.h"
00123 #include "asterisk/tdd.h"
00124 #include "asterisk/term.h"
00125 #include "asterisk/manager.h"
00126 #include "asterisk/cdr.h"
00127 #include "asterisk/cel.h"
00128 #include "asterisk/pbx.h"
00129 #include "asterisk/enum.h"
00130 #include "asterisk/http.h"
00131 #include "asterisk/udptl.h"
00132 #include "asterisk/app.h"
00133 #include "asterisk/lock.h"
00134 #include "asterisk/utils.h"
00135 #include "asterisk/file.h"
00136 #include "asterisk/io.h"
00137 #include "editline/histedit.h"
00138 #include "asterisk/config.h"
00139 #include "asterisk/ast_version.h"
00140 #include "asterisk/linkedlists.h"
00141 #include "asterisk/devicestate.h"
00142 #include "asterisk/module.h"
00143 #include "asterisk/dsp.h"
00144 #include "asterisk/buildinfo.h"
00145 #include "asterisk/xmldoc.h"
00146 #include "asterisk/poll-compat.h"
00147 #include "asterisk/ccss.h"
00148 #include "asterisk/test.h"
00149 #include "asterisk/aoc.h"
00150 
00151 #include "../defaults.h"
00152 
00153 #ifndef AF_LOCAL
00154 #define AF_LOCAL AF_UNIX
00155 #define PF_LOCAL PF_UNIX
00156 #endif
00157 
00158 #define AST_MAX_CONNECTS 128
00159 #define NUM_MSGS 64
00160 
00161 /*! \brief Welcome message when starting a CLI interface */
00162 #define WELCOME_MESSAGE \
00163     ast_verbose("Asterisk %s, Copyright (C) 1999 - 2012 Digium, Inc. and others.\n" \
00164                 "Created by Mark Spencer <markster@digium.com>\n" \
00165                 "Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.\n" \
00166                 "This is free software, with components licensed under the GNU General Public\n" \
00167                 "License version 2 and other licenses; you are welcome to redistribute it under\n" \
00168                 "certain conditions. Type 'core show license' for details.\n" \
00169                 "=========================================================================\n", ast_get_version()) \
00170 
00171 /*! \defgroup main_options Main Configuration Options
00172  * \brief Main configuration options from asterisk.conf or OS command line on starting Asterisk.
00173  * \arg \ref Config_ast "asterisk.conf"
00174  * \note Some of them can be changed in the CLI 
00175  */
00176 /*! @{ */
00177 
00178 struct ast_flags ast_options = { AST_DEFAULT_OPTIONS };
00179 struct ast_flags ast_compat = { 0 };
00180 
00181 int option_verbose;           /*!< Verbosity level */
00182 int option_debug;          /*!< Debug level */
00183 double option_maxload;           /*!< Max load avg on system */
00184 int option_maxcalls;          /*!< Max number of active calls */
00185 int option_maxfiles;          /*!< Max number of open file handles (files, sockets) */
00186 #if defined(HAVE_SYSINFO)
00187 long option_minmemfree;          /*!< Minimum amount of free system memory - stop accepting calls if free memory falls below this watermark */
00188 #endif
00189 
00190 /*! @} */
00191 
00192 struct ast_eid ast_eid_default;
00193 
00194 /* XXX tmpdir is a subdir of the spool directory, and no way to remap it */
00195 char record_cache_dir[AST_CACHE_DIR_LEN] = DEFAULT_TMP_DIR;
00196 
00197 static int ast_socket = -1;      /*!< UNIX Socket for allowing remote control */
00198 static int ast_consock = -1;     /*!< UNIX Socket for controlling another asterisk */
00199 pid_t ast_mainpid;
00200 struct console {
00201    int fd;           /*!< File descriptor */
00202    int p[2];         /*!< Pipe */
00203    pthread_t t;         /*!< Thread of handler */
00204    int mute;         /*!< Is the console muted for logs */
00205    int uid;       /*!< Remote user ID. */
00206    int gid;       /*!< Remote group ID. */
00207    int levels[NUMLOGLEVELS];  /*!< Which log levels are enabled for the console */
00208 };
00209 
00210 struct ast_atexit {
00211    void (*func)(void);
00212    AST_RWLIST_ENTRY(ast_atexit) list;
00213 };
00214 
00215 static AST_RWLIST_HEAD_STATIC(atexits, ast_atexit);
00216 
00217 struct timeval ast_startuptime;
00218 struct timeval ast_lastreloadtime;
00219 
00220 static History *el_hist;
00221 static EditLine *el;
00222 static char *remotehostname;
00223 
00224 struct console consoles[AST_MAX_CONNECTS];
00225 
00226 char defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE;
00227 
00228 static int ast_el_add_history(char *);
00229 static int ast_el_read_history(char *);
00230 static int ast_el_write_history(char *);
00231 
00232 struct _cfg_paths {
00233    char config_dir[PATH_MAX];
00234    char module_dir[PATH_MAX];
00235    char spool_dir[PATH_MAX];
00236    char monitor_dir[PATH_MAX];
00237    char var_dir[PATH_MAX];
00238    char data_dir[PATH_MAX];
00239    char log_dir[PATH_MAX];
00240    char agi_dir[PATH_MAX];
00241    char run_dir[PATH_MAX];
00242    char key_dir[PATH_MAX];
00243 
00244    char config_file[PATH_MAX];
00245    char db_path[PATH_MAX];
00246    char pid_path[PATH_MAX];
00247    char socket_path[PATH_MAX];
00248    char run_user[PATH_MAX];
00249    char run_group[PATH_MAX];
00250    char system_name[128];
00251 };
00252 
00253 static struct _cfg_paths cfg_paths;
00254 
00255 const char *ast_config_AST_CONFIG_DIR  = cfg_paths.config_dir;
00256 const char *ast_config_AST_CONFIG_FILE = cfg_paths.config_file;
00257 const char *ast_config_AST_MODULE_DIR  = cfg_paths.module_dir;
00258 const char *ast_config_AST_SPOOL_DIR   = cfg_paths.spool_dir;
00259 const char *ast_config_AST_MONITOR_DIR = cfg_paths.monitor_dir;
00260 const char *ast_config_AST_VAR_DIR  = cfg_paths.var_dir;
00261 const char *ast_config_AST_DATA_DIR = cfg_paths.data_dir;
00262 const char *ast_config_AST_LOG_DIR  = cfg_paths.log_dir;
00263 const char *ast_config_AST_AGI_DIR  = cfg_paths.agi_dir;
00264 const char *ast_config_AST_KEY_DIR  = cfg_paths.key_dir;
00265 const char *ast_config_AST_RUN_DIR  = cfg_paths.run_dir;
00266 
00267 const char *ast_config_AST_DB    = cfg_paths.db_path;
00268 const char *ast_config_AST_PID      = cfg_paths.pid_path;
00269 const char *ast_config_AST_SOCKET   = cfg_paths.socket_path;
00270 const char *ast_config_AST_RUN_USER = cfg_paths.run_user;
00271 const char *ast_config_AST_RUN_GROUP   = cfg_paths.run_group;
00272 const char *ast_config_AST_SYSTEM_NAME = cfg_paths.system_name;
00273 
00274 static char ast_config_AST_CTL_PERMISSIONS[PATH_MAX];
00275 static char ast_config_AST_CTL_OWNER[PATH_MAX] = "\0";
00276 static char ast_config_AST_CTL_GROUP[PATH_MAX] = "\0";
00277 static char ast_config_AST_CTL[PATH_MAX] = "asterisk.ctl";
00278 
00279 extern unsigned int ast_FD_SETSIZE;
00280 
00281 static char *_argv[256];
00282 typedef enum {
00283    NOT_SHUTTING_DOWN = -2,
00284    SHUTTING_DOWN = -1,
00285    /* Valid values for quit_handler niceness below: */
00286    SHUTDOWN_FAST,
00287    SHUTDOWN_NORMAL,
00288    SHUTDOWN_NICE,
00289    SHUTDOWN_REALLY_NICE
00290 } shutdown_nice_t;
00291 static shutdown_nice_t shuttingdown = NOT_SHUTTING_DOWN;
00292 static int restartnow;
00293 static pthread_t consolethread = AST_PTHREADT_NULL;
00294 static pthread_t mon_sig_flags;
00295 static int canary_pid = 0;
00296 static char canary_filename[128];
00297 
00298 static char randompool[256];
00299 
00300 static int sig_alert_pipe[2] = { -1, -1 };
00301 static struct {
00302     unsigned int need_reload:1;
00303     unsigned int need_quit:1;
00304     unsigned int need_quit_handler:1;
00305 } sig_flags;
00306 
00307 #if !defined(LOW_MEMORY)
00308 struct file_version {
00309    AST_RWLIST_ENTRY(file_version) list;
00310    const char *file;
00311    char *version;
00312 };
00313 
00314 static AST_RWLIST_HEAD_STATIC(file_versions, file_version);
00315 
00316 void ast_register_file_version(const char *file, const char *version)
00317 {
00318    struct file_version *new;
00319    char *work;
00320    size_t version_length;
00321 
00322    work = ast_strdupa(version);
00323    work = ast_strip(ast_strip_quoted(work, "$", "$"));
00324    version_length = strlen(work) + 1;
00325    
00326    if (!(new = ast_calloc(1, sizeof(*new) + version_length)))
00327       return;
00328 
00329    new->file = file;
00330    new->version = (char *) new + sizeof(*new);
00331    memcpy(new->version, work, version_length);
00332    AST_RWLIST_WRLOCK(&file_versions);
00333    AST_RWLIST_INSERT_HEAD(&file_versions, new, list);
00334    AST_RWLIST_UNLOCK(&file_versions);
00335 }
00336 
00337 void ast_unregister_file_version(const char *file)
00338 {
00339    struct file_version *find;
00340 
00341    AST_RWLIST_WRLOCK(&file_versions);
00342    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&file_versions, find, list) {
00343       if (!strcasecmp(find->file, file)) {
00344          AST_RWLIST_REMOVE_CURRENT(list);
00345          break;
00346       }
00347    }
00348    AST_RWLIST_TRAVERSE_SAFE_END;
00349    AST_RWLIST_UNLOCK(&file_versions);
00350 
00351    if (find)
00352       ast_free(find);
00353 }
00354 
00355 char *ast_complete_source_filename(const char *partial, int n)
00356 {
00357    struct file_version *find;
00358    size_t len = strlen(partial);
00359    int count = 0;
00360    char *res = NULL;
00361 
00362    AST_RWLIST_RDLOCK(&file_versions);
00363    AST_RWLIST_TRAVERSE(&file_versions, find, list) {
00364       if (!strncasecmp(find->file, partial, len) && ++count > n) {
00365          res = ast_strdup(find->file);
00366          break;
00367       }
00368    }
00369    AST_RWLIST_UNLOCK(&file_versions);
00370    return res;
00371 }
00372 
00373 /*! \brief Find version for given module name */
00374 const char *ast_file_version_find(const char *file)
00375 {
00376    struct file_version *iterator;
00377 
00378    AST_RWLIST_WRLOCK(&file_versions);
00379    AST_RWLIST_TRAVERSE(&file_versions, iterator, list) {
00380       if (!strcasecmp(iterator->file, file))
00381          break;
00382    }
00383    AST_RWLIST_UNLOCK(&file_versions);
00384    if (iterator)
00385       return iterator->version;
00386    return NULL;
00387 }      
00388        
00389 
00390 
00391 struct thread_list_t {
00392    AST_RWLIST_ENTRY(thread_list_t) list;
00393    char *name;
00394    pthread_t id;
00395 };
00396 
00397 static AST_RWLIST_HEAD_STATIC(thread_list, thread_list_t);
00398 
00399 void ast_register_thread(char *name)
00400 { 
00401    struct thread_list_t *new = ast_calloc(1, sizeof(*new));
00402 
00403    if (!new)
00404       return;
00405    new->id = pthread_self();
00406    new->name = name; /* steal the allocated memory for the thread name */
00407    AST_RWLIST_WRLOCK(&thread_list);
00408    AST_RWLIST_INSERT_HEAD(&thread_list, new, list);
00409    AST_RWLIST_UNLOCK(&thread_list);
00410 }
00411 
00412 void ast_unregister_thread(void *id)
00413 {
00414    struct thread_list_t *x;
00415 
00416    AST_RWLIST_WRLOCK(&thread_list);
00417    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&thread_list, x, list) {
00418       if ((void *) x->id == id) {
00419          AST_RWLIST_REMOVE_CURRENT(list);
00420          break;
00421       }
00422    }
00423    AST_RWLIST_TRAVERSE_SAFE_END;
00424    AST_RWLIST_UNLOCK(&thread_list);
00425    if (x) {
00426       ast_free(x->name);
00427       ast_free(x);
00428    }
00429 }
00430 
00431 /*! \brief Give an overview of core settings */
00432 static char *handle_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00433 {
00434    char buf[BUFSIZ];
00435    struct ast_tm tm;
00436    char eid_str[128];
00437 
00438    switch (cmd) {
00439    case CLI_INIT:
00440       e->command = "core show settings";
00441       e->usage = "Usage: core show settings\n"
00442             "       Show core misc settings";
00443       return NULL;
00444    case CLI_GENERATE:
00445       return NULL;
00446    }
00447 
00448    ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
00449 
00450    ast_cli(a->fd, "\nPBX Core settings\n");
00451    ast_cli(a->fd, "-----------------\n");
00452    ast_cli(a->fd, "  Version:                     %s\n", ast_get_version());
00453    ast_cli(a->fd, "  Build Options:               %s\n", S_OR(AST_BUILDOPTS, "(none)"));
00454    if (option_maxcalls)
00455       ast_cli(a->fd, "  Maximum calls:               %d (Current %d)\n", option_maxcalls, ast_active_channels());
00456    else
00457       ast_cli(a->fd, "  Maximum calls:               Not set\n");
00458    if (option_maxfiles)
00459       ast_cli(a->fd, "  Maximum open file handles:   %d\n", option_maxfiles); 
00460    else
00461       ast_cli(a->fd, "  Maximum open file handles:   Not set\n");
00462    ast_cli(a->fd, "  Verbosity:                   %d\n", option_verbose);
00463    ast_cli(a->fd, "  Debug level:                 %d\n", option_debug);
00464    ast_cli(a->fd, "  Maximum load average:        %lf\n", option_maxload);
00465 #if defined(HAVE_SYSINFO)
00466    ast_cli(a->fd, "  Minimum free memory:         %ld MB\n", option_minmemfree);
00467 #endif
00468    if (ast_localtime(&ast_startuptime, &tm, NULL)) {
00469       ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
00470       ast_cli(a->fd, "  Startup time:                %s\n", buf);
00471    }
00472    if (ast_localtime(&ast_lastreloadtime, &tm, NULL)) {
00473       ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
00474       ast_cli(a->fd, "  Last reload time:            %s\n", buf);
00475    }
00476    ast_cli(a->fd, "  System:                      %s/%s built by %s on %s %s\n", ast_build_os, ast_build_kernel, ast_build_user, ast_build_machine, ast_build_date);
00477    ast_cli(a->fd, "  System name:                 %s\n", ast_config_AST_SYSTEM_NAME);
00478    ast_cli(a->fd, "  Entity ID:                   %s\n", eid_str);
00479    ast_cli(a->fd, "  Default language:            %s\n", defaultlanguage);
00480    ast_cli(a->fd, "  Language prefix:             %s\n", ast_language_is_prefix ? "Enabled" : "Disabled");
00481    ast_cli(a->fd, "  User name and group:         %s/%s\n", ast_config_AST_RUN_USER, ast_config_AST_RUN_GROUP);
00482    ast_cli(a->fd, "  Executable includes:         %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_EXEC_INCLUDES) ? "Enabled" : "Disabled");
00483    ast_cli(a->fd, "  Transcode via SLIN:          %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSCODE_VIA_SLIN) ? "Enabled" : "Disabled");
00484    ast_cli(a->fd, "  Internal timing:             %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_INTERNAL_TIMING) ? "Enabled" : "Disabled");
00485    ast_cli(a->fd, "  Transmit silence during rec: %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSMIT_SILENCE) ? "Enabled" : "Disabled");
00486    ast_cli(a->fd, "  Generic PLC:                 %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_GENERIC_PLC) ? "Enabled" : "Disabled");
00487 
00488    ast_cli(a->fd, "\n* Subsystems\n");
00489    ast_cli(a->fd, "  -------------\n");
00490    ast_cli(a->fd, "  Manager (AMI):               %s\n", check_manager_enabled() ? "Enabled" : "Disabled");
00491    ast_cli(a->fd, "  Web Manager (AMI/HTTP):      %s\n", check_webmanager_enabled() ? "Enabled" : "Disabled");
00492    ast_cli(a->fd, "  Call data records:           %s\n", check_cdr_enabled() ? "Enabled" : "Disabled");
00493    ast_cli(a->fd, "  Realtime Architecture (ARA): %s\n", ast_realtime_enabled() ? "Enabled" : "Disabled");
00494 
00495    /*! \todo we could check musiconhold, voicemail, smdi, adsi, queues  */
00496 
00497    ast_cli(a->fd, "\n* Directories\n");
00498    ast_cli(a->fd, "  -------------\n");
00499    ast_cli(a->fd, "  Configuration file:          %s\n", ast_config_AST_CONFIG_FILE);
00500    ast_cli(a->fd, "  Configuration directory:     %s\n", ast_config_AST_CONFIG_DIR);
00501    ast_cli(a->fd, "  Module directory:            %s\n", ast_config_AST_MODULE_DIR);
00502    ast_cli(a->fd, "  Spool directory:             %s\n", ast_config_AST_SPOOL_DIR);
00503    ast_cli(a->fd, "  Log directory:               %s\n", ast_config_AST_LOG_DIR);
00504    ast_cli(a->fd, "  Run/Sockets directory:       %s\n", ast_config_AST_RUN_DIR);
00505    ast_cli(a->fd, "  PID file:                    %s\n", ast_config_AST_PID);
00506    ast_cli(a->fd, "  VarLib directory:            %s\n", ast_config_AST_VAR_DIR);
00507    ast_cli(a->fd, "  Data directory:              %s\n", ast_config_AST_DATA_DIR);
00508    ast_cli(a->fd, "  ASTDB:                       %s\n", ast_config_AST_DB);
00509    ast_cli(a->fd, "  IAX2 Keys directory:         %s\n", ast_config_AST_KEY_DIR);
00510    ast_cli(a->fd, "  AGI Scripts directory:       %s\n", ast_config_AST_AGI_DIR);
00511    ast_cli(a->fd, "\n\n");
00512    return CLI_SUCCESS;
00513 }
00514 
00515 static char *handle_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00516 {
00517    int count = 0;
00518    struct thread_list_t *cur;
00519    switch (cmd) {
00520    case CLI_INIT:
00521       e->command = "core show threads";
00522       e->usage = 
00523          "Usage: core show threads\n"
00524          "       List threads currently active in the system.\n";
00525       return NULL;
00526    case CLI_GENERATE:
00527       return NULL;
00528    }
00529 
00530    AST_RWLIST_RDLOCK(&thread_list);
00531    AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
00532       ast_cli(a->fd, "%p %s\n", (void *)cur->id, cur->name);
00533       count++;
00534    }
00535         AST_RWLIST_UNLOCK(&thread_list);
00536    ast_cli(a->fd, "%d threads listed.\n", count);
00537    return CLI_SUCCESS;
00538 }
00539 
00540 #if defined (HAVE_SYSCTL) && defined(HAVE_SWAPCTL)
00541 /*
00542  * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
00543  * to be based on the new swapctl(2) system call.
00544  */
00545 static int swapmode(int *used, int *total)
00546 {
00547    struct swapent *swdev;
00548    int nswap, rnswap, i;
00549 
00550    nswap = swapctl(SWAP_NSWAP, 0, 0);
00551    if (nswap == 0)
00552       return 0;
00553 
00554    swdev = ast_calloc(nswap, sizeof(*swdev));
00555    if (swdev == NULL)
00556       return 0;
00557 
00558    rnswap = swapctl(SWAP_STATS, swdev, nswap);
00559    if (rnswap == -1) {
00560       ast_free(swdev);
00561       return 0;
00562    }
00563 
00564    /* if rnswap != nswap, then what? */
00565 
00566    /* Total things up */
00567    *total = *used = 0;
00568    for (i = 0; i < nswap; i++) {
00569       if (swdev[i].se_flags & SWF_ENABLE) {
00570          *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
00571          *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
00572       }
00573    }
00574    ast_free(swdev);
00575    return 1;
00576 }
00577 #elif defined(HAVE_SYSCTL) && !defined(HAVE_SYSINFO)
00578 static int swapmode(int *used, int *total)
00579 {
00580    *used = *total = 0;
00581    return 1;
00582 }
00583 #endif
00584 
00585 #if defined(HAVE_SYSINFO) || defined(HAVE_SYSCTL)
00586 /*! \brief Give an overview of system statistics */
00587 static char *handle_show_sysinfo(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00588 {
00589    uint64_t physmem, freeram;
00590    uint64_t freeswap = 0;
00591    int nprocs = 0;
00592    long uptime = 0;
00593    int totalswap = 0;
00594 #if defined(HAVE_SYSINFO)
00595    struct sysinfo sys_info;
00596    sysinfo(&sys_info);
00597    uptime = sys_info.uptime / 3600;
00598    physmem = sys_info.totalram * sys_info.mem_unit;
00599    freeram = (sys_info.freeram * sys_info.mem_unit) / 1024;
00600    totalswap = (sys_info.totalswap * sys_info.mem_unit) / 1024;
00601    freeswap = (sys_info.freeswap * sys_info.mem_unit) / 1024;
00602    nprocs = sys_info.procs;
00603 #elif defined(HAVE_SYSCTL)
00604    static int pageshift;
00605    struct vmtotal vmtotal;
00606    struct timeval boottime;
00607    time_t   now;
00608    int mib[2], pagesize, usedswap = 0;
00609    size_t len;
00610    /* calculate the uptime by looking at boottime */
00611    time(&now);
00612    mib[0] = CTL_KERN;
00613    mib[1] = KERN_BOOTTIME;
00614    len = sizeof(boottime);
00615    if (sysctl(mib, 2, &boottime, &len, NULL, 0) != -1) {
00616       uptime = now - boottime.tv_sec;
00617    }
00618    uptime = uptime/3600;
00619    /* grab total physical memory  */
00620    mib[0] = CTL_HW;
00621 #if defined(HW_PHYSMEM64)
00622    mib[1] = HW_PHYSMEM64;
00623 #else
00624    mib[1] = HW_PHYSMEM;
00625 #endif
00626    len = sizeof(physmem);
00627    sysctl(mib, 2, &physmem, &len, NULL, 0);
00628 
00629    pagesize = getpagesize();
00630    pageshift = 0;
00631    while (pagesize > 1) {
00632       pageshift++;
00633       pagesize >>= 1;
00634    }
00635 
00636    /* we only need the amount of log(2)1024 for our conversion */
00637    pageshift -= 10;
00638 
00639    /* grab vm totals */
00640    mib[0] = CTL_VM;
00641    mib[1] = VM_METER;
00642    len = sizeof(vmtotal);
00643    sysctl(mib, 2, &vmtotal, &len, NULL, 0);
00644    freeram = (vmtotal.t_free << pageshift);
00645    /* generate swap usage and totals */
00646    swapmode(&usedswap, &totalswap);
00647    freeswap = (totalswap - usedswap);
00648    /* grab number of processes */
00649 #if defined(__OpenBSD__)
00650    mib[0] = CTL_KERN;
00651    mib[1] = KERN_NPROCS;
00652    len = sizeof(nprocs);
00653    sysctl(mib, 2, &nprocs, &len, NULL, 0);
00654 #endif
00655 #endif
00656 
00657    switch (cmd) {
00658    case CLI_INIT:
00659       e->command = "core show sysinfo";
00660       e->usage =
00661          "Usage: core show sysinfo\n"
00662          "       List current system information.\n";
00663       return NULL;
00664    case CLI_GENERATE:
00665       return NULL;
00666    }
00667 
00668    ast_cli(a->fd, "\nSystem Statistics\n");
00669    ast_cli(a->fd, "-----------------\n");
00670    ast_cli(a->fd, "  System Uptime:             %lu hours\n", uptime);
00671    ast_cli(a->fd, "  Total RAM:                 %" PRIu64 " KiB\n", physmem / 1024);
00672    ast_cli(a->fd, "  Free RAM:                  %" PRIu64 " KiB\n", freeram);
00673 #if defined(HAVE_SYSINFO)
00674    ast_cli(a->fd, "  Buffer RAM:                %" PRIu64 " KiB\n", ((uint64_t) sys_info.bufferram * sys_info.mem_unit) / 1024);
00675 #endif
00676 #if defined (HAVE_SYSCTL) || defined(HAVE_SWAPCTL)
00677    ast_cli(a->fd, "  Total Swap Space:          %u KiB\n", totalswap);
00678    ast_cli(a->fd, "  Free Swap Space:           %" PRIu64 " KiB\n\n", freeswap);
00679 #endif
00680    ast_cli(a->fd, "  Number of Processes:       %d \n\n", nprocs);
00681    return CLI_SUCCESS;
00682 }
00683 #endif
00684 
00685 struct profile_entry {
00686    const char *name;
00687    uint64_t scale;   /* if non-zero, values are scaled by this */
00688    int64_t  mark;
00689    int64_t  value;
00690    int64_t  events;
00691 };
00692 
00693 struct profile_data {
00694    int entries;
00695    int max_size;
00696    struct profile_entry e[0];
00697 };
00698 
00699 static struct profile_data *prof_data;
00700 
00701 /*! \brief allocates a counter with a given name and scale.
00702  * \return Returns the identifier of the counter.
00703  */
00704 int ast_add_profile(const char *name, uint64_t scale)
00705 {
00706    int l = sizeof(struct profile_data);
00707    int n = 10; /* default entries */
00708 
00709    if (prof_data == NULL) {
00710       prof_data = ast_calloc(1, l + n*sizeof(struct profile_entry));
00711       if (prof_data == NULL)
00712          return -1;
00713       prof_data->entries = 0;
00714       prof_data->max_size = n;
00715    }
00716    if (prof_data->entries >= prof_data->max_size) {
00717       void *p;
00718       n = prof_data->max_size + 20;
00719       p = ast_realloc(prof_data, l + n*sizeof(struct profile_entry));
00720       if (p == NULL)
00721          return -1;
00722       prof_data = p;
00723       prof_data->max_size = n;
00724    }
00725    n = prof_data->entries++;
00726    prof_data->e[n].name = ast_strdup(name);
00727    prof_data->e[n].value = 0;
00728    prof_data->e[n].events = 0;
00729    prof_data->e[n].mark = 0;
00730    prof_data->e[n].scale = scale;
00731    return n;
00732 }
00733 
00734 int64_t ast_profile(int i, int64_t delta)
00735 {
00736    if (!prof_data || i < 0 || i > prof_data->entries) /* invalid index */
00737       return 0;
00738    if (prof_data->e[i].scale > 1)
00739       delta /= prof_data->e[i].scale;
00740    prof_data->e[i].value += delta;
00741    prof_data->e[i].events++;
00742    return prof_data->e[i].value;
00743 }
00744 
00745 /* The RDTSC instruction was introduced on the Pentium processor and is not
00746  * implemented on certain clones, like the Cyrix 586. Hence, the previous
00747  * expectation of __i386__ was in error. */
00748 #if defined ( __i686__) && (defined(__FreeBSD__) || defined(linux))
00749 #if defined(__FreeBSD__)
00750 #include <machine/cpufunc.h>
00751 #elif defined(linux)
00752 static __inline uint64_t
00753 rdtsc(void)
00754 { 
00755    uint64_t rv;
00756 
00757    __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
00758    return (rv);
00759 }
00760 #endif
00761 #else /* supply a dummy function on other platforms */
00762 static __inline uint64_t
00763 rdtsc(void)
00764 {
00765    return 0;
00766 }
00767 #endif
00768 
00769 int64_t ast_mark(int i, int startstop)
00770 {
00771    if (!prof_data || i < 0 || i > prof_data->entries) /* invalid index */
00772       return 0;
00773    if (startstop == 1)
00774       prof_data->e[i].mark = rdtsc();
00775    else {
00776       prof_data->e[i].mark = (rdtsc() - prof_data->e[i].mark);
00777       if (prof_data->e[i].scale > 1)
00778          prof_data->e[i].mark /= prof_data->e[i].scale;
00779       prof_data->e[i].value += prof_data->e[i].mark;
00780       prof_data->e[i].events++;
00781    }
00782    return prof_data->e[i].mark;
00783 }
00784 
00785 #define DEFINE_PROFILE_MIN_MAX_VALUES min = 0; \
00786    max = prof_data->entries;\
00787    if  (a->argc > 3) { /* specific entries */ \
00788       if (isdigit(a->argv[3][0])) { \
00789          min = atoi(a->argv[3]); \
00790          if (a->argc == 5 && strcmp(a->argv[4], "-")) \
00791             max = atoi(a->argv[4]); \
00792       } else \
00793          search = a->argv[3]; \
00794    } \
00795    if (max > prof_data->entries) \
00796       max = prof_data->entries;
00797 
00798 static char *handle_show_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00799 {
00800    int i, min, max;
00801    const char *search = NULL;
00802    switch (cmd) {
00803    case CLI_INIT:
00804       e->command = "core show profile";
00805       e->usage = "Usage: core show profile\n"
00806             "       show profile information";
00807       return NULL;
00808    case CLI_GENERATE:
00809       return NULL;
00810    }
00811 
00812    if (prof_data == NULL)
00813       return 0;
00814 
00815    DEFINE_PROFILE_MIN_MAX_VALUES;
00816    ast_cli(a->fd, "profile values (%d, allocated %d)\n-------------------\n",
00817       prof_data->entries, prof_data->max_size);
00818    ast_cli(a->fd, "%6s   %8s  %10s %12s %12s  %s\n", "ID", "Scale", "Events",
00819          "Value", "Average", "Name");
00820    for (i = min; i < max; i++) {
00821       struct profile_entry *entry = &prof_data->e[i];
00822       if (!search || strstr(entry->name, search))
00823           ast_cli(a->fd, "%6d: [%8ld] %10ld %12lld %12lld  %s\n",
00824          i,
00825          (long)entry->scale,
00826          (long)entry->events, (long long)entry->value,
00827          (long long)(entry->events ? entry->value / entry->events : entry->value),
00828          entry->name);
00829    }
00830    return CLI_SUCCESS;
00831 }
00832 
00833 static char *handle_clear_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00834 {
00835    int i, min, max;
00836    const char *search = NULL;
00837    switch (cmd) {
00838    case CLI_INIT:
00839       e->command = "core clear profile";
00840       e->usage = "Usage: core clear profile\n"
00841             "       clear profile information";
00842       return NULL;
00843    case CLI_GENERATE:
00844       return NULL;
00845    }
00846 
00847    if (prof_data == NULL)
00848       return 0;
00849 
00850    DEFINE_PROFILE_MIN_MAX_VALUES;
00851    for (i= min; i < max; i++) {
00852       if (!search || strstr(prof_data->e[i].name, search)) {
00853          prof_data->e[i].value = 0;
00854          prof_data->e[i].events = 0;
00855       }
00856    }
00857    return CLI_SUCCESS;
00858 }
00859 #undef DEFINE_PROFILE_MIN_MAX_VALUES
00860 
00861 /*! \brief CLI command to list module versions */
00862 static char *handle_show_version_files(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00863 {
00864 #define FORMAT "%-25.25s %-40.40s\n"
00865    struct file_version *iterator;
00866    regex_t regexbuf;
00867    int havepattern = 0;
00868    int havename = 0;
00869    int count_files = 0;
00870    char *ret = NULL;
00871    int matchlen, which = 0;
00872    struct file_version *find;
00873 
00874    switch (cmd) {
00875    case CLI_INIT:
00876       e->command = "core show file version [like]";
00877       e->usage = 
00878          "Usage: core show file version [like <pattern>]\n"
00879          "       Lists the revision numbers of the files used to build this copy of Asterisk.\n"
00880          "       Optional regular expression pattern is used to filter the file list.\n";
00881       return NULL;
00882    case CLI_GENERATE:
00883       matchlen = strlen(a->word);
00884       if (a->pos != 3)
00885          return NULL;
00886       AST_RWLIST_RDLOCK(&file_versions);
00887       AST_RWLIST_TRAVERSE(&file_versions, find, list) {
00888          if (!strncasecmp(a->word, find->file, matchlen) && ++which > a->n) {
00889             ret = ast_strdup(find->file);
00890             break;
00891          }
00892       }
00893       AST_RWLIST_UNLOCK(&file_versions);
00894       return ret;
00895    }
00896 
00897 
00898    switch (a->argc) {
00899    case 6:
00900       if (!strcasecmp(a->argv[4], "like")) {
00901          if (regcomp(&regexbuf, a->argv[5], REG_EXTENDED | REG_NOSUB))
00902             return CLI_SHOWUSAGE;
00903          havepattern = 1;
00904       } else
00905          return CLI_SHOWUSAGE;
00906       break;
00907    case 5:
00908       havename = 1;
00909       break;
00910    case 4:
00911       break;
00912    default:
00913       return CLI_SHOWUSAGE;
00914    }
00915 
00916    ast_cli(a->fd, FORMAT, "File", "Revision");
00917    ast_cli(a->fd, FORMAT, "----", "--------");
00918    AST_RWLIST_RDLOCK(&file_versions);
00919    AST_RWLIST_TRAVERSE(&file_versions, iterator, list) {
00920       if (havename && strcasecmp(iterator->file, a->argv[4]))
00921          continue;
00922 
00923       if (havepattern && regexec(&regexbuf, iterator->file, 0, NULL, 0))
00924          continue;
00925 
00926       ast_cli(a->fd, FORMAT, iterator->file, iterator->version);
00927       count_files++;
00928       if (havename)
00929          break;
00930    }
00931    AST_RWLIST_UNLOCK(&file_versions);
00932    if (!havename) {
00933       ast_cli(a->fd, "%d files listed.\n", count_files);
00934    }
00935 
00936    if (havepattern)
00937       regfree(&regexbuf);
00938 
00939    return CLI_SUCCESS;
00940 #undef FORMAT
00941 }
00942 
00943 #endif /* ! LOW_MEMORY */
00944 
00945 int ast_register_atexit(void (*func)(void))
00946 {
00947    struct ast_atexit *ae;
00948 
00949    if (!(ae = ast_calloc(1, sizeof(*ae))))
00950       return -1;
00951 
00952    ae->func = func;
00953 
00954    ast_unregister_atexit(func);  
00955 
00956    AST_RWLIST_WRLOCK(&atexits);
00957    AST_RWLIST_INSERT_HEAD(&atexits, ae, list);
00958    AST_RWLIST_UNLOCK(&atexits);
00959 
00960    return 0;
00961 }
00962 
00963 void ast_unregister_atexit(void (*func)(void))
00964 {
00965    struct ast_atexit *ae = NULL;
00966 
00967    AST_RWLIST_WRLOCK(&atexits);
00968    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&atexits, ae, list) {
00969       if (ae->func == func) {
00970          AST_RWLIST_REMOVE_CURRENT(list);
00971          break;
00972       }
00973    }
00974    AST_RWLIST_TRAVERSE_SAFE_END;
00975    AST_RWLIST_UNLOCK(&atexits);
00976 
00977    free(ae);
00978 }
00979 
00980 /* Sending commands from consoles back to the daemon requires a terminating NULL */
00981 static int fdsend(int fd, const char *s)
00982 {
00983    return write(fd, s, strlen(s) + 1);
00984 }
00985 
00986 /* Sending messages from the daemon back to the display requires _excluding_ the terminating NULL */
00987 static int fdprint(int fd, const char *s)
00988 {
00989    return write(fd, s, strlen(s));
00990 }
00991 
00992 /*! \brief NULL handler so we can collect the child exit status */
00993 static void _null_sig_handler(int sig)
00994 {
00995 }
00996 
00997 static struct sigaction null_sig_handler = {
00998    .sa_handler = _null_sig_handler,
00999    .sa_flags = SA_RESTART,
01000 };
01001 
01002 static struct sigaction ignore_sig_handler = {
01003    .sa_handler = SIG_IGN,
01004 };
01005 
01006 AST_MUTEX_DEFINE_STATIC(safe_system_lock);
01007 /*! \brief Keep track of how many threads are currently trying to wait*() on
01008  *  a child process */
01009 static unsigned int safe_system_level = 0;
01010 static struct sigaction safe_system_prev_handler;
01011 
01012 void ast_replace_sigchld(void)
01013 {
01014    unsigned int level;
01015 
01016    ast_mutex_lock(&safe_system_lock);
01017    level = safe_system_level++;
01018 
01019    /* only replace the handler if it has not already been done */
01020    if (level == 0) {
01021       sigaction(SIGCHLD, &null_sig_handler, &safe_system_prev_handler);
01022    }
01023 
01024    ast_mutex_unlock(&safe_system_lock);
01025 }
01026 
01027 void ast_unreplace_sigchld(void)
01028 {
01029    unsigned int level;
01030 
01031    ast_mutex_lock(&safe_system_lock);
01032    level = --safe_system_level;
01033 
01034    /* only restore the handler if we are the last one */
01035    if (level == 0) {
01036       sigaction(SIGCHLD, &safe_system_prev_handler, NULL);
01037    }
01038 
01039    ast_mutex_unlock(&safe_system_lock);
01040 }
01041 
01042 int ast_safe_system(const char *s)
01043 {
01044    pid_t pid;
01045    int res;
01046    struct rusage rusage;
01047    int status;
01048 
01049 #if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
01050    ast_replace_sigchld();
01051 
01052 #ifdef HAVE_WORKING_FORK
01053    pid = fork();
01054 #else
01055    pid = vfork();
01056 #endif   
01057 
01058    if (pid == 0) {
01059 #ifdef HAVE_CAP
01060       cap_t cap = cap_from_text("cap_net_admin-eip");
01061 
01062       if (cap_set_proc(cap)) {
01063          /* Careful with order! Logging cannot happen after we close FDs */
01064          ast_log(LOG_WARNING, "Unable to remove capabilities.\n");
01065       }
01066       cap_free(cap);
01067 #endif
01068 #ifdef HAVE_WORKING_FORK
01069       if (ast_opt_high_priority)
01070          ast_set_priority(0);
01071       /* Close file descriptors and launch system command */
01072       ast_close_fds_above_n(STDERR_FILENO);
01073 #endif
01074       execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
01075       _exit(1);
01076    } else if (pid > 0) {
01077       for (;;) {
01078          res = wait4(pid, &status, 0, &rusage);
01079          if (res > -1) {
01080             res = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
01081             break;
01082          } else if (errno != EINTR) 
01083             break;
01084       }
01085    } else {
01086       ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
01087       res = -1;
01088    }
01089 
01090    ast_unreplace_sigchld();
01091 #else /* !defined(HAVE_WORKING_FORK) && !defined(HAVE_WORKING_VFORK) */
01092    res = -1;
01093 #endif
01094 
01095    return res;
01096 }
01097 
01098 /*!
01099  * \brief enable or disable a logging level to a specified console
01100  */
01101 void ast_console_toggle_loglevel(int fd, int level, int state)
01102 {
01103    int x;
01104 
01105    if (level >= NUMLOGLEVELS) {
01106       level = NUMLOGLEVELS - 1;
01107    }
01108 
01109    for (x = 0;x < AST_MAX_CONNECTS; x++) {
01110       if (fd == consoles[x].fd) {
01111          /*
01112           * Since the logging occurs when levels are false, set to
01113           * flipped iinput because this function accepts 0 as off and 1 as on
01114           */
01115          consoles[x].levels[level] = state ? 0 : 1;
01116          return;
01117       }
01118    }
01119 }
01120 
01121 /*!
01122  * \brief mute or unmute a console from logging
01123  */
01124 void ast_console_toggle_mute(int fd, int silent)
01125 {
01126    int x;
01127    for (x = 0;x < AST_MAX_CONNECTS; x++) {
01128       if (fd == consoles[x].fd) {
01129          if (consoles[x].mute) {
01130             consoles[x].mute = 0;
01131             if (!silent)
01132                ast_cli(fd, "Console is not muted anymore.\n");
01133          } else {
01134             consoles[x].mute = 1;
01135             if (!silent)
01136                ast_cli(fd, "Console is muted.\n");
01137          }
01138          return;
01139       }
01140    }
01141    ast_cli(fd, "Couldn't find remote console.\n");
01142 }
01143 
01144 /*!
01145  * \brief log the string to all attached console clients
01146  */
01147 static void ast_network_puts_mutable(const char *string, int level)
01148 {
01149    int x;
01150    for (x = 0;x < AST_MAX_CONNECTS; x++) {
01151       if (consoles[x].mute)
01152          continue;
01153       if (consoles[x].fd > -1) {
01154          if (!consoles[x].levels[level]) 
01155             fdprint(consoles[x].p[1], string);
01156       }
01157    }
01158 }
01159 
01160 /*!
01161  * \brief log the string to the console, and all attached
01162  * console clients
01163  */
01164 void ast_console_puts_mutable(const char *string, int level)
01165 {
01166    fputs(string, stdout);
01167    fflush(stdout);
01168    ast_network_puts_mutable(string, level);
01169 }
01170 
01171 /*!
01172  * \brief write the string to all attached console clients
01173  */
01174 static void ast_network_puts(const char *string)
01175 {
01176    int x;
01177    for (x = 0; x < AST_MAX_CONNECTS; x++) {
01178       if (consoles[x].fd > -1) 
01179          fdprint(consoles[x].p[1], string);
01180    }
01181 }
01182 
01183 /*!
01184  * write the string to the console, and all attached
01185  * console clients
01186  */
01187 void ast_console_puts(const char *string)
01188 {
01189    fputs(string, stdout);
01190    fflush(stdout);
01191    ast_network_puts(string);
01192 }
01193 
01194 static void network_verboser(const char *s)
01195 {
01196    ast_network_puts_mutable(s, __LOG_VERBOSE);
01197 }
01198 
01199 static pthread_t lthread;
01200 
01201 /*!
01202  * \brief read() function supporting the reception of user credentials.
01203  *
01204  * \param fd Socket file descriptor.
01205  * \param buffer Receive buffer.
01206  * \param size 'buffer' size.
01207  * \param con Console structure to set received credentials
01208  * \retval -1 on error
01209  * \retval the number of bytes received on success.
01210  */
01211 static int read_credentials(int fd, char *buffer, size_t size, struct console *con)
01212 {
01213 #if defined(SO_PEERCRED)
01214 #ifdef HAVE_STRUCT_SOCKPEERCRED_UID
01215 #define HAVE_STRUCT_UCRED_UID
01216    struct sockpeercred cred;
01217 #else
01218    struct ucred cred;
01219 #endif
01220    socklen_t len = sizeof(cred);
01221 #endif
01222 #if defined(HAVE_GETPEEREID)
01223    uid_t uid;
01224    gid_t gid;
01225 #else
01226    int uid, gid;
01227 #endif
01228    int result;
01229 
01230    result = read(fd, buffer, size);
01231    if (result < 0) {
01232       return result;
01233    }
01234 
01235 #if defined(SO_PEERCRED) && (defined(HAVE_STRUCT_UCRED_UID) || defined(HAVE_STRUCT_UCRED_CR_UID))
01236    if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len)) {
01237       return result;
01238    }
01239 #if defined(HAVE_STRUCT_UCRED_UID)
01240    uid = cred.uid;
01241    gid = cred.gid;
01242 #else /* defined(HAVE_STRUCT_UCRED_CR_UID) */
01243    uid = cred.cr_uid;
01244    gid = cred.cr_gid;
01245 #endif /* defined(HAVE_STRUCT_UCRED_UID) */
01246 
01247 #elif defined(HAVE_GETPEEREID)
01248    if (getpeereid(fd, &uid, &gid)) {
01249       return result;
01250    }
01251 #else
01252    return result;
01253 #endif
01254    con->uid = uid;
01255    con->gid = gid;
01256 
01257    return result;
01258 }
01259 
01260 static void *netconsole(void *vconsole)
01261 {
01262    struct console *con = vconsole;
01263    char hostname[MAXHOSTNAMELEN] = "";
01264    char inbuf[512];
01265    char outbuf[512];
01266    const char * const end_buf = inbuf + sizeof(inbuf);
01267    char *start_read = inbuf;
01268    int res;
01269    struct pollfd fds[2];
01270 
01271    if (gethostname(hostname, sizeof(hostname)-1))
01272       ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
01273    snprintf(outbuf, sizeof(outbuf), "%s/%ld/%s\n", hostname, (long)ast_mainpid, ast_get_version());
01274    fdprint(con->fd, outbuf);
01275    for (;;) {
01276       fds[0].fd = con->fd;
01277       fds[0].events = POLLIN;
01278       fds[0].revents = 0;
01279       fds[1].fd = con->p[0];
01280       fds[1].events = POLLIN;
01281       fds[1].revents = 0;
01282 
01283       res = ast_poll(fds, 2, -1);
01284       if (res < 0) {
01285          if (errno != EINTR)
01286             ast_log(LOG_WARNING, "poll returned < 0: %s\n", strerror(errno));
01287          continue;
01288       }
01289       if (fds[0].revents) {
01290          int cmds_read, bytes_read;
01291          if ((bytes_read = read_credentials(con->fd, start_read, end_buf - start_read, con)) < 1) {
01292             break;
01293          }
01294          /* XXX This will only work if it is the first command, and I'm not sure fixing it is worth the effort. */
01295          if (strncmp(inbuf, "cli quit after ", 15) == 0) {
01296             ast_cli_command_multiple_full(con->uid, con->gid, con->fd, bytes_read - 15, inbuf + 15);
01297             break;
01298          }
01299          /* ast_cli_command_multiple_full will only process individual commands terminated by a
01300           * NULL and not trailing partial commands. */
01301          if (!(cmds_read = ast_cli_command_multiple_full(con->uid, con->gid, con->fd, bytes_read + start_read - inbuf, inbuf))) {
01302             /* No commands were read. We either have a short read on the first command
01303              * with space left, or a command that is too long */
01304             if (start_read + bytes_read < end_buf) {
01305                start_read += bytes_read;
01306             } else {
01307                ast_log(LOG_ERROR, "Command too long! Skipping\n");
01308                start_read = inbuf;
01309             }
01310             continue;
01311          }
01312          if (start_read[bytes_read - 1] == '\0') {
01313             /* The read ended on a command boundary, start reading again at the head of inbuf */
01314             start_read = inbuf;
01315             continue;
01316          }
01317          /* If we get this far, we have left over characters that have not been processed.
01318           * Advance to the character after the last command read by ast_cli_command_multiple_full.
01319           * We are guaranteed to have at least cmds_read NULLs */
01320          while (cmds_read-- && (start_read = strchr(start_read, '\0'))) {
01321             start_read++;
01322          }
01323          memmove(inbuf, start_read, end_buf - start_read);
01324          start_read = end_buf - start_read + inbuf;
01325       }
01326       if (fds[1].revents) {
01327          res = read_credentials(con->p[0], outbuf, sizeof(outbuf), con);
01328          if (res < 1) {
01329             ast_log(LOG_ERROR, "read returned %d\n", res);
01330             break;
01331          }
01332          res = write(con->fd, outbuf, res);
01333          if (res < 1)
01334             break;
01335       }
01336    }
01337    if (!ast_opt_hide_connect) {
01338       ast_verb(3, "Remote UNIX connection disconnected\n");
01339    }
01340    close(con->fd);
01341    close(con->p[0]);
01342    close(con->p[1]);
01343    con->fd = -1;
01344    
01345    return NULL;
01346 }
01347 
01348 static void *listener(void *unused)
01349 {
01350    struct sockaddr_un sunaddr;
01351    int s;
01352    socklen_t len;
01353    int x;
01354    int flags;
01355    struct pollfd fds[1];
01356    for (;;) {
01357       if (ast_socket < 0)
01358          return NULL;
01359       fds[0].fd = ast_socket;
01360       fds[0].events = POLLIN;
01361       s = ast_poll(fds, 1, -1);
01362       pthread_testcancel();
01363       if (s < 0) {
01364          if (errno != EINTR)
01365             ast_log(LOG_WARNING, "poll returned error: %s\n", strerror(errno));
01366          continue;
01367       }
01368       len = sizeof(sunaddr);
01369       s = accept(ast_socket, (struct sockaddr *)&sunaddr, &len);
01370       if (s < 0) {
01371          if (errno != EINTR)
01372             ast_log(LOG_WARNING, "Accept returned %d: %s\n", s, strerror(errno));
01373       } else {
01374 #if !defined(SO_PASSCRED)
01375          {
01376 #else
01377          int sckopt = 1;
01378          /* turn on socket credentials passing. */
01379          if (setsockopt(s, SOL_SOCKET, SO_PASSCRED, &sckopt, sizeof(sckopt)) < 0) {
01380             ast_log(LOG_WARNING, "Unable to turn on socket credentials passing\n");
01381          } else {
01382 #endif
01383             for (x = 0; x < AST_MAX_CONNECTS; x++) {
01384                if (consoles[x].fd >= 0) {
01385                   continue;
01386                }
01387                if (socketpair(AF_LOCAL, SOCK_STREAM, 0, consoles[x].p)) {
01388                   ast_log(LOG_ERROR, "Unable to create pipe: %s\n", strerror(errno));
01389                   consoles[x].fd = -1;
01390                   fdprint(s, "Server failed to create pipe\n");
01391                   close(s);
01392                   break;
01393                }
01394                flags = fcntl(consoles[x].p[1], F_GETFL);
01395                fcntl(consoles[x].p[1], F_SETFL, flags | O_NONBLOCK);
01396                consoles[x].fd = s;
01397                consoles[x].mute = 1; /* Default is muted, we will un-mute if necessary */
01398                /* Default uid and gid to -2, so then in cli.c/cli_has_permissions() we will be able
01399                   to know if the user didn't send the credentials. */
01400                consoles[x].uid = -2;
01401                consoles[x].gid = -2;
01402                if (ast_pthread_create_detached_background(&consoles[x].t, NULL, netconsole, &consoles[x])) {
01403                   ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno));
01404                   close(consoles[x].p[0]);
01405                   close(consoles[x].p[1]);
01406                   consoles[x].fd = -1;
01407                   fdprint(s, "Server failed to spawn thread\n");
01408                   close(s);
01409                }
01410                break;
01411             }
01412             if (x >= AST_MAX_CONNECTS) {
01413                fdprint(s, "No more connections allowed\n");
01414                ast_log(LOG_WARNING, "No more connections allowed\n");
01415                close(s);
01416             } else if ((consoles[x].fd > -1) && (!ast_opt_hide_connect)) {
01417                ast_verb(3, "Remote UNIX connection\n");
01418             }
01419          }
01420       }
01421    }
01422    return NULL;
01423 }
01424 
01425 static int ast_makesocket(void)
01426 {
01427    struct sockaddr_un sunaddr;
01428    int res;
01429    int x;
01430    uid_t uid = -1;
01431    gid_t gid = -1;
01432 
01433    for (x = 0; x < AST_MAX_CONNECTS; x++) 
01434       consoles[x].fd = -1;
01435    unlink(ast_config_AST_SOCKET);
01436    ast_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
01437    if (ast_socket < 0) {
01438       ast_log(LOG_WARNING, "Unable to create control socket: %s\n", strerror(errno));
01439       return -1;
01440    }     
01441    memset(&sunaddr, 0, sizeof(sunaddr));
01442    sunaddr.sun_family = AF_LOCAL;
01443    ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
01444    res = bind(ast_socket, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
01445    if (res) {
01446       ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
01447       close(ast_socket);
01448       ast_socket = -1;
01449       return -1;
01450    }
01451    res = listen(ast_socket, 2);
01452    if (res < 0) {
01453       ast_log(LOG_WARNING, "Unable to listen on socket %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
01454       close(ast_socket);
01455       ast_socket = -1;
01456       return -1;
01457    }
01458    if (ast_register_verbose(network_verboser)) {
01459       ast_log(LOG_WARNING, "Unable to register network verboser?\n");
01460    }
01461 
01462    if (ast_pthread_create_background(&lthread, NULL, listener, NULL)) {
01463       ast_log(LOG_WARNING, "Unable to create listener thread.\n");
01464       close(ast_socket);
01465       return -1;
01466    }
01467 
01468    if (!ast_strlen_zero(ast_config_AST_CTL_OWNER)) {
01469       struct passwd *pw;
01470       if ((pw = getpwnam(ast_config_AST_CTL_OWNER)) == NULL)
01471          ast_log(LOG_WARNING, "Unable to find uid of user %s\n", ast_config_AST_CTL_OWNER);
01472       else
01473          uid = pw->pw_uid;
01474    }
01475       
01476    if (!ast_strlen_zero(ast_config_AST_CTL_GROUP)) {
01477       struct group *grp;
01478       if ((grp = getgrnam(ast_config_AST_CTL_GROUP)) == NULL)
01479          ast_log(LOG_WARNING, "Unable to find gid of group %s\n", ast_config_AST_CTL_GROUP);
01480       else
01481          gid = grp->gr_gid;
01482    }
01483 
01484    if (chown(ast_config_AST_SOCKET, uid, gid) < 0)
01485       ast_log(LOG_WARNING, "Unable to change ownership of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
01486 
01487    if (!ast_strlen_zero(ast_config_AST_CTL_PERMISSIONS)) {
01488       int p1;
01489       mode_t p;
01490       sscanf(ast_config_AST_CTL_PERMISSIONS, "%30o", &p1);
01491       p = p1;
01492       if ((chmod(ast_config_AST_SOCKET, p)) < 0)
01493          ast_log(LOG_WARNING, "Unable to change file permissions of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
01494    }
01495 
01496    return 0;
01497 }
01498 
01499 static int ast_tryconnect(void)
01500 {
01501    struct sockaddr_un sunaddr;
01502    int res;
01503    ast_consock = socket(PF_LOCAL, SOCK_STREAM, 0);
01504    if (ast_consock < 0) {
01505       ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
01506       return 0;
01507    }
01508    memset(&sunaddr, 0, sizeof(sunaddr));
01509    sunaddr.sun_family = AF_LOCAL;
01510    ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
01511    res = connect(ast_consock, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
01512    if (res) {
01513       close(ast_consock);
01514       ast_consock = -1;
01515       return 0;
01516    } else
01517       return 1;
01518 }
01519 
01520 /*! \brief Urgent handler
01521 
01522  Called by soft_hangup to interrupt the poll, read, or other
01523  system call.  We don't actually need to do anything though.  
01524  Remember: Cannot EVER ast_log from within a signal handler 
01525  */
01526 static void _urg_handler(int num)
01527 {
01528    return;
01529 }
01530 
01531 static struct sigaction urg_handler = {
01532    .sa_handler = _urg_handler,
01533    .sa_flags = SA_RESTART,
01534 };
01535 
01536 static void _hup_handler(int num)
01537 {
01538    int a = 0, save_errno = errno;
01539    if (option_verbose > 1) 
01540       printf("Received HUP signal -- Reloading configs\n");
01541    if (restartnow)
01542       execvp(_argv[0], _argv);
01543    sig_flags.need_reload = 1;
01544    if (sig_alert_pipe[1] != -1) {
01545       if (write(sig_alert_pipe[1], &a, sizeof(a)) < 0) {
01546          fprintf(stderr, "hup_handler: write() failed: %s\n", strerror(errno));
01547       }
01548    }
01549    errno = save_errno;
01550 }
01551 
01552 static struct sigaction hup_handler = {
01553    .sa_handler = _hup_handler,
01554    .sa_flags = SA_RESTART,
01555 };
01556 
01557 static void _child_handler(int sig)
01558 {
01559    /* Must not ever ast_log or ast_verbose within signal handler */
01560    int n, status, save_errno = errno;
01561 
01562    /*
01563     * Reap all dead children -- not just one
01564     */
01565    for (n = 0; wait4(-1, &status, WNOHANG, NULL) > 0; n++)
01566       ;
01567    if (n == 0 && option_debug)   
01568       printf("Huh?  Child handler, but nobody there?\n");
01569    errno = save_errno;
01570 }
01571 
01572 static struct sigaction child_handler = {
01573    .sa_handler = _child_handler,
01574    .sa_flags = SA_RESTART,
01575 };
01576 
01577 /*! \brief Set maximum open files */
01578 static void set_ulimit(int value)
01579 {
01580    struct rlimit l = {0, 0};
01581    
01582    if (value <= 0) {
01583       ast_log(LOG_WARNING, "Unable to change max files open to invalid value %i\n",value);
01584       return;
01585    }
01586    
01587    l.rlim_cur = value;
01588    l.rlim_max = value;
01589    
01590    if (setrlimit(RLIMIT_NOFILE, &l)) {
01591       ast_log(LOG_WARNING, "Unable to disable core size resource limit: %s\n",strerror(errno));
01592       return;
01593    }
01594    
01595    ast_log(LOG_NOTICE, "Setting max files open to %d\n",value);
01596    
01597    return;
01598 }
01599 
01600 /*! \brief Set an X-term or screen title */
01601 static void set_title(char *text)
01602 {
01603    if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
01604       fprintf(stdout, "\033]2;%s\007", text);
01605 }
01606 
01607 static void set_icon(char *text)
01608 {
01609    if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
01610       fprintf(stdout, "\033]1;%s\007", text);
01611 }
01612 
01613 /*! \brief We set ourselves to a high priority, that we might pre-empt everything
01614    else.  If your PBX has heavy activity on it, this is a good thing.  */
01615 int ast_set_priority(int pri)
01616 {
01617    struct sched_param sched;
01618    memset(&sched, 0, sizeof(sched));
01619 #ifdef __linux__
01620    if (pri) {  
01621       sched.sched_priority = 10;
01622       if (sched_setscheduler(0, SCHED_RR, &sched)) {
01623          ast_log(LOG_WARNING, "Unable to set high priority\n");
01624          return -1;
01625       } else
01626          if (option_verbose)
01627             ast_verbose("Set to realtime thread\n");
01628    } else {
01629       sched.sched_priority = 0;
01630       /* According to the manpage, these parameters can never fail. */
01631       sched_setscheduler(0, SCHED_OTHER, &sched);
01632    }
01633 #else
01634    if (pri) {
01635       if (setpriority(PRIO_PROCESS, 0, -10) == -1) {
01636          ast_log(LOG_WARNING, "Unable to set high priority\n");
01637          return -1;
01638       } else
01639          if (option_verbose)
01640             ast_verbose("Set to high priority\n");
01641    } else {
01642       /* According to the manpage, these parameters can never fail. */
01643       setpriority(PRIO_PROCESS, 0, 0);
01644    }
01645 #endif
01646    return 0;
01647 }
01648 
01649 static void ast_run_atexits(void)
01650 {
01651    struct ast_atexit *ae;
01652    AST_RWLIST_RDLOCK(&atexits);
01653    AST_RWLIST_TRAVERSE(&atexits, ae, list) {
01654       if (ae->func) 
01655          ae->func();
01656    }
01657    AST_RWLIST_UNLOCK(&atexits);
01658 }
01659 
01660 static int can_safely_quit(shutdown_nice_t niceness, int restart);
01661 static void really_quit(int num, shutdown_nice_t niceness, int restart);
01662 
01663 static void quit_handler(int num, shutdown_nice_t niceness, int restart)
01664 {
01665    if (can_safely_quit(niceness, restart)) {
01666       really_quit(num, niceness, restart);
01667       /* No one gets here. */
01668    }
01669    /* It wasn't our time. */
01670 }
01671 
01672 static int can_safely_quit(shutdown_nice_t niceness, int restart)
01673 {
01674    /* Check if someone else isn't already doing this. */
01675    ast_mutex_lock(&safe_system_lock);
01676    if (shuttingdown != NOT_SHUTTING_DOWN && niceness >= shuttingdown) {
01677       /* Already in progress and other request was less nice. */
01678       ast_mutex_unlock(&safe_system_lock);
01679       ast_verbose("Ignoring asterisk %s request, already in progress.\n", restart ? "restart" : "shutdown");
01680       return 0;
01681    }
01682    shuttingdown = niceness;
01683    ast_mutex_unlock(&safe_system_lock);
01684 
01685    /* Try to get as many CDRs as possible submitted to the backend engines
01686     * (if in batch mode). really_quit happens to call it again when running
01687     * the atexit handlers, otherwise this would be a bit early. */
01688    ast_cdr_engine_term();
01689 
01690    if (niceness == SHUTDOWN_NORMAL) {
01691       time_t s, e;
01692       /* Begin shutdown routine, hanging up active channels */
01693       ast_begin_shutdown(1);
01694       if (option_verbose && ast_opt_console) {
01695          ast_verbose("Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
01696       }
01697       time(&s);
01698       for (;;) {
01699          time(&e);
01700          /* Wait up to 15 seconds for all channels to go away */
01701          if ((e - s) > 15 || !ast_undestroyed_channels() || shuttingdown != niceness) {
01702             break;
01703          }
01704          /* Sleep 1/10 of a second */
01705          usleep(100000);
01706       }
01707    } else if (niceness >= SHUTDOWN_NICE) {
01708       if (niceness != SHUTDOWN_REALLY_NICE) {
01709          ast_begin_shutdown(0);
01710       }
01711       if (option_verbose && ast_opt_console) {
01712          ast_verbose("Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt");
01713       }
01714       for (;;) {
01715          if (!ast_undestroyed_channels() || shuttingdown != niceness) {
01716             break;
01717          }
01718          sleep(1);
01719       }
01720    }
01721 
01722    /* Re-acquire lock and check if someone changed the niceness, in which
01723     * case someone else has taken over the shutdown. */
01724    ast_mutex_lock(&safe_system_lock);
01725    if (shuttingdown != niceness) {
01726       if (shuttingdown == NOT_SHUTTING_DOWN && option_verbose && ast_opt_console) {
01727          ast_verbose("Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
01728       }
01729       ast_mutex_unlock(&safe_system_lock);
01730       return 0;
01731    }
01732    shuttingdown = SHUTTING_DOWN;
01733    ast_mutex_unlock(&safe_system_lock);
01734 
01735    return 1;
01736 }
01737 
01738 static void really_quit(int num, shutdown_nice_t niceness, int restart)
01739 {
01740    if (niceness >= SHUTDOWN_NICE) {
01741       ast_module_shutdown();
01742    }
01743 
01744    if (ast_opt_console || (ast_opt_remote && !ast_opt_exec)) {
01745       char filename[80] = "";
01746       if (getenv("HOME")) {
01747          snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
01748       }
01749       if (!ast_strlen_zero(filename)) {
01750          ast_el_write_history(filename);
01751       }
01752       if (consolethread == AST_PTHREADT_NULL || consolethread == pthread_self()) {
01753          /* Only end if we are the consolethread, otherwise there's a race with that thread. */
01754          if (el != NULL) {
01755             el_end(el);
01756          }
01757          if (el_hist != NULL) {
01758             history_end(el_hist);
01759          }
01760       } else if (mon_sig_flags == pthread_self()) {
01761          if (consolethread != AST_PTHREADT_NULL) {
01762             pthread_kill(consolethread, SIGURG);
01763          }
01764       }
01765    }
01766    if (option_verbose)
01767       ast_verbose("Executing last minute cleanups\n");
01768    ast_run_atexits();
01769    /* Called on exit */
01770    if (option_verbose && ast_opt_console)
01771       ast_verbose("Asterisk %s ending (%d).\n", ast_active_channels() ? "uncleanly" : "cleanly", num);
01772    ast_debug(1, "Asterisk ending (%d).\n", num);
01773    manager_event(EVENT_FLAG_SYSTEM, "Shutdown", "Shutdown: %s\r\nRestart: %s\r\n", ast_active_channels() ? "Uncleanly" : "Cleanly", restart ? "True" : "False");
01774    if (ast_socket > -1) {
01775       pthread_cancel(lthread);
01776       close(ast_socket);
01777       ast_socket = -1;
01778       unlink(ast_config_AST_SOCKET);
01779    }
01780    if (ast_consock > -1)
01781       close(ast_consock);
01782    if (!ast_opt_remote)
01783       unlink(ast_config_AST_PID);
01784    printf("%s", term_quit());
01785    if (restart) {
01786       int i;
01787       if (option_verbose || ast_opt_console)
01788          ast_verbose("Preparing for Asterisk restart...\n");
01789       /* Mark all FD's for closing on exec */
01790       for (i = 3; i < 32768; i++) {
01791          fcntl(i, F_SETFD, FD_CLOEXEC);
01792       }
01793       if (option_verbose || ast_opt_console)
01794          ast_verbose("Asterisk is now restarting...\n");
01795       restartnow = 1;
01796 
01797       /* close logger */
01798       close_logger();
01799 
01800       /* If there is a consolethread running send it a SIGHUP 
01801          so it can execvp, otherwise we can do it ourselves */
01802       if ((consolethread != AST_PTHREADT_NULL) && (consolethread != pthread_self())) {
01803          pthread_kill(consolethread, SIGHUP);
01804          /* Give the signal handler some time to complete */
01805          sleep(2);
01806       } else
01807          execvp(_argv[0], _argv);
01808    
01809    } else {
01810       /* close logger */
01811       close_logger();
01812    }
01813 
01814    exit(0);
01815 }
01816 
01817 static void __quit_handler(int num)
01818 {
01819    int a = 0;
01820    sig_flags.need_quit = 1;
01821    if (sig_alert_pipe[1] != -1) {
01822       if (write(sig_alert_pipe[1], &a, sizeof(a)) < 0) {
01823          fprintf(stderr, "quit_handler: write() failed: %s\n", strerror(errno));
01824       }
01825    }
01826    /* There is no need to restore the signal handler here, since the app
01827     * is going to exit */
01828 }
01829 
01830 static void __remote_quit_handler(int num)
01831 {
01832    sig_flags.need_quit = 1;
01833 }
01834 
01835 static const char *fix_header(char *outbuf, int maxout, const char *s, char *cmp)
01836 {
01837    const char *c;
01838 
01839    /* Check for verboser preamble */
01840    if (*s == 127) {
01841       s++;
01842    }
01843 
01844    if (!strncmp(s, cmp, strlen(cmp))) {
01845       c = s + strlen(cmp);
01846       term_color(outbuf, cmp, COLOR_GRAY, 0, maxout);
01847       return c;
01848    }
01849    return NULL;
01850 }
01851 
01852 static void console_verboser(const char *s)
01853 {
01854    char tmp[80];
01855    const char *c = NULL;
01856 
01857    if ((c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_4)) ||
01858        (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_3)) ||
01859        (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_2)) ||
01860        (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_1))) {
01861       fputs(tmp, stdout);
01862       fputs(c, stdout);
01863    } else {
01864       if (*s == 127) {
01865          s++;
01866       }
01867       fputs(s, stdout);
01868    }
01869 
01870    fflush(stdout);
01871    
01872    /* Wake up a poll()ing console */
01873    if (ast_opt_console && consolethread != AST_PTHREADT_NULL)
01874       pthread_kill(consolethread, SIGURG);
01875 }
01876 
01877 static int ast_all_zeros(char *s)
01878 {
01879    while (*s) {
01880       if (*s > 32)
01881          return 0;
01882       s++;  
01883    }
01884    return 1;
01885 }
01886 
01887 static void consolehandler(char *s)
01888 {
01889    printf("%s", term_end());
01890    fflush(stdout);
01891 
01892    /* Called when readline data is available */
01893    if (!ast_all_zeros(s))
01894       ast_el_add_history(s);
01895    /* The real handler for bang */
01896    if (s[0] == '!') {
01897       if (s[1])
01898          ast_safe_system(s+1);
01899       else
01900          ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
01901    } else 
01902       ast_cli_command(STDOUT_FILENO, s);
01903 }
01904 
01905 static int remoteconsolehandler(char *s)
01906 {
01907    int ret = 0;
01908 
01909    /* Called when readline data is available */
01910    if (!ast_all_zeros(s))
01911       ast_el_add_history(s);
01912    /* The real handler for bang */
01913    if (s[0] == '!') {
01914       if (s[1])
01915          ast_safe_system(s+1);
01916       else
01917          ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
01918       ret = 1;
01919    }
01920    if ((strncasecmp(s, "quit", 4) == 0 || strncasecmp(s, "exit", 4) == 0) &&
01921        (s[4] == '\0' || isspace(s[4]))) {
01922       quit_handler(0, SHUTDOWN_FAST, 0);
01923       ret = 1;
01924    }
01925 
01926    return ret;
01927 }
01928 
01929 static char *handle_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01930 {
01931    switch (cmd) {
01932    case CLI_INIT:
01933       e->command = "core show version";
01934       e->usage = 
01935          "Usage: core show version\n"
01936          "       Shows Asterisk version information.\n";
01937       return NULL;
01938    case CLI_GENERATE:
01939       return NULL;
01940    }
01941 
01942    if (a->argc != 3)
01943       return CLI_SHOWUSAGE;
01944    ast_cli(a->fd, "Asterisk %s built by %s @ %s on a %s running %s on %s\n",
01945       ast_get_version(), ast_build_user, ast_build_hostname,
01946       ast_build_machine, ast_build_os, ast_build_date);
01947    return CLI_SUCCESS;
01948 }
01949 
01950 #if 0
01951 static int handle_quit(int fd, int argc, char *argv[])
01952 {
01953    if (argc != 1)
01954       return RESULT_SHOWUSAGE;
01955    quit_handler(0, SHUTDOWN_NORMAL, 0);
01956    return RESULT_SUCCESS;
01957 }
01958 #endif
01959 
01960 static char *handle_stop_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01961 {
01962    switch (cmd) {
01963    case CLI_INIT:
01964       e->command = "core stop now";
01965       e->usage = 
01966          "Usage: core stop now\n"
01967          "       Shuts down a running Asterisk immediately, hanging up all active calls .\n";
01968       return NULL;
01969    case CLI_GENERATE:
01970       return NULL;
01971    }
01972 
01973    if (a->argc != e->args)
01974       return CLI_SHOWUSAGE;
01975    quit_handler(0, SHUTDOWN_NORMAL, 0 /* not restart */);
01976    return CLI_SUCCESS;
01977 }
01978 
01979 static char *handle_stop_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01980 {
01981    switch (cmd) {
01982    case CLI_INIT:
01983       e->command = "core stop gracefully";
01984       e->usage = 
01985          "Usage: core stop gracefully\n"
01986          "       Causes Asterisk to not accept new calls, and exit when all\n"
01987          "       active calls have terminated normally.\n";
01988       return NULL;
01989    case CLI_GENERATE:
01990       return NULL;
01991    }
01992 
01993    if (a->argc != e->args)
01994       return CLI_SHOWUSAGE;
01995    quit_handler(0, SHUTDOWN_NICE, 0 /* no restart */);
01996    return CLI_SUCCESS;
01997 }
01998 
01999 static char *handle_stop_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02000 {
02001    switch (cmd) {
02002    case CLI_INIT:
02003       e->command = "core stop when convenient";
02004       e->usage = 
02005          "Usage: core stop when convenient\n"
02006          "       Causes Asterisk to perform a shutdown when all active calls have ended.\n";
02007       return NULL;
02008    case CLI_GENERATE:
02009       return NULL;
02010    }
02011 
02012    if (a->argc != e->args)
02013       return CLI_SHOWUSAGE;
02014    ast_cli(a->fd, "Waiting for inactivity to perform halt\n");
02015    quit_handler(0, SHUTDOWN_REALLY_NICE, 0 /* don't restart */);
02016    return CLI_SUCCESS;
02017 }
02018 
02019 static char *handle_restart_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02020 {
02021    switch (cmd) {
02022    case CLI_INIT:
02023       e->command = "core restart now";
02024       e->usage = 
02025          "Usage: core restart now\n"
02026          "       Causes Asterisk to hangup all calls and exec() itself performing a cold\n"
02027          "       restart.\n";
02028       return NULL;
02029    case CLI_GENERATE:
02030       return NULL;
02031    }
02032 
02033    if (a->argc != e->args)
02034       return CLI_SHOWUSAGE;
02035    quit_handler(0, SHUTDOWN_NORMAL, 1 /* restart */);
02036    return CLI_SUCCESS;
02037 }
02038 
02039 static char *handle_restart_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02040 {
02041    switch (cmd) {
02042    case CLI_INIT:
02043       e->command = "core restart gracefully";
02044       e->usage = 
02045          "Usage: core restart gracefully\n"
02046          "       Causes Asterisk to stop accepting new calls and exec() itself performing a cold\n"
02047          "       restart when all active calls have ended.\n";
02048       return NULL;
02049    case CLI_GENERATE:
02050       return NULL;
02051    }
02052 
02053    if (a->argc != e->args)
02054       return CLI_SHOWUSAGE;
02055    quit_handler(0, SHUTDOWN_NICE, 1 /* restart */);
02056    return CLI_SUCCESS;
02057 }
02058 
02059 static char *handle_restart_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02060 {
02061    switch (cmd) {
02062    case CLI_INIT:
02063       e->command = "core restart when convenient";
02064       e->usage = 
02065          "Usage: core restart when convenient\n"
02066          "       Causes Asterisk to perform a cold restart when all active calls have ended.\n";
02067       return NULL;
02068    case CLI_GENERATE:
02069       return NULL;
02070    }
02071 
02072    if (a->argc != e->args)
02073       return CLI_SHOWUSAGE;
02074    ast_cli(a->fd, "Waiting for inactivity to perform restart\n");
02075    quit_handler(0, SHUTDOWN_REALLY_NICE, 1 /* restart */);
02076    return CLI_SUCCESS;
02077 }
02078 
02079 static char *handle_abort_shutdown(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02080 {
02081    int aborting_shutdown = 0;
02082 
02083    switch (cmd) {
02084    case CLI_INIT:
02085       e->command = "core abort shutdown";
02086       e->usage = 
02087          "Usage: core abort shutdown\n"
02088          "       Causes Asterisk to abort an executing shutdown or restart, and resume normal\n"
02089          "       call operations.\n";
02090       return NULL;
02091    case CLI_GENERATE:
02092       return NULL;
02093    }
02094 
02095    if (a->argc != e->args)
02096       return CLI_SHOWUSAGE;
02097 
02098    ast_mutex_lock(&safe_system_lock);
02099    if (shuttingdown >= SHUTDOWN_FAST) {
02100       aborting_shutdown = 1;
02101       shuttingdown = NOT_SHUTTING_DOWN;
02102    }
02103    ast_mutex_unlock(&safe_system_lock);
02104 
02105    if (aborting_shutdown) {
02106       ast_cancel_shutdown();
02107    }
02108    return CLI_SUCCESS;
02109 }
02110 
02111 static char *handle_bang(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02112 {
02113    switch (cmd) {
02114    case CLI_INIT:
02115       e->command = "!";
02116       e->usage = 
02117          "Usage: !<command>\n"
02118          "       Executes a given shell command\n";
02119       return NULL;
02120    case CLI_GENERATE:
02121       return NULL;
02122    }
02123 
02124    return CLI_SUCCESS;
02125 }
02126 static const char warranty_lines[] = {
02127    "\n"
02128    "            NO WARRANTY\n"
02129    "\n"
02130    "BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n"
02131    "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN\n"
02132    "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n"
02133    "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n"
02134    "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
02135    "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS\n"
02136    "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE\n"
02137    "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n"
02138    "REPAIR OR CORRECTION.\n"
02139    "\n"
02140    "IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
02141    "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n"
02142    "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n"
02143    "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n"
02144    "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n"
02145    "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n"
02146    "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n"
02147    "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n"
02148    "POSSIBILITY OF SUCH DAMAGES.\n"
02149 };
02150 
02151 static char *show_warranty(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02152 {
02153    switch (cmd) {
02154    case CLI_INIT:
02155       e->command = "core show warranty";
02156       e->usage = 
02157          "Usage: core show warranty\n"
02158          "       Shows the warranty (if any) for this copy of Asterisk.\n";
02159       return NULL;
02160    case CLI_GENERATE:
02161       return NULL;
02162    }
02163 
02164    ast_cli(a->fd, "%s", warranty_lines);
02165 
02166    return CLI_SUCCESS;
02167 }
02168 
02169 static const char license_lines[] = {
02170    "\n"
02171    "This program is free software; you can redistribute it and/or modify\n"
02172    "it under the terms of the GNU General Public License version 2 as\n"
02173    "published by the Free Software Foundation.\n"
02174    "\n"
02175    "This program also contains components licensed under other licenses.\n"
02176    "They include:\n"
02177    "\n"
02178    "This program is distributed in the hope that it will be useful,\n"
02179    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
02180    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
02181    "GNU General Public License for more details.\n"
02182    "\n"
02183    "You should have received a copy of the GNU General Public License\n"
02184    "along with this program; if not, write to the Free Software\n"
02185    "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n"
02186 };
02187 
02188 static char *show_license(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02189 {
02190    switch (cmd) {
02191    case CLI_INIT:
02192       e->command = "core show license";
02193       e->usage = 
02194          "Usage: core show license\n"
02195          "       Shows the license(s) for this copy of Asterisk.\n";
02196       return NULL;
02197    case CLI_GENERATE:
02198       return NULL;
02199    }
02200 
02201    ast_cli(a->fd, "%s", license_lines);
02202 
02203    return CLI_SUCCESS;
02204 }
02205 
02206 #define ASTERISK_PROMPT "*CLI> "
02207 
02208 #define ASTERISK_PROMPT2 "%s*CLI> "
02209 
02210 static struct ast_cli_entry cli_asterisk[] = {
02211    AST_CLI_DEFINE(handle_abort_shutdown, "Cancel a running shutdown"),
02212    AST_CLI_DEFINE(handle_stop_now, "Shut down Asterisk immediately"),
02213    AST_CLI_DEFINE(handle_stop_gracefully, "Gracefully shut down Asterisk"),
02214    AST_CLI_DEFINE(handle_stop_when_convenient, "Shut down Asterisk at empty call volume"),
02215    AST_CLI_DEFINE(handle_restart_now, "Restart Asterisk immediately"), 
02216    AST_CLI_DEFINE(handle_restart_gracefully, "Restart Asterisk gracefully"),
02217    AST_CLI_DEFINE(handle_restart_when_convenient, "Restart Asterisk at empty call volume"),
02218    AST_CLI_DEFINE(show_warranty, "Show the warranty (if any) for this copy of Asterisk"),
02219    AST_CLI_DEFINE(show_license, "Show the license(s) for this copy of Asterisk"),
02220    AST_CLI_DEFINE(handle_version, "Display version info"),
02221    AST_CLI_DEFINE(handle_bang, "Execute a shell command"),
02222 #if !defined(LOW_MEMORY)
02223    AST_CLI_DEFINE(handle_show_version_files, "List versions of files used to build Asterisk"),
02224    AST_CLI_DEFINE(handle_show_threads, "Show running threads"),
02225 #if defined(HAVE_SYSINFO) || defined(HAVE_SYSCTL)
02226    AST_CLI_DEFINE(handle_show_sysinfo, "Show System Information"),
02227 #endif
02228    AST_CLI_DEFINE(handle_show_profile, "Display profiling info"),
02229    AST_CLI_DEFINE(handle_show_settings, "Show some core settings"),
02230    AST_CLI_DEFINE(handle_clear_profile, "Clear profiling info"),
02231 #endif /* ! LOW_MEMORY */
02232 };
02233 
02234 static int ast_el_read_char(EditLine *editline, char *cp)
02235 {
02236    int num_read = 0;
02237    int lastpos = 0;
02238    struct pollfd fds[2];
02239    int res;
02240    int max;
02241 #define EL_BUF_SIZE 512
02242    char buf[EL_BUF_SIZE];
02243 
02244    for (;;) {
02245       max = 1;
02246       fds[0].fd = ast_consock;
02247       fds[0].events = POLLIN;
02248       if (!ast_opt_exec) {
02249          fds[1].fd = STDIN_FILENO;
02250          fds[1].events = POLLIN;
02251          max++;
02252       }
02253       res = ast_poll(fds, max, -1);
02254       if (res < 0) {
02255          if (sig_flags.need_quit || sig_flags.need_quit_handler)
02256             break;
02257          if (errno == EINTR)
02258             continue;
02259          ast_log(LOG_ERROR, "poll failed: %s\n", strerror(errno));
02260          break;
02261       }
02262 
02263       if (!ast_opt_exec && fds[1].revents) {
02264          num_read = read(STDIN_FILENO, cp, 1);
02265          if (num_read < 1) {
02266             break;
02267          } else 
02268             return (num_read);
02269       }
02270       if (fds[0].revents) {
02271          char *tmp;
02272          res = read(ast_consock, buf, sizeof(buf) - 1);
02273          /* if the remote side disappears exit */
02274          if (res < 1) {
02275             fprintf(stderr, "\nDisconnected from Asterisk server\n");
02276             if (!ast_opt_reconnect) {
02277                quit_handler(0, SHUTDOWN_FAST, 0);
02278             } else {
02279                int tries;
02280                int reconnects_per_second = 20;
02281                fprintf(stderr, "Attempting to reconnect for 30 seconds\n");
02282                for (tries = 0; tries < 30 * reconnects_per_second; tries++) {
02283                   if (ast_tryconnect()) {
02284                      fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries);
02285                      printf("%s", term_quit());
02286                      WELCOME_MESSAGE;
02287                      if (!ast_opt_mute)
02288                         fdsend(ast_consock, "logger mute silent");
02289                      else 
02290                         printf("log and verbose output currently muted ('logger mute' to unmute)\n");
02291                      break;
02292                   } else
02293                      usleep(1000000 / reconnects_per_second);
02294                }
02295                if (tries >= 30 * reconnects_per_second) {
02296                   fprintf(stderr, "Failed to reconnect for 30 seconds.  Quitting.\n");
02297                   quit_handler(0, SHUTDOWN_FAST, 0);
02298                }
02299             }
02300             continue;
02301          }
02302 
02303          buf[res] = '\0';
02304 
02305          /* Strip preamble from asynchronous events, too */
02306          for (tmp = buf; *tmp; tmp++) {
02307             if (*tmp == 127) {
02308                memmove(tmp, tmp + 1, strlen(tmp));
02309                tmp--;
02310                res--;
02311             }
02312          }
02313 
02314          /* Write over the CLI prompt */
02315          if (!ast_opt_exec && !lastpos) {
02316             if (write(STDOUT_FILENO, "\r", 5) < 0) {
02317             }
02318          }
02319          if (write(STDOUT_FILENO, buf, res) < 0) {
02320          }
02321          if ((res < EL_BUF_SIZE - 1) && ((buf[res-1] == '\n') || (buf[res-2] == '\n'))) {
02322             *cp = CC_REFRESH;
02323             return(1);
02324          } else
02325             lastpos = 1;
02326       }
02327    }
02328 
02329    *cp = '\0';
02330    return (0);
02331 }
02332 
02333 static struct ast_str *prompt = NULL;
02334 
02335 static char *cli_prompt(EditLine *editline)
02336 {
02337    char tmp[100];
02338    char *pfmt;
02339    int color_used = 0;
02340    static int cli_prompt_changes = 0;
02341    char term_code[20];
02342    struct passwd *pw;
02343    struct group *gr;
02344 
02345    if (prompt == NULL) {
02346       prompt = ast_str_create(100);
02347    } else if (!cli_prompt_changes) {
02348       return ast_str_buffer(prompt);
02349    } else {
02350       ast_str_reset(prompt);
02351    }
02352 
02353    if ((pfmt = getenv("ASTERISK_PROMPT"))) {
02354       char *t = pfmt;
02355       struct timeval ts = ast_tvnow();
02356       while (*t != '\0') {
02357          if (*t == '%') {
02358             char hostname[MAXHOSTNAMELEN] = "";
02359             int i, which;
02360             struct ast_tm tm = { 0, };
02361             int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
02362 
02363             t++;
02364             switch (*t) {
02365             case 'C': /* color */
02366                t++;
02367                if (sscanf(t, "%30d;%30d%n", &fgcolor, &bgcolor, &i) == 2) {
02368                   ast_str_append(&prompt, 0, "%s", term_color_code(term_code, fgcolor, bgcolor, sizeof(term_code)));
02369                   t += i - 1;
02370                } else if (sscanf(t, "%30d%n", &fgcolor, &i) == 1) {
02371                   ast_str_append(&prompt, 0, "%s", term_color_code(term_code, fgcolor, 0, sizeof(term_code)));
02372                   t += i - 1;
02373                }
02374 
02375                /* If the color has been reset correctly, then there's no need to reset it later */
02376                color_used = ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) ? 0 : 1;
02377                break;
02378             case 'd': /* date */
02379                if (ast_localtime(&ts, &tm, NULL)) {
02380                   ast_strftime(tmp, sizeof(tmp), "%Y-%m-%d", &tm);
02381                   ast_str_append(&prompt, 0, "%s", tmp);
02382                   cli_prompt_changes++;
02383                }
02384                break;
02385             case 'g': /* group */
02386                if ((gr = getgrgid(getgid()))) {
02387                   ast_str_append(&prompt, 0, "%s", gr->gr_name);
02388                }
02389                break;
02390             case 'h': /* hostname */
02391                if (!gethostname(hostname, sizeof(hostname) - 1)) {
02392                   ast_str_append(&prompt, 0, "%s", hostname);
02393                } else {
02394                   ast_str_append(&prompt, 0, "%s", "localhost");
02395                }
02396                break;
02397             case 'H': /* short hostname */
02398                if (!gethostname(hostname, sizeof(hostname) - 1)) {
02399                   char *dotptr;
02400                   if ((dotptr = strchr(hostname, '.'))) {
02401                      *dotptr = '\0';
02402                   }
02403                   ast_str_append(&prompt, 0, "%s", hostname);
02404                } else {
02405                   ast_str_append(&prompt, 0, "%s", "localhost");
02406                }
02407                break;
02408 #ifdef HAVE_GETLOADAVG
02409             case 'l': /* load avg */
02410                t++;
02411                if (sscanf(t, "%30d", &which) == 1 && which > 0 && which <= 3) {
02412                   double list[3];
02413                   getloadavg(list, 3);
02414                   ast_str_append(&prompt, 0, "%.2f", list[which - 1]);
02415                   cli_prompt_changes++;
02416                }
02417                break;
02418 #endif
02419             case 's': /* Asterisk system name (from asterisk.conf) */
02420                ast_str_append(&prompt, 0, "%s", ast_config_AST_SYSTEM_NAME);
02421                break;
02422             case 't': /* time */
02423                if (ast_localtime(&ts, &tm, NULL)) {
02424                   ast_strftime(tmp, sizeof(tmp), "%H:%M:%S", &tm);
02425                   ast_str_append(&prompt, 0, "%s", tmp);
02426                   cli_prompt_changes++;
02427                }
02428                break;
02429             case 'u': /* username */
02430                if ((pw = getpwuid(getuid()))) {
02431                   ast_str_append(&prompt, 0, "%s", pw->pw_name);
02432                }
02433                break;
02434             case '#': /* process console or remote? */
02435                ast_str_append(&prompt, 0, "%c", ast_opt_remote ? '>' : '#');
02436                break;
02437             case '%': /* literal % */
02438                ast_str_append(&prompt, 0, "%c", '%');
02439                break;
02440             case '\0': /* % is last character - prevent bug */
02441                t--;
02442                break;
02443             }
02444          } else {
02445             ast_str_append(&prompt, 0, "%c", *t);
02446          }
02447          t++;
02448       }
02449       if (color_used) {
02450          /* Force colors back to normal at end */
02451          ast_str_append(&prompt, 0, "%s", term_color_code(term_code, 0, 0, sizeof(term_code)));
02452       }
02453    } else if (remotehostname) {
02454       ast_str_set(&prompt, 0, ASTERISK_PROMPT2, remotehostname);
02455    } else {
02456       ast_str_set(&prompt, 0, "%s", ASTERISK_PROMPT);
02457    }
02458 
02459    return ast_str_buffer(prompt);   
02460 }
02461 
02462 static char **ast_el_strtoarr(char *buf)
02463 {
02464    char **match_list = NULL, **match_list_tmp, *retstr;
02465    size_t match_list_len;
02466    int matches = 0;
02467 
02468    match_list_len = 1;
02469    while ( (retstr = strsep(&buf, " ")) != NULL) {
02470 
02471       if (!strcmp(retstr, AST_CLI_COMPLETE_EOF))
02472          break;
02473       if (matches + 1 >= match_list_len) {
02474          match_list_len <<= 1;
02475          if ((match_list_tmp = ast_realloc(match_list, match_list_len * sizeof(char *)))) {
02476             match_list = match_list_tmp;
02477          } else {
02478             if (match_list)
02479                ast_free(match_list);
02480             return (char **) NULL;
02481          }
02482       }
02483 
02484       match_list[matches++] = ast_strdup(retstr);
02485    }
02486 
02487    if (!match_list)
02488       return (char **) NULL;
02489 
02490    if (matches >= match_list_len) {
02491       if ((match_list_tmp = ast_realloc(match_list, (match_list_len + 1) * sizeof(char *)))) {
02492          match_list = match_list_tmp;
02493       } else {
02494          if (match_list)
02495             ast_free(match_list);
02496          return (char **) NULL;
02497       }
02498    }
02499 
02500    match_list[matches] = (char *) NULL;
02501 
02502    return match_list;
02503 }
02504 
02505 static int ast_el_sort_compare(const void *i1, const void *i2)
02506 {
02507    char *s1, *s2;
02508 
02509    s1 = ((char **)i1)[0];
02510    s2 = ((char **)i2)[0];
02511 
02512    return strcasecmp(s1, s2);
02513 }
02514 
02515 static int ast_cli_display_match_list(char **matches, int len, int max)
02516 {
02517    int i, idx, limit, count;
02518    int screenwidth = 0;
02519    int numoutput = 0, numoutputline = 0;
02520 
02521    screenwidth = ast_get_termcols(STDOUT_FILENO);
02522 
02523    /* find out how many entries can be put on one line, with two spaces between strings */
02524    limit = screenwidth / (max + 2);
02525    if (limit == 0)
02526       limit = 1;
02527 
02528    /* how many lines of output */
02529    count = len / limit;
02530    if (count * limit < len)
02531       count++;
02532 
02533    idx = 1;
02534 
02535    qsort(&matches[0], (size_t)(len), sizeof(char *), ast_el_sort_compare);
02536 
02537    for (; count > 0; count--) {
02538       numoutputline = 0;
02539       for (i = 0; i < limit && matches[idx]; i++, idx++) {
02540 
02541          /* Don't print dupes */
02542          if ( (matches[idx+1] != NULL && strcmp(matches[idx], matches[idx+1]) == 0 ) ) {
02543             i--;
02544             ast_free(matches[idx]);
02545             matches[idx] = NULL;
02546             continue;
02547          }
02548 
02549          numoutput++;
02550          numoutputline++;
02551          fprintf(stdout, "%-*s  ", max, matches[idx]);
02552          ast_free(matches[idx]);
02553          matches[idx] = NULL;
02554       }
02555       if (numoutputline > 0)
02556          fprintf(stdout, "\n");
02557    }
02558 
02559    return numoutput;
02560 }
02561 
02562 
02563 static char *cli_complete(EditLine *editline, int ch)
02564 {
02565    int len = 0;
02566    char *ptr;
02567    int nummatches = 0;
02568    char **matches;
02569    int retval = CC_ERROR;
02570    char buf[2048], savechr;
02571    int res;
02572 
02573    LineInfo *lf = (LineInfo *)el_line(editline);
02574 
02575    savechr = *(char *)lf->cursor;
02576    *(char *)lf->cursor = '\0';
02577    ptr = (char *)lf->cursor;
02578    if (ptr) {
02579       while (ptr > lf->buffer) {
02580          if (isspace(*ptr)) {
02581             ptr++;
02582             break;
02583          }
02584          ptr--;
02585       }
02586    }
02587 
02588    len = lf->cursor - ptr;
02589 
02590    if (ast_opt_remote) {
02591       snprintf(buf, sizeof(buf), "_COMMAND NUMMATCHES \"%s\" \"%s\"", lf->buffer, ptr); 
02592       fdsend(ast_consock, buf);
02593       if ((res = read(ast_consock, buf, sizeof(buf) - 1)) < 0) {
02594          return (char*)(CC_ERROR);
02595       }
02596       buf[res] = '\0';
02597       nummatches = atoi(buf);
02598 
02599       if (nummatches > 0) {
02600          char *mbuf;
02601          int mlen = 0, maxmbuf = 2048;
02602          /* Start with a 2048 byte buffer */       
02603          if (!(mbuf = ast_malloc(maxmbuf))) {
02604             lf->cursor[0] = savechr;
02605             return (char *)(CC_ERROR);
02606          }
02607          snprintf(buf, sizeof(buf), "_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr); 
02608          fdsend(ast_consock, buf);
02609          res = 0;
02610          mbuf[0] = '\0';
02611          while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {
02612             if (mlen + 1024 > maxmbuf) {
02613                /* Every step increment buffer 1024 bytes */
02614                maxmbuf += 1024;              
02615                if (!(mbuf = ast_realloc(mbuf, maxmbuf))) {
02616                   lf->cursor[0] = savechr;
02617                   return (char *)(CC_ERROR);
02618                }
02619             }
02620             /* Only read 1024 bytes at a time */
02621             res = read(ast_consock, mbuf + mlen, 1024);
02622             if (res > 0)
02623                mlen += res;
02624          }
02625          mbuf[mlen] = '\0';
02626 
02627          matches = ast_el_strtoarr(mbuf);
02628          ast_free(mbuf);
02629       } else
02630          matches = (char **) NULL;
02631    } else {
02632       char **p, *oldbuf=NULL;
02633       nummatches = 0;
02634       matches = ast_cli_completion_matches((char *)lf->buffer,ptr);
02635       for (p = matches; p && *p; p++) {
02636          if (!oldbuf || strcmp(*p,oldbuf))
02637             nummatches++;
02638          oldbuf = *p;
02639       }
02640    }
02641 
02642    if (matches) {
02643       int i;
02644       int matches_num, maxlen, match_len;
02645 
02646       if (matches[0][0] != '\0') {
02647          el_deletestr(editline, (int) len);
02648          el_insertstr(editline, matches[0]);
02649          retval = CC_REFRESH;
02650       }
02651 
02652       if (nummatches == 1) {
02653          /* Found an exact match */
02654          el_insertstr(editline, " ");
02655          retval = CC_REFRESH;
02656       } else {
02657          /* Must be more than one match */
02658          for (i = 1, maxlen = 0; matches[i]; i++) {
02659             match_len = strlen(matches[i]);
02660             if (match_len > maxlen)
02661                maxlen = match_len;
02662          }
02663          matches_num = i - 1;
02664          if (matches_num >1) {
02665             fprintf(stdout, "\n");
02666             ast_cli_display_match_list(matches, nummatches, maxlen);
02667             retval = CC_REDISPLAY;
02668          } else { 
02669             el_insertstr(editline," ");
02670             retval = CC_REFRESH;
02671          }
02672       }
02673       for (i = 0; matches[i]; i++)
02674          ast_free(matches[i]);
02675       ast_free(matches);
02676    }
02677 
02678    lf->cursor[0] = savechr;
02679 
02680    return (char *)(long)retval;
02681 }
02682 
02683 static int ast_el_initialize(void)
02684 {
02685    HistEvent ev;
02686    char *editor = getenv("AST_EDITOR");
02687 
02688    if (el != NULL)
02689       el_end(el);
02690    if (el_hist != NULL)
02691       history_end(el_hist);
02692 
02693    el = el_init("asterisk", stdin, stdout, stderr);
02694    el_set(el, EL_PROMPT, cli_prompt);
02695 
02696    el_set(el, EL_EDITMODE, 1);      
02697    el_set(el, EL_EDITOR, editor ? editor : "emacs");     
02698    el_hist = history_init();
02699    if (!el || !el_hist)
02700       return -1;
02701 
02702    /* setup history with 100 entries */
02703    history(el_hist, &ev, H_SETSIZE, 100);
02704 
02705    el_set(el, EL_HIST, history, el_hist);
02706 
02707    el_set(el, EL_ADDFN, "ed-complete", "Complete argument", cli_complete);
02708    /* Bind <tab> to command completion */
02709    el_set(el, EL_BIND, "^I", "ed-complete", NULL);
02710    /* Bind ? to command completion */
02711    el_set(el, EL_BIND, "?", "ed-complete", NULL);
02712    /* Bind ^D to redisplay */
02713    el_set(el, EL_BIND, "^D", "ed-redisplay", NULL);
02714 
02715    return 0;
02716 }
02717 
02718 #define MAX_HISTORY_COMMAND_LENGTH 256
02719 
02720 static int ast_el_add_history(char *buf)
02721 {
02722    HistEvent ev;
02723 
02724    if (el_hist == NULL || el == NULL)
02725       ast_el_initialize();
02726    if (strlen(buf) > (MAX_HISTORY_COMMAND_LENGTH - 1))
02727       return 0;
02728    return (history(el_hist, &ev, H_ENTER, ast_strip(ast_strdupa(buf))));
02729 }
02730 
02731 static int ast_el_write_history(char *filename)
02732 {
02733    HistEvent ev;
02734 
02735    if (el_hist == NULL || el == NULL)
02736       ast_el_initialize();
02737 
02738    return (history(el_hist, &ev, H_SAVE, filename));
02739 }
02740 
02741 static int ast_el_read_history(char *filename)
02742 {
02743    HistEvent ev;
02744 
02745    if (el_hist == NULL || el == NULL)
02746       ast_el_initialize();
02747 
02748    return (history(el_hist, &ev, H_LOAD, filename));
02749 }
02750 
02751 static void ast_remotecontrol(char *data)
02752 {
02753    char buf[80];
02754    int res;
02755    char filename[80] = "";
02756    char *hostname;
02757    char *cpid;
02758    char *version;
02759    int pid;
02760    char *stringp = NULL;
02761 
02762    char *ebuf;
02763    int num = 0;
02764 
02765    memset(&sig_flags, 0, sizeof(sig_flags));
02766    signal(SIGINT, __remote_quit_handler);
02767    signal(SIGTERM, __remote_quit_handler);
02768    signal(SIGHUP, __remote_quit_handler);
02769 
02770    if (read(ast_consock, buf, sizeof(buf)) < 0) {
02771       ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno));
02772       return;
02773    }
02774    if (data) {
02775       char prefix[] = "cli quit after ";
02776       char *tmp = alloca(strlen(data) + strlen(prefix) + 1);
02777       sprintf(tmp, "%s%s", prefix, data);
02778       if (write(ast_consock, tmp, strlen(tmp) + 1) < 0) {
02779          ast_log(LOG_ERROR, "write() failed: %s\n", strerror(errno));
02780          if (sig_flags.need_quit || sig_flags.need_quit_handler) {
02781             return;
02782          }
02783       }
02784    }
02785    stringp = buf;
02786    hostname = strsep(&stringp, "/");
02787    cpid = strsep(&stringp, "/");
02788    version = strsep(&stringp, "\n");
02789    if (!version)
02790       version = "<Version Unknown>";
02791    stringp = hostname;
02792    strsep(&stringp, ".");
02793    if (cpid)
02794       pid = atoi(cpid);
02795    else
02796       pid = -1;
02797    if (!data) {
02798       char tmp[80];
02799       snprintf(tmp, sizeof(tmp), "core set verbose atleast %d", option_verbose);
02800       fdsend(ast_consock, tmp);
02801       snprintf(tmp, sizeof(tmp), "core set debug atleast %d", option_debug);
02802       fdsend(ast_consock, tmp);
02803       if (!ast_opt_mute)
02804          fdsend(ast_consock, "logger mute silent");
02805       else 
02806          printf("log and verbose output currently muted ('logger mute' to unmute)\n");
02807    }
02808 
02809    if (ast_opt_exec && data) {  /* hack to print output then exit if asterisk -rx is used */
02810       struct pollfd fds;
02811       fds.fd = ast_consock;
02812       fds.events = POLLIN;
02813       fds.revents = 0;
02814       while (ast_poll(&fds, 1, 60000) > 0) {
02815          char buffer[512] = "", *curline = buffer, *nextline;
02816          int not_written = 1;
02817 
02818          if (sig_flags.need_quit || sig_flags.need_quit_handler) {
02819             break;
02820          }
02821 
02822          if (read(ast_consock, buffer, sizeof(buffer) - 1) <= 0) {
02823             break;
02824          }
02825 
02826          do {
02827             if ((nextline = strchr(curline, '\n'))) {
02828                nextline++;
02829             } else {
02830                nextline = strchr(curline, '\0');
02831             }
02832 
02833             /* Skip verbose lines */
02834             if (*curline != 127) {
02835                not_written = 0;
02836                if (write(STDOUT_FILENO, curline, nextline - curline) < 0) {
02837                   ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
02838                }
02839             }
02840             curline = nextline;
02841          } while (!ast_strlen_zero(curline));
02842 
02843          /* No non-verbose output in 60 seconds. */
02844          if (not_written) {
02845             break;
02846          }
02847       }
02848       return;
02849    }
02850 
02851    ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid);
02852    remotehostname = hostname;
02853    if (getenv("HOME")) 
02854       snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
02855    if (el_hist == NULL || el == NULL)
02856       ast_el_initialize();
02857 
02858    el_set(el, EL_GETCFN, ast_el_read_char);
02859 
02860    if (!ast_strlen_zero(filename))
02861       ast_el_read_history(filename);
02862 
02863    for (;;) {
02864       ebuf = (char *)el_gets(el, &num);
02865 
02866       if (sig_flags.need_quit || sig_flags.need_quit_handler) {
02867          break;
02868       }
02869 
02870       if (!ebuf && write(1, "", 1) < 0)
02871          break;
02872 
02873       if (!ast_strlen_zero(ebuf)) {
02874          if (ebuf[strlen(ebuf)-1] == '\n')
02875             ebuf[strlen(ebuf)-1] = '\0';
02876          if (!remoteconsolehandler(ebuf)) {
02877             /* Strip preamble from output */
02878             char *temp;
02879             for (temp = ebuf; *temp; temp++) {
02880                if (*temp == 127) {
02881                   memmove(temp, temp + 1, strlen(temp));
02882                   temp--;
02883                }
02884             }
02885             res = write(ast_consock, ebuf, strlen(ebuf) + 1);
02886             if (res < 1) {
02887                ast_log(LOG_WARNING, "Unable to write: %s\n", strerror(errno));
02888                break;
02889             }
02890          }
02891       }
02892    }
02893    printf("\nDisconnected from Asterisk server\n");
02894 }
02895 
02896 static int show_version(void)
02897 {
02898    printf("Asterisk %s\n", ast_get_version());
02899    return 0;
02900 }
02901 
02902 static int show_cli_help(void)
02903 {
02904    printf("Asterisk %s, Copyright (C) 1999 - 2012, Digium, Inc. and others.\n", ast_get_version());
02905    printf("Usage: asterisk [OPTIONS]\n");
02906    printf("Valid Options:\n");
02907    printf("   -V              Display version number and exit\n");
02908    printf("   -C <configfile> Use an alternate configuration file\n");
02909    printf("   -G <group>      Run as a group other than the caller\n");
02910    printf("   -U <user>       Run as a user other than the caller\n");
02911    printf("   -c              Provide console CLI\n");
02912    printf("   -d              Enable extra debugging\n");
02913 #if HAVE_WORKING_FORK
02914    printf("   -f              Do not fork\n");
02915    printf("   -F              Always fork\n");
02916 #endif
02917    printf("   -g              Dump core in case of a crash\n");
02918    printf("   -h              This help screen\n");
02919    printf("   -i              Initialize crypto keys at startup\n");
02920    printf("   -I              Enable internal timing if DAHDI timer is available\n");
02921    printf("   -L <load>       Limit the maximum load average before rejecting new calls\n");
02922    printf("   -M <value>      Limit the maximum number of calls to the specified value\n");
02923    printf("   -m              Mute debugging and console output on the console\n");
02924    printf("   -n              Disable console colorization\n");
02925    printf("   -p              Run as pseudo-realtime thread\n");
02926    printf("   -q              Quiet mode (suppress output)\n");
02927    printf("   -r              Connect to Asterisk on this machine\n");
02928    printf("   -R              Same as -r, except attempt to reconnect if disconnected\n");
02929    printf("   -s <socket>     Connect to Asterisk via socket <socket> (only valid with -r)\n");
02930    printf("   -t              Record soundfiles in /var/tmp and move them where they\n");
02931    printf("                   belong after they are done\n");
02932    printf("   -T              Display the time in [Mmm dd hh:mm:ss] format for each line\n");
02933    printf("                   of output to the CLI\n");
02934    printf("   -v              Increase verbosity (multiple v's = more verbose)\n");
02935    printf("   -x <cmd>        Execute command <cmd> (implies -r)\n");
02936    printf("   -X              Execute includes by default (allows #exec in asterisk.conf)\n");
02937    printf("   -W              Adjust terminal colors to compensate for a light background\n");
02938    printf("\n");
02939    return 0;
02940 }
02941 
02942 static void ast_readconfig(void) 
02943 {
02944    struct ast_config *cfg;
02945    struct ast_variable *v;
02946    char *config = DEFAULT_CONFIG_FILE;
02947    char hostname[MAXHOSTNAMELEN] = "";
02948    struct ast_flags config_flags = { CONFIG_FLAG_NOREALTIME };
02949    struct {
02950       unsigned int dbdir:1;
02951       unsigned int keydir:1;
02952    } found = { 0, 0 };
02953 
02954    if (ast_opt_override_config) {
02955       cfg = ast_config_load2(ast_config_AST_CONFIG_FILE, "" /* core, can't reload */, config_flags);
02956       if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
02957          ast_log(LOG_WARNING, "Unable to open specified master config file '%s', using built-in defaults\n", ast_config_AST_CONFIG_FILE);
02958    } else 
02959       cfg = ast_config_load2(config, "" /* core, can't reload */, config_flags);
02960 
02961    /* init with buildtime config */
02962    ast_copy_string(cfg_paths.config_dir, DEFAULT_CONFIG_DIR, sizeof(cfg_paths.config_dir));
02963    ast_copy_string(cfg_paths.spool_dir, DEFAULT_SPOOL_DIR, sizeof(cfg_paths.spool_dir));
02964    ast_copy_string(cfg_paths.module_dir, DEFAULT_MODULE_DIR, sizeof(cfg_paths.module_dir));
02965    snprintf(cfg_paths.monitor_dir, sizeof(cfg_paths.monitor_dir), "%s/monitor", cfg_paths.spool_dir);
02966    ast_copy_string(cfg_paths.var_dir, DEFAULT_VAR_DIR, sizeof(cfg_paths.var_dir));
02967    ast_copy_string(cfg_paths.data_dir, DEFAULT_DATA_DIR, sizeof(cfg_paths.data_dir));
02968    ast_copy_string(cfg_paths.log_dir, DEFAULT_LOG_DIR, sizeof(cfg_paths.log_dir));
02969    ast_copy_string(cfg_paths.agi_dir, DEFAULT_AGI_DIR, sizeof(cfg_paths.agi_dir));
02970    ast_copy_string(cfg_paths.db_path, DEFAULT_DB, sizeof(cfg_paths.db_path));
02971    ast_copy_string(cfg_paths.key_dir, DEFAULT_KEY_DIR, sizeof(cfg_paths.key_dir));
02972    ast_copy_string(cfg_paths.pid_path, DEFAULT_PID, sizeof(cfg_paths.pid_path));
02973    ast_copy_string(cfg_paths.socket_path, DEFAULT_SOCKET, sizeof(cfg_paths.socket_path));
02974    ast_copy_string(cfg_paths.run_dir, DEFAULT_RUN_DIR, sizeof(cfg_paths.run_dir));
02975 
02976    ast_set_default_eid(&ast_eid_default);
02977 
02978    /* no asterisk.conf? no problem, use buildtime config! */
02979    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
02980       return;
02981    }
02982 
02983    for (v = ast_variable_browse(cfg, "files"); v; v = v->next) {
02984       if (!strcasecmp(v->name, "astctlpermissions"))
02985          ast_copy_string(ast_config_AST_CTL_PERMISSIONS, v->value, sizeof(ast_config_AST_CTL_PERMISSIONS));
02986       else if (!strcasecmp(v->name, "astctlowner"))
02987          ast_copy_string(ast_config_AST_CTL_OWNER, v->value, sizeof(ast_config_AST_CTL_OWNER));
02988       else if (!strcasecmp(v->name, "astctlgroup"))
02989          ast_copy_string(ast_config_AST_CTL_GROUP, v->value, sizeof(ast_config_AST_CTL_GROUP));
02990       else if (!strcasecmp(v->name, "astctl"))
02991          ast_copy_string(ast_config_AST_CTL, v->value, sizeof(ast_config_AST_CTL));
02992    }
02993 
02994    for (v = ast_variable_browse(cfg, "directories"); v; v = v->next) {
02995       if (!strcasecmp(v->name, "astetcdir")) {
02996          ast_copy_string(cfg_paths.config_dir, v->value, sizeof(cfg_paths.config_dir));
02997       } else if (!strcasecmp(v->name, "astspooldir")) {
02998          ast_copy_string(cfg_paths.spool_dir, v->value, sizeof(cfg_paths.spool_dir));
02999          snprintf(cfg_paths.monitor_dir, sizeof(cfg_paths.monitor_dir), "%s/monitor", v->value);
03000       } else if (!strcasecmp(v->name, "astvarlibdir")) {
03001          ast_copy_string(cfg_paths.var_dir, v->value, sizeof(cfg_paths.var_dir));
03002          if (!found.dbdir)
03003             snprintf(cfg_paths.db_path, sizeof(cfg_paths.db_path), "%s/astdb", v->value);
03004       } else if (!strcasecmp(v->name, "astdbdir")) {
03005          snprintf(cfg_paths.db_path, sizeof(cfg_paths.db_path), "%s/astdb", v->value);
03006          found.dbdir = 1;
03007       } else if (!strcasecmp(v->name, "astdatadir")) {
03008          ast_copy_string(cfg_paths.data_dir, v->value, sizeof(cfg_paths.data_dir));
03009          if (!found.keydir)
03010             snprintf(cfg_paths.key_dir, sizeof(cfg_paths.key_dir), "%s/keys", v->value);
03011       } else if (!strcasecmp(v->name, "astkeydir")) {
03012          snprintf(cfg_paths.key_dir, sizeof(cfg_paths.key_dir), "%s/keys", v->value);
03013          found.keydir = 1;
03014       } else if (!strcasecmp(v->name, "astlogdir")) {
03015          ast_copy_string(cfg_paths.log_dir, v->value, sizeof(cfg_paths.log_dir));
03016       } else if (!strcasecmp(v->name, "astagidir")) {
03017          ast_copy_string(cfg_paths.agi_dir, v->value, sizeof(cfg_paths.agi_dir));
03018       } else if (!strcasecmp(v->name, "astrundir")) {
03019          snprintf(cfg_paths.pid_path, sizeof(cfg_paths.pid_path), "%s/%s", v->value, "asterisk.pid");
03020          snprintf(cfg_paths.socket_path, sizeof(cfg_paths.socket_path), "%s/%s", v->value, ast_config_AST_CTL);
03021          ast_copy_string(cfg_paths.run_dir, v->value, sizeof(cfg_paths.run_dir));
03022       } else if (!strcasecmp(v->name, "astmoddir")) {
03023          ast_copy_string(cfg_paths.module_dir, v->value, sizeof(cfg_paths.module_dir));
03024       }
03025    }
03026 
03027    for (v = ast_variable_browse(cfg, "options"); v; v = v->next) {
03028       /* verbose level (-v at startup) */
03029       if (!strcasecmp(v->name, "verbose")) {
03030          option_verbose = atoi(v->value);
03031       /* whether or not to force timestamping in CLI verbose output. (-T at startup) */
03032       } else if (!strcasecmp(v->name, "timestamp")) {
03033          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_TIMESTAMP);
03034       /* whether or not to support #exec in config files */
03035       } else if (!strcasecmp(v->name, "execincludes")) {
03036          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_EXEC_INCLUDES);
03037       /* debug level (-d at startup) */
03038       } else if (!strcasecmp(v->name, "debug")) {
03039          option_debug = 0;
03040          if (sscanf(v->value, "%30d", &option_debug) != 1) {
03041             option_debug = ast_true(v->value);
03042          }
03043 #if HAVE_WORKING_FORK
03044       /* Disable forking (-f at startup) */
03045       } else if (!strcasecmp(v->name, "nofork")) {
03046          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_NO_FORK);
03047       /* Always fork, even if verbose or debug are enabled (-F at startup) */
03048       } else if (!strcasecmp(v->name, "alwaysfork")) {
03049          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_ALWAYS_FORK);
03050 #endif
03051       /* Run quietly (-q at startup ) */
03052       } else if (!strcasecmp(v->name, "quiet")) {
03053          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_QUIET);
03054       /* Run as console (-c at startup, implies nofork) */
03055       } else if (!strcasecmp(v->name, "console")) {
03056          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_CONSOLE);
03057       /* Run with high priority if the O/S permits (-p at startup) */
03058       } else if (!strcasecmp(v->name, "highpriority")) {
03059          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_HIGH_PRIORITY);
03060       /* Initialize RSA auth keys (IAX2) (-i at startup) */
03061       } else if (!strcasecmp(v->name, "initcrypto")) {
03062          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_INIT_KEYS);
03063       /* Disable ANSI colors for console (-c at startup) */
03064       } else if (!strcasecmp(v->name, "nocolor")) {
03065          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_NO_COLOR);
03066       /* Disable some usage warnings for picky people :p */
03067       } else if (!strcasecmp(v->name, "dontwarn")) {
03068          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_DONT_WARN);
03069       /* Dump core in case of crash (-g) */
03070       } else if (!strcasecmp(v->name, "dumpcore")) {
03071          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_DUMP_CORE);
03072       /* Cache recorded sound files to another directory during recording */
03073       } else if (!strcasecmp(v->name, "cache_record_files")) {
03074          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_CACHE_RECORD_FILES);
03075       /* Specify cache directory */
03076       }  else if (!strcasecmp(v->name, "record_cache_dir")) {
03077          ast_copy_string(record_cache_dir, v->value, AST_CACHE_DIR_LEN);
03078       /* Build transcode paths via SLINEAR, instead of directly */
03079       } else if (!strcasecmp(v->name, "transcode_via_sln")) {
03080          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_TRANSCODE_VIA_SLIN);
03081       /* Transmit SLINEAR silence while a channel is being recorded or DTMF is being generated on a channel */
03082       } else if (!strcasecmp(v->name, "transmit_silence_during_record") || !strcasecmp(v->name, "transmit_silence")) {
03083          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_TRANSMIT_SILENCE);
03084       /* Enable internal timing */
03085       } else if (!strcasecmp(v->name, "internal_timing")) {
03086          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_INTERNAL_TIMING);
03087       } else if (!strcasecmp(v->name, "maxcalls")) {
03088          if ((sscanf(v->value, "%30d", &option_maxcalls) != 1) || (option_maxcalls < 0)) {
03089             option_maxcalls = 0;
03090          }
03091       } else if (!strcasecmp(v->name, "maxload")) {
03092          double test[1];
03093 
03094          if (getloadavg(test, 1) == -1) {
03095             ast_log(LOG_ERROR, "Cannot obtain load average on this system. 'maxload' option disabled.\n");
03096             option_maxload = 0.0;
03097          } else if ((sscanf(v->value, "%30lf", &option_maxload) != 1) || (option_maxload < 0.0)) {
03098             option_maxload = 0.0;
03099          }
03100       /* Set the maximum amount of open files */
03101       } else if (!strcasecmp(v->name, "maxfiles")) {
03102          option_maxfiles = atoi(v->value);
03103          set_ulimit(option_maxfiles);
03104       /* What user to run as */
03105       } else if (!strcasecmp(v->name, "runuser")) {
03106          ast_copy_string(cfg_paths.run_user, v->value, sizeof(cfg_paths.run_user));
03107       /* What group to run as */
03108       } else if (!strcasecmp(v->name, "rungroup")) {
03109          ast_copy_string(cfg_paths.run_group, v->value, sizeof(cfg_paths.run_group));
03110       } else if (!strcasecmp(v->name, "systemname")) {
03111          ast_copy_string(cfg_paths.system_name, v->value, sizeof(cfg_paths.system_name));
03112       } else if (!strcasecmp(v->name, "autosystemname")) {
03113          if (ast_true(v->value)) {
03114             if (!gethostname(hostname, sizeof(hostname) - 1))
03115                ast_copy_string(cfg_paths.system_name, hostname, sizeof(cfg_paths.system_name));
03116             else {
03117                if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)){
03118                   ast_copy_string(cfg_paths.system_name, "localhost", sizeof(cfg_paths.system_name));
03119                }
03120                ast_log(LOG_ERROR, "Cannot obtain hostname for this system.  Using '%s' instead.\n", ast_config_AST_SYSTEM_NAME);
03121             }
03122          }
03123       } else if (!strcasecmp(v->name, "languageprefix")) {
03124          ast_language_is_prefix = ast_true(v->value);
03125       } else if (!strcasecmp(v->name, "defaultlanguage")) {
03126          ast_copy_string(defaultlanguage, v->value, MAX_LANGUAGE);
03127       } else if (!strcasecmp(v->name, "lockmode")) {
03128          if (!strcasecmp(v->value, "lockfile")) {
03129             ast_set_lock_type(AST_LOCK_TYPE_LOCKFILE);
03130          } else if (!strcasecmp(v->value, "flock")) {
03131             ast_set_lock_type(AST_LOCK_TYPE_FLOCK);
03132          } else {
03133             ast_log(LOG_WARNING, "'%s' is not a valid setting for the lockmode option, "
03134                "defaulting to 'lockfile'\n", v->value);
03135             ast_set_lock_type(AST_LOCK_TYPE_LOCKFILE);
03136          }
03137 #if defined(HAVE_SYSINFO)
03138       } else if (!strcasecmp(v->name, "minmemfree")) {
03139          /* specify the minimum amount of free memory to retain.  Asterisk should stop accepting new calls
03140           * if the amount of free memory falls below this watermark */
03141          if ((sscanf(v->value, "%30ld", &option_minmemfree) != 1) || (option_minmemfree < 0)) {
03142             option_minmemfree = 0;
03143          }
03144 #endif
03145       } else if (!strcasecmp(v->name, "entityid")) {
03146          struct ast_eid tmp_eid;
03147          if (!ast_str_to_eid(&tmp_eid, v->value)) {
03148             ast_verbose("Successfully set global EID to '%s'\n", v->value);
03149             ast_eid_default = tmp_eid;
03150          } else
03151             ast_verbose("Invalid Entity ID '%s' provided\n", v->value);
03152       } else if (!strcasecmp(v->name, "lightbackground")) {
03153          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_LIGHT_BACKGROUND);
03154       } else if (!strcasecmp(v->name, "forceblackbackground")) {
03155          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_FORCE_BLACK_BACKGROUND);
03156       } else if (!strcasecmp(v->name, "hideconnect")) {
03157          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_HIDE_CONSOLE_CONNECT);
03158       } else if (!strcasecmp(v->name, "lockconfdir")) {
03159          ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_LOCK_CONFIG_DIR);
03160       }
03161    }
03162    for (v = ast_variable_browse(cfg, "compat"); v; v = v->next) {
03163       float version;
03164       if (sscanf(v->value, "%30f", &version) != 1) {
03165          ast_log(LOG_WARNING, "Compatibility version for option '%s' is not a number: '%s'\n", v->name, v->value);
03166          continue;
03167       }
03168       if (!strcasecmp(v->name, "app_set")) {
03169          ast_set2_flag(&ast_compat, version < 1.5 ? 1 : 0, AST_COMPAT_APP_SET);
03170       } else if (!strcasecmp(v->name, "res_agi")) {
03171          ast_set2_flag(&ast_compat, version < 1.5 ? 1 : 0, AST_COMPAT_DELIM_RES_AGI);
03172       } else if (!strcasecmp(v->name, "pbx_realtime")) {
03173          ast_set2_flag(&ast_compat, version < 1.5 ? 1 : 0, AST_COMPAT_DELIM_PBX_REALTIME);
03174       }
03175    }
03176    ast_config_destroy(cfg);
03177 }
03178 
03179 static void *monitor_sig_flags(void *unused)
03180 {
03181    for (;;) {
03182       struct pollfd p = { sig_alert_pipe[0], POLLIN, 0 };
03183       int a;
03184       ast_poll(&p, 1, -1);
03185       if (sig_flags.need_reload) {
03186          sig_flags.need_reload = 0;
03187          ast_module_reload(NULL);
03188       }
03189       if (sig_flags.need_quit) {
03190          sig_flags.need_quit = 0;
03191          if ((consolethread != AST_PTHREADT_NULL) && (consolethread != pthread_self())) {
03192             sig_flags.need_quit_handler = 1;
03193             pthread_kill(consolethread, SIGURG);
03194          } else {
03195             quit_handler(0, SHUTDOWN_NORMAL, 0);
03196          }
03197       }
03198       if (read(sig_alert_pipe[0], &a, sizeof(a)) != sizeof(a)) {
03199       }
03200    }
03201 
03202    return NULL;
03203 }
03204 
03205 static void *canary_thread(void *unused)
03206 {
03207    struct stat canary_stat;
03208    struct timeval now;
03209 
03210    /* Give the canary time to sing */
03211    sleep(120);
03212 
03213    for (;;) {
03214       now = ast_tvnow();
03215       if (stat(canary_filename, &canary_stat) || now.tv_sec > canary_stat.st_mtime + 60) {
03216          ast_log(LOG_WARNING,
03217             "The canary is no more.  He has ceased to be!  "
03218             "He's expired and gone to meet his maker!  "
03219             "He's a stiff!  Bereft of life, he rests in peace.  "
03220             "His metabolic processes are now history!  He's off the twig!  "
03221             "He's kicked the bucket.  He's shuffled off his mortal coil, "
03222             "run down the curtain, and joined the bleeding choir invisible!!  "
03223             "THIS is an EX-CANARY.  (Reducing priority)\n");
03224          ast_set_priority(0);
03225          pthread_exit(NULL);
03226       }
03227 
03228       /* Check the canary once a minute */
03229       sleep(60);
03230    }
03231 }
03232 
03233 /* Used by libc's atexit(3) function */
03234 static void canary_exit(void)
03235 {
03236    if (canary_pid > 0)
03237       kill(canary_pid, SIGKILL);
03238 }
03239 
03240 static void run_startup_commands(void)
03241 {
03242    int fd;
03243    struct ast_config *cfg;
03244    struct ast_flags cfg_flags = { 0 };
03245    struct ast_variable *v;
03246 
03247    if (!(cfg = ast_config_load2("cli.conf", "" /* core, can't reload */, cfg_flags)))
03248       return;
03249    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
03250       return;
03251    }
03252 
03253    fd = open("/dev/null", O_RDWR);
03254    if (fd < 0) {
03255       ast_config_destroy(cfg);
03256       return;
03257    }
03258 
03259    for (v = ast_variable_browse(cfg, "startup_commands"); v; v = v->next) {
03260       if (ast_true(v->value))
03261          ast_cli_command(fd, v->name);
03262    }
03263 
03264    close(fd);
03265    ast_config_destroy(cfg);
03266 }
03267 
03268 static void env_init(void)
03269 {
03270    setenv("AST_SYSTEMNAME", ast_config_AST_SYSTEM_NAME, 1);
03271    setenv("AST_BUILD_HOST", ast_build_hostname, 1);
03272    setenv("AST_BUILD_DATE", ast_build_date, 1);
03273    setenv("AST_BUILD_KERNEL", ast_build_kernel, 1);
03274    setenv("AST_BUILD_MACHINE", ast_build_machine, 1);
03275    setenv("AST_BUILD_OS", ast_build_os, 1);
03276    setenv("AST_BUILD_USER", ast_build_user, 1);
03277    setenv("AST_VERSION", ast_get_version(), 1);
03278 }
03279 
03280 int main(int argc, char *argv[])
03281 {
03282    int c;
03283    char filename[80] = "";
03284    char hostname[MAXHOSTNAMELEN] = "";
03285    char tmp[80];
03286    char * xarg = NULL;
03287    int x;
03288    FILE *f;
03289    sigset_t sigs;
03290    int num;
03291    int isroot = 1, rundir_exists = 0;
03292    char *buf;
03293    const char *runuser = NULL, *rungroup = NULL;
03294    char *remotesock = NULL;
03295    int moduleresult;         /*!< Result from the module load subsystem */
03296    struct rlimit l;
03297 
03298    /* Remember original args for restart */
03299    if (argc > ARRAY_LEN(_argv) - 1) {
03300       fprintf(stderr, "Truncating argument size to %d\n", (int)ARRAY_LEN(_argv) - 1);
03301       argc = ARRAY_LEN(_argv) - 1;
03302    }
03303    for (x = 0; x < argc; x++)
03304       _argv[x] = argv[x];
03305    _argv[x] = NULL;
03306 
03307    if (geteuid() != 0)
03308       isroot = 0;
03309 
03310    /* if the progname is rasterisk consider it a remote console */
03311    if (argv[0] && (strstr(argv[0], "rasterisk")) != NULL) {
03312       ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE);
03313    }
03314    if (gethostname(hostname, sizeof(hostname)-1))
03315       ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
03316    ast_mainpid = getpid();
03317    ast_ulaw_init();
03318    ast_alaw_init();
03319    callerid_init();
03320    ast_builtins_init();
03321    ast_utils_init();
03322    tdd_init();
03323    ast_tps_init();
03324    ast_fd_init();
03325    ast_pbx_init();
03326 
03327    if (getenv("HOME")) 
03328       snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
03329    /* Check for options */
03330    while ((c = getopt(argc, argv, "BC:cde:FfG:ghIiL:M:mnpqRrs:TtU:VvWXx:")) != -1) {
03331       /*!\note Please keep the ordering here to alphabetical, capital letters
03332        * first.  This will make it easier in the future to select unused
03333        * option flags for new features. */
03334       switch (c) {
03335       case 'B': /* Force black background */
03336          ast_set_flag(&ast_options, AST_OPT_FLAG_FORCE_BLACK_BACKGROUND);
03337          ast_clear_flag(&ast_options, AST_OPT_FLAG_LIGHT_BACKGROUND);
03338          break;
03339       case 'X':
03340          ast_set_flag(&ast_options, AST_OPT_FLAG_EXEC_INCLUDES);
03341          break;
03342       case 'C':
03343          ast_copy_string(cfg_paths.config_file, optarg, sizeof(cfg_paths.config_file));
03344          ast_set_flag(&ast_options, AST_OPT_FLAG_OVERRIDE_CONFIG);
03345          break;
03346       case 'c':
03347          ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_CONSOLE);
03348          break;
03349       case 'd':
03350          option_debug++;
03351          ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
03352          break;
03353 #if defined(HAVE_SYSINFO)
03354       case 'e':
03355          if ((sscanf(&optarg[1], "%30ld", &option_minmemfree) != 1) || (option_minmemfree < 0)) {
03356             option_minmemfree = 0;
03357          }
03358          break;
03359 #endif
03360 #if HAVE_WORKING_FORK
03361       case 'F':
03362          ast_set_flag(&ast_options, AST_OPT_FLAG_ALWAYS_FORK);
03363          break;
03364       case 'f':
03365          ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
03366          break;
03367 #endif
03368       case 'G':
03369          rungroup = ast_strdupa(optarg);
03370          break;
03371       case 'g':
03372          ast_set_flag(&ast_options, AST_OPT_FLAG_DUMP_CORE);
03373          break;
03374       case 'h':
03375          show_cli_help();
03376          exit(0);
03377       case 'I':
03378          ast_set_flag(&ast_options, AST_OPT_FLAG_INTERNAL_TIMING);
03379          break;
03380       case 'i':
03381          ast_set_flag(&ast_options, AST_OPT_FLAG_INIT_KEYS);
03382          break;
03383       case 'L':
03384          if ((sscanf(optarg, "%30lf", &option_maxload) != 1) || (option_maxload < 0.0)) {
03385             option_maxload = 0.0;
03386          }
03387          break;
03388       case 'M':
03389          if ((sscanf(optarg, "%30d", &option_maxcalls) != 1) || (option_maxcalls < 0)) {
03390             option_maxcalls = 0;
03391          }
03392          break;
03393       case 'm':
03394          ast_set_flag(&ast_options, AST_OPT_FLAG_MUTE);
03395          break;
03396       case 'n':
03397          ast_set_flag(&ast_options, AST_OPT_FLAG_NO_COLOR);
03398          break;
03399       case 'p':
03400          ast_set_flag(&ast_options, AST_OPT_FLAG_HIGH_PRIORITY);
03401          break;
03402       case 'q':
03403          ast_set_flag(&ast_options, AST_OPT_FLAG_QUIET);
03404          break;
03405       case 'R':
03406          ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE | AST_OPT_FLAG_RECONNECT);
03407          break;
03408       case 'r':
03409          ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE);
03410          break;
03411       case 's':
03412          remotesock = ast_strdupa(optarg);
03413          break;
03414       case 'T':
03415          ast_set_flag(&ast_options, AST_OPT_FLAG_TIMESTAMP);
03416          break;
03417       case 't':
03418          ast_set_flag(&ast_options, AST_OPT_FLAG_CACHE_RECORD_FILES);
03419          break;
03420       case 'U':
03421          runuser = ast_strdupa(optarg);
03422          break;
03423       case 'V':
03424          show_version();
03425          exit(0);
03426       case 'v':
03427          option_verbose++;
03428          ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
03429          break;
03430       case 'W': /* White background */
03431          ast_set_flag(&ast_options, AST_OPT_FLAG_LIGHT_BACKGROUND);
03432          ast_clear_flag(&ast_options, AST_OPT_FLAG_FORCE_BLACK_BACKGROUND);
03433          break;
03434       case 'x':
03435          /* -r is implied by -x so set the flags -r sets as well. */
03436          ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE);
03437 
03438          ast_set_flag(&ast_options, AST_OPT_FLAG_EXEC | AST_OPT_FLAG_NO_COLOR);
03439          xarg = ast_strdupa(optarg);
03440          break;
03441       case '?':
03442          exit(1);
03443       }
03444    }
03445 
03446    if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
03447       if (ast_register_verbose(console_verboser)) {
03448          ast_log(LOG_WARNING, "Unable to register console verboser?\n");
03449       }
03450       WELCOME_MESSAGE;
03451    }
03452 
03453    if (ast_opt_console && !option_verbose) 
03454       ast_verbose("[ Booting...\n");
03455 
03456    /* For remote connections, change the name of the remote connection.
03457     * We do this for the benefit of init scripts (which need to know if/when
03458     * the main asterisk process has died yet). */
03459    if (ast_opt_remote) {
03460       strcpy(argv[0], "rasterisk");
03461       for (x = 1; x < argc; x++) {
03462          argv[x] = argv[0] + 10;
03463       }
03464    }
03465 
03466    if (ast_opt_console && !option_verbose) {
03467       ast_verbose("[ Reading Master Configuration ]\n");
03468    }
03469 
03470    ast_readconfig();
03471    env_init();
03472 
03473    if (ast_opt_remote && remotesock != NULL)
03474       ast_copy_string((char *) cfg_paths.socket_path, remotesock, sizeof(cfg_paths.socket_path));
03475 
03476    if (!ast_language_is_prefix && !ast_opt_remote)
03477       ast_log(LOG_WARNING, "The 'languageprefix' option in asterisk.conf is deprecated; in a future release it will be removed, and your sound files will need to be organized in the 'new style' language layout.\n");
03478 
03479    if (ast_opt_always_fork && (ast_opt_remote || ast_opt_console)) {
03480       ast_log(LOG_WARNING, "'alwaysfork' is not compatible with console or remote console mode; ignored\n");
03481       ast_clear_flag(&ast_options, AST_OPT_FLAG_ALWAYS_FORK);
03482    }
03483 
03484    if (ast_opt_dump_core) {
03485       memset(&l, 0, sizeof(l));
03486       l.rlim_cur = RLIM_INFINITY;
03487       l.rlim_max = RLIM_INFINITY;
03488       if (setrlimit(RLIMIT_CORE, &l)) {
03489          ast_log(LOG_WARNING, "Unable to disable core size resource limit: %s\n", strerror(errno));
03490       }
03491    }
03492 
03493    if (getrlimit(RLIMIT_NOFILE, &l)) {
03494       ast_log(LOG_WARNING, "Unable to check file descriptor limit: %s\n", strerror(errno));
03495    }
03496 
03497 #if !defined(CONFIGURE_RAN_AS_ROOT)
03498    /* Check if select(2) will run with more file descriptors */
03499    do {
03500       int fd, fd2;
03501       ast_fdset readers;
03502       struct timeval tv = { 0, };
03503 
03504       if (l.rlim_cur <= FD_SETSIZE) {
03505          /* The limit of select()able FDs is irrelevant, because we'll never
03506           * open one that high. */
03507          break;
03508       }
03509 
03510       if (!(fd = open("/dev/null", O_RDONLY))) {
03511          ast_log(LOG_ERROR, "Cannot open a file descriptor at boot? %s\n", strerror(errno));
03512          break; /* XXX Should we exit() here? XXX */
03513       }
03514 
03515       fd2 = (l.rlim_cur > sizeof(readers) * 8 ? sizeof(readers) * 8 : l.rlim_cur) - 1;
03516       if (dup2(fd, fd2) < 0) {
03517          ast_log(LOG_WARNING, "Cannot open maximum file descriptor %d at boot? %s\n", fd2, strerror(errno));
03518          close(fd);
03519          break;
03520       }
03521 
03522       FD_ZERO(&readers);
03523       FD_SET(fd2, &readers);
03524       if (ast_select(fd2 + 1, &readers, NULL, NULL, &tv) < 0) {
03525          ast_log(LOG_WARNING, "Maximum select()able file descriptor is %d\n", FD_SETSIZE);
03526       }
03527       ast_FD_SETSIZE = l.rlim_cur > ast_FDMAX ? ast_FDMAX : l.rlim_cur;
03528       close(fd);
03529       close(fd2);
03530    } while (0);
03531 #elif defined(HAVE_VARIABLE_FDSET)
03532    ast_FD_SETSIZE = l.rlim_cur > ast_FDMAX ? ast_FDMAX : l.rlim_cur;
03533 #endif /* !defined(CONFIGURE_RAN_AS_ROOT) */
03534 
03535    if ((!rungroup) && !ast_strlen_zero(ast_config_AST_RUN_GROUP))
03536       rungroup = ast_config_AST_RUN_GROUP;
03537    if ((!runuser) && !ast_strlen_zero(ast_config_AST_RUN_USER))
03538       runuser = ast_config_AST_RUN_USER;
03539 
03540    /* Must install this signal handler up here to ensure that if the canary
03541     * fails to execute that it doesn't kill the Asterisk process.
03542     */
03543    sigaction(SIGCHLD, &child_handler, NULL);
03544 
03545    /* It's common on some platforms to clear /var/run at boot.  Create the
03546     * socket file directory before we drop privileges. */
03547    if (mkdir(ast_config_AST_RUN_DIR, 0755)) {
03548       if (errno == EEXIST) {
03549          rundir_exists = 1;
03550       } else {
03551          ast_log(LOG_WARNING, "Unable to create socket file directory.  Remote consoles will not be able to connect! (%s)\n", strerror(x));
03552       }
03553    }
03554 
03555 #ifndef __CYGWIN__
03556 
03557    if (isroot) {
03558       ast_set_priority(ast_opt_high_priority);
03559    }
03560 
03561    if (isroot && rungroup) {
03562       struct group *gr;
03563       gr = getgrnam(rungroup);
03564       if (!gr) {
03565          ast_log(LOG_WARNING, "No such group '%s'!\n", rungroup);
03566          exit(1);
03567       }
03568       if (!rundir_exists && chown(ast_config_AST_RUN_DIR, -1, gr->gr_gid)) {
03569          ast_log(LOG_WARNING, "Unable to chgrp run directory to %d (%s)\n", (int) gr->gr_gid, rungroup);
03570       }
03571       if (setgid(gr->gr_gid)) {
03572          ast_log(LOG_WARNING, "Unable to setgid to %d (%s)\n", (int)gr->gr_gid, rungroup);
03573          exit(1);
03574       }
03575       if (setgroups(0, NULL)) {
03576          ast_log(LOG_WARNING, "Unable to drop unneeded groups\n");
03577          exit(1);
03578       }
03579       if (option_verbose)
03580          ast_verbose("Running as group '%s'\n", rungroup);
03581    }
03582 
03583    if (runuser && !ast_test_flag(&ast_options, AST_OPT_FLAG_REMOTE)) {
03584 #ifdef HAVE_CAP
03585       int has_cap = 1;
03586 #endif /* HAVE_CAP */
03587       struct passwd *pw;
03588       pw = getpwnam(runuser);
03589       if (!pw) {
03590          ast_log(LOG_WARNING, "No such user '%s'!\n", runuser);
03591          exit(1);
03592       }
03593       if (chown(ast_config_AST_RUN_DIR, pw->pw_uid, -1)) {
03594          ast_log(LOG_WARNING, "Unable to chown run directory to %d (%s)\n", (int) pw->pw_uid, runuser);
03595       }
03596 #ifdef HAVE_CAP
03597       if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0)) {
03598          ast_log(LOG_WARNING, "Unable to keep capabilities.\n");
03599          has_cap = 0;
03600       }
03601 #endif /* HAVE_CAP */
03602       if (!isroot && pw->pw_uid != geteuid()) {
03603          ast_log(LOG_ERROR, "Asterisk started as nonroot, but runuser '%s' requested.\n", runuser);
03604          exit(1);
03605       }
03606       if (!rungroup) {
03607          if (setgid(pw->pw_gid)) {
03608             ast_log(LOG_WARNING, "Unable to setgid to %d!\n", (int)pw->pw_gid);
03609             exit(1);
03610          }
03611          if (isroot && initgroups(pw->pw_name, pw->pw_gid)) {
03612             ast_log(LOG_WARNING, "Unable to init groups for '%s'\n", runuser);
03613             exit(1);
03614          }
03615       }
03616       if (setuid(pw->pw_uid)) {
03617          ast_log(LOG_WARNING, "Unable to setuid to %d (%s)\n", (int)pw->pw_uid, runuser);
03618          exit(1);
03619       }
03620       if (option_verbose)
03621          ast_verbose("Running as user '%s'\n", runuser);
03622 #ifdef HAVE_CAP
03623       if (has_cap) {
03624          cap_t cap;
03625 
03626          cap = cap_from_text("cap_net_admin=eip");
03627 
03628          if (cap_set_proc(cap))
03629             ast_log(LOG_WARNING, "Unable to install capabilities.\n");
03630 
03631          if (cap_free(cap))
03632             ast_log(LOG_WARNING, "Unable to drop capabilities.\n");
03633       }
03634 #endif /* HAVE_CAP */
03635    }
03636 
03637 #endif /* __CYGWIN__ */
03638 
03639 #ifdef linux
03640    if (geteuid() && ast_opt_dump_core) {
03641       if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) < 0) {
03642          ast_log(LOG_WARNING, "Unable to set the process for core dumps after changing to a non-root user. %s\n", strerror(errno));
03643       }
03644    }
03645 #endif
03646 
03647    {
03648 #if defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS)
03649 #if defined(HAVE_EUIDACCESS) && !defined(HAVE_EACCESS)
03650 #define eaccess euidaccess
03651 #endif
03652       char dir[PATH_MAX];
03653       if (!getcwd(dir, sizeof(dir)) || eaccess(dir, R_OK | X_OK | F_OK)) {
03654          ast_log(LOG_ERROR, "Unable to access the running directory (%s).  Changing to '/' for compatibility.\n", strerror(errno));
03655          /* If we cannot access the CWD, then we couldn't dump core anyway,
03656           * so chdir("/") won't break anything. */
03657          if (chdir("/")) {
03658             /* chdir(/) should never fail, so this ends up being a no-op */
03659             ast_log(LOG_ERROR, "chdir(\"/\") failed?!! %s\n", strerror(errno));
03660          }
03661       } else
03662 #endif /* defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS) */
03663       if (!ast_opt_no_fork && !ast_opt_dump_core) {
03664          /* Backgrounding, but no cores, so chdir won't break anything. */
03665          if (chdir("/")) {
03666             ast_log(LOG_ERROR, "Unable to chdir(\"/\") ?!! %s\n", strerror(errno));
03667          }
03668       }
03669    }
03670 
03671    ast_term_init();
03672    printf("%s", term_end());
03673    fflush(stdout);
03674 
03675    if (ast_opt_console && !option_verbose) 
03676       ast_verbose("[ Initializing Custom Configuration Options ]\n");
03677    /* custom config setup */
03678    register_config_cli();
03679    read_config_maps();
03680    
03681    if (ast_opt_console) {
03682       if (el_hist == NULL || el == NULL)
03683          ast_el_initialize();
03684 
03685       if (!ast_strlen_zero(filename))
03686          ast_el_read_history(filename);
03687    }
03688 
03689    if (ast_tryconnect()) {
03690       /* One is already running */
03691       if (ast_opt_remote) {
03692          if (ast_opt_exec) {
03693             ast_remotecontrol(xarg);
03694             quit_handler(0, SHUTDOWN_FAST, 0);
03695             exit(0);
03696          }
03697          printf("%s", term_quit());
03698          ast_remotecontrol(NULL);
03699          quit_handler(0, SHUTDOWN_FAST, 0);
03700          exit(0);
03701       } else {
03702          ast_log(LOG_ERROR, "Asterisk already running on %s.  Use 'asterisk -r' to connect.\n", ast_config_AST_SOCKET);
03703          printf("%s", term_quit());
03704          exit(1);
03705       }
03706    } else if (ast_opt_remote || ast_opt_exec) {
03707       ast_log(LOG_ERROR, "Unable to connect to remote asterisk (does %s exist?)\n", ast_config_AST_SOCKET);
03708       printf("%s", term_quit());
03709       exit(1);
03710    }
03711    /* Blindly write pid file since we couldn't connect */
03712    unlink(ast_config_AST_PID);
03713    f = fopen(ast_config_AST_PID, "w");
03714    if (f) {
03715       fprintf(f, "%ld\n", (long)getpid());
03716       fclose(f);
03717    } else
03718       ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno));
03719 
03720 #if HAVE_WORKING_FORK
03721    if (ast_opt_always_fork || !ast_opt_no_fork) {
03722 #ifndef HAVE_SBIN_LAUNCHD
03723       if (daemon(1, 0) < 0) {
03724          ast_log(LOG_ERROR, "daemon() failed: %s\n", strerror(errno));
03725       }
03726       ast_mainpid = getpid();
03727       /* Blindly re-write pid file since we are forking */
03728       unlink(ast_config_AST_PID);
03729       f = fopen(ast_config_AST_PID, "w");
03730       if (f) {
03731          fprintf(f, "%ld\n", (long)ast_mainpid);
03732          fclose(f);
03733       } else
03734          ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno));
03735 #else
03736       ast_log(LOG_WARNING, "Mac OS X detected.  Use 'launchctl load /Library/LaunchDaemon/org.asterisk.asterisk.plist'.\n");
03737 #endif
03738    }
03739 #endif
03740 
03741    /* Spawning of astcanary must happen AFTER the call to daemon(3) */
03742    if (isroot && ast_opt_high_priority) {
03743       snprintf(canary_filename, sizeof(canary_filename), "%s/alt.asterisk.canary.tweet.tweet.tweet", ast_config_AST_RUN_DIR);
03744 
03745       /* Don't let the canary child kill Asterisk, if it dies immediately */
03746       sigaction(SIGPIPE, &ignore_sig_handler, NULL);
03747 
03748       canary_pid = fork();
03749       if (canary_pid == 0) {
03750          char canary_binary[128], *lastslash, ppid[12];
03751 
03752          /* Reset signal handler */
03753          signal(SIGCHLD, SIG_DFL);
03754          signal(SIGPIPE, SIG_DFL);
03755 
03756          ast_close_fds_above_n(0);
03757          ast_set_priority(0);
03758          snprintf(ppid, sizeof(ppid), "%d", (int) ast_mainpid);
03759 
03760          execlp("astcanary", "astcanary", canary_filename, ppid, (char *)NULL);
03761 
03762          /* If not found, try the same path as used to execute asterisk */
03763          ast_copy_string(canary_binary, argv[0], sizeof(canary_binary));
03764          if ((lastslash = strrchr(canary_binary, '/'))) {
03765             ast_copy_string(lastslash + 1, "astcanary", sizeof(canary_binary) + canary_binary - (lastslash + 1));
03766             execl(canary_binary, "astcanary", canary_filename, ppid, (char *)NULL);
03767          }
03768 
03769          /* Should never happen */
03770          _exit(1);
03771       } else if (canary_pid > 0) {
03772          pthread_t dont_care;
03773          ast_pthread_create_detached(&dont_care, NULL, canary_thread, NULL);
03774       }
03775 
03776       /* Kill the canary when we exit */
03777       ast_register_atexit(canary_exit);
03778    }
03779 
03780    if (ast_event_init()) {
03781       printf("%s", term_quit());
03782       exit(1);
03783    }
03784 
03785 #ifdef TEST_FRAMEWORK
03786    if (ast_test_init()) {
03787       printf("%s", term_quit());
03788       exit(1);
03789    }
03790 #endif
03791 
03792    ast_aoc_cli_init();
03793 
03794    ast_makesocket();
03795    sigemptyset(&sigs);
03796    sigaddset(&sigs, SIGHUP);
03797    sigaddset(&sigs, SIGTERM);
03798    sigaddset(&sigs, SIGINT);
03799    sigaddset(&sigs, SIGPIPE);
03800    sigaddset(&sigs, SIGWINCH);
03801    pthread_sigmask(SIG_BLOCK, &sigs, NULL);
03802    sigaction(SIGURG, &urg_handler, NULL);
03803    signal(SIGINT, __quit_handler);
03804    signal(SIGTERM, __quit_handler);
03805    sigaction(SIGHUP, &hup_handler, NULL);
03806    sigaction(SIGPIPE, &ignore_sig_handler, NULL);
03807 
03808    /* ensure that the random number generators are seeded with a different value every time
03809       Asterisk is started
03810    */
03811    srand((unsigned int) getpid() + (unsigned int) time(NULL));
03812    initstate((unsigned int) getpid() * 65536 + (unsigned int) time(NULL), randompool, sizeof(randompool));
03813 
03814    if (init_logger()) {    /* Start logging subsystem */
03815       printf("%s", term_quit());
03816       exit(1);
03817    }
03818 
03819    threadstorage_init();
03820 
03821    astobj2_init();
03822 
03823    ast_autoservice_init();
03824 
03825    if (ast_timing_init()) {
03826       printf("%s", term_quit());
03827       exit(1);
03828    }
03829 
03830    if (ast_ssl_init()) {
03831       printf("%s", term_quit());
03832       exit(1);
03833    }
03834 
03835 #ifdef AST_XML_DOCS
03836    /* Load XML documentation. */
03837    ast_xmldoc_load_documentation();
03838 #endif
03839 
03840    /* initialize the data retrieval API */
03841    if (ast_data_init()) {
03842       printf ("%s", term_quit());
03843       exit(1);
03844    }
03845 
03846    ast_channels_init();
03847 
03848    if ((moduleresult = load_modules(1))) {      /* Load modules, pre-load only */
03849       printf("%s", term_quit());
03850       exit(moduleresult == -2 ? 2 : 1);
03851    }
03852 
03853    if (dnsmgr_init()) {    /* Initialize the DNS manager */
03854       printf("%s", term_quit());
03855       exit(1);
03856    }
03857 
03858    ast_http_init();     /* Start the HTTP server, if needed */
03859 
03860    if (init_manager()) {
03861       printf("%s", term_quit());
03862       exit(1);
03863    }
03864 
03865    if (ast_cdr_engine_init()) {
03866       printf("%s", term_quit());
03867       exit(1);
03868    }
03869 
03870    if (ast_cel_engine_init()) {
03871       printf("%s", term_quit());
03872       exit(1);
03873    }
03874 
03875    if (ast_device_state_engine_init()) {
03876       printf("%s", term_quit());
03877       exit(1);
03878    }
03879 
03880    ast_dsp_init();
03881    ast_udptl_init();
03882 
03883    if (ast_image_init()) {
03884       printf("%s", term_quit());
03885       exit(1);
03886    }
03887 
03888    if (ast_file_init()) {
03889       printf("%s", term_quit());
03890       exit(1);
03891    }
03892 
03893    if (load_pbx()) {
03894       printf("%s", term_quit());
03895       exit(1);
03896    }
03897 
03898    if (ast_indications_init()) {
03899       printf("%s", term_quit());
03900       exit(1);
03901    }
03902 
03903    if (ast_features_init()) {
03904       printf("%s", term_quit());
03905       exit(1);
03906    }
03907 
03908    if (init_framer()) {
03909       printf("%s", term_quit());
03910       exit(1);
03911    }
03912 
03913    if (astdb_init()) {
03914       printf("%s", term_quit());
03915       exit(1);
03916    }
03917 
03918    if (ast_enum_init()) {
03919       printf("%s", term_quit());
03920       exit(1);
03921    }
03922 
03923    if (ast_cc_init()) {
03924       printf("%s", term_quit());
03925       exit(1);
03926    }
03927 
03928    if ((moduleresult = load_modules(0))) {      /* Load modules */
03929       printf("%s", term_quit());
03930       exit(moduleresult == -2 ? 2 : 1);
03931    }
03932 
03933    /* loads the cli_permissoins.conf file needed to implement cli restrictions. */
03934    ast_cli_perms_init(0);
03935 
03936    ast_stun_init();
03937 
03938    dnsmgr_start_refresh();
03939 
03940    /* We might have the option of showing a console, but for now just
03941       do nothing... */
03942    if (ast_opt_console && !option_verbose)
03943       ast_verbose(" ]\n");
03944    if (option_verbose || ast_opt_console)
03945       ast_verbose("%s", term_color(tmp, "Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK, sizeof(tmp)));
03946    if (ast_opt_no_fork)
03947       consolethread = pthread_self();
03948 
03949    if (pipe(sig_alert_pipe))
03950       sig_alert_pipe[0] = sig_alert_pipe[1] = -1;
03951 
03952    ast_set_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED);
03953    manager_event(EVENT_FLAG_SYSTEM, "FullyBooted", "Status: Fully Booted\r\n");
03954 
03955    ast_process_pending_reloads();
03956 
03957    pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
03958 
03959 #ifdef __AST_DEBUG_MALLOC
03960    __ast_mm_init();
03961 #endif   
03962 
03963    ast_lastreloadtime = ast_startuptime = ast_tvnow();
03964    ast_cli_register_multiple(cli_asterisk, ARRAY_LEN(cli_asterisk));
03965 
03966    run_startup_commands();
03967 
03968    if (ast_opt_console) {
03969       /* Console stuff now... */
03970       /* Register our quit function */
03971       char title[256];
03972 
03973       ast_pthread_create_detached(&mon_sig_flags, NULL, monitor_sig_flags, NULL);
03974 
03975       set_icon("Asterisk");
03976       snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %ld)", hostname, (long)ast_mainpid);
03977       set_title(title);
03978 
03979       el_set(el, EL_GETCFN, ast_el_read_char);
03980 
03981       for (;;) {
03982          if (sig_flags.need_quit || sig_flags.need_quit_handler) {
03983             quit_handler(0, SHUTDOWN_FAST, 0);
03984             break;
03985          }
03986          buf = (char *) el_gets(el, &num);
03987 
03988          if (!buf && write(1, "", 1) < 0)
03989             goto lostterm;
03990 
03991          if (buf) {
03992             if (buf[strlen(buf)-1] == '\n')
03993                buf[strlen(buf)-1] = '\0';
03994 
03995             consolehandler((char *)buf);
03996          } else if (ast_opt_remote && (write(STDOUT_FILENO, "\nUse EXIT or QUIT to exit the asterisk console\n",
03997                strlen("\nUse EXIT or QUIT to exit the asterisk console\n")) < 0)) {
03998             /* Whoa, stdout disappeared from under us... Make /dev/null's */
03999             int fd;
04000             fd = open("/dev/null", O_RDWR);
04001             if (fd > -1) {
04002                dup2(fd, STDOUT_FILENO);
04003                dup2(fd, STDIN_FILENO);
04004             } else
04005                ast_log(LOG_WARNING, "Failed to open /dev/null to recover from dead console. Bad things will happen!\n");
04006             break;
04007          }
04008       }
04009    }
04010 
04011    monitor_sig_flags(NULL);
04012 
04013 lostterm:
04014    return 0;
04015 }

Generated on 30 Aug 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1