fixeria has uploaded this change for review.
bankd: fix handling of partial writes in worker_send_rspro()
worker_send_rspro() calls write() once and treats anything other than
a complete write as an error. Even on a blocking socket, write() may
transfer less than requested, e.g. if it gets interrupted by a signal
(such as SIGMAPDEL or SIGMAPADD) after some data has already been sent.
Bailing out in this case leaves a truncated IPA message on the wire.
Keep calling write() until the whole message has been sent, retrying
on EINTR, and only fail on real errors. While at it, make the error
message more informative.
Change-Id: I2c1675485404ecedb39f9ca25c234623c71cddf5
---
M src/bankd/bankd_main.c
1 file changed, 15 insertions(+), 7 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-remsim refs/changes/84/44184/1
diff --git a/src/bankd/bankd_main.c b/src/bankd/bankd_main.c
index 70aa9f9..5873446 100644
--- a/src/bankd/bankd_main.c
+++ b/src/bankd/bankd_main.c
@@ -705,6 +705,7 @@
static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
{
struct msgb *msg = rspro_enc_msg(pdu);
+ unsigned int len, written = 0;
int rc;
if (!msg) {
@@ -718,14 +719,21 @@
ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
ipa_prepend_header(msg, IPAC_PROTO_OSMO);
- /* actually send it through the socket */
- rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
- if (rc == msgb_length(msg))
- rc = 0;
- else {
- LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
- rc = -1;
+ /* actually send it through the socket; a blocking write() may still
+ * transfer less than requested, e.g. if interrupted by a signal */
+ len = msgb_length(msg);
+ while (written < len) {
+ rc = write(worker->client.fd, msgb_data(msg) + written, len - written);
+ if (rc == -1 && errno == EINTR)
+ continue;
+ if (rc <= 0) {
+ LOGW(worker, "error during write (%u of %u bytes written): %s\n",
+ written, len, rc < 0 ? strerror(errno) : "no progress");
+ break;
+ }
+ written += rc;
}
+ rc = (written == len) ? 0 : -1;
msgb_free(msg);
To view, visit change 44184. To unsubscribe, or for help writing mail filters, visit settings.