Example #1
0
int sc_unlock(sc_card_t *card)
{
	int r, r2;

	SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);

	if (card == NULL)
		return SC_ERROR_INVALID_ARGUMENTS;
	r = sc_mutex_lock(card->ctx, card->mutex);
	if (r != SC_SUCCESS)
		return r;
	assert(card->lock_count >= 1);
	if (--card->lock_count == 0) {
#ifdef INVALIDATE_CARD_CACHE_IN_UNLOCK
		/* invalidate cache */
		memset(&card->cache, 0, sizeof(card->cache));
		card->cache.valid = 0;
		sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "cache invalidated");
#endif
		/* release reader lock */
		if (card->reader->ops->unlock != NULL)
			r = card->reader->ops->unlock(card->reader);
	}
	r2 = sc_mutex_unlock(card->ctx, card->mutex);
	if (r2 != SC_SUCCESS) {
		sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "unable to release lock");
		r = (r == SC_SUCCESS) ? r2 : r;
	}
	return r;
}
Example #2
0
int sc_unlock(sc_card_t *card)
{
	int r, r2;

	if (!card)
		return SC_ERROR_INVALID_ARGUMENTS;

	LOG_FUNC_CALLED(card->ctx);

	r = sc_mutex_lock(card->ctx, card->mutex);
	if (r != SC_SUCCESS)
		return r;

	if (card->lock_count < 1) {
		return SC_ERROR_INVALID_ARGUMENTS;
	}
	if (--card->lock_count == 0) {
		if (card->flags & SC_CARD_FLAG_KEEP_ALIVE) {
			/* Multiple processes accessing the card will most likely render
			 * the card cache useless. To not have a bad cache, we explicitly
			 * invalidate it. */
			sc_invalidate_cache(card);
		}
		/* release reader lock */
		if (card->reader->ops->unlock != NULL)
			r = card->reader->ops->unlock(card->reader);
	}
	r2 = sc_mutex_unlock(card->ctx, card->mutex);
	if (r2 != SC_SUCCESS) {
		sc_log(card->ctx, "unable to release lock");
		r = (r == SC_SUCCESS) ? r2 : r;
	}

	return r;
}
Example #3
0
int sc_lock(sc_card_t *card)
{
	int r = 0, r2 = 0;

	SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);
	
	if (card == NULL)
		return SC_ERROR_INVALID_ARGUMENTS;
	r = sc_mutex_lock(card->ctx, card->mutex);
	if (r != SC_SUCCESS)
		return r;
	if (card->lock_count == 0) {
		if (card->reader->ops->lock != NULL) {
			r = card->reader->ops->lock(card->reader);
			if (r == SC_ERROR_CARD_RESET || r == SC_ERROR_READER_REATTACHED) {
				/* invalidate cache */
				memset(&card->cache, 0, sizeof(card->cache));
				card->cache.valid = 0;
				r = card->reader->ops->lock(card->reader);
			}
		}
		if (r == 0)
			card->cache.valid = 1;
	}
	if (r == 0)
		card->lock_count++;
	r2 = sc_mutex_unlock(card->ctx, card->mutex);
	if (r2 != SC_SUCCESS) {
		sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "unable to release lock");
		r = r != SC_SUCCESS ? r : r2;
	}
	return r;
}
Example #4
0
int sc_reset(sc_card_t *card, int do_cold_reset)
{
	int r, r2;

	if (card == NULL)
		return SC_ERROR_INVALID_ARGUMENTS;
	if (card->reader->ops->reset == NULL)
		return SC_ERROR_NOT_SUPPORTED;

	r = sc_mutex_lock(card->ctx, card->mutex);
	if (r != SC_SUCCESS)
		return r;

	r = card->reader->ops->reset(card->reader, do_cold_reset);
	/* invalidate cache */
	memset(&card->cache, 0, sizeof(card->cache));
	card->cache.valid = 0;

	r2 = sc_mutex_unlock(card->ctx, card->mutex);
	if (r2 != SC_SUCCESS) {
		sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "unable to release lock");
		r = r != SC_SUCCESS ? r : r2;
	}

	return r;
}
Example #5
0
int sc_lock(sc_card_t *card)
{
	int r = 0, r2 = 0;
	int was_reset = 0;
	int reader_lock_obtained  = 0;

	if (card == NULL)
		return SC_ERROR_INVALID_ARGUMENTS;

	LOG_FUNC_CALLED(card->ctx);

	r = sc_mutex_lock(card->ctx, card->mutex);
	if (r != SC_SUCCESS)
		return r;
	if (card->lock_count == 0) {
		if (card->reader->ops->lock != NULL) {
			r = card->reader->ops->lock(card->reader);
			while (r == SC_ERROR_CARD_RESET || r == SC_ERROR_READER_REATTACHED) {
				sc_invalidate_cache(card);
				if (was_reset++ > 4) /* TODO retry a few times */
					break;
				r = card->reader->ops->lock(card->reader);
			}
			if (r == 0)
				reader_lock_obtained = 1;
		}
		if (r == 0)
			card->cache.valid = 1;
	}
	if (r == 0)
		card->lock_count++;

	if (r == 0 && was_reset > 0) {
#ifdef ENABLE_SM
		if (card->sm_ctx.ops.open)
			card->sm_ctx.ops.open(card);
#endif
	}

	r2 = sc_mutex_unlock(card->ctx, card->mutex);
	if (r2 != SC_SUCCESS) {
		sc_log(card->ctx, "unable to release card->mutex lock");
		r = r != SC_SUCCESS ? r : r2;
	}

	/* give card driver a chance to do something when reader lock first obtained */
	if (r == 0 && reader_lock_obtained == 1  && card->ops->card_reader_lock_obtained)
		r = card->ops->card_reader_lock_obtained(card, was_reset);

	LOG_FUNC_RETURN(card->ctx, r);
}
Example #6
0
int sc_ctx_detect_readers(sc_context_t *ctx)
{
	int r = 0;
	const struct sc_reader_driver *drv = ctx->reader_driver;

	sc_mutex_lock(ctx, ctx->mutex);

	if (drv->ops->detect_readers != NULL)
		r = drv->ops->detect_readers(ctx);
	
	sc_mutex_unlock(ctx, ctx->mutex);

	return r;
}
Example #7
0
File: card.c Project: DDvO/OpenSC
int sc_lock(sc_card_t *card)
{
	int r = 0, r2 = 0;

	if (card == NULL)
		return SC_ERROR_INVALID_ARGUMENTS;

	LOG_FUNC_CALLED(card->ctx);

	r = sc_mutex_lock(card->ctx, card->mutex);
	if (r != SC_SUCCESS)
		return r;
	if (card->lock_count == 0) {
		if (card->reader->ops->lock != NULL) {
			r = card->reader->ops->lock(card->reader);
			if (r == SC_ERROR_CARD_RESET || r == SC_ERROR_READER_REATTACHED) {
				/* invalidate cache */
				memset(&card->cache, 0, sizeof(card->cache));
				card->cache.valid = 0;
#ifdef ENABLE_SM
				if (card->sm_ctx.ops.open)
					card->sm_ctx.ops.open(card);
#endif
				r = card->reader->ops->lock(card->reader);
			}
		}
		if (r == 0)
			card->cache.valid = 1;
	}
	if (r == 0)
		card->lock_count++;
	r2 = sc_mutex_unlock(card->ctx, card->mutex);
	if (r2 != SC_SUCCESS) {
		sc_log(card->ctx, "unable to release lock");
		r = r != SC_SUCCESS ? r : r2;
	}

	return r;
}
Example #8
0
int sc_set_card_driver(sc_context_t *ctx, const char *short_name)
{
	int i = 0, match = 0;

	sc_mutex_lock(ctx, ctx->mutex);
	if (short_name == NULL) {
		ctx->forced_driver = NULL;
		match = 1;
	} else while (ctx->card_drivers[i] != NULL && i < SC_MAX_CARD_DRIVERS) {
		struct sc_card_driver *drv = ctx->card_drivers[i];

		if (strcmp(short_name, drv->short_name) == 0) {
			ctx->forced_driver = drv;
			match = 1;
			break;
		}
		i++;
	}
	sc_mutex_unlock(ctx, ctx->mutex);
	if (match == 0)
		return SC_ERROR_OBJECT_NOT_FOUND; /* FIXME: invent error */
	return SC_SUCCESS;
}
Example #9
0
int sc_reset(sc_card_t *card, int do_cold_reset)
{
	int r, r2;

	if (card == NULL)
		return SC_ERROR_INVALID_ARGUMENTS;
	if (card->reader->ops->reset == NULL)
		return SC_ERROR_NOT_SUPPORTED;

	r = sc_mutex_lock(card->ctx, card->mutex);
	if (r != SC_SUCCESS)
		return r;

	r = card->reader->ops->reset(card->reader, do_cold_reset);
	sc_invalidate_cache(card);

	r2 = sc_mutex_unlock(card->ctx, card->mutex);
	if (r2 != SC_SUCCESS) {
		sc_log(card->ctx, "unable to release lock");
		r = r != SC_SUCCESS ? r : r2;
	}

	return r;
}