Index: main/asterisk.c =================================================================== --- main/asterisk.c (revision 128562) +++ main/asterisk.c (working copy) @@ -181,6 +181,7 @@ int p[2]; /*!< Pipe */ pthread_t t; /*!< Thread of handler */ int mute; /*!< Is the console muted for logs */ + int closeit; /*!< The next executed command will close the socket when finish. */ int levels[NUMLOGLEVELS]; /*!< Which log levels are enabled for the console */ }; @@ -1044,6 +1045,8 @@ if (res < 1) break; } + if (con->closeit) + break; } ast_verb(3, "Remote UNIX connection disconnected\n"); close(con->fd); @@ -1093,6 +1096,7 @@ fcntl(consoles[x].p[1], F_SETFL, flags | O_NONBLOCK); consoles[x].fd = s; consoles[x].mute = 1; /* Default is muted, we will un-mute if necessary */ + consoles[x].closeit = 0; if (ast_pthread_create_detached_background(&consoles[x].t, NULL, netconsole, &consoles[x])) { ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno)); close(consoles[x].p[0]); @@ -1864,6 +1868,39 @@ return CLI_SUCCESS; } +static char *handle_cli_quit_after(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + int x; + char buf[80]; + + switch(cmd) { + case CLI_INIT: + e->command = "cli quit after"; + e->usage = "Usage: cli quit after [cli-command]\n" + " Use this command to run another cli command and disconnect\n" + " from the asterisk CLI when done.\n"; + break; + case CLI_GENERATE: + return ast_cli_generator(a->line + strlen(e->_full_cmd), a->word, a->n); + } + + if (a->argc <= e->args) + return CLI_SHOWUSAGE; + + /* Mark this session so we run this command and close it. */ + for (x=0;x < AST_MAX_CONNECTS; x++) { + if (a->fd == consoles[x].fd) { + consoles[x].closeit = 1; + break; + } + } + + ast_join(buf, sizeof(buf), a->argv); + ast_cli_command(a->fd, buf + strlen(e->_full_cmd)); + + return CLI_SUCCESS; +} + #define ASTERISK_PROMPT "*CLI> " #define ASTERISK_PROMPT2 "%s*CLI> " @@ -1890,6 +1927,7 @@ AST_CLI_DEFINE(show_license, "Show the license(s) for this copy of Asterisk"), AST_CLI_DEFINE(handle_version, "Display version info"), AST_CLI_DEFINE(handle_bang, "Execute a shell command"), + AST_CLI_DEFINE(handle_cli_quit_after, "Execute a CLI command and disconnect"), #if !defined(LOW_MEMORY) AST_CLI_DEFINE(handle_show_version_files, "List versions of files used to build Asterisk"), AST_CLI_DEFINE(handle_show_threads, "Show running threads"), @@ -2439,8 +2477,10 @@ int num = 0; read(ast_consock, buf, sizeof(buf)); - if (data) - write(ast_consock, data, strlen(data) + 1); + if (data) { + snprintf(tmp, sizeof(tmp), "cli quit after %s", data); + fdsend(ast_consock, tmp); + } stringp = buf; hostname = strsep(&stringp, "/"); cpid = strsep(&stringp, "/"); @@ -2480,7 +2520,7 @@ fds.fd = ast_consock; fds.events = POLLIN; fds.revents = 0; - while (poll(&fds, 1, 500) > 0) { + while (poll(&fds, 1, -1) > 0) { char buf[512] = "", *curline = buf, *nextline; int not_written = 1;