//plugincontainer.c: /* * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2012 * * This file is part of libroar a part of RoarAudio, * a cross-platform sound system for both, home and professional use. * See README for details. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 * as published by the Free Software Foundation. * * libroar 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; see the file COPYING. If not, write to * the Free Software Foundation, 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * * NOTE for everyone want's to change something and send patches: * read README and HACKING! There a addition information on * the license of this document you need to read before you send * any patches. * * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc * or libpulse*: * The libs libroaresd, libroararts and libroarpulse link this lib * and are therefore GPL. Because of this it may be illigal to use * them with any software that uses libesd, libartsc or libpulse*. */ #include "libroar.h" #ifdef ROAR_HAVE_H_DIRENT #include #endif #define MAX_PLUGINS 64 struct roar_plugincontainer { size_t refc; struct roar_dl_librarypara * default_para; struct roar_dl_lhandle * handle[MAX_PLUGINS]; size_t deprefc[MAX_PLUGINS]; size_t numhandles; }; static struct roar_plugincontainer * _new_init(void) { struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer)); if ( ret == NULL ) return NULL; memset(ret, 0, sizeof(struct roar_plugincontainer)); ret->refc = 1; return ret; } static struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) { struct roar_plugincontainer * ret = _new_init(); int err; if ( ret == NULL ) return NULL; if ( default_para != NULL ) { if ( roar_dl_para_ref(default_para) == -1 ) { err = roar_error; roar_plugincontainer_unref(ret); roar_err_set(err); return NULL; } ret->default_para = default_para; } return ret; } struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) { struct roar_plugincontainer * ret; struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion); int err; ret = roar_plugincontainer_new(para); err = roar_error; roar_dl_para_unref(para); roar_err_set(err); return ret; } int roar_plugincontainer_ref(struct roar_plugincontainer * cont) { if ( cont == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } cont->refc++; return 0; } int roar_plugincontainer_unref(struct roar_plugincontainer * cont) { size_t i; if ( cont == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } cont->refc--; if ( cont->refc ) return 0; while (cont->numhandles) { for (i = 0; i < MAX_PLUGINS; i++) { if ( cont->handle[i] == NULL ) continue; // skip plugins in use by others. We will unload them in a later loop. if ( cont->deprefc[i] ) continue; roar_dl_close(cont->handle[i]); cont->handle[i] = NULL; cont->numhandles--; } } if ( cont->default_para != NULL ) roar_dl_para_unref(cont->default_para); roar_mm_free(cont); return 0; } // plugin loading and unloading: #ifdef ROAR_HAVE_H_DIRENT // pvn = prefix, host vendor, host name static struct roar_dl_lhandle * _load_from_path_pvn(const char * name, struct roar_dl_librarypara * para, const char * prefix, const char * hostvendor, const char * hostname) { struct roar_dl_lhandle * ret = NULL; int i, j; char path[1024]; const char * path_format[] = {"%s/%s/%s", "%s/%s/universal", "%s/universal/universal"}; DIR * dir; struct dirent * dirent; char file[1024]; const char * file_format[] = {"%s/%s/%s", "%s/%s/%s.so", "%s/%s/lib%s.so"}; // cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, para); //#vars: $PREFIX_PLUGINS, $hostvendor, $hostname, $name //#search order: $PREFIX_PLUGINS/{$hostvendor/{$hostname,universal},universal/universal}/*/{,lib}$name.so for (i = 0; i < 3; i++) { snprintf(path, sizeof(path), path_format[i], prefix, hostvendor, hostname); dir = opendir(path); if ( dir == NULL ) continue; while ((dirent = readdir(dir)) != NULL) { if ( !strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..") ) continue; for (j = 0; j < 3; j++) { snprintf(file, sizeof(file), file_format[j], path, dirent->d_name, name); ret = roar_dl_open(file, ROAR_DL_FLAG_DEFAULTS, 1, para); if ( ret != NULL ) { closedir(dir); return ret; } } } closedir(dir); } return NULL; } static struct roar_dl_lhandle * _load_from_path(const char * name, struct roar_dl_librarypara * para) { struct roar_dl_lhandle * ret = NULL; char * host = NULL; char * hostvendor_buffer = NULL; char * hostvendor = NULL; char * hostname = NULL; char * c, * d; if ( para->appname != NULL && para->appname[0] != 0 ) { host = roar_mm_strdup(para->appname); // if we are out of memory we will likely not pass the rest, so just return in error. if ( host == NULL ) return NULL; hostname = host; c = strstr(host, "<"); if ( c != NULL ) { while (c[-1] == ' ') c--; *c = 0; c++; while (*c == ' ' || *c == '<') c++; if ( *c ) { d = strstr(c, ">"); if ( d != NULL ) *d = 0; d = strstr(c, "/"); if ( d != NULL ) { *d = '-'; hostvendor = c; } else { hostvendor_buffer = roar_mm_malloc(roar_mm_strlen(c) + 1 /* tailing \0 */ + 6 /* "unreg-" */); // see above if ( hostvendor_buffer == NULL ) { roar_mm_free(host); return NULL; } strcpy(hostvendor_buffer, "unreg-"); strcat(hostvendor_buffer, c); hostvendor = hostvendor_buffer; } } } } if ( hostvendor == NULL ) hostvendor = "universal"; if ( hostname == NULL ) hostname = "universal"; ret = _load_from_path_pvn(name, para, ROAR_PREFIX_PLUGINS, hostvendor, hostname); if ( host != NULL ) roar_mm_free(host); if ( hostvendor_buffer != NULL ) roar_mm_free(hostvendor_buffer); return ret; } #endif int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) { ssize_t idx = -1; size_t i; if ( cont == NULL || name == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } if ( para == NULL ) para = cont->default_para; // we do not need to _ref(para) here. This will be done by the roardl open function. // search for a free index. if ( cont->numhandles == MAX_PLUGINS ) { // return early if we are full. roar_err_set(ROAR_ERROR_NOSPC); return -1; } for (i = 0; i < MAX_PLUGINS; i++) { if ( cont->handle[i] == NULL ) { idx = i; break; } } if ( idx == -1 ) { roar_err_set(ROAR_ERROR_NOSPC); return -1; } if ( strstr(name, "/") != NULL ) { cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, para); } else { #ifdef ROAR_HAVE_H_DIRENT cont->handle[idx] = _load_from_path(name, para); #else roar_err_set(ROAR_ERROR_NOSYS); return -1; #endif } if ( cont->handle[idx] == NULL ) return -1; cont->deprefc[idx] = 0; cont->numhandles++; return 0; } #if 0 int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) { // get b name, then call roar_plugincontainer_unload_lhandle(). roar_err_set(ROAR_ERROR_NOSYS); return -1; } int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) { size_t i; if ( cont == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } if ( lhandle == NULL ) { roar_err_set(ROAR_ERROR_INVAL); return -1; } for (i = 0; i < MAX_PLUGINS; i++) { if ( cont->handle[i] == lhandle ) { if ( cont->deprefc[i] ) { // still in use. roar_err_set(ROAR_ERROR_BUSY); return -1; } roar_dl_close(cont->handle[i]); cont->handle[i] = NULL; cont->numhandles--; return 0; } } roar_err_set(ROAR_ERROR_NOENT); return -1; } #endif // appsched: int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) { size_t i; if ( cont == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } for (i = 0; i < MAX_PLUGINS; i++) { if ( cont->handle[i] == NULL ) continue; roar_dl_appsched_trigger(cont->handle[i], trigger); } return 0; } //ll