Skip to content
Snippets Groups Projects
loader.c 45.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 1999 - 2006, Digium, Inc.
     *
     * Mark Spencer <markster@digium.com>
     * Kevin P. Fleming <kpfleming@digium.com>
     * Luigi Rizzo <rizzo@icir.org>
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    /*! \file
     *
     * \brief Module Loader
    
     * \author Mark Spencer <markster@digium.com>
    
     * \author Kevin P. Fleming <kpfleming@digium.com>
     * \author Luigi Rizzo <rizzo@icir.org>
     * - See ModMngMnt
     */
    
    
    /*** MODULEINFO
    	<support_level>core</support_level>
     ***/
    
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include "asterisk/_private.h"
    
    #include "asterisk/paths.h"	/* use ast_config_AST_MODULE_DIR */
    
    #include "asterisk/dlinkedlists.h"
    
    #include "asterisk/module.h"
    #include "asterisk/config.h"
    #include "asterisk/channel.h"
    #include "asterisk/term.h"
    
    #include "asterisk/manager.h"
    #include "asterisk/cdr.h"
    #include "asterisk/enum.h"
    #include "asterisk/http.h"
    #include "asterisk/lock.h"
    
    Matthew Jordan's avatar
    Matthew Jordan committed
    #include "asterisk/features_config.h"
    
    David Vossel's avatar
    David Vossel committed
    #include "asterisk/heap.h"
    
    Kinsey Moore's avatar
    Kinsey Moore committed
    #include "asterisk/test.h"
    
    #include "asterisk/sounds_index.h"
    
    
    #include <dlfcn.h>
    
    #include "asterisk/md5.h"
    #include "asterisk/utils.h"
    
    
    	<managerEvent language="en_US" name="Reload">
    		<managerEventInstance class="EVENT_FLAG_SYSTEM">
    			<synopsis>Raised when a module has been reloaded in Asterisk.</synopsis>
    			<syntax>
    				<parameter name="Module">
    					<para>The name of the module that was reloaded, or
    					<literal>All</literal> if all modules were reloaded</para>
    				</parameter>
    				<parameter name="Status">
    					<para>The numeric status code denoting the success or failure
    					of the reload request.</para>
    					<enumlist>
    						<enum name="0"><para>Success</para></enum>
    						<enum name="1"><para>Request queued</para></enum>
    						<enum name="2"><para>Module not found</para></enum>
    						<enum name="3"><para>Error</para></enum>
    						<enum name="4"><para>Reload already in progress</para></enum>
    						<enum name="5"><para>Module uninitialized</para></enum>
    						<enum name="6"><para>Reload not supported</para></enum>
    					</enumlist>
    				</parameter>
    			</syntax>
    		</managerEventInstance>
    	</managerEvent>
    
    #ifndef RTLD_LOCAL
    #define RTLD_LOCAL 0
    #endif
    
    
    struct ast_module_user {
    	struct ast_channel *chan;
    	AST_LIST_ENTRY(ast_module_user) entry;
    };
    
    
    AST_DLLIST_HEAD(module_user_list, ast_module_user);
    
    static const unsigned char expected_key[] =
    
    { 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
      0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
    
    
    static char buildopt_sum[33] = AST_BUILDOPT_SUM;
    
    static unsigned int embedding = 1; /* we always start out by registering embedded modules,
    				      since they are here before we dlopen() any
    				   */
    
    
    /*!
     * \brief Internal flag to indicate all modules have been initially loaded.
     */
    static int modules_loaded;
    
    
    struct ast_module {
    	const struct ast_module_info *info;
    
    #ifdef REF_DEBUG
    	/* Used to get module references into REF_DEBUG logs */
    	void *ref_debug;
    #endif
    
    	void *lib;					/* the shared lib, or NULL if embedded */
    	int usecount;					/* the number of 'users' currently in this module */
    	struct module_user_list users;			/* the list of users in the module */
    
    		unsigned int running:1;
    		unsigned int declined:1;
    
    		unsigned int keepuntilshutdown:1;
    
    	AST_LIST_ENTRY(ast_module) list_entry;
    	AST_DLLIST_ENTRY(ast_module) entry;
    
    static AST_DLLIST_HEAD_STATIC(module_list, ast_module);
    
    const char *ast_module_name(const struct ast_module *mod)
    {
    	if (!mod || !mod->info) {
    		return NULL;
    	}
    
    	return mod->info->name;
    }
    
    
    /*
     * module_list is cleared by its constructor possibly after
     * we start accumulating embedded modules, so we need to
     * use another list (without the lock) to accumulate them.
     * Then we update the main list when embedding is done.
     */
    static struct module_list embedded_module_list;
    
    
    struct loadupdate {
    	int (*updater)(void);
    	AST_LIST_ENTRY(loadupdate) entry;
    };
    
    
    static AST_DLLIST_HEAD_STATIC(updaters, loadupdate);
    
    
    AST_MUTEX_DEFINE_STATIC(reloadlock);
    
    
    struct reload_queue_item {
    	AST_LIST_ENTRY(reload_queue_item) entry;
    	char module[0];
    };
    
    static int do_full_reload = 0;
    
    
    static AST_DLLIST_HEAD_STATIC(reload_queue, reload_queue_item);
    
    /* when dynamic modules are being loaded, ast_module_register() will
       need to know what filename the module was loaded from while it
       is being registered
    */
    
    static struct ast_module *resource_being_loaded;
    
    
    /* XXX: should we check for duplicate resource names here? */
    
    void ast_module_register(const struct ast_module_info *info)
    {
    	struct ast_module *mod;
    
    	if (embedding) {
    		if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
    			return;
    		strcpy(mod->resource, info->name);
    	} else {
    		mod = resource_being_loaded;
    	}
    
    
    	ast_debug(5, "Registering module %s\n", info->name);
    
    #ifdef REF_DEBUG
    	mod->ref_debug = ao2_t_alloc(0, NULL, info->name);
    #endif
    
    	AST_LIST_HEAD_INIT(&mod->users);
    
    	/* during startup, before the loader has been initialized,
    	   there are no threads, so there is no need to take the lock
    	   on this list to manipulate it. it is also possible that it
    	   might be unsafe to use the list lock at that point... so
    	   let's avoid it altogether
    	*/
    
    	if (embedding) {
    
    		AST_DLLIST_INSERT_TAIL(&embedded_module_list, mod, entry);
    
    		/* it is paramount that the new entry be placed at the tail of
    		   the list, otherwise the code that uses dlopen() to load
    		   dynamic modules won't be able to find out if the module it
    		   just opened was registered or failed to load
    		*/
    
    		AST_DLLIST_INSERT_TAIL(&module_list, mod, entry);
    		AST_DLLIST_UNLOCK(&module_list);
    
    
    	/* give the module a copy of its own handle, for later use in registrations and the like */
    	*((struct ast_module **) &(info->self)) = mod;
    }
    
    void ast_module_unregister(const struct ast_module_info *info)
    {
    	struct ast_module *mod = NULL;
    
    	/* it is assumed that the users list in the module structure
    	   will already be empty, or we cannot have gotten to this
    	   point
    	*/
    
    	AST_DLLIST_LOCK(&module_list);
    	AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&module_list, mod, entry) {
    
    			AST_DLLIST_REMOVE_CURRENT(entry);
    
    	AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
    	AST_DLLIST_UNLOCK(&module_list);
    
    		ast_debug(5, "Unregistering module %s\n", info->name);
    
    		AST_LIST_HEAD_DESTROY(&mod->users);
    
    #ifdef REF_DEBUG
    		ao2_cleanup(mod->ref_debug);
    #endif
    
    struct ast_module_user *__ast_module_user_add(struct ast_module *mod, struct ast_channel *chan)
    
    	struct ast_module_user *u;
    
    	u = ast_calloc(1, sizeof(*u));
    	if (!u) {
    
    
    	u->chan = chan;
    
    	AST_LIST_LOCK(&mod->users);
    	AST_LIST_INSERT_HEAD(&mod->users, u, entry);
    	AST_LIST_UNLOCK(&mod->users);
    
    
    #ifdef REF_DEBUG
    	ao2_ref(mod->ref_debug, +1);
    #endif
    
    
    	ast_atomic_fetchadd_int(&mod->usecount, +1);
    
    	ast_update_use_count();
    
    	return u;
    }
    
    void __ast_module_user_remove(struct ast_module *mod, struct ast_module_user *u)
    {
    
    	AST_LIST_LOCK(&mod->users);
    
    	u = AST_LIST_REMOVE(&mod->users, u, entry);
    
    	AST_LIST_UNLOCK(&mod->users);
    
    	if (!u) {
    		/*
    		 * Was not in the list.  Either a bad pointer or
    		 * __ast_module_user_hangup_all() has been called.
    		 */
    		return;
    	}
    
    
    #ifdef REF_DEBUG
    	ao2_ref(mod->ref_debug, -1);
    #endif
    
    
    	ast_atomic_fetchadd_int(&mod->usecount, -1);
    
    
    	ast_update_use_count();
    }
    
    void __ast_module_user_hangup_all(struct ast_module *mod)
    {
    	struct ast_module_user *u;
    
    	AST_LIST_LOCK(&mod->users);
    	while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
    
    		if (u->chan) {
    			ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
    		}
    
    
    #ifdef REF_DEBUG
    		ao2_ref(mod->ref_debug, -1);
    #endif
    
    
    		ast_atomic_fetchadd_int(&mod->usecount, -1);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	ast_update_use_count();
    
    }
    
    /*! \note
     * In addition to modules, the reload command handles some extra keywords
     * which are listed here together with the corresponding handlers.
     * This table is also used by the command completion code.
     */
    static struct reload_classes {
    	const char *name;
    	int (*reload_fn)(void);
    } reload_classes[] = {	/* list in alpha order, longest match first for cli completion */
    
    	{ "acl",         ast_named_acl_reload },
    	{ "cdr",         ast_cdr_engine_reload },
    	{ "cel",         ast_cel_engine_reload },
    	{ "dnsmgr",      dnsmgr_reload },
    	{ "dsp",         ast_dsp_reload},
    	{ "extconfig",   read_config_maps },
    	{ "enum",        ast_enum_reload },
    	{ "features",    ast_features_config_reload },
    	{ "http",        ast_http_reload },
    
    	{ "indications", ast_indications_reload },
    
    	{ "logger",      logger_reload },
    	{ "manager",     reload_manager },
    	{ "plc",         ast_plc_reload },
    	{ "sounds",      ast_sounds_reindex },
    	{ "udptl",       ast_udptl_reload },
    	{ NULL,          NULL }
    
    };
    
    static int printdigest(const unsigned char *d)
    {
    	int x, pos;
    	char buf[256]; /* large enough so we don't have to worry */
    
    	for (pos = 0, x = 0; x < 16; x++)
    
    		pos += sprintf(buf + pos, " %02hhx", *d++);
    
    	ast_debug(1, "Unexpected signature:%s\n", buf);
    
    
    	return 0;
    }
    
    static int key_matches(const unsigned char *key1, const unsigned char *key2)
    {
    	int x;
    
    	for (x = 0; x < 16; x++) {
    		if (key1[x] != key2[x])
    			return 0;
    	}
    
    	return 1;
    }
    
    static int verify_key(const unsigned char *key)
    {
    	struct MD5Context c;
    	unsigned char digest[16];
    
    	MD5Init(&c);
    	MD5Update(&c, key, strlen((char *)key));
    	MD5Final(digest, &c);
    
    	if (key_matches(expected_key, digest))
    		return 0;
    
    	printdigest(digest);
    
    	return -1;
    }
    
    static int resource_name_match(const char *name1_in, const char *name2_in)
    {
    	char *name1 = (char *) name1_in;
    	char *name2 = (char *) name2_in;
    
    	/* trim off any .so extensions */
    	if (!strcasecmp(name1 + strlen(name1) - 3, ".so")) {
    		name1 = ast_strdupa(name1);
    		name1[strlen(name1) - 3] = '\0';
    	}
    	if (!strcasecmp(name2 + strlen(name2) - 3, ".so")) {
    		name2 = ast_strdupa(name2);
    		name2[strlen(name2) - 3] = '\0';
    	}
    
    	return strcasecmp(name1, name2);
    }
    
    static struct ast_module *find_resource(const char *resource, int do_lock)
    {
    	struct ast_module *cur;
    
    
    	if (do_lock) {
    		AST_DLLIST_LOCK(&module_list);
    	}
    
    	AST_DLLIST_TRAVERSE(&module_list, cur, entry) {
    
    		if (!resource_name_match(resource, cur->resource))
    			break;
    	}
    
    
    	if (do_lock) {
    		AST_DLLIST_UNLOCK(&module_list);
    	}
    
    #ifdef LOADABLE_MODULES
    
    
    /*!
     * \brief dlclose(), with failure logging.
     */
    static void logged_dlclose(const char *name, void *lib)
    {
    
    	char *error;
    
    	if (!lib) {
    		return;
    	}
    
    	/* Clear any existing error */
    	dlerror();
    	if (dlclose(lib)) {
    		error = dlerror();
    		ast_log(AST_LOG_ERROR, "Failure in dlclose for module '%s': %s\n",
    			S_OR(name, "unknown"), S_OR(error, "Unknown error"));
    
    	}
    }
    
    #if defined(HAVE_RTLD_NOLOAD)
    /*!
     * \brief Check to see if the given resource is loaded.
     *
     * \param resource_name Name of the resource, including .so suffix.
     * \return False (0) if module is not loaded.
     * \return True (non-zero) if module is loaded.
     */
    static int is_module_loaded(const char *resource_name)
    {
    	char fn[PATH_MAX] = "";
    	void *lib;
    
    	snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_MODULE_DIR,
    		resource_name);
    
    	lib = dlopen(fn, RTLD_LAZY | RTLD_NOLOAD);
    
    	if (lib) {
    		logged_dlclose(resource_name, lib);
    		return 1;
    	}
    
    	return 0;
    }
    #endif
    
    
    static void unload_dynamic_module(struct ast_module *mod)
    {
    
    #if defined(HAVE_RTLD_NOLOAD)
    
    	char *name = ast_strdupa(ast_module_name(mod));
    
    	void *lib = mod->lib;
    
    	/* WARNING: the structure pointed to by mod is going to
    	   disappear when this operation succeeds, so we can't
    	   dereference it */
    
    	logged_dlclose(ast_module_name(mod), lib);
    
    
    	/* There are several situations where the module might still be resident
    	 * in memory.
    	 *
    	 * If somehow there was another dlopen() on the same module (unlikely,
    	 * since that all is supposed to happen in loader.c).
    	 *
    	 * Or the lazy resolution of a global symbol (very likely, since that is
    	 * how we load all of our modules that export global symbols).
    	 *
    	 * Avoid the temptation of repeating the dlclose(). The other code that
    	 * dlopened the module still has its module reference, and should close
    	 * it itself. In other situations, dlclose() will happily return success
    	 * for as many times as you wish to call it.
    	 */
    #if defined(HAVE_RTLD_NOLOAD)
    	if (is_module_loaded(name)) {
    		ast_log(LOG_WARNING, "Module '%s' could not be completely unloaded\n", name);
    	}
    #endif
    
    static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, unsigned int suppress_logging, struct ast_heap *resource_heap, int required);
    
    #define MODULE_LOCAL_ONLY (void *)-1
    
    static struct ast_module *load_dynamic_module(const char *resource_in, unsigned int global_symbols_only, unsigned int suppress_logging, struct ast_heap *resource_heap)
    
    	void *lib = NULL;
    
    	int space;	/* room needed for the descriptor */
    	int missing_so = 0;
    
    	space = sizeof(*resource_being_loaded) + strlen(resource_in) + 1;
    	if (strcasecmp(resource_in + strlen(resource_in) - 3, ".so")) {
    		missing_so = 1;
    		space += 3;	/* room for the extra ".so" */
    
    	snprintf(fn, sizeof(fn), "%s/%s%s", ast_config_AST_MODULE_DIR, resource_in, missing_so ? ".so" : "");
    
    	/* make a first load of the module in 'quiet' mode... don't try to resolve
    	   any symbols, and don't export any symbols. this will allow us to peek into
    	   the module's info block (if available) to see what flags it has set */
    
    
    	resource_being_loaded = ast_calloc(1, space);
    	if (!resource_being_loaded)
    
    	strcpy(resource_being_loaded->resource, resource_in);
    	if (missing_so)
    		strcat(resource_being_loaded->resource, ".so");
    
    	if (!(lib = dlopen(fn, RTLD_LAZY | RTLD_GLOBAL))) {
    		if (!suppress_logging) {
    			ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
    		}
    
    		return NULL;
    	}
    
    	/* the dlopen() succeeded, let's find out if the module
    	   registered itself */
    	/* note that this will only work properly as long as
    	   ast_module_register() (which is called by the module's
    	   constructor) places the new module at the tail of the
    	   module_list
    	*/
    
    	if (resource_being_loaded != (mod = AST_DLLIST_LAST(&module_list))) {
    
    		ast_log(LOG_WARNING, "Module '%s' did not register itself during load\n", resource_in);
    
    		/* no, it did not, so close it and return */
    
    		/* note that the module's destructor will call ast_module_unregister(),
    		   which will free the structure we allocated in resource_being_loaded */
    
    	wants_global = ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS);
    
    
    	/* if we are being asked only to load modules that provide global symbols,
    	   and this one does not, then close it and return */
    
    	if (global_symbols_only && !wants_global) {
    
    		return MODULE_LOCAL_ONLY;
    
    	resource_being_loaded = ast_calloc(1, space);
    	if (!resource_being_loaded)
    
    	strcpy(resource_being_loaded->resource, resource_in);
    	if (missing_so)
    		strcat(resource_being_loaded->resource, ".so");
    
    
    	if (!(lib = dlopen(fn, wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL))) {
    
    		ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
    
    	/* since the module was successfully opened, and it registered itself
    	   the previous time we did that, we're going to assume it worked this
    	   time too :) */
    
    	AST_DLLIST_LAST(&module_list)->lib = lib;
    
    	return AST_DLLIST_LAST(&module_list);
    
    void ast_module_shutdown(void)
    {
    	struct ast_module *mod;
    
    	int somethingchanged = 1, final = 0;
    
    	/*!\note Some resources, like timers, are started up dynamically, and thus
    	 * may be still in use, even if all channels are dead.  We must therefore
    	 * check the usecount before asking modules to unload. */
    	do {
    		if (!somethingchanged) {
    			/*!\note If we go through the entire list without changing
    			 * anything, ignore the usecounts and unload, then exit. */
    			final = 1;
    		}
    
    		/* Reset flag before traversing the list */
    		somethingchanged = 0;
    
    
    		AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&module_list, mod, entry) {
    
    			if (!final && mod->usecount) {
    
    				ast_debug(1, "Passing on %s: its use count is %d\n",
    					mod->resource, mod->usecount);
    
    			AST_DLLIST_REMOVE_CURRENT(entry);
    
    			if (mod->flags.running && !mod->flags.declined && mod->info->unload) {
    
    				ast_verb(1, "Unloading %s\n", mod->resource);
    
    				mod->info->unload();
    			}
    			AST_LIST_HEAD_DESTROY(&mod->users);
    
    #ifdef REF_DEBUG
    			ao2_cleanup(mod->ref_debug);
    #endif
    
    			free(mod);
    			somethingchanged = 1;
    		}
    
    		AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
    
    		if (!somethingchanged) {
    			AST_DLLIST_TRAVERSE(&module_list, mod, entry) {
    				if (mod->flags.keepuntilshutdown) {
    					ast_module_unref(mod);
    					mod->flags.keepuntilshutdown = 0;
    					somethingchanged = 1;
    				}
    			}
    		}
    
    	} while (somethingchanged && !final);
    
    
    	AST_DLLIST_UNLOCK(&module_list);
    
    int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode force)
    {
    	struct ast_module *mod;
    	int res = -1;
    	int error = 0;
    
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	if (!(mod = find_resource(resource_name, 0))) {
    
    		AST_DLLIST_UNLOCK(&module_list);
    
    		ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	}
    
    	if (!mod->flags.running || mod->flags.declined) {
    		ast_log(LOG_WARNING, "Unload failed, '%s' is not loaded.\n", resource_name);
    
    
    	if (!error && (mod->usecount > 0)) {
    
    			ast_log(LOG_WARNING, "Warning:  Forcing removal of module '%s' with use count %d\n",
    				resource_name, mod->usecount);
    		else {
    			ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
    				mod->usecount);
    			error = 1;
    		}
    	}
    
    	if (!error) {
    
    		/* Request any channels attached to the module to hangup. */
    
    		__ast_module_user_hangup_all(mod);
    
    
    		ast_verb(1, "Unloading %s\n", mod->resource);
    
    		res = mod->info->unload();
    
    		if (res) {
    			ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
    
    			if (force <= AST_FORCE_FIRM) {
    
    				ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
    
    			}
    		}
    
    		if (!error) {
    			/*
    			 * Request hangup on any channels that managed to get attached
    			 * while we called the module unload function.
    			 */
    			__ast_module_user_hangup_all(mod);
    			sched_yield();
    
    		mod->flags.running = mod->flags.declined = 0;
    
    	AST_DLLIST_UNLOCK(&module_list);
    
    	if (!error && !mod->lib && mod->info && mod->info->restore_globals)
    
    #ifdef LOADABLE_MODULES
    
    Kinsey Moore's avatar
    Kinsey Moore committed
    	if (!error) {
    
    		unload_dynamic_module(mod);
    
    Kinsey Moore's avatar
    Kinsey Moore committed
    		ast_test_suite_event_notify("MODULE_UNLOAD", "Message: %s", resource_name);
    	}
    
    #endif
    
    	if (!error)
    		ast_update_use_count();
    
    	return res;
    }
    
    char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload)
    {
    	struct ast_module *cur;
    	int i, which=0, l = strlen(word);
    	char *ret = NULL;
    
    	if (pos != rpos)
    		return NULL;
    
    
    	AST_DLLIST_LOCK(&module_list);
    	AST_DLLIST_TRAVERSE(&module_list, cur, entry) {
    
    		if (!strncasecmp(word, cur->resource, l) &&
    		    (cur->info->reload || !needsreload) &&
    		    ++which > state) {
    
    			ret = ast_strdup(cur->resource);
    
    	AST_DLLIST_UNLOCK(&module_list);
    
    
    	if (!ret) {
    		for (i=0; !ret && reload_classes[i].name; i++) {
    			if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
    
    				ret = ast_strdup(reload_classes[i].name);
    
    void ast_process_pending_reloads(void)
    {
    	struct reload_queue_item *item;
    
    
    
    	AST_LIST_LOCK(&reload_queue);
    
    	if (do_full_reload) {
    		do_full_reload = 0;
    		AST_LIST_UNLOCK(&reload_queue);
    		ast_log(LOG_NOTICE, "Executing deferred reload request.\n");
    		ast_module_reload(NULL);
    		return;
    	}
    
    	while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
    		ast_log(LOG_NOTICE, "Executing deferred reload request for module '%s'.\n", item->module);
    		ast_module_reload(item->module);
    		ast_free(item);
    	}
    
    	AST_LIST_UNLOCK(&reload_queue);
    }
    
    static void queue_reload_request(const char *module)
    {
    	struct reload_queue_item *item;
    
    	AST_LIST_LOCK(&reload_queue);
    
    	if (do_full_reload) {
    		AST_LIST_UNLOCK(&reload_queue);
    		return;
    	}
    
    	if (ast_strlen_zero(module)) {
    		/* A full reload request (when module is NULL) wipes out any previous
    		   reload requests and causes the queue to ignore future ones */
    		while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
    			ast_free(item);
    		}
    		do_full_reload = 1;
    	} else {
    		/* No reason to add the same module twice */
    		AST_LIST_TRAVERSE(&reload_queue, item, entry) {
    			if (!strcasecmp(item->module, module)) {
    				AST_LIST_UNLOCK(&reload_queue);
    				return;
    			}
    		}
    		item = ast_calloc(1, sizeof(*item) + strlen(module) + 1);
    		if (!item) {
    			ast_log(LOG_ERROR, "Failed to allocate reload queue item.\n");
    			AST_LIST_UNLOCK(&reload_queue);
    			return;
    		}
    		strcpy(item->module, module);
    		AST_LIST_INSERT_TAIL(&reload_queue, item, entry);
    	}
    	AST_LIST_UNLOCK(&reload_queue);
    }
    
    
    /*!
     * \since 12
     * \internal
     * \brief Publish a \ref stasis message regarding the reload result
     */
    static void publish_reload_message(const char *name, enum ast_module_reload_result result)
    {
    	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
    	RAII_VAR(struct ast_json_payload *, payload, NULL, ao2_cleanup);
    	RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
    	RAII_VAR(struct ast_json *, event_object, NULL, ast_json_unref);
    	char res_buffer[8];
    
    
    	if (!ast_manager_get_generic_type()) {
    		return;
    	}
    
    
    	snprintf(res_buffer, sizeof(res_buffer), "%u", result);
    
    	event_object = ast_json_pack("{s: s, s: s}",
    			"Module", S_OR(name, "All"),
    			"Status", res_buffer);
    
    	json_object = ast_json_pack("{s: s, s: i, s: o}",
    
    			"type", "Reload",
    			"class_type", EVENT_FLAG_SYSTEM,
    
    			"event", ast_json_ref(event_object));
    
    
    	if (!json_object) {
    		return;
    	}
    
    	payload = ast_json_payload_create(json_object);
    	if (!payload) {
    		return;
    	}
    
    	message = stasis_message_create(ast_manager_get_generic_type(), payload);
    	if (!message) {
    		return;
    	}
    
    	stasis_publish(ast_manager_get_topic(), message);
    }
    
    enum ast_module_reload_result ast_module_reload(const char *name)
    
    	enum ast_module_reload_result res = AST_MODULE_RELOAD_NOT_FOUND;
    
    	/* If we aren't fully booted, we just pretend we reloaded but we queue this
    	   up to run once we are booted up. */
    
    		queue_reload_request(name);
    
    		res = AST_MODULE_RELOAD_QUEUED;
    		goto module_reload_exit;
    
    	if (ast_mutex_trylock(&reloadlock)) {
    
    		ast_verb(3, "The previous reload command didn't finish yet\n");
    
    		res = AST_MODULE_RELOAD_IN_PROGRESS;
    		goto module_reload_exit;
    
    	ast_sd_notify("RELOAD=1");
    
    	if (ast_opt_lock_confdir) {
    		int try;
    		int res;
    		for (try = 1, res = AST_LOCK_TIMEOUT; try < 6 && (res == AST_LOCK_TIMEOUT); try++) {
    			res = ast_lock_path(ast_config_AST_CONFIG_DIR);
    			if (res == AST_LOCK_TIMEOUT) {
    				ast_log(LOG_WARNING, "Failed to grab lock on %s, try %d\n", ast_config_AST_CONFIG_DIR, try);
    			}
    		}
    		if (res != AST_LOCK_SUCCESS) {
    
    			ast_log(AST_LOG_WARNING, "Cannot grab lock on %s\n", ast_config_AST_CONFIG_DIR);
    
    			res = AST_MODULE_RELOAD_ERROR;
    
    			goto module_reload_done;
    
    	/* Call "predefined" reload here first */
    	for (i = 0; reload_classes[i].name; i++) {
    		if (!name || !strcasecmp(name, reload_classes[i].name)) {
    
    			if (reload_classes[i].reload_fn() == AST_MODULE_LOAD_SUCCESS) {
    				res = AST_MODULE_RELOAD_SUCCESS;
    
    	if (name && res == AST_MODULE_RELOAD_SUCCESS) {
    
    		if (ast_opt_lock_confdir) {
    			ast_unlock_path(ast_config_AST_CONFIG_DIR);
    		}
    
    		goto module_reload_done;
    
    	AST_DLLIST_LOCK(&module_list);
    	AST_DLLIST_TRAVERSE(&module_list, cur, entry) {
    
    		const struct ast_module_info *info = cur->info;
    
    		if (name && resource_name_match(name, cur->resource))
    			continue;
    
    
    		if (!cur->flags.running || cur->flags.declined) {
    
    			if (res == AST_MODULE_RELOAD_NOT_FOUND) {
    				res = AST_MODULE_RELOAD_UNINITIALIZED;
    			}
    			if (!name) {
    
    
    		if (!info->reload) {	/* cannot be reloaded */
    
    			if (res == AST_MODULE_RELOAD_NOT_FOUND) {
    				res = AST_MODULE_RELOAD_NOT_IMPLEMENTED;
    			}
    			if (!name) {
    				continue;
    			}
    			break;
    
    		ast_verb(3, "Reloading module '%s' (%s)\n", cur->resource, info->description);
    
    		if (info->reload() == AST_MODULE_LOAD_SUCCESS) {
    			res = AST_MODULE_RELOAD_SUCCESS;
    		}
    		if (name) {
    			break;
    
    	AST_DLLIST_UNLOCK(&module_list);
    
    	if (ast_opt_lock_confdir) {
    		ast_unlock_path(ast_config_AST_CONFIG_DIR);
    	}
    
    	ast_mutex_unlock(&reloadlock);
    
    	ast_sd_notify("READY=1");
    
    module_reload_exit:
    	publish_reload_message(name, res);
    
    	return res;
    }
    
    static unsigned int inspect_module(const struct ast_module *mod)
    {
    	if (!mod->info->description) {
    		ast_log(LOG_WARNING, "Module '%s' does not provide a description.\n", mod->resource);
    		return 1;
    	}
    
    	if (!mod->info->key) {
    		ast_log(LOG_WARNING, "Module '%s' does not provide a license key.\n", mod->resource);
    		return 1;
    	}
    
    	if (verify_key((unsigned char *) mod->info->key)) {
    		ast_log(LOG_WARNING, "Module '%s' did not provide a valid license key.\n", mod->resource);
    		return 1;
    	}
    
    
    	if (!ast_strlen_zero(mod->info->buildopt_sum) &&