/* For multithreaded issues */ int sc_context_repair(sc_context_t **ctx_out) { /* Must already exist */ if ((ctx_out == NULL) || (*ctx_out == NULL) || ((*ctx_out)->app_name == NULL)) return SC_ERROR_INVALID_ARGUMENTS; /* The only thing that should be shared across different contexts are the * card drivers - so rebuild the ATR's */ load_card_atrs(*ctx_out); /* TODO: May need to re-open any card driver DLL's */ return SC_SUCCESS; }
int sc_context_create(sc_context_t **ctx_out, const sc_context_param_t *parm) { sc_context_t *ctx; struct _sc_ctx_options opts; int r; if (ctx_out == NULL || parm == NULL) return SC_ERROR_INVALID_ARGUMENTS; ctx = calloc(1, sizeof(sc_context_t)); if (ctx == NULL) return SC_ERROR_OUT_OF_MEMORY; memset(&opts, 0, sizeof(opts)); /* set the application name if set in the parameter options */ if (parm->app_name != NULL) ctx->app_name = strdup(parm->app_name); else ctx->app_name = strdup("default"); if (ctx->app_name == NULL) { sc_release_context(ctx); return SC_ERROR_OUT_OF_MEMORY; } set_defaults(ctx, &opts); list_init(&ctx->readers); list_attributes_seeker(&ctx->readers, reader_list_seeker); /* set thread context and create mutex object (if specified) */ if (parm->thread_ctx != NULL) ctx->thread_ctx = parm->thread_ctx; r = sc_mutex_create(ctx, &ctx->mutex); if (r != SC_SUCCESS) { sc_release_context(ctx); return r; } process_config_file(ctx, &opts); sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "==================================="); /* first thing in the log */ sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "opensc version: %s", sc_get_version()); #ifdef HAVE_LTDL_H /* initialize ltdl, if available. See scdl.c for more information */ if (lt_dlinit() != 0) { sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "lt_dlinit() failed"); sc_release_context(ctx); return SC_ERROR_INTERNAL; } #endif #ifdef ENABLE_PCSC ctx->reader_driver = sc_get_pcsc_driver(); /* XXX: remove cardmod pseudoreader driver */ #ifdef ENABLE_MINIDRIVER if(strcmp(ctx->app_name, "cardmod") == 0) { ctx->reader_driver = sc_get_cardmod_driver(); } #endif #elif ENABLE_CTAPI ctx->reader_driver = sc_get_ctapi_driver(); #elif ENABLE_OPENCT ctx->reader_driver = sc_get_openct_driver(); #endif load_reader_driver_options(ctx); ctx->reader_driver->ops->init(ctx); load_card_drivers(ctx, &opts); load_card_atrs(ctx); if (opts.forced_card_driver) { /* FIXME: check return value? */ sc_set_card_driver(ctx, opts.forced_card_driver); free(opts.forced_card_driver); } del_drvs(&opts); sc_ctx_detect_readers(ctx); *ctx_out = ctx; return SC_SUCCESS; }
int sc_context_create(sc_context_t **ctx_out, const sc_context_param_t *parm) { sc_context_t *ctx; struct _sc_ctx_options opts; int r; char *driver; if (ctx_out == NULL || parm == NULL) return SC_ERROR_INVALID_ARGUMENTS; ctx = calloc(1, sizeof(sc_context_t)); if (ctx == NULL) return SC_ERROR_OUT_OF_MEMORY; memset(&opts, 0, sizeof(opts)); /* set the application name if set in the parameter options */ if (parm->app_name != NULL) ctx->app_name = strdup(parm->app_name); else ctx->app_name = strdup("default"); if (ctx->app_name == NULL) { sc_release_context(ctx); return SC_ERROR_OUT_OF_MEMORY; } ctx->flags = parm->flags; set_defaults(ctx, &opts); if (0 != list_init(&ctx->readers)) { sc_release_context(ctx); return SC_ERROR_OUT_OF_MEMORY; } list_attributes_seeker(&ctx->readers, reader_list_seeker); /* set thread context and create mutex object (if specified) */ if (parm->thread_ctx != NULL) ctx->thread_ctx = parm->thread_ctx; r = sc_mutex_create(ctx, &ctx->mutex); if (r != SC_SUCCESS) { sc_release_context(ctx); return r; } #if defined(ENABLE_OPENSSL) && defined(OPENSSL_SECURE_MALLOC_SIZE) if (!CRYPTO_secure_malloc_initialized()) { CRYPTO_secure_malloc_init(OPENSSL_SECURE_MALLOC_SIZE, OPENSSL_SECURE_MALLOC_SIZE/8); } #endif process_config_file(ctx, &opts); sc_log(ctx, "==================================="); /* first thing in the log */ sc_log(ctx, "opensc version: %s", sc_get_version()); #ifdef ENABLE_PCSC ctx->reader_driver = sc_get_pcsc_driver(); #elif defined(ENABLE_CRYPTOTOKENKIT) ctx->reader_driver = sc_get_cryptotokenkit_driver(); #elif defined(ENABLE_CTAPI) ctx->reader_driver = sc_get_ctapi_driver(); #elif defined(ENABLE_OPENCT) ctx->reader_driver = sc_get_openct_driver(); #endif r = ctx->reader_driver->ops->init(ctx); if (r != SC_SUCCESS) { sc_release_context(ctx); return r; } driver = getenv("OPENSC_DRIVER"); if (driver) { scconf_list *list = NULL; scconf_list_add(&list, driver); set_drivers(&opts, list); scconf_list_destroy(list); } load_card_drivers(ctx, &opts); load_card_atrs(ctx); del_drvs(&opts); sc_ctx_detect_readers(ctx); *ctx_out = ctx; return SC_SUCCESS; }