📄 asterisk.c
字号:
} else { /* close logger */ close_logger(); } exit(0);}static void __quit_handler(int num){ quit_handler(num, 0, 1, 0);}static const char *fix_header(char *outbuf, int maxout, const char *s, char *cmp){ const char *c; if (!strncmp(s, cmp, strlen(cmp))) { c = s + strlen(cmp); term_color(outbuf, cmp, COLOR_GRAY, 0, maxout); return c; } return NULL;}static void console_verboser(const char *s, int pos, int replace, int complete){ char tmp[80]; const char *c=NULL; /* Return to the beginning of the line */ if (!pos) { fprintf(stdout, "\r"); if ((c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_4)) || (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_3)) || (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_2)) || (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_1))) fputs(tmp, stdout); } if (c) fputs(c + pos,stdout); else fputs(s + pos,stdout); fflush(stdout); if (complete) { /* Wake up a poll()ing console */ if (option_console && consolethread != AST_PTHREADT_NULL) pthread_kill(consolethread, SIGURG); }}static int ast_all_zeros(char *s){ while(*s) { if (*s > 32) return 0; s++; } return 1;}static void consolehandler(char *s){ printf(term_end()); fflush(stdout); /* Called when readline data is available */ if (!ast_all_zeros(s)) ast_el_add_history(s); /* The real handler for bang */ if (s[0] == '!') { if (s[1]) ast_safe_system(s+1); else ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh"); } else ast_cli_command(STDOUT_FILENO, s);}static int remoteconsolehandler(char *s){ int ret = 0; /* Called when readline data is available */ if (!ast_all_zeros(s)) ast_el_add_history(s); /* The real handler for bang */ if (s[0] == '!') { if (s[1]) ast_safe_system(s+1); else ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh"); ret = 1; } if ((strncasecmp(s, "quit", 4) == 0 || strncasecmp(s, "exit", 4) == 0) && (s[4] == '\0' || isspace(s[4]))) { quit_handler(0, 0, 0, 0); ret = 1; } return ret;}static char abort_halt_help[] = "Usage: abort shutdown\n"" Causes Asterisk to abort an executing shutdown or restart, and resume normal\n"" call operations.\n";static char shutdown_now_help[] = "Usage: stop now\n"" Shuts down a running Asterisk immediately, hanging up all active calls .\n";static char shutdown_gracefully_help[] = "Usage: stop gracefully\n"" Causes Asterisk to not accept new calls, and exit when all\n"" active calls have terminated normally.\n";static char shutdown_when_convenient_help[] = "Usage: stop when convenient\n"" Causes Asterisk to perform a shutdown when all active calls have ended.\n";static char restart_now_help[] = "Usage: restart now\n"" Causes Asterisk to hangup all calls and exec() itself performing a cold\n"" restart.\n";static char restart_gracefully_help[] = "Usage: restart gracefully\n"" Causes Asterisk to stop accepting new calls and exec() itself performing a cold\n"" restart when all active calls have ended.\n";static char restart_when_convenient_help[] = "Usage: restart when convenient\n"" Causes Asterisk to perform a cold restart when all active calls have ended.\n";static char bang_help[] ="Usage: !<command>\n"" Executes a given shell command\n";static char show_warranty_help[] ="Usage: show warranty\n"" Shows the warranty (if any) for this copy of Asterisk.\n";static char show_license_help[] ="Usage: show license\n"" Shows the license(s) for this copy of Asterisk.\n";#if 0static int handle_quit(int fd, int argc, char *argv[]){ if (argc != 1) return RESULT_SHOWUSAGE; quit_handler(0, 0, 1, 0); return RESULT_SUCCESS;}#endifstatic int handle_shutdown_now(int fd, int argc, char *argv[]){ if (argc != 2) return RESULT_SHOWUSAGE; quit_handler(0, 0 /* Not nice */, 1 /* safely */, 0 /* not restart */); return RESULT_SUCCESS;}static int handle_shutdown_gracefully(int fd, int argc, char *argv[]){ if (argc != 2) return RESULT_SHOWUSAGE; quit_handler(0, 1 /* nicely */, 1 /* safely */, 0 /* no restart */); return RESULT_SUCCESS;}static int handle_shutdown_when_convenient(int fd, int argc, char *argv[]){ if (argc != 3) return RESULT_SHOWUSAGE; quit_handler(0, 2 /* really nicely */, 1 /* safely */, 0 /* don't restart */); return RESULT_SUCCESS;}static int handle_restart_now(int fd, int argc, char *argv[]){ if (argc != 2) return RESULT_SHOWUSAGE; quit_handler(0, 0 /* not nicely */, 1 /* safely */, 1 /* restart */); return RESULT_SUCCESS;}static int handle_restart_gracefully(int fd, int argc, char *argv[]){ if (argc != 2) return RESULT_SHOWUSAGE; quit_handler(0, 1 /* nicely */, 1 /* safely */, 1 /* restart */); return RESULT_SUCCESS;}static int handle_restart_when_convenient(int fd, int argc, char *argv[]){ if (argc != 3) return RESULT_SHOWUSAGE; quit_handler(0, 2 /* really nicely */, 1 /* safely */, 1 /* restart */); return RESULT_SUCCESS;}static int handle_abort_halt(int fd, int argc, char *argv[]){ if (argc != 2) return RESULT_SHOWUSAGE; ast_cancel_shutdown(); shuttingdown = 0; return RESULT_SUCCESS;}static int handle_bang(int fd, int argc, char *argv[]){ return RESULT_SUCCESS;}static const char *warranty_lines[] = { "\n", " NO WARRANTY\n", "\n", "BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n", "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN\n", "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n", "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n", "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n", "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS\n", "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE\n", "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n", "REPAIR OR CORRECTION.\n", "\n", "IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n", "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n", "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n", "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n", "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n", "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n", "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n", "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n", "POSSIBILITY OF SUCH DAMAGES.\n",};static int show_warranty(int fd, int argc, char *argv[]){ int x; for (x = 0; x < sizeof(warranty_lines) / sizeof(warranty_lines[0]); x++) ast_cli(fd, (char *) warranty_lines[x]); return RESULT_SUCCESS;}static const char *license_lines[] = { "\n", "This program is free software; you can redistribute it and/or modify\n", "it under the terms of the GNU General Public License version 2 as\n", "published by the Free Software Foundation.\n", "\n", "This program also contains components licensed under other licenses.\n", "They include:\n", "\n", "This program is distributed in the hope that it will be useful,\n", "but WITHOUT ANY WARRANTY; without even the implied warranty of\n", "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n", "GNU General Public License for more details.\n", "\n", "You should have received a copy of the GNU General Public License\n", "along with this program; if not, write to the Free Software\n", "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n",};static int show_license(int fd, int argc, char *argv[]){ int x; for (x = 0; x < sizeof(license_lines) / sizeof(license_lines[0]); x++) ast_cli(fd, (char *) license_lines[x]); return RESULT_SUCCESS;}#define ASTERISK_PROMPT "*CLI> "#define ASTERISK_PROMPT2 "%s*CLI> "static struct ast_cli_entry core_cli[] = { { { "abort", "halt", NULL }, handle_abort_halt, "Cancel a running halt", abort_halt_help }, { { "stop", "now", NULL }, handle_shutdown_now, "Shut down Asterisk immediately", shutdown_now_help }, { { "stop", "gracefully", NULL }, handle_shutdown_gracefully, "Gracefully shut down Asterisk", shutdown_gracefully_help }, { { "stop", "when","convenient", NULL }, handle_shutdown_when_convenient, "Shut down Asterisk at empty call volume", shutdown_when_convenient_help }, { { "restart", "now", NULL }, handle_restart_now, "Restart Asterisk immediately", restart_now_help }, { { "restart", "gracefully", NULL }, handle_restart_gracefully, "Restart Asterisk gracefully", restart_gracefully_help }, { { "restart", "when", "convenient", NULL }, handle_restart_when_convenient, "Restart Asterisk at empty call volume", restart_when_convenient_help }, { { "show", "warranty", NULL }, show_warranty, "Show the warranty (if any) for this copy of Asterisk", show_warranty_help }, { { "show", "license", NULL }, show_license, "Show the license(s) for this copy of Asterisk", show_license_help }, { { "!", NULL }, handle_bang, "Execute a shell command", bang_help },#if !defined(LOW_MEMORY) { { "show", "version", "files", NULL }, handle_show_version_files, "Show versions of files used to build Asterisk", show_version_files_help, complete_show_version_files },#endif /* ! LOW_MEMORY */};static int ast_el_read_char(EditLine *el, char *cp){ int num_read=0; int lastpos=0; struct pollfd fds[2]; int res; int max; char buf[512]; for (;;) { max = 1; fds[0].fd = ast_consock; fds[0].events = POLLIN; if (!option_exec) { fds[1].fd = STDIN_FILENO; fds[1].events = POLLIN; max++; } res = poll(fds, max, -1); if (res < 0) { if (errno == EINTR) continue; ast_log(LOG_ERROR, "poll failed: %s\n", strerror(errno)); break; } if (!option_exec && fds[1].revents) { num_read = read(STDIN_FILENO, cp, 1); if (num_read < 1) { break; } else return (num_read); } if (fds[0].revents) { res = read(ast_consock, buf, sizeof(buf) - 1); /* if the remote side disappears exit */ if (res < 1) { fprintf(stderr, "\nDisconnected from Asterisk server\n"); if (!option_reconnect) { quit_handler(0, 0, 0, 0); } else { int tries; int reconnects_per_second = 20; fprintf(stderr, "Attempting to reconnect for 30 seconds\n"); for (tries=0;tries<30 * reconnects_per_second;tries++) { if (ast_tryconnect()) { fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries); printf(term_quit()); WELCOME_MESSAGE; break; } else { usleep(1000000 / reconnects_per_second); } } if (tries >= 30 * reconnects_per_second) { fprintf(stderr, "Failed to reconnect for 30 seconds. Quitting.\n"); quit_handler(0, 0, 0, 0); } } } buf[res] = '\0'; if (!option_exec && !lastpos) write(STDOUT_FILENO, "\r", 1); write(STDOUT_FILENO, buf, res); if ((buf[res-1] == '\n') || (buf[res-2] == '\n')) { *cp = CC_REFRESH; return(1); } else { lastpos = 1; } } } *cp = '\0'; return (0);}static char *cli_prompt(EditLine *el){ static char prompt[200]; char *pfmt; int color_used=0; char term_code[20]; if ((pfmt = getenv("ASTERISK_PROMPT"))) { char *t = pfmt, *p = prompt; memset(prompt, 0, sizeof(prompt)); while (*t != '\0' && *p < sizeof(prompt)) { if (*t == '%') { char hostname[MAXHOSTNAMELEN]=""; int i; time_t ts; struct tm tm;#ifdef linux FILE *LOADAVG;#endif int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK; t++; switch (*t) { case 'C': /* color */ t++; if (sscanf(t, "%d;%d%n", &fgcolor, &bgcolor, &i) == 2) { strncat(p, term_color_code(term_code, fgcolor, bgcolor, sizeof(term_code)),sizeof(prompt) - strlen(prompt) - 1); t += i - 1; } else if (sscanf(t, "%d%n", &fgcolor, &i) == 1) { strncat(p, term_color_code(term_code, fgcolor, 0, sizeof(term_code)),sizeof(prompt) - strlen(prompt) - 1); t += i - 1; } /* If the color has been reset correctly, then there's no need to reset it later */ if ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) { color_used = 0; } else { color_used = 1; } break; case 'd': /* date */ memset(&tm, 0, sizeof(struct tm)); time(&ts); if (localtime_r(&ts, &tm)) { strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm); } break; case 'h': /* hostname */ if (!gethostname(hostname, sizeof(hostname) - 1)) { strncat(p, hostname, sizeof(prompt) - strlen(prompt) - 1); } else { strncat(p, "localhost", sizeof(prompt) - strlen(prompt) - 1); } break; case 'H': /* short hostname */ if (!gethostname(hostname, sizeof(hostname) - 1)) { for (i=0;i<sizeof(hostname);i++) { if (hostname[i] == '.') { hostname[i] = '\0'; break; } } strncat(p, hostname, sizeof(prompt) - strlen(prompt) - 1); } else { strncat(p, "localhost", sizeof(prompt) - strlen(prompt) - 1); } break;#ifdef linux case 'l': /* load avg */ t++; if ((LOADAVG = fopen("/proc/loadavg", "r"))) { float avg1, avg2, avg3; int actproc, totproc, npid, which; fscanf(LOADAVG, "%f %f %f %d/%d %d", &avg1, &avg2, &avg3, &actproc, &totproc, &npid); if (sscanf(t, "%d", &which) == 1) { switch (which) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -