[svn-commits] rmudgett: branch 11 r377075 - in /branches/11: ./ main/cli.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Mon Dec 3 13:16:23 CST 2012


Author: rmudgett
Date: Mon Dec  3 13:16:20 2012
New Revision: 377075

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=377075
Log:
Cleanup CLI resources on exit and CLI command registration errors.

(issue ASTERISK-20649)
Reported by: Corey Farrell
Patches:
      cli-leaks-1_8-10.patch (license #5909) patch uploaded by Corey Farrell
      cli-leaks-11-trunk.patch (license #5909) patch uploaded by Corey Farrell
      Modified
........

Merged revisions 377073 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 377074 from http://svn.asterisk.org/svn/asterisk/branches/10

Modified:
    branches/11/   (props changed)
    branches/11/main/cli.c

Propchange: branches/11/
------------------------------------------------------------------------------
Binary property 'branch-10-merged' - no diff available.

Modified: branches/11/main/cli.c
URL: http://svnview.digium.com/svn/asterisk/branches/11/main/cli.c?view=diff&rev=377075&r1=377074&r2=377075
==============================================================================
--- branches/11/main/cli.c (original)
+++ branches/11/main/cli.c Mon Dec  3 13:16:20 2012
@@ -1888,10 +1888,16 @@
 	return 0;
 }
 
+static void cli_shutdown(void)
+{
+	ast_cli_unregister_multiple(cli_cli, ARRAY_LEN(cli_cli));
+}
+
 /*! \brief initialize the _full_cmd string in * each of the builtins. */
 void ast_builtins_init(void)
 {
 	ast_cli_register_multiple(cli_cli, ARRAY_LEN(cli_cli));
+	ast_register_atexit(cli_shutdown);
 }
 
 /*!
@@ -2056,6 +2062,18 @@
 	return cmdline;
 }
 
+static int cli_is_registered(struct ast_cli_entry *e)
+{
+	struct ast_cli_entry *cur = NULL;
+
+	while ((cur = cli_next(cur))) {
+		if (cur == e) {
+			return 1;
+		}
+	}
+	return 0;
+}
+
 static int __ast_cli_unregister(struct ast_cli_entry *e, struct ast_cli_entry *ed)
 {
 	if (e->inuse) {
@@ -2087,6 +2105,15 @@
 	char **dst = (char **)e->cmda;	/* need to cast as the entry is readonly */
 	char *s;
 
+	AST_RWLIST_WRLOCK(&helpers);
+
+	if (cli_is_registered(e)) {
+		ast_log(LOG_WARNING, "Command '%s' already registered (the same ast_cli_entry)\n",
+			S_OR(e->_full_cmd, e->command));
+		ret = 0;  /* report success */
+		goto done;
+	}
+
 	memset(&a, '\0', sizeof(a));
 	e->handler(e, CLI_INIT, &a);
 	/* XXX check that usage and command are filled up */
@@ -2102,14 +2129,16 @@
 	}
 	*dst++ = NULL;
 
-	AST_RWLIST_WRLOCK(&helpers);
-
 	if (find_cli(e->cmda, 1)) {
-		ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", S_OR(e->_full_cmd, e->command));
+		ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n",
+			S_OR(e->_full_cmd, e->command));
 		goto done;
 	}
-	if (set_full_cmd(e))
+	if (set_full_cmd(e)) {
+		ast_log(LOG_WARNING, "Error registering CLI Command '%s'\n",
+			S_OR(e->_full_cmd, e->command));
 		goto done;
+	}
 
 	lf = e->cmdlen;
 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&helpers, cur, list) {
@@ -2129,6 +2158,10 @@
 
 done:
 	AST_RWLIST_UNLOCK(&helpers);
+	if (ret) {
+		ast_free(e->command);
+		e->command = NULL;
+	}
 
 	return ret;
 }




More information about the svn-commits mailing list