pipewire/pinos/server/module.c

192 lines
4.5 KiB
C
Raw Normal View History

/* Pinos
* Copyright (C) 2016 Axis Communications <dev-gstreamer@axis.com>
* @author Linus Svensson <linus.svensson@axis.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2016-11-14 12:42:00 +01:00
#include <dlfcn.h>
#include <glib.h>
2016-09-26 12:15:52 +02:00
#include "pinos/client/pinos.h"
2016-11-14 12:42:00 +01:00
#include "pinos/client/utils.h"
#include "pinos/server/module.h"
#define PINOS_SYMBOL_MODULE_INIT "pinos__module_init"
2016-11-10 15:42:14 +01:00
typedef struct
{
2016-11-10 15:42:14 +01:00
PinosModule this;
2016-11-14 12:42:00 +01:00
void *hnd;
2016-11-10 15:42:14 +01:00
} PinosModuleImpl;
2016-11-14 12:42:00 +01:00
static char *
find_module (const char * path, const char *name)
{
char *filename;
GDir *dir;
const gchar *entry;
GError *err = NULL;
filename = g_strconcat (path, G_DIR_SEPARATOR_S, name, ".", G_MODULE_SUFFIX, NULL);
if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
/* found a regular file with name */
return filename;
}
g_clear_pointer (&filename, g_free);
/* now recurse down in subdirectories and look for it there */
dir = g_dir_open (path, 0, &err);
if (dir == NULL) {
pinos_log_warn ("could not open %s: %s", path, err->message);
g_error_free (err);
return NULL;
}
while ((entry = g_dir_read_name (dir))) {
gchar *newpath;
newpath = g_build_filename (path, entry, NULL);
if (g_file_test (newpath, G_FILE_TEST_IS_DIR)) {
filename = find_module (newpath, name);
}
g_free (newpath);
if (filename != NULL)
break;
}
g_dir_close (dir);
return filename;
}
/**
* pinos_module_load:
2016-11-10 15:42:14 +01:00
* @core: a #PinosCore
* @name: name of the module to load
* @args: A string with arguments for the module
2016-11-14 12:42:00 +01:00
* @err: Return location for an error string, or %NULL
*
* Load module with @name.
*
* Returns: A #PinosModule if the module could be loaded, or %NULL on failure.
*/
PinosModule *
2016-11-10 15:42:14 +01:00
pinos_module_load (PinosCore *core,
const gchar *name,
const gchar *args,
2016-11-14 12:42:00 +01:00
char **err)
{
2016-11-10 15:42:14 +01:00
PinosModule *this;
PinosModuleImpl *impl;
2016-11-14 12:42:00 +01:00
void *hnd;
char *filename = NULL;
const char *module_dir;
PinosModuleInitFunc init_func;
2016-11-14 12:42:00 +01:00
module_dir = getenv ("PINOS_MODULE_DIR");
if (module_dir != NULL) {
2016-11-14 12:42:00 +01:00
char **l;
int i, n_paths;
pinos_log_debug ("PINOS_MODULE_DIR set to: %s", module_dir);
2016-11-14 12:42:00 +01:00
l = pinos_split_strv (module_dir, G_SEARCHPATH_SEPARATOR_S, 0, &n_paths);
for (i = 0; l[i] != NULL; i++) {
filename = find_module (l[i], name);
if (filename != NULL)
break;
}
2016-11-14 12:42:00 +01:00
pinos_free_strv (l);
} else {
pinos_log_debug ("moduledir set to: %s", MODULEDIR);
filename = find_module (MODULEDIR, name);
}
2016-11-14 12:42:00 +01:00
if (filename == NULL)
goto not_found;
pinos_log_debug ("trying to load module: %s (%s)", name, filename);
2016-11-14 12:42:00 +01:00
hnd = dlopen (filename, RTLD_NOW | RTLD_LOCAL);
free (filename);
2016-11-14 12:42:00 +01:00
if (hnd == NULL)
goto open_failed;
2016-11-14 12:42:00 +01:00
if ((init_func = dlsym (hnd, PINOS_SYMBOL_MODULE_INIT)) == NULL)
goto no_pinos_module;
2016-11-10 15:42:14 +01:00
impl = calloc (1, sizeof (PinosModuleImpl));
2016-11-14 12:42:00 +01:00
impl->hnd = hnd;
2016-11-10 15:42:14 +01:00
this = &impl->this;
this->name = strdup (name);
this->core = core;
2016-11-14 12:42:00 +01:00
if (!init_func (this, (gchar *) args))
goto init_failed;
2016-11-10 15:42:14 +01:00
2016-11-14 12:42:00 +01:00
pinos_log_debug ("loaded module: %s", this->name);
2016-11-10 15:42:14 +01:00
2016-11-14 12:42:00 +01:00
return this;
2016-11-14 12:42:00 +01:00
not_found:
{
asprintf (err, "No module \"%s\" was found", name);
return NULL;
}
2016-11-14 12:42:00 +01:00
open_failed:
{
asprintf (err, "Failed to open module: %s", dlerror ());
return NULL;
}
no_pinos_module:
{
asprintf (err, "\"%s\" is not a pinos module", name);
dlclose (hnd);
return NULL;
}
init_failed:
{
asprintf (err, "\"%s\" failed to initialize", name);
pinos_module_destroy (this);
return NULL;
}
}
2016-11-14 12:42:00 +01:00
void
pinos_module_destroy (PinosModule *this)
{
PinosModuleImpl *impl = SPA_CONTAINER_OF (this, PinosModuleImpl, this);
pinos_signal_emit (&this->destroy_signal, this);
2016-11-14 12:42:00 +01:00
free (this->name);
dlclose (impl->hnd);
free (impl);
}