2 * Asterisk -- A telephony toolkit for Linux.
4 * Top level source file for asterisk
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
17 #include <asterisk/logger.h>
18 #include <asterisk/options.h>
19 #include <asterisk/cli.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/ulaw.h>
22 #include <asterisk/alaw.h>
23 #include <asterisk/callerid.h>
24 #include <asterisk/module.h>
25 #include <asterisk/image.h>
26 #include <asterisk/tdd.h>
27 #include <asterisk/term.h>
28 #include <asterisk/manager.h>
29 #include <asterisk/pbx.h>
30 #include <asterisk/enum.h>
31 #include <asterisk/rtp.h>
32 #include <asterisk/app.h>
33 #include <asterisk/lock.h>
34 #include <asterisk/utils.h>
35 #include <asterisk/file.h>
36 #include <sys/resource.h>
41 #include <asterisk/io.h>
42 #include <asterisk/lock.h>
43 #include <sys/socket.h>
49 #include "editline/histedit.h"
51 #include <asterisk/config.h>
52 #include <asterisk/config_pvt.h>
53 #include <sys/resource.h>
57 #if defined(__FreeBSD__)
61 #define AST_MAX_CONNECTS 128
64 #define WELCOME_MESSAGE ast_verbose( "Asterisk " ASTERISK_VERSION ", Copyright (C) 1999-2004 Digium.\n"); \
65 ast_verbose( "Written by Mark Spencer <markster@digium.com>\n"); \
66 ast_verbose( "=========================================================================\n")
73 int option_highpriority=0;
76 int option_initcrypto=0;
78 int option_dumpcore = 0;
79 int option_overrideconfig = 0;
80 int option_reconnect = 0;
83 static int ast_socket = -1; /* UNIX Socket for allowing remote control */
84 static int ast_consock = -1; /* UNIX Socket for controlling another asterisk */
87 int fd; /* File descriptor */
89 pthread_t t; /* Thread of handler */
92 static struct ast_atexit {
94 struct ast_atexit *next;
96 AST_MUTEX_DEFINE_STATIC(atexitslock);
98 time_t ast_startuptime;
99 time_t ast_lastreloadtime;
101 static History *el_hist = NULL;
102 static EditLine *el = NULL;
103 static char *remotehostname;
105 struct console consoles[AST_MAX_CONNECTS];
107 char defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE;
109 static int ast_el_add_history(char *);
110 static int ast_el_read_history(char *);
111 static int ast_el_write_history(char *);
113 char ast_config_AST_CONFIG_DIR[AST_CONFIG_MAX_PATH];
114 char ast_config_AST_CONFIG_FILE[AST_CONFIG_MAX_PATH];
115 char ast_config_AST_MODULE_DIR[AST_CONFIG_MAX_PATH];
116 char ast_config_AST_SPOOL_DIR[AST_CONFIG_MAX_PATH];
117 char ast_config_AST_VAR_DIR[AST_CONFIG_MAX_PATH];
118 char ast_config_AST_LOG_DIR[AST_CONFIG_MAX_PATH];
119 char ast_config_AST_AGI_DIR[AST_CONFIG_MAX_PATH];
120 char ast_config_AST_DB[AST_CONFIG_MAX_PATH];
121 char ast_config_AST_KEY_DIR[AST_CONFIG_MAX_PATH];
122 char ast_config_AST_PID[AST_CONFIG_MAX_PATH];
123 char ast_config_AST_SOCKET[AST_CONFIG_MAX_PATH];
124 char ast_config_AST_RUN_DIR[AST_CONFIG_MAX_PATH];
126 static char *_argv[256];
127 static int shuttingdown = 0;
128 static int restartnow = 0;
129 static pthread_t consolethread = AST_PTHREADT_NULL;
131 int ast_register_atexit(void (*func)(void))
134 struct ast_atexit *ae;
135 ast_unregister_atexit(func);
136 ae = malloc(sizeof(struct ast_atexit));
137 ast_mutex_lock(&atexitslock);
139 memset(ae, 0, sizeof(struct ast_atexit));
145 ast_mutex_unlock(&atexitslock);
149 void ast_unregister_atexit(void (*func)(void))
151 struct ast_atexit *ae, *prev = NULL;
152 ast_mutex_lock(&atexitslock);
155 if (ae->func == func) {
157 prev->next = ae->next;
165 ast_mutex_unlock(&atexitslock);
168 static int fdprint(int fd, const char *s)
170 return write(fd, s, strlen(s) + 1);
173 int ast_safe_system(const char *s)
175 /* XXX This function needs some optimization work XXX */
179 struct rusage rusage;
183 /* Close file descriptors and launch system command */
184 for (x=STDERR_FILENO + 1; x<4096;x++) {
187 res = execl("/bin/sh", "/bin/sh", "-c", s, NULL);
189 } else if (pid > 0) {
191 res = wait4(pid, &status, 0, &rusage);
193 if (WIFEXITED(status))
194 res = WEXITSTATUS(status);
204 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
211 * write the string to all attached console clients
213 static void ast_network_puts(const char *string)
216 for (x=0;x<AST_MAX_CONNECTS; x++) {
217 if (consoles[x].fd > -1)
218 fdprint(consoles[x].p[1], string);
223 * write the string to the console, and all attached
226 void ast_console_puts(const char *string)
228 fputs(string, stdout);
230 ast_network_puts(string);
233 static void network_verboser(const char *s, int pos, int replace, int complete)
239 static pthread_t lthread;
241 static void *netconsole(void *vconsole)
243 struct console *con = vconsole;
247 struct pollfd fds[2];
249 if (gethostname(hostname, sizeof(hostname)))
250 strncpy(hostname, "<Unknown>", sizeof(hostname)-1);
251 snprintf(tmp, sizeof(tmp), "%s/%d/%s\n", hostname, ast_mainpid, ASTERISK_VERSION);
252 fdprint(con->fd, tmp);
255 fds[0].events = POLLIN;
256 fds[1].fd = con->p[0];
257 fds[1].events = POLLIN;
259 res = poll(fds, 2, -1);
262 ast_log(LOG_WARNING, "poll returned < 0: %s\n", strerror(errno));
265 if (fds[0].revents) {
266 res = read(con->fd, tmp, sizeof(tmp));
271 ast_cli_command(con->fd, tmp);
273 if (fds[1].revents) {
274 res = read(con->p[0], tmp, sizeof(tmp));
276 ast_log(LOG_ERROR, "read returned %d\n", res);
279 res = write(con->fd, tmp, res);
284 if (option_verbose > 2)
285 ast_verbose(VERBOSE_PREFIX_3 "Remote UNIX connection disconnected\n");
294 static void *listener(void *unused)
296 struct sockaddr_un sun;
301 struct pollfd fds[1];
303 pthread_attr_init(&attr);
304 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
308 fds[0].fd = ast_socket;
309 fds[0].events= POLLIN;
310 s = poll(fds, 1, -1);
313 ast_log(LOG_WARNING, "poll returned error: %s\n", strerror(errno));
317 s = accept(ast_socket, (struct sockaddr *)&sun, &len);
320 ast_log(LOG_WARNING, "Accept returned %d: %s\n", s, strerror(errno));
322 for (x=0;x<AST_MAX_CONNECTS;x++) {
323 if (consoles[x].fd < 0) {
324 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, consoles[x].p)) {
325 ast_log(LOG_ERROR, "Unable to create pipe: %s\n", strerror(errno));
327 fdprint(s, "Server failed to create pipe\n");
331 flags = fcntl(consoles[x].p[1], F_GETFL);
332 fcntl(consoles[x].p[1], F_SETFL, flags | O_NONBLOCK);
334 if (pthread_create(&consoles[x].t, &attr, netconsole, &consoles[x])) {
335 ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno));
337 fdprint(s, "Server failed to spawn thread\n");
343 if (x >= AST_MAX_CONNECTS) {
344 fdprint(s, "No more connections allowed\n");
345 ast_log(LOG_WARNING, "No more connections allowed\n");
347 } else if (consoles[x].fd > -1) {
348 if (option_verbose > 2)
349 ast_verbose(VERBOSE_PREFIX_3 "Remote UNIX connection\n");
356 static int ast_makesocket(void)
358 struct sockaddr_un sun;
361 for (x=0;x<AST_MAX_CONNECTS;x++)
363 unlink((char *)ast_config_AST_SOCKET);
364 ast_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
365 if (ast_socket < 0) {
366 ast_log(LOG_WARNING, "Unable to create control socket: %s\n", strerror(errno));
369 memset(&sun, 0, sizeof(sun));
370 sun.sun_family = AF_LOCAL;
371 strncpy(sun.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sun.sun_path)-1);
372 res = bind(ast_socket, (struct sockaddr *)&sun, sizeof(sun));
374 ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", (char *)ast_config_AST_SOCKET, strerror(errno));
379 res = listen(ast_socket, 2);
381 ast_log(LOG_WARNING, "Unable to listen on socket %s: %s\n", (char *)ast_config_AST_SOCKET, strerror(errno));
386 ast_register_verbose(network_verboser);
387 pthread_create(<hread, NULL, listener, NULL);
391 static int ast_tryconnect(void)
393 struct sockaddr_un sun;
395 ast_consock = socket(PF_LOCAL, SOCK_STREAM, 0);
396 if (ast_consock < 0) {
397 ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
400 memset(&sun, 0, sizeof(sun));
401 sun.sun_family = AF_LOCAL;
402 strncpy(sun.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sun.sun_path)-1);
403 res = connect(ast_consock, (struct sockaddr *)&sun, sizeof(sun));
412 static void urg_handler(int num)
414 /* Called by soft_hangup to interrupt the poll, read, or other
415 system call. We don't actually need to do anything though. */
416 /* Cannot EVER ast_log from within a signal handler */
418 printf("Urgent handler\n");
419 signal(num, urg_handler);
423 static void hup_handler(int num)
425 if (option_verbose > 1)
426 printf("Received HUP signal -- Reloading configs\n");
428 execvp(_argv[0], _argv);
429 /* XXX This could deadlock XXX */
433 static void child_handler(int sig)
435 /* Must not ever ast_log or ast_verbose within signal handler */
439 * Reap all dead children -- not just one
441 for (n = 0; wait4(-1, &status, WNOHANG, NULL) > 0; n++)
443 if (n == 0 && option_debug)
444 printf("Huh? Child handler, but nobody there?\n");
447 static void set_title(char *text)
449 /* Set an X-term or screen title */
450 if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
451 fprintf(stdout, "\033]2;%s\007", text);
454 static void set_icon(char *text)
456 if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
457 fprintf(stdout, "\033]1;%s\007", text);
460 static int set_priority(int pri)
462 struct sched_param sched;
463 memset(&sched, 0, sizeof(sched));
464 /* We set ourselves to a high priority, that we might pre-empt everything
465 else. If your PBX has heavy activity on it, this is a good thing. */
468 sched.sched_priority = 10;
469 if (sched_setscheduler(0, SCHED_RR, &sched)) {
470 ast_log(LOG_WARNING, "Unable to set high priority\n");
474 ast_verbose("Set to realtime thread\n");
476 sched.sched_priority = 0;
477 if (sched_setscheduler(0, SCHED_OTHER, &sched)) {
478 ast_log(LOG_WARNING, "Unable to set normal priority\n");
484 if (setpriority(PRIO_PROCESS, 0, -10) == -1) {
485 ast_log(LOG_WARNING, "Unable to set high priority\n");
489 ast_verbose("Set to high priority\n");
491 if (setpriority(PRIO_PROCESS, 0, 0) == -1) {
492 ast_log(LOG_WARNING, "Unable to set normal priority\n");
500 static void ast_run_atexits(void)
502 struct ast_atexit *ae;
503 ast_mutex_lock(&atexitslock);
510 ast_mutex_unlock(&atexitslock);
513 static void quit_handler(int num, int nice, int safeshutdown, int restart)
515 char filename[80] = "";
521 /* Begin shutdown routine, hanging up active channels */
522 ast_begin_shutdown(1);
523 if (option_verbose && option_console)
524 ast_verbose("Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
528 /* Wait up to 15 seconds for all channels to go away */
531 if (!ast_active_channels())
535 /* Sleep 1/10 of a second */
540 ast_begin_shutdown(0);
541 if (option_verbose && option_console)
542 ast_verbose("Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt");
544 if (!ast_active_channels())
553 if (option_verbose && option_console)
554 ast_verbose("Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
558 if (option_console || option_remote) {
560 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
561 if (!ast_strlen_zero(filename))
562 ast_el_write_history(filename);
566 history_end(el_hist);
569 ast_verbose("Executing last minute cleanups\n");
572 if (option_verbose && option_console)
573 ast_verbose("Asterisk %s ending (%d).\n", ast_active_channels() ? "uncleanly" : "cleanly", num);
574 else if (option_debug)
575 ast_log(LOG_DEBUG, "Asterisk ending (%d).\n", num);
576 manager_event(EVENT_FLAG_SYSTEM, "Shutdown", "Shutdown: %s\r\nRestart: %s\r\n", ast_active_channels() ? "Uncleanly" : "Cleanly", restart ? "True" : "False");
577 if (ast_socket > -1) {
581 if (ast_consock > -1)
584 unlink((char *)ast_config_AST_SOCKET);
585 if (!option_remote) unlink((char *)ast_config_AST_PID);
588 if (option_verbose || option_console)
589 ast_verbose("Preparing for Asterisk restart...\n");
590 /* Mark all FD's for closing on exec */
591 for (x=3;x<32768;x++) {
592 fcntl(x, F_SETFD, FD_CLOEXEC);
594 if (option_verbose || option_console)
595 ast_verbose("Restarting Asterisk NOW...\n");
601 /* If there is a consolethread running send it a SIGHUP
602 so it can execvp, otherwise we can do it ourselves */
603 if (consolethread != AST_PTHREADT_NULL) {
604 pthread_kill(consolethread, SIGHUP);
605 /* Give the signal handler some time to complete */
608 execvp(_argv[0], _argv);
617 static void __quit_handler(int num)
619 quit_handler(num, 0, 1, 0);
622 static const char *fix_header(char *outbuf, int maxout, const char *s, char *cmp)
625 if (!strncmp(s, cmp, strlen(cmp))) {
627 term_color(outbuf, cmp, COLOR_GRAY, 0, maxout);
633 static void console_verboser(const char *s, int pos, int replace, int complete)
637 /* Return to the beginning of the line */
639 fprintf(stdout, "\r");
640 if ((c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_4)) ||
641 (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_3)) ||
642 (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_2)) ||
643 (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_1)))
647 fputs(c + pos,stdout);
649 fputs(s + pos,stdout);
652 /* Wake up a poll()ing console */
653 if (option_console && consolethread != AST_PTHREADT_NULL)
654 pthread_kill(consolethread, SIGURG);
657 static void consolehandler(char *s)
661 /* Called when readline data is available */
662 if (s && !ast_strlen_zero(s))
663 ast_el_add_history(s);
664 /* Give the console access to the shell */
666 /* The real handler for bang */
669 ast_safe_system(s+1);
671 ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
673 ast_cli_command(STDOUT_FILENO, s);
675 fprintf(stdout, "\nUse \"quit\" to exit\n");
678 static int remoteconsolehandler(char *s)
681 /* Called when readline data is available */
682 if (s && !ast_strlen_zero(s))
683 ast_el_add_history(s);
684 /* Give the console access to the shell */
686 /* The real handler for bang */
689 ast_safe_system(s+1);
691 ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
694 if ((strncasecmp(s, "quit", 4) == 0 || strncasecmp(s, "exit", 4) == 0) &&
695 (s[4] == '\0' || isspace(s[4]))) {
696 quit_handler(0, 0, 0, 0);
700 fprintf(stdout, "\nUse \"quit\" to exit\n");
705 static char quit_help[] =
707 " Exits Asterisk.\n";
709 static char abort_halt_help[] =
710 "Usage: abort shutdown\n"
711 " Causes Asterisk to abort an executing shutdown or restart, and resume normal\n"
712 " call operations.\n";
714 static char shutdown_now_help[] =
716 " Shuts down a running Asterisk immediately, hanging up all active calls .\n";
718 static char shutdown_gracefully_help[] =
719 "Usage: stop gracefully\n"
720 " Causes Asterisk to not accept new calls, and exit when all\n"
721 " active calls have terminated normally.\n";
723 static char shutdown_when_convenient_help[] =
724 "Usage: stop when convenient\n"
725 " Causes Asterisk to perform a shutdown when all active calls have ended.\n";
727 static char restart_now_help[] =
728 "Usage: restart now\n"
729 " Causes Asterisk to hangup all calls and exec() itself performing a cold.\n"
732 static char restart_gracefully_help[] =
733 "Usage: restart gracefully\n"
734 " Causes Asterisk to stop accepting new calls and exec() itself performing a cold.\n"
735 " restart when all active calls have ended.\n";
737 static char restart_when_convenient_help[] =
738 "Usage: restart when convenient\n"
739 " Causes Asterisk to perform a cold restart when all active calls have ended.\n";
741 static char bang_help[] =
742 "Usage: !<command>\n"
743 " Executes a given shell command\n";
746 static int handle_quit(int fd, int argc, char *argv[])
749 return RESULT_SHOWUSAGE;
750 quit_handler(0, 0, 1, 0);
751 return RESULT_SUCCESS;
755 static int no_more_quit(int fd, int argc, char *argv[])
758 return RESULT_SHOWUSAGE;
759 ast_cli(fd, "The QUIT and EXIT commands may no longer be used to shutdown the PBX.\n"
760 "Please use STOP NOW instead, if you wish to shutdown the PBX.\n");
761 return RESULT_SUCCESS;
764 static int handle_shutdown_now(int fd, int argc, char *argv[])
767 return RESULT_SHOWUSAGE;
768 quit_handler(0, 0 /* Not nice */, 1 /* safely */, 0 /* not restart */);
769 return RESULT_SUCCESS;
772 static int handle_shutdown_gracefully(int fd, int argc, char *argv[])
775 return RESULT_SHOWUSAGE;
776 quit_handler(0, 1 /* nicely */, 1 /* safely */, 0 /* no restart */);
777 return RESULT_SUCCESS;
780 static int handle_shutdown_when_convenient(int fd, int argc, char *argv[])
783 return RESULT_SHOWUSAGE;
784 quit_handler(0, 2 /* really nicely */, 1 /* safely */, 0 /* don't restart */);
785 return RESULT_SUCCESS;
788 static int handle_restart_now(int fd, int argc, char *argv[])
791 return RESULT_SHOWUSAGE;
792 quit_handler(0, 0 /* not nicely */, 1 /* safely */, 1 /* restart */);
793 return RESULT_SUCCESS;
796 static int handle_restart_gracefully(int fd, int argc, char *argv[])
799 return RESULT_SHOWUSAGE;
800 quit_handler(0, 1 /* nicely */, 1 /* safely */, 1 /* restart */);
801 return RESULT_SUCCESS;
804 static int handle_restart_when_convenient(int fd, int argc, char *argv[])
807 return RESULT_SHOWUSAGE;
808 quit_handler(0, 2 /* really nicely */, 1 /* safely */, 1 /* restart */);
809 return RESULT_SUCCESS;
812 static int handle_abort_halt(int fd, int argc, char *argv[])
815 return RESULT_SHOWUSAGE;
816 ast_cancel_shutdown();
818 return RESULT_SUCCESS;
821 static int handle_bang(int fd, int argc, char *argv[])
823 return RESULT_SUCCESS;
826 #define ASTERISK_PROMPT "*CLI> "
828 #define ASTERISK_PROMPT2 "%s*CLI> "
830 static struct ast_cli_entry aborthalt = { { "abort", "halt", NULL }, handle_abort_halt, "Cancel a running halt", abort_halt_help };
832 static struct ast_cli_entry quit = { { "quit", NULL }, no_more_quit, "Exit Asterisk", quit_help };
833 static struct ast_cli_entry astexit = { { "exit", NULL }, no_more_quit, "Exit Asterisk", quit_help };
835 static struct ast_cli_entry astshutdownnow = { { "stop", "now", NULL }, handle_shutdown_now, "Shut down Asterisk immediately", shutdown_now_help };
836 static struct ast_cli_entry astshutdowngracefully = { { "stop", "gracefully", NULL }, handle_shutdown_gracefully, "Gracefully shut down Asterisk", shutdown_gracefully_help };
837 static struct ast_cli_entry astshutdownwhenconvenient = { { "stop", "when","convenient", NULL }, handle_shutdown_when_convenient, "Shut down Asterisk at empty call volume", shutdown_when_convenient_help };
838 static struct ast_cli_entry astrestartnow = { { "restart", "now", NULL }, handle_restart_now, "Restart Asterisk immediately", restart_now_help };
839 static struct ast_cli_entry astrestartgracefully = { { "restart", "gracefully", NULL }, handle_restart_gracefully, "Restart Asterisk gracefully", restart_gracefully_help };
840 static struct ast_cli_entry astrestartwhenconvenient= { { "restart", "when", "convenient", NULL }, handle_restart_when_convenient, "Restart Asterisk at empty call volume", restart_when_convenient_help };
841 static struct ast_cli_entry astbang = { { "!", NULL }, handle_bang, "Execute a shell command", bang_help };
843 static int ast_el_read_char(EditLine *el, char *cp)
847 struct pollfd fds[2];
854 fds[0].fd = ast_consock;
855 fds[0].events = POLLIN;
857 fds[1].fd = STDIN_FILENO;
858 fds[1].events = POLLIN;
861 res = poll(fds, max, -1);
865 ast_log(LOG_ERROR, "poll failed: %s\n", strerror(errno));
869 if (!option_exec && fds[1].revents) {
870 num_read = read(STDIN_FILENO, cp, 1);
876 if (fds[0].revents) {
877 res = read(ast_consock, buf, sizeof(buf) - 1);
878 /* if the remote side disappears exit */
880 fprintf(stderr, "\nDisconnected from Asterisk server\n");
881 if (!option_reconnect) {
882 quit_handler(0, 0, 0, 0);
885 int reconnects_per_second = 20;
886 fprintf(stderr, "Attempting to reconnect for 30 seconds\n");
887 for (tries=0;tries<30 * reconnects_per_second;tries++) {
888 if (ast_tryconnect()) {
889 fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries);
894 usleep(1000000 / reconnects_per_second);
898 fprintf(stderr, "Failed to reconnect for 30 seconds. Quitting.\n");
899 quit_handler(0, 0, 0, 0);
906 if (!option_exec && !lastpos)
907 write(STDOUT_FILENO, "\r", 1);
908 write(STDOUT_FILENO, buf, res);
909 if ((buf[res-1] == '\n') || (buf[res-2] == '\n')) {
922 static char *cli_prompt(EditLine *el)
924 static char prompt[200];
929 if ((pfmt = getenv("ASTERISK_PROMPT"))) {
930 char *t = pfmt, *p = prompt;
931 memset(prompt, 0, sizeof(prompt));
932 while (*t != '\0' && *p < sizeof(prompt)) {
941 int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
945 case 'C': /* color */
947 if (sscanf(t, "%d;%d%n", &fgcolor, &bgcolor, &i) == 2) {
948 strncat(p, term_color_code(term_code, fgcolor, bgcolor, sizeof(term_code)),sizeof(prompt) - strlen(prompt) - 1);
950 } else if (sscanf(t, "%d%n", &fgcolor, &i) == 1) {
951 strncat(p, term_color_code(term_code, fgcolor, 0, sizeof(term_code)),sizeof(prompt) - strlen(prompt) - 1);
955 /* If the color has been reset correctly, then there's no need to reset it later */
956 if ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) {
963 memset(&tm, 0, sizeof(struct tm));
964 gettimeofday(&tv, NULL);
965 if (localtime_r(&(tv.tv_sec), &tm)) {
966 strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm);
969 case 'h': /* hostname */
970 if (!gethostname(hostname, sizeof(hostname) - 1)) {
971 strncat(p, hostname, sizeof(prompt) - strlen(prompt) - 1);
973 strncat(p, "localhost", sizeof(prompt) - strlen(prompt) - 1);
976 case 'H': /* short hostname */
977 if (!gethostname(hostname, sizeof(hostname) - 1)) {
978 for (i=0;i<sizeof(hostname);i++) {
979 if (hostname[i] == '.') {
984 strncat(p, hostname, sizeof(prompt) - strlen(prompt) - 1);
986 strncat(p, "localhost", sizeof(prompt) - strlen(prompt) - 1);
990 case 'l': /* load avg */
992 if ((LOADAVG = fopen("/proc/loadavg", "r"))) {
993 float avg1, avg2, avg3;
994 int actproc, totproc, npid, which;
995 fscanf(LOADAVG, "%f %f %f %d/%d %d",
996 &avg1, &avg2, &avg3, &actproc, &totproc, &npid);
997 if (sscanf(t, "%d", &which) == 1) {
1000 snprintf(p, sizeof(prompt) - strlen(prompt), "%.2f", avg1);
1003 snprintf(p, sizeof(prompt) - strlen(prompt), "%.2f", avg2);
1006 snprintf(p, sizeof(prompt) - strlen(prompt), "%.2f", avg3);
1009 snprintf(p, sizeof(prompt) - strlen(prompt), "%d/%d", actproc, totproc);
1012 snprintf(p, sizeof(prompt) - strlen(prompt), "%d", npid);
1019 case 't': /* time */
1020 memset(&tm, 0, sizeof(struct tm));
1021 gettimeofday(&tv, NULL);
1022 if (localtime_r(&(tv.tv_sec), &tm)) {
1023 strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);
1026 case '#': /* process console or remote? */
1027 if (! option_remote) {
1028 strncat(p, "#", sizeof(prompt) - strlen(prompt) - 1);
1030 strncat(p, ">", sizeof(prompt) - strlen(prompt) - 1);
1033 case '%': /* literal % */
1034 strncat(p, "%", sizeof(prompt) - strlen(prompt) - 1);
1036 case '\0': /* % is last character - prevent bug */
1040 while (*p != '\0') {
1051 /* Force colors back to normal at end */
1052 term_color_code(term_code, COLOR_WHITE, COLOR_BLACK, sizeof(term_code));
1053 if (strlen(term_code) > sizeof(prompt) - strlen(prompt)) {
1054 strncat(prompt + sizeof(prompt) - strlen(term_code) - 1, term_code, strlen(term_code));
1056 strncat(p, term_code, sizeof(term_code));
1059 } else if (remotehostname)
1060 snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT2, remotehostname);
1062 snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT);
1067 static char **ast_el_strtoarr(char *buf)
1069 char **match_list = NULL, *retstr;
1070 size_t match_list_len;
1074 while ( (retstr = strsep(&buf, " ")) != NULL) {
1076 if (!strcmp(retstr, AST_CLI_COMPLETE_EOF))
1078 if (matches + 1 >= match_list_len) {
1079 match_list_len <<= 1;
1080 match_list = realloc(match_list, match_list_len * sizeof(char *));
1083 match_list[matches++] = strdup(retstr);
1087 return (char **) NULL;
1089 if (matches>= match_list_len)
1090 match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
1092 match_list[matches] = (char *) NULL;
1097 static int ast_el_sort_compare(const void *i1, const void *i2)
1101 s1 = ((char **)i1)[0];
1102 s2 = ((char **)i2)[0];
1104 return strcasecmp(s1, s2);
1107 static int ast_cli_display_match_list(char **matches, int len, int max)
1109 int i, idx, limit, count;
1110 int screenwidth = 0;
1111 int numoutput = 0, numoutputline = 0;
1113 screenwidth = ast_get_termcols(STDOUT_FILENO);
1115 /* find out how many entries can be put on one line, with two spaces between strings */
1116 limit = screenwidth / (max + 2);
1120 /* how many lines of output */
1121 count = len / limit;
1122 if (count * limit < len)
1127 qsort(&matches[0], (size_t)(len + 1), sizeof(char *), ast_el_sort_compare);
1129 for (; count > 0; count--) {
1131 for (i=0; i < limit && matches[idx]; i++, idx++) {
1133 /* Don't print dupes */
1134 if ( (matches[idx+1] != NULL && strcmp(matches[idx], matches[idx+1]) == 0 ) ) {
1137 matches[idx] = NULL;
1141 numoutput++; numoutputline++;
1142 fprintf(stdout, "%-*s ", max, matches[idx]);
1144 matches[idx] = NULL;
1146 if (numoutputline > 0)
1147 fprintf(stdout, "\n");
1154 static char *cli_complete(EditLine *el, int ch)
1160 int retval = CC_ERROR;
1164 LineInfo *lf = (LineInfo *)el_line(el);
1166 *(char *)lf->cursor = '\0';
1167 ptr = (char *)lf->cursor;
1169 while (ptr > lf->buffer) {
1170 if (isspace(*ptr)) {
1178 len = lf->cursor - ptr;
1180 if (option_remote) {
1181 snprintf(buf, sizeof(buf),"_COMMAND NUMMATCHES \"%s\" \"%s\"", lf->buffer, ptr);
1182 fdprint(ast_consock, buf);
1183 res = read(ast_consock, buf, sizeof(buf));
1185 nummatches = atoi(buf);
1187 if (nummatches > 0) {
1189 int mlen = 0, maxmbuf = 2048;
1190 /* Start with a 2048 byte buffer */
1191 mbuf = malloc(maxmbuf);
1193 return (char *)(CC_ERROR);
1194 snprintf(buf, sizeof(buf),"_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr);
1195 fdprint(ast_consock, buf);
1198 while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {
1199 if (mlen + 1024 > maxmbuf) {
1200 /* Every step increment buffer 1024 bytes */
1202 mbuf = realloc(mbuf, maxmbuf);
1204 return (char *)(CC_ERROR);
1206 /* Only read 1024 bytes at a time */
1207 res = read(ast_consock, mbuf + mlen, 1024);
1213 matches = ast_el_strtoarr(mbuf);
1216 matches = (char **) NULL;
1221 nummatches = ast_cli_generatornummatches((char *)lf->buffer,ptr);
1222 matches = ast_cli_completion_matches((char *)lf->buffer,ptr);
1227 int matches_num, maxlen, match_len;
1229 if (matches[0][0] != '\0') {
1230 el_deletestr(el, (int) len);
1231 el_insertstr(el, matches[0]);
1232 retval = CC_REFRESH;
1235 if (nummatches == 1) {
1236 /* Found an exact match */
1237 el_insertstr(el, " ");
1238 retval = CC_REFRESH;
1240 /* Must be more than one match */
1241 for (i=1, maxlen=0; matches[i]; i++) {
1242 match_len = strlen(matches[i]);
1243 if (match_len > maxlen)
1246 matches_num = i - 1;
1247 if (matches_num >1) {
1248 fprintf(stdout, "\n");
1249 ast_cli_display_match_list(matches, nummatches, maxlen);
1250 retval = CC_REDISPLAY;
1252 el_insertstr(el," ");
1253 retval = CC_REFRESH;
1259 return (char *)(long)retval;
1262 static int ast_el_initialize(void)
1265 char *editor = getenv("AST_EDITOR");
1269 if (el_hist != NULL)
1270 history_end(el_hist);
1272 el = el_init("asterisk", stdin, stdout, stderr);
1273 el_set(el, EL_PROMPT, cli_prompt);
1275 el_set(el, EL_EDITMODE, 1);
1276 el_set(el, EL_EDITOR, editor ? editor : "emacs");
1277 el_hist = history_init();
1278 if (!el || !el_hist)
1281 /* setup history with 100 entries */
1282 history(el_hist, &ev, H_SETSIZE, 100);
1284 el_set(el, EL_HIST, history, el_hist);
1286 el_set(el, EL_ADDFN, "ed-complete", "Complete argument", cli_complete);
1287 /* Bind <tab> to command completion */
1288 el_set(el, EL_BIND, "^I", "ed-complete", NULL);
1289 /* Bind ? to command completion */
1290 el_set(el, EL_BIND, "?", "ed-complete", NULL);
1291 /* Bind ^D to redisplay */
1292 el_set(el, EL_BIND, "^D", "ed-redisplay", NULL);
1297 static int ast_el_add_history(char *buf)
1301 if (el_hist == NULL || el == NULL)
1302 ast_el_initialize();
1303 if (strlen(buf) > 256)
1305 return (history(el_hist, &ev, H_ENTER, buf));
1308 static int ast_el_write_history(char *filename)
1312 if (el_hist == NULL || el == NULL)
1313 ast_el_initialize();
1315 return (history(el_hist, &ev, H_SAVE, filename));
1318 static int ast_el_read_history(char *filename)
1324 if (el_hist == NULL || el == NULL)
1325 ast_el_initialize();
1327 if ((f = fopen(filename, "r")) == NULL)
1331 fgets(buf, sizeof(buf), f);
1332 if (!strcmp(buf, "_HiStOrY_V2_\n"))
1334 if ((ret = ast_el_add_history(buf)) == -1)
1342 static void ast_remotecontrol(char * data)
1346 char filename[80] = "";
1357 read(ast_consock, buf, sizeof(buf));
1359 write(ast_consock, data, strlen(data) + 1);
1361 hostname = strsep(&stringp, "/");
1362 cpid = strsep(&stringp, "/");
1363 version = strsep(&stringp, "\n");
1365 version = "<Version Unknown>";
1367 strsep(&stringp, ".");
1372 snprintf(tmp, sizeof(tmp), "set verbose atleast %d", option_verbose);
1373 fdprint(ast_consock, tmp);
1374 ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid);
1375 remotehostname = hostname;
1377 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
1378 if (el_hist == NULL || el == NULL)
1379 ast_el_initialize();
1381 el_set(el, EL_GETCFN, ast_el_read_char);
1383 if (!ast_strlen_zero(filename))
1384 ast_el_read_history(filename);
1386 ast_cli_register(&quit);
1387 ast_cli_register(&astexit);
1389 ast_cli_register(&astshutdown);
1391 if (option_exec && data) { /* hack to print output then exit if asterisk -rx is used */
1393 ast_el_read_char(el, &tempchar);
1397 ebuf = (char *)el_gets(el, &num);
1399 if (ebuf && !ast_strlen_zero(ebuf)) {
1400 if (ebuf[strlen(ebuf)-1] == '\n')
1401 ebuf[strlen(ebuf)-1] = '\0';
1402 if (!remoteconsolehandler(ebuf)) {
1403 res = write(ast_consock, ebuf, strlen(ebuf) + 1);
1405 ast_log(LOG_WARNING, "Unable to write: %s\n", strerror(errno));
1411 printf("\nDisconnected from Asterisk server\n");
1414 static int show_version(void)
1416 printf("Asterisk " ASTERISK_VERSION "\n");
1420 static int show_cli_help(void) {
1421 printf("Asterisk " ASTERISK_VERSION ", Copyright (C) 2000-2004, Digium.\n");
1422 printf("Usage: asterisk [OPTIONS]\n");
1423 printf("Valid Options:\n");
1424 printf(" -V Display version number and exit\n");
1425 printf(" -C <configfile> Use an alternate configuration file\n");
1426 printf(" -G <group> Run as a group other than the caller\n");
1427 printf(" -U <user> Run as a user other than the caller\n");
1428 printf(" -c Provide console CLI\n");
1429 printf(" -d Enable extra debugging\n");
1430 printf(" -f Do not fork\n");
1431 printf(" -g Dump core in case of a crash\n");
1432 printf(" -h This help screen\n");
1433 printf(" -i Initializie crypto keys at startup\n");
1434 printf(" -n Disable console colorization\n");
1435 printf(" -p Run as pseudo-realtime thread\n");
1436 printf(" -q Quiet mode (supress output)\n");
1437 printf(" -r Connect to Asterisk on this machine\n");
1438 printf(" -R Connect to Asterisk, and attempt to reconnect if disconnected\n");
1439 printf(" -v Increase verbosity (multiple v's = more verbose)\n");
1440 printf(" -x <cmd> Execute command <cmd> (only valid with -r)\n");
1445 static void ast_readconfig(void) {
1446 struct ast_config *cfg;
1447 struct ast_variable *v;
1448 char *config = ASTCONFPATH;
1450 if (option_overrideconfig == 1) {
1451 cfg = ast_load((char *)ast_config_AST_CONFIG_FILE);
1453 cfg = ast_load(config);
1456 /* init with buildtime config */
1457 strncpy((char *)ast_config_AST_CONFIG_DIR,AST_CONFIG_DIR,sizeof(ast_config_AST_CONFIG_DIR)-1);
1458 strncpy((char *)ast_config_AST_SPOOL_DIR,AST_SPOOL_DIR,sizeof(ast_config_AST_SPOOL_DIR)-1);
1459 strncpy((char *)ast_config_AST_MODULE_DIR,AST_MODULE_DIR,sizeof(ast_config_AST_VAR_DIR)-1);
1460 strncpy((char *)ast_config_AST_VAR_DIR,AST_VAR_DIR,sizeof(ast_config_AST_VAR_DIR)-1);
1461 strncpy((char *)ast_config_AST_LOG_DIR,AST_LOG_DIR,sizeof(ast_config_AST_LOG_DIR)-1);
1462 strncpy((char *)ast_config_AST_AGI_DIR,AST_AGI_DIR,sizeof(ast_config_AST_AGI_DIR)-1);
1463 strncpy((char *)ast_config_AST_DB,AST_DB,sizeof(ast_config_AST_DB)-1);
1464 strncpy((char *)ast_config_AST_KEY_DIR,AST_KEY_DIR,sizeof(ast_config_AST_KEY_DIR)-1);
1465 strncpy((char *)ast_config_AST_PID,AST_PID,sizeof(ast_config_AST_PID)-1);
1466 strncpy((char *)ast_config_AST_SOCKET,AST_SOCKET,sizeof(ast_config_AST_SOCKET)-1);
1467 strncpy((char *)ast_config_AST_RUN_DIR,AST_RUN_DIR,sizeof(ast_config_AST_RUN_DIR)-1);
1469 /* no asterisk.conf? no problem, use buildtime config! */
1473 v = ast_variable_browse(cfg, "directories");
1475 if (!strcasecmp(v->name, "astetcdir")) {
1476 strncpy((char *)ast_config_AST_CONFIG_DIR,v->value,sizeof(ast_config_AST_CONFIG_DIR)-1);
1477 } else if (!strcasecmp(v->name, "astspooldir")) {
1478 strncpy((char *)ast_config_AST_SPOOL_DIR,v->value,sizeof(ast_config_AST_SPOOL_DIR)-1);
1479 } else if (!strcasecmp(v->name, "astvarlibdir")) {
1480 strncpy((char *)ast_config_AST_VAR_DIR,v->value,sizeof(ast_config_AST_VAR_DIR)-1);
1481 snprintf((char *)ast_config_AST_DB,sizeof(ast_config_AST_DB),"%s/%s",v->value,"astdb");
1482 } else if (!strcasecmp(v->name, "astlogdir")) {
1483 strncpy((char *)ast_config_AST_LOG_DIR,v->value,sizeof(ast_config_AST_LOG_DIR)-1);
1484 } else if (!strcasecmp(v->name, "astagidir")) {
1485 strncpy((char *)ast_config_AST_AGI_DIR,v->value,sizeof(ast_config_AST_AGI_DIR)-1);
1486 } else if (!strcasecmp(v->name, "astrundir")) {
1487 snprintf((char *)ast_config_AST_PID,sizeof(ast_config_AST_PID),"%s/%s",v->value,"asterisk.pid");
1488 snprintf((char *)ast_config_AST_SOCKET,sizeof(ast_config_AST_SOCKET),"%s/%s",v->value,"asterisk.ctl");
1489 strncpy((char *)ast_config_AST_RUN_DIR,v->value,sizeof(ast_config_AST_RUN_DIR)-1);
1490 } else if (!strcasecmp(v->name, "astmoddir")) {
1491 strncpy((char *)ast_config_AST_MODULE_DIR,v->value,sizeof(ast_config_AST_MODULE_DIR)-1);
1498 int main(int argc, char *argv[])
1501 char filename[80] = "";
1510 char *runuser=NULL, *rungroup=NULL;
1512 /* Remember original args for restart */
1513 if (argc > sizeof(_argv) / sizeof(_argv[0]) - 1) {
1514 fprintf(stderr, "Truncating argument size to %d\n", (int)(sizeof(_argv) / sizeof(_argv[0])) - 1);
1515 argc = sizeof(_argv) / sizeof(_argv[0]) - 1;
1517 for (x=0;x<argc;x++)
1521 /* if the progname is rasterisk consider it a remote console */
1522 if ( argv[0] && (strstr(argv[0], "rasterisk")) != NULL) {
1526 if (gethostname(hostname, sizeof(hostname)))
1527 strncpy(hostname, "<Unknown>", sizeof(hostname)-1);
1528 ast_mainpid = getpid();
1535 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
1536 /* Check if we're root */
1539 ast_log(LOG_ERROR, "Must be run as root\n");
1543 /* Check for options */
1544 while((c=getopt(argc, argv, "hfdvVqprRgcinx:U:G:C:")) != -1) {
1570 option_highpriority++;
1584 strncpy((char *)ast_config_AST_CONFIG_FILE,optarg,sizeof(ast_config_AST_CONFIG_FILE) - 1);
1585 option_overrideconfig++;
1588 option_initcrypto++;
1610 if (option_dumpcore) {
1612 memset(&l, 0, sizeof(l));
1613 l.rlim_cur = RLIM_INFINITY;
1614 l.rlim_max = RLIM_INFINITY;
1615 if (setrlimit(RLIMIT_CORE, &l)) {
1616 ast_log(LOG_WARNING, "Unable to disable core size resource limit: %s\n", strerror(errno));
1622 gr = getgrnam(rungroup);
1624 ast_log(LOG_WARNING, "No such group '%s'!\n", rungroup);
1627 if (setgid(gr->gr_gid)) {
1628 ast_log(LOG_WARNING, "Unable to setgid to %d (%s)\n", gr->gr_gid, rungroup);
1632 ast_verbose("Running as group '%s'\n", rungroup);
1635 if (set_priority(option_highpriority)) {
1640 pw = getpwnam(runuser);
1642 ast_log(LOG_WARNING, "No such user '%s'!\n", runuser);
1645 if (setuid(pw->pw_uid)) {
1646 ast_log(LOG_WARNING, "Unable to setuid to %d (%s)\n", pw->pw_uid, runuser);
1650 ast_verbose("Running as user '%s'\n", runuser);
1657 /* Test recursive mutex locking. */
1658 if (test_for_thread_safety())
1659 ast_verbose("Warning! Asterisk is not thread safe.\n");
1661 if (option_console && !option_verbose)
1662 ast_verbose("[ Reading Master Configuration ]");
1665 if (option_console && !option_verbose)
1666 ast_verbose("[ Initializing Custom Configuration Options]");
1667 /* custom config setup */
1668 register_config_cli();
1669 read_ast_cust_config();
1672 if (option_console) {
1673 if (el_hist == NULL || el == NULL)
1674 ast_el_initialize();
1676 if (!ast_strlen_zero(filename))
1677 ast_el_read_history(filename);
1680 if (ast_tryconnect()) {
1681 /* One is already running */
1682 if (option_remote) {
1684 ast_remotecontrol(xarg);
1685 quit_handler(0, 0, 0, 0);
1688 printf(term_quit());
1689 ast_register_verbose(console_verboser);
1691 ast_remotecontrol(NULL);
1692 quit_handler(0, 0, 0, 0);
1695 ast_log(LOG_ERROR, "Asterisk already running on %s. Use 'asterisk -r' to connect.\n", (char *)ast_config_AST_SOCKET);
1696 printf(term_quit());
1699 } else if (option_remote || option_exec) {
1700 ast_log(LOG_ERROR, "Unable to connect to remote asterisk\n");
1701 printf(term_quit());
1704 /* Blindly write pid file since we couldn't connect */
1705 unlink((char *)ast_config_AST_PID);
1706 f = fopen((char *)ast_config_AST_PID, "w");
1708 fprintf(f, "%d\n", getpid());
1711 ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", (char *)ast_config_AST_PID, strerror(errno));
1713 if (!option_verbose && !option_debug && !option_nofork && !option_console) {
1715 /* Blindly re-write pid file since we are forking */
1716 unlink((char *)ast_config_AST_PID);
1717 f = fopen((char *)ast_config_AST_PID, "w");
1719 fprintf(f, "%d\n", getpid());
1722 ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", (char *)ast_config_AST_PID, strerror(errno));
1727 sigaddset(&sigs, SIGHUP);
1728 sigaddset(&sigs, SIGTERM);
1729 sigaddset(&sigs, SIGINT);
1730 sigaddset(&sigs, SIGPIPE);
1731 sigaddset(&sigs, SIGWINCH);
1732 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
1733 if (option_console || option_verbose || option_remote)
1734 ast_register_verbose(console_verboser);
1735 /* Print a welcome message if desired */
1736 if (option_verbose || option_console) {
1739 if (option_console && !option_verbose)
1740 ast_verbose("[ Booting...");
1742 signal(SIGURG, urg_handler);
1743 signal(SIGINT, __quit_handler);
1744 signal(SIGTERM, __quit_handler);
1745 signal(SIGHUP, hup_handler);
1746 signal(SIGCHLD, child_handler);
1747 signal(SIGPIPE, SIG_IGN);
1749 if (init_logger()) {
1750 printf(term_quit());
1753 if (init_manager()) {
1754 printf(term_quit());
1758 if (ast_image_init()) {
1759 printf(term_quit());
1762 if (ast_file_init()) {
1763 printf(term_quit());
1767 printf(term_quit());
1770 if (load_modules()) {
1771 printf(term_quit());
1774 if (init_framer()) {
1775 printf(term_quit());
1779 printf(term_quit());
1782 if (ast_enum_init()) {
1783 printf(term_quit());
1786 /* reload logger in case a custom config handler binded to logger.conf*/
1789 /* We might have the option of showing a console, but for now just
1791 if (option_console && !option_verbose)
1792 ast_verbose(" ]\n");
1793 if (option_verbose || option_console)
1794 ast_verbose(term_color(tmp, "Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK, sizeof(tmp)));
1796 consolethread = pthread_self();
1798 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
1799 #ifdef __AST_DEBUG_MALLOC
1802 time(&ast_startuptime);
1803 ast_cli_register(&astshutdownnow);
1804 ast_cli_register(&astshutdowngracefully);
1805 ast_cli_register(&astrestartnow);
1806 ast_cli_register(&astrestartgracefully);
1807 ast_cli_register(&astrestartwhenconvenient);
1808 ast_cli_register(&astshutdownwhenconvenient);
1809 ast_cli_register(&aborthalt);
1810 ast_cli_register(&astbang);
1811 if (option_console) {
1812 /* Console stuff now... */
1813 /* Register our quit function */
1815 set_icon("Asterisk");
1816 snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %d)", hostname, ast_mainpid);
1818 ast_cli_register(&quit);
1819 ast_cli_register(&astexit);
1822 buf = (char *)el_gets(el, &num);
1824 if (buf[strlen(buf)-1] == '\n')
1825 buf[strlen(buf)-1] = '\0';
1827 consolehandler((char *)buf);
1830 ast_cli(STDOUT_FILENO, "\nUse EXIT or QUIT to exit the asterisk console\n");