代码解析之:plugin_init(config);
此函数定义在当前目录下plugin.c文件里面,主要的工作是将提供的plugins添加到plugins全局链表中,并初始化每个plugins:
- gboolean plugin_init(GKeyFile *config)
- {
- GSList *list;
- GDir *dir;
- const gchar *file;
- gchar **disabled;
- unsigned int i;
- /* Make a call to BtIO API so its symbols got resolved before the
- * plugins are loaded. */
- bt_io_error_quark();
- if (config)
- disabled = g_key_file_get_string_list(config, "General",
- "DisablePlugins",
- NULL, NULL);
- else
- disabled = NULL;
- DBG("Loading builtin plugins");
- //add default plugins, those plugins always need for bluetoothd runing
- //those plugins will add to the global link named plugins
- for (i = 0; <span style="color:#990000;"><strong>__bluetooth_builtin</strong></span>[i]; i++) {
- if (is_disabled(__bluetooth_builtin[i]->name, disabled))
- continue;
- <span style="color:#990000;"><strong>add_plugin(NULL, __bluetooth_builtin[i]);</strong></span>
- }
- if (strlen(PLUGINDIR) == 0) {
- g_strfreev(disabled);
- goto start;
- }
- DBG("Loading plugins %s", PLUGINDIR);
- dir = g_dir_open(PLUGINDIR, 0, NULL);
- if (!dir) {
- g_strfreev(disabled);
- goto start;
- }
- //add user plugins, those plugins stored in PLUGINDIR path, and the
- //PLUGINDIR = /usr/local/lib/bluetooth/plugins. The bluetoothd will
- //find all those plugins which name *.so, and open them, get the method
- //named bluetooth_plugin_desc, it will also add those plugins to the
- //plugins links.
- while ((file = g_dir_read_name(dir)) != NULL) {
- struct bluetooth_plugin_desc *desc;
- void *handle;
- gchar *filename;
- if (g_str_has_prefix(file, "lib") == TRUE ||
- g_str_has_suffix(file, ".so") == FALSE)
- continue;
- if (is_disabled(file, disabled))
- continue;
- filename = g_build_filename(PLUGINDIR, file, NULL);
- handle = dlopen(filename, RTLD_NOW);
- if (handle == NULL) {
- error("Can't load plugin %s: %s", filename,
- dlerror());
- g_free(filename);
- continue;
- }
- g_free(filename);
- desc = dlsym(handle, "bluetooth_plugin_desc");
- if (desc == NULL) {
- error("Can't load plugin description: %s", dlerror());
- dlclose(handle);
- continue;
- }
- if (add_plugin(handle, desc) == FALSE)
- dlclose(handle);
- }
- g_dir_close(dir);
- g_strfreev(disabled);
- start:
- //init all of the plugins by calling the plugins init function
- for (list = plugins; list; list = list->next) {
- struct bluetooth_plugin *plugin = list->data;
- if (plugin->desc->init() < 0) {
- error("Failed to init %s plugin", plugin->desc->name);
- continue;
- }
- plugin->active = TRUE;
- }
- return TRUE;
- }
