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/>.
28 #include "../common/exechelp.h"
29 #include "app-common.h"
32 #include "../common/tlv.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 whether 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,
170 int periodical_check_needed)
174 unsigned char *result = NULL;
178 /* Need to allocate a new one. */
179 app = xtrycalloc (1, sizeof *app);
182 err = gpg_error_from_syserror ();
183 log_info ("error allocating context: %s\n", gpg_strerror (err));
188 app->card_status = (unsigned int)-1;
190 if (npth_mutex_init (&app->lock, NULL))
192 err = gpg_error_from_syserror ();
193 log_error ("error initializing mutex: %s\n", gpg_strerror (err));
198 err = lock_app (app, ctrl);
205 want_undefined = (name && !strcmp (name, "undefined"));
207 /* Try to read the GDO file first to get a default serial number.
208 We skip this if the undefined application has been requested. */
211 err = iso7816_select_file (slot, 0x3F00, 1);
213 err = iso7816_select_file (slot, 0x2F02, 0);
215 err = iso7816_read_binary (slot, 0, 0, &result, &resultlen);
219 const unsigned char *p;
221 p = find_tlv_unchecked (result, resultlen, 0x5A, &n);
223 resultlen -= (p-result);
224 if (p && n > resultlen && n == 0x0d && resultlen+1 == n)
226 /* The object it does not fit into the buffer. This is an
227 invalid encoding (or the buffer is too short. However, I
228 have some test cards with such an invalid encoding and
229 therefore I use this ugly workaround to return something
230 I can further experiment with. */
231 log_info ("enabling BMI testcard workaround\n");
235 if (p && n <= resultlen)
237 /* The GDO file is pretty short, thus we simply reuse it for
238 storing the serial number. */
239 memmove (result, p, n);
240 app->serialno = result;
241 app->serialnolen = n;
242 err = app_munge_serialno (app);
252 /* For certain error codes, there is no need to try more. */
253 if (gpg_err_code (err) == GPG_ERR_CARD_NOT_PRESENT
254 || gpg_err_code (err) == GPG_ERR_ENODEV)
257 /* Figure out the application to use. */
260 /* We switch to the "undefined" application only if explicitly
262 app->apptype = "UNDEFINED";
266 err = gpg_error (GPG_ERR_NOT_FOUND);
268 if (err && is_app_allowed ("openpgp")
269 && (!name || !strcmp (name, "openpgp")))
270 err = app_select_openpgp (app);
271 if (err && is_app_allowed ("nks") && (!name || !strcmp (name, "nks")))
272 err = app_select_nks (app);
273 if (err && is_app_allowed ("p15") && (!name || !strcmp (name, "p15")))
274 err = app_select_p15 (app);
275 if (err && is_app_allowed ("geldkarte")
276 && (!name || !strcmp (name, "geldkarte")))
277 err = app_select_geldkarte (app);
278 if (err && is_app_allowed ("dinsig") && (!name || !strcmp (name, "dinsig")))
279 err = app_select_dinsig (app);
280 if (err && is_app_allowed ("sc-hsm") && (!name || !strcmp (name, "sc-hsm")))
281 err = app_select_sc_hsm (app);
282 if (err && name && gpg_err_code (err) != GPG_ERR_OBJ_TERM_STATE)
283 err = gpg_error (GPG_ERR_NOT_SUPPORTED);
289 log_info ("can't select application '%s': %s\n",
290 name, gpg_strerror (err));
292 log_info ("no supported card application found: %s\n",
299 app->periodical_check_needed = periodical_check_needed;
301 npth_mutex_lock (&app_list_lock);
304 npth_mutex_unlock (&app_list_lock);
309 /* If called with NAME as NULL, select the best fitting application
310 and return a context; otherwise select the application with NAME
311 and return a context. Returns an error code and stores NULL at
312 R_APP if no application was found or no card is present. */
314 select_application (ctrl_t ctrl, const char *name, app_t *r_app,
315 int scan, const unsigned char *serialno_bin,
316 size_t serialno_bin_len)
319 app_t a, a_prev = NULL;
323 if (scan || !app_top)
328 /* Scan the devices to find new device(s). */
329 err = apdu_dev_list_start (opt.reader_port, &l);
336 int periodical_check_needed_this;
338 slot = apdu_open_reader (l, !app_top);
342 periodical_check_needed_this = apdu_connect (slot);
343 if (periodical_check_needed_this < 0)
345 /* We close a reader with no card. */
346 err = gpg_error (GPG_ERR_ENODEV);
350 err = app_new_register (slot, ctrl, name,
351 periodical_check_needed_this);
356 apdu_close_reader (slot);
359 apdu_dev_list_finish (l);
361 /* If new device(s), kick the scdaemon loop. */
363 scd_kick_the_loop ();
366 npth_mutex_lock (&app_list_lock);
367 for (a = app_top; a; a = a->next)
370 if (serialno_bin == NULL)
372 if (a->serialnolen == serialno_bin_len
373 && !memcmp (a->serialno, serialno_bin, a->serialnolen))
381 err = check_conflict (a, name);
388 a_prev->next = a->next;
396 err = gpg_error (GPG_ERR_ENODEV);
398 npth_mutex_unlock (&app_list_lock);
405 get_supported_applications (void)
407 const char *list[] = {
414 /* Note: "undefined" is not listed here because it needs special
415 treatment by the client. */
422 for (nbytes=1, idx=0; list[idx]; idx++)
423 nbytes += strlen (list[idx]) + 1 + 1;
425 buffer = xtrymalloc (nbytes);
429 for (p=buffer, idx=0; list[idx]; idx++)
430 if (is_app_allowed (list[idx]))
431 p = stpcpy (stpcpy (p, list[idx]), ":\n");
438 /* Deallocate the application. */
440 deallocate_app (app_t app)
442 app_t a, a_prev = NULL;
444 for (a = app_top; a; a = a->next)
450 a_prev->next = a->next;
457 log_error ("trying to release context used yet (%d)\n", app->ref_count);
461 app->fnc.deinit (app);
462 app->fnc.deinit = NULL;
465 xfree (app->serialno);
471 /* Free the resources associated with the application APP. APP is
472 allowed to be NULL in which case this is a no-op. Note that we are
473 using reference counting to track the users of the application and
474 actually deferring the deallocation to allow for a later reuse by
477 release_application (app_t app, int locked_already)
482 /* We don't deallocate app here. Instead, we keep it. This is
483 useful so that a card does not get reset even if only one session
484 is using the card - this way the PIN cache and other cached data
488 lock_app (app, NULL);
491 log_bug ("trying to release an already released context\n");
500 /* The serial number may need some cosmetics. Do it here. This
501 function shall only be called once after a new serial number has
502 been put into APP->serialno.
506 FF 00 00 = For serial numbers starting with an FF
507 FF 01 00 = Some german p15 cards return an empty serial number so the
508 serial number from the EF(TokenInfo) is used instead.
509 FF 7F 00 = No serialno.
511 All other serial number not starting with FF are used as they are.
514 app_munge_serialno (app_t app)
516 if (app->serialnolen && app->serialno[0] == 0xff)
518 /* The serial number starts with our special prefix. This
519 requires that we put our default prefix "FF0000" in front. */
520 unsigned char *p = xtrymalloc (app->serialnolen + 3);
522 return gpg_error_from_syserror ();
523 memcpy (p, "\xff\0", 3);
524 memcpy (p+3, app->serialno, app->serialnolen);
525 app->serialnolen += 3;
526 xfree (app->serialno);
529 else if (!app->serialnolen)
531 unsigned char *p = xtrymalloc (3);
533 return gpg_error_from_syserror ();
534 memcpy (p, "\xff\x7f", 3);
535 app->serialnolen = 3;
536 xfree (app->serialno);
544 /* Retrieve the serial number of the card. The serial number is
545 returned as a malloced string (hex encoded) in SERIAL. Caller must
546 free SERIAL unless the function returns an error. */
548 app_get_serialno (app_t app)
555 if (!app->serialnolen)
556 serial = xtrystrdup ("FF7F00");
558 serial = bin2hex (app->serialno, app->serialnolen, NULL);
564 /* Write out the application specifig status lines for the LEARN
567 app_write_learn_status (app_t app, ctrl_t ctrl, unsigned int flags)
572 return gpg_error (GPG_ERR_INV_VALUE);
573 if (!app->fnc.learn_status)
574 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
576 /* We do not send APPTYPE if only keypairinfo is requested. */
577 if (app->apptype && !(flags & 1))
578 send_status_direct (ctrl, "APPTYPE", app->apptype);
579 err = lock_app (app, ctrl);
582 err = app->fnc.learn_status (app, ctrl, flags);
588 /* Read the certificate with id CERTID (as returned by learn_status in
589 the CERTINFO status lines) and return it in the freshly allocated
590 buffer put into CERT and the length of the certificate put into
593 app_readcert (app_t app, ctrl_t ctrl, const char *certid,
594 unsigned char **cert, size_t *certlen)
599 return gpg_error (GPG_ERR_INV_VALUE);
601 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
602 if (!app->fnc.readcert)
603 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
604 err = lock_app (app, ctrl);
607 err = app->fnc.readcert (app, certid, cert, certlen);
613 /* Read the key with ID KEYID. On success a canonical encoded
614 S-expression with the public key will get stored at PK and its
615 length (for assertions) at PKLEN; the caller must release that
616 buffer. On error NULL will be stored at PK and PKLEN and an error
619 This function might not be supported by all applications. */
621 app_readkey (app_t app, ctrl_t ctrl, int advanced, const char *keyid,
622 unsigned char **pk, size_t *pklen)
631 if (!app || !keyid || !pk || !pklen)
632 return gpg_error (GPG_ERR_INV_VALUE);
634 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
635 if (!app->fnc.readkey)
636 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
637 err = lock_app (app, ctrl);
640 err= app->fnc.readkey (app, advanced, keyid, pk, pklen);
646 /* Perform a GETATTR operation. */
648 app_getattr (app_t app, ctrl_t ctrl, const char *name)
652 if (!app || !name || !*name)
653 return gpg_error (GPG_ERR_INV_VALUE);
655 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
657 if (app->apptype && name && !strcmp (name, "APPTYPE"))
659 send_status_direct (ctrl, "APPTYPE", app->apptype);
662 if (name && !strcmp (name, "SERIALNO"))
666 serial = app_get_serialno (app);
668 return gpg_error (GPG_ERR_INV_VALUE);
670 send_status_direct (ctrl, "SERIALNO", serial);
675 if (!app->fnc.getattr)
676 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
677 err = lock_app (app, ctrl);
680 err = app->fnc.getattr (app, ctrl, name);
685 /* Perform a SETATTR operation. */
687 app_setattr (app_t app, ctrl_t ctrl, const char *name,
688 gpg_error_t (*pincb)(void*, const char *, char **),
690 const unsigned char *value, size_t valuelen)
694 if (!app || !name || !*name || !value)
695 return gpg_error (GPG_ERR_INV_VALUE);
697 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
698 if (!app->fnc.setattr)
699 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
700 err = lock_app (app, ctrl);
703 err = app->fnc.setattr (app, name, pincb, pincb_arg, value, valuelen);
708 /* Create the signature and return the allocated result in OUTDATA.
709 If a PIN is required the PINCB will be used to ask for the PIN; it
710 should return the PIN in an allocated buffer and put it into PIN. */
712 app_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo,
713 gpg_error_t (*pincb)(void*, const char *, char **),
715 const void *indata, size_t indatalen,
716 unsigned char **outdata, size_t *outdatalen )
720 if (!app || !indata || !indatalen || !outdata || !outdatalen || !pincb)
721 return gpg_error (GPG_ERR_INV_VALUE);
723 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
725 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
726 err = lock_app (app, ctrl);
729 err = app->fnc.sign (app, keyidstr, hashalgo,
732 outdata, outdatalen);
735 log_info ("operation sign result: %s\n", gpg_strerror (err));
739 /* Create the signature using the INTERNAL AUTHENTICATE command and
740 return the allocated result in OUTDATA. If a PIN is required the
741 PINCB will be used to ask for the PIN; it should return the PIN in
742 an allocated buffer and put it into PIN. */
744 app_auth (app_t app, ctrl_t ctrl, const char *keyidstr,
745 gpg_error_t (*pincb)(void*, const char *, char **),
747 const void *indata, size_t indatalen,
748 unsigned char **outdata, size_t *outdatalen )
752 if (!app || !indata || !indatalen || !outdata || !outdatalen || !pincb)
753 return gpg_error (GPG_ERR_INV_VALUE);
755 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
757 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
758 err = lock_app (app, ctrl);
761 err = app->fnc.auth (app, keyidstr,
764 outdata, outdatalen);
767 log_info ("operation auth result: %s\n", gpg_strerror (err));
772 /* Decrypt the data in INDATA and return the allocated result in OUTDATA.
773 If a PIN is required the PINCB will be used to ask for the PIN; it
774 should return the PIN in an allocated buffer and put it into PIN. */
776 app_decipher (app_t app, ctrl_t ctrl, const char *keyidstr,
777 gpg_error_t (*pincb)(void*, const char *, char **),
779 const void *indata, size_t indatalen,
780 unsigned char **outdata, size_t *outdatalen,
781 unsigned int *r_info)
787 if (!app || !indata || !indatalen || !outdata || !outdatalen || !pincb)
788 return gpg_error (GPG_ERR_INV_VALUE);
790 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
791 if (!app->fnc.decipher)
792 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
793 err = lock_app (app, ctrl);
796 err = app->fnc.decipher (app, keyidstr,
803 log_info ("operation decipher result: %s\n", gpg_strerror (err));
808 /* Perform the WRITECERT operation. */
810 app_writecert (app_t app, ctrl_t ctrl,
811 const char *certidstr,
812 gpg_error_t (*pincb)(void*, const char *, char **),
814 const unsigned char *data, size_t datalen)
818 if (!app || !certidstr || !*certidstr || !pincb)
819 return gpg_error (GPG_ERR_INV_VALUE);
821 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
822 if (!app->fnc.writecert)
823 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
824 err = lock_app (app, ctrl);
827 err = app->fnc.writecert (app, ctrl, certidstr,
828 pincb, pincb_arg, data, datalen);
831 log_info ("operation writecert result: %s\n", gpg_strerror (err));
836 /* Perform the WRITEKEY operation. */
838 app_writekey (app_t app, ctrl_t ctrl,
839 const char *keyidstr, unsigned int flags,
840 gpg_error_t (*pincb)(void*, const char *, char **),
842 const unsigned char *keydata, size_t keydatalen)
846 if (!app || !keyidstr || !*keyidstr || !pincb)
847 return gpg_error (GPG_ERR_INV_VALUE);
849 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
850 if (!app->fnc.writekey)
851 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
852 err = lock_app (app, ctrl);
855 err = app->fnc.writekey (app, ctrl, keyidstr, flags,
856 pincb, pincb_arg, keydata, keydatalen);
859 log_info ("operation writekey result: %s\n", gpg_strerror (err));
864 /* Perform a SETATTR operation. */
866 app_genkey (app_t app, ctrl_t ctrl, const char *keynostr, unsigned int flags,
868 gpg_error_t (*pincb)(void*, const char *, char **),
873 if (!app || !keynostr || !*keynostr || !pincb)
874 return gpg_error (GPG_ERR_INV_VALUE);
876 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
877 if (!app->fnc.genkey)
878 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
879 err = lock_app (app, ctrl);
882 err = app->fnc.genkey (app, ctrl, keynostr, flags,
883 createtime, pincb, pincb_arg);
886 log_info ("operation genkey result: %s\n", gpg_strerror (err));
891 /* Perform a GET CHALLENGE operation. This function is special as it
892 directly accesses the card without any application specific
895 app_get_challenge (app_t app, ctrl_t ctrl, size_t nbytes, unsigned char *buffer)
899 if (!app || !nbytes || !buffer)
900 return gpg_error (GPG_ERR_INV_VALUE);
902 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
903 err = lock_app (app, ctrl);
906 err = iso7816_get_challenge (app->slot, nbytes, buffer);
913 /* Perform a CHANGE REFERENCE DATA or RESET RETRY COUNTER operation. */
915 app_change_pin (app_t app, ctrl_t ctrl, const char *chvnostr, int reset_mode,
916 gpg_error_t (*pincb)(void*, const char *, char **),
921 if (!app || !chvnostr || !*chvnostr || !pincb)
922 return gpg_error (GPG_ERR_INV_VALUE);
924 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
925 if (!app->fnc.change_pin)
926 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
927 err = lock_app (app, ctrl);
930 err = app->fnc.change_pin (app, ctrl, chvnostr, reset_mode,
934 log_info ("operation change_pin result: %s\n", gpg_strerror (err));
939 /* Perform a VERIFY operation without doing anything lese. This may
940 be used to initialize a the PIN cache for long lasting other
941 operations. Its use is highly application dependent. */
943 app_check_pin (app_t app, ctrl_t ctrl, const char *keyidstr,
944 gpg_error_t (*pincb)(void*, const char *, char **),
949 if (!app || !keyidstr || !*keyidstr || !pincb)
950 return gpg_error (GPG_ERR_INV_VALUE);
952 return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
953 if (!app->fnc.check_pin)
954 return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
955 err = lock_app (app, ctrl);
958 err = app->fnc.check_pin (app, keyidstr, pincb, pincb_arg);
961 log_info ("operation check_pin result: %s\n", gpg_strerror (err));
966 report_change (int slot, int old_status, int cur_status)
968 char *homestr, *envstr;
973 snprintf (templ, sizeof templ, "reader_%d.status", slot);
974 fname = make_filename (gnupg_homedir (), templ, NULL );
975 fp = fopen (fname, "w");
979 (cur_status & 1)? "USABLE":
980 (cur_status & 4)? "ACTIVE":
981 (cur_status & 2)? "PRESENT": "NOCARD");
986 homestr = make_filename (gnupg_homedir (), NULL);
987 if (gpgrt_asprintf (&envstr, "GNUPGHOME=%s", homestr) < 0)
988 log_error ("out of core while building environment\n");
992 const char *args[9], *envs[2];
993 char numbuf1[30], numbuf2[30], numbuf3[30];
998 sprintf (numbuf1, "%d", slot);
999 sprintf (numbuf2, "0x%04X", old_status);
1000 sprintf (numbuf3, "0x%04X", cur_status);
1001 args[0] = "--reader-port";
1003 args[2] = "--old-code";
1005 args[4] = "--new-code";
1007 args[6] = "--status";
1008 args[7] = ((cur_status & 1)? "USABLE":
1009 (cur_status & 4)? "ACTIVE":
1010 (cur_status & 2)? "PRESENT": "NOCARD");
1013 fname = make_filename (gnupg_homedir (), "scd-event", NULL);
1014 err = gnupg_spawn_process_detached (fname, args, envs);
1015 if (err && gpg_err_code (err) != GPG_ERR_ENOENT)
1016 log_error ("failed to run event handler '%s': %s\n",
1017 fname, gpg_strerror (err));
1025 scd_update_reader_status_file (void)
1028 int periodical_check_needed = 0;
1030 npth_mutex_lock (&app_list_lock);
1031 for (a = app_top; a; a = app_next)
1034 unsigned int status;
1039 if (a->reset_requested)
1043 sw = apdu_get_status (a->slot, 0, &status);
1044 if (sw == SW_HOST_NO_READER)
1046 /* Most likely the _reader_ has been unplugged. */
1051 /* Get status failed. Ignore that. */
1052 if (a->periodical_check_needed)
1053 periodical_check_needed = 1;
1059 if (a->card_status != status)
1061 report_change (a->slot, a->card_status, status);
1062 send_client_notifications (a, status == 0);
1066 log_debug ("Removal of a card: %d\n", a->slot);
1067 apdu_close_reader (a->slot);
1072 a->card_status = status;
1073 if (a->periodical_check_needed)
1074 periodical_check_needed = 1;
1080 if (a->periodical_check_needed)
1081 periodical_check_needed = 1;
1085 npth_mutex_unlock (&app_list_lock);
1087 return periodical_check_needed;
1090 /* This function must be called once to initialize this module. This
1091 has to be done before a second thread is spawned. We can't do the
1092 static initialization because Pth emulation code might not be able
1093 to do a static init; in particular, it is not possible for W32. */
1095 initialize_module_command (void)
1099 if (npth_mutex_init (&app_list_lock, NULL))
1101 err = gpg_error_from_syserror ();
1102 log_error ("app: error initializing mutex: %s\n", gpg_strerror (err));
1106 return apdu_init ();
1110 app_send_card_list (ctrl_t ctrl)
1115 npth_mutex_lock (&app_list_lock);
1116 for (a = app_top; a; a = a->next)
1118 if (DIM (buf) < 2 * a->serialnolen + 1)
1121 bin2hex (a->serialno, a->serialnolen, buf);
1122 send_status_direct (ctrl, "SERIALNO", buf);
1124 npth_mutex_unlock (&app_list_lock);