From 0f26455c95b2e6d2667da49d88c1336f6b3dbf49 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sat, 14 Mar 2009 19:30:21 -0400 Subject: [PATCH] Let pwmd_process() work when not inside a command. This is needed so pending status messages can be parsed. It will return ASYNC_INIT when no (asynchronous) command is running. --- src/libpwmd.c | 56 ++++++-------------------------------------------------- src/pwmc.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/libpwmd.c b/src/libpwmd.c index 99f3e1e5..d0d51602 100644 --- a/src/libpwmd.c +++ b/src/libpwmd.c @@ -987,38 +987,6 @@ gpg_error_t pwmd_save_async(pwm_t *pwm) return do_nb_command(pwm, "SAVE", NULL); } -static gpg_error_t parse_assuan_line(pwm_t *pwm) -{ - gpg_error_t rc; - char *line; - size_t len; - - rc = assuan_read_line(pwm->ctx, &line, &len); - - if (!rc) { - if (line[0] == 'O' && line[1] == 'K' && - (line[2] == 0 || line[2] == ' ')) { - pwm->state = ASYNC_DONE; - } - else if (line[0] == '#') { - } - else if (line[0] == 'S' && (line[1] == 0 || line[1] == ' ')) { - if (pwm->status_func) { - pwm->status_func(pwm->status_data, - line[1] == 0 ? line+1 : line+2); - } - } - else if (line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && - (line[3] == 0 || line[3] == ' ')) { - line += 4; - rc = atoi(line); - pwm->state = ASYNC_DONE; - } - } - - return rc; -} - gpg_error_t pwmd_get_result(pwm_t *pwm, const char **result) { if (!pwm || !result) @@ -1034,6 +1002,8 @@ gpg_error_t pwmd_get_result(pwm_t *pwm, const char **result) return 0; } +static gpg_error_t assuan_command(pwm_t *pwm, assuan_context_t ctx, + char **result, const char *cmd); pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc) { fd_set fds; @@ -1120,26 +1090,12 @@ pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc) else if (pwm->state == ASYNC_DONE) return pwm->state; else if (pwm->state == ASYNC_INIT) { - *rc = GPG_ERR_INV_STATE; - return ASYNC_DONE; - } - - FD_ZERO(&fds); - FD_SET(pwm->fd, &fds); -#ifdef WITH_LIBPTH - n = pth_select(pwm->fd+1, &fds, NULL, NULL, &tv); -#else - n = select(pwm->fd+1, &fds, NULL, NULL, &tv); -#endif - - if (n > 0) { - if (FD_ISSET(pwm->fd, &fds)) - *rc = parse_assuan_line(pwm); + /* This will allow pending status messages to be parsed. */ + *rc = assuan_command(pwm, pwm->ctx, NULL, "NOP"); + return pwm->state; } - while (!*rc && assuan_pending_line(pwm->ctx)) - *rc = parse_assuan_line(pwm); - + /* For pinentry retries. */ if (pwm->is_open_cmd && gpg_err_code(*rc) == EPWMD_BADKEY && ++pwm->ntries < pwm->pinentry_tries) { pwm->state = ASYNC_INIT; diff --git a/src/pwmc.c b/src/pwmc.c index 582564c1..fef33ea8 100644 --- a/src/pwmc.c +++ b/src/pwmc.c @@ -611,9 +611,57 @@ int main(int argc, char *argv[]) goto done; } +#ifdef DEBUG + p = NULL; + + if (method) { + fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); + + for (;;) { + fd_set rfds; + struct timeval tv = {0, 50000}; + int n; + + FD_ZERO(&rfds); + FD_SET(STDIN_FILENO, &rfds); + n = select(STDIN_FILENO+1, &rfds, NULL, NULL, &tv); + + if (n >= 0) { + s = pwmd_process(pwm, &error); + + if (!n) { + fprintf(stderr, "."); + continue; + } + } + + if (n == -1) { + n = errno; + warn("select()"); + error = gpg_error_from_errno(n); + goto done; + } + + if (FD_ISSET(STDIN_FILENO, &rfds)) { + fprintf(stderr, "\n"); + n = read(STDOUT_FILENO, command, sizeof(command)); + + if (n > 0) + command[n-1] = 0; + + command[n] = 0; + p = command; + break; + } + } + } + else + p = fgets(command, sizeof(command), stdin); +#else p = fgets(command, sizeof(command), stdin); +#endif - if (!p) + if (!p || !*p) goto done; /* -- 2.11.4.GIT