laforge submitted this change.

View Change

Approvals: laforge: Looks good to me, but someone else must approve pespin: Looks good to me, approved Jenkins Builder: Verified
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, 13 insertions(+), 7 deletions(-)

diff --git a/src/bankd/bankd_main.c b/src/bankd/bankd_main.c
index 70aa9f9..fda700b 100644
--- a/src/bankd/bankd_main.c
+++ b/src/bankd/bankd_main.c
@@ -718,14 +718,20 @@
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 */
+ while (msgb_length(msg) > 0) {
+ rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
+ if (rc == -1 && errno == EINTR)
+ continue;
+ if (rc <= 0) {
+ LOGW(worker, "error during write (%u bytes remaining): %s\n",
+ msgb_length(msg), rc < 0 ? strerror(errno) : "no progress");
+ break;
+ }
+ msgb_pull(msg, rc);
}
+ rc = (msgb_length(msg) == 0) ? 0 : -1;

msgb_free(msg);

To view, visit change 44184. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: I2c1675485404ecedb39f9ca25c234623c71cddf5
Gerrit-Change-Number: 44184
Gerrit-PatchSet: 2
Gerrit-Owner: fixeria <vyanitskiy@sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-Reviewer: pespin <pespin@sysmocom.de>