[5317] | 1 | //plugincontainer.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2012 |
---|
[5317] | 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
| 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | #include "libroar.h" |
---|
| 37 | |
---|
| 38 | #define MAX_PLUGINS 64 |
---|
| 39 | |
---|
| 40 | struct roar_plugincontainer { |
---|
| 41 | size_t refc; |
---|
| 42 | struct roar_dl_librarypara * default_para; |
---|
| 43 | struct roar_dl_lhandle * handle[MAX_PLUGINS]; |
---|
[5320] | 44 | size_t deprefc[MAX_PLUGINS]; |
---|
| 45 | size_t numhandles; |
---|
[5317] | 46 | }; |
---|
| 47 | |
---|
[5436] | 48 | static int _loader(struct roar_dl_librarypara * lhandle, void * loader_userdata, enum roar_dl_loadercmd cmd, void * argp) { |
---|
| 49 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 50 | return -1; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | static inline void _copy_str(char ** dst, const char * src) { |
---|
| 54 | for (; *src; src++, (*dst)++) **dst = *src; |
---|
| 55 | *((*dst)++) = 0; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | static void * _copy_kv(struct roar_keyval ** copy, struct roar_keyval * src, size_t len) { |
---|
[5438] | 59 | size_t buflen = 0; |
---|
| 60 | size_t i; |
---|
[5436] | 61 | void * ret; |
---|
| 62 | char * p; |
---|
| 63 | struct roar_keyval * c; |
---|
| 64 | |
---|
| 65 | for (i = 0; i < len; i++) { |
---|
| 66 | if ( src[i].key != NULL ) |
---|
[5438] | 67 | buflen += roar_mm_strlen(src[i].key) + 1; |
---|
[5436] | 68 | if ( src[i].value != NULL ) |
---|
[5438] | 69 | buflen += roar_mm_strlen(src[i].value) + 1; |
---|
[5436] | 70 | } |
---|
| 71 | |
---|
[5438] | 72 | c = roar_mm_malloc(len * sizeof(struct roar_keyval)); |
---|
| 73 | if ( c == NULL ) |
---|
| 74 | return NULL; |
---|
| 75 | |
---|
| 76 | memset(c, 0, len * sizeof(struct roar_keyval)); |
---|
| 77 | |
---|
[5436] | 78 | ret = roar_mm_malloc(buflen); |
---|
| 79 | if ( ret == NULL ) |
---|
| 80 | return NULL; |
---|
| 81 | |
---|
| 82 | memset(ret, 0, buflen); |
---|
| 83 | |
---|
[5438] | 84 | p = ret; |
---|
[5436] | 85 | |
---|
| 86 | for (i = 0; i < len; i++) { |
---|
| 87 | if ( src[i].key == NULL ) { |
---|
| 88 | c[i].key = NULL; |
---|
| 89 | } else { |
---|
| 90 | c[i].key = p; |
---|
| 91 | _copy_str(&p, src[i].key); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | if ( src[i].value == NULL ) { |
---|
| 95 | c[i].value = NULL; |
---|
| 96 | } else { |
---|
| 97 | c[i].value = p; |
---|
| 98 | _copy_str(&p, src[i].value); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | *copy = c; |
---|
| 103 | return ret; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | static struct roar_dl_librarypara * _copy_para(struct roar_dl_librarypara * para, struct roar_plugincontainer * cont) { |
---|
| 107 | struct roar_dl_librarypara * ret = NULL; |
---|
| 108 | int err; |
---|
| 109 | |
---|
| 110 | if ( para == NULL || cont == NULL ) { |
---|
| 111 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 112 | return NULL; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | ret = roar_dl_para_new(NULL, para->binargv, para->appname, para->abiversion); |
---|
| 116 | |
---|
| 117 | if ( para->argc && para->argv != NULL ) { |
---|
| 118 | ret->argc = para->argc; |
---|
| 119 | ret->args_store = _copy_kv(&(ret->argv), para->argv, para->argc); |
---|
| 120 | if ( ret->args_store == NULL ) { |
---|
| 121 | err = roar_error; |
---|
| 122 | roar_dl_para_unref(ret); |
---|
| 123 | roar_error = err; |
---|
| 124 | return NULL; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | ret->notifycore = para->notifycore; |
---|
| 129 | ret->container = cont; |
---|
| 130 | ret->loader = _loader; |
---|
| 131 | ret->loader_userdata = NULL; |
---|
| 132 | |
---|
| 133 | return ret; |
---|
| 134 | } |
---|
| 135 | |
---|
[5317] | 136 | static struct roar_plugincontainer * _new_init(void) { |
---|
| 137 | struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer)); |
---|
| 138 | if ( ret == NULL ) |
---|
| 139 | return NULL; |
---|
| 140 | |
---|
| 141 | memset(ret, 0, sizeof(struct roar_plugincontainer)); |
---|
| 142 | |
---|
| 143 | ret->refc = 1; |
---|
| 144 | |
---|
| 145 | return ret; |
---|
| 146 | } |
---|
| 147 | |
---|
[5436] | 148 | struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) { |
---|
[5317] | 149 | struct roar_plugincontainer * ret = _new_init(); |
---|
| 150 | int err; |
---|
| 151 | |
---|
| 152 | if ( ret == NULL ) |
---|
| 153 | return NULL; |
---|
| 154 | |
---|
| 155 | if ( default_para != NULL ) { |
---|
[5436] | 156 | ret->default_para = _copy_para(default_para, ret); |
---|
| 157 | if ( ret->default_para == NULL ) { |
---|
[5317] | 158 | err = roar_error; |
---|
| 159 | roar_plugincontainer_unref(ret); |
---|
| 160 | roar_err_set(err); |
---|
| 161 | return NULL; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | return ret; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) { |
---|
| 169 | struct roar_plugincontainer * ret; |
---|
| 170 | struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion); |
---|
| 171 | int err; |
---|
| 172 | |
---|
| 173 | ret = roar_plugincontainer_new(para); |
---|
| 174 | err = roar_error; |
---|
| 175 | |
---|
| 176 | roar_dl_para_unref(para); |
---|
| 177 | |
---|
| 178 | roar_err_set(err); |
---|
| 179 | return ret; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | int roar_plugincontainer_ref(struct roar_plugincontainer * cont) { |
---|
| 183 | if ( cont == NULL ) { |
---|
[5438] | 184 | ROAR_DBG("roar_plugincontainer_ref(cont=%p) = -1 // error=FAULT", cont); |
---|
[5317] | 185 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 186 | return -1; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | cont->refc++; |
---|
| 190 | |
---|
[5438] | 191 | ROAR_DBG("roar_plugincontainer_ref(cont=%p) = 0", cont); |
---|
[5317] | 192 | return 0; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | int roar_plugincontainer_unref(struct roar_plugincontainer * cont) { |
---|
| 196 | size_t i; |
---|
| 197 | |
---|
| 198 | if ( cont == NULL ) { |
---|
[5438] | 199 | ROAR_DBG("roar_plugincontainer_unref(cont=%p) = -1 // error=FAULT", cont); |
---|
[5317] | 200 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 201 | return -1; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | cont->refc--; |
---|
| 205 | |
---|
[5438] | 206 | if ( cont->refc ) { |
---|
| 207 | ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont); |
---|
[5317] | 208 | return 0; |
---|
[5438] | 209 | } |
---|
[5317] | 210 | |
---|
[5320] | 211 | while (cont->numhandles) { |
---|
| 212 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 213 | if ( cont->handle[i] == NULL ) |
---|
| 214 | continue; |
---|
[5317] | 215 | |
---|
[5320] | 216 | // skip plugins in use by others. We will unload them in a later loop. |
---|
| 217 | if ( cont->deprefc[i] ) |
---|
| 218 | continue; |
---|
| 219 | |
---|
| 220 | roar_dl_close(cont->handle[i]); |
---|
[5353] | 221 | cont->handle[i] = NULL; |
---|
| 222 | cont->numhandles--; |
---|
[5320] | 223 | } |
---|
[5317] | 224 | } |
---|
| 225 | |
---|
| 226 | if ( cont->default_para != NULL ) |
---|
| 227 | roar_dl_para_unref(cont->default_para); |
---|
| 228 | |
---|
| 229 | roar_mm_free(cont); |
---|
| 230 | |
---|
[5438] | 231 | ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont); |
---|
[5317] | 232 | return 0; |
---|
| 233 | } |
---|
| 234 | |
---|
[5487] | 235 | struct roar_dl_lhandle * roar_plugincontainer_get_lhandle_by_name (struct roar_plugincontainer * cont, |
---|
| 236 | const char * name) { |
---|
| 237 | const struct roar_dl_libraryname * libname; |
---|
| 238 | size_t i; |
---|
| 239 | |
---|
| 240 | if ( cont == NULL || name == NULL ) { |
---|
| 241 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 242 | return NULL; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 246 | if ( cont->handle[i] == NULL ) |
---|
| 247 | continue; |
---|
| 248 | libname = roar_dl_getlibname(cont->handle[i]); |
---|
| 249 | if ( libname == NULL ) |
---|
| 250 | continue; |
---|
| 251 | |
---|
| 252 | if ( !strcmp(libname->name, name) ) { |
---|
| 253 | if ( roar_dl_ref(cont->handle[i]) == -1 ) |
---|
| 254 | return NULL; |
---|
| 255 | |
---|
| 256 | return cont->handle[i]; |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 261 | return NULL; |
---|
| 262 | } |
---|
| 263 | |
---|
[5317] | 264 | // plugin loading and unloading: |
---|
[5320] | 265 | int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) { |
---|
[5432] | 266 | struct roar_dl_lhandle * ret = roar_plugincontainer_load_lhandle(cont, name, ROAR_DL_FLAG_PLUGIN, 1, para); |
---|
[5429] | 267 | |
---|
| 268 | if ( ret == NULL ) |
---|
| 269 | return -1; |
---|
| 270 | |
---|
| 271 | roar_dl_unref(ret); |
---|
| 272 | |
---|
| 273 | return 0; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | struct roar_dl_lhandle * roar_plugincontainer_load_lhandle (struct roar_plugincontainer * cont, |
---|
| 277 | const char * name, |
---|
[5432] | 278 | int flags, |
---|
| 279 | int ra_init, |
---|
[5429] | 280 | struct roar_dl_librarypara * para) { |
---|
[5320] | 281 | ssize_t idx = -1; |
---|
| 282 | size_t i; |
---|
[5436] | 283 | int err; |
---|
[5320] | 284 | |
---|
| 285 | if ( cont == NULL || name == NULL ) { |
---|
| 286 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[5429] | 287 | return NULL; |
---|
[5320] | 288 | } |
---|
| 289 | |
---|
[5436] | 290 | if ( para == NULL ) { |
---|
[5320] | 291 | para = cont->default_para; |
---|
[5436] | 292 | roar_dl_para_ref(para); |
---|
| 293 | } else { |
---|
| 294 | para = _copy_para(para, cont); |
---|
| 295 | } |
---|
[5320] | 296 | |
---|
| 297 | |
---|
| 298 | // search for a free index. |
---|
| 299 | if ( cont->numhandles == MAX_PLUGINS ) { |
---|
| 300 | // return early if we are full. |
---|
[5436] | 301 | roar_dl_para_unref(para); |
---|
[5320] | 302 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
[5429] | 303 | return NULL; |
---|
[5320] | 304 | } |
---|
| 305 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 306 | if ( cont->handle[i] == NULL ) { |
---|
| 307 | idx = i; |
---|
| 308 | break; |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | if ( idx == -1 ) { |
---|
[5436] | 313 | roar_dl_para_unref(para); |
---|
[5320] | 314 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
[5429] | 315 | return NULL; |
---|
[5320] | 316 | } |
---|
| 317 | |
---|
[5432] | 318 | cont->handle[idx] = roar_dl_open(name, flags, ra_init, para); |
---|
[5436] | 319 | if ( cont->handle[idx] == NULL ) { |
---|
| 320 | err = roar_error; |
---|
| 321 | roar_dl_para_unref(para); |
---|
| 322 | roar_error = err; |
---|
[5429] | 323 | return NULL; |
---|
[5436] | 324 | } |
---|
[5423] | 325 | |
---|
| 326 | cont->deprefc[idx] = 0; |
---|
| 327 | cont->numhandles++; |
---|
| 328 | |
---|
[5436] | 329 | roar_dl_para_unref(para); |
---|
[5429] | 330 | roar_dl_ref(cont->handle[idx]); |
---|
| 331 | return cont->handle[idx]; |
---|
[5320] | 332 | } |
---|
| 333 | |
---|
[5317] | 334 | int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) { |
---|
[5487] | 335 | struct roar_dl_lhandle * lhandle; |
---|
[5429] | 336 | |
---|
| 337 | if ( cont == NULL || name == NULL ) { |
---|
| 338 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 339 | return -1; |
---|
| 340 | } |
---|
| 341 | |
---|
[5487] | 342 | lhandle = roar_plugincontainer_get_lhandle_by_name(cont, name); |
---|
| 343 | if ( lhandle == NULL ) |
---|
| 344 | return -1; |
---|
[5429] | 345 | |
---|
[5487] | 346 | roar_dl_unref(lhandle); // we can do this early here as the handle stay valid in the container's array. |
---|
[5429] | 347 | |
---|
[5487] | 348 | return roar_plugincontainer_unload_lhandle(cont, lhandle); |
---|
[5317] | 349 | } |
---|
| 350 | |
---|
| 351 | int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) { |
---|
| 352 | size_t i; |
---|
| 353 | |
---|
| 354 | if ( cont == NULL ) { |
---|
| 355 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 356 | return -1; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | if ( lhandle == NULL ) { |
---|
| 360 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 361 | return -1; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 365 | if ( cont->handle[i] == lhandle ) { |
---|
[5320] | 366 | if ( cont->deprefc[i] ) { |
---|
| 367 | // still in use. |
---|
| 368 | roar_err_set(ROAR_ERROR_BUSY); |
---|
| 369 | return -1; |
---|
| 370 | } |
---|
[5317] | 371 | roar_dl_close(cont->handle[i]); |
---|
| 372 | cont->handle[i] = NULL; |
---|
[5320] | 373 | cont->numhandles--; |
---|
[5317] | 374 | return 0; |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 379 | return -1; |
---|
| 380 | } |
---|
| 381 | |
---|
[5432] | 382 | int roar_plugincontainer_ra_init (struct roar_plugincontainer * cont) { |
---|
| 383 | size_t i; |
---|
| 384 | |
---|
| 385 | if ( cont == NULL ) { |
---|
| 386 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 387 | return -1; |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 391 | if ( cont->handle[i] == NULL ) |
---|
| 392 | continue; |
---|
| 393 | roar_dl_ra_init(cont->handle[i], NULL, cont->default_para); |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | return 0; |
---|
| 397 | } |
---|
| 398 | |
---|
[5317] | 399 | // appsched: |
---|
| 400 | int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) { |
---|
| 401 | size_t i; |
---|
[5441] | 402 | int ret = -1; |
---|
| 403 | int rv; |
---|
[5317] | 404 | |
---|
| 405 | if ( cont == NULL ) { |
---|
| 406 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 407 | return -1; |
---|
| 408 | } |
---|
| 409 | |
---|
[5441] | 410 | if ( cont->numhandles == 1 ) { |
---|
| 411 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 412 | if ( cont->handle[i] == NULL ) |
---|
| 413 | continue; |
---|
| 414 | return roar_dl_appsched_trigger(cont->handle[i], trigger); |
---|
| 415 | } |
---|
| 416 | roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL); |
---|
| 417 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 418 | return -1; |
---|
| 419 | } |
---|
| 420 | |
---|
[5317] | 421 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 422 | if ( cont->handle[i] == NULL ) |
---|
| 423 | continue; |
---|
| 424 | |
---|
[5441] | 425 | rv = roar_dl_appsched_trigger(cont->handle[i], trigger); |
---|
| 426 | if ( rv == 0 ) |
---|
| 427 | ret = 0; |
---|
[5317] | 428 | } |
---|
| 429 | |
---|
[5441] | 430 | return ret; |
---|
[5317] | 431 | } |
---|
| 432 | |
---|
| 433 | //ll |
---|