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