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