Do initial slot enumeration manually
authorMartin Willi <martin@revosec.ch>
Tue, 20 Jul 2010 10:23:06 +0000 (12:23 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 4 Aug 2010 07:26:22 +0000 (09:26 +0200)
src/libstrongswan/plugins/pkcs11/pkcs11_manager.c
src/libstrongswan/plugins/pkcs11/pkcs11_plugin.c

index 204a954..8ff63fd 100644 (file)
@@ -137,7 +137,7 @@ static void handle_token(lib_entry_t *entry, CK_SLOT_ID slot)
 /**
  * Handle slot changes
  */
-static void handle_slot(lib_entry_t *entry, CK_SLOT_ID slot)
+static void handle_slot(lib_entry_t *entry, CK_SLOT_ID slot, bool hot)
 {
        CK_SLOT_INFO info;
        CK_RV rv;
@@ -155,13 +155,19 @@ static void handle_slot(lib_entry_t *entry, CK_SLOT_ID slot)
                DBG1(DBG_CFG, "  found token in slot '%s':%lu (%s)",
                         entry->lib->get_name(entry->lib), slot, info.slotDescription);
                handle_token(entry, slot);
-               entry->this->cb(entry->this->data, entry->lib, slot, TRUE);
+               if (hot)
+               {
+                       entry->this->cb(entry->this->data, entry->lib, slot, TRUE);
+               }
        }
        else
        {
                DBG1(DBG_CFG, "token removed from slot '%s':%lu (%s)",
                         entry->lib->get_name(entry->lib), slot, info.slotDescription);
-               entry->this->cb(entry->this->data, entry->lib, slot, FALSE);
+               if (hot)
+               {
+                       entry->this->cb(entry->this->data, entry->lib, slot, FALSE);
+               }
        }
 }
 
@@ -191,7 +197,7 @@ static job_requeue_t dispatch_slot_events(lib_entry_t *entry)
        {
                DBG1(DBG_CFG, "error in C_WaitForSlotEvent: %N", ck_rv_names, rv);
        }
-       handle_slot(entry, slot);
+       handle_slot(entry, slot, TRUE);
 
        return JOB_REQUEUE_DIRECT;
 }
@@ -249,7 +255,7 @@ static void query_slots(lib_entry_t *entry)
        {
                for (i = 0; i < count; i++)
                {
-                       handle_slot(entry, slots[i]);
+                       handle_slot(entry, slots[i], FALSE);
                }
                free(slots);
        }
index 68e3076..40970b3 100644 (file)
@@ -126,6 +126,9 @@ METHOD(plugin_t, destroy, void,
 plugin_t *pkcs11_plugin_create()
 {
        private_pkcs11_plugin_t *this;
+       enumerator_t *enumerator;
+       pkcs11_library_t *p11;
+       CK_SLOT_ID slot;
 
        INIT(this,
                .public.plugin.destroy = _destroy,
@@ -133,6 +136,8 @@ plugin_t *pkcs11_plugin_create()
                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
        );
 
+       this->manager = pkcs11_manager_create((void*)token_event_cb, this);
+
        if (lib->settings->get_bool(lib->settings,
                                                        "libstrongswan.plugins.pkcs11.use_hasher", FALSE))
        {
@@ -150,10 +155,15 @@ plugin_t *pkcs11_plugin_create()
                                        (hasher_constructor_t)pkcs11_hasher_create);
        }
 
-       this->manager = pkcs11_manager_create((void*)token_event_cb, this);
-
        lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_ANY,
                                                        (builder_function_t)pkcs11_private_key_connect);
 
+       enumerator = this->manager->create_token_enumerator(this->manager);
+       while (enumerator->enumerate(enumerator, &p11, &slot))
+       {
+               token_event_cb(this, p11, slot, TRUE);
+       }
+       enumerator->destroy(enumerator);
+
        return &this->public.plugin;
 }