From d733d378e795155d2334b2b3e13db2c0327264b2 Mon Sep 17 00:00:00 2001
From: Fredrik Thulin <fredrik@thulin.net>
Date: Wed, 19 Dec 2012 15:57:34 +0100
Subject: [PATCH] Call C_Finalize when client disconnects.

Necessary to close all ongoing sessions.
---
 gck-rpc-dispatch.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/gck-rpc-dispatch.c b/gck-rpc-dispatch.c
index 1186d2e..3f20fc1 100644
--- a/gck-rpc-dispatch.c
+++ b/gck-rpc-dispatch.c
@@ -80,6 +80,9 @@ static DispatchState *pkcs11_dispatchers = NULL;
 /* A mutex to protect the dispatcher list */
 static pthread_mutex_t pkcs11_dispatchers_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+/* To be able to call C_Finalize from call_uninit. */
+static CK_RV rpc_C_Finalize(CallState *);
+
 /* -----------------------------------------------------------------------------
  * LOGGING and DEBUGGING
  */
@@ -180,6 +183,12 @@ static void call_uninit(CallState * cs)
 {
 	assert(cs);
 
+	/* Close any open sessions. Without this, the application won't be able
+	 * to reconnect (possibly after a crash).
+	 */
+	if (cs->req)
+		rpc_C_Finalize(cs);
+
 	call_reset(cs);
 
 	gck_rpc_message_free(cs->req);
@@ -433,7 +442,7 @@ proto_read_attribute_array(CallState * cs, CK_ATTRIBUTE_PTR * result,
 
 	msg = cs->req;
 
-	/* Make sure this is in the rigth order */
+	/* Make sure this is in the right order */
 	assert(!msg->signature || gck_rpc_message_verify_part(msg, "aA"));
 
 	/* Read the number of attributes */
-- 
GitLab