| 1 | /* wait-for-card.c - Waiting for smartcard insertion (Poldi) |
| 2 | Copyright (C) 2004, 2005, 2007, 2008 g10 Code GmbH |
| 3 | |
| 4 | This file is part of Poldi. |
| 5 | |
| 6 | Poldi is free software; you can redistribute it and/or modify it |
| 7 | 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. |
| 10 | |
| 11 | Poldi is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <config.h> |
| 21 | |
| 22 | #include <gpg-error.h> |
| 23 | #include <time.h> |
| 24 | |
| 25 | #include "scd.h" |
| 26 | |
| 27 | |
| 28 | |
| 29 | /* Wait for insertion of a card in slot specified by SLOT, |
| 30 | communication with the user through the PAM conversation function |
| 31 | CONV. |
| 32 | |
| 33 | Returns proper error code. */ |
| 34 | gpg_error_t |
| 35 | wait_for_card (scd_context_t ctx, unsigned int timeout) |
| 36 | { |
| 37 | gpg_error_t err; /* <- rc? */ |
| 38 | time_t t0; |
| 39 | time_t t; |
| 40 | |
| 41 | if (timeout) |
| 42 | time (&t0); |
| 43 | |
| 44 | err = 0; |
| 45 | |
| 46 | while (1) |
| 47 | { |
| 48 | err = scd_serialno (ctx, NULL); |
| 49 | |
| 50 | if (err == 0) |
| 51 | /* Card present! */ |
| 52 | break; |
| 53 | else if (gpg_err_code (err) == GPG_ERR_CARD_NOT_PRESENT) |
| 54 | |
| 55 | { |
| 56 | /* Card not present. */ |
| 57 | |
| 58 | /* FIXME: are there error codes besides |
| 59 | GPG_ERR_CARD_NOT_PRESENT, which can be thrown in case a |
| 60 | smartcard is not currently inserted? */ |
| 61 | |
| 62 | #ifdef HAVE_NANOSLEEP |
| 63 | { |
| 64 | /* Wait 500ms. */ |
| 65 | struct timespec augenblick; |
| 66 | |
| 67 | augenblick.tv_sec = 0; |
| 68 | augenblick.tv_nsec = 500000000; |
| 69 | nanosleep (&augenblick, NULL); |
| 70 | } |
| 71 | #else |
| 72 | sleep (1); |
| 73 | #endif |
| 74 | |
| 75 | if (timeout) |
| 76 | { |
| 77 | time (&t); |
| 78 | if ((t - t0) > timeout) |
| 79 | { |
| 80 | err = GPG_ERR_CARD_NOT_PRESENT; |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | else |
| 86 | /* Unexpected different error -> stop waiting and propagate |
| 87 | error upwards. */ |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | return err; |
| 92 | } |