[Asterisk-code-review] CLI: Enable automatic references to modules. (asterisk[master])
Corey Farrell
asteriskteam at digium.com
Sat May 2 02:52:20 CDT 2015
Corey Farrell has uploaded a new change for review.
https://gerrit.asterisk.org/337
Change subject: CLI: Enable automatic references to modules.
......................................................................
CLI: Enable automatic references to modules.
* Pass module to ast_cli_register and ast_cli_register_multiple.
* Add a module reference before executing any CLI callback, remove
the reference when complete.
* Modify extra sources in modules to make module pointer available.
ASTERISK-25049 #close
Reported by: Corey Farrell
Change-Id: I7aafc7c9f2b912918f28fe51d51e9e8a755750e3
---
M apps/app_confbridge.c
M apps/confbridge/conf_config_parser.c
M apps/confbridge/include/confbridge.h
M channels/chan_iax2.c
M channels/iax2/include/iax2.h
M channels/iax2/provision.c
M include/asterisk/cli.h
M main/cli.c
M res/ari/cli.c
M res/ari/internal.h
M res/parking/parking_ui.c
M res/parking/res_parking.h
M res/res_ari.c
M res/res_clioriginate.c
M res/res_convert.c
M res/res_parking.c
M res/res_pjsip/config_auth.c
M res/res_pjsip/config_transport.c
M res/res_pjsip/include/res_pjsip_private.h
M res/res_pjsip/location.c
M res/res_pjsip/pjsip_cli.c
M res/res_pjsip/pjsip_configuration.c
M res/res_pjsip/pjsip_options.c
M utils/ael_main.c
M utils/clicompat.c
M utils/conf2ael.c
26 files changed, 80 insertions(+), 66 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/37/337/1
diff --git a/apps/app_confbridge.c b/apps/app_confbridge.c
index 0718887..82965c0 100644
--- a/apps/app_confbridge.c
+++ b/apps/app_confbridge.c
@@ -3373,7 +3373,7 @@
{
int res = 0;
- if (conf_load_config()) {
+ if (conf_load_config(ast_module_info)) {
ast_log(LOG_ERROR, "Unable to load config. Not loading module.\n");
return AST_MODULE_LOAD_DECLINE;
}
diff --git a/apps/confbridge/conf_config_parser.c b/apps/confbridge/conf_config_parser.c
index 7fcc700..441de58 100644
--- a/apps/confbridge/conf_config_parser.c
+++ b/apps/confbridge/conf_config_parser.c
@@ -2110,7 +2110,7 @@
return 0;
}
-int conf_load_config(void)
+int conf_load_config(const struct ast_module_info *ast_module_info)
{
if (aco_info_init(&cfg_info)) {
return -1;
diff --git a/apps/confbridge/include/confbridge.h b/apps/confbridge/include/confbridge.h
index 54a9dc4..ba82f1f 100644
--- a/apps/confbridge/include/confbridge.h
+++ b/apps/confbridge/include/confbridge.h
@@ -28,6 +28,7 @@
#include "asterisk/channel.h"
#include "asterisk/bridge.h"
#include "asterisk/bridge_features.h"
+#include "asterisk/module.h"
#include "conf_state.h"
/* Maximum length of a conference bridge name */
@@ -256,7 +257,7 @@
};
/*! \brief load confbridge.conf file */
-int conf_load_config(void);
+int conf_load_config(const struct ast_module_info *ast_module_info);
/*! \brief reload confbridge.conf file */
int conf_reload_config(void);
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index 253160a..c500a9f 100644
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -15006,6 +15006,11 @@
AST_DATA_ENTRY("asterisk/channel/iax2/users", &users_data_provider),
};
+const struct ast_module_info *iax2_module_info(void)
+{
+ return ast_module_info;
+}
+
/*!
* \brief Load the module
*
diff --git a/channels/iax2/include/iax2.h b/channels/iax2/include/iax2.h
index ca9ab74..0d4813f 100644
--- a/channels/iax2/include/iax2.h
+++ b/channels/iax2/include/iax2.h
@@ -224,6 +224,8 @@
/*!\brief iax2 wrapper function for ast_getformatname */
const char *iax2_getformatname(iax2_format format);
+const struct ast_module_info *iax2_module_info(void);
+
/*! Full frames are always delivered reliably */
struct ast_iax2_full_hdr {
unsigned short scallno; /*!< Source call number -- high bit must be 1 */
diff --git a/channels/iax2/provision.c b/channels/iax2/provision.c
index fcad2fe..f1cffd9 100644
--- a/channels/iax2/provision.c
+++ b/channels/iax2/provision.c
@@ -42,6 +42,7 @@
#include "asterisk/lock.h"
#include "asterisk/frame.h"
#include "asterisk/md5.h"
+#include "asterisk/module.h"
#include "asterisk/astdb.h"
#include "asterisk/utils.h"
#include "asterisk/acl.h"
@@ -498,7 +499,9 @@
static int iax_provision_init(void)
{
- ast_cli_register_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
+ __ast_cli_register_multiple(cli_iax2_provision,
+ sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry),
+ iax2_module_info()->self);
provinit = 1;
return 0;
}
diff --git a/include/asterisk/cli.h b/include/asterisk/cli.h
index 458ebc8..e8f6be0 100644
--- a/include/asterisk/cli.h
+++ b/include/asterisk/cli.h
@@ -177,7 +177,7 @@
const char * usage; /*!< Detailed usage information */
int inuse; /*!< For keeping track of usage */
- struct module *module; /*!< module this belongs to */
+ struct ast_module *module; /*!< module this belongs to */
char *_full_cmd; /*!< built at load time from cmda[] */
int cmdlen; /*!< len up to the first invalid char [<{% */
/*! \brief This gets set in ast_cli_register()
@@ -253,14 +253,28 @@
* \retval 0 on success
* \retval -1 on failure
*/
-int ast_cli_register(struct ast_cli_entry *e);
+#if defined(AST_IN_CORE)
+#define ast_cli_register(e) __ast_cli_register(e, NULL)
+#else
+#define ast_cli_register(e) __ast_cli_register(e, ast_module_info->self)
+#endif
+
+int __ast_cli_register(struct ast_cli_entry *e, struct ast_module *mod);
/*!
* \brief Register multiple commands
* \param e pointer to first cli entry to register
* \param len number of entries to register
*/
-int ast_cli_register_multiple(struct ast_cli_entry *e, int len);
+#if defined(AST_IN_CORE)
+#define ast_cli_register_multiple(e, len) \
+ __ast_cli_register_multiple(e, len, NULL)
+#else
+#define ast_cli_register_multiple(e, len) \
+ __ast_cli_register_multiple(e, len, ast_module_info->self)
+#endif
+
+int __ast_cli_register_multiple(struct ast_cli_entry *e, int len, struct ast_module *mod);
/*!
* \brief Unregisters a command or an array of commands
diff --git a/main/cli.c b/main/cli.c
index a230c20..78b6d51 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -2203,7 +2203,7 @@
return 0;
}
-static int __ast_cli_unregister(struct ast_cli_entry *e, struct ast_cli_entry *ed)
+int ast_cli_unregister(struct ast_cli_entry *e)
{
if (e->inuse) {
ast_log(LOG_WARNING, "Can't remove command that is in use\n");
@@ -2225,7 +2225,7 @@
return 0;
}
-static int __ast_cli_register(struct ast_cli_entry *e, struct ast_cli_entry *ed)
+int __ast_cli_register(struct ast_cli_entry *e, struct ast_module *module)
{
struct ast_cli_entry *cur;
int i, lf, ret = -1;
@@ -2244,7 +2244,13 @@
}
memset(&a, '\0', sizeof(a));
+
+ e->module = module;
+
+ ast_module_ref(e->module);
e->handler(e, CLI_INIT, &a);
+ ast_module_unref(e->module);
+
/* XXX check that usage and command are filled up */
s = ast_skip_blanks(e->command);
s = e->command = ast_strdup(s);
@@ -2295,27 +2301,16 @@
return ret;
}
-/* wrapper function, so we can unregister deprecated commands recursively */
-int ast_cli_unregister(struct ast_cli_entry *e)
-{
- return __ast_cli_unregister(e, NULL);
-}
-
-/* wrapper function, so we can register deprecated commands recursively */
-int ast_cli_register(struct ast_cli_entry *e)
-{
- return __ast_cli_register(e, NULL);
-}
-
/*
* register/unregister an array of entries.
*/
-int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
+int __ast_cli_register_multiple(struct ast_cli_entry *e, int len, struct ast_module *module)
{
int i, res = 0;
- for (i = 0; i < len; i++)
- res |= ast_cli_register(e + i);
+ for (i = 0; i < len; i++) {
+ res |= __ast_cli_register(e + i, module);
+ }
return res;
}
@@ -2329,7 +2324,6 @@
return res;
}
-
/*! \brief helper for final part of handle_help
* if locked = 1, assume the list is already locked
@@ -2657,7 +2651,9 @@
.n = state - matchnum,
.argv = argv,
.argc = x};
+ ast_module_ref(e->module);
ret = e->handler(e, CLI_GENERATE, &a);
+ ast_module_unref(e->module);
}
if (ret)
break;
@@ -2714,7 +2710,9 @@
*/
args[0] = (char *)e;
+ ast_module_ref(e->module);
retval = e->handler(e, CLI_HANDLER, &a);
+ ast_module_unref(e->module);
if (retval == CLI_SHOWUSAGE) {
ast_cli(fd, "%s", S_OR(e->usage, "Invalid usage, but no usage information available.\n"));
diff --git a/res/ari/cli.c b/res/ari/cli.c
index 4468e44..ff1569f 100644
--- a/res/ari/cli.c
+++ b/res/ari/cli.c
@@ -258,7 +258,7 @@
AST_CLI_DEFINE(ari_mkpasswd, "Encrypts a password"),
};
-int ast_ari_cli_register(void) {
+int ast_ari_cli_register(const struct ast_module_info *ast_module_info) {
return ast_cli_register_multiple(cli_ari, ARRAY_LEN(cli_ari));
}
diff --git a/res/ari/internal.h b/res/ari/internal.h
index 93ea0b7..8d064de 100644
--- a/res/ari/internal.h
+++ b/res/ari/internal.h
@@ -28,6 +28,7 @@
#include "asterisk/http.h"
#include "asterisk/json.h"
#include "asterisk/stringfields.h"
+#include "asterisk/module.h"
/*! @{ */
@@ -37,7 +38,7 @@
* \return 0 on success.
* \return Non-zero on error.
*/
-int ast_ari_cli_register(void);
+int ast_ari_cli_register(const struct ast_module_info *ast_module_info);
/*!
* \brief Unregister CLI commands for ARI.
diff --git a/res/parking/parking_ui.c b/res/parking/parking_ui.c
index 7d4726c..00eee72 100644
--- a/res/parking/parking_ui.c
+++ b/res/parking/parking_ui.c
@@ -197,7 +197,7 @@
AST_CLI_DEFINE(handle_show_parking_lot_cmd, "Show a parking lot or a list of all parking lots."),
};
-int load_parking_ui(void)
+int load_parking_ui(const struct ast_module_info *ast_module_info)
{
return ast_cli_register_multiple(cli_parking_lot, ARRAY_LEN(cli_parking_lot));
}
diff --git a/res/parking/res_parking.h b/res/parking/res_parking.h
index 3d77e51..2720a1a 100644
--- a/res/parking/res_parking.h
+++ b/res/parking/res_parking.h
@@ -497,7 +497,7 @@
* \retval 0 if successful
* \retval -1 on failure
*/
-int load_parking_ui(void);
+int load_parking_ui(const struct ast_module_info *ast_module_info);
/*!
* \since 12.0.0
diff --git a/res/res_ari.c b/res/res_ari.c
index d676b5e..cbc2a8b 100644
--- a/res/res_ari.c
+++ b/res/res_ari.c
@@ -1052,7 +1052,7 @@
ast_debug(3, "ARI disabled\n");
}
- if (ast_ari_cli_register() != 0) {
+ if (ast_ari_cli_register(ast_module_info) != 0) {
return AST_MODULE_LOAD_FAILURE;
}
diff --git a/res/res_clioriginate.c b/res/res_clioriginate.c
index 173f9f5..451b174 100644
--- a/res/res_clioriginate.c
+++ b/res/res_clioriginate.c
@@ -156,8 +156,6 @@
"used. If no extension is given, the 's' extension will be used.\n";
return NULL;
case CLI_GENERATE:
- /* ugly, can be removed when CLI entries have ast_module pointers */
- ast_module_ref(ast_module_info->self);
if (a->pos == 3) {
res = ast_cli_complete(a->word, choices, a->n);
} else if (a->pos == 4) {
@@ -165,15 +163,11 @@
res = ast_complete_applications(a->line, a->word, a->n);
}
}
- ast_module_unref(ast_module_info->self);
return res;
}
if (ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3]))
return CLI_SHOWUSAGE;
-
- /* ugly, can be removed when CLI entries have ast_module pointers */
- ast_module_ref(ast_module_info->self);
if (!strcasecmp("application", a->argv[3])) {
res = orig_app(a->fd, a->argv[2], a->argv[4], a->argv[5]);
@@ -182,8 +176,6 @@
} else {
res = CLI_SHOWUSAGE;
}
-
- ast_module_unref(ast_module_info->self);
return res;
}
diff --git a/res/res_convert.c b/res/res_convert.c
index 2a691a5..3ca5965 100644
--- a/res/res_convert.c
+++ b/res/res_convert.c
@@ -88,9 +88,6 @@
return NULL;
}
- /* ugly, can be removed when CLI entries have ast_module pointers */
- ast_module_ref(ast_module_info->self);
-
if (a->argc != 4 || ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3])) {
ret = CLI_SHOWUSAGE;
goto fail_out;
@@ -141,8 +138,6 @@
if (fs_in)
ast_closestream(fs_in);
-
- ast_module_unref(ast_module_info->self);
return ret;
}
diff --git a/res/res_parking.c b/res/res_parking.c
index a40990e..ad846e6 100644
--- a/res/res_parking.c
+++ b/res/res_parking.c
@@ -190,7 +190,6 @@
ASTERISK_REGISTER_FILE()
-#include "parking/res_parking.h"
#include "asterisk/config.h"
#include "asterisk/config_options.h"
#include "asterisk/utils.h"
@@ -200,6 +199,8 @@
#include "asterisk/features.h"
#include "asterisk/manager.h"
#include "asterisk/pbx.h"
+
+#include "parking/res_parking.h"
static int parking_lot_sort_fn(const void *obj_left, const void *obj_right, int flags)
{
@@ -1227,7 +1228,7 @@
goto error;
}
- if (load_parking_ui()) {
+ if (load_parking_ui(ast_module_info)) {
goto error;
}
diff --git a/res/res_pjsip/config_auth.c b/res/res_pjsip/config_auth.c
index 773889c..7328181 100644
--- a/res/res_pjsip/config_auth.c
+++ b/res/res_pjsip/config_auth.c
@@ -287,7 +287,7 @@
static struct ast_sip_cli_formatter_entry *cli_formatter;
/*! \brief Initialize sorcery with auth support */
-int ast_sip_initialize_sorcery_auth(void)
+int ast_sip_initialize_sorcery_auth(const struct ast_module_info *ast_module_info)
{
struct ast_sorcery *sorcery = ast_sip_get_sorcery();
diff --git a/res/res_pjsip/config_transport.c b/res/res_pjsip/config_transport.c
index 73030b1..b1f05b5 100644
--- a/res/res_pjsip/config_transport.c
+++ b/res/res_pjsip/config_transport.c
@@ -735,7 +735,7 @@
static struct ast_sip_cli_formatter_entry *cli_formatter;
/*! \brief Initialize sorcery with transport support */
-int ast_sip_initialize_sorcery_transport(void)
+int ast_sip_initialize_sorcery_transport(const struct ast_module_info *ast_module_info)
{
struct ast_sorcery *sorcery = ast_sip_get_sorcery();
diff --git a/res/res_pjsip/include/res_pjsip_private.h b/res/res_pjsip/include/res_pjsip_private.h
index 5120fc6..5c60c61 100644
--- a/res/res_pjsip/include/res_pjsip_private.h
+++ b/res/res_pjsip/include/res_pjsip_private.h
@@ -46,7 +46,7 @@
* \retval -1 failure
* \retval 0 success
*/
-int ast_sip_initialize_sorcery_transport(void);
+int ast_sip_initialize_sorcery_transport(const struct ast_module_info *ast_module_info);
/*!
* \internal
@@ -73,7 +73,7 @@
* \retval -1 failure
* \retval 0 success
*/
-int ast_sip_initialize_sorcery_location(void);
+int ast_sip_initialize_sorcery_location(const struct ast_module_info *ast_module_info);
/*!
* \internal
@@ -100,7 +100,7 @@
* \retval -1 failure
* \retval 0 success
*/
-int ast_sip_initialize_sorcery_auth(void);
+int ast_sip_initialize_sorcery_auth(const struct ast_module_info *ast_module_info);
/*!
* \internal
@@ -181,7 +181,9 @@
* \retval 0 on success
* \retval other on failure
*/
-int ast_res_pjsip_init_options_handling(int reload);
+#define ast_res_pjsip_init_options_handling(reload) \
+ __ast_res_pjsip_init_options_handling(reload ? NULL : ast_module_info)
+int __ast_res_pjsip_init_options_handling(const struct ast_module_info *ast_module_info);
/*!
* \internal
@@ -274,7 +276,7 @@
* \internal
* \brief Functions for initializing and destroying the CLI.
*/
-int ast_sip_initialize_cli(void);
+int ast_sip_initialize_cli(const struct ast_module_info *ast_module_info);
void ast_sip_destroy_cli(void);
/*!
diff --git a/res/res_pjsip/location.c b/res/res_pjsip/location.c
index 887053b..be486f1 100644
--- a/res/res_pjsip/location.c
+++ b/res/res_pjsip/location.c
@@ -891,7 +891,7 @@
}
/*! \brief Initialize sorcery with location support */
-int ast_sip_initialize_sorcery_location(void)
+int ast_sip_initialize_sorcery_location(const struct ast_module_info *ast_module_info)
{
struct ast_sorcery *sorcery = ast_sip_get_sorcery();
ast_sorcery_apply_default(sorcery, "contact", "astdb", "registrar");
diff --git a/res/res_pjsip/pjsip_cli.c b/res/res_pjsip/pjsip_cli.c
index 16df3f5..a83190d 100644
--- a/res/res_pjsip/pjsip_cli.c
+++ b/res/res_pjsip/pjsip_cli.c
@@ -350,7 +350,7 @@
AST_CLI_DEFINE(handle_pjsip_show_version, "Show the version of pjproject in use"),
};
-int ast_sip_initialize_cli(void)
+int ast_sip_initialize_cli(const struct ast_module_info *ast_module_info)
{
formatter_registry = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, 17,
formatter_hash, formatter_sort, formatter_compare);
diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c
index 54fdb65..4b4badd 100644
--- a/res/res_pjsip/pjsip_configuration.c
+++ b/res/res_pjsip/pjsip_configuration.c
@@ -1758,9 +1758,9 @@
return -1;
}
- ast_sip_initialize_cli();
+ ast_sip_initialize_cli(ast_module_info);
- if (ast_sip_initialize_sorcery_auth()) {
+ if (ast_sip_initialize_sorcery_auth(ast_module_info)) {
ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
ast_sorcery_unref(sip_sorcery);
sip_sorcery = NULL;
@@ -1872,14 +1872,14 @@
ast_sorcery_object_field_register(sip_sorcery, "endpoint", "message_context", "", OPT_STRINGFIELD_T, 1, STRFLDSET(struct ast_sip_endpoint, message_context));
ast_sorcery_object_field_register(sip_sorcery, "endpoint", "accountcode", "", OPT_STRINGFIELD_T, 1, STRFLDSET(struct ast_sip_endpoint, accountcode));
- if (ast_sip_initialize_sorcery_transport()) {
+ if (ast_sip_initialize_sorcery_transport(ast_module_info)) {
ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
ast_sorcery_unref(sip_sorcery);
sip_sorcery = NULL;
return -1;
}
- if (ast_sip_initialize_sorcery_location()) {
+ if (ast_sip_initialize_sorcery_location(ast_module_info)) {
ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
ast_sorcery_unref(sip_sorcery);
sip_sorcery = NULL;
diff --git a/res/res_pjsip/pjsip_options.c b/res/res_pjsip/pjsip_options.c
index 8ffb88c..45fa669 100644
--- a/res/res_pjsip/pjsip_options.c
+++ b/res/res_pjsip/pjsip_options.c
@@ -1150,11 +1150,11 @@
.format_ami = format_ami_contact_status
};
-int ast_res_pjsip_init_options_handling(int reload)
+int __ast_res_pjsip_init_options_handling(const struct ast_module_info *ast_module_info)
{
static const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
- if (reload) {
+ if (!ast_module_info) {
qualify_and_schedule_all();
return 0;
}
diff --git a/utils/ael_main.c b/utils/ael_main.c
index 2237625..06ba8e6 100644
--- a/utils/ael_main.c
+++ b/utils/ael_main.c
@@ -103,7 +103,7 @@
const char *ast_config_AST_CONFIG_DIR = config_dir;
const char *ast_config_AST_VAR_DIR = var_dir;
-void ast_cli_register_multiple(void);
+void __ast_cli_register_multiple(void);
int ast_add_extension2(struct ast_context *con,
int replace, const char *extension, int priority, const char *label, const char *callerid,
const char *application, void *data, void (*datad)(void *),
@@ -208,7 +208,7 @@
}
-void ast_cli_register_multiple(void)
+void __ast_cli_register_multiple(void)
{
if(!no_comp)
printf("Executed ast_cli_register_multiple();\n");
diff --git a/utils/clicompat.c b/utils/clicompat.c
index d25a710..426dd5c 100644
--- a/utils/clicompat.c
+++ b/utils/clicompat.c
@@ -21,8 +21,8 @@
return 0;
}
-int ast_cli_register_multiple(struct ast_cli_entry *e, int len);
-int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
+int __ast_cli_register_multiple(struct ast_cli_entry *e, int len);
+int __ast_cli_register_multiple(struct ast_cli_entry *e, int len)
{
return 0;
}
diff --git a/utils/conf2ael.c b/utils/conf2ael.c
index 99304b2..76a3ad3 100644
--- a/utils/conf2ael.c
+++ b/utils/conf2ael.c
@@ -605,9 +605,9 @@
return localized_context_find_or_create(extcontexts, exttable, name, registrar);
}
-void ast_cli_register_multiple(void);
+void __ast_cli_register_multiple(void);
-void ast_cli_register_multiple(void)
+void __ast_cli_register_multiple(void)
{
}
--
To view, visit https://gerrit.asterisk.org/337
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I7aafc7c9f2b912918f28fe51d51e9e8a755750e3
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Corey Farrell <git at cfware.com>
More information about the asterisk-code-review
mailing list