ref.h: doc: fix typo
[babeltrace.git] / lib / plugin-system / component-factory.c
index fbf36d6837877862b125986afa83333a3cfdb942..14454f506cd1bbd8af4333d1783bcede9c5cbf9d 100644 (file)
@@ -48,6 +48,8 @@
 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
                sizeof(LIBTOOL_PLUGIN_SUFFIX))
 
+DECLARE_PLUG_IN_SECTIONS;
+
 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
 static
 struct dirent *alloc_dirent(const char *path)
@@ -65,13 +67,36 @@ struct dirent *alloc_dirent(const char *path)
        return entry;
 }
 
+static
+enum bt_component_factory_status init_plugin(
+               struct bt_component_factory *factory, struct bt_plugin *plugin)
+{
+       enum bt_component_status component_status;
+       enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
+
+       BT_MOVE(factory->current_plugin, plugin);
+       component_status = bt_plugin_register_component_classes(
+                       factory->current_plugin, factory);
+       BT_PUT(factory->current_plugin);
+       if (component_status != BT_COMPONENT_STATUS_OK) {
+               switch (component_status) {
+               case BT_COMPONENT_STATUS_NOMEM:
+                       ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
+                       break;
+               default:
+                       ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
+                       break;
+               }
+       }
+       return ret;
+}
+
 static
 enum bt_component_factory_status
 bt_component_factory_load_file(struct bt_component_factory *factory,
                const char *path)
 {
        enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
-       enum bt_component_status component_status;
        size_t path_len;
        GModule *module;
        struct bt_plugin *plugin;
@@ -112,33 +137,17 @@ bt_component_factory_load_file(struct bt_component_factory *factory,
                goto end;
        }
 
-       /* Load plugin and make sure it defines the required entry points */
-       plugin = bt_plugin_create(module, path);
+       /* Load plugin and make sure it defines the required entry points. */
+       plugin = bt_plugin_create_from_module(module, path);
        if (!plugin) {
                ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN;
                if (!g_module_close(module)) {
-                       printf_error("Module close error: %s",
+                       printf_error("Module close error: %s\n",
                                g_module_error());
                }
                goto end;
        }
-
-       BT_MOVE(factory->current_plugin, plugin);
-       component_status = bt_plugin_register_component_classes(
-                       factory->current_plugin, factory);      
-       BT_PUT(factory->current_plugin);
-       if (component_status != BT_COMPONENT_STATUS_OK) {
-               switch (component_status) {
-               case BT_COMPONENT_STATUS_NOMEM:
-                       ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
-                       break;
-               default:
-                       ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
-                       break;
-               }
-
-               goto end;
-       }
+       ret = init_plugin(factory, plugin);
 end:
        return ret;
 }
@@ -207,7 +216,7 @@ bt_component_factory_load_dir(struct bt_component_factory *factory,
                stat_ret = stat(file_path, &st);
                if (stat_ret < 0) {
                        /* Continue to next file / directory. */
-                       printf_perror("Failed to stat() plugin file");
+                       printf_perror("Failed to stat() plugin file\n");
                        continue;
                }
 
@@ -218,7 +227,7 @@ bt_component_factory_load_dir(struct bt_component_factory *factory,
                                goto end;
                        }
                } else if (S_ISREG(st.st_mode)) {
-                       bt_component_factory_load_file(factory, file_path);
+                       bt_component_factory_load_file(factory, file_path);
                }
        }
 end:
@@ -398,6 +407,39 @@ enum bt_component_factory_status bt_component_factory_load(
        return _bt_component_factory_load(factory, path, false);
 }
 
+enum bt_component_factory_status bt_component_factory_load_static(
+               struct bt_component_factory *factory)
+{
+       size_t count, i;
+       enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
+
+       PRINT_PLUG_IN_SECTIONS(printf_verbose);
+
+       count = SECTION_ELEMENT_COUNT(__plugin_register_funcs);
+       if (SECTION_ELEMENT_COUNT(__plugin_register_funcs) != count ||
+                       SECTION_ELEMENT_COUNT(__plugin_names) != count ||
+                       SECTION_ELEMENT_COUNT(__plugin_authors) != count ||
+                       SECTION_ELEMENT_COUNT(__plugin_licenses) != count ||
+                       SECTION_ELEMENT_COUNT(__plugin_descriptions) != count) {
+               printf_error("Some statically-linked plug-ins do not define all mandatory symbols\n");
+               ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN;
+               goto end;
+       }
+       printf_verbose("Detected %zu statically-linked plug-ins\n", count);
+
+       for (i = 0; i < count; i++) {
+               struct bt_plugin *plugin = bt_plugin_create_from_static(i);
+
+               if (!plugin) {
+                       continue;
+               }
+
+               (void) init_plugin(factory, plugin);
+       }
+end:
+       return ret;
+}
+
 static
 enum bt_component_factory_status
 add_component_class(struct bt_component_factory *factory, const char *name,
@@ -423,7 +465,7 @@ add_component_class(struct bt_component_factory *factory, const char *name,
                struct bt_plugin *plugin = bt_component_class_get_plugin(
                        component_class);
 
-               printf_warning("Duplicate component class registration attempted. Component class %s being registered by plugin %s (path: %s) conflicts with one already registered by plugin %s (path: %s)",
+               printf_verbose("Duplicate component class registration attempted. Component class %s being registered by plugin %s (path: %s) conflicts with one already registered by plugin %s (path: %s)\n",
                        name, bt_plugin_get_name(factory->current_plugin),
                        bt_plugin_get_path(factory->current_plugin),
                        bt_plugin_get_name(plugin),
@@ -435,7 +477,7 @@ add_component_class(struct bt_component_factory *factory, const char *name,
        }
 
        component_class = bt_component_class_create(type, name, description,
-               init, factory->current_plugin);
+                       init, factory->current_plugin);
        g_ptr_array_add(factory->component_classes, component_class);
 end:
        return ret;
@@ -458,3 +500,12 @@ bt_component_factory_register_sink_component_class(
        return add_component_class(factory, name, description, init,
                        BT_COMPONENT_TYPE_SINK);
 }
+
+enum bt_component_factory_status
+bt_component_factory_register_filter_component_class(
+               struct bt_component_factory *factory, const char *name,
+               const char *description, bt_component_init_cb init)
+{
+       return add_component_class(factory, name, description, init,
+                       BT_COMPONENT_TYPE_FILTER);
+}
This page took 0.025796 seconds and 4 git commands to generate.