From cb0f5bbd6ec2437609eb9b12cd77ab5fdf6670ad Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sat, 6 Jan 2007 08:07:00 -0500 Subject: [PATCH] send_to_daemon() cleanup. --- libpwmd/TODO | 2 ++ libpwmd/libpwmd.c | 51 +++++++++++++++++++-------------------------------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/libpwmd/TODO b/libpwmd/TODO index d17aefb6..9d1ef5a0 100644 --- a/libpwmd/TODO +++ b/libpwmd/TODO @@ -1,3 +1,5 @@ Communicate with the pinentry program directly and remove the libassuan and gpg-agent dependencies (???). Not sure whether to do this because gpg-agent does a good job of handling multiple connections and error checking. + +Nonblocking send() and recv()? diff --git a/libpwmd/libpwmd.c b/libpwmd/libpwmd.c index 90f9f460..fa69af87 100644 --- a/libpwmd/libpwmd.c +++ b/libpwmd/libpwmd.c @@ -333,51 +333,38 @@ static int send_to_daemon(pwm_t *pwm, char *fmt, ...) char *buf; fd_set fds; int n; - size_t sent = 0; size_t bufsize = 0; + int ret = 1; va_start(ap, fmt); bufsize = vasprintf(&buf, fmt, ap); va_end(ap); bufsize++; + FD_ZERO(&fds); + FD_SET(pwm->fd, &fds); + n = select(pwm->fd + 1, NULL, &fds, NULL, NULL); - while (1) { - FD_ZERO(&fds); - FD_SET(pwm->fd, &fds); - n = select(pwm->fd + 1, NULL, &fds, NULL, NULL); - - if (n == -1) { - secure_free(buf, bufsize); - return 1; - } - - if (FD_ISSET(pwm->fd, &fds)) { - size_t t = strlen(buf); - ssize_t len = send(pwm->fd, buf, t, 0); - - if (len == -1) { - secure_free(buf, bufsize); - return 1; - } + if (n == -1) { + ret = 1; + goto done; + } - sent += len; + if (FD_ISSET(pwm->fd, &fds)) { + size_t t = strlen(buf); + ssize_t len = send(pwm->fd, buf, t, 0); - /* - * Keep sending data to the socket until the entire buffer has - * been sent. - */ - if (sent < t) { - memmove(&(buf[0]), buf + len, t - len); - buf[t - len] = 0; - continue; - } - - break; + if (len == -1) { + ret = 1; + goto done; } + + if (len == t) + ret = 0; } +done: secure_free(buf, bufsize); - return 0; + return ret; } static char **parse_list_command(char *str) -- 2.11.4.GIT