diff --git a/mgmt/utils/MgmtSocket.cc b/mgmt/utils/MgmtSocket.cc index 3293617b1e1..d6e5e7478f4 100644 --- a/mgmt/utils/MgmtSocket.cc +++ b/mgmt/utils/MgmtSocket.cc @@ -42,15 +42,27 @@ bool mgmt_transient_error() { - bool transient = false; - transient = (errno == EINTR); + switch (errno) { + case EINTR: + case EAGAIN: + #ifdef ENOMEM - transient = transient || (errno == ENOMEM); + case ENOMEM: #endif + #ifdef ENOBUF - transient = transient || (errno == ENOBUF); + case ENOBUF: +#endif + +#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN) + case EWOULDBLOCK: #endif - return transient; + + return true; + + default: + return false; + } } //------------------------------------------------------------------------- diff --git a/mgmt/utils/MgmtUtils.cc b/mgmt/utils/MgmtUtils.cc index f85578b6b71..ebe1bdf0785 100644 --- a/mgmt/utils/MgmtUtils.cc +++ b/mgmt/utils/MgmtUtils.cc @@ -23,6 +23,7 @@ #include "ts/ink_platform.h" #include "ts/ink_sock.h" #include "MgmtUtils.h" +#include "MgmtSocket.h" #include "ts/Diags.h" #include "LocalManager.h" @@ -52,11 +53,12 @@ mgmt_use_syslog() int mgmt_readline(int soc, char *buf, int maxlen) { - int n, rc; + int n = 0, rc; char c; - for (n = 1; n < maxlen; n++) { - if ((rc = read_socket(soc, &c, 1)) == 1) { + for (; n < maxlen; n++) { + rc = read_socket(soc, &c, 1); + if (rc == 1) { *buf++ = c; if (c == '\n') { --buf; @@ -74,9 +76,19 @@ mgmt_readline(int soc, char *buf, int maxlen) break; } } else { /* Error */ + if (errno == ECONNRESET || errno == EPIPE) { + return n ? n : 0; + } + + if (mgmt_transient_error()) { + mgmt_sleep_msec(1); + continue; + } + return -1; } } + return n; } /* End mgmt_readline */ @@ -132,30 +144,30 @@ mgmt_read_pipe(int fd, char *buf, int bytes_to_read) int err = 0; char *p = buf; int bytes_read = 0; + while (bytes_to_read > 0) { err = read_socket(fd, p, bytes_to_read); if (err == 0) { return err; } else if (err < 0) { - switch (errno) { - case EINTR: - case EAGAIN: -#if defined(hpux) - case EWOULDBLOCK: -#endif + // Turn ECONNRESET into EOF. + if (errno == ECONNRESET || errno == EPIPE) { + return bytes_read ? bytes_read : 0; + } + + if (mgmt_transient_error()) { mgmt_sleep_msec(1); continue; - default: - return -errno; } + + return -errno; } + bytes_to_read -= err; bytes_read += err; p += err; } - /* - Ldone: - */ + return bytes_read; } @@ -174,30 +186,25 @@ mgmt_write_pipe(int fd, char *buf, int bytes_to_write) int err = 0; char *p = buf; int bytes_written = 0; + while (bytes_to_write > 0) { err = write_socket(fd, p, bytes_to_write); if (err == 0) { return err; } else if (err < 0) { - switch (errno) { - case EINTR: - case EAGAIN: -#if defined(hpux) - case EWOULDBLOCK: -#endif + if (mgmt_transient_error()) { mgmt_sleep_msec(1); continue; - default: - return -errno; } + + return -errno; } + bytes_to_write -= err; bytes_written += err; p += err; } - /* - Ldone: - */ + return bytes_written; }