/** * Reports a descriptor error. Does not set the parser to error state but * increments the error count, unless this is merely a warning. * * @param context the parsing context * @param warn whether this is only a warning * @param error_msg the error message * @param ... parameters for the error message */ static void descriptor_errorf(ploader_context_t *plcontext, int warn, const char *error_msg, ...) { va_list ap; char message[128]; va_start(ap, error_msg); vsnprintf(message, sizeof(message), error_msg, ap); va_end(ap); message[127] = '\0'; if (warn) { cpi_warnf(plcontext->context, N_("Suspicious plug-in descriptor content in %s, line %d, column %d (%s)."), plcontext->file, XML_GetCurrentLineNumber(plcontext->parser), XML_GetCurrentColumnNumber(plcontext->parser) + 1, message); } else { cpi_errorf(plcontext->context, N_("Invalid plug-in descriptor content in %s, line %d, column %d (%s)."), plcontext->file, XML_GetCurrentLineNumber(plcontext->parser), XML_GetCurrentColumnNumber(plcontext->parser) + 1, message); } if (!warn) { plcontext->error_count++; } }
CP_C_API void * cp_resolve_symbol(cp_context_t *context, const char *id, const char *name, cp_status_t *error) { cp_status_t status = CP_OK; int error_reported = 1; hnode_t *node; void *symbol = NULL; symbol_info_t *symbol_info = NULL; symbol_provider_info_t *provider_info = NULL; cp_plugin_t *pp = NULL; CHECK_NOT_NULL(context); CHECK_NOT_NULL(id); CHECK_NOT_NULL(name); // Resolve the symbol cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_LOGGER | CPI_CF_LISTENER | CPI_CF_STOP, __func__); do { // Allocate space for symbol hashes, if necessary if (context->resolved_symbols == NULL) { context->resolved_symbols = hash_create(HASHCOUNT_T_MAX, cpi_comp_ptr, cpi_hashfunc_ptr); } if (context->symbol_providers == NULL) { context->symbol_providers = hash_create(HASHCOUNT_T_MAX, cpi_comp_ptr, cpi_hashfunc_ptr); } if (context->resolved_symbols == NULL || context->symbol_providers == NULL) { status = CP_ERR_RESOURCE; break; } // Look up the symbol defining plug-in node = hash_lookup(context->env->plugins, id); if (node == NULL) { cpi_warnf(context, N_("Symbol %s in unknown plug-in %s could not be resolved."), name, id); status = CP_ERR_UNKNOWN; break; } pp = hnode_get(node); // Make sure the plug-in has been started if ((status = cpi_start_plugin(context, pp)) != CP_OK) { printf("Symbol %s in plug-in %s could not be resolved because the plug-in could not be started.\n", name, id); cpi_errorf(context, N_("Symbol %s in plug-in %s could not be resolved because the plug-in could not be started."), name, id); error_reported = 1; break; } // Check for a context specific symbol if (pp->defined_symbols != NULL && (node = hash_lookup(pp->defined_symbols, name)) != NULL) { symbol = hnode_get(node); } // Fall back to global symbols, if necessary if (symbol == NULL && pp->runtime_lib != NULL) { symbol = DLSYM(pp->runtime_lib, name); } if (symbol == NULL) { const char *error = DLERROR(); if (error == NULL) { error = _("Unspecified error."); } cpi_warnf(context, N_("Symbol %s in plug-in %s could not be resolved: %s"), name, id, error); status = CP_ERR_UNKNOWN; break; } // Lookup or initialize symbol provider information if ((node = hash_lookup(context->symbol_providers, pp)) != NULL) { provider_info = hnode_get(node); } else { if ((provider_info = malloc(sizeof(symbol_provider_info_t))) == NULL) { status = CP_ERR_RESOURCE; break; } memset(provider_info, 0, sizeof(symbol_provider_info_t)); provider_info->plugin = pp; provider_info->imported = (context->plugin == NULL || cpi_ptrset_contains(context->plugin->imported, pp)); if (!hash_alloc_insert(context->symbol_providers, pp, provider_info)) { status = CP_ERR_RESOURCE; break; } } // Lookup or initialize symbol information if ((node = hash_lookup(context->resolved_symbols, symbol)) != NULL) { symbol_info = hnode_get(node); } else { if ((symbol_info = malloc(sizeof(symbol_info_t))) == NULL) { status = CP_ERR_RESOURCE; break; } memset(symbol_info, 0, sizeof(symbol_info_t)); symbol_info->provider_info = provider_info; if (!hash_alloc_insert(context->resolved_symbols, symbol, symbol_info)) { status = CP_ERR_RESOURCE; break; } } // Add dependencies (for plug-in) if (provider_info != NULL && !provider_info->imported && provider_info->usage_count == 0) { if (!cpi_ptrset_add(context->plugin->imported, pp)) { status = CP_ERR_RESOURCE; break; } if (!cpi_ptrset_add(pp->importing, context->plugin)) { cpi_ptrset_remove(context->plugin->imported, pp); status = CP_ERR_RESOURCE; break; } cpi_debugf(context, "A dynamic dependency was created from plug-in %s to plug-in %s.", context->plugin->plugin->identifier, pp->plugin->identifier); } // Increase usage counts symbol_info->usage_count++; provider_info->usage_count++; if (cpi_is_logged(context, CP_LOG_DEBUG)) { char owner[64]; /* TRANSLATORS: First %s is the context owner */ cpi_debugf(context, "%s resolved symbol %s defined by plug-in %s.", cpi_context_owner(context, owner, sizeof(owner)), name, id); } } while (0); // Clean up if (symbol_info != NULL && symbol_info->usage_count == 0) { if ((node = hash_lookup(context->resolved_symbols, symbol)) != NULL) { hash_delete_free(context->resolved_symbols, node); } free(symbol_info); } if (provider_info != NULL && provider_info->usage_count == 0) { if ((node = hash_lookup(context->symbol_providers, pp)) != NULL) { hash_delete_free(context->symbol_providers, node); } free(provider_info); } // Report insufficient memory error if (status == CP_ERR_RESOURCE && !error_reported) { cpi_errorf(context, N_("Symbol %s in plug-in %s could not be resolved due to insufficient memory."), name, id); } cpi_unlock_context(context); // Return error code if (error != NULL) { *error = status; } // Return symbol return symbol; }