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