add tab completion for 'core set debug X filename.c'
[asterisk/asterisk.git] / main / asterisk.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19
20 /* Doxygenified Copyright Header */
21 /*!
22  * \mainpage Asterisk -- An Open Source Telephony Toolkit
23  *
24  * \par Developer Documentation for Asterisk
25  *
26  * This is the main developer documentation for Asterisk. It is 
27  * generated by running "make progdocs" from the Asterisk source tree.  
28  *
29  * In addition to the information available on the Asterisk source code, 
30  * please see the appendices for information on coding guidelines, 
31  * release management, commit policies, and more.
32  *
33  * \par Additional documentation
34  * \arg \ref Licensing
35  * \arg \ref DevDoc 
36  * \arg \ref ConfigFiles
37  *
38  * \section copyright Copyright and Author
39  *
40  * Copyright (C) 1999 - 2008, Digium, Inc.
41  * Asterisk is a <a href="http://www.digium.com/en/company/view-policy.php?id=Trademark-Policy">registered trademark</a>
42  * of <a href="http://www.digium.com">Digium, Inc</a>.
43  *
44  * \author Mark Spencer <markster@digium.com>
45  * Also see \ref AstCREDITS
46  *
47  * See http://www.asterisk.org for more information about
48  * the Asterisk project. Please do not directly contact
49  * any of the maintainers of this project for assistance;
50  * the project provides a web site, mailing lists, and IRC
51  * channels for your use.
52  */
53
54 /*! \file
55   \brief Top level source file for Asterisk  - the Open Source PBX. Implementation
56   of PBX core functions and CLI interface.
57   
58  */
59
60 #include "asterisk.h"
61
62 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
63
64 #include "asterisk/_private.h"
65
66 #undef sched_setscheduler
67 #undef setpriority
68 #include <sys/time.h>
69 #include <fcntl.h>
70 #include <signal.h>
71 #include <sched.h>
72 #include <sys/un.h>
73 #include <sys/wait.h>
74 #include <ctype.h>
75 #include <sys/resource.h>
76 #include <grp.h>
77 #include <pwd.h>
78 #include <sys/stat.h>
79 #if defined(HAVE_SYSINFO)
80 #include <sys/sysinfo.h>
81 #endif
82 #include <regex.h>
83
84 #if defined(SOLARIS)
85 int daemon(int, int);  /* defined in libresolv of all places */
86 #include <sys/loadavg.h>
87 #endif
88
89 #ifdef linux
90 #include <sys/prctl.h>
91 #ifdef HAVE_CAP
92 #include <sys/capability.h>
93 #endif /* HAVE_CAP */
94 #endif /* linux */
95
96 #include "asterisk/paths.h"     /* we define here the variables so better agree on the prototype */
97 #include "asterisk/network.h"
98 #include "asterisk/cli.h"
99 #include "asterisk/channel.h"
100 #include "asterisk/features.h"
101 #include "asterisk/ulaw.h"
102 #include "asterisk/alaw.h"
103 #include "asterisk/callerid.h"
104 #include "asterisk/image.h"
105 #include "asterisk/tdd.h"
106 #include "asterisk/term.h"
107 #include "asterisk/manager.h"
108 #include "asterisk/cdr.h"
109 #include "asterisk/pbx.h"
110 #include "asterisk/enum.h"
111 #include "asterisk/rtp.h"
112 #include "asterisk/http.h"
113 #include "asterisk/udptl.h"
114 #include "asterisk/app.h"
115 #include "asterisk/lock.h"
116 #include "asterisk/utils.h"
117 #include "asterisk/file.h"
118 #include "asterisk/io.h"
119 #include "editline/histedit.h"
120 #include "asterisk/config.h"
121 #include "asterisk/ast_version.h"
122 #include "asterisk/linkedlists.h"
123 #include "asterisk/devicestate.h"
124 #include "asterisk/module.h"
125 #include "asterisk/dsp.h"
126 #include "asterisk/buildinfo.h"
127 #include "asterisk/xmldoc.h"
128
129 #include "asterisk/doxyref.h"           /* Doxygen documentation */
130
131 #include "../defaults.h"
132
133 #ifndef AF_LOCAL
134 #define AF_LOCAL AF_UNIX
135 #define PF_LOCAL PF_UNIX
136 #endif
137
138 #define AST_MAX_CONNECTS 128
139 #define NUM_MSGS 64
140
141 /*! \brief Welcome message when starting a CLI interface */
142 #define WELCOME_MESSAGE \
143     ast_verbose("Asterisk %s, Copyright (C) 1999 - 2008 Digium, Inc. and others.\n" \
144                 "Created by Mark Spencer <markster@digium.com>\n" \
145                 "Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.\n" \
146                 "This is free software, with components licensed under the GNU General Public\n" \
147                 "License version 2 and other licenses; you are welcome to redistribute it under\n" \
148                 "certain conditions. Type 'core show license' for details.\n" \
149                 "=========================================================================\n", ast_get_version()) \
150
151 /*! \defgroup main_options Main Configuration Options
152  * \brief Main configuration options from asterisk.conf or OS command line on starting Asterisk.
153  * \arg \ref Config_ast "asterisk.conf"
154  * \note Some of them can be changed in the CLI 
155  */
156 /*! @{ */
157
158 struct ast_flags ast_options = { AST_DEFAULT_OPTIONS };
159 struct ast_flags ast_compat = { 7 };
160
161 int option_verbose;                             /*!< Verbosity level */
162 int option_debug;                               /*!< Debug level */
163 double option_maxload;                          /*!< Max load avg on system */
164 int option_maxcalls;                            /*!< Max number of active calls */
165 int option_maxfiles;                            /*!< Max number of open file handles (files, sockets) */
166 #if defined(HAVE_SYSINFO)
167 long option_minmemfree;                         /*!< Minimum amount of free system memory - stop accepting calls if free memory falls below this watermark */
168 #endif
169
170 /*! @} */
171
172 struct ast_eid g_eid;
173
174 /* XXX tmpdir is a subdir of the spool directory, and no way to remap it */
175 char record_cache_dir[AST_CACHE_DIR_LEN] = DEFAULT_TMP_DIR;
176
177 static int ast_socket = -1;             /*!< UNIX Socket for allowing remote control */
178 static int ast_consock = -1;            /*!< UNIX Socket for controlling another asterisk */
179 pid_t ast_mainpid;
180 struct console {
181         int fd;                         /*!< File descriptor */
182         int p[2];                       /*!< Pipe */
183         pthread_t t;                    /*!< Thread of handler */
184         int mute;                       /*!< Is the console muted for logs */
185         int uid;                        /*!< Remote user ID. */
186         int gid;                        /*!< Remote group ID. */
187         int levels[NUMLOGLEVELS];       /*!< Which log levels are enabled for the console */
188 };
189
190 struct ast_atexit {
191         void (*func)(void);
192         AST_RWLIST_ENTRY(ast_atexit) list;
193 };
194
195 static AST_RWLIST_HEAD_STATIC(atexits, ast_atexit);
196
197 struct timeval ast_startuptime;
198 struct timeval ast_lastreloadtime;
199
200 static History *el_hist;
201 static EditLine *el;
202 static char *remotehostname;
203
204 struct console consoles[AST_MAX_CONNECTS];
205
206 char defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE;
207
208 static int ast_el_add_history(char *);
209 static int ast_el_read_history(char *);
210 static int ast_el_write_history(char *);
211
212 struct _cfg_paths {
213         char config_dir[PATH_MAX];
214         char module_dir[PATH_MAX];
215         char spool_dir[PATH_MAX];
216         char monitor_dir[PATH_MAX];
217         char var_dir[PATH_MAX];
218         char data_dir[PATH_MAX];
219         char log_dir[PATH_MAX];
220         char agi_dir[PATH_MAX];
221         char run_dir[PATH_MAX];
222         char key_dir[PATH_MAX];
223
224         char config_file[PATH_MAX];
225         char db_path[PATH_MAX];
226         char pid_path[PATH_MAX];
227         char socket_path[PATH_MAX];
228         char run_user[PATH_MAX];
229         char run_group[PATH_MAX];
230         char system_name[128];
231 };
232
233 static struct _cfg_paths cfg_paths;
234
235 const char *ast_config_AST_CONFIG_DIR   = cfg_paths.config_dir;
236 const char *ast_config_AST_CONFIG_FILE  = cfg_paths.config_file;
237 const char *ast_config_AST_MODULE_DIR   = cfg_paths.module_dir;
238 const char *ast_config_AST_SPOOL_DIR    = cfg_paths.spool_dir;
239 const char *ast_config_AST_MONITOR_DIR  = cfg_paths.monitor_dir;
240 const char *ast_config_AST_VAR_DIR      = cfg_paths.var_dir;
241 const char *ast_config_AST_DATA_DIR     = cfg_paths.data_dir;
242 const char *ast_config_AST_LOG_DIR      = cfg_paths.log_dir;
243 const char *ast_config_AST_AGI_DIR      = cfg_paths.agi_dir;
244 const char *ast_config_AST_KEY_DIR      = cfg_paths.key_dir;
245 const char *ast_config_AST_RUN_DIR      = cfg_paths.run_dir;
246
247 const char *ast_config_AST_DB           = cfg_paths.db_path;
248 const char *ast_config_AST_PID          = cfg_paths.pid_path;
249 const char *ast_config_AST_SOCKET       = cfg_paths.socket_path;
250 const char *ast_config_AST_RUN_USER     = cfg_paths.run_user;
251 const char *ast_config_AST_RUN_GROUP    = cfg_paths.run_group;
252 const char *ast_config_AST_SYSTEM_NAME  = cfg_paths.system_name;
253
254 static char ast_config_AST_CTL_PERMISSIONS[PATH_MAX];
255 static char ast_config_AST_CTL_OWNER[PATH_MAX] = "\0";
256 static char ast_config_AST_CTL_GROUP[PATH_MAX] = "\0";
257 static char ast_config_AST_CTL[PATH_MAX] = "asterisk.ctl";
258
259 static char *_argv[256];
260 static int shuttingdown;
261 static int restartnow;
262 static pthread_t consolethread = AST_PTHREADT_NULL;
263 static int canary_pid = 0;
264 static char canary_filename[128];
265
266 static char randompool[256];
267
268 static int sig_alert_pipe[2] = { -1, -1 };
269 static struct {
270          unsigned int need_reload:1;
271          unsigned int need_quit:1;
272 } sig_flags;
273
274 #if !defined(LOW_MEMORY)
275 struct file_version {
276         AST_RWLIST_ENTRY(file_version) list;
277         const char *file;
278         char *version;
279 };
280
281 static AST_RWLIST_HEAD_STATIC(file_versions, file_version);
282
283 void ast_register_file_version(const char *file, const char *version)
284 {
285         struct file_version *new;
286         char *work;
287         size_t version_length;
288
289         work = ast_strdupa(version);
290         work = ast_strip(ast_strip_quoted(work, "$", "$"));
291         version_length = strlen(work) + 1;
292         
293         if (!(new = ast_calloc(1, sizeof(*new) + version_length)))
294                 return;
295
296         new->file = file;
297         new->version = (char *) new + sizeof(*new);
298         memcpy(new->version, work, version_length);
299         AST_RWLIST_WRLOCK(&file_versions);
300         AST_RWLIST_INSERT_HEAD(&file_versions, new, list);
301         AST_RWLIST_UNLOCK(&file_versions);
302 }
303
304 void ast_unregister_file_version(const char *file)
305 {
306         struct file_version *find;
307
308         AST_RWLIST_WRLOCK(&file_versions);
309         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&file_versions, find, list) {
310                 if (!strcasecmp(find->file, file)) {
311                         AST_RWLIST_REMOVE_CURRENT(list);
312                         break;
313                 }
314         }
315         AST_RWLIST_TRAVERSE_SAFE_END;
316         AST_RWLIST_UNLOCK(&file_versions);
317
318         if (find)
319                 ast_free(find);
320 }
321
322 char *ast_complete_source_filename(const char *partial, int n)
323 {
324         struct file_version *find;
325         size_t len = strlen(partial);
326         int count = 0;
327         char *res = NULL;
328
329         AST_RWLIST_RDLOCK(&file_versions);
330         AST_RWLIST_TRAVERSE(&file_versions, find, list) {
331                 if (!strncasecmp(find->file, partial, len) && ++count > n) {
332                         res = ast_strdup(find->file);
333                         break;
334                 }
335         }
336         AST_RWLIST_UNLOCK(&file_versions);
337         return res;
338 }
339
340 /*! \brief Find version for given module name */
341 const char *ast_file_version_find(const char *file)
342 {
343         struct file_version *iterator;
344
345         AST_RWLIST_WRLOCK(&file_versions);
346         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&file_versions, iterator, list) {
347                 if (!strcasecmp(iterator->file, file))
348                         break;
349         }
350         AST_RWLIST_TRAVERSE_SAFE_END;
351         AST_RWLIST_UNLOCK(&file_versions);
352         if (iterator)
353                 return iterator->version;
354         return NULL;
355 }      
356        
357
358
359 struct thread_list_t {
360         AST_RWLIST_ENTRY(thread_list_t) list;
361         char *name;
362         pthread_t id;
363 };
364
365 static AST_RWLIST_HEAD_STATIC(thread_list, thread_list_t);
366
367 void ast_register_thread(char *name)
368
369         struct thread_list_t *new = ast_calloc(1, sizeof(*new));
370
371         if (!new)
372                 return;
373         new->id = pthread_self();
374         new->name = name; /* steal the allocated memory for the thread name */
375         AST_RWLIST_WRLOCK(&thread_list);
376         AST_RWLIST_INSERT_HEAD(&thread_list, new, list);
377         AST_RWLIST_UNLOCK(&thread_list);
378 }
379
380 void ast_unregister_thread(void *id)
381 {
382         struct thread_list_t *x;
383
384         AST_RWLIST_WRLOCK(&thread_list);
385         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&thread_list, x, list) {
386                 if ((void *) x->id == id) {
387                         AST_RWLIST_REMOVE_CURRENT(list);
388                         break;
389                 }
390         }
391         AST_RWLIST_TRAVERSE_SAFE_END;
392         AST_RWLIST_UNLOCK(&thread_list);
393         if (x) {
394                 ast_free(x->name);
395                 ast_free(x);
396         }
397 }
398
399 /*! \brief Give an overview of core settings */
400 static char *handle_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
401 {
402         char buf[BUFSIZ];
403         struct ast_tm tm;
404         char eid_str[128];
405
406         switch (cmd) {
407         case CLI_INIT:
408                 e->command = "core show settings";
409                 e->usage = "Usage: core show settings\n"
410                            "       Show core misc settings";
411                 return NULL;
412         case CLI_GENERATE:
413                 return NULL;
414         }
415
416         ast_eid_to_str(eid_str, sizeof(eid_str), &g_eid);
417
418         ast_cli(a->fd, "\nPBX Core settings\n");
419         ast_cli(a->fd, "-----------------\n");
420         ast_cli(a->fd, "  Version:                     %s\n", ast_get_version());
421         ast_cli(a->fd, "  Build Options:               %s\n", S_OR(AST_BUILDOPTS, "(none)"));
422         if (option_maxcalls)
423                 ast_cli(a->fd, "  Maximum calls:               %d (Current %d)\n", option_maxcalls, ast_active_channels());
424         else
425                 ast_cli(a->fd, "  Maximum calls:               Not set\n");
426         if (option_maxfiles)
427                 ast_cli(a->fd, "  Maximum open file handles:   %d\n", option_maxfiles); 
428         else
429                 ast_cli(a->fd, "  Maximum open file handles:   Not set\n");
430         ast_cli(a->fd, "  Verbosity:                   %d\n", option_verbose);
431         ast_cli(a->fd, "  Debug level:                 %d\n", option_debug);
432         ast_cli(a->fd, "  Maximum load average:        %lf\n", option_maxload);
433 #if defined(HAVE_SYSINFO)
434         ast_cli(a->fd, "  Minimum free memory:         %ld MB\n", option_minmemfree);
435 #endif
436         if (ast_localtime(&ast_startuptime, &tm, NULL)) {
437                 ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
438                 ast_cli(a->fd, "  Startup time:                %s\n", buf);
439         }
440         if (ast_localtime(&ast_lastreloadtime, &tm, NULL)) {
441                 ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
442                 ast_cli(a->fd, "  Last reload time:            %s\n", buf);
443         }
444         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);
445         ast_cli(a->fd, "  System name:                 %s\n", ast_config_AST_SYSTEM_NAME);
446         ast_cli(a->fd, "  Entity ID:                   %s\n", eid_str);
447         ast_cli(a->fd, "  Default language:            %s\n", defaultlanguage);
448         ast_cli(a->fd, "  Language prefix:             %s\n", ast_language_is_prefix ? "Enabled" : "Disabled");
449         ast_cli(a->fd, "  User name and group:         %s/%s\n", ast_config_AST_RUN_USER, ast_config_AST_RUN_GROUP);
450         ast_cli(a->fd, "  Executable includes:         %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_EXEC_INCLUDES) ? "Enabled" : "Disabled");
451         ast_cli(a->fd, "  Transcode via SLIN:          %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSCODE_VIA_SLIN) ? "Enabled" : "Disabled");
452         ast_cli(a->fd, "  Internal timing:             %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_INTERNAL_TIMING) ? "Enabled" : "Disabled");
453         ast_cli(a->fd, "  Transmit silence during rec: %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_INTERNAL_TIMING) ? "Enabled" : "Disabled");
454
455         ast_cli(a->fd, "\n* Subsystems\n");
456         ast_cli(a->fd, "  -------------\n");
457         ast_cli(a->fd, "  Manager (AMI):               %s\n", check_manager_enabled() ? "Enabled" : "Disabled");
458         ast_cli(a->fd, "  Web Manager (AMI/HTTP):      %s\n", check_webmanager_enabled() ? "Enabled" : "Disabled");
459         ast_cli(a->fd, "  Call data records:           %s\n", check_cdr_enabled() ? "Enabled" : "Disabled");
460         ast_cli(a->fd, "  Realtime Architecture (ARA): %s\n", ast_realtime_enabled() ? "Enabled" : "Disabled");
461
462         /*! \todo we could check musiconhold, voicemail, smdi, adsi, queues  */
463
464         ast_cli(a->fd, "\n* Directories\n");
465         ast_cli(a->fd, "  -------------\n");
466         ast_cli(a->fd, "  Configuration file:          %s\n", ast_config_AST_CONFIG_FILE);
467         ast_cli(a->fd, "  Configuration directory:     %s\n", ast_config_AST_CONFIG_DIR);
468         ast_cli(a->fd, "  Module directory:            %s\n", ast_config_AST_MODULE_DIR);
469         ast_cli(a->fd, "  Spool directory:             %s\n", ast_config_AST_SPOOL_DIR);
470         ast_cli(a->fd, "  Log directory:               %s\n", ast_config_AST_LOG_DIR);
471         ast_cli(a->fd, "\n\n");
472         return CLI_SUCCESS;
473 }
474
475 static char *handle_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
476 {
477         int count = 0;
478         struct thread_list_t *cur;
479         switch (cmd) {
480         case CLI_INIT:
481                 e->command = "core show threads";
482                 e->usage = 
483                         "Usage: core show threads\n"
484                         "       List threads currently active in the system.\n";
485                 return NULL;
486         case CLI_GENERATE:
487                 return NULL;
488         }
489
490         AST_RWLIST_RDLOCK(&thread_list);
491         AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
492                 ast_cli(a->fd, "%p %s\n", (void *)cur->id, cur->name);
493                 count++;
494         }
495         AST_RWLIST_UNLOCK(&thread_list);
496         ast_cli(a->fd, "%d threads listed.\n", count);
497         return CLI_SUCCESS;
498 }
499
500 #if defined(HAVE_SYSINFO)
501 /*! \brief Give an overview of system statistics */
502 static char *handle_show_sysinfo(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
503 {
504         struct sysinfo sys_info;
505         switch (cmd) {
506         case CLI_INIT:
507                 e->command = "core show sysinfo";
508                 e->usage =
509                         "Usage: core show sysinfo\n"
510                         "       List current system information.\n";
511                 return NULL;
512         case CLI_GENERATE:
513                 return NULL;
514         }
515         if (sysinfo(&sys_info)) {
516                 ast_cli(a->fd, "FAILED to retrieve system information\n\n");
517                 return CLI_FAILURE;
518         }
519         ast_cli(a->fd, "\nSystem Statistics\n");
520         ast_cli(a->fd, "-----------------\n");
521         ast_cli(a->fd, "  System Uptime:             %ld hours\n", sys_info.uptime/3600);
522         ast_cli(a->fd, "  Total RAM:                 %ld KiB\n", (sys_info.totalram * sys_info.mem_unit)/1024);
523         ast_cli(a->fd, "  Free RAM:                  %ld KiB\n", (sys_info.freeram * sys_info.mem_unit)/1024);
524         ast_cli(a->fd, "  Buffer RAM:                %ld KiB\n", (sys_info.bufferram * sys_info.mem_unit)/1024);
525         ast_cli(a->fd, "  Total Swap Space:          %ld KiB\n", (sys_info.totalswap * sys_info.mem_unit)/1024);
526         ast_cli(a->fd, "  Free Swap Space:           %ld KiB\n\n", (sys_info.freeswap * sys_info.mem_unit)/1024);
527         ast_cli(a->fd, "  Number of Processes:       %d \n\n", sys_info.procs);
528         return CLI_SUCCESS;
529 }
530 #endif
531
532 struct profile_entry {
533         const char *name;
534         uint64_t        scale;  /* if non-zero, values are scaled by this */
535         int64_t mark;
536         int64_t value;
537         int64_t events;
538 };
539
540 struct profile_data {
541         int entries;
542         int max_size;
543         struct profile_entry e[0];
544 };
545
546 static struct profile_data *prof_data;
547
548 /*! \brief allocates a counter with a given name and scale.
549  * \return Returns the identifier of the counter.
550  */
551 int ast_add_profile(const char *name, uint64_t scale)
552 {
553         int l = sizeof(struct profile_data);
554         int n = 10;     /* default entries */
555
556         if (prof_data == NULL) {
557                 prof_data = ast_calloc(1, l + n*sizeof(struct profile_entry));
558                 if (prof_data == NULL)
559                         return -1;
560                 prof_data->entries = 0;
561                 prof_data->max_size = n;
562         }
563         if (prof_data->entries >= prof_data->max_size) {
564                 void *p;
565                 n = prof_data->max_size + 20;
566                 p = ast_realloc(prof_data, l + n*sizeof(struct profile_entry));
567                 if (p == NULL)
568                         return -1;
569                 prof_data = p;
570                 prof_data->max_size = n;
571         }
572         n = prof_data->entries++;
573         prof_data->e[n].name = ast_strdup(name);
574         prof_data->e[n].value = 0;
575         prof_data->e[n].events = 0;
576         prof_data->e[n].mark = 0;
577         prof_data->e[n].scale = scale;
578         return n;
579 }
580
581 int64_t ast_profile(int i, int64_t delta)
582 {
583         if (!prof_data || i < 0 || i > prof_data->entries)      /* invalid index */
584                 return 0;
585         if (prof_data->e[i].scale > 1)
586                 delta /= prof_data->e[i].scale;
587         prof_data->e[i].value += delta;
588         prof_data->e[i].events++;
589         return prof_data->e[i].value;
590 }
591
592 /* The RDTSC instruction was introduced on the Pentium processor and is not
593  * implemented on certain clones, like the Cyrix 586. Hence, the previous
594  * expectation of __i386__ was in error. */
595 #if defined ( __i686__) && (defined(__FreeBSD__) || defined(linux))
596 #if defined(__FreeBSD__)
597 #include <machine/cpufunc.h>
598 #elif defined(linux)
599 static __inline uint64_t
600 rdtsc(void)
601
602         uint64_t rv;
603
604         __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
605         return (rv);
606 }
607 #endif
608 #else   /* supply a dummy function on other platforms */
609 static __inline uint64_t
610 rdtsc(void)
611 {
612         return 0;
613 }
614 #endif
615
616 int64_t ast_mark(int i, int startstop)
617 {
618         if (!prof_data || i < 0 || i > prof_data->entries) /* invalid index */
619                 return 0;
620         if (startstop == 1)
621                 prof_data->e[i].mark = rdtsc();
622         else {
623                 prof_data->e[i].mark = (rdtsc() - prof_data->e[i].mark);
624                 if (prof_data->e[i].scale > 1)
625                         prof_data->e[i].mark /= prof_data->e[i].scale;
626                 prof_data->e[i].value += prof_data->e[i].mark;
627                 prof_data->e[i].events++;
628         }
629         return prof_data->e[i].mark;
630 }
631
632 #define DEFINE_PROFILE_MIN_MAX_VALUES min = 0; \
633         max = prof_data->entries;\
634         if  (a->argc > 3) { /* specific entries */ \
635                 if (isdigit(a->argv[3][0])) { \
636                         min = atoi(a->argv[3]); \
637                         if (a->argc == 5 && strcmp(a->argv[4], "-")) \
638                                 max = atoi(a->argv[4]); \
639                 } else \
640                         search = a->argv[3]; \
641         } \
642         if (max > prof_data->entries) \
643                 max = prof_data->entries;
644
645 static char *handle_show_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
646 {
647         int i, min, max;
648         char *search = NULL;
649         switch (cmd) {
650         case CLI_INIT:
651                 e->command = "core show profile";
652                 e->usage = "Usage: core show profile\n"
653                            "       show profile information";
654                 return NULL;
655         case CLI_GENERATE:
656                 return NULL;
657         }
658
659         if (prof_data == NULL)
660                 return 0;
661
662         DEFINE_PROFILE_MIN_MAX_VALUES;
663         ast_cli(a->fd, "profile values (%d, allocated %d)\n-------------------\n",
664                 prof_data->entries, prof_data->max_size);
665         ast_cli(a->fd, "%6s   %8s  %10s %12s %12s  %s\n", "ID", "Scale", "Events",
666                         "Value", "Average", "Name");
667         for (i = min; i < max; i++) {
668                 struct profile_entry *entry = &prof_data->e[i];
669                 if (!search || strstr(entry->name, search))
670                     ast_cli(a->fd, "%6d: [%8ld] %10ld %12lld %12lld  %s\n",
671                         i,
672                         (long)entry->scale,
673                         (long)entry->events, (long long)entry->value,
674                         (long long)(entry->events ? entry->value / entry->events : entry->value),
675                         entry->name);
676         }
677         return CLI_SUCCESS;
678 }
679
680 static char *handle_clear_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
681 {
682         int i, min, max;
683         char *search = NULL;
684         switch (cmd) {
685         case CLI_INIT:
686                 e->command = "core clear profile";
687                 e->usage = "Usage: core clear profile\n"
688                            "       clear profile information";
689                 return NULL;
690         case CLI_GENERATE:
691                 return NULL;
692         }
693
694         if (prof_data == NULL)
695                 return 0;
696
697         DEFINE_PROFILE_MIN_MAX_VALUES;
698         for (i= min; i < max; i++) {
699                 if (!search || strstr(prof_data->e[i].name, search)) {
700                         prof_data->e[i].value = 0;
701                         prof_data->e[i].events = 0;
702                 }
703         }
704         return CLI_SUCCESS;
705 }
706 #undef DEFINE_PROFILE_MIN_MAX_VALUES
707
708 /*! \brief CLI command to list module versions */
709 static char *handle_show_version_files(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
710 {
711 #define FORMAT "%-25.25s %-40.40s\n"
712         struct file_version *iterator;
713         regex_t regexbuf;
714         int havepattern = 0;
715         int havename = 0;
716         int count_files = 0;
717         char *ret = NULL;
718         int matchlen, which = 0;
719         struct file_version *find;
720
721         switch (cmd) {
722         case CLI_INIT:
723                 e->command = "core show file version [like]";
724                 e->usage = 
725                         "Usage: core show file version [like <pattern>]\n"
726                         "       Lists the revision numbers of the files used to build this copy of Asterisk.\n"
727                         "       Optional regular expression pattern is used to filter the file list.\n";
728                 return NULL;
729         case CLI_GENERATE:
730                 matchlen = strlen(a->word);
731                 if (a->pos != 3)
732                         return NULL;
733                 AST_RWLIST_RDLOCK(&file_versions);
734                 AST_RWLIST_TRAVERSE(&file_versions, find, list) {
735                         if (!strncasecmp(a->word, find->file, matchlen) && ++which > a->n) {
736                                 ret = ast_strdup(find->file);
737                                 break;
738                         }
739                 }
740                 AST_RWLIST_UNLOCK(&file_versions);
741                 return ret;
742         }
743
744
745         switch (a->argc) {
746         case 6:
747                 if (!strcasecmp(a->argv[4], "like")) {
748                         if (regcomp(&regexbuf, a->argv[5], REG_EXTENDED | REG_NOSUB))
749                                 return CLI_SHOWUSAGE;
750                         havepattern = 1;
751                 } else
752                         return CLI_SHOWUSAGE;
753                 break;
754         case 5:
755                 havename = 1;
756                 break;
757         case 4:
758                 break;
759         default:
760                 return CLI_SHOWUSAGE;
761         }
762
763         ast_cli(a->fd, FORMAT, "File", "Revision");
764         ast_cli(a->fd, FORMAT, "----", "--------");
765         AST_RWLIST_RDLOCK(&file_versions);
766         AST_RWLIST_TRAVERSE(&file_versions, iterator, list) {
767                 if (havename && strcasecmp(iterator->file, a->argv[4]))
768                         continue;
769
770                 if (havepattern && regexec(&regexbuf, iterator->file, 0, NULL, 0))
771                         continue;
772
773                 ast_cli(a->fd, FORMAT, iterator->file, iterator->version);
774                 count_files++;
775                 if (havename)
776                         break;
777         }
778         AST_RWLIST_UNLOCK(&file_versions);
779         if (!havename) {
780                 ast_cli(a->fd, "%d files listed.\n", count_files);
781         }
782
783         if (havepattern)
784                 regfree(&regexbuf);
785
786         return CLI_SUCCESS;
787 #undef FORMAT
788 }
789
790 #endif /* ! LOW_MEMORY */
791
792 int ast_register_atexit(void (*func)(void))
793 {
794         struct ast_atexit *ae;
795
796         if (!(ae = ast_calloc(1, sizeof(*ae))))
797                 return -1;
798
799         ae->func = func;
800
801         ast_unregister_atexit(func);    
802
803         AST_RWLIST_WRLOCK(&atexits);
804         AST_RWLIST_INSERT_HEAD(&atexits, ae, list);
805         AST_RWLIST_UNLOCK(&atexits);
806
807         return 0;
808 }
809
810 void ast_unregister_atexit(void (*func)(void))
811 {
812         struct ast_atexit *ae = NULL;
813
814         AST_RWLIST_WRLOCK(&atexits);
815         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&atexits, ae, list) {
816                 if (ae->func == func) {
817                         AST_RWLIST_REMOVE_CURRENT(list);
818                         break;
819                 }
820         }
821         AST_RWLIST_TRAVERSE_SAFE_END;
822         AST_RWLIST_UNLOCK(&atexits);
823
824         if (ae)
825                 free(ae);
826 }
827
828 /* Sending commands from consoles back to the daemon requires a terminating NULL */
829 static int fdsend(int fd, const char *s)
830 {
831         return write(fd, s, strlen(s) + 1);
832 }
833
834 /* Sending messages from the daemon back to the display requires _excluding_ the terminating NULL */
835 static int fdprint(int fd, const char *s)
836 {
837         return write(fd, s, strlen(s));
838 }
839
840 /*! \brief NULL handler so we can collect the child exit status */
841 static void null_sig_handler(int sig)
842 {
843
844 }
845
846 AST_MUTEX_DEFINE_STATIC(safe_system_lock);
847 /*! \brief Keep track of how many threads are currently trying to wait*() on
848  *  a child process */
849 static unsigned int safe_system_level = 0;
850 static void *safe_system_prev_handler;
851
852 void ast_replace_sigchld(void)
853 {
854         unsigned int level;
855
856         ast_mutex_lock(&safe_system_lock);
857         level = safe_system_level++;
858
859         /* only replace the handler if it has not already been done */
860         if (level == 0)
861                 safe_system_prev_handler = signal(SIGCHLD, null_sig_handler);
862
863         ast_mutex_unlock(&safe_system_lock);
864 }
865
866 void ast_unreplace_sigchld(void)
867 {
868         unsigned int level;
869
870         ast_mutex_lock(&safe_system_lock);
871         level = --safe_system_level;
872
873         /* only restore the handler if we are the last one */
874         if (level == 0)
875                 signal(SIGCHLD, safe_system_prev_handler);
876
877         ast_mutex_unlock(&safe_system_lock);
878 }
879
880 int ast_safe_system(const char *s)
881 {
882         pid_t pid;
883         int res;
884         struct rusage rusage;
885         int status;
886
887 #if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
888         ast_replace_sigchld();
889
890 #ifdef HAVE_WORKING_FORK
891         pid = fork();
892 #else
893         pid = vfork();
894 #endif  
895
896         if (pid == 0) {
897 #ifdef HAVE_WORKING_FORK
898                 if (ast_opt_high_priority)
899                         ast_set_priority(0);
900                 /* Close file descriptors and launch system command */
901                 ast_close_fds_above_n(STDERR_FILENO);
902 #endif
903                 execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
904                 _exit(1);
905         } else if (pid > 0) {
906                 for (;;) {
907                         res = wait4(pid, &status, 0, &rusage);
908                         if (res > -1) {
909                                 res = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
910                                 break;
911                         } else if (errno != EINTR) 
912                                 break;
913                 }
914         } else {
915                 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
916                 res = -1;
917         }
918
919         ast_unreplace_sigchld();
920 #else
921         res = -1;
922 #endif
923
924         return res;
925 }
926
927 void ast_console_toggle_loglevel(int fd, int level, int state)
928 {
929         int x;
930         for (x = 0;x < AST_MAX_CONNECTS; x++) {
931                 if (fd == consoles[x].fd) {
932                         consoles[x].levels[level] = state;
933                         return;
934                 }
935         }
936 }
937
938 /*!
939  * \brief mute or unmute a console from logging
940  */
941 void ast_console_toggle_mute(int fd, int silent) {
942         int x;
943         for (x = 0;x < AST_MAX_CONNECTS; x++) {
944                 if (fd == consoles[x].fd) {
945                         if (consoles[x].mute) {
946                                 consoles[x].mute = 0;
947                                 if (!silent)
948                                         ast_cli(fd, "Console is not muted anymore.\n");
949                         } else {
950                                 consoles[x].mute = 1;
951                                 if (!silent)
952                                         ast_cli(fd, "Console is muted.\n");
953                         }
954                         return;
955                 }
956         }
957         ast_cli(fd, "Couldn't find remote console.\n");
958 }
959
960 /*!
961  * \brief log the string to all attached console clients
962  */
963 static void ast_network_puts_mutable(const char *string, int level)
964 {
965         int x;
966         for (x = 0;x < AST_MAX_CONNECTS; x++) {
967                 if (consoles[x].mute)
968                         continue;
969                 if (consoles[x].fd > -1) {
970                         if (!consoles[x].levels[level]) 
971                                 fdprint(consoles[x].p[1], string);
972                 }
973         }
974 }
975
976 /*!
977  * \brief log the string to the console, and all attached
978  * console clients
979  */
980 void ast_console_puts_mutable(const char *string, int level)
981 {
982         fputs(string, stdout);
983         fflush(stdout);
984         ast_network_puts_mutable(string, level);
985 }
986
987 /*!
988  * \brief write the string to all attached console clients
989  */
990 static void ast_network_puts(const char *string)
991 {
992         int x;
993         for (x = 0; x < AST_MAX_CONNECTS; x++) {
994                 if (consoles[x].fd > -1) 
995                         fdprint(consoles[x].p[1], string);
996         }
997 }
998
999 /*!
1000  * write the string to the console, and all attached
1001  * console clients
1002  */
1003 void ast_console_puts(const char *string)
1004 {
1005         fputs(string, stdout);
1006         fflush(stdout);
1007         ast_network_puts(string);
1008 }
1009
1010 static void network_verboser(const char *s)
1011 {
1012         ast_network_puts_mutable(s, __LOG_VERBOSE);
1013 }
1014
1015 static pthread_t lthread;
1016
1017 /*!
1018  * \brief read() function supporting the reception of user credentials.
1019  *
1020  * \param fd Socket file descriptor.
1021  * \param buffer Receive buffer.
1022  * \param size 'buffer' size.
1023  * \param con Console structure to set received credentials
1024  * \retval -1 on error
1025  * \retval the number of bytes received on success.
1026  */
1027 static int read_credentials(int fd, char *buffer, size_t size, struct console *con)
1028 {
1029 #if defined(SO_PEERCRED)
1030         struct ucred cred;
1031         socklen_t len = sizeof(cred);
1032 #endif
1033         int result, uid, gid;
1034
1035         result = read(fd, buffer, size);
1036         if (result < 0) {
1037                 return result;
1038         }
1039
1040 #if defined(SO_PEERCRED)
1041         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len)) {
1042                 return result;
1043         }
1044         uid = cred.uid;
1045         gid = cred.gid;
1046 #elif defined(HAVE_GETPEEREID)
1047         if (getpeereid(fd, &uid, &gid)) {
1048                 return result;
1049         }
1050 #else
1051         return result;
1052 #endif
1053         con->uid = uid;
1054         con->gid = gid;
1055
1056         return result;
1057 }
1058
1059 static void *netconsole(void *vconsole)
1060 {
1061         struct console *con = vconsole;
1062         char hostname[MAXHOSTNAMELEN] = "";
1063         char tmp[512];
1064         int res;
1065         struct pollfd fds[2];
1066         
1067         if (gethostname(hostname, sizeof(hostname)-1))
1068                 ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
1069         snprintf(tmp, sizeof(tmp), "%s/%ld/%s\n", hostname, (long)ast_mainpid, ast_get_version());
1070         fdprint(con->fd, tmp);
1071         for (;;) {
1072                 fds[0].fd = con->fd;
1073                 fds[0].events = POLLIN;
1074                 fds[0].revents = 0;
1075                 fds[1].fd = con->p[0];
1076                 fds[1].events = POLLIN;
1077                 fds[1].revents = 0;
1078
1079                 res = poll(fds, 2, -1);
1080                 if (res < 0) {
1081                         if (errno != EINTR)
1082                                 ast_log(LOG_WARNING, "poll returned < 0: %s\n", strerror(errno));
1083                         continue;
1084                 }
1085                 if (fds[0].revents) {
1086                         res = read_credentials(con->fd, tmp, sizeof(tmp), con);
1087                         if (res < 1) {
1088                                 break;
1089                         }
1090                         tmp[res] = 0;
1091                         if (strncmp(tmp, "cli quit after ", 15) == 0) {
1092                                 ast_cli_command_multiple_full(con->uid, con->gid, con->fd, res - 15, tmp + 15);
1093                                 break;
1094                         }
1095                         ast_cli_command_multiple_full(con->uid, con->gid, con->fd, res, tmp);
1096                 }
1097                 if (fds[1].revents) {
1098                         res = read_credentials(con->p[0], tmp, sizeof(tmp), con);
1099                         if (res < 1) {
1100                                 ast_log(LOG_ERROR, "read returned %d\n", res);
1101                                 break;
1102                         }
1103                         res = write(con->fd, tmp, res);
1104                         if (res < 1)
1105                                 break;
1106                 }
1107         }
1108         ast_verb(3, "Remote UNIX connection disconnected\n");
1109         close(con->fd);
1110         close(con->p[0]);
1111         close(con->p[1]);
1112         con->fd = -1;
1113         
1114         return NULL;
1115 }
1116
1117 static void *listener(void *unused)
1118 {
1119         struct sockaddr_un sunaddr;
1120         int s;
1121         socklen_t len;
1122         int x;
1123         int flags;
1124         struct pollfd fds[1];
1125         for (;;) {
1126                 if (ast_socket < 0)
1127                         return NULL;
1128                 fds[0].fd = ast_socket;
1129                 fds[0].events = POLLIN;
1130                 s = poll(fds, 1, -1);
1131                 pthread_testcancel();
1132                 if (s < 0) {
1133                         if (errno != EINTR)
1134                                 ast_log(LOG_WARNING, "poll returned error: %s\n", strerror(errno));
1135                         continue;
1136                 }
1137                 len = sizeof(sunaddr);
1138                 s = accept(ast_socket, (struct sockaddr *)&sunaddr, &len);
1139                 if (s < 0) {
1140                         if (errno != EINTR)
1141                                 ast_log(LOG_WARNING, "Accept returned %d: %s\n", s, strerror(errno));
1142                 } else {
1143 #if !defined(SO_PASSCRED)
1144                         {
1145 #else
1146                         int sckopt = 1;
1147                         /* turn on socket credentials passing. */
1148                         if (setsockopt(s, SOL_SOCKET, SO_PASSCRED, &sckopt, sizeof(sckopt)) < 0) {
1149                                 ast_log(LOG_WARNING, "Unable to turn on socket credentials passing\n");
1150                         } else {
1151 #endif
1152                                 for (x = 0; x < AST_MAX_CONNECTS; x++) {
1153                                         if (consoles[x].fd >= 0) {
1154                                                 continue;
1155                                         }
1156                                         if (socketpair(AF_LOCAL, SOCK_STREAM, 0, consoles[x].p)) {
1157                                                 ast_log(LOG_ERROR, "Unable to create pipe: %s\n", strerror(errno));
1158                                                 consoles[x].fd = -1;
1159                                                 fdprint(s, "Server failed to create pipe\n");
1160                                                 close(s);
1161                                                 break;
1162                                         }
1163                                         flags = fcntl(consoles[x].p[1], F_GETFL);
1164                                         fcntl(consoles[x].p[1], F_SETFL, flags | O_NONBLOCK);
1165                                         consoles[x].fd = s;
1166                                         consoles[x].mute = 1; /* Default is muted, we will un-mute if necessary */
1167                                         /* Default uid and gid to -2, so then in cli.c/cli_has_permissions() we will be able
1168                                            to know if the user didn't send the credentials. */
1169                                         consoles[x].uid = -2;
1170                                         consoles[x].gid = -2;
1171                                         if (ast_pthread_create_detached_background(&consoles[x].t, NULL, netconsole, &consoles[x])) {
1172                                                 ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno));
1173                                                 close(consoles[x].p[0]);
1174                                                 close(consoles[x].p[1]);
1175                                                 consoles[x].fd = -1;
1176                                                 fdprint(s, "Server failed to spawn thread\n");
1177                                                 close(s);
1178                                         }
1179                                         break;
1180                                 }
1181                                 if (x >= AST_MAX_CONNECTS) {
1182                                         fdprint(s, "No more connections allowed\n");
1183                                         ast_log(LOG_WARNING, "No more connections allowed\n");
1184                                         close(s);
1185                                 } else if (consoles[x].fd > -1) 
1186                                         ast_verb(3, "Remote UNIX connection\n");
1187                         }
1188                 }
1189         }
1190         return NULL;
1191 }
1192
1193 static int ast_makesocket(void)
1194 {
1195         struct sockaddr_un sunaddr;
1196         int res;
1197         int x;
1198         uid_t uid = -1;
1199         gid_t gid = -1;
1200
1201         for (x = 0; x < AST_MAX_CONNECTS; x++)  
1202                 consoles[x].fd = -1;
1203         unlink(ast_config_AST_SOCKET);
1204         ast_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
1205         if (ast_socket < 0) {
1206                 ast_log(LOG_WARNING, "Unable to create control socket: %s\n", strerror(errno));
1207                 return -1;
1208         }               
1209         memset(&sunaddr, 0, sizeof(sunaddr));
1210         sunaddr.sun_family = AF_LOCAL;
1211         ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
1212         res = bind(ast_socket, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
1213         if (res) {
1214                 ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1215                 close(ast_socket);
1216                 ast_socket = -1;
1217                 return -1;
1218         }
1219         res = listen(ast_socket, 2);
1220         if (res < 0) {
1221                 ast_log(LOG_WARNING, "Unable to listen on socket %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1222                 close(ast_socket);
1223                 ast_socket = -1;
1224                 return -1;
1225         }
1226         if (ast_register_verbose(network_verboser)) {
1227                 ast_log(LOG_WARNING, "Unable to register network verboser?\n");
1228         }
1229
1230         ast_pthread_create_background(&lthread, NULL, listener, NULL);
1231
1232         if (!ast_strlen_zero(ast_config_AST_CTL_OWNER)) {
1233                 struct passwd *pw;
1234                 if ((pw = getpwnam(ast_config_AST_CTL_OWNER)) == NULL)
1235                         ast_log(LOG_WARNING, "Unable to find uid of user %s\n", ast_config_AST_CTL_OWNER);
1236                 else
1237                         uid = pw->pw_uid;
1238         }
1239                 
1240         if (!ast_strlen_zero(ast_config_AST_CTL_GROUP)) {
1241                 struct group *grp;
1242                 if ((grp = getgrnam(ast_config_AST_CTL_GROUP)) == NULL)
1243                         ast_log(LOG_WARNING, "Unable to find gid of group %s\n", ast_config_AST_CTL_GROUP);
1244                 else
1245                         gid = grp->gr_gid;
1246         }
1247
1248         if (chown(ast_config_AST_SOCKET, uid, gid) < 0)
1249                 ast_log(LOG_WARNING, "Unable to change ownership of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1250
1251         if (!ast_strlen_zero(ast_config_AST_CTL_PERMISSIONS)) {
1252                 int p1;
1253                 mode_t p;
1254                 sscanf(ast_config_AST_CTL_PERMISSIONS, "%o", &p1);
1255                 p = p1;
1256                 if ((chmod(ast_config_AST_SOCKET, p)) < 0)
1257                         ast_log(LOG_WARNING, "Unable to change file permissions of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1258         }
1259
1260         return 0;
1261 }
1262
1263 static int ast_tryconnect(void)
1264 {
1265         struct sockaddr_un sunaddr;
1266         int res;
1267         ast_consock = socket(PF_LOCAL, SOCK_STREAM, 0);
1268         if (ast_consock < 0) {
1269                 ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
1270                 return 0;
1271         }
1272         memset(&sunaddr, 0, sizeof(sunaddr));
1273         sunaddr.sun_family = AF_LOCAL;
1274         ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
1275         res = connect(ast_consock, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
1276         if (res) {
1277                 close(ast_consock);
1278                 ast_consock = -1;
1279                 return 0;
1280         } else
1281                 return 1;
1282 }
1283
1284 /*! \brief Urgent handler
1285
1286  Called by soft_hangup to interrupt the poll, read, or other
1287  system call.  We don't actually need to do anything though.  
1288  Remember: Cannot EVER ast_log from within a signal handler 
1289  */
1290 static void urg_handler(int num)
1291 {
1292         signal(num, urg_handler);
1293         return;
1294 }
1295
1296 static void hup_handler(int num)
1297 {
1298         int a = 0;
1299         if (option_verbose > 1) 
1300                 printf("Received HUP signal -- Reloading configs\n");
1301         if (restartnow)
1302                 execvp(_argv[0], _argv);
1303         sig_flags.need_reload = 1;
1304         if (sig_alert_pipe[1] != -1) {
1305                 if (write(sig_alert_pipe[1], &a, sizeof(a)) < 0) {
1306                         fprintf(stderr, "hup_handler: write() failed: %s\n", strerror(errno));
1307                 }
1308         }
1309         signal(num, hup_handler);
1310 }
1311
1312 static void child_handler(int sig)
1313 {
1314         /* Must not ever ast_log or ast_verbose within signal handler */
1315         int n, status;
1316
1317         /*
1318          * Reap all dead children -- not just one
1319          */
1320         for (n = 0; wait4(-1, &status, WNOHANG, NULL) > 0; n++)
1321                 ;
1322         if (n == 0 && option_debug)     
1323                 printf("Huh?  Child handler, but nobody there?\n");
1324         signal(sig, child_handler);
1325 }
1326
1327 /*! \brief Set maximum open files */
1328 static void set_ulimit(int value)
1329 {
1330         struct rlimit l = {0, 0};
1331         
1332         if (value <= 0) {
1333                 ast_log(LOG_WARNING, "Unable to change max files open to invalid value %i\n",value);
1334                 return;
1335         }
1336         
1337         l.rlim_cur = value;
1338         l.rlim_max = value;
1339         
1340         if (setrlimit(RLIMIT_NOFILE, &l)) {
1341                 ast_log(LOG_WARNING, "Unable to disable core size resource limit: %s\n",strerror(errno));
1342                 return;
1343         }
1344         
1345         ast_log(LOG_NOTICE, "Setting max files open to %d\n",value);
1346         
1347         return;
1348 }
1349
1350 /*! \brief Set an X-term or screen title */
1351 static void set_title(char *text)
1352 {
1353         if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
1354                 fprintf(stdout, "\033]2;%s\007", text);
1355 }
1356
1357 static void set_icon(char *text)
1358 {
1359         if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
1360                 fprintf(stdout, "\033]1;%s\007", text);
1361 }
1362
1363 /*! \brief We set ourselves to a high priority, that we might pre-empt everything
1364    else.  If your PBX has heavy activity on it, this is a good thing.  */
1365 int ast_set_priority(int pri)
1366 {
1367         struct sched_param sched;
1368         memset(&sched, 0, sizeof(sched));
1369 #ifdef __linux__
1370         if (pri) {  
1371                 sched.sched_priority = 10;
1372                 if (sched_setscheduler(0, SCHED_RR, &sched)) {
1373                         ast_log(LOG_WARNING, "Unable to set high priority\n");
1374                         return -1;
1375                 } else
1376                         if (option_verbose)
1377                                 ast_verbose("Set to realtime thread\n");
1378         } else {
1379                 sched.sched_priority = 0;
1380                 /* According to the manpage, these parameters can never fail. */
1381                 sched_setscheduler(0, SCHED_OTHER, &sched);
1382         }
1383 #else
1384         if (pri) {
1385                 if (setpriority(PRIO_PROCESS, 0, -10) == -1) {
1386                         ast_log(LOG_WARNING, "Unable to set high priority\n");
1387                         return -1;
1388                 } else
1389                         if (option_verbose)
1390                                 ast_verbose("Set to high priority\n");
1391         } else {
1392                 /* According to the manpage, these parameters can never fail. */
1393                 setpriority(PRIO_PROCESS, 0, 0);
1394         }
1395 #endif
1396         return 0;
1397 }
1398
1399 static void ast_run_atexits(void)
1400 {
1401         struct ast_atexit *ae;
1402         AST_RWLIST_RDLOCK(&atexits);
1403         AST_RWLIST_TRAVERSE(&atexits, ae, list) {
1404                 if (ae->func) 
1405                         ae->func();
1406         }
1407         AST_RWLIST_UNLOCK(&atexits);
1408 }
1409
1410 static void quit_handler(int num, int niceness, int safeshutdown, int restart)
1411 {
1412         char filename[80] = "";
1413         time_t s,e;
1414         int x;
1415         /* Try to get as many CDRs as possible submitted to the backend engines (if in batch mode) */
1416         ast_cdr_engine_term();
1417         if (safeshutdown) {
1418                 shuttingdown = 1;
1419                 if (!niceness) {
1420                         /* Begin shutdown routine, hanging up active channels */
1421                         ast_begin_shutdown(1);
1422                         if (option_verbose && ast_opt_console)
1423                                 ast_verbose("Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
1424                         time(&s);
1425                         for (;;) {
1426                                 time(&e);
1427                                 /* Wait up to 15 seconds for all channels to go away */
1428                                 if ((e - s) > 15)
1429                                         break;
1430                                 if (!ast_active_channels())
1431                                         break;
1432                                 if (!shuttingdown)
1433                                         break;
1434                                 /* Sleep 1/10 of a second */
1435                                 usleep(100000);
1436                         }
1437                 } else {
1438                         if (niceness < 2)
1439                                 ast_begin_shutdown(0);
1440                         if (option_verbose && ast_opt_console)
1441                                 ast_verbose("Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt");
1442                         for (;;) {
1443                                 if (!ast_active_channels())
1444                                         break;
1445                                 if (!shuttingdown)
1446                                         break;
1447                                 sleep(1);
1448                         }
1449                 }
1450
1451                 if (!shuttingdown) {
1452                         if (option_verbose && ast_opt_console)
1453                                 ast_verbose("Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
1454                         return;
1455                 }
1456
1457                 if (niceness)
1458                         ast_module_shutdown();
1459         }
1460         if (ast_opt_console || (ast_opt_remote && !ast_opt_exec)) {
1461                 if (getenv("HOME")) 
1462                         snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
1463                 if (!ast_strlen_zero(filename))
1464                         ast_el_write_history(filename);
1465                 if (el != NULL)
1466                         el_end(el);
1467                 if (el_hist != NULL)
1468                         history_end(el_hist);
1469         }
1470         if (option_verbose)
1471                 ast_verbose("Executing last minute cleanups\n");
1472         ast_run_atexits();
1473         /* Called on exit */
1474         if (option_verbose && ast_opt_console)
1475                 ast_verbose("Asterisk %s ending (%d).\n", ast_active_channels() ? "uncleanly" : "cleanly", num);
1476         ast_debug(1, "Asterisk ending (%d).\n", num);
1477         manager_event(EVENT_FLAG_SYSTEM, "Shutdown", "Shutdown: %s\r\nRestart: %s\r\n", ast_active_channels() ? "Uncleanly" : "Cleanly", restart ? "True" : "False");
1478         if (ast_socket > -1) {
1479                 pthread_cancel(lthread);
1480                 close(ast_socket);
1481                 ast_socket = -1;
1482                 unlink(ast_config_AST_SOCKET);
1483         }
1484         if (ast_consock > -1)
1485                 close(ast_consock);
1486         if (!ast_opt_remote)
1487                 unlink(ast_config_AST_PID);
1488         printf("%s", term_quit());
1489         if (restart) {
1490                 if (option_verbose || ast_opt_console)
1491                         ast_verbose("Preparing for Asterisk restart...\n");
1492                 /* Mark all FD's for closing on exec */
1493                 for (x=3; x < 32768; x++) {
1494                         fcntl(x, F_SETFD, FD_CLOEXEC);
1495                 }
1496                 if (option_verbose || ast_opt_console)
1497                         ast_verbose("Asterisk is now restarting...\n");
1498                 restartnow = 1;
1499
1500                 /* close logger */
1501                 close_logger();
1502
1503                 /* If there is a consolethread running send it a SIGHUP 
1504                    so it can execvp, otherwise we can do it ourselves */
1505                 if ((consolethread != AST_PTHREADT_NULL) && (consolethread != pthread_self())) {
1506                         pthread_kill(consolethread, SIGHUP);
1507                         /* Give the signal handler some time to complete */
1508                         sleep(2);
1509                 } else
1510                         execvp(_argv[0], _argv);
1511         
1512         } else {
1513                 /* close logger */
1514                 close_logger();
1515         }
1516         exit(0);
1517 }
1518
1519 static void __quit_handler(int num)
1520 {
1521         int a = 0;
1522         sig_flags.need_quit = 1;
1523         if (sig_alert_pipe[1] != -1) {
1524                 if (write(sig_alert_pipe[1], &a, sizeof(a)) < 0) {
1525                         fprintf(stderr, "hup_handler: write() failed: %s\n", strerror(errno));
1526                 }
1527         }
1528         /* There is no need to restore the signal handler here, since the app
1529          * is going to exit */
1530 }
1531
1532 static const char *fix_header(char *outbuf, int maxout, const char *s, char *cmp)
1533 {
1534         const char *c;
1535
1536         /* Check for verboser preamble */
1537         if (*s == 127) {
1538                 s++;
1539         }
1540
1541         if (!strncmp(s, cmp, strlen(cmp))) {
1542                 c = s + strlen(cmp);
1543                 term_color(outbuf, cmp, COLOR_GRAY, 0, maxout);
1544                 return c;
1545         }
1546         return NULL;
1547 }
1548
1549 static void console_verboser(const char *s)
1550 {
1551         char tmp[80];
1552         const char *c = NULL;
1553
1554         if ((c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_4)) ||
1555             (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_3)) ||
1556             (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_2)) ||
1557             (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_1))) {
1558                 fputs(tmp, stdout);
1559                 fputs(c, stdout);
1560         } else {
1561                 if (*s == 127) {
1562                         s++;
1563                 }
1564                 fputs(s, stdout);
1565         }
1566
1567         fflush(stdout);
1568         
1569         /* Wake up a poll()ing console */
1570         if (ast_opt_console && consolethread != AST_PTHREADT_NULL)
1571                 pthread_kill(consolethread, SIGURG);
1572 }
1573
1574 static int ast_all_zeros(char *s)
1575 {
1576         while (*s) {
1577                 if (*s > 32)
1578                         return 0;
1579                 s++;  
1580         }
1581         return 1;
1582 }
1583
1584 static void consolehandler(char *s)
1585 {
1586         printf("%s", term_end());
1587         fflush(stdout);
1588
1589         /* Called when readline data is available */
1590         if (!ast_all_zeros(s))
1591                 ast_el_add_history(s);
1592         /* The real handler for bang */
1593         if (s[0] == '!') {
1594                 if (s[1])
1595                         ast_safe_system(s+1);
1596                 else
1597                         ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
1598         } else 
1599                 ast_cli_command(STDOUT_FILENO, s);
1600 }
1601
1602 static int remoteconsolehandler(char *s)
1603 {
1604         int ret = 0;
1605
1606         /* Called when readline data is available */
1607         if (!ast_all_zeros(s))
1608                 ast_el_add_history(s);
1609         /* The real handler for bang */
1610         if (s[0] == '!') {
1611                 if (s[1])
1612                         ast_safe_system(s+1);
1613                 else
1614                         ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
1615                 ret = 1;
1616         }
1617         if ((strncasecmp(s, "quit", 4) == 0 || strncasecmp(s, "exit", 4) == 0) &&
1618             (s[4] == '\0' || isspace(s[4]))) {
1619                 quit_handler(0, 0, 0, 0);
1620                 ret = 1;
1621         }
1622
1623         return ret;
1624 }
1625
1626 static char *handle_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1627 {
1628         switch (cmd) {
1629         case CLI_INIT:
1630                 e->command = "core show version";
1631                 e->usage = 
1632                         "Usage: core show version\n"
1633                         "       Shows Asterisk version information.\n";
1634                 return NULL;
1635         case CLI_GENERATE:
1636                 return NULL;
1637         }
1638
1639         if (a->argc != 3)
1640                 return CLI_SHOWUSAGE;
1641         ast_cli(a->fd, "Asterisk %s built by %s @ %s on a %s running %s on %s\n",
1642                 ast_get_version(), ast_build_user, ast_build_hostname,
1643                 ast_build_machine, ast_build_os, ast_build_date);
1644         return CLI_SUCCESS;
1645 }
1646
1647 #if 0
1648 static int handle_quit(int fd, int argc, char *argv[])
1649 {
1650         if (argc != 1)
1651                 return RESULT_SHOWUSAGE;
1652         quit_handler(0, 0, 1, 0);
1653         return RESULT_SUCCESS;
1654 }
1655 #endif
1656
1657 static char *handle_stop_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1658 {
1659         switch (cmd) {
1660         case CLI_INIT:
1661                 e->command = "core stop now";
1662                 e->usage = 
1663                         "Usage: core stop now\n"
1664                         "       Shuts down a running Asterisk immediately, hanging up all active calls .\n";
1665                 return NULL;
1666         case CLI_GENERATE:
1667                 return NULL;
1668         }
1669
1670         if (a->argc != e->args)
1671                 return CLI_SHOWUSAGE;
1672         quit_handler(0, 0 /* Not nice */, 1 /* safely */, 0 /* not restart */);
1673         return CLI_SUCCESS;
1674 }
1675
1676 static char *handle_stop_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1677 {
1678         switch (cmd) {
1679         case CLI_INIT:
1680                 e->command = "core stop gracefully";
1681                 e->usage = 
1682                         "Usage: core stop gracefully\n"
1683                         "       Causes Asterisk to not accept new calls, and exit when all\n"
1684                         "       active calls have terminated normally.\n";
1685                 return NULL;
1686         case CLI_GENERATE:
1687                 return NULL;
1688         }
1689
1690         if (a->argc != e->args)
1691                 return CLI_SHOWUSAGE;
1692         quit_handler(0, 1 /* nicely */, 1 /* safely */, 0 /* no restart */);
1693         return CLI_SUCCESS;
1694 }
1695
1696 static char *handle_stop_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1697 {
1698         switch (cmd) {
1699         case CLI_INIT:
1700                 e->command = "core stop when convenient";
1701                 e->usage = 
1702                         "Usage: core stop when convenient\n"
1703                         "       Causes Asterisk to perform a shutdown when all active calls have ended.\n";
1704                 return NULL;
1705         case CLI_GENERATE:
1706                 return NULL;
1707         }
1708
1709         if (a->argc != e->args)
1710                 return CLI_SHOWUSAGE;
1711         ast_cli(a->fd, "Waiting for inactivity to perform halt\n");
1712         quit_handler(0, 2 /* really nicely */, 1 /* safely */, 0 /* don't restart */);
1713         return CLI_SUCCESS;
1714 }
1715
1716 static char *handle_restart_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1717 {
1718         switch (cmd) {
1719         case CLI_INIT:
1720                 e->command = "core restart now";
1721                 e->usage = 
1722                         "Usage: core restart now\n"
1723                         "       Causes Asterisk to hangup all calls and exec() itself performing a cold\n"
1724                         "       restart.\n";
1725                 return NULL;
1726         case CLI_GENERATE:
1727                 return NULL;
1728         }
1729
1730         if (a->argc != e->args)
1731                 return CLI_SHOWUSAGE;
1732         quit_handler(0, 0 /* not nicely */, 1 /* safely */, 1 /* restart */);
1733         return CLI_SUCCESS;
1734 }
1735
1736 static char *handle_restart_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1737 {
1738         switch (cmd) {
1739         case CLI_INIT:
1740                 e->command = "core restart gracefully";
1741                 e->usage = 
1742                         "Usage: core restart gracefully\n"
1743                         "       Causes Asterisk to stop accepting new calls and exec() itself performing a cold\n"
1744                         "       restart when all active calls have ended.\n";
1745                 return NULL;
1746         case CLI_GENERATE:
1747                 return NULL;
1748         }
1749
1750         if (a->argc != e->args)
1751                 return CLI_SHOWUSAGE;
1752         quit_handler(0, 1 /* nicely */, 1 /* safely */, 1 /* restart */);
1753         return CLI_SUCCESS;
1754 }
1755
1756 static char *handle_restart_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1757 {
1758         switch (cmd) {
1759         case CLI_INIT:
1760                 e->command = "core restart when convenient";
1761                 e->usage = 
1762                         "Usage: core restart when convenient\n"
1763                         "       Causes Asterisk to perform a cold restart when all active calls have ended.\n";
1764                 return NULL;
1765         case CLI_GENERATE:
1766                 return NULL;
1767         }
1768
1769         if (a->argc != e->args)
1770                 return CLI_SHOWUSAGE;
1771         ast_cli(a->fd, "Waiting for inactivity to perform restart\n");
1772         quit_handler(0, 2 /* really nicely */, 1 /* safely */, 1 /* restart */);
1773         return CLI_SUCCESS;
1774 }
1775
1776 static char *handle_abort_shutdown(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1777 {
1778         switch (cmd) {
1779         case CLI_INIT:
1780                 e->command = "core abort shutdown";
1781                 e->usage = 
1782                         "Usage: core abort shutdown\n"
1783                         "       Causes Asterisk to abort an executing shutdown or restart, and resume normal\n"
1784                         "       call operations.\n";
1785                 return NULL;
1786         case CLI_GENERATE:
1787                 return NULL;
1788         }
1789
1790         if (a->argc != e->args)
1791                 return CLI_SHOWUSAGE;
1792         ast_cancel_shutdown();
1793         shuttingdown = 0;
1794         return CLI_SUCCESS;
1795 }
1796
1797 static char *handle_bang(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1798 {
1799         switch (cmd) {
1800         case CLI_INIT:
1801                 e->command = "!";
1802                 e->usage = 
1803                         "Usage: !<command>\n"
1804                         "       Executes a given shell command\n";
1805                 return NULL;
1806         case CLI_GENERATE:
1807                 return NULL;
1808         }
1809
1810         return CLI_SUCCESS;
1811 }
1812 static const char warranty_lines[] = {
1813         "\n"
1814         "                           NO WARRANTY\n"
1815         "\n"
1816         "BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n"
1817         "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN\n"
1818         "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n"
1819         "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n"
1820         "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
1821         "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS\n"
1822         "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE\n"
1823         "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n"
1824         "REPAIR OR CORRECTION.\n"
1825         "\n"
1826         "IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
1827         "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n"
1828         "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n"
1829         "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n"
1830         "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n"
1831         "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n"
1832         "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n"
1833         "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n"
1834         "POSSIBILITY OF SUCH DAMAGES.\n"
1835 };
1836
1837 static char *show_warranty(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1838 {
1839         switch (cmd) {
1840         case CLI_INIT:
1841                 e->command = "core show warranty";
1842                 e->usage = 
1843                         "Usage: core show warranty\n"
1844                         "       Shows the warranty (if any) for this copy of Asterisk.\n";
1845                 return NULL;
1846         case CLI_GENERATE:
1847                 return NULL;
1848         }
1849
1850         ast_cli(a->fd, "%s", warranty_lines);
1851
1852         return CLI_SUCCESS;
1853 }
1854
1855 static const char license_lines[] = {
1856         "\n"
1857         "This program is free software; you can redistribute it and/or modify\n"
1858         "it under the terms of the GNU General Public License version 2 as\n"
1859         "published by the Free Software Foundation.\n"
1860         "\n"
1861         "This program also contains components licensed under other licenses.\n"
1862         "They include:\n"
1863         "\n"
1864         "This program is distributed in the hope that it will be useful,\n"
1865         "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1866         "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1867         "GNU General Public License for more details.\n"
1868         "\n"
1869         "You should have received a copy of the GNU General Public License\n"
1870         "along with this program; if not, write to the Free Software\n"
1871         "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n"
1872 };
1873
1874 static char *show_license(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1875 {
1876         switch (cmd) {
1877         case CLI_INIT:
1878                 e->command = "core show license";
1879                 e->usage = 
1880                         "Usage: core show license\n"
1881                         "       Shows the license(s) for this copy of Asterisk.\n";
1882                 return NULL;
1883         case CLI_GENERATE:
1884                 return NULL;
1885         }
1886
1887         ast_cli(a->fd, "%s", license_lines);
1888
1889         return CLI_SUCCESS;
1890 }
1891
1892 #define ASTERISK_PROMPT "*CLI> "
1893
1894 #define ASTERISK_PROMPT2 "%s*CLI> "
1895
1896 static struct ast_cli_entry cli_asterisk[] = {
1897         AST_CLI_DEFINE(handle_abort_shutdown, "Cancel a running shutdown"),
1898         AST_CLI_DEFINE(handle_stop_now, "Shut down Asterisk immediately"),
1899         AST_CLI_DEFINE(handle_stop_gracefully, "Gracefully shut down Asterisk"),
1900         AST_CLI_DEFINE(handle_stop_when_convenient, "Shut down Asterisk at empty call volume"),
1901         AST_CLI_DEFINE(handle_restart_now, "Restart Asterisk immediately"), 
1902         AST_CLI_DEFINE(handle_restart_gracefully, "Restart Asterisk gracefully"),
1903         AST_CLI_DEFINE(handle_restart_when_convenient, "Restart Asterisk at empty call volume"),
1904         AST_CLI_DEFINE(show_warranty, "Show the warranty (if any) for this copy of Asterisk"),
1905         AST_CLI_DEFINE(show_license, "Show the license(s) for this copy of Asterisk"),
1906         AST_CLI_DEFINE(handle_version, "Display version info"),
1907         AST_CLI_DEFINE(handle_bang, "Execute a shell command"),
1908 #if !defined(LOW_MEMORY)
1909         AST_CLI_DEFINE(handle_show_version_files, "List versions of files used to build Asterisk"),
1910         AST_CLI_DEFINE(handle_show_threads, "Show running threads"),
1911 #if defined(HAVE_SYSINFO)
1912         AST_CLI_DEFINE(handle_show_sysinfo, "Show System Information"),
1913 #endif
1914         AST_CLI_DEFINE(handle_show_profile, "Display profiling info"),
1915         AST_CLI_DEFINE(handle_show_settings, "Show some core settings"),
1916         AST_CLI_DEFINE(handle_clear_profile, "Clear profiling info"),
1917 #endif /* ! LOW_MEMORY */
1918 };
1919
1920 static int ast_el_read_char(EditLine *editline, char *cp)
1921 {
1922         int num_read = 0;
1923         int lastpos = 0;
1924         struct pollfd fds[2];
1925         int res;
1926         int max;
1927 #define EL_BUF_SIZE 512
1928         char buf[EL_BUF_SIZE];
1929
1930         for (;;) {
1931                 max = 1;
1932                 fds[0].fd = ast_consock;
1933                 fds[0].events = POLLIN;
1934                 if (!ast_opt_exec) {
1935                         fds[1].fd = STDIN_FILENO;
1936                         fds[1].events = POLLIN;
1937                         max++;
1938                 }
1939                 res = poll(fds, max, -1);
1940                 if (res < 0) {
1941                         if (errno == EINTR)
1942                                 continue;
1943                         ast_log(LOG_ERROR, "poll failed: %s\n", strerror(errno));
1944                         break;
1945                 }
1946
1947                 if (!ast_opt_exec && fds[1].revents) {
1948                         num_read = read(STDIN_FILENO, cp, 1);
1949                         if (num_read < 1) {
1950                                 break;
1951                         } else 
1952                                 return (num_read);
1953                 }
1954                 if (fds[0].revents) {
1955                         char *tmp;
1956                         res = read(ast_consock, buf, sizeof(buf) - 1);
1957                         /* if the remote side disappears exit */
1958                         if (res < 1) {
1959                                 fprintf(stderr, "\nDisconnected from Asterisk server\n");
1960                                 if (!ast_opt_reconnect) {
1961                                         quit_handler(0, 0, 0, 0);
1962                                 } else {
1963                                         int tries;
1964                                         int reconnects_per_second = 20;
1965                                         fprintf(stderr, "Attempting to reconnect for 30 seconds\n");
1966                                         for (tries = 0; tries < 30 * reconnects_per_second; tries++) {
1967                                                 if (ast_tryconnect()) {
1968                                                         fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries);
1969                                                         printf("%s", term_quit());
1970                                                         WELCOME_MESSAGE;
1971                                                         if (!ast_opt_mute)
1972                                                                 fdsend(ast_consock, "logger mute silent");
1973                                                         else 
1974                                                                 printf("log and verbose output currently muted ('logger mute' to unmute)\n");
1975                                                         break;
1976                                                 } else
1977                                                         usleep(1000000 / reconnects_per_second);
1978                                         }
1979                                         if (tries >= 30 * reconnects_per_second) {
1980                                                 fprintf(stderr, "Failed to reconnect for 30 seconds.  Quitting.\n");
1981                                                 quit_handler(0, 0, 0, 0);
1982                                         }
1983                                 }
1984                         }
1985
1986                         buf[res] = '\0';
1987
1988                         /* Strip preamble from asynchronous events, too */
1989                         for (tmp = buf; *tmp; tmp++) {
1990                                 if (*tmp == 127) {
1991                                         memmove(tmp, tmp + 1, strlen(tmp));
1992                                         tmp--;
1993                                 }
1994                         }
1995
1996                         /* Write over the CLI prompt */
1997                         if (!ast_opt_exec && !lastpos) {
1998                                 if (write(STDOUT_FILENO, "\r", 1) < 0) {
1999                                 }
2000                         }
2001                         if (write(STDOUT_FILENO, buf, res) < 0) {
2002                         }
2003                         if ((res < EL_BUF_SIZE - 1) && ((buf[res-1] == '\n') || (buf[res-2] == '\n'))) {
2004                                 *cp = CC_REFRESH;
2005                                 return(1);
2006                         } else
2007                                 lastpos = 1;
2008                 }
2009         }
2010
2011         *cp = '\0';
2012         return (0);
2013 }
2014
2015 static struct ast_str *prompt = NULL;
2016
2017 static char *cli_prompt(EditLine *editline)
2018 {
2019         char tmp[100];
2020         char *pfmt;
2021         int color_used = 0;
2022         static int cli_prompt_changes = 0;
2023         char term_code[20];
2024         struct passwd *pw;
2025         struct group *gr;
2026
2027         if (prompt == NULL) {
2028                 prompt = ast_str_create(100);
2029         } else if (!cli_prompt_changes) {
2030                 return prompt->str;
2031         } else {
2032                 ast_str_reset(prompt);
2033         }
2034
2035         if ((pfmt = getenv("ASTERISK_PROMPT"))) {
2036                 char *t = pfmt;
2037                 struct timeval ts = ast_tvnow();
2038                 while (*t != '\0') {
2039                         if (*t == '%') {
2040                                 char hostname[MAXHOSTNAMELEN] = "";
2041                                 int i, which;
2042                                 struct ast_tm tm = { 0, };
2043                                 int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
2044
2045                                 t++;
2046                                 switch (*t) {
2047                                 case 'C': /* color */
2048                                         t++;
2049                                         if (sscanf(t, "%d;%d%n", &fgcolor, &bgcolor, &i) == 2) {
2050                                                 ast_str_append(&prompt, 0, "%s", term_color_code(term_code, fgcolor, bgcolor, sizeof(term_code)));
2051                                                 t += i - 1;
2052                                         } else if (sscanf(t, "%d%n", &fgcolor, &i) == 1) {
2053                                                 ast_str_append(&prompt, 0, "%s", term_color_code(term_code, fgcolor, 0, sizeof(term_code)));
2054                                                 t += i - 1;
2055                                         }
2056
2057                                         /* If the color has been reset correctly, then there's no need to reset it later */
2058                                         color_used = ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) ? 0 : 1;
2059                                         break;
2060                                 case 'd': /* date */
2061                                         if (ast_localtime(&ts, &tm, NULL)) {
2062                                                 ast_strftime(tmp, sizeof(tmp), "%Y-%m-%d", &tm);
2063                                                 ast_str_append(&prompt, 0, "%s", tmp);
2064                                                 cli_prompt_changes++;
2065                                         }
2066                                         break;
2067                                 case 'g': /* group */
2068                                         if ((gr = getgrgid(getgid()))) {
2069                                                 ast_str_append(&prompt, 0, "%s", gr->gr_name);
2070                                         }
2071                                         break;
2072                                 case 'h': /* hostname */
2073                                         if (!gethostname(hostname, sizeof(hostname) - 1)) {
2074                                                 ast_str_append(&prompt, 0, "%s", hostname);
2075                                         } else {
2076                                                 ast_str_append(&prompt, 0, "%s", "localhost");
2077                                         }
2078                                         break;
2079                                 case 'H': /* short hostname */
2080                                         if (!gethostname(hostname, sizeof(hostname) - 1)) {
2081                                                 char *dotptr;
2082                                                 if ((dotptr = strchr(hostname, '.'))) {
2083                                                         *dotptr = '\0';
2084                                                 }
2085                                                 ast_str_append(&prompt, 0, "%s", hostname);
2086                                         } else {
2087                                                 ast_str_append(&prompt, 0, "%s", "localhost");
2088                                         }
2089                                         break;
2090 #ifdef HAVE_GETLOADAVG
2091                                 case 'l': /* load avg */
2092                                         t++;
2093                                         if (sscanf(t, "%d", &which) == 1 && which > 0 && which <= 3) {
2094                                                 double list[3];
2095                                                 getloadavg(list, 3);
2096                                                 ast_str_append(&prompt, 0, "%.2f", list[which - 1]);
2097                                                 cli_prompt_changes++;
2098                                         }
2099                                         break;
2100 #endif
2101                                 case 's': /* Asterisk system name (from asterisk.conf) */
2102                                         ast_str_append(&prompt, 0, "%s", ast_config_AST_SYSTEM_NAME);
2103                                         break;
2104                                 case 't': /* time */
2105                                         if (ast_localtime(&ts, &tm, NULL)) {
2106                                                 ast_strftime(tmp, sizeof(tmp), "%H:%M:%S", &tm);
2107                                                 ast_str_append(&prompt, 0, "%s", tmp);
2108                                                 cli_prompt_changes++;
2109                                         }
2110                                         break;
2111                                 case 'u': /* username */
2112                                         if ((pw = getpwuid(getuid()))) {
2113                                                 ast_str_append(&prompt, 0, "%s", pw->pw_name);
2114                                         }
2115                                         break;
2116                                 case '#': /* process console or remote? */
2117                                         ast_str_append(&prompt, 0, "%c", ast_opt_remote ? '>' : '#');
2118                                         break;
2119                                 case '%': /* literal % */
2120                                         ast_str_append(&prompt, 0, "%c", '%');
2121                                         break;
2122                                 case '\0': /* % is last character - prevent bug */
2123                                         t--;
2124                                         break;
2125                                 }
2126                                 t++;
2127                         } else {
2128                                 if (prompt->used + 5 > prompt->len) {
2129                                         ast_str_make_space(&prompt, prompt->len + 5);
2130                                 }
2131                                 prompt->str[prompt->used++] = *t++;
2132                                 prompt->str[prompt->used] = '\0';
2133                         }
2134                 }
2135                 if (color_used) {
2136                         /* Force colors back to normal at end */
2137                         ast_str_append(&prompt, 0, "%s", term_color_code(term_code, 0, 0, sizeof(term_code)));
2138                 }
2139         } else if (remotehostname) {
2140                 ast_str_set(&prompt, 0, ASTERISK_PROMPT2, remotehostname);
2141         } else {
2142                 ast_str_set(&prompt, 0, "%s", ASTERISK_PROMPT);
2143         }
2144
2145         return(prompt->str);    
2146 }
2147
2148 static char **ast_el_strtoarr(char *buf)
2149 {
2150         char **match_list = NULL, **match_list_tmp, *retstr;
2151         size_t match_list_len;
2152         int matches = 0;
2153
2154         match_list_len = 1;
2155         while ( (retstr = strsep(&buf, " ")) != NULL) {
2156
2157                 if (!strcmp(retstr, AST_CLI_COMPLETE_EOF))
2158                         break;
2159                 if (matches + 1 >= match_list_len) {
2160                         match_list_len <<= 1;
2161                         if ((match_list_tmp = ast_realloc(match_list, match_list_len * sizeof(char *)))) {
2162                                 match_list = match_list_tmp;
2163                         } else {
2164                                 if (match_list)
2165                                         ast_free(match_list);
2166                                 return (char **) NULL;
2167                         }
2168                 }
2169
2170                 match_list[matches++] = ast_strdup(retstr);
2171         }
2172
2173         if (!match_list)
2174                 return (char **) NULL;
2175
2176         if (matches >= match_list_len) {
2177                 if ((match_list_tmp = ast_realloc(match_list, (match_list_len + 1) * sizeof(char *)))) {
2178                         match_list = match_list_tmp;
2179                 } else {
2180                         if (match_list)
2181                                 ast_free(match_list);
2182                         return (char **) NULL;
2183                 }
2184         }
2185
2186         match_list[matches] = (char *) NULL;
2187
2188         return match_list;
2189 }
2190
2191 static int ast_el_sort_compare(const void *i1, const void *i2)
2192 {
2193         char *s1, *s2;
2194
2195         s1 = ((char **)i1)[0];
2196         s2 = ((char **)i2)[0];
2197
2198         return strcasecmp(s1, s2);
2199 }
2200
2201 static int ast_cli_display_match_list(char **matches, int len, int max)
2202 {
2203         int i, idx, limit, count;
2204         int screenwidth = 0;
2205         int numoutput = 0, numoutputline = 0;
2206
2207         screenwidth = ast_get_termcols(STDOUT_FILENO);
2208
2209         /* find out how many entries can be put on one line, with two spaces between strings */
2210         limit = screenwidth / (max + 2);
2211         if (limit == 0)
2212                 limit = 1;
2213
2214         /* how many lines of output */
2215         count = len / limit;
2216         if (count * limit < len)
2217                 count++;
2218
2219         idx = 1;
2220
2221         qsort(&matches[0], (size_t)(len), sizeof(char *), ast_el_sort_compare);
2222
2223         for (; count > 0; count--) {
2224                 numoutputline = 0;
2225                 for (i = 0; i < limit && matches[idx]; i++, idx++) {
2226
2227                         /* Don't print dupes */
2228                         if ( (matches[idx+1] != NULL && strcmp(matches[idx], matches[idx+1]) == 0 ) ) {
2229                                 i--;
2230                                 ast_free(matches[idx]);
2231                                 matches[idx] = NULL;
2232                                 continue;
2233                         }
2234
2235                         numoutput++;
2236                         numoutputline++;
2237                         fprintf(stdout, "%-*s  ", max, matches[idx]);
2238                         ast_free(matches[idx]);
2239                         matches[idx] = NULL;
2240                 }
2241                 if (numoutputline > 0)
2242                         fprintf(stdout, "\n");
2243         }
2244
2245         return numoutput;
2246 }
2247
2248
2249 static char *cli_complete(EditLine *editline, int ch)
2250 {
2251         int len = 0;
2252         char *ptr;
2253         int nummatches = 0;
2254         char **matches;
2255         int retval = CC_ERROR;
2256         char buf[2048];
2257         int res;
2258
2259         LineInfo *lf = (LineInfo *)el_line(editline);
2260
2261         *(char *)lf->cursor = '\0';
2262         ptr = (char *)lf->cursor;
2263         if (ptr) {
2264                 while (ptr > lf->buffer) {
2265                         if (isspace(*ptr)) {
2266                                 ptr++;
2267                                 break;
2268                         }
2269                         ptr--;
2270                 }
2271         }
2272
2273         len = lf->cursor - ptr;
2274
2275         if (ast_opt_remote) {
2276                 snprintf(buf, sizeof(buf), "_COMMAND NUMMATCHES \"%s\" \"%s\"", lf->buffer, ptr); 
2277                 fdsend(ast_consock, buf);
2278                 res = read(ast_consock, buf, sizeof(buf));
2279                 buf[res] = '\0';
2280                 nummatches = atoi(buf);
2281
2282                 if (nummatches > 0) {
2283                         char *mbuf;
2284                         int mlen = 0, maxmbuf = 2048;
2285                         /* Start with a 2048 byte buffer */                     
2286                         if (!(mbuf = ast_malloc(maxmbuf)))
2287                                 return (char *)(CC_ERROR);
2288                         snprintf(buf, sizeof(buf), "_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr); 
2289                         fdsend(ast_consock, buf);
2290                         res = 0;
2291                         mbuf[0] = '\0';
2292                         while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {
2293                                 if (mlen + 1024 > maxmbuf) {
2294                                         /* Every step increment buffer 1024 bytes */
2295                                         maxmbuf += 1024;                                        
2296                                         if (!(mbuf = ast_realloc(mbuf, maxmbuf)))
2297                                                 return (char *)(CC_ERROR);
2298                                 }
2299                                 /* Only read 1024 bytes at a time */
2300                                 res = read(ast_consock, mbuf + mlen, 1024);
2301                                 if (res > 0)
2302                                         mlen += res;
2303                         }
2304                         mbuf[mlen] = '\0';
2305
2306                         matches = ast_el_strtoarr(mbuf);
2307                         ast_free(mbuf);
2308                 } else
2309                         matches = (char **) NULL;
2310         } else {
2311                 char **p, *oldbuf=NULL;
2312                 nummatches = 0;
2313                 matches = ast_cli_completion_matches((char *)lf->buffer,ptr);
2314                 for (p = matches; p && *p; p++) {
2315                         if (!oldbuf || strcmp(*p,oldbuf))
2316                                 nummatches++;
2317                         oldbuf = *p;
2318                 }
2319         }
2320
2321         if (matches) {
2322                 int i;
2323                 int matches_num, maxlen, match_len;
2324
2325                 if (matches[0][0] != '\0') {
2326                         el_deletestr(editline, (int) len);
2327                         el_insertstr(editline, matches[0]);
2328                         retval = CC_REFRESH;
2329                 }
2330
2331                 if (nummatches == 1) {
2332                         /* Found an exact match */
2333                         el_insertstr(editline, " ");
2334                         retval = CC_REFRESH;
2335                 } else {
2336                         /* Must be more than one match */
2337                         for (i = 1, maxlen = 0; matches[i]; i++) {
2338                                 match_len = strlen(matches[i]);
2339                                 if (match_len > maxlen)
2340                                         maxlen = match_len;
2341                         }
2342                         matches_num = i - 1;
2343                         if (matches_num >1) {
2344                                 fprintf(stdout, "\n");
2345                                 ast_cli_display_match_list(matches, nummatches, maxlen);
2346                                 retval = CC_REDISPLAY;
2347                         } else { 
2348                                 el_insertstr(editline," ");
2349                                 retval = CC_REFRESH;
2350                         }
2351                 }
2352                 for (i = 0; matches[i]; i++)
2353                         ast_free(matches[i]);
2354                 ast_free(matches);
2355         }
2356
2357         return (char *)(long)retval;
2358 }
2359
2360 static int ast_el_initialize(void)
2361 {
2362         HistEvent ev;
2363         char *editor = getenv("AST_EDITOR");
2364
2365         if (el != NULL)
2366                 el_end(el);
2367         if (el_hist != NULL)
2368                 history_end(el_hist);
2369
2370         el = el_init("asterisk", stdin, stdout, stderr);
2371         el_set(el, EL_PROMPT, cli_prompt);
2372
2373         el_set(el, EL_EDITMODE, 1);             
2374         el_set(el, EL_EDITOR, editor ? editor : "emacs");               
2375         el_hist = history_init();
2376         if (!el || !el_hist)
2377                 return -1;
2378
2379         /* setup history with 100 entries */
2380         history(el_hist, &ev, H_SETSIZE, 100);
2381
2382         el_set(el, EL_HIST, history, el_hist);
2383
2384         el_set(el, EL_ADDFN, "ed-complete", "Complete argument", cli_complete);
2385         /* Bind <tab> to command completion */
2386         el_set(el, EL_BIND, "^I", "ed-complete", NULL);
2387         /* Bind ? to command completion */
2388         el_set(el, EL_BIND, "?", "ed-complete", NULL);
2389         /* Bind ^D to redisplay */
2390         el_set(el, EL_BIND, "^D", "ed-redisplay", NULL);
2391
2392         return 0;
2393 }
2394
2395 #define MAX_HISTORY_COMMAND_LENGTH 256
2396
2397 static int ast_el_add_history(char *buf)
2398 {
2399         HistEvent ev;
2400
2401         if (el_hist == NULL || el == NULL)
2402                 ast_el_initialize();
2403         if (strlen(buf) > (MAX_HISTORY_COMMAND_LENGTH - 1))
2404                 return 0;
2405         return (history(el_hist, &ev, H_ENTER, ast_strip(ast_strdupa(buf))));
2406 }
2407
2408 static int ast_el_write_history(char *filename)
2409 {
2410         HistEvent ev;
2411
2412         if (el_hist == NULL || el == NULL)
2413                 ast_el_initialize();
2414
2415         return (history(el_hist, &ev, H_SAVE, filename));
2416 }
2417
2418 static int ast_el_read_history(char *filename)
2419 {
2420         char buf[MAX_HISTORY_COMMAND_LENGTH];
2421         FILE *f;
2422         int ret = -1;
2423
2424         if (el_hist == NULL || el == NULL)
2425                 ast_el_initialize();
2426
2427         if ((f = fopen(filename, "r")) == NULL)
2428                 return ret;
2429
2430         while (!feof(f)) {
2431                 if (!fgets(buf, sizeof(buf), f))
2432                         break;
2433                 if (!strcmp(buf, "_HiStOrY_V2_\n"))
2434                         continue;
2435                 if (ast_all_zeros(buf))
2436                         continue;
2437                 if ((ret = ast_el_add_history(buf)) == -1)
2438                         break;
2439         }
2440         fclose(f);
2441
2442         return ret;
2443 }
2444
2445 static void ast_remotecontrol(char *data)
2446 {
2447         char buf[80];
2448         int res;
2449         char filename[80] = "";
2450         char *hostname;
2451         char *cpid;
2452         char *version;
2453         int pid;
2454         char *stringp = NULL;
2455
2456         char *ebuf;
2457         int num = 0;
2458
2459         if (read(ast_consock, buf, sizeof(buf)) < 0) {
2460                 ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno));
2461                 return;
2462         }
2463         if (data) {
2464                 char prefix[] = "cli quit after ";
2465                 char *tmp = alloca(strlen(data) + strlen(prefix) + 1);
2466                 sprintf(tmp, "%s%s", prefix, data);
2467                 if (write(ast_consock, tmp, strlen(tmp) + 1) < 0) {
2468                         ast_log(LOG_ERROR, "write() failed: %s\n", strerror(errno));
2469                 }
2470         }
2471         stringp = buf;
2472         hostname = strsep(&stringp, "/");
2473         cpid = strsep(&stringp, "/");
2474         version = strsep(&stringp, "\n");
2475         if (!version)
2476                 version = "<Version Unknown>";
2477         stringp = hostname;
2478         strsep(&stringp, ".");
2479         if (cpid)
2480                 pid = atoi(cpid);
2481         else
2482                 pid = -1;
2483         if (!data) {
2484                 char tmp[80];
2485                 snprintf(tmp, sizeof(tmp), "core set verbose atleast %d", option_verbose);
2486                 fdsend(ast_consock, tmp);
2487                 snprintf(tmp, sizeof(tmp), "core set debug atleast %d", option_debug);
2488                 fdsend(ast_consock, tmp);
2489                 if (!ast_opt_mute)
2490                         fdsend(ast_consock, "logger mute silent");
2491                 else 
2492                         printf("log and verbose output currently muted ('logger mute' to unmute)\n");
2493         }
2494
2495         if (ast_opt_exec && data) {  /* hack to print output then exit if asterisk -rx is used */
2496                 struct pollfd fds;
2497                 fds.fd = ast_consock;
2498                 fds.events = POLLIN;
2499                 fds.revents = 0;
2500                 while (poll(&fds, 1, 500) > 0) {
2501                         char buffer[512] = "", *curline = buffer, *nextline;
2502                         int not_written = 1;
2503
2504                         if (read(ast_consock, buffer, sizeof(buffer) - 1) <= 0) {
2505                                 break;
2506                         }
2507
2508                         do {
2509                                 if ((nextline = strchr(curline, '\n'))) {
2510                                         nextline++;
2511                                 } else {
2512                                         nextline = strchr(curline, '\0');
2513                                 }
2514
2515                                 /* Skip verbose lines */
2516                                 if (*curline != 127) {
2517                                         not_written = 0;
2518                                         if (write(STDOUT_FILENO, curline, nextline - curline) < 0) {
2519                                                 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
2520                                         }
2521                                 }
2522                                 curline = nextline;
2523                         } while (!ast_strlen_zero(curline));
2524
2525                         /* No non-verbose output in 500ms */
2526                         if (not_written) {
2527                                 break;
2528                         }
2529                 }
2530                 return;
2531         }
2532
2533         ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid);
2534         remotehostname = hostname;
2535         if (getenv("HOME")) 
2536                 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
2537         if (el_hist == NULL || el == NULL)
2538                 ast_el_initialize();
2539
2540         el_set(el, EL_GETCFN, ast_el_read_char);
2541
2542         if (!ast_strlen_zero(filename))
2543                 ast_el_read_history(filename);
2544
2545         for (;;) {
2546                 ebuf = (char *)el_gets(el, &num);
2547
2548                 if (!ebuf && write(1, "", 1) < 0)
2549                         break;
2550
2551                 if (!ast_strlen_zero(ebuf)) {
2552                         if (ebuf[strlen(ebuf)-1] == '\n')
2553                                 ebuf[strlen(ebuf)-1] = '\0';
2554                         if (!remoteconsolehandler(ebuf)) {
2555                                 /* Strip preamble from output */
2556                                 char *temp;
2557                                 for (temp = ebuf; *temp; temp++) {
2558                                         if (*temp == 127) {
2559                                                 memmove(temp, temp + 1, strlen(temp));
2560                                                 temp--;
2561                                         }
2562                                 }
2563                                 res = write(ast_consock, ebuf, strlen(ebuf) + 1);
2564                                 if (res < 1) {
2565                                         ast_log(LOG_WARNING, "Unable to write: %s\n", strerror(errno));
2566                                         break;
2567                                 }
2568                         }
2569                 }
2570         }
2571         printf("\nDisconnected from Asterisk server\n");
2572 }
2573
2574 static int show_version(void)
2575 {
2576         printf("Asterisk %s\n", ast_get_version());
2577         return 0;
2578 }
2579
2580 static int show_cli_help(void) {
2581         printf("Asterisk %s, Copyright (C) 1999 - 2008, Digium, Inc. and others.\n", ast_get_version());
2582         printf("Usage: asterisk [OPTIONS]\n");
2583         printf("Valid Options:\n");
2584         printf("   -V              Display version number and exit\n");
2585         printf("   -C <configfile> Use an alternate configuration file\n");
2586         printf("   -G <group>      Run as a group other than the caller\n");
2587         printf("   -U <user>       Run as a user other than the caller\n");
2588         printf("   -c              Provide console CLI\n");
2589         printf("   -d              Enable extra debugging\n");
2590 #if HAVE_WORKING_FORK
2591         printf("   -f              Do not fork\n");
2592         printf("   -F              Always fork\n");
2593 #endif
2594         printf("   -g              Dump core in case of a crash\n");
2595         printf("   -h              This help screen\n");
2596         printf("   -i              Initialize crypto keys at startup\n");
2597         printf("   -I              Enable internal timing if DAHDI timer is available\n");
2598         printf("   -L <load>       Limit the maximum load average before rejecting new calls\n");
2599         printf("   -M <value>      Limit the maximum number of calls to the specified value\n");
2600         printf("   -m              Mute debugging and console output on the console\n");
2601         printf("   -n              Disable console colorization\n");
2602         printf("   -p              Run as pseudo-realtime thread\n");
2603         printf("   -q              Quiet mode (suppress output)\n");
2604         printf("   -r              Connect to Asterisk on this machine\n");
2605         printf("   -R              Same as -r, except attempt to reconnect if disconnected\n");
2606         printf("   -s <socket>     Connect to Asterisk via socket <socket> (only valid with -r)\n");
2607         printf("   -t              Record soundfiles in /var/tmp and move them where they\n");
2608         printf("                   belong after they are done\n");
2609         printf("   -T              Display the time in [Mmm dd hh:mm:ss] format for each line\n");
2610         printf("                   of output to the CLI\n");
2611         printf("   -v              Increase verbosity (multiple v's = more verbose)\n");
2612         printf("   -x <cmd>        Execute command <cmd> (only valid with -r)\n");
2613         printf("   -W              Adjust terminal colors to compensate for a light background\n");
2614         printf("\n");
2615         return 0;
2616 }
2617
2618 static void ast_readconfig(void) 
2619 {
2620         struct ast_config *cfg;
2621         struct ast_variable *v;
2622         char *config = DEFAULT_CONFIG_FILE;
2623         char hostname[MAXHOSTNAMELEN] = "";
2624         struct ast_flags config_flags = { 0 };
2625         struct {
2626                 unsigned int dbdir:1;
2627                 unsigned int keydir:1;
2628         } found = { 0, 0 };
2629
2630         if (ast_opt_override_config) {
2631                 cfg = ast_config_load2(ast_config_AST_CONFIG_FILE, "" /* core, can't reload */, config_flags);
2632                 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
2633                         ast_log(LOG_WARNING, "Unable to open specified master config file '%s', using built-in defaults\n", ast_config_AST_CONFIG_FILE);
2634         } else 
2635                 cfg = ast_config_load2(config, "" /* core, can't reload */, config_flags);
2636
2637         /* init with buildtime config */
2638         ast_copy_string(cfg_paths.config_dir, DEFAULT_CONFIG_DIR, sizeof(cfg_paths.config_dir));
2639         ast_copy_string(cfg_paths.spool_dir, DEFAULT_SPOOL_DIR, sizeof(cfg_paths.spool_dir));
2640         ast_copy_string(cfg_paths.module_dir, DEFAULT_MODULE_DIR, sizeof(cfg_paths.module_dir));
2641         snprintf(cfg_paths.monitor_dir, sizeof(cfg_paths.monitor_dir), "%s/monitor", cfg_paths.spool_dir);
2642         ast_copy_string(cfg_paths.var_dir, DEFAULT_VAR_DIR, sizeof(cfg_paths.var_dir));
2643         ast_copy_string(cfg_paths.data_dir, DEFAULT_DATA_DIR, sizeof(cfg_paths.data_dir));
2644         ast_copy_string(cfg_paths.log_dir, DEFAULT_LOG_DIR, sizeof(cfg_paths.log_dir));
2645         ast_copy_string(cfg_paths.agi_dir, DEFAULT_AGI_DIR, sizeof(cfg_paths.agi_dir));
2646         ast_copy_string(cfg_paths.db_path, DEFAULT_DB, sizeof(cfg_paths.db_path));
2647         ast_copy_string(cfg_paths.key_dir, DEFAULT_KEY_DIR, sizeof(cfg_paths.key_dir));
2648         ast_copy_string(cfg_paths.pid_path, DEFAULT_PID, sizeof(cfg_paths.pid_path));
2649         ast_copy_string(cfg_paths.socket_path, DEFAULT_SOCKET, sizeof(cfg_paths.socket_path));
2650         ast_copy_string(cfg_paths.run_dir, DEFAULT_RUN_DIR, sizeof(cfg_paths.run_dir));
2651
2652         ast_set_default_eid(&g_eid);
2653
2654         /* no asterisk.conf? no problem, use buildtime config! */
2655         if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
2656                 return;
2657         }
2658
2659         for (v = ast_variable_browse(cfg, "files"); v; v = v->next) {
2660                 if (!strcasecmp(v->name, "astctlpermissions"))
2661                         ast_copy_string(ast_config_AST_CTL_PERMISSIONS, v->value, sizeof(ast_config_AST_CTL_PERMISSIONS));
2662                 else if (!strcasecmp(v->name, "astctlowner"))
2663                         ast_copy_string(ast_config_AST_CTL_OWNER, v->value, sizeof(ast_config_AST_CTL_OWNER));
2664                 else if (!strcasecmp(v->name, "astctlgroup"))
2665                         ast_copy_string(ast_config_AST_CTL_GROUP, v->value, sizeof(ast_config_AST_CTL_GROUP));
2666                 else if (!strcasecmp(v->name, "astctl"))
2667                         ast_copy_string(ast_config_AST_CTL, v->value, sizeof(ast_config_AST_CTL));
2668         }
2669
2670         for (v = ast_variable_browse(cfg, "directories"); v; v = v->next) {
2671                 if (!strcasecmp(v->name, "astetcdir")) {
2672                         ast_copy_string(cfg_paths.config_dir, v->value, sizeof(cfg_paths.config_dir));
2673                 } else if (!strcasecmp(v->name, "astspooldir")) {
2674                         ast_copy_string(cfg_paths.spool_dir, v->value, sizeof(cfg_paths.spool_dir));
2675                         snprintf(cfg_paths.monitor_dir, sizeof(cfg_paths.monitor_dir), "%s/monitor", v->value);
2676                 } else if (!strcasecmp(v->name, "astvarlibdir")) {
2677                         ast_copy_string(cfg_paths.var_dir, v->value, sizeof(cfg_paths.var_dir));
2678                         if (!found.dbdir)
2679                                 snprintf(cfg_paths.db_path, sizeof(cfg_paths.db_path), "%s/astdb", v->value);
2680                 } else if (!strcasecmp(v->name, "astdbdir")) {
2681                         snprintf(cfg_paths.db_path, sizeof(cfg_paths.db_path), "%s/astdb", v->value);
2682                         found.dbdir = 1;
2683                 } else if (!strcasecmp(v->name, "astdatadir")) {
2684                         ast_copy_string(cfg_paths.data_dir, v->value, sizeof(cfg_paths.data_dir));
2685                         if (!found.keydir)
2686                                 snprintf(cfg_paths.key_dir, sizeof(cfg_paths.key_dir), "%s/keys", v->value);
2687                 } else if (!strcasecmp(v->name, "astkeydir")) {
2688                         snprintf(cfg_paths.key_dir, sizeof(cfg_paths.key_dir), "%s/keys", v->value);
2689                         found.keydir = 1;
2690                 } else if (!strcasecmp(v->name, "astlogdir")) {
2691                         ast_copy_string(cfg_paths.log_dir, v->value, sizeof(cfg_paths.log_dir));
2692                 } else if (!strcasecmp(v->name, "astagidir")) {
2693                         ast_copy_string(cfg_paths.agi_dir, v->value, sizeof(cfg_paths.agi_dir));
2694                 } else if (!strcasecmp(v->name, "astrundir")) {
2695                         snprintf(cfg_paths.pid_path, sizeof(cfg_paths.pid_path), "%s/%s", v->value, "asterisk.pid");
2696                         snprintf(cfg_paths.socket_path, sizeof(cfg_paths.socket_path), "%s/%s", v->value, ast_config_AST_CTL);
2697                         ast_copy_string(cfg_paths.run_dir, v->value, sizeof(cfg_paths.run_dir));
2698                 } else if (!strcasecmp(v->name, "astmoddir")) {
2699                         ast_copy_string(cfg_paths.module_dir, v->value, sizeof(cfg_paths.module_dir));
2700                 }
2701         }
2702
2703         for (v = ast_variable_browse(cfg, "options"); v; v = v->next) {
2704                 /* verbose level (-v at startup) */
2705                 if (!strcasecmp(v->name, "verbose")) {
2706                         option_verbose = atoi(v->value);
2707                 /* whether or not to force timestamping in CLI verbose output. (-T at startup) */
2708                 } else if (!strcasecmp(v->name, "timestamp")) {
2709                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_TIMESTAMP);
2710                 /* whether or not to support #exec in config files */
2711                 } else if (!strcasecmp(v->name, "execincludes")) {
2712                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_EXEC_INCLUDES);
2713                 /* debug level (-d at startup) */
2714                 } else if (!strcasecmp(v->name, "debug")) {
2715                         option_debug = 0;
2716                         if (sscanf(v->value, "%d", &option_debug) != 1) {
2717                                 option_debug = ast_true(v->value);
2718                         }
2719 #if HAVE_WORKING_FORK
2720                 /* Disable forking (-f at startup) */
2721                 } else if (!strcasecmp(v->name, "nofork")) {
2722                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_NO_FORK);
2723                 /* Always fork, even if verbose or debug are enabled (-F at startup) */
2724                 } else if (!strcasecmp(v->name, "alwaysfork")) {
2725                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_ALWAYS_FORK);
2726 #endif
2727                 /* Run quietly (-q at startup ) */
2728                 } else if (!strcasecmp(v->name, "quiet")) {
2729                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_QUIET);
2730                 /* Run as console (-c at startup, implies nofork) */
2731                 } else if (!strcasecmp(v->name, "console")) {
2732                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_CONSOLE);
2733                 /* Run with high priority if the O/S permits (-p at startup) */
2734                 } else if (!strcasecmp(v->name, "highpriority")) {
2735                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_HIGH_PRIORITY);
2736                 /* Initialize RSA auth keys (IAX2) (-i at startup) */
2737                 } else if (!strcasecmp(v->name, "initcrypto")) {
2738                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_INIT_KEYS);
2739                 /* Disable ANSI colors for console (-c at startup) */
2740                 } else if (!strcasecmp(v->name, "nocolor")) {
2741                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_NO_COLOR);
2742                 /* Disable some usage warnings for picky people :p */
2743                 } else if (!strcasecmp(v->name, "dontwarn")) {
2744                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_DONT_WARN);
2745                 /* Dump core in case of crash (-g) */
2746                 } else if (!strcasecmp(v->name, "dumpcore")) {
2747                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_DUMP_CORE);
2748                 /* Cache recorded sound files to another directory during recording */
2749                 } else if (!strcasecmp(v->name, "cache_record_files")) {
2750                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_CACHE_RECORD_FILES);
2751                 /* Specify cache directory */
2752                 }  else if (!strcasecmp(v->name, "record_cache_dir")) {
2753                         ast_copy_string(record_cache_dir, v->value, AST_CACHE_DIR_LEN);
2754                 /* Build transcode paths via SLINEAR, instead of directly */
2755                 } else if (!strcasecmp(v->name, "transcode_via_sln")) {
2756                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_TRANSCODE_VIA_SLIN);
2757                 /* Transmit SLINEAR silence while a channel is being recorded or DTMF is being generated on a channel */
2758                 } else if (!strcasecmp(v->name, "transmit_silence_during_record") || !strcasecmp(v->name, "transmit_silence")) {
2759                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_TRANSMIT_SILENCE);
2760                 /* Enable internal timing */
2761                 } else if (!strcasecmp(v->name, "internal_timing")) {
2762                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_INTERNAL_TIMING);
2763                 } else if (!strcasecmp(v->name, "maxcalls")) {
2764                         if ((sscanf(v->value, "%d", &option_maxcalls) != 1) || (option_maxcalls < 0)) {
2765                                 option_maxcalls = 0;
2766                         }
2767                 } else if (!strcasecmp(v->name, "maxload")) {
2768                         double test[1];
2769
2770                         if (getloadavg(test, 1) == -1) {
2771                                 ast_log(LOG_ERROR, "Cannot obtain load average on this system. 'maxload' option disabled.\n");
2772                                 option_maxload = 0.0;
2773                         } else if ((sscanf(v->value, "%lf", &option_maxload) != 1) || (option_maxload < 0.0)) {
2774                                 option_maxload = 0.0;
2775                         }
2776                 /* Set the maximum amount of open files */
2777                 } else if (!strcasecmp(v->name, "maxfiles")) {
2778                         option_maxfiles = atoi(v->value);
2779                         set_ulimit(option_maxfiles);
2780                 /* What user to run as */
2781                 } else if (!strcasecmp(v->name, "runuser")) {
2782                         ast_copy_string(cfg_paths.run_user, v->value, sizeof(cfg_paths.run_user));
2783                 /* What group to run as */
2784                 } else if (!strcasecmp(v->name, "rungroup")) {
2785                         ast_copy_string(cfg_paths.run_group, v->value, sizeof(cfg_paths.run_group));
2786                 } else if (!strcasecmp(v->name, "systemname")) {
2787                         ast_copy_string(cfg_paths.system_name, v->value, sizeof(cfg_paths.system_name));
2788                 } else if (!strcasecmp(v->name, "autosystemname")) {
2789                         if (ast_true(v->value)) {
2790                                 if (!gethostname(hostname, sizeof(hostname) - 1))
2791                                         ast_copy_string(cfg_paths.system_name, hostname, sizeof(cfg_paths.system_name));
2792                                 else {
2793                                         if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)){
2794                                                 ast_copy_string(cfg_paths.system_name, "localhost", sizeof(cfg_paths.system_name));
2795                                         }
2796                                         ast_log(LOG_ERROR, "Cannot obtain hostname for this system.  Using '%s' instead.\n", ast_config_AST_SYSTEM_NAME);
2797                                 }
2798                         }
2799                 } else if (!strcasecmp(v->name, "languageprefix")) {
2800                         ast_language_is_prefix = ast_true(v->value);
2801                 } else if (!strcasecmp(v->name, "lockmode")) {
2802                         if (!strcasecmp(v->value, "lockfile")) {
2803                                 ast_set_lock_type(AST_LOCK_TYPE_LOCKFILE);
2804                         } else if (!strcasecmp(v->value, "flock")) {
2805                                 ast_set_lock_type(AST_LOCK_TYPE_FLOCK);
2806                         } else {
2807                                 ast_log(LOG_WARNING, "'%s' is not a valid setting for the lockmode option, "
2808                                         "defaulting to 'lockfile'\n", v->value);
2809                                 ast_set_lock_type(AST_LOCK_TYPE_LOCKFILE);
2810                         }
2811 #if defined(HAVE_SYSINFO)
2812                 } else if (!strcasecmp(v->name, "minmemfree")) {
2813                         /* specify the minimum amount of free memory to retain.  Asterisk should stop accepting new calls
2814                          * if the amount of free memory falls below this watermark */
2815                         if ((sscanf(v->value, "%ld", &option_minmemfree) != 1) || (option_minmemfree < 0)) {
2816                                 option_minmemfree = 0;
2817                         }
2818 #endif
2819                 } else if (!strcasecmp(v->name, "entityid")) {
2820                         struct ast_eid tmp_eid;
2821                         if (!ast_str_to_eid(&tmp_eid, v->value)) {
2822                                 ast_verbose("Successfully set global EID to '%s'\n", v->value);
2823                                 g_eid = tmp_eid;
2824                         } else
2825                                 ast_verbose("Invalid Entity ID '%s' provided\n", v->value);
2826                 } else if (!strcasecmp(v->name, "lightbackground")) {
2827                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_LIGHT_BACKGROUND);
2828                 } else if (!strcasecmp(v->name, "forceblackbackground")) {
2829                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_FORCE_BLACK_BACKGROUND);
2830                 }
2831         }
2832         for (v = ast_variable_browse(cfg, "compat"); v; v = v->next) {
2833                 float version;
2834                 if (sscanf(v->value, "%f", &version) != 1) {
2835                         ast_log(LOG_WARNING, "Compatibility version for option '%s' is not a number: '%s'\n", v->name, v->value);
2836                         continue;
2837                 }
2838                 if (!strcasecmp(v->name, "app_set")) {
2839                         ast_set2_flag(&ast_compat, version < 1.5 ? 1 : 0, AST_COMPAT_APP_SET);
2840                 } else if (!strcasecmp(v->name, "res_agi")) {
2841                         ast_set2_flag(&ast_compat, version < 1.5 ? 1 : 0, AST_COMPAT_DELIM_RES_AGI);
2842                 } else if (!strcasecmp(v->name, "pbx_realtime")) {
2843                         ast_set2_flag(&ast_compat, version < 1.5 ? 1 : 0, AST_COMPAT_DELIM_PBX_REALTIME);
2844                 }
2845         }
2846         ast_config_destroy(cfg);
2847 }
2848
2849 static void *monitor_sig_flags(void *unused)
2850 {
2851         for (;;) {
2852                 struct pollfd p = { sig_alert_pipe[0], POLLIN, 0 };
2853                 int a;
2854                 poll(&p, 1, -1);
2855                 if (sig_flags.need_reload) {
2856                         sig_flags.need_reload = 0;
2857                         ast_module_reload(NULL);
2858                 }
2859                 if (sig_flags.need_quit) {
2860                         sig_flags.need_quit = 0;
2861                         quit_handler(0, 0, 1, 0);
2862                 }
2863                 if (read(sig_alert_pipe[0], &a, sizeof(a)) != sizeof(a)) {
2864                 }
2865         }
2866
2867         return NULL;
2868 }
2869
2870 static void *canary_thread(void *unused)
2871 {
2872         struct stat canary_stat;
2873         struct timeval now;
2874
2875         /* Give the canary time to sing */
2876         sleep(120);
2877
2878         for (;;) {
2879                 stat(canary_filename, &canary_stat);
2880                 now = ast_tvnow();
2881                 if (now.tv_sec > canary_stat.st_mtime + 60) {
2882                         ast_log(LOG_WARNING, "The canary is no more.  He has ceased to be!  He's expired and gone to meet his maker!  He's a stiff!  Bereft of life, he rests in peace.  His metabolic processes are now history!  He's off the twig!  He's kicked the bucket.  He's shuffled off his mortal coil, run down the curtain, and joined the bleeding choir invisible!!  THIS is an EX-CANARY.  (Reducing priority)\n");
2883                         ast_set_priority(0);
2884                         pthread_exit(NULL);
2885                 }
2886
2887                 /* Check the canary once a minute */
2888                 sleep(60);
2889         }
2890 }
2891
2892 /* Used by libc's atexit(3) function */
2893 static void canary_exit(void)
2894 {
2895         if (canary_pid > 0)
2896                 kill(canary_pid, SIGKILL);
2897 }
2898
2899 static void run_startup_commands(void)
2900 {
2901         int fd;
2902         struct ast_config *cfg;
2903         struct ast_flags cfg_flags = { 0 };
2904         struct ast_variable *v;
2905
2906         if (!(cfg = ast_config_load2("cli.conf", "" /* core, can't reload */, cfg_flags)))
2907                 return;
2908         if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
2909                 return;
2910         }
2911
2912         fd = open("/dev/null", O_RDWR);
2913         if (fd < 0) {
2914                 ast_config_destroy(cfg);
2915                 return;
2916         }
2917
2918         for (v = ast_variable_browse(cfg, "startup_commands"); v; v = v->next) {
2919                 if (ast_true(v->value))
2920                         ast_cli_command(fd, v->name);
2921         }
2922
2923         close(fd);
2924         ast_config_destroy(cfg);
2925 }
2926
2927 int main(int argc, char *argv[])
2928 {
2929         int c;
2930         char filename[80] = "";
2931         char hostname[MAXHOSTNAMELEN] = "";
2932         char tmp[80];
2933         char * xarg = NULL;
2934         int x;
2935         FILE *f;
2936         sigset_t sigs;
2937         int num;
2938         int isroot = 1;
2939         char *buf;
2940         const char *runuser = NULL, *rungroup = NULL;
2941         char *remotesock = NULL;
2942
2943         /* Remember original args for restart */
2944         if (argc > ARRAY_LEN(_argv) - 1) {
2945                 fprintf(stderr, "Truncating argument size to %d\n", (int)ARRAY_LEN(_argv) - 1);
2946                 argc = ARRAY_LEN(_argv) - 1;
2947         }
2948         for (x = 0; x < argc; x++)
2949                 _argv[x] = argv[x];
2950         _argv[x] = NULL;
2951
2952         if (geteuid() != 0)
2953                 isroot = 0;
2954
2955         /* if the progname is rasterisk consider it a remote console */
2956         if (argv[0] && (strstr(argv[0], "rasterisk")) != NULL) {
2957                 ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE);
2958         }
2959         if (gethostname(hostname, sizeof(hostname)-1))
2960                 ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
2961         ast_mainpid = getpid();
2962         ast_ulaw_init();
2963         ast_alaw_init();
2964         callerid_init();
2965         ast_builtins_init();
2966         ast_utils_init();
2967         tdd_init();
2968         ast_tps_init();
2969
2970         if (getenv("HOME")) 
2971                 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
2972         /* Check for options */
2973         while ((c = getopt(argc, argv, "mtThfFdvVqprRgciInx:U:G:C:L:M:e:s:WB")) != -1) {
2974                 switch (c) {
2975 #if defined(HAVE_SYSINFO)
2976                 case 'e':
2977                         if ((sscanf(&optarg[1], "%ld", &option_minmemfree) != 1) || (option_minmemfree < 0)) {
2978                                 option_minmemfree = 0;
2979                         }
2980                         break;
2981 #endif
2982 #if HAVE_WORKING_FORK
2983                 case 'F':
2984                         ast_set_flag(&ast_options, AST_OPT_FLAG_ALWAYS_FORK);
2985                         break;
2986                 case 'f':
2987                         ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
2988                         break;
2989 #endif
2990                 case 'd':
2991                         option_debug++;
2992                         ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
2993                         break;
2994                 case 'c':
2995                         ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_CONSOLE);
2996                         break;
2997                 case 'n':
2998                         ast_set_flag(&ast_options, AST_OPT_FLAG_NO_COLOR);
2999                         break;
3000                 case 'r':
3001                         ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE);
3002                         break;
3003                 case 'R':
3004                         ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE | AST_OPT_FLAG_RECONNECT);
3005                         break;
3006                 case 'p':
3007                         ast_set_flag(&ast_options, AST_OPT_FLAG_HIGH_PRIORITY);
3008                         break;
3009                 case 'v':
3010                         option_verbose++;
3011                         ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
3012                         break;
3013                 case 'm':
3014                         ast_set_flag(&ast_options, AST_OPT_FLAG_MUTE);
3015                         break;
3016                 case 'M':
3017                         if ((sscanf(optarg, "%d", &option_maxcalls) != 1) || (option_maxcalls < 0))
3018                                 option_maxcalls = 0;
3019                         break;
3020                 case 'L':
3021                         if ((sscanf(optarg, "%lf", &option_maxload) != 1) || (option_maxload < 0.0))
3022                                 option_maxload = 0.0;
3023                         break;
3024                 case 'q':
3025                         ast_set_flag(&ast_options, AST_OPT_FLAG_QUIET);
3026                         break;
3027                 case 't':
3028                         ast_set_flag(&ast_options, AST_OPT_FLAG_CACHE_RECORD_FILES);
3029                         break;
3030                 case 'T':
3031                         ast_set_flag(&ast_options, AST_OPT_FLAG_TIMESTAMP);
3032                         break;
3033                 case 'x':
3034                         ast_set_flag(&ast_options, AST_OPT_FLAG_EXEC);
3035                         xarg = ast_strdupa(optarg);
3036                         break;
3037                 case 'C':
3038                         ast_copy_string(cfg_paths.config_file, optarg, sizeof(cfg_paths.config_file));
3039                         ast_set_flag(&ast_options, AST_OPT_FLAG_OVERRIDE_CONFIG);
3040                         break;
3041                 case 'I':
3042                         ast_set_flag(&ast_options, AST_OPT_FLAG_INTERNAL_TIMING);
3043                         break;
3044                 case 'i':
3045                         ast_set_flag(&ast_options, AST_OPT_FLAG_INIT_KEYS);
3046                         break;
3047                 case 'g':
3048                         ast_set_flag(&ast_options, AST_OPT_FLAG_DUMP_CORE);
3049                         break;
3050                 case 'h':
3051                         show_cli_help();
3052                         exit(0);
3053                 case 'V':
3054                         show_version();
3055                         exit(0);
3056                 case 'U':
3057                         runuser = ast_strdupa(optarg);
3058                         break;
3059                 case 'G':
3060                         rungroup = ast_strdupa(optarg);
3061                         break;
3062                 case 's':
3063                         remotesock = ast_strdupa(optarg);
3064                         break;
3065                 case 'W': /* White background */
3066                         ast_set_flag(&ast_options, AST_OPT_FLAG_LIGHT_BACKGROUND);
3067                         ast_clear_flag(&ast_options, AST_OPT_FLAG_FORCE_BLACK_BACKGROUND);
3068                         break;
3069                 case 'B': /* Force black background */
3070                         ast_set_flag(&ast_options, AST_OPT_FLAG_FORCE_BLACK_BACKGROUND);
3071                         ast_clear_flag(&ast_options, AST_OPT_FLAG_LIGHT_BACKGROUND);
3072                         break;
3073                 case '?':
3074                         exit(1);
3075                 }
3076         }
3077
3078         if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
3079                 if (ast_register_verbose(console_verboser)) {
3080                         ast_log(LOG_WARNING, "Unable to register console verboser?\n");
3081                 }
3082                 WELCOME_MESSAGE;
3083         }
3084
3085         if (ast_opt_console && !option_verbose) 
3086                 ast_verbose("[ Booting...\n");
3087
3088         /* For remote connections, change the name of the remote connection.
3089          * We do this for the benefit of init scripts (which need to know if/when
3090          * the main asterisk process has died yet). */
3091         if (ast_opt_remote) {
3092                 strcpy(argv[0], "rasterisk");
3093                 for (x = 1; x < argc; x++) {
3094                         argv[x] = argv[0] + 10;
3095                 }
3096         }
3097
3098         if (ast_opt_console && !option_verbose) {
3099                 ast_verbose("[ Reading Master Configuration ]\n");
3100         }
3101
3102         ast_readconfig();
3103
3104         if (ast_opt_remote && remotesock != NULL)