1 /* app.c - Application selection.
2 * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
29 #include "app-common.h"
34 static npth_mutex_t app_list_lock;
38 print_progress_line (void *opaque, const char *what, int pc, int cur, int tot)
45 snprintf (line, sizeof line, "%s %c %d %d", what, pc, cur, tot);
46 send_status_direct (ctrl, "PROGRESS", line);
51 /* Lock the reader SLOT. This function shall be used right before
52 calling any of the actual application functions to serialize access
53 to the reader. We do this always even if the reader is not
54 actually used. This allows an actual connection to assume that it
55 never shares a reader (while performing one command). Returns 0 on
56 success; only then the unlock_reader function must be called after
57 returning from the handler. */
59 lock_app (app_t app, ctrl_t ctrl)
61 if (npth_mutex_lock (&app->lock))
63 gpg_error_t err = gpg_error_from_syserror ();
64 log_error ("failed to acquire APP lock for %p: %s\n",
65 app, gpg_strerror (err));
69 apdu_set_progress_cb (app->slot, print_progress_line, ctrl);
74 /* Release a lock on the reader. See lock_reader(). */
76 unlock_app (app_t app)
78 apdu_set_progress_cb (app->slot, NULL, NULL);
80 if (npth_mutex_unlock (&app->lock))
82 gpg_error_t err = gpg_error_from_syserror ();
83 log_error ("failed to release APP lock for %p: %s\n",
84 app, gpg_strerror (err));
89 /* This function may be called to print information pertaining to the
90 current state of this module to the log. */
96 npth_mutex_lock (&app_list_lock);
97 for (a = app_top; a; a = a->next)
98 log_info ("app_dump_state: app=%p type='%s'\n", a, a->apptype);
99 npth_mutex_unlock (&app_list_lock);
102 /* Check wether the application NAME is allowed. This does not mean
103 we have support for it though. */
105 is_app_allowed (const char *name)
109 for (l=opt.disabled_applications; l; l = l->next)
110 if (!strcmp (l->d, name))
117 check_conflict (app_t app, const char *name)
119 if (!app || !name || (app->apptype && !ascii_strcasecmp (app->apptype, name)))
122 log_info ("application '%s' in use - can't switch\n",
123 app->apptype? app->apptype : "<null>");
125 return gpg_error (GPG_ERR_CONFLICT);
128 /* This function is used by the serialno command to check for an
129 application conflict which may appear if the serialno command is
130 used to request a specific application and the connection has
131 already done a select_application. */
133 check_application_conflict (const char *name, app_t app)
135 return check_conflict (app, name);
140 app_reset (app_t app, ctrl_t ctrl, int send_reset)
148 lock_app (app, ctrl);
149 sw = apdu_reset (app->slot);
151 err = gpg_error (GPG_ERR_CARD_RESET);
153 app->reset_requested = 1;
156 scd_kick_the_loop ();
161 ctrl->app_ctx = NULL;
162 release_application (app, 0);
169 app_new_register (int slot, ctrl_t ctrl, const char *name)
173 unsigned char *result = NULL;
177 /* Need to allocate a new one. */
178 app = xtrycalloc (1, sizeof *app);
181 err = gpg_error_from_syserror ();
182 log_info ("error allocating context: %s\n", gpg_strerror (err));
187 app->card_status = (unsigned int)-1;
189 if (npth_mutex_init (&app->lock, NULL))
191 err = gpg_error_from_syserror ();
192 log_error ("error initializing mutex: %s\n", gpg_strerror (err));
197 err = lock_app (app, ctrl);
204 want_undefined = (name && !strcmp (name, "undefined"));
206 /* Try to read the GDO file first to get a default serial number.
207 We skip this if the undefined application has been requested. */
210 err = iso7816_select_file (slot, 0x3F00, 1, NULL, NULL);
212 err = iso7816_select_file (slot, 0x2F02, 0, NULL, NULL);
214 err = iso7816_read_binary (slot, 0, 0, &result, &resultlen);
218 const unsigned char *p;
220 p = find_tlv_unchecked (result, resultlen, 0x5A, &n);
222 resultlen -= (p-result);
223 if (p && n > resultlen && n == 0x0d && resultlen+1 == n)
225 /* The object it does not fit into the buffer. This is an
226 invalid encoding (or the buffer is too short. However, I
227 have some test cards with such an invalid encoding and
228 therefore I use this ugly workaround to return something
229 I can further experiment with. */
230 log_info ("enabling BMI testcard workaround\n");
234 if (p && n <= resultlen)
236 /* The GDO file is pretty short, thus we simply reuse it for
237 storing the serial number. */
238 memmove (result, p, n);
239 app->serialno = result;
240 app->serialnolen = n;
241 err = app_munge_serialno (app);
251 /* For certain error codes, there is no need to try more. */
252 if (gpg_err_code (err) == GPG_ERR_CARD_NOT_PRESENT
253 || gpg_err_code (err) == GPG_ERR_ENODEV)
256 /* Figure out the application to use. */
259 /* We switch to the "undefined" application only if explicitly
261 app->apptype = "UNDEFINED";
265 err = gpg_error (GPG_ERR_NOT_FOUND);
267 if (err && is_app_allowed ("openpgp")
268 && (!name || !strcmp (name, "openpgp")))
269 err = app_select_openpgp (app);
270 if (err && is_app_allowed ("nks") && (!name || !strcmp (name, "nks")))
271 err = app_select_nks (app);
272 if (err && is_app_allowed ("p15") && (!name || !strcmp (name, "p15")))
273 err = app_select_p15 (app);
274 if (err && is_app_allowed ("geldkarte")
275 && (!name || !strcmp (name, "geldkarte")))
276 err = app_select_geldkarte (app);
277 if (err && is_app_allowed ("dinsig") && (!name || !strcmp (name, "dinsig")))
278 err = app_select_dinsig (app);
279 if (err && is_app_allowed ("sc-hsm") && (!name || !strcmp (name, "sc-hsm")))
280 err = app_select_sc_hsm (app);
281 if (err && name && gpg_err_code (err) != GPG_ERR_OBJ_TERM_STATE)
282 err = gpg_error (GPG_ERR_NOT_SUPPORTED);
288 log_info ("can't select application '%s': %s\n",
289 name, gpg_strerror (err));
291 log_info ("no supported card application found: %s\n",
298 app->require_get_status = 1; /* For token, this can be 0. */
300 npth_mutex_lock (&app_list_lock);
303 npth_mutex_unlock (&app_list_lock);
308 /* If called with NAME as NULL, select the best fitting application
309 and return a context; otherwise select the application with NAME
310 and return a context. Returns an error code and stores NULL at
311 R_APP if no application was found or no card is present. */
313 select_application (ctrl_t ctrl, const char *name, app_t *r_app,
314 int scan, const unsigned char *serialno_bin,
315 size_t serialno_bin_len)
322 if (scan || !app_top)
326 err = apdu_dev_list_start (opt.reader_port, &l);
335 slot = apdu_open_reader (l, !app_top);
340 sw = apdu_connect (slot);
342 if (sw == SW_HOST_CARD_INACTIVE)
345 sw = apdu_reset (slot);
348 if (!sw || sw == SW_HOST_ALREADY_CONNECTED)
350 else if (sw == SW_HOST_NO_CARD)
351 err = gpg_error (GPG_ERR_CARD_NOT_PRESENT);
353 err = gpg_error (GPG_ERR_ENODEV);
356 err = app_new_register (slot, ctrl, name);
359 /* We close a reader with no card. */
360 apdu_close_reader (slot);
364 apdu_dev_list_finish (l);
365 scd_kick_the_loop ();
368 npth_mutex_lock (&app_list_lock);
369 for (a = app_top; a; a = a->next)
372 if (serialno_bin == NULL)
374 if (a->serialnolen == serialno_bin_len
375 && !memcmp (a->serialno, serialno_bin, a->serialnolen))
382 err = check_conflict (a, name);
391 err = gpg_error (GPG_ERR_ENODEV);
393 npth_mutex_unlock (&app_list_lock);
400 get_supported_applications (void)
402 const char *list[] = {
409 /* Note: "undefined" is not listed here because it needs special
410 treatment by the client. */
417 for (nbytes=1, idx=0; list[idx]; idx++)
418 nbytes += strlen (list[idx]) + 1 + 1;
420 buffer = xtrymalloc (nbytes);
424 for (p=buffer, idx=0; list[idx]; idx++)
425 if (is_app_allowed (list[idx]))
426 p = stpcpy (stpcpy (p, list[idx]), ":\n");
433 /* Deallocate the application. */
435 deallocate_app (app_t app)
437 app_t a, a_prev = NULL;
439 for (a = app_top; a; a = a->next)
445 a_prev->next = a->next;
452 log_error ("trying to release context used yet (%d)\n", app->ref_count);
456 app->fnc.deinit (app);
457 app->fnc.deinit = NULL;
460 xfree (app->serialno);
466 /* Free the resources associated with the application APP. APP is
467 allowed to be NULL in which case this is a no-op. Note that we are
468 using reference counting to track the users of the application and
469 actually deferring the deallocation to allow for a later reuse by
472 release_application (app_t app, int locked_already)
477 /* We don't deallocate app here. Instead, we keep it. This is
478 useful so that a card does not get reset even if only one session
479 is using the card - this way the PIN cache and other cached data
483 lock_app (app, NULL);
486 log_bug ("trying to release an already released context\n");
495 /* The serial number may need some cosmetics. Do it here. This
496 function shall only be called once after a new serial number has
497 been put into APP->serialno.
501 FF 00 00 = For serial numbers starting with an FF
502 FF 01 00 = Some german p15 cards return an empty serial number so the
503 serial number from the EF(TokenInfo) is used instead.
504 FF 7F 00 = No serialno.
506 All other serial number not starting with FF are used as they are.
509 app_munge_serialno (app_t app)
511 if (app->serialnolen && app->serialno[0] == 0xff)
513 /* The serial number starts with our special prefix. This
514 requires that we put our default prefix "FF0000" in front. */
515 unsigned char *p = xtrymalloc (app->serialnolen + 3);
517 return gpg_error_from_syserror ();
518 memcpy (p, "\xff\0", 3);
519 memcpy (p+3, app->serialno, app->serialnolen);
520 app->serialnolen += 3;
521 xfree (app->serialno);
524 else if (!app->serialnolen)
526 unsigned char *p = xtrymalloc (3);
528 return gpg_error_from_syserror ();
529 memcpy (p, "\xff\x7f", 3);
530 app->serialnolen = 3;
531 xfree (app->serialno);
539 /* Retrieve the serial number of the card. The serial number is
540 returned as a malloced string (hex encoded) in SERIAL. Caller must
541 free SERIAL unless the function returns an error. */
543 app_get_serialno (app_t app)
550 if (!app->serialnolen)
551 serial = xtrystrdup ("FF7F00");
553 serial = bin2hex (app->serialno, app->serialnolen, NULL);
559 /* Write out the application specifig status lines for the LEARN
562 app_write_learn_status (app_t app, ctrl_t ctrl, unsigned int flags)
567 return gpg_error (GPG_ERR_INV_VALUE);
568 if (!app->fnc.learn_status)
569 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
571 /* We do not send APPTYPE if only keypairinfo is requested. */
572 if (app->apptype && !(flags & 1))
573 send_status_direct (ctrl, "APPTYPE", app->apptype);
574 err = lock_app (app, ctrl);
577 err = app->fnc.learn_status (app, ctrl, flags);
583 /* Read the certificate with id CERTID (as returned by learn_status in
584 the CERTINFO status lines) and return it in the freshly allocated
585 buffer put into CERT and the length of the certificate put into
588 app_readcert (app_t app, ctrl_t ctrl, const char *certid,
589 unsigned char **cert, size_t *certlen)
594 return gpg_error (GPG_ERR_INV_VALUE);
596 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
597 if (!app->fnc.readcert)
598 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
599 err = lock_app (app, ctrl);
602 err = app->fnc.readcert (app, certid, cert, certlen);
608 /* Read the key with ID KEYID. On success a canonical encoded
609 S-expression with the public key will get stored at PK and its
610 length (for assertions) at PKLEN; the caller must release that
611 buffer. On error NULL will be stored at PK and PKLEN and an error
614 This function might not be supported by all applications. */
616 app_readkey (app_t app, ctrl_t ctrl, int advanced, const char *keyid,
617 unsigned char **pk, size_t *pklen)
626 if (!app || !keyid || !pk || !pklen)
627 return gpg_error (GPG_ERR_INV_VALUE);
629 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
630 if (!app->fnc.readkey)
631 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
632 err = lock_app (app, ctrl);
635 err= app->fnc.readkey (app, advanced, keyid, pk, pklen);
641 /* Perform a GETATTR operation. */
643 app_getattr (app_t app, ctrl_t ctrl, const char *name)
647 if (!app || !name || !*name)
648 return gpg_error (GPG_ERR_INV_VALUE);
650 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
652 if (app->apptype && name && !strcmp (name, "APPTYPE"))
654 send_status_direct (ctrl, "APPTYPE", app->apptype);
657 if (name && !strcmp (name, "SERIALNO"))
661 serial = app_get_serialno (app);
663 return gpg_error (GPG_ERR_INV_VALUE);
665 send_status_direct (ctrl, "SERIALNO", serial);
670 if (!app->fnc.getattr)
671 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
672 err = lock_app (app, ctrl);
675 err = app->fnc.getattr (app, ctrl, name);
680 /* Perform a SETATTR operation. */
682 app_setattr (app_t app, ctrl_t ctrl, const char *name,
683 gpg_error_t (*pincb)(void*, const char *, char **),
685 const unsigned char *value, size_t valuelen)
689 if (!app || !name || !*name || !value)
690 return gpg_error (GPG_ERR_INV_VALUE);
692 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
693 if (!app->fnc.setattr)
694 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
695 err = lock_app (app, ctrl);
698 err = app->fnc.setattr (app, name, pincb, pincb_arg, value, valuelen);
703 /* Create the signature and return the allocated result in OUTDATA.
704 If a PIN is required the PINCB will be used to ask for the PIN; it
705 should return the PIN in an allocated buffer and put it into PIN. */
707 app_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo,
708 gpg_error_t (*pincb)(void*, const char *, char **),
710 const void *indata, size_t indatalen,
711 unsigned char **outdata, size_t *outdatalen )
715 if (!app || !indata || !indatalen || !outdata || !outdatalen || !pincb)
716 return gpg_error (GPG_ERR_INV_VALUE);
718 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
720 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
721 err = lock_app (app, ctrl);
724 err = app->fnc.sign (app, keyidstr, hashalgo,
727 outdata, outdatalen);
730 log_info ("operation sign result: %s\n", gpg_strerror (err));
734 /* Create the signature using the INTERNAL AUTHENTICATE command and
735 return the allocated result in OUTDATA. If a PIN is required the
736 PINCB will be used to ask for the PIN; it should return the PIN in
737 an allocated buffer and put it into PIN. */
739 app_auth (app_t app, ctrl_t ctrl, const char *keyidstr,
740 gpg_error_t (*pincb)(void*, const char *, char **),
742 const void *indata, size_t indatalen,
743 unsigned char **outdata, size_t *outdatalen )
747 if (!app || !indata || !indatalen || !outdata || !outdatalen || !pincb)
748 return gpg_error (GPG_ERR_INV_VALUE);
750 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
752 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
753 err = lock_app (app, ctrl);
756 err = app->fnc.auth (app, keyidstr,
759 outdata, outdatalen);
762 log_info ("operation auth result: %s\n", gpg_strerror (err));
767 /* Decrypt the data in INDATA and return the allocated result in OUTDATA.
768 If a PIN is required the PINCB will be used to ask for the PIN; it
769 should return the PIN in an allocated buffer and put it into PIN. */
771 app_decipher (app_t app, ctrl_t ctrl, const char *keyidstr,
772 gpg_error_t (*pincb)(void*, const char *, char **),
774 const void *indata, size_t indatalen,
775 unsigned char **outdata, size_t *outdatalen,
776 unsigned int *r_info)
782 if (!app || !indata || !indatalen || !outdata || !outdatalen || !pincb)
783 return gpg_error (GPG_ERR_INV_VALUE);
785 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
786 if (!app->fnc.decipher)
787 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
788 err = lock_app (app, ctrl);
791 err = app->fnc.decipher (app, keyidstr,
798 log_info ("operation decipher result: %s\n", gpg_strerror (err));
803 /* Perform the WRITECERT operation. */
805 app_writecert (app_t app, ctrl_t ctrl,
806 const char *certidstr,
807 gpg_error_t (*pincb)(void*, const char *, char **),
809 const unsigned char *data, size_t datalen)
813 if (!app || !certidstr || !*certidstr || !pincb)
814 return gpg_error (GPG_ERR_INV_VALUE);
816 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
817 if (!app->fnc.writecert)
818 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
819 err = lock_app (app, ctrl);
822 err = app->fnc.writecert (app, ctrl, certidstr,
823 pincb, pincb_arg, data, datalen);
826 log_info ("operation writecert result: %s\n", gpg_strerror (err));
831 /* Perform the WRITEKEY operation. */
833 app_writekey (app_t app, ctrl_t ctrl,
834 const char *keyidstr, unsigned int flags,
835 gpg_error_t (*pincb)(void*, const char *, char **),
837 const unsigned char *keydata, size_t keydatalen)
841 if (!app || !keyidstr || !*keyidstr || !pincb)
842 return gpg_error (GPG_ERR_INV_VALUE);
844 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
845 if (!app->fnc.writekey)
846 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
847 err = lock_app (app, ctrl);
850 err = app->fnc.writekey (app, ctrl, keyidstr, flags,
851 pincb, pincb_arg, keydata, keydatalen);
854 log_info ("operation writekey result: %s\n", gpg_strerror (err));
859 /* Perform a SETATTR operation. */
861 app_genkey (app_t app, ctrl_t ctrl, const char *keynostr, unsigned int flags,
863 gpg_error_t (*pincb)(void*, const char *, char **),
868 if (!app || !keynostr || !*keynostr || !pincb)
869 return gpg_error (GPG_ERR_INV_VALUE);
871 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
872 if (!app->fnc.genkey)
873 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
874 err = lock_app (app, ctrl);
877 err = app->fnc.genkey (app, ctrl, keynostr, flags,
878 createtime, pincb, pincb_arg);
881 log_info ("operation genkey result: %s\n", gpg_strerror (err));
886 /* Perform a GET CHALLENGE operation. This function is special as it
887 directly accesses the card without any application specific
890 app_get_challenge (app_t app, ctrl_t ctrl, size_t nbytes, unsigned char *buffer)
894 if (!app || !nbytes || !buffer)
895 return gpg_error (GPG_ERR_INV_VALUE);
897 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
898 err = lock_app (app, ctrl);
901 err = iso7816_get_challenge (app->slot, nbytes, buffer);
908 /* Perform a CHANGE REFERENCE DATA or RESET RETRY COUNTER operation. */
910 app_change_pin (app_t app, ctrl_t ctrl, const char *chvnostr, int reset_mode,
911 gpg_error_t (*pincb)(void*, const char *, char **),
916 if (!app || !chvnostr || !*chvnostr || !pincb)
917 return gpg_error (GPG_ERR_INV_VALUE);
919 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
920 if (!app->fnc.change_pin)
921 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
922 err = lock_app (app, ctrl);
925 err = app->fnc.change_pin (app, ctrl, chvnostr, reset_mode,
929 log_info ("operation change_pin result: %s\n", gpg_strerror (err));
934 /* Perform a VERIFY operation without doing anything lese. This may
935 be used to initialze a the PIN cache for long lasting other
936 operations. Its use is highly application dependent. */
938 app_check_pin (app_t app, ctrl_t ctrl, const char *keyidstr,
939 gpg_error_t (*pincb)(void*, const char *, char **),
944 if (!app || !keyidstr || !*keyidstr || !pincb)
945 return gpg_error (GPG_ERR_INV_VALUE);
947 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
948 if (!app->fnc.check_pin)
949 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
950 err = lock_app (app, ctrl);
953 err = app->fnc.check_pin (app, keyidstr, pincb, pincb_arg);
956 log_info ("operation check_pin result: %s\n", gpg_strerror (err));
961 report_change (int slot, int old_status, int cur_status)
963 char *homestr, *envstr;
968 snprintf (templ, sizeof templ, "reader_%d.status", slot);
969 fname = make_filename (gnupg_homedir (), templ, NULL );
970 fp = fopen (fname, "w");
974 (cur_status & 1)? "USABLE":
975 (cur_status & 4)? "ACTIVE":
976 (cur_status & 2)? "PRESENT": "NOCARD");
981 homestr = make_filename (gnupg_homedir (), NULL);
982 if (gpgrt_asprintf (&envstr, "GNUPGHOME=%s", homestr) < 0)
983 log_error ("out of core while building environment\n");
987 const char *args[9], *envs[2];
988 char numbuf1[30], numbuf2[30], numbuf3[30];
993 sprintf (numbuf1, "%d", slot);
994 sprintf (numbuf2, "0x%04X", old_status);
995 sprintf (numbuf3, "0x%04X", cur_status);
996 args[0] = "--reader-port";
998 args[2] = "--old-code";
1000 args[4] = "--new-code";
1002 args[6] = "--status";
1003 args[7] = ((cur_status & 1)? "USABLE":
1004 (cur_status & 4)? "ACTIVE":
1005 (cur_status & 2)? "PRESENT": "NOCARD");
1008 fname = make_filename (gnupg_homedir (), "scd-event", NULL);
1009 err = gnupg_spawn_process_detached (fname, args, envs);
1010 if (err && gpg_err_code (err) != GPG_ERR_ENOENT)
1011 log_error ("failed to run event handler '%s': %s\n",
1012 fname, gpg_strerror (err));
1020 scd_update_reader_status_file (void)
1024 npth_mutex_lock (&app_list_lock);
1025 for (a = app_top; a; a = app_next)
1027 unsigned int status;
1032 if (a->reset_requested)
1037 sw = apdu_get_status (a->slot, 0, &status);
1039 if (sw == SW_HOST_NO_READER)
1041 /* Most likely the _reader_ has been unplugged. */
1046 /* Get status failed. Ignore that. */
1052 if (a->card_status != status)
1054 report_change (a->slot, a->card_status, status);
1055 send_client_notifications (a, status == 0);
1059 log_debug ("Removal of a card: %d\n", a->slot);
1060 apdu_close_reader (a->slot);
1065 a->card_status = status;
1072 npth_mutex_unlock (&app_list_lock);
1075 /* This function must be called once to initialize this module. This
1076 has to be done before a second thread is spawned. We can't do the
1077 static initialization because Pth emulation code might not be able
1078 to do a static init; in particular, it is not possible for W32. */
1080 initialize_module_command (void)
1084 if (npth_mutex_init (&app_list_lock, NULL))
1086 err = gpg_error_from_syserror ();
1087 log_error ("app: error initializing mutex: %s\n", gpg_strerror (err));
1091 return apdu_init ();
1095 app_list_start (void)
1097 npth_mutex_lock (&app_list_lock);
1102 app_list_finish (void)
1104 npth_mutex_unlock (&app_list_lock);
1108 app_send_card_list (ctrl_t ctrl)
1113 npth_mutex_lock (&app_list_lock);
1114 for (a = app_top; a; a = a->next)
1116 if (DIM (buf) < 2 * a->serialnolen + 1)
1119 bin2hex (a->serialno, a->serialnolen, buf);
1120 send_status_direct (ctrl, "SERIALNO", buf);
1122 npth_mutex_unlock (&app_list_lock);