1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
|
/*-------------------------------------------------------------------------
* C-Pluff, a plug-in framework for C
* Copyright 2007 Johannes Lehtinen
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*-----------------------------------------------------------------------*/
/** @file
* Plug-in context implementation
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include "../kazlib/list.h"
#include "cpluff.h"
#include "util.h"
#ifdef CP_THREADS
#include "thread.h"
#endif
#include "internal.h"
/* ------------------------------------------------------------------------
* Variables
* ----------------------------------------------------------------------*/
/// Existing contexts
static list_t *contexts = NULL;
/* ------------------------------------------------------------------------
* Function definitions
* ----------------------------------------------------------------------*/
// Generic
static void free_plugin_env(cp_plugin_env_t *env) {
assert(env != NULL);
// Free environment data
if (env->plugin_listeners != NULL) {
cpi_unregister_plisteners(env->plugin_listeners, NULL);
list_destroy(env->plugin_listeners);
env->plugin_listeners = NULL;
}
if (env->loggers != NULL) {
cpi_unregister_loggers(env->loggers, NULL);
list_destroy(env->loggers);
env->loggers = NULL;
}
if (env->local_loader != NULL) {
cp_destroy_local_ploader(env->local_loader);
env->local_loader = NULL;
}
if (env->loaders_to_plugins != NULL) {
assert(hash_isempty(env->loaders_to_plugins));
hash_destroy(env->loaders_to_plugins);
env->loaders_to_plugins = NULL;
}
if (env->infos != NULL) {
assert(hash_isempty(env->infos));
hash_destroy(env->infos);
env->infos = NULL;
}
if (env->plugins != NULL) {
assert(hash_isempty(env->plugins));
hash_destroy(env->plugins);
env->plugins = NULL;
}
if (env->started_plugins != NULL) {
assert(list_isempty(env->started_plugins));
list_destroy(env->started_plugins);
env->started_plugins = NULL;
}
if (env->ext_points != NULL) {
assert(hash_isempty(env->ext_points));
hash_destroy(env->ext_points);
}
if (env->extensions != NULL) {
assert(hash_isempty(env->extensions));
hash_destroy(env->extensions);
}
if (env->run_funcs != NULL) {
assert(list_isempty(env->run_funcs));
list_destroy(env->run_funcs);
}
// Destroy mutex
#ifdef CP_THREADS
if (env->mutex != NULL) {
cpi_destroy_mutex(env->mutex);
}
#endif
// Free environment
free(env);
}
CP_HIDDEN void cpi_free_context(cp_context_t *context) {
assert(context != NULL);
// Free environment if this is the client program context
if (context->plugin == NULL && context->env != NULL) {
free_plugin_env(context->env);
}
// Destroy symbol lists
if (context->resolved_symbols != NULL) {
assert(hash_isempty(context->resolved_symbols));
hash_destroy(context->resolved_symbols);
}
if (context->symbol_providers != NULL) {
assert(hash_isempty(context->symbol_providers));
hash_destroy(context->symbol_providers);
}
// Free context
free(context);
}
CP_HIDDEN cp_context_t * cpi_new_context(cp_plugin_t *plugin, cp_plugin_env_t *env, cp_status_t *error) {
cp_context_t *context = NULL;
cp_status_t status = CP_OK;
assert(env != NULL);
assert(error != NULL);
do {
// Allocate memory for the context
if ((context = malloc(sizeof(cp_context_t))) == NULL) {
status = CP_ERR_RESOURCE;
break;
}
// Initialize context
context->plugin = plugin;
context->env = env;
context->resolved_symbols = NULL;
context->symbol_providers = NULL;
} while (0);
// Free context on error
if (status != CP_OK && context != NULL) {
free(context);
context = NULL;
}
*error = status;
return context;
}
CP_C_API cp_context_t * cp_create_context(cp_status_t *error) {
cp_plugin_env_t *env = NULL;
cp_context_t *context = NULL;
cp_status_t status = CP_OK;
// Initialize internal state
do {
// Allocate memory for the plug-in environment
if ((env = malloc(sizeof(cp_plugin_env_t))) == NULL) {
status = CP_ERR_RESOURCE;
break;
}
// Initialize plug-in environment
memset(env, 0, sizeof(cp_plugin_env_t));
#ifdef CP_THREADS
env->mutex = cpi_create_mutex();
#endif
env->argc = 0;
env->argv = NULL;
env->plugin_descriptor_name = CP_PLUGIN_DESCRIPTOR;
env->plugin_descriptor_root_element = CP_PLUGIN_ROOT_ELEMENT;
env->plugin_listeners = list_create(LISTCOUNT_T_MAX);
env->loggers = list_create(LISTCOUNT_T_MAX);
env->log_min_severity = CP_LOG_NONE;
env->local_loader = NULL;
env->loaders_to_plugins = hash_create(LISTCOUNT_T_MAX, cpi_comp_ptr, cpi_hashfunc_ptr);
env->infos = hash_create(HASHCOUNT_T_MAX, cpi_comp_ptr, cpi_hashfunc_ptr);
env->plugins = hash_create(HASHCOUNT_T_MAX,
(int (*)(const void *, const void *)) strcmp, NULL);
env->started_plugins = list_create(LISTCOUNT_T_MAX);
env->ext_points = hash_create(HASHCOUNT_T_MAX,
(int (*)(const void *, const void *)) strcmp, NULL);
env->extensions = hash_create(HASHCOUNT_T_MAX,
(int (*)(const void *, const void *)) strcmp, NULL);
env->run_funcs = list_create(LISTCOUNT_T_MAX);
env->run_wait = NULL;
if (env->plugin_listeners == NULL
|| env->loggers == NULL
#ifdef CP_THREADS
|| env->mutex == NULL
#endif
|| env->loaders_to_plugins == NULL
|| env->infos == NULL
|| env->plugins == NULL
|| env->started_plugins == NULL
|| env->ext_points == NULL
|| env->extensions == NULL
|| env->run_funcs == NULL) {
status = CP_ERR_RESOURCE;
break;
}
// Create the plug-in context
if ((context = cpi_new_context(NULL, env, &status)) == NULL) {
break;
}
env = NULL;
// Create a context list, if necessary, and add context to the list
cpi_lock_framework();
if (contexts == NULL) {
if ((contexts = list_create(LISTCOUNT_T_MAX)) == NULL) {
status = CP_ERR_RESOURCE;
}
}
if (status == CP_OK) {
lnode_t *node;
if ((node = lnode_create(context)) == NULL) {
status = CP_ERR_RESOURCE;
} else {
list_append(contexts, node);
}
}
cpi_unlock_framework();
} while (0);
// Release resources on failure
if (status != CP_OK) {
if (env != NULL) {
free_plugin_env(env);
}
if (context != NULL) {
cpi_free_context(context);
}
context = NULL;
}
// Return the final status
if (error != NULL) {
*error = status;
}
// Return the context (or NULL on failure)
return context;
}
CP_C_API void cp_set_plugin_descriptor_root_element(cp_context_t *context, const char *root) {
CHECK_NOT_NULL(context);
CHECK_NOT_NULL(context->env);
CHECK_NOT_NULL(root);
context->env->plugin_descriptor_root_element = root;
}
CP_C_API void cp_set_plugin_descriptor_name(cp_context_t *context, const char *name) {
CHECK_NOT_NULL(context);
CHECK_NOT_NULL(context->env);
CHECK_NOT_NULL(name);
context->env->plugin_descriptor_name = name;
}
CP_C_API void cp_destroy_context(cp_context_t *context) {
CHECK_NOT_NULL(context);
if (context->plugin != NULL) {
cpi_fatalf(_("Only the main program can destroy a plug-in context."));
}
// Check invocation
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_ANY, __func__);
cpi_unlock_context(context);
#ifdef CP_THREADS
assert(context->env->mutex == NULL || !cpi_is_mutex_locked(context->env->mutex));
#else
assert(!context->env->locked);
#endif
// Remove context from the context list
cpi_lock_framework();
if (contexts != NULL) {
lnode_t *node;
if ((node = list_find(contexts, context, cpi_comp_ptr)) != NULL) {
list_delete(contexts, node);
lnode_destroy(node);
}
}
cpi_unlock_framework();
// Unload all plug-ins
cp_uninstall_plugins(context);
// Unregister all plug-in loaders
cp_unregister_ploaders(context);
// Unregister implicit local plug-in loader, if any
if (context->env->local_loader != NULL) {
cp_unregister_ploader(context, context->env->local_loader);
}
// Release remaining information objects
cpi_release_infos(context);
// Free context
cpi_free_context(context);
}
CP_HIDDEN void cpi_destroy_all_contexts(void) {
cpi_lock_framework();
if (contexts != NULL) {
lnode_t *node;
while ((node = list_last(contexts)) != NULL) {
cpi_unlock_framework();
cp_destroy_context(lnode_get(node));
cpi_lock_framework();
}
list_destroy(contexts);
contexts = NULL;
}
cpi_unlock_framework();
}
// Plug-in directories
/*
* Initializes the implicit local plug-in loader of the specified context.
* Does nothing if the loader has already been initialized.
*/
static cp_status_t init_local_ploader(cp_context_t *context) {
cp_status_t status = CP_OK;
assert(cpi_is_context_locked(context));
// Create new local plug-in loader, if one does not exist
if (context->env->local_loader == NULL) {
context->env->local_loader = cp_create_local_ploader(&status);
status = cp_register_ploader(context, context->env->local_loader);
}
return status;
}
CP_C_API cp_status_t cp_register_pcollection(cp_context_t *context, const char *dir) {
cp_status_t status = CP_OK;
CHECK_NOT_NULL(context);
CHECK_NOT_NULL(dir);
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_ANY, __func__);
do {
// Initialize plug-in loader
if ((status = init_local_ploader(context)) != CP_OK) {
break;
}
// Register directory
status = cp_lpl_register_dir(context->env->local_loader, dir);
} while (0);
// Report error or success
if (status != CP_OK) {
cpi_errorf(context, N_("The plug-in collection in path %s could not be registered due to insufficient memory."), dir);
} else {
cpi_debugf(context, N_("The plug-in collection in path %s was registered."), dir);
}
cpi_unlock_context(context);
return status;
}
CP_C_API void cp_unregister_pcollection(cp_context_t *context, const char *dir) {
CHECK_NOT_NULL(context);
CHECK_NOT_NULL(dir);
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_ANY, __func__);
if (context->env->local_loader != NULL) {
cp_lpl_unregister_dir(context->env->local_loader, dir);
}
cpi_debugf(context, N_("The plug-in collection in path %s was unregistered."), dir);
cpi_unlock_context(context);
}
CP_C_API void cp_unregister_pcollections(cp_context_t *context) {
CHECK_NOT_NULL(context);
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_ANY, __func__);
if (context->env->local_loader != NULL) {
cp_lpl_unregister_dirs(context->env->local_loader);
}
cpi_debug(context, N_("All plug-in collections were unregistered."));
cpi_unlock_context(context);
}
// Plug-in loaders
CP_C_API cp_status_t cp_register_ploader(cp_context_t *ctx, cp_plugin_loader_t *loader) {
cp_status_t status = CP_OK;
hash_t *loader_plugins = NULL;
CHECK_NOT_NULL(ctx);
CHECK_NOT_NULL(loader);
cpi_lock_context(ctx);
cpi_check_invocation(ctx, CPI_CF_ANY, __func__);
do {
if ((loader_plugins = hash_create(HASHCOUNT_T_MAX, (int (*)(const void *, const void *)) strcmp, NULL)) == NULL) {
status = CP_ERR_RESOURCE;
break;
}
if (!hash_alloc_insert(ctx->env->loaders_to_plugins, loader, loader_plugins)) {
status = CP_ERR_RESOURCE;
break;
}
} while (0);
// Report error or success
if (status != CP_OK) {
cpi_errorf(ctx, N_("The plug-in loader %p could not be registered due to insufficient memory."), (void *) loader);
} else {
cpi_debugf(ctx, N_("The plug-in loader %p was registered."), (void *) loader);
}
cpi_unlock_context(ctx);
// Release resources
if (status != CP_OK) {
if (loader_plugins != NULL) {
assert(hash_isempty(loader_plugins));
hash_destroy(loader_plugins);
}
}
return status;
}
CP_C_API void cp_unregister_ploader(cp_context_t *ctx, cp_plugin_loader_t *loader) {
hnode_t *hnode;
CHECK_NOT_NULL(ctx);
CHECK_NOT_NULL(loader);
cpi_lock_context(ctx);
cpi_check_invocation(ctx, CPI_CF_ANY, __func__);
hnode = hash_lookup(ctx->env->loaders_to_plugins, loader);
if (hnode != NULL) {
hash_t *loader_plugins = hnode_get(hnode);
// Uninstall all plug-ins loaded by the loader
while (!hash_isempty(loader_plugins)) {
hscan_t hscan;
hnode_t *hnode2;
cp_status_t status;
hash_scan_begin(&hscan, loader_plugins);
hnode2 = hash_scan_next(&hscan);
assert(hnode2 != NULL);
status = cp_uninstall_plugin(ctx, hnode_getkey(hnode2));
assert(status == CP_OK);
}
hash_delete_free(ctx->env->loaders_to_plugins, hnode);
assert(hash_isempty(loader_plugins));
hash_destroy(loader_plugins);
cpi_debugf(ctx, N_("The plug-in loader %p was unregistered."), (void *) loader);
}
cpi_unlock_context(ctx);
}
CP_C_API void cp_unregister_ploaders(cp_context_t *ctx) {
int found;
CHECK_NOT_NULL(ctx);
cpi_lock_context(ctx);
cpi_check_invocation(ctx, CPI_CF_ANY, __func__);
do {
hscan_t hscan;
hnode_t *hnode;
hash_scan_begin(&hscan, ctx->env->loaders_to_plugins);
do {
hnode = hash_scan_next(&hscan);
} while (hnode != NULL && hnode_getkey(hnode) == ctx->env->local_loader);
if (hnode != NULL) {
cp_plugin_loader_t *loader = (cp_plugin_loader_t *) hnode_getkey(hnode);
cp_unregister_ploader(ctx, loader);
found = 1;
} else {
found = 0;
}
} while (found);
cpi_unlock_context(ctx);
}
// Startup arguments
CP_C_API void cp_set_context_args(cp_context_t *ctx, char **argv) {
int argc;
CHECK_NOT_NULL(ctx);
CHECK_NOT_NULL(argv);
for (argc = 0; argv[argc] != NULL; argc++);
if (argc < 1) {
cpi_fatalf(_("At least one startup argument must be given in call to function %s."), __func__);
}
cpi_lock_context(ctx);
ctx->env->argc = argc;
ctx->env->argv = argv;
cpi_unlock_context(ctx);
}
CP_C_API char **cp_get_context_args(cp_context_t *ctx, int *argc) {
char **argv;
CHECK_NOT_NULL(ctx);
cpi_lock_context(ctx);
if (argc != NULL) {
*argc = ctx->env->argc;
}
argv = ctx->env->argv;
cpi_unlock_context(ctx);
return argv;
}
// Checking API call invocation
CP_HIDDEN void cpi_check_invocation(cp_context_t *ctx, int funcmask, const char *func) {
assert(ctx != NULL);
assert(funcmask != 0);
assert(func != NULL);
assert(cpi_is_context_locked(ctx));
if ((funcmask & CPI_CF_LOGGER)
&&ctx->env->in_logger_invocation) {
cpi_fatalf(_("Function %s was called from within a logger invocation."), func);
}
if ((funcmask & CPI_CF_LISTENER)
&& ctx->env->in_event_listener_invocation) {
cpi_fatalf(_("Function %s was called from within an event listener invocation."), func);
}
if ((funcmask & CPI_CF_START)
&& ctx->env->in_start_func_invocation) {
cpi_fatalf(_("Function %s was called from within a plug-in start function invocation."), func);
}
if ((funcmask & CPI_CF_STOP)
&& ctx->env->in_stop_func_invocation) {
cpi_fatalf(_("Function %s was called from within a plug-in stop function invocation."), func);
}
if (ctx->env->in_create_func_invocation) {
cpi_fatalf(_("Function %s was called from within a plug-in create function invocation."), func);
}
if (ctx->env->in_destroy_func_invocation) {
cpi_fatalf(_("Function %s was called from within a plug-in destroy function invocation."), func);
}
}
// Locking
#if defined(CP_THREADS) || !defined(NDEBUG)
CP_HIDDEN void cpi_lock_context(cp_context_t *context) {
#if defined(CP_THREADS)
cpi_lock_mutex(context->env->mutex);
#elif !defined(NDEBUG)
context->env->locked++;
#endif
}
CP_HIDDEN void cpi_unlock_context(cp_context_t *context) {
#if defined(CP_THREADS)
cpi_unlock_mutex(context->env->mutex);
#elif !defined(NDEBUG)
assert(context->env->locked > 0);
context->env->locked--;
#endif
}
CP_HIDDEN void cpi_wait_context(cp_context_t *context) {
#if defined(CP_THREADS)
cpi_wait_mutex(context->env->mutex);
#elif !defined(NDEBUG)
assert(context->env->locked > 0);
assert(0);
#endif
}
CP_HIDDEN void cpi_signal_context(cp_context_t *context) {
#if defined(CP_THREADS)
cpi_signal_mutex(context->env->mutex);
#elif !defined(NDEBUG)
assert(context->env->locked > 0);
#endif
}
// Debug helpers
CP_HIDDEN char *cpi_context_owner(cp_context_t *ctx, char *name, size_t size) {
if (ctx->plugin != NULL) {
/* TRANSLATORS: The context owner (when it is a plug-in) used in some strings.
Search for "context owner" to find these strings. */
snprintf(name, size, _("Plug-in %s"), ctx->plugin->plugin->identifier);
} else {
/* TRANSLATORS: The context owner (when it is the main program) used in some strings.
Search for "context owner" to find these strings. */
strncpy(name, _("The main program"), size);
}
assert(size >= 4);
strcpy(name + size - 4, "...");
return name;
}
#endif
|