From 9d7d4458d94d0aac0a7edf999368eb18f89cb76a Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Mon, 2 Apr 2012 17:26:05 -0400 Subject: Convert read and write operations to sss_atomic_read https://fedorahosted.org/sssd/ticket/1209 --- src/providers/krb5/krb5_child.c | 51 ++++++++++++++++---------------------- src/providers/krb5/krb5_common.c | 26 ++++++++------------ src/providers/ldap/ldap_child.c | 53 ++++++++++++++++------------------------ 3 files changed, 52 insertions(+), 78 deletions(-) (limited to 'src/providers') diff --git a/src/providers/krb5/krb5_child.c b/src/providers/krb5/krb5_child.c index 209643a0..cc29c00f 100644 --- a/src/providers/krb5/krb5_child.c +++ b/src/providers/krb5/krb5_child.c @@ -461,18 +461,20 @@ static errno_t sendresponse(int fd, krb5_error_code kerr, int pam_status, return ENOMEM; } - written = 0; - while (written < resp->size) { - ret = write(fd, resp->buf + written, resp->size - written); - if (ret == -1) { - if (errno == EAGAIN || errno == EINTR) { - continue; - } - ret = errno; - DEBUG(1, ("write failed [%d][%s].\n", ret, strerror(ret))); - return ret; - } - written += ret; + errno = 0; + written = sss_atomic_write_s(fd, resp->buf, resp->size); + if (written == -1) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, + ("write failed [%d][%s].\n", ret, strerror(ret))); + return ret; + } + + if (written != resp->size) { + DEBUG(SSSDBG_CRIT_FAILURE, + ("Write error, wrote [%d] bytes, expected [%d]\n", + written, resp->size)); + return EOK; } return EOK; @@ -1631,25 +1633,14 @@ int main(int argc, const char *argv[]) goto fail; } - while ((ret = read(STDIN_FILENO, buf + len, IN_BUF_SIZE - len)) != 0) { - if (ret == -1) { - if (errno == EINTR || errno == EAGAIN) { - continue; - } - DEBUG(1, ("read failed [%d][%s].\n", errno, strerror(errno))); - goto fail; - } else if (ret > 0) { - len += ret; - if (len > IN_BUF_SIZE) { - DEBUG(1, ("read too much, this should never happen.\n")); - goto fail; - } - continue; - } else { - DEBUG(1, ("unexpected return code of read [%d].\n", ret)); - goto fail; - } + errno = 0; + len = sss_atomic_read_s(STDIN_FILENO, buf, IN_BUF_SIZE); + if (len == -1) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, ("read failed [%d][%s].\n", ret, strerror(ret))); + goto fail; } + close(STDIN_FILENO); kr = talloc_zero(pd, struct krb5_req); diff --git a/src/providers/krb5/krb5_common.c b/src/providers/krb5/krb5_common.c index d33900f1..022745d6 100644 --- a/src/providers/krb5/krb5_common.c +++ b/src/providers/krb5/krb5_common.c @@ -321,25 +321,19 @@ errno_t write_krb5info_file(const char *realm, const char *server, goto done; } - written = 0; - while (written < server_len) { - ret = write(fd, server+written, server_len-written); - if (ret == -1) { - if (errno == EINTR || errno == EAGAIN) { - continue; - } - ret = errno; - DEBUG(1, ("write failed [%d][%s].\n", ret, strerror(ret))); - goto done; - } - else { - written += ret; - } + errno = 0; + written = sss_atomic_write_s(fd, discard_const(server), server_len); + if (written == -1) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, + ("write failed [%d][%s].\n", ret, strerror(ret))); + goto done; } if (written != server_len) { - DEBUG(1, ("Write error, wrote [%d] bytes, expected [%d]\n", - written, server_len)); + DEBUG(SSSDBG_CRIT_FAILURE, + ("Write error, wrote [%d] bytes, expected [%d]\n", + written, server_len)); ret = EIO; goto done; } diff --git a/src/providers/ldap/ldap_child.c b/src/providers/ldap/ldap_child.c index e66406c0..5356f883 100644 --- a/src/providers/ldap/ldap_child.c +++ b/src/providers/ldap/ldap_child.c @@ -446,25 +446,14 @@ int main(int argc, const char *argv[]) goto fail; } - while ((ret = read(STDIN_FILENO, buf + len, IN_BUF_SIZE - len)) != 0) { - if (ret == -1) { - if (errno == EINTR || errno == EAGAIN) { - continue; - } - DEBUG(1, ("read failed [%d][%s].\n", errno, strerror(errno))); - goto fail; - } else if (ret > 0) { - len += ret; - if (len > IN_BUF_SIZE) { - DEBUG(1, ("read too much, this should never happen.\n")); - goto fail; - } - continue; - } else { - DEBUG(1, ("unexpected return code of read [%d].\n", ret)); - goto fail; - } + errno = 0; + len = sss_atomic_read_s(STDIN_FILENO, buf, IN_BUF_SIZE); + if (len == -1) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, ("read failed [%d][%s].\n", ret, strerror(ret))); + goto fail; } + close(STDIN_FILENO); ret = unpack_buffer(buf, len, ibuf); @@ -484,22 +473,22 @@ int main(int argc, const char *argv[]) ret = prepare_response(main_ctx, ccname, expire_time, kerr, &resp); if (ret != EOK) { - DEBUG(1, ("prepare_response failed. [%d][%s].\n", ret, strerror(ret))); - return ENOMEM; + DEBUG(SSSDBG_CRIT_FAILURE, ("prepare_response failed. [%d][%s].\n", ret, strerror(ret))); + goto fail; } - written = 0; - while (written < resp->size) { - ret = write(STDOUT_FILENO, resp->buf + written, resp->size - written); - if (ret == -1) { - if (errno == EAGAIN || errno == EINTR) { - continue; - } - ret = errno; - DEBUG(1, ("write failed [%d][%s].\n", ret, strerror(ret))); - return ret; - } - written += ret; + errno = 0; + written = sss_atomic_write_s(STDOUT_FILENO, resp->buf, resp->size); + if (written == -1) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, ("write failed [%d][%s].\n", ret, strerror(ret))); + goto fail; + } + + if (written != resp->size) { + DEBUG(SSSDBG_CRIT_FAILURE, ("Expected to write %d bytes, wrote %d\n", + resp->size, written)); + goto fail; } close(STDOUT_FILENO); -- cgit