[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 | |
---|
[5504] | 40 | #define OPT_AUTOAPPSCHED 0x0001 |
---|
| 41 | |
---|
[5317] | 42 | struct roar_plugincontainer { |
---|
| 43 | size_t refc; |
---|
[5488] | 44 | const struct roar_plugincontainer_callbacks * callbacks; |
---|
| 45 | void * userdata; |
---|
[5504] | 46 | unsigned int options; |
---|
[5317] | 47 | struct roar_dl_librarypara * default_para; |
---|
| 48 | struct roar_dl_lhandle * handle[MAX_PLUGINS]; |
---|
[5320] | 49 | size_t deprefc[MAX_PLUGINS]; |
---|
[5488] | 50 | void * context[MAX_PLUGINS]; |
---|
[5320] | 51 | size_t numhandles; |
---|
[5317] | 52 | }; |
---|
| 53 | |
---|
[5488] | 54 | static inline void _unload(struct roar_plugincontainer * cont, size_t index) { |
---|
| 55 | int err = roar_error; |
---|
| 56 | |
---|
[5504] | 57 | if ( cont->options & OPT_AUTOAPPSCHED ) |
---|
| 58 | roar_dl_appsched_trigger(cont->handle[index], ROAR_DL_APPSCHED_FREE); |
---|
| 59 | |
---|
[5488] | 60 | if ( cont->callbacks != NULL && cont->callbacks->preunload != NULL ) |
---|
| 61 | cont->callbacks->preunload(cont, &(cont->context[index]), cont->handle[index]); |
---|
| 62 | |
---|
| 63 | roar_err_set(err); |
---|
| 64 | roar_dl_unref(cont->handle[index]); |
---|
| 65 | err = roar_error; |
---|
| 66 | cont->handle[index] = NULL; |
---|
| 67 | cont->numhandles--; |
---|
| 68 | |
---|
| 69 | if ( cont->callbacks != NULL && cont->callbacks->postunload != NULL ) |
---|
| 70 | cont->callbacks->postunload(cont, &(cont->context[index])); |
---|
| 71 | |
---|
| 72 | if ( cont->context[index] != NULL ) |
---|
| 73 | if ( cont->callbacks != NULL && cont->callbacks->freecontext != NULL ) |
---|
| 74 | cont->callbacks->freecontext(cont, &(cont->context[index])); |
---|
| 75 | |
---|
| 76 | if ( cont->context[index] != NULL ) { |
---|
| 77 | roar_mm_free(cont->context[index]); |
---|
| 78 | cont->context[index] = NULL; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | roar_err_set(err); |
---|
| 82 | } |
---|
| 83 | |
---|
[5436] | 84 | static int _loader(struct roar_dl_librarypara * lhandle, void * loader_userdata, enum roar_dl_loadercmd cmd, void * argp) { |
---|
| 85 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 86 | return -1; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | static inline void _copy_str(char ** dst, const char * src) { |
---|
| 90 | for (; *src; src++, (*dst)++) **dst = *src; |
---|
| 91 | *((*dst)++) = 0; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | static void * _copy_kv(struct roar_keyval ** copy, struct roar_keyval * src, size_t len) { |
---|
[5438] | 95 | size_t buflen = 0; |
---|
| 96 | size_t i; |
---|
[5436] | 97 | void * ret; |
---|
| 98 | char * p; |
---|
| 99 | struct roar_keyval * c; |
---|
| 100 | |
---|
| 101 | for (i = 0; i < len; i++) { |
---|
| 102 | if ( src[i].key != NULL ) |
---|
[5438] | 103 | buflen += roar_mm_strlen(src[i].key) + 1; |
---|
[5436] | 104 | if ( src[i].value != NULL ) |
---|
[5438] | 105 | buflen += roar_mm_strlen(src[i].value) + 1; |
---|
[5436] | 106 | } |
---|
| 107 | |
---|
[5438] | 108 | c = roar_mm_malloc(len * sizeof(struct roar_keyval)); |
---|
| 109 | if ( c == NULL ) |
---|
| 110 | return NULL; |
---|
| 111 | |
---|
| 112 | memset(c, 0, len * sizeof(struct roar_keyval)); |
---|
| 113 | |
---|
[5436] | 114 | ret = roar_mm_malloc(buflen); |
---|
| 115 | if ( ret == NULL ) |
---|
| 116 | return NULL; |
---|
| 117 | |
---|
| 118 | memset(ret, 0, buflen); |
---|
| 119 | |
---|
[5438] | 120 | p = ret; |
---|
[5436] | 121 | |
---|
| 122 | for (i = 0; i < len; i++) { |
---|
| 123 | if ( src[i].key == NULL ) { |
---|
| 124 | c[i].key = NULL; |
---|
| 125 | } else { |
---|
| 126 | c[i].key = p; |
---|
| 127 | _copy_str(&p, src[i].key); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | if ( src[i].value == NULL ) { |
---|
| 131 | c[i].value = NULL; |
---|
| 132 | } else { |
---|
| 133 | c[i].value = p; |
---|
| 134 | _copy_str(&p, src[i].value); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | *copy = c; |
---|
| 139 | return ret; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | static struct roar_dl_librarypara * _copy_para(struct roar_dl_librarypara * para, struct roar_plugincontainer * cont) { |
---|
| 143 | struct roar_dl_librarypara * ret = NULL; |
---|
| 144 | int err; |
---|
| 145 | |
---|
| 146 | if ( para == NULL || cont == NULL ) { |
---|
| 147 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 148 | return NULL; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | ret = roar_dl_para_new(NULL, para->binargv, para->appname, para->abiversion); |
---|
| 152 | |
---|
| 153 | if ( para->argc && para->argv != NULL ) { |
---|
| 154 | ret->argc = para->argc; |
---|
| 155 | ret->args_store = _copy_kv(&(ret->argv), para->argv, para->argc); |
---|
| 156 | if ( ret->args_store == NULL ) { |
---|
| 157 | err = roar_error; |
---|
| 158 | roar_dl_para_unref(ret); |
---|
| 159 | roar_error = err; |
---|
| 160 | return NULL; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | ret->notifycore = para->notifycore; |
---|
| 165 | ret->container = cont; |
---|
| 166 | ret->loader = _loader; |
---|
| 167 | ret->loader_userdata = NULL; |
---|
| 168 | |
---|
| 169 | return ret; |
---|
| 170 | } |
---|
| 171 | |
---|
[5317] | 172 | static struct roar_plugincontainer * _new_init(void) { |
---|
| 173 | struct roar_plugincontainer * ret = roar_mm_malloc(sizeof(struct roar_plugincontainer)); |
---|
| 174 | if ( ret == NULL ) |
---|
| 175 | return NULL; |
---|
| 176 | |
---|
| 177 | memset(ret, 0, sizeof(struct roar_plugincontainer)); |
---|
| 178 | |
---|
| 179 | ret->refc = 1; |
---|
| 180 | |
---|
| 181 | return ret; |
---|
| 182 | } |
---|
| 183 | |
---|
[5436] | 184 | struct roar_plugincontainer * roar_plugincontainer_new(struct roar_dl_librarypara * default_para) { |
---|
[5317] | 185 | struct roar_plugincontainer * ret = _new_init(); |
---|
| 186 | int err; |
---|
| 187 | |
---|
| 188 | if ( ret == NULL ) |
---|
| 189 | return NULL; |
---|
| 190 | |
---|
| 191 | if ( default_para != NULL ) { |
---|
[5436] | 192 | ret->default_para = _copy_para(default_para, ret); |
---|
| 193 | if ( ret->default_para == NULL ) { |
---|
[5317] | 194 | err = roar_error; |
---|
| 195 | roar_plugincontainer_unref(ret); |
---|
| 196 | roar_err_set(err); |
---|
| 197 | return NULL; |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | return ret; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | struct roar_plugincontainer * roar_plugincontainer_new_simple(const char * appname, const char * abiversion) { |
---|
| 205 | struct roar_plugincontainer * ret; |
---|
| 206 | struct roar_dl_librarypara * para = roar_dl_para_new(NULL, NULL, appname, abiversion); |
---|
| 207 | int err; |
---|
| 208 | |
---|
| 209 | ret = roar_plugincontainer_new(para); |
---|
| 210 | err = roar_error; |
---|
| 211 | |
---|
| 212 | roar_dl_para_unref(para); |
---|
| 213 | |
---|
| 214 | roar_err_set(err); |
---|
| 215 | return ret; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | int roar_plugincontainer_ref(struct roar_plugincontainer * cont) { |
---|
| 219 | if ( cont == NULL ) { |
---|
[5438] | 220 | ROAR_DBG("roar_plugincontainer_ref(cont=%p) = -1 // error=FAULT", cont); |
---|
[5317] | 221 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 222 | return -1; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | cont->refc++; |
---|
| 226 | |
---|
[5438] | 227 | ROAR_DBG("roar_plugincontainer_ref(cont=%p) = 0", cont); |
---|
[5317] | 228 | return 0; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | int roar_plugincontainer_unref(struct roar_plugincontainer * cont) { |
---|
| 232 | size_t i; |
---|
| 233 | |
---|
| 234 | if ( cont == NULL ) { |
---|
[5438] | 235 | ROAR_DBG("roar_plugincontainer_unref(cont=%p) = -1 // error=FAULT", cont); |
---|
[5317] | 236 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 237 | return -1; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | cont->refc--; |
---|
| 241 | |
---|
[5438] | 242 | if ( cont->refc ) { |
---|
| 243 | ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont); |
---|
[5317] | 244 | return 0; |
---|
[5438] | 245 | } |
---|
[5317] | 246 | |
---|
[5488] | 247 | if ( cont->callbacks != NULL && cont->callbacks->prefree != NULL ) |
---|
| 248 | cont->callbacks->prefree(cont, &(cont->userdata)); |
---|
| 249 | |
---|
[5320] | 250 | while (cont->numhandles) { |
---|
| 251 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 252 | if ( cont->handle[i] == NULL ) |
---|
| 253 | continue; |
---|
[5317] | 254 | |
---|
[5320] | 255 | // skip plugins in use by others. We will unload them in a later loop. |
---|
| 256 | if ( cont->deprefc[i] ) |
---|
| 257 | continue; |
---|
| 258 | |
---|
[5488] | 259 | _unload(cont, i); |
---|
[5320] | 260 | } |
---|
[5317] | 261 | } |
---|
| 262 | |
---|
[5488] | 263 | // try to free user data... |
---|
| 264 | if ( cont->userdata != NULL ) { |
---|
| 265 | if ( cont->callbacks != NULL && cont->callbacks->freeuserdata != NULL ) { |
---|
| 266 | cont->callbacks->freeuserdata(cont, &(cont->userdata)); |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | // if still not freed by caller, free it using memmgr. |
---|
| 271 | if ( cont->userdata != NULL ) |
---|
| 272 | roar_mm_free(cont->userdata); |
---|
| 273 | |
---|
[5317] | 274 | if ( cont->default_para != NULL ) |
---|
| 275 | roar_dl_para_unref(cont->default_para); |
---|
| 276 | |
---|
| 277 | roar_mm_free(cont); |
---|
| 278 | |
---|
[5438] | 279 | ROAR_DBG("roar_plugincontainer_unref(cont=%p) = 0", cont); |
---|
[5317] | 280 | return 0; |
---|
| 281 | } |
---|
| 282 | |
---|
[5504] | 283 | int roar_plugincontainer_set_autoappsched(struct roar_plugincontainer * cont, int val) { |
---|
| 284 | if ( cont == NULL ) { |
---|
| 285 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 286 | return -1; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | if ( val ) { |
---|
| 290 | cont->options |= OPT_AUTOAPPSCHED; |
---|
| 291 | return 0; |
---|
| 292 | } else { |
---|
| 293 | if ( cont->options & OPT_AUTOAPPSCHED ) { |
---|
| 294 | roar_err_set(ROAR_ERROR_BUSY); |
---|
| 295 | return -1; |
---|
| 296 | } else { |
---|
| 297 | return 0; |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | |
---|
[5488] | 302 | int roar_plugincontainer_set_callbacks(struct roar_plugincontainer * cont, |
---|
| 303 | const struct roar_plugincontainer_callbacks * callbacks) { |
---|
| 304 | if ( cont == NULL ) { |
---|
| 305 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 306 | return -1; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | cont->callbacks = callbacks; |
---|
| 310 | |
---|
| 311 | return 0; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | int roar_plugincontainer_set_userdata(struct roar_plugincontainer * cont, void * userdata) { |
---|
| 315 | if ( cont == NULL ) { |
---|
| 316 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 317 | return -1; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | cont->userdata = userdata; |
---|
| 321 | |
---|
| 322 | return 0; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | void * roar_plugincontainer_get_userdata(struct roar_plugincontainer * cont) { |
---|
| 326 | if ( cont == NULL ) { |
---|
| 327 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 328 | return NULL; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | if ( cont->userdata == NULL ) { |
---|
| 332 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 333 | return NULL; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | return cont->userdata; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | struct roar_plugincontainer_plugininfo roar_plugincontainer_get_info_by_name (struct roar_plugincontainer * cont, |
---|
| 340 | const char * name) { |
---|
| 341 | struct roar_plugincontainer_plugininfo ret; |
---|
[5487] | 342 | const struct roar_dl_libraryname * libname; |
---|
| 343 | size_t i; |
---|
| 344 | |
---|
[5488] | 345 | memset(&ret, 0, sizeof(ret)); |
---|
| 346 | |
---|
[5487] | 347 | if ( cont == NULL || name == NULL ) { |
---|
| 348 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[5488] | 349 | return ret; |
---|
[5487] | 350 | } |
---|
| 351 | |
---|
| 352 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 353 | if ( cont->handle[i] == NULL ) |
---|
| 354 | continue; |
---|
| 355 | libname = roar_dl_getlibname(cont->handle[i]); |
---|
| 356 | if ( libname == NULL ) |
---|
| 357 | continue; |
---|
| 358 | |
---|
| 359 | if ( !strcmp(libname->name, name) ) { |
---|
| 360 | if ( roar_dl_ref(cont->handle[i]) == -1 ) |
---|
[5488] | 361 | return ret; |
---|
[5487] | 362 | |
---|
[5488] | 363 | ret.libname = libname->name; |
---|
| 364 | ret.handle = cont->handle[i]; |
---|
| 365 | ret.rdepends = cont->deprefc[i]; |
---|
| 366 | ret.context = &(cont->context[i]); |
---|
| 367 | return ret; |
---|
[5487] | 368 | } |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | roar_err_set(ROAR_ERROR_NOENT); |
---|
[5488] | 372 | return ret; |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | struct roar_dl_lhandle * roar_plugincontainer_get_lhandle_by_name (struct roar_plugincontainer * cont, |
---|
| 376 | const char * name) { |
---|
| 377 | struct roar_plugincontainer_plugininfo info = roar_plugincontainer_get_info_by_name(cont, name); |
---|
| 378 | |
---|
| 379 | if ( info.libname == NULL ) |
---|
| 380 | return NULL; |
---|
| 381 | |
---|
| 382 | return info.handle; |
---|
[5487] | 383 | } |
---|
| 384 | |
---|
[5317] | 385 | // plugin loading and unloading: |
---|
[5320] | 386 | int roar_plugincontainer_load(struct roar_plugincontainer * cont, const char * name, struct roar_dl_librarypara * para) { |
---|
[5432] | 387 | struct roar_dl_lhandle * ret = roar_plugincontainer_load_lhandle(cont, name, ROAR_DL_FLAG_PLUGIN, 1, para); |
---|
[5429] | 388 | |
---|
| 389 | if ( ret == NULL ) |
---|
| 390 | return -1; |
---|
| 391 | |
---|
| 392 | roar_dl_unref(ret); |
---|
| 393 | |
---|
| 394 | return 0; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | struct roar_dl_lhandle * roar_plugincontainer_load_lhandle (struct roar_plugincontainer * cont, |
---|
| 398 | const char * name, |
---|
[5432] | 399 | int flags, |
---|
| 400 | int ra_init, |
---|
[5429] | 401 | struct roar_dl_librarypara * para) { |
---|
[5320] | 402 | ssize_t idx = -1; |
---|
| 403 | size_t i; |
---|
[5436] | 404 | int err; |
---|
[5320] | 405 | |
---|
| 406 | if ( cont == NULL || name == NULL ) { |
---|
| 407 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[5429] | 408 | return NULL; |
---|
[5320] | 409 | } |
---|
| 410 | |
---|
[5436] | 411 | if ( para == NULL ) { |
---|
[5320] | 412 | para = cont->default_para; |
---|
[5436] | 413 | roar_dl_para_ref(para); |
---|
| 414 | } else { |
---|
| 415 | para = _copy_para(para, cont); |
---|
| 416 | } |
---|
[5320] | 417 | |
---|
| 418 | |
---|
| 419 | // search for a free index. |
---|
| 420 | if ( cont->numhandles == MAX_PLUGINS ) { |
---|
| 421 | // return early if we are full. |
---|
[5436] | 422 | roar_dl_para_unref(para); |
---|
[5320] | 423 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
[5429] | 424 | return NULL; |
---|
[5320] | 425 | } |
---|
| 426 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 427 | if ( cont->handle[i] == NULL ) { |
---|
| 428 | idx = i; |
---|
| 429 | break; |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | if ( idx == -1 ) { |
---|
[5436] | 434 | roar_dl_para_unref(para); |
---|
[5320] | 435 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
[5429] | 436 | return NULL; |
---|
[5320] | 437 | } |
---|
| 438 | |
---|
[5488] | 439 | cont->context[idx] = NULL; // clear context. |
---|
| 440 | cont->deprefc[idx] = 0; |
---|
| 441 | |
---|
| 442 | if ( cont->callbacks != NULL && cont->callbacks->preload != NULL ) |
---|
| 443 | cont->callbacks->preload(cont, &(cont->context[idx]), name, flags, para); |
---|
| 444 | |
---|
| 445 | cont->handle[idx] = roar_dl_open(name, flags, 0, para); |
---|
[5436] | 446 | if ( cont->handle[idx] == NULL ) { |
---|
| 447 | err = roar_error; |
---|
| 448 | roar_dl_para_unref(para); |
---|
| 449 | roar_error = err; |
---|
[5429] | 450 | return NULL; |
---|
[5436] | 451 | } |
---|
[5423] | 452 | |
---|
| 453 | cont->numhandles++; |
---|
| 454 | |
---|
[5488] | 455 | if ( cont->callbacks != NULL && cont->callbacks->postload != NULL ) |
---|
| 456 | cont->callbacks->postload(cont, &(cont->context[idx]), cont->handle[idx], name, flags, para); |
---|
| 457 | |
---|
| 458 | if ( ra_init ) { |
---|
| 459 | if ( cont->callbacks != NULL && cont->callbacks->prera_init != NULL ) |
---|
| 460 | cont->callbacks->prera_init(cont, &(cont->context[idx]), cont->handle[idx], para); |
---|
| 461 | if ( roar_dl_ra_init(cont->handle[idx], NULL, para) == -1 ) { |
---|
| 462 | err = roar_error; |
---|
| 463 | |
---|
| 464 | if ( cont->callbacks != NULL && cont->callbacks->postra_init != NULL ) |
---|
| 465 | cont->callbacks->postra_init(cont, &(cont->context[idx]), cont->handle[idx], para); |
---|
| 466 | |
---|
| 467 | _unload(cont, idx); |
---|
| 468 | |
---|
| 469 | roar_dl_para_unref(para); |
---|
| 470 | cont->handle[idx] = NULL; |
---|
| 471 | roar_error = err; |
---|
| 472 | return NULL; |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | if ( cont->callbacks != NULL && cont->callbacks->postra_init != NULL ) |
---|
| 476 | cont->callbacks->postra_init(cont, &(cont->context[idx]), cont->handle[idx], para); |
---|
[5504] | 477 | |
---|
| 478 | if ( cont->options & OPT_AUTOAPPSCHED ) |
---|
| 479 | roar_dl_appsched_trigger(cont->handle[idx], ROAR_DL_APPSCHED_INIT); |
---|
[5488] | 480 | } |
---|
| 481 | |
---|
[5436] | 482 | roar_dl_para_unref(para); |
---|
[5429] | 483 | roar_dl_ref(cont->handle[idx]); |
---|
| 484 | return cont->handle[idx]; |
---|
[5320] | 485 | } |
---|
| 486 | |
---|
[5317] | 487 | int roar_plugincontainer_unload(struct roar_plugincontainer * cont, const char * name) { |
---|
[5487] | 488 | struct roar_dl_lhandle * lhandle; |
---|
[5429] | 489 | |
---|
| 490 | if ( cont == NULL || name == NULL ) { |
---|
| 491 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 492 | return -1; |
---|
| 493 | } |
---|
| 494 | |
---|
[5487] | 495 | lhandle = roar_plugincontainer_get_lhandle_by_name(cont, name); |
---|
| 496 | if ( lhandle == NULL ) |
---|
| 497 | return -1; |
---|
[5429] | 498 | |
---|
[5487] | 499 | roar_dl_unref(lhandle); // we can do this early here as the handle stay valid in the container's array. |
---|
[5429] | 500 | |
---|
[5487] | 501 | return roar_plugincontainer_unload_lhandle(cont, lhandle); |
---|
[5317] | 502 | } |
---|
| 503 | |
---|
| 504 | int roar_plugincontainer_unload_lhandle(struct roar_plugincontainer * cont, struct roar_dl_lhandle * lhandle) { |
---|
| 505 | size_t i; |
---|
| 506 | |
---|
| 507 | if ( cont == NULL ) { |
---|
| 508 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 509 | return -1; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | if ( lhandle == NULL ) { |
---|
| 513 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 514 | return -1; |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 518 | if ( cont->handle[i] == lhandle ) { |
---|
[5320] | 519 | if ( cont->deprefc[i] ) { |
---|
| 520 | // still in use. |
---|
| 521 | roar_err_set(ROAR_ERROR_BUSY); |
---|
| 522 | return -1; |
---|
| 523 | } |
---|
[5488] | 524 | _unload(cont, i); |
---|
[5317] | 525 | return 0; |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 530 | return -1; |
---|
| 531 | } |
---|
| 532 | |
---|
[5432] | 533 | int roar_plugincontainer_ra_init (struct roar_plugincontainer * cont) { |
---|
| 534 | size_t i; |
---|
| 535 | |
---|
| 536 | if ( cont == NULL ) { |
---|
| 537 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 538 | return -1; |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 542 | if ( cont->handle[i] == NULL ) |
---|
| 543 | continue; |
---|
[5488] | 544 | |
---|
| 545 | if ( cont->callbacks != NULL && cont->callbacks->prera_init != NULL ) |
---|
| 546 | cont->callbacks->prera_init(cont, &(cont->context[i]), cont->handle[i], cont->default_para); |
---|
| 547 | |
---|
[5432] | 548 | roar_dl_ra_init(cont->handle[i], NULL, cont->default_para); |
---|
[5488] | 549 | |
---|
| 550 | if ( cont->callbacks != NULL && cont->callbacks->postra_init != NULL ) |
---|
| 551 | cont->callbacks->postra_init(cont, &(cont->context[i]), cont->handle[i], cont->default_para); |
---|
[5504] | 552 | |
---|
| 553 | if ( cont->options & OPT_AUTOAPPSCHED ) |
---|
| 554 | roar_dl_appsched_trigger(cont->handle[i], ROAR_DL_APPSCHED_INIT); |
---|
[5432] | 555 | } |
---|
| 556 | |
---|
| 557 | return 0; |
---|
| 558 | } |
---|
| 559 | |
---|
[5317] | 560 | // appsched: |
---|
| 561 | int roar_plugincontainer_appsched_trigger(struct roar_plugincontainer * cont, enum roar_dl_appsched_trigger trigger) { |
---|
| 562 | size_t i; |
---|
[5441] | 563 | int ret = -1; |
---|
| 564 | int rv; |
---|
[5317] | 565 | |
---|
| 566 | if ( cont == NULL ) { |
---|
| 567 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 568 | return -1; |
---|
| 569 | } |
---|
| 570 | |
---|
[5441] | 571 | if ( cont->numhandles == 1 ) { |
---|
| 572 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 573 | if ( cont->handle[i] == NULL ) |
---|
| 574 | continue; |
---|
| 575 | return roar_dl_appsched_trigger(cont->handle[i], trigger); |
---|
| 576 | } |
---|
| 577 | roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL); |
---|
| 578 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 579 | return -1; |
---|
| 580 | } |
---|
| 581 | |
---|
[5317] | 582 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
| 583 | if ( cont->handle[i] == NULL ) |
---|
| 584 | continue; |
---|
| 585 | |
---|
[5441] | 586 | rv = roar_dl_appsched_trigger(cont->handle[i], trigger); |
---|
| 587 | if ( rv == 0 ) |
---|
| 588 | ret = 0; |
---|
[5317] | 589 | } |
---|
| 590 | |
---|
[5441] | 591 | return ret; |
---|
[5317] | 592 | } |
---|
| 593 | |
---|
| 594 | //ll |
---|