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