On Mon, Apr 18, 2011 at 10:40, Viktor TARASOV
<viktor.tara...@opentrust.com> wrote:
> I do not have such behavior on Linux.
> On Windows I'll look it later, otherwise you can activate and post here the 
> logs.

I think I know the problem. Here's an excerpt from MSDN:

"""
File Handles
It is not possible to share file handles between applications or DLLs.
Each application has its own file-handle table. For two applications
to use the same file using a DLL, they must both open the file
individually.
"""

The attached patch introduces a new sc_ctx call that sets the debug
file handle from a string. pkcs15-tool -vvvvv does not crash after
this.
From 5a5b9c04e5a19194883f75f6adaa11e6633ad8df Mon Sep 17 00:00:00 2001
From: Martin Paljak <mar...@martinpaljak.net>
Date: Mon, 18 Apr 2011 11:48:27 +0300
Subject: [PATCH] Introduce sc_ctx_log_to_file to set the debug file of libopensc.

On Windows every DLL has their own file descriptor table, thus specifying
-v from any of the OpenSC tools resulted in a crash when the tool tried to override
ctx->debug_file with stderr.
---
 src/libopensc/ctx.c             |   36 ++++++++++++++++++++++++++----------
 src/libopensc/libopensc.exports |    1 +
 src/libopensc/opensc.h          |    8 ++++++++
 src/tools/cardos-tool.c         |    2 +-
 src/tools/cryptoflex-tool.c     |    2 +-
 src/tools/netkey-tool.c         |    2 +-
 src/tools/opensc-explorer.c     |    6 ++----
 src/tools/opensc-tool.c         |    2 +-
 src/tools/piv-tool.c            |    2 +-
 src/tools/pkcs15-crypt.c        |    2 +-
 src/tools/pkcs15-init.c         |    2 +-
 src/tools/pkcs15-tool.c         |    2 +-
 src/tools/westcos-tool.c        |    2 +-
 13 files changed, 46 insertions(+), 23 deletions(-)

diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c
index b7fe575..c91626a 100644
--- a/src/libopensc/ctx.c
+++ b/src/libopensc/ctx.c
@@ -190,6 +190,29 @@ static void set_defaults(sc_context_t *ctx, struct _sc_ctx_options *opts)
 	add_internal_drvs(opts);
 }
 
+/* In Windows, file handles can not be shared between DLL-s,
+ * each DLL has a separate file handle table. Thus tools and utilities
+ * can not set the file handle themselves when -v is specified on command line.
+ */
+int sc_ctx_log_to_file(sc_context_t *ctx, const char* filename)
+{
+	/* Close any existing handles */
+	if (ctx->debug_file && (ctx->debug_file != stderr && ctx->debug_file != stdout))
+		fclose(ctx->debug_file);
+
+	/* Handle special names */
+	if (!strcmp(filename, "stdout"))
+		ctx->debug_file = stdout;
+	else if (!strcmp(filename, "stderr"))
+		ctx->debug_file = stderr;
+	else {
+		ctx->debug_file = fopen(filename, "a");
+		if (ctx->debug_file == NULL)
+			return SC_ERROR_INTERNAL;
+	}
+	return SC_SUCCESS;
+}
+
 static int load_parameters(sc_context_t *ctx, scconf_block *block,
 			   struct _sc_ctx_options *opts)
 {
@@ -204,16 +227,9 @@ static int load_parameters(sc_context_t *ctx, scconf_block *block,
 		ctx->debug = atoi(debug);
 
 	val = scconf_get_str(block, "debug_file", NULL);
-	if (val) {
-		if (ctx->debug_file && (ctx->debug_file != stderr && ctx->debug_file != stdout))
-			fclose(ctx->debug_file);
-		if (!strcmp(val, "stdout"))
-			ctx->debug_file = stdout;
-		else if (!strcmp(val, "stderr"))
-			ctx->debug_file = stderr;
-		else
-			ctx->debug_file = fopen(val, "a");
-	}
+	if (val)
+		sc_ctx_log_to_file(ctx, val);
+
 	val = scconf_get_str(block, "force_card_driver", NULL);
 	if (val) {
 		if (opts->forced_card_driver)
diff --git a/src/libopensc/libopensc.exports b/src/libopensc/libopensc.exports
index d533443..e75160b 100644
--- a/src/libopensc/libopensc.exports
+++ b/src/libopensc/libopensc.exports
@@ -71,6 +71,7 @@ sc_ctx_get_reader
 sc_ctx_get_reader_by_id
 sc_ctx_get_reader_by_name
 sc_ctx_get_reader_count
+sc_ctx_log_to_file
 sc_ctx_use_reader
 sc_decipher
 sc_delete_file
diff --git a/src/libopensc/opensc.h b/src/libopensc/opensc.h
index bfd52bb..2976f03 100644
--- a/src/libopensc/opensc.h
+++ b/src/libopensc/opensc.h
@@ -756,6 +756,14 @@ sc_reader_t *sc_ctx_get_reader_by_id(sc_context_t *ctx, unsigned int id);
 unsigned int sc_ctx_get_reader_count(sc_context_t *ctx);
 
 /**
+ * Redirects OpenSC debug log to the specified file
+ * @param  ctx existing OpenSC context
+ * @param  filename path to the file or "stderr" or "stdout"
+ * @return SC_SUCCESS on success and an error code otherwise
+ */
+int sc_ctx_log_to_file(sc_context_t *ctx, const char* filename);
+
+/**
  * Forces the use of a specified card driver
  * @param ctx OpenSC context
  * @param short_name The short name of the driver to use (e.g. 'cardos')
diff --git a/src/tools/cardos-tool.c b/src/tools/cardos-tool.c
index 0856e3b..e811795 100644
--- a/src/tools/cardos-tool.c
+++ b/src/tools/cardos-tool.c
@@ -1135,7 +1135,7 @@ int main(int argc, char *const argv[])
 
 	if (verbose > 1) {
 		ctx->debug = verbose;
-		ctx->debug_file = stderr;
+		sc_ctx_log_to_file(ctx, "stderr");
 	}
 
 	if (opt_driver != NULL) {
diff --git a/src/tools/cryptoflex-tool.c b/src/tools/cryptoflex-tool.c
index 4e02b58..8b92a77 100644
--- a/src/tools/cryptoflex-tool.c
+++ b/src/tools/cryptoflex-tool.c
@@ -1047,7 +1047,7 @@ int main(int argc, char * const argv[])
 
 	if (verbose > 1) {
 		ctx->debug = verbose;
-		ctx->debug_file = stderr;	
+		sc_ctx_log_to_file(ctx, "stderr");
 	}
 
 	err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
diff --git a/src/tools/netkey-tool.c b/src/tools/netkey-tool.c
index 389d75c..a777b10 100644
--- a/src/tools/netkey-tool.c
+++ b/src/tools/netkey-tool.c
@@ -544,7 +544,7 @@ int main(
 	}
 	if (debug > 1) {
 		ctx->debug = debug;
-		ctx->debug_file = stderr;
+		sc_ctx_log_to_file(ctx, "stderr");
 	}
 	if(ctx->debug>0)
 		printf("Context for application \"%s\" created, Debug=%d\n", ctx->app_name, ctx->debug);
diff --git a/src/tools/opensc-explorer.c b/src/tools/opensc-explorer.c
index b5ab2b5..45c46a6 100644
--- a/src/tools/opensc-explorer.c
+++ b/src/tools/opensc-explorer.c
@@ -1192,10 +1192,8 @@ static int do_debug(int argc, char **argv)
 			return -1;
 		printf("Debug level set to %d\n", i);
 		ctx->debug = i;
-		if (i) {
-			ctx->debug_file = stderr;
-		} else {
-			ctx->debug_file = NULL;
+		if (i > 1) {
+			sc_ctx_log_to_file(ctx, "stderr");
 		}
 	}
 	return 0;
diff --git a/src/tools/opensc-tool.c b/src/tools/opensc-tool.c
index 529e842..aaabf33 100644
--- a/src/tools/opensc-tool.c
+++ b/src/tools/opensc-tool.c
@@ -760,7 +760,7 @@ int main(int argc, char * const argv[])
 
 	if (verbose > 1) {
 		ctx->debug = verbose;
-		ctx->debug_file = stderr;
+		sc_ctx_log_to_file(ctx, "stderr");
 	}
 
 	if (do_get_conf_entry) {
diff --git a/src/tools/piv-tool.c b/src/tools/piv-tool.c
index c215fde..a5e9db6 100644
--- a/src/tools/piv-tool.c
+++ b/src/tools/piv-tool.c
@@ -745,7 +745,7 @@ int main(int argc, char * const argv[])
 	/* Only change if not in opensc.conf */
 	if (verbose > 1 && ctx->debug == 0) { 
 		ctx->debug = verbose;
-		ctx->debug_file = stderr;
+		sc_ctx_log_to_file(ctx, "stderr");
 	}
 
 	if (action_count <= 0)
diff --git a/src/tools/pkcs15-crypt.c b/src/tools/pkcs15-crypt.c
index 3873118..618364e 100644
--- a/src/tools/pkcs15-crypt.c
+++ b/src/tools/pkcs15-crypt.c
@@ -577,7 +577,7 @@ int main(int argc, char * const argv[])
 
 	if (verbose > 1) {
 		ctx->debug = verbose;
-		ctx->debug_file = stderr;
+		sc_ctx_log_to_file(ctx, "stderr");
 	}
 
 	err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
diff --git a/src/tools/pkcs15-init.c b/src/tools/pkcs15-init.c
index 71c0f6d..0b3fa39 100644
--- a/src/tools/pkcs15-init.c
+++ b/src/tools/pkcs15-init.c
@@ -589,7 +589,7 @@ open_reader_and_card(char *reader)
 
 	if (verbose > 1) {
 		ctx->debug = verbose;
-		ctx->debug_file = stderr;
+		sc_ctx_log_to_file(ctx, "stderr");
 	}
 
 	if (util_connect_card(ctx, &card, reader, opt_wait, verbose))
diff --git a/src/tools/pkcs15-tool.c b/src/tools/pkcs15-tool.c
index 4494d92..8617424 100644
--- a/src/tools/pkcs15-tool.c
+++ b/src/tools/pkcs15-tool.c
@@ -1785,7 +1785,7 @@ int main(int argc, char * const argv[])
 
 	if (verbose > 1) {
 		ctx->debug = verbose;
-		ctx->debug_file = stderr;
+		sc_ctx_log_to_file(ctx, "stderr");
 	}
                                          
 	err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
diff --git a/src/tools/westcos-tool.c b/src/tools/westcos-tool.c
index 577f782..c8911c7 100644
--- a/src/tools/westcos-tool.c
+++ b/src/tools/westcos-tool.c
@@ -442,7 +442,7 @@ int main(int argc, char *argv[])
 
 	if (verbose > 1) {
 		ctx->debug = verbose;
-		ctx->debug_file = stderr;
+		sc_ctx_log_to_file(ctx, "stderr");
 	}
 
 	if (opt_driver != NULL) 
-- 
1.7.1

_______________________________________________
opensc-devel mailing list
opensc-devel@lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc-devel

Reply via email to