Skip to contentSkip to conversation

tools/libxs: Fix SIGPIPE handling issues

PATCH In review Andrew Cooper 4 participants 28 messages Jul 18, 2024 – Jul 24, 2024

lore
Share & tools
lore
#0 lore raw

cover tools/libxs: Fix SIGPIPE handling issues

While the purpose of this series is patch 6, it has a side effect of reducing the number of system calls substantally.

Using a strace of test-xenstore as an example, we go from this:

rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fda8278e2f0}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fda8278e2f0}, 8) = 0 write(3, "\v\0\0\0\0\0\0\0\0\0\0\0\30\0\0\0", 16) = 16
write(3, "xenstore-test/502673/a\0", 23) = 23
write(3, "a", 1) = 1
read(3, "\v\0\0\0\0\0\0\0\0\0\0\0\3\0\0\0", 16) = 16
read(3, "OK\0", 3) = 3
rt_sigaction(SIGPIPE, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fda8278e2f0}, NULL, 8) = 0

down to this:

sendmsg(3, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\v\0\0\0\0\0\0\0\0\0\0\0\30\0\0\0", iov_len=16}, {iov_base="xenstore-test/504021/a\0", iov_len=23}, {iov_base="a", iov_len=1}], msg_iovlen=3, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 40 read(3, "\v\0\0\0\0\0\0\0\0\0\0\0\3\0\0\0", 16) = 16
read(3, "OK\0", 3) = 3

I.e., it removes 2x rt_sigaction(), and turns all write()'s into a single writev() or sendmsg().

Reads are a little more problematic to deal with. Xenstored will produce a full package basically in one go, but libxenstore's reading is horrbly complicated by virtue of it being completely different depending on whether xs_watch() has created a secondary read thread or not.

Andrew Cooper (6):
tools/libxs: Fix length check in xs_talkv()
tools/libxs: Rework xs_talkv() to take xsd_sockmsg within the iovec tools/libxs: Rationalise the definition of struct xs_handle tools/libxs: Track whether we're using a socket or file tools/libxs: Use writev()/sendmsg() instead of write() tools/libxs: Stop playing with SIGPIPE

 tools/libs/store/xs.c | 394 +++++++++++++++++++++++++++---------------
 1 file changed, 258 insertions(+), 136 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/6 tools/libxs: Fix length check in xs_talkv()

If the sum of iov element lengths overflows, the XENSTORE_PAYLOAD_MAX can pass, after which we'll write 4G of data with a good-looking length field, and the remainder of the payload will be interpreted as subsequent commands.

Check each iov element length for XENSTORE_PAYLOAD_MAX before accmulating it.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
 tools/libs/store/xs.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
tools/libs/store/xs.c +10 -7
index ec77379ab9bd..81a790cfe60f 100644
@@ -571,21 +571,24 @@ static void *xs_talkv(struct xs_handle *h, xs_transaction_t t,
 	struct xsd_sockmsg msg;
 	void *ret = NULL;
 	int saved_errno;
-	unsigned int i;
+	unsigned int i, msg_len;
 	struct sigaction ignorepipe, oldact;
 
 	msg.tx_id = t;
 	msg.req_id = 0;
 	msg.type = type;
-	msg.len = 0;
-	for (i = 0; i < num_vecs; i++)
-		msg.len += iovec[i].iov_len;
 
-	if (msg.len > XENSTORE_PAYLOAD_MAX) {
-		errno = E2BIG;
-		return 0;
+	/* Calculate the payload length by summing iovec elements */
+	for (i = 0, msg_len = 0; i < num_vecs; i++) {
+		if ((iovec[i].iov_len > XENSTORE_PAYLOAD_MAX) ||
+		    ((msg_len += iovec[i].iov_len) > XENSTORE_PAYLOAD_MAX)) {
+			errno = E2BIG;
+			return 0;
+		}
 	}
 
+	msg.len = msg_len;
+
 	ignorepipe.sa_handler = SIG_IGN;
 	sigemptyset(&ignorepipe.sa_mask);
 	ignorepipe.sa_flags = 0;
Signature · 1 line
2.39.2
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] tools/libxs: Fix length check in xs_talkv()

Quoted 32 lines · Andrew Cooper

If the sum of iov element lengths overflows, the XENSTORE_PAYLOAD_MAX can pass, after which we'll write 4G of data with a good-looking length field, and the remainder of the payload will be interpreted as subsequent commands.

Check each iov element length for XENSTORE_PAYLOAD_MAX before accmulating it.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
  tools/libs/store/xs.c | 17 ++++++++++-------
  1 file changed, 10 insertions(+), 7 deletions(-)
tools/libs/store/xs.c +1 -4
index ec77379ab9bd..81a790cfe60f 100644
@@ -571,21 +571,24 @@ static void *xs_talkv(struct xs_handle *h, xs_transaction_t t,
  	struct xsd_sockmsg msg;
  	void *ret = NULL;
  	int saved_errno;
-	unsigned int i;
+	unsigned int i, msg_len;
  	struct sigaction ignorepipe, oldact;
 
  	msg.tx_id = t;
  	msg.req_id = 0;
  	msg.type = type;
-	msg.len = 0;
-	for (i = 0; i < num_vecs; i++)
-		msg.len += iovec[i].iov_len;

tools/libs/store/xs.c static void *xs_talkv(struct xs_handle *h, xs_transaction_t t, · line 586

Quoted 10 lines
tools/libs/store/xs.c +6 -3
 
-	if (msg.len > XENSTORE_PAYLOAD_MAX) {
-		errno = E2BIG;
-		return 0;
+	/* Calculate the payload length by summing iovec elements */
+	for (i = 0, msg_len = 0; i < num_vecs; i++) {
+		if ((iovec[i].iov_len > XENSTORE_PAYLOAD_MAX) ||
+		    ((msg_len += iovec[i].iov_len) > XENSTORE_PAYLOAD_MAX)) {
+			errno = E2BIG;
+			return 0;

return NULL;

With that,

Reviewed-by
#3 lore raw

Re: [PATCH 1/6] tools/libxs: Fix length check in xs_talkv()

Quoted 45 lines · Jason Andryuk
Quoted 31 lines · Andrew Cooper

If the sum of iov element lengths overflows, the XENSTORE_PAYLOAD_MAX can pass, after which we'll write 4G of data with a good-looking length field, and the remainder of the payload will be interpreted as subsequent commands.

Check each iov element length for XENSTORE_PAYLOAD_MAX before accmulating it.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
---
  tools/libs/store/xs.c | 17 ++++++++++-------

  1 file changed, 10 insertions(+), 7 deletions(-)
tools/libs/store/xs.c +1 -3
index ec77379ab9bd..81a790cfe60f 100644
@@ -571,21 +571,24 @@ static void *xs_talkv(struct xs_handle *h, 
 xs_transaction_t t,
       struct xsd_sockmsg msg;
       void *ret = NULL;
       int saved_errno;
-    unsigned int i;
+    unsigned int i, msg_len;
       struct sigaction ignorepipe, oldact;
       msg.tx_id = t;
       msg.req_id = 0;
       msg.type = type;
-    msg.len = 0;
-    for (i = 0; i < num_vecs; i++)

tools/libs/store/xs.c static void *xs_talkv(struct xs_handle *h, · line 585

Quoted 10 lines
tools/libs/store/xs.c +6 -4
-        msg.len += iovec[i].iov_len;
-    if (msg.len > XENSTORE_PAYLOAD_MAX) {
-        errno = E2BIG;
-        return 0;
+    /* Calculate the payload length by summing iovec elements */
+    for (i = 0, msg_len = 0; i < num_vecs; i++) {
+        if ((iovec[i].iov_len > XENSTORE_PAYLOAD_MAX) ||
+            ((msg_len += iovec[i].iov_len) > XENSTORE_PAYLOAD_MAX)) {
+            errno = E2BIG;
+            return 0;

return NULL;

Quoted 2 lines

With that,

With the suggested change:

Juergen

#4 lore raw

Re: [PATCH 1/6] tools/libxs: Fix length check in xs_talkv()

Quoted 50 lines · Jürgen Groß
Quoted 49 lines · Jason Andryuk
Quoted 35 lines · Andrew Cooper

If the sum of iov element lengths overflows, the XENSTORE_PAYLOAD_MAX can
pass, after which we'll write 4G of data with a good-looking length field, and
the remainder of the payload will be interpreted as subsequent commands.

Check each iov element length for XENSTORE_PAYLOAD_MAX before accmulating it.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
---
  tools/libs/store/xs.c | 17 ++++++++++-------

  1 file changed, 10 insertions(+), 7 deletions(-)
tools/libs/store/xs.c +1 -3
index ec77379ab9bd..81a790cfe60f 100644
@@ -571,21 +571,24 @@ static void *xs_talkv(struct xs_handle *h,
 xs_transaction_t t,
       struct xsd_sockmsg msg;
       void *ret = NULL;
       int saved_errno;
-    unsigned int i;
+    unsigned int i, msg_len;
       struct sigaction ignorepipe, oldact;
       msg.tx_id = t;
       msg.req_id = 0;
       msg.type = type;
-    msg.len = 0;
-    for (i = 0; i < num_vecs; i++)

tools/libs/store/xs.c static void *xs_talkv(struct xs_handle *h, · line 585

Quoted 10 lines
tools/libs/store/xs.c +6 -4
-        msg.len += iovec[i].iov_len;
-    if (msg.len > XENSTORE_PAYLOAD_MAX) {
-        errno = E2BIG;
-        return 0;
+    /* Calculate the payload length by summing iovec elements */
+    for (i = 0, msg_len = 0; i < num_vecs; i++) {
+        if ((iovec[i].iov_len > XENSTORE_PAYLOAD_MAX) ||
+            ((msg_len += iovec[i].iov_len) > XENSTORE_PAYLOAD_MAX)) {
+            errno = E2BIG;
+            return 0;

return NULL;

Quoted 6 lines
Quoted 2 lines

With that,

With the suggested change:

Thankyou both.  I'd not even spotted the mistake when just rearranging the logic.

~Andrew

#5 lore raw

PATCH 2/6 tools/libxs: Rework xs_talkv() to take xsd_sockmsg within the iovec

In reply to Andrew Cooper

We would like to writev() the whole outgoing message, which is hard given the current need to prepend the locally-constructed xsd_sockmsg.

Instead, have the caller provide xsd_sockmsg in iovec[0]. This in turn drops the t and type parameters from xs_talkv().

Note that xs_talkv() may alter the iovec structure. This may happen when writev() is really used under the covers, and it's preforable to having the lower levels need to duplciate the iovec to edit it upon encountering a short write. xs_directory_part() is the only function impacted by this, and it's easy to rearrange.

No functional change.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
 tools/libs/store/xs.c | 187 ++++++++++++++++++++++++------------------
 1 file changed, 107 insertions(+), 80 deletions(-)
tools/libs/store/xs.c +107 -80
index 81a790cfe60f..1b5a913a1594 100644
@@ -561,25 +561,32 @@ static void *read_reply(
 	return body;
 }
 
-/* Send message to xs, get malloc'ed reply.  NULL and set errno on error. */
-static void *xs_talkv(struct xs_handle *h, xs_transaction_t t,
-		      enum xsd_sockmsg_type type,
-		      const struct iovec *iovec,
+/*
+ * Send message to xenstore, get malloc'ed reply.  NULL and set errno on error.
+ *
+ * @iovec describes the entire outgoing message, starting with the xsd_sockmsg
+ * header.  xs_talkv() calculates the outgoing message length, updating
+ * xsd_sockmsg in element 0.  xs_talkv() might edit the iovec structure in
+ * place (e.g. following short writes).
+ */
+static void *xs_talkv(struct xs_handle *h,
+		      struct iovec *iovec,
 		      unsigned int num_vecs,
 		      unsigned int *len)
 {
-	struct xsd_sockmsg msg;
+	struct xsd_sockmsg *msg = iovec[0].iov_base;
+	enum xsd_sockmsg_type reply_type;
 	void *ret = NULL;
 	int saved_errno;
 	unsigned int i, msg_len;
 	struct sigaction ignorepipe, oldact;
 
-	msg.tx_id = t;
-	msg.req_id = 0;
-	msg.type = type;
+	/* Element 0 must be xsd_sockmsg */
+	assert(num_vecs >= 1);
+	assert(iovec[0].iov_len == sizeof(*msg));
 
 	/* Calculate the payload length by summing iovec elements */
-	for (i = 0, msg_len = 0; i < num_vecs; i++) {
+	for (i = 1, msg_len = 0; i < num_vecs; i++) {
 		if ((iovec[i].iov_len > XENSTORE_PAYLOAD_MAX) ||
 		    ((msg_len += iovec[i].iov_len) > XENSTORE_PAYLOAD_MAX)) {
 			errno = E2BIG;
@@ -587,7 +594,7 @@ static void *xs_talkv(struct xs_handle *h, xs_transaction_t t,
 		}
 	}
 
-	msg.len = msg_len;
+	msg->len = msg_len;
 
 	ignorepipe.sa_handler = SIG_IGN;
 	sigemptyset(&ignorepipe.sa_mask);
@@ -596,28 +603,25 @@ static void *xs_talkv(struct xs_handle *h, xs_transaction_t t,
 
 	mutex_lock(&h->request_mutex);
 
-	if (!xs_write_all(h->fd, &msg, sizeof(msg)))
-		goto fail;
-
 	for (i = 0; i < num_vecs; i++)
 		if (!xs_write_all(h->fd, iovec[i].iov_base, iovec[i].iov_len))
 			goto fail;
 
-	ret = read_reply(h, &msg.type, len);
+	ret = read_reply(h, &reply_type, len);
 	if (!ret)
 		goto fail;
 
 	mutex_unlock(&h->request_mutex);
 
 	sigaction(SIGPIPE, &oldact, NULL);
-	if (msg.type == XS_ERROR) {
+	if (reply_type == XS_ERROR) {
 		saved_errno = get_error(ret);
 		free(ret);
 		errno = saved_errno;
 		return NULL;
 	}
 
-	if (msg.type != type) {
+	if (reply_type != msg->type) {
 		free(ret);
 		saved_errno = EBADF;
 		goto close_fd;
@@ -650,11 +654,15 @@ static void *xs_single(struct xs_handle *h, xs_transaction_t t,
 		       const char *string,
 		       unsigned int *len)
 {
-	struct iovec iovec;
+	struct xsd_sockmsg msg = { .type = type, .tx_id = t };
+	struct iovec iov[2];
+
+	iov[0].iov_base = &msg;
+	iov[0].iov_len  = sizeof(msg);
+	iov[1].iov_base = (void *)string;
+	iov[1].iov_len  = strlen(string) + 1;
 
-	iovec.iov_base = (void *)string;
-	iovec.iov_len = strlen(string) + 1;
-	return xs_talkv(h, t, type, &iovec, 1, len);
+	return xs_talkv(h, iov, ARRAY_SIZE(iov), len);
 }
 
 static bool xs_bool(char *reply)
@@ -691,21 +699,25 @@ static char **xs_directory_common(char *strings, unsigned int len,
 static char **xs_directory_part(struct xs_handle *h, xs_transaction_t t,
 				const char *path, unsigned int *num)
 {
+	struct xsd_sockmsg msg = { .type = XS_DIRECTORY_PART, .tx_id = t };
 	unsigned int off, result_len;
 	char gen[24], offstr[8];
-	struct iovec iovec[2];
+	struct iovec iov[3];
 	char *result = NULL, *strings = NULL;
 
 	memset(gen, 0, sizeof(gen));
-	iovec[0].iov_base = (void *)path;
-	iovec[0].iov_len = strlen(path) + 1;
 
 	for (off = 0;;) {
 		snprintf(offstr, sizeof(offstr), "%u", off);
-		iovec[1].iov_base = (void *)offstr;
-		iovec[1].iov_len = strlen(offstr) + 1;
-		result = xs_talkv(h, t, XS_DIRECTORY_PART, iovec, 2,
-				  &result_len);
+
+		iov[0].iov_base = &msg;
+		iov[0].iov_len  = sizeof(msg);
+		iov[1].iov_base = (void *)path;
+		iov[1].iov_len  = strlen(path) + 1;
+		iov[2].iov_base = (void *)offstr;
+		iov[2].iov_len  = strlen(offstr) + 1;
+
+		result = xs_talkv(h, iov, ARRAY_SIZE(iov), &result_len);
 
 		/* If XS_DIRECTORY_PART isn't supported return E2BIG. */
 		if (!result) {
@@ -774,15 +786,17 @@ void *xs_read(struct xs_handle *h, xs_transaction_t t,
 bool xs_write(struct xs_handle *h, xs_transaction_t t,
 	      const char *path, const void *data, unsigned int len)
 {
-	struct iovec iovec[2];
+	struct xsd_sockmsg msg = { .type = XS_WRITE, .tx_id = t };
+	struct iovec iov[3];
 
-	iovec[0].iov_base = (void *)path;
-	iovec[0].iov_len = strlen(path) + 1;
-	iovec[1].iov_base = (void *)data;
-	iovec[1].iov_len = len;
+	iov[0].iov_base = &msg;
+	iov[0].iov_len  = sizeof(msg);
+	iov[1].iov_base = (void *)path;
+	iov[1].iov_len  = strlen(path) + 1;
+	iov[2].iov_base = (void *)data;
+	iov[2].iov_len  = len;
 
-	return xs_bool(xs_talkv(h, t, XS_WRITE, iovec,
-				ARRAY_SIZE(iovec), NULL));
+	return xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL));
 }
 
 /* Create a new directory.
@@ -846,34 +860,37 @@ bool xs_set_permissions(struct xs_handle *h,
 			struct xs_permissions *perms,
 			unsigned int num_perms)
 {
+	struct xsd_sockmsg msg = { .type = XS_SET_PERMS, .tx_id = t };
 	unsigned int i;
-	struct iovec iov[1+num_perms];
+	struct iovec iov[2 + num_perms];
+
+	iov[0].iov_base = &msg;
+	iov[0].iov_len  = sizeof(msg);
+	iov[1].iov_base = (void *)path;
+	iov[1].iov_len  = strlen(path) + 1;
 
-	iov[0].iov_base = (void *)path;
-	iov[0].iov_len = strlen(path) + 1;
-	
 	for (i = 0; i < num_perms; i++) {
 		char buffer[MAX_STRLEN(unsigned int)+1];
 
 		if (!xenstore_perm_to_string(&perms[i], buffer, sizeof(buffer)))
 			goto unwind;
 
-		iov[i+1].iov_base = strdup(buffer);
-		iov[i+1].iov_len = strlen(buffer) + 1;
+		iov[i + 2].iov_base = strdup(buffer);
+		iov[i + 2].iov_len  = strlen(buffer) + 1;
 		if (!iov[i+1].iov_base)
 			goto unwind;
 	}
 
-	if (!xs_bool(xs_talkv(h, t, XS_SET_PERMS, iov, 1+num_perms, NULL)))
+	if (!xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL)))
 		goto unwind;
 	for (i = 0; i < num_perms; i++)
-		free(iov[i+1].iov_base);
+		free(iov[i + 2].iov_base);
 	return true;
 
 unwind:
 	num_perms = i;
 	for (i = 0; i < num_perms; i++)
-		free_no_errno(iov[i+1].iov_base);
+		free_no_errno(iov[i + 2].iov_base);
 	return false;
 }
 
@@ -890,7 +907,8 @@ bool xs_restrict(struct xs_handle *h, unsigned domid)
  */
 bool xs_watch(struct xs_handle *h, const char *path, const char *token)
 {
-	struct iovec iov[2];
+	struct xsd_sockmsg msg = { .type = XS_WATCH };
+	struct iovec iov[3];
 
 #ifdef USE_PTHREAD
 #define DEFAULT_THREAD_STACKSIZE (16 * 1024)
@@ -948,13 +966,14 @@ bool xs_watch(struct xs_handle *h, const char *path, const char *token)
 	mutex_unlock(&h->request_mutex);
 #endif
 
-	iov[0].iov_base = (void *)path;
-	iov[0].iov_len = strlen(path) + 1;
-	iov[1].iov_base = (void *)token;
-	iov[1].iov_len = strlen(token) + 1;
+	iov[0].iov_base = &msg;
+	iov[0].iov_len  = sizeof(msg);
+	iov[1].iov_base = (void *)path;
+	iov[1].iov_len  = strlen(path) + 1;
+	iov[2].iov_base = (void *)token;
+	iov[2].iov_len  = strlen(token) + 1;
 
-	return xs_bool(xs_talkv(h, XBT_NULL, XS_WATCH, iov,
-				ARRAY_SIZE(iov), NULL));
+	return xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL));
 }
 
 
@@ -1067,20 +1086,22 @@ char **xs_read_watch(struct xs_handle *h, unsigned int *num)
  */
 bool xs_unwatch(struct xs_handle *h, const char *path, const char *token)
 {
-	struct iovec iov[2];
+	struct xsd_sockmsg sockmsg = { .type = XS_UNWATCH };
+	struct iovec iov[3];
 	struct xs_stored_msg *msg, *tmsg;
 	bool res;
 	char *s, *p;
 	unsigned int i;
 	char *l_token, *l_path;
 
-	iov[0].iov_base = (char *)path;
-	iov[0].iov_len = strlen(path) + 1;
-	iov[1].iov_base = (char *)token;
-	iov[1].iov_len = strlen(token) + 1;
+	iov[0].iov_base = &sockmsg;
+	iov[0].iov_len  = sizeof(sockmsg);
+	iov[1].iov_base = (char *)path;
+	iov[1].iov_len  = strlen(path) + 1;
+	iov[2].iov_base = (char *)token;
+	iov[2].iov_len  = strlen(token) + 1;
 
-	res = xs_bool(xs_talkv(h, XBT_NULL, XS_UNWATCH, iov,
-			       ARRAY_SIZE(iov), NULL));
+	res = xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL));
 
 	if (!h->unwatch_filter) /* Don't filter the watch list */
 		return res;
@@ -1173,43 +1194,47 @@ bool xs_introduce_domain(struct xs_handle *h,
 			 unsigned int domid, unsigned long mfn,
 			 unsigned int eventchn)
 {
+	struct xsd_sockmsg msg = { .type = XS_INTRODUCE };
 	char domid_str[MAX_STRLEN(domid)];
 	char mfn_str[MAX_STRLEN(mfn)];
 	char eventchn_str[MAX_STRLEN(eventchn)];
-	struct iovec iov[3];
+	struct iovec iov[4];
 
 	snprintf(domid_str, sizeof(domid_str), "%u", domid);
 	snprintf(mfn_str, sizeof(mfn_str), "%lu", mfn);
 	snprintf(eventchn_str, sizeof(eventchn_str), "%u", eventchn);
 
-	iov[0].iov_base = domid_str;
-	iov[0].iov_len = strlen(domid_str) + 1;
-	iov[1].iov_base = mfn_str;
-	iov[1].iov_len = strlen(mfn_str) + 1;
-	iov[2].iov_base = eventchn_str;
-	iov[2].iov_len = strlen(eventchn_str) + 1;
+	iov[0].iov_base = &msg;
+	iov[0].iov_len  = sizeof(msg);
+	iov[1].iov_base = domid_str;
+	iov[1].iov_len  = strlen(domid_str) + 1;
+	iov[2].iov_base = mfn_str;
+	iov[2].iov_len  = strlen(mfn_str) + 1;
+	iov[3].iov_base = eventchn_str;
+	iov[3].iov_len  = strlen(eventchn_str) + 1;
 
-	return xs_bool(xs_talkv(h, XBT_NULL, XS_INTRODUCE, iov,
-				ARRAY_SIZE(iov), NULL));
+	return xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL));
 }
 
 bool xs_set_target(struct xs_handle *h,
 			 unsigned int domid, unsigned int target)
 {
+	struct xsd_sockmsg msg = { .type = XS_SET_TARGET };
 	char domid_str[MAX_STRLEN(domid)];
 	char target_str[MAX_STRLEN(target)];
-	struct iovec iov[2];
+	struct iovec iov[3];
 
 	snprintf(domid_str, sizeof(domid_str), "%u", domid);
 	snprintf(target_str, sizeof(target_str), "%u", target);
 
-	iov[0].iov_base = domid_str;
-	iov[0].iov_len = strlen(domid_str) + 1;
-	iov[1].iov_base = target_str;
-	iov[1].iov_len = strlen(target_str) + 1;
+	iov[0].iov_base = &msg;
+	iov[0].iov_len  = sizeof(msg);
+	iov[1].iov_base = domid_str;
+	iov[1].iov_len  = strlen(domid_str) + 1;
+	iov[2].iov_base = target_str;
+	iov[2].iov_len  = strlen(target_str) + 1;
 
-	return xs_bool(xs_talkv(h, XBT_NULL, XS_SET_TARGET, iov,
-				ARRAY_SIZE(iov), NULL));
+	return xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL));
 }
 
 static void * single_with_domid(struct xs_handle *h,
@@ -1305,15 +1330,17 @@ int xs_suspend_evtchn_port(int domid)
 char *xs_control_command(struct xs_handle *h, const char *cmd,
 			 void *data, unsigned int len)
 {
-	struct iovec iov[2];
+	struct xsd_sockmsg msg = { .type = XS_CONTROL };
+	struct iovec iov[3];
 
-	iov[0].iov_base = (void *)cmd;
-	iov[0].iov_len = strlen(cmd) + 1;
-	iov[1].iov_base = data;
-	iov[1].iov_len = len;
+	iov[0].iov_base = &msg;
+	iov[0].iov_len  = sizeof(msg);
+	iov[1].iov_base = (void *)cmd;
+	iov[1].iov_len  = strlen(cmd) + 1;
+	iov[2].iov_base = data;
+	iov[2].iov_len  = len;
 
-	return xs_talkv(h, XBT_NULL, XS_CONTROL, iov,
-			ARRAY_SIZE(iov), NULL);
+	return xs_talkv(h, iov, ARRAY_SIZE(iov), NULL);
 }
 
 char *xs_debug_command(struct xs_handle *h, const char *cmd,
Signature · 1 line
2.39.2
Reviewed-by on v1 2/6
#6 lore raw

Re: [PATCH 2/6] tools/libxs: Rework xs_talkv() to take xsd_sockmsg within the iovec

Quoted 12 lines · Andrew Cooper

We would like to writev() the whole outgoing message, which is hard given the current need to prepend the locally-constructed xsd_sockmsg.

Instead, have the caller provide xsd_sockmsg in iovec[0]. This in turn drops the t and type parameters from xs_talkv().

Note that xs_talkv() may alter the iovec structure. This may happen when writev() is really used under the covers, and it's preforable to having the lower levels need to duplciate the iovec to edit it upon encountering a short write. xs_directory_part() is the only function impacted by this, and it's easy to rearrange.

Quoted 3 lines

No functional change.

Juergen

Reviewed-by on v1 2/6
#7 lore raw

Re: [PATCH 2/6] tools/libxs: Rework xs_talkv() to take xsd_sockmsg within the iovec

In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper

We would like to writev() the whole outgoing message, which is hard given the current need to prepend the locally-constructed xsd_sockmsg.

Instead, have the caller provide xsd_sockmsg in iovec[0]. This in turn drops the t and type parameters from xs_talkv().

Note that xs_talkv() may alter the iovec structure. This may happen when writev() is really used under the covers, and it's preforable to having the

preferable

Quoted 1 line

lower levels need to duplciate the iovec to edit it upon encountering a short

duplicate

Quoted 6 lines

write. xs_directory_part() is the only function impacted by this, and it's easy to rearrange.

No functional change.

#8 lore raw

PATCH 3/6 tools/libxs: Rationalise the definition of struct xs_handle

In reply to Andrew Cooper

Right now there are two completely different struct xs_handle definitions, depend on #ifdef USE_PTHREAD. One is especially well hidden, and often escapes updates.

Rework struct xs_handle using some interior ifdefary. It's slightly longer, but much easier to follow. Importanly, this makes it much harder to forget the !PTHREAD case when adding a "common" variable.

No functional change.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
 tools/libs/store/xs.c | 77 +++++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 40 deletions(-)
tools/libs/store/xs.c +37 -40
index 1b5a913a1594..96ea2b192924 100644
@@ -40,6 +40,14 @@
 #include <xentoolcore_internal.h>
 #include <xen_list.h>
 
+#ifdef USE_PTHREAD
+# include <pthread.h>
+#endif
+
+#ifdef USE_DLSYM
+# include <dlfcn.h>
+#endif
+
 #ifndef O_CLOEXEC
 #define O_CLOEXEC 0
 #endif
@@ -54,14 +62,6 @@ struct xs_stored_msg {
 	char *body;
 };
 
-#ifdef USE_PTHREAD
-
-#include <pthread.h>
-
-#ifdef USE_DLSYM
-#include <dlfcn.h>
-#endif
-
 struct xs_handle {
 	/* Communications channel to xenstore daemon. */
 	int fd;
@@ -71,16 +71,20 @@ struct xs_handle {
          * A read thread which pulls messages off the comms channel and
          * signals waiters.
          */
+#ifdef USE_PTHREAD
 	pthread_t read_thr;
 	int read_thr_exists;
+#endif
 
 	/*
          * A list of fired watch messages, protected by a mutex. Users can
          * wait on the conditional variable until a watch is pending.
          */
 	XEN_TAILQ_HEAD(, struct xs_stored_msg) watch_list;
+#ifdef USE_PTHREAD
 	pthread_mutex_t watch_mutex;
 	pthread_cond_t watch_condvar;
+#endif
 
 	/* Clients can select() on this pipe to wait for a watch to fire. */
 	int watch_pipe[2];
@@ -93,6 +97,7 @@ struct xs_handle {
          * conditional variable for its response.
          */
 	XEN_TAILQ_HEAD(, struct xs_stored_msg) reply_list;
+#ifdef USE_PTHREAD
 	pthread_mutex_t reply_mutex;
 	pthread_cond_t reply_condvar;
 
@@ -112,55 +117,47 @@ struct xs_handle {
 	 *     reply_mutex
 	 *     watch_mutex
 	 */
+#endif
 };
 
-#define mutex_lock(m)		pthread_mutex_lock(m)
-#define mutex_unlock(m)		pthread_mutex_unlock(m)
-#define condvar_signal(c)	pthread_cond_signal(c)
-#define condvar_wait(c,m)	pthread_cond_wait(c,m)
-#define cleanup_push(f, a)	\
-    pthread_cleanup_push((void (*)(void *))(f), (void *)(a))
+
+#ifdef USE_PTHREAD
+
+# define mutex_lock(m)             pthread_mutex_lock(m)
+# define mutex_unlock(m)           pthread_mutex_unlock(m)
+# define condvar_signal(c)         pthread_cond_signal(c)
+# define condvar_wait(c, m)        pthread_cond_wait(c, m)
+# define cleanup_push(f, a)        pthread_cleanup_push((void (*)(void *))(f), (void *)(a))
 /*
  * Some definitions of pthread_cleanup_pop() are a macro starting with an
  * end-brace. GCC then complains if we immediately precede that with a label.
  * Hence we insert a dummy statement to appease the compiler in this situation.
  */
-#define cleanup_pop(run)        ((void)0); pthread_cleanup_pop(run)
+# define cleanup_pop(run)          ((void)0); pthread_cleanup_pop(run)
 
-#define read_thread_exists(h)	(h->read_thr_exists)
+# define read_thread_exists(h)     ((h)->read_thr_exists)
 
 /* Because pthread_cleanup_p* are not available when USE_PTHREAD is
  * disabled, use these macros which convert appropriately. */
-#define cleanup_push_heap(p)        cleanup_push(free, p)
-#define cleanup_pop_heap(run, p)    cleanup_pop((run))
+# define cleanup_push_heap(p)      cleanup_push(free, p)
+# define cleanup_pop_heap(run, p)  cleanup_pop((run))
 
 static void *read_thread(void *arg);
 
-#else /* !defined(USE_PTHREAD) */
+#else /* USE_PTHREAD */
 
-struct xs_handle {
-	int fd;
-	Xentoolcore__Active_Handle tc_ah; /* for restrict */
-	XEN_TAILQ_HEAD(, struct xs_stored_msg) reply_list;
-	XEN_TAILQ_HEAD(, struct xs_stored_msg) watch_list;
-	/* Clients can select() on this pipe to wait for a watch to fire. */
-	int watch_pipe[2];
-	/* Filtering watch event in unwatch function? */
-	bool unwatch_filter;
-};
+# define mutex_lock(m)               ((void)0)
+# define mutex_unlock(m)             ((void)0)
+# define condvar_signal(c)           ((void)0)
+# define condvar_wait(c, m)          ((void)0)
+# define cleanup_push(f, a)          ((void)0)
+# define cleanup_pop(run)            ((void)0)
+# define read_thread_exists(h)       (0)
+# define cleanup_push_heap(p)        ((void)0)
+# define cleanup_pop_heap(run, p)    do { if ((run)) free(p); } while(0)
 
-#define mutex_lock(m)		((void)0)
-#define mutex_unlock(m)		((void)0)
-#define condvar_signal(c)	((void)0)
-#define condvar_wait(c,m)	((void)0)
-#define cleanup_push(f, a)	((void)0)
-#define cleanup_pop(run)	((void)0)
-#define read_thread_exists(h)	(0)
+#endif /* !USE_PTHREAD */
 
-#define cleanup_push_heap(p)        ((void)0)
-#define cleanup_pop_heap(run, p)    do { if ((run)) free(p); } while(0)
-
-#endif
 
 static int read_message(struct xs_handle *h, int nonblocking);
 
Signature · 1 line
2.39.2
Reviewed-by on v1 3/6
#9 lore raw

Re: [PATCH 3/6] tools/libxs: Rationalise the definition of struct xs_handle

Quoted 11 lines · Andrew Cooper

Right now there are two completely different struct xs_handle definitions, depend on #ifdef USE_PTHREAD. One is especially well hidden, and often escapes updates.

Rework struct xs_handle using some interior ifdefary. It's slightly longer, but much easier to follow. Importanly, this makes it much harder to forget the !PTHREAD case when adding a "common" variable.

No functional change.

Juergen

Reviewed-by on v1 3/6
#10 lore raw

Re: [PATCH 3/6] tools/libxs: Rationalise the definition of struct xs_handle

In reply to Andrew Cooper
Quoted 11 lines · Andrew Cooper

Right now there are two completely different struct xs_handle definitions, depend on #ifdef USE_PTHREAD. One is especially well hidden, and often escapes updates.

Rework struct xs_handle using some interior ifdefary. It's slightly longer, but much easier to follow. Importanly, this makes it much harder to forget the !PTHREAD case when adding a "common" variable.

No functional change.

#11 lore raw

PATCH 4/6 tools/libxs: Track whether we're using a socket or file

In reply to Andrew Cooper

It will determine whether to use writev() or sendmsg().

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
 tools/libs/store/xs.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/libs/store/xs.c +5 -0
index 96ea2b192924..f4edeb05f03b 100644
@@ -65,6 +65,9 @@ struct xs_stored_msg {
 struct xs_handle {
 	/* Communications channel to xenstore daemon. */
 	int fd;
+
+	bool is_socket; /* is @fd a file or socket? */
+
 	Xentoolcore__Active_Handle tc_ah; /* for restrict */
 
 	/*
@@ -305,6 +308,8 @@ static struct xs_handle *get_handle(const char *connect_to)
 	if (h->fd == -1)
 		goto err;
 
+	h->is_socket = S_ISSOCK(buf.st_mode);
+
 	XEN_TAILQ_INIT(&h->reply_list);
 	XEN_TAILQ_INIT(&h->watch_list);
 
Signature · 1 line
2.39.2
Reviewed-by on v1 4/6
#12 lore raw

Re: [PATCH 4/6] tools/libxs: Track whether we're using a socket or file

Quoted 25 lines · Andrew Cooper

It will determine whether to use writev() or sendmsg().

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
  tools/libs/store/xs.c | 5 +++++
  1 file changed, 5 insertions(+)
tools/libs/store/xs.c +3 -0
index 96ea2b192924..f4edeb05f03b 100644
@@ -65,6 +65,9 @@ struct xs_stored_msg {
  struct xs_handle {
  	/* Communications channel to xenstore daemon. */
  	int fd;
+
+	bool is_socket; /* is @fd a file or socket? */
+
  	Xentoolcore__Active_Handle tc_ah; /* for restrict */
 
  	/*

tools/libs/store/xs.c static struct xs_handle *get_handle(const char *connect_to) · line 315

Quoted 8 lines
tools/libs/store/xs.c +2 -0
@@ -305,6 +308,8 @@ static struct xs_handle *get_handle(const char *connect_to)
  	if (h->fd == -1)
  		goto err;
 
+	h->is_socket = S_ISSOCK(buf.st_mode);
+
  	XEN_TAILQ_INIT(&h->reply_list);
  	XEN_TAILQ_INIT(&h->watch_list);
 

I'd prefer to set h->is_socket above the current

    if (S_ISSOCK(buf.st_mode))

and then use h->is_socket in this if instead.

With that:

Juergen

#13 lore raw

Re: [PATCH 4/6] tools/libxs: Track whether we're using a socket or file

Quoted 36 lines · Jürgen Groß
Quoted 31 lines · Andrew Cooper

It will determine whether to use writev() or sendmsg().

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
---
  tools/libs/store/xs.c | 5 +++++

  1 file changed, 5 insertions(+)
tools/libs/store/xs.c +3 -0
index 96ea2b192924..f4edeb05f03b 100644
@@ -65,6 +65,9 @@ struct xs_stored_msg {
   struct xs_handle {
       /* Communications channel to xenstore daemon. */
       int fd;
+
+    bool is_socket; /* is @fd a file or socket? */
+

      Xentoolcore__Active_Handle tc_ah; /* for restrict */ /*

tools/libs/store/xs.c +1 -0
@@ -305,6 +308,8 @@ static struct xs_handle *get_handle(const char
 *connect_to)
       if (h->fd == -1)
           goto err;
   +    h->is_socket = S_ISSOCK(buf.st_mode);
+
       XEN_TAILQ_INIT(&h->reply_list);
       XEN_TAILQ_INIT(&h->watch_list);
Quoted 1 line

I'd prefer to set h->is_socket above the current

Quoted 7 lines

    if (S_ISSOCK(buf.st_mode))

and then use h->is_socket in this if instead.

With that:

To confirm, you mean like this?

@@ -297,7 +300,9 @@ static struct xs_handle *get_handle(const char *connect_to)
        if (stat(connect_to, &buf) != 0)
                goto err;

-       if (S_ISSOCK(buf.st_mode))
+       h->is_socket = S_ISSOCK(buf.st_mode);
+
+       if (h->is_socket)
                h->fd = get_socket(connect_to);
        else
                h->fd = get_dev(connect_to);

#14 lore raw

Re: [PATCH 4/6] tools/libxs: Track whether we're using a socket or file

Quoted 58 lines · Andrew Cooper
Quoted 36 lines · Jürgen Groß
Quoted 25 lines · Andrew Cooper

It will determine whether to use writev() or sendmsg().

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
   tools/libs/store/xs.c | 5 +++++
   1 file changed, 5 insertions(+)
tools/libs/store/xs.c +3 -0
index 96ea2b192924..f4edeb05f03b 100644
@@ -65,6 +65,9 @@ struct xs_stored_msg {
   struct xs_handle {
       /* Communications channel to xenstore daemon. */
       int fd;
+
+    bool is_socket; /* is @fd a file or socket? */
+
       Xentoolcore__Active_Handle tc_ah; /* for restrict */
         /*

tools/libs/store/xs.c static struct xs_handle *get_handle(const char · line 314

Quoted 7 lines
tools/libs/store/xs.c +1 -0
@@ -305,6 +308,8 @@ static struct xs_handle *get_handle(const char
 *connect_to)
       if (h->fd == -1)
           goto err;
   +    h->is_socket = S_ISSOCK(buf.st_mode);
+
       XEN_TAILQ_INIT(&h->reply_list);
       XEN_TAILQ_INIT(&h->watch_list);

I'd prefer to set h->is_socket above the current

Quoted 7 lines

    if (S_ISSOCK(buf.st_mode))

and then use h->is_socket in this if instead.

With that:

To confirm, you mean like this?

(quoted diff) +3 -1
@@ -297,7 +300,9 @@ static struct xs_handle *get_handle(const char
 *connect_to)
         if (stat(connect_to, &buf) != 0)
                 goto err;
 
-       if (S_ISSOCK(buf.st_mode))
+       h->is_socket = S_ISSOCK(buf.st_mode);
+
+       if (h->is_socket)
                 h->fd = get_socket(connect_to);
         else
Quoted 2 lines

                h->fd = get_dev(connect_to);

Yes.

Juergen

Reviewed-by
#15 lore raw

Re: [PATCH 4/6] tools/libxs: Track whether we're using a socket or file

Quoted 47 lines · Jürgen Groß
Quoted 46 lines · Andrew Cooper
Quoted 35 lines · Jürgen Groß
Quoted 24 lines · Andrew Cooper

It will determine whether to use writev() or sendmsg().

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
---
   tools/libs/store/xs.c | 5 +++++

   1 file changed, 5 insertions(+)
tools/libs/store/xs.c +3 -0
index 96ea2b192924..f4edeb05f03b 100644
@@ -65,6 +65,9 @@ struct xs_stored_msg {
    struct xs_handle {
        /* Communications channel to xenstore daemon. */
        int fd;
+
+    bool is_socket; /* is @fd a file or socket? */
+

       Xentoolcore__Active_Handle tc_ah; /* for restrict */ /*

tools/libs/store/xs.c static struct xs_handle *get_handle(const char · line 314

Quoted 7 lines
tools/libs/store/xs.c +1 -0
@@ -305,6 +308,8 @@ static struct xs_handle *get_handle(const char
 *connect_to)
        if (h->fd == -1)
            goto err;
    +    h->is_socket = S_ISSOCK(buf.st_mode);
+
        XEN_TAILQ_INIT(&h->reply_list);
        XEN_TAILQ_INIT(&h->watch_list);

I'd prefer to set h->is_socket above the current

Quoted 7 lines

     if (S_ISSOCK(buf.st_mode))

and then use h->is_socket in this if instead.

With that:

To confirm, you mean like this?

Quoted 12 lines
Quoted 9 lines
(quoted diff) +3 -1
@@ -297,7 +300,9 @@ static struct xs_handle *get_handle(const char
 *connect_to)
          if (stat(connect_to, &buf) != 0)
                  goto err;
-       if (S_ISSOCK(buf.st_mode))
+       h->is_socket = S_ISSOCK(buf.st_mode);
+
+       if (h->is_socket)
                  h->fd = get_socket(connect_to);
Quoted 3 lines

         else
                 h->fd = get_dev(connect_to);

#16 lore raw

PATCH 5/6 tools/libxs: Use writev()/sendmsg() instead of write()

In reply to Andrew Cooper

With the input data now conveniently arranged, use writev()/sendmsg() instead of decomposing it into write() calls.

This causes all requests to be submitted with a single system call, rather than at least two. While in principle short writes can occur, the chances of it happening are slim given that most xenbus comms are only a handful of bytes.

Nevertheless, provide {writev,sendmsg}_exact() wrappers which take care of resubmitting on EINTR or short write.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
 tools/libs/store/xs.c | 104 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 101 insertions(+), 3 deletions(-)
tools/libs/store/xs.c +101 -3
index f4edeb05f03b..9db5c02661f5 100644
@@ -563,6 +563,105 @@ static void *read_reply(
 	return body;
 }
 
+/*
+ * Wrapper around sendmsg() to resubmit on EINTR or short write.  Returns
+ * @true if all data was transmitted, or @false with errno for an error.
+ * Note: May alter @iov in place on resubmit.
+ */
+static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr)
+{
+	struct msghdr hdr = {
+		.msg_iov = iov,
+		.msg_iovlen = nr,
+	};
+
+	/* Sanity check first element isn't empty */
+	assert(iov->iov_len == sizeof(struct xsd_sockmsg));
+
+	while (hdr.msg_iovlen) {
+		ssize_t res = sendmsg(fd, &hdr, 0);
+
+		if (res < 0 && errno == EINTR)
+			continue;
+		if (res <= 0)
+			return false;
+
+		while (res) {
+			if (res >= hdr.msg_iov->iov_len) {
+				/* Skip full element. */
+				res -= hdr.msg_iov->iov_len;
+				hdr.msg_iovlen--;
+				hdr.msg_iov++;
+				continue;
+			}
+
+			/* Partial element, adjust base/len. */
+			hdr.msg_iov->iov_len  -= res;
+			hdr.msg_iov->iov_base += res;
+			break;
+		}
+
+		while (hdr.msg_iov->iov_len == 0) {
+			/* Skip empty element. */
+			hdr.msg_iovlen--;
+			hdr.msg_iov++;
+		}
+	}
+
+	return true;
+}
+
+/*
+ * Wrapper around sendmsg() to resubmit on EINTR or short write.  Returns
+ * @true if all data was transmitted, or @false with errno for an error.
+ * Note: May alter @iov in place on resubmit.
+ */
+static bool writev_exact(int fd, struct iovec *iov, unsigned int nr)
+{
+	/* Sanity check first element isn't empty */
+	assert(iov->iov_len == sizeof(struct xsd_sockmsg));
+
+	while (nr) {
+		ssize_t res = writev(fd, iov, nr);
+
+		if (res < 0 && errno == EINTR)
+			continue;
+		if (res <= 0)
+			return false;
+
+		while (res) {
+			if (res >= iov->iov_len) {
+				/* Skip full element. */
+				res -= iov->iov_len;
+				nr--;
+				iov++;
+				continue;
+			}
+
+			/* Partial element, adjust base/len. */
+			iov->iov_len  -= res;
+			iov->iov_base += res;
+			break;
+		}
+
+		while (iov->iov_len == 0) {
+			/* Skip empty element. */
+			nr--;
+			iov++;
+		}
+	}
+
+	return true;
+}
+
+static bool write_request(struct xs_handle *h, struct iovec *iov, unsigned int nr)
+{
+	if (h->is_socket)
+		return sendmsg_exact(h->fd, iov, nr);
+	else
+		return writev_exact(h->fd, iov, nr);
+}
+
 /*
  * Send message to xenstore, get malloc'ed reply.  NULL and set errno on error.
  *
@@ -605,9 +704,8 @@ static void *xs_talkv(struct xs_handle *h,
 
 	mutex_lock(&h->request_mutex);
 
-	for (i = 0; i < num_vecs; i++)
-		if (!xs_write_all(h->fd, iovec[i].iov_base, iovec[i].iov_len))
-			goto fail;
+	if (!write_request(h, iovec, num_vecs))
+		goto fail;
 
 	ret = read_reply(h, &reply_type, len);
 	if (!ret)
Signature · 1 line
2.39.2
#17 lore raw

PATCH 5/6 tools/libxs: Use writev()/sendmsg() instead of write()

With the input data now conveniently arranged, use writev()/sendmsg() instead of decomposing it into write() calls.

This causes all requests to be submitted with a single system call, rather than at least two. While in principle short writes can occur, the chances of it happening are slim given that most xenbus comms are only a handful of bytes.

Nevertheless, provide {writev,sendmsg}_exact() wrappers which take care of resubmitting on EINTR or short write.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Frediano Ziglio <[email protected]>

v1.1:
* Fix iov overread, spotted by Frediano. Factor the common updating logic out into update_iov().

---
 tools/libs/store/xs.c | 94 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 91 insertions(+), 3 deletions(-)
tools/libs/store/xs.c +91 -3
index e820cccc2314..f80ac7558cbe 100644
@@ -563,6 +563,95 @@ static void *read_reply(
 	return body;
 }
 
+/*
+ * Update an iov/nr pair after an incomplete writev()/sendmsg().
+ *
+ * Awkwardly, nr has different widths and signs between writev() and
+ * sendmsg(), so we take it and return it by value, rather than by pointer.
+ */
+static size_t update_iov(struct iovec **p_iov, size_t nr, size_t res)
+{
+	struct iovec *iov = *p_iov;
+
+        /* Skip fully complete elements, including empty elements. */
+        while (nr && res >= iov->iov_len) {
+                res -= iov->iov_len;
+                nr--;
+                iov++;
+        }
+
+        /* Partial element, adjust base/len. */
+        if (res) {
+                iov->iov_len  -= res;
+                iov->iov_base += res;
+        }
+
+        *p_iov = iov;
+
+	return nr;
+}
+
+/*
+ * Wrapper around sendmsg() to resubmit on EINTR or short write.  Returns
+ * @true if all data was transmitted, or @false with errno for an error.
+ * Note: May alter @iov in place on resubmit.
+ */
+static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr)
+{
+	struct msghdr hdr = {
+		.msg_iov = iov,
+		.msg_iovlen = nr,
+	};
+
+	/* Sanity check first element isn't empty */
+	assert(iov->iov_len == sizeof(struct xsd_sockmsg));
+
+	while (hdr.msg_iovlen) {
+		ssize_t res = sendmsg(fd, &hdr, 0);
+
+		if (res < 0 && errno == EINTR)
+			continue;
+		if (res <= 0)
+			return false;
+
+		hdr.msg_iovlen = update_iov(&hdr.msg_iov, hdr.msg_iovlen, res);
+	}
+
+	return true;
+}
+
+/*
+ * Wrapper around sendmsg() to resubmit on EINTR or short write.  Returns
+ * @true if all data was transmitted, or @false with errno for an error.
+ * Note: May alter @iov in place on resubmit.
+ */
+static bool writev_exact(int fd, struct iovec *iov, unsigned int nr)
+{
+	/* Sanity check first element isn't empty */
+	assert(iov->iov_len == sizeof(struct xsd_sockmsg));
+
+	while (nr) {
+		ssize_t res = writev(fd, iov, nr);
+
+		if (res < 0 && errno == EINTR)
+			continue;
+		if (res <= 0)
+			return false;
+
+		nr = update_iov(&iov, nr, res);
+	}
+
+	return true;
+}
+
+static bool write_request(struct xs_handle *h, struct iovec *iov, unsigned int nr)
+{
+	if (h->is_socket)
+		return sendmsg_exact(h->fd, iov, nr);
+	else
+		return writev_exact(h->fd, iov, nr);
+}
+
 /*
  * Send message to xenstore, get malloc'ed reply.  NULL and set errno on error.
  *
@@ -605,9 +694,8 @@ static void *xs_talkv(struct xs_handle *h,
 
 	mutex_lock(&h->request_mutex);
 
-	for (i = 0; i < num_vecs; i++)
-		if (!xs_write_all(h->fd, iovec[i].iov_base, iovec[i].iov_len))
-			goto fail;
+	if (!write_request(h, iovec, num_vecs))
+		goto fail;
 
 	ret = read_reply(h, &reply_type, len);
 	if (!ret)
Signature · 1 line
2.39.2
Reviewed-by on v1 5/6
#18 lore raw

Re: [PATCH v1.1 5/6] tools/libxs: Use writev()/sendmsg() instead of write()

Quoted 64 lines · Andrew Cooper

With the input data now conveniently arranged, use writev()/sendmsg() instead of decomposing it into write() calls.

This causes all requests to be submitted with a single system call, rather than at least two. While in principle short writes can occur, the chances of it happening are slim given that most xenbus comms are only a handful of bytes.

Nevertheless, provide {writev,sendmsg}_exact() wrappers which take care of resubmitting on EINTR or short write.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Frediano Ziglio <[email protected]>

v1.1:
* Fix iov overread, spotted by Frediano. Factor the common updating logic out into update_iov().

---
  tools/libs/store/xs.c | 94 +++++++++++++++++++++++++++++++++++++++++--
  1 file changed, 91 insertions(+), 3 deletions(-)
tools/libs/store/xs.c +32 -0
index e820cccc2314..f80ac7558cbe 100644
@@ -563,6 +563,95 @@ static void *read_reply(
  	return body;
  }
 
+/*
+ * Update an iov/nr pair after an incomplete writev()/sendmsg().
+ *
+ * Awkwardly, nr has different widths and signs between writev() and
+ * sendmsg(), so we take it and return it by value, rather than by pointer.
+ */
+static size_t update_iov(struct iovec **p_iov, size_t nr, size_t res)
+{
+	struct iovec *iov = *p_iov;
+
+        /* Skip fully complete elements, including empty elements. */
+        while (nr && res >= iov->iov_len) {
+                res -= iov->iov_len;
+                nr--;
+                iov++;
+        }
+
+        /* Partial element, adjust base/len. */
+        if (res) {
+                iov->iov_len  -= res;
+                iov->iov_base += res;
+        }
+
+        *p_iov = iov;
+
+	return nr;
+}
+
+/*
+ * Wrapper around sendmsg() to resubmit on EINTR or short write.  Returns
+ * @true if all data was transmitted, or @false with errno for an error.
+ * Note: May alter @iov in place on resubmit.

tools/libs/store/xs.c static void *read_reply( · line 607

Quoted 10 lines
tools/libs/store/xs.c +10 -0
+ */
+static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr)
+{
+	struct msghdr hdr = {
+		.msg_iov = iov,
+		.msg_iovlen = nr,
+	};
+
+	/* Sanity check first element isn't empty */
+	assert(iov->iov_len == sizeof(struct xsd_sockmsg));

Can you please move this assert() into write_request(), avoiding to have 2 copies of it?

With that:

Juergen

#19 lore raw

Re: [PATCH v1.1 5/6] tools/libxs: Use writev()/sendmsg() instead of write()

Quoted 77 lines · Jürgen Groß
Quoted 76 lines · Andrew Cooper

With the input data now conveniently arranged, use writev()/sendmsg() instead
of decomposing it into write() calls.

This causes all requests to be submitted with a single system call, rather
than at least two.  While in principle short writes can occur, the chances of
it happening are slim given that most xenbus comms are only a handful of bytes.

Nevertheless, provide {writev,sendmsg}_exact() wrappers which take care of
resubmitting on EINTR or short write.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Frediano Ziglio <[email protected]>

v1.1:
  * Fix iov overread, spotted by Frediano.  Factor the common updating logic
    out into update_iov().
---
  tools/libs/store/xs.c | 94 +++++++++++++++++++++++++++++++++++++++++--

  1 file changed, 91 insertions(+), 3 deletions(-)
tools/libs/store/xs.c +0 -0
index e820cccc2314..f80ac7558cbe 100644
@@ -563,6 +563,95 @@ static void *read_reply(
       return body;
   }

  +/*
+ * Update an iov/nr pair after an incomplete writev()/sendmsg().
+ *
+ * Awkwardly, nr has different widths and signs between writev() and
+ * sendmsg(), so we take it and return it by value, rather than by pointer.
+ */

tools/libs/store/xs.c +24 -0
+static size_t update_iov(struct iovec **p_iov, size_t nr, size_t res)
+{
+    struct iovec *iov = *p_iov;
+
+        /* Skip fully complete elements, including empty elements. */
+        while (nr && res >= iov->iov_len) {
+                res -= iov->iov_len;
+                nr--;
+                iov++;
+        }
+
+        /* Partial element, adjust base/len. */
+        if (res) {
+                iov->iov_len  -= res;
+                iov->iov_base += res;
+        }
+
+        *p_iov = iov;
+
+    return nr;
+}
+
+/*
+ * Wrapper around sendmsg() to resubmit on EINTR or short write. 

Returns
+ * @true if all data was transmitted, or @false with errno for an error.
+ * Note: May alter @iov in place on resubmit.
+ */

tools/libs/store/xs.c +4 -0
+static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr)
+{
+    struct msghdr hdr = {
+        .msg_iov = iov,
Quoted 8 lines
Quoted 5 lines

+        .msg_iovlen = nr,
+    };
+
+    /* Sanity check first element isn't empty */ +    assert(iov->iov_len == sizeof(struct xsd_sockmsg));

Can you please move this assert() into write_request(), avoiding to have 2 copies of it?

It was more relevant before update_iov() was split out.

But, there's exactly the same assertion in the write_request()'s caller, so I'd prefer to simply drop it if that's ok?

The writev()/sendmsg() won't malfunction if the first element is 0, and update_iov() will now cope too, so I don't think it's necessary.

~Andrew

#20 lore raw

Re: [PATCH v1.1 5/6] tools/libxs: Use writev()/sendmsg() instead of write()

Quoted 87 lines · Andrew Cooper
Quoted 77 lines · Jürgen Groß
Quoted 76 lines · Andrew Cooper

With the input data now conveniently arranged, use writev()/sendmsg() instead
of decomposing it into write() calls.

This causes all requests to be submitted with a single system call, rather
than at least two.  While in principle short writes can occur, the chances of
it happening are slim given that most xenbus comms are only a handful of bytes.

Nevertheless, provide {writev,sendmsg}_exact() wrappers which take care of
resubmitting on EINTR or short write.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Frediano Ziglio <[email protected]>

v1.1:
  * Fix iov overread, spotted by Frediano.  Factor the common
updating logic
    out into update_iov().

---
   tools/libs/store/xs.c | 94 +++++++++++++++++++++++++++++++++++++++++--
   1 file changed, 91 insertions(+), 3 deletions(-)
tools/libs/store/xs.c +4 -0
index e820cccc2314..f80ac7558cbe 100644
@@ -563,6 +563,95 @@ static void *read_reply(
       return body;
   }
   +/*
+ * Update an iov/nr pair after an incomplete writev()/sendmsg().
+ *
+ * Awkwardly, nr has different widths and signs between writev() and
+ * sendmsg(), so we take it and return it by value, rather than by

pointer.
+ */

tools/libs/store/xs.c +24 -0
+static size_t update_iov(struct iovec **p_iov, size_t nr, size_t res)
+{
+    struct iovec *iov = *p_iov;
+
+        /* Skip fully complete elements, including empty elements. */
+        while (nr && res >= iov->iov_len) {
+                res -= iov->iov_len;
+                nr--;
+                iov++;
+        }
+
+        /* Partial element, adjust base/len. */
+        if (res) {
+                iov->iov_len  -= res;
+                iov->iov_base += res;
+        }
+
+        *p_iov = iov;
+
+    return nr;
+}
+
+/*
+ * Wrapper around sendmsg() to resubmit on EINTR or short write.

Returns
+ * @true if all data was transmitted, or @false with errno for an error.
+ * Note: May alter @iov in place on resubmit.
+ */

tools/libs/store/xs.c +4 -0
+static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr)
+{
+    struct msghdr hdr = {
+        .msg_iov = iov,
Quoted 8 lines
Quoted 5 lines

+        .msg_iovlen = nr,
+    };
+
+    /* Sanity check first element isn't empty */ +    assert(iov->iov_len == sizeof(struct xsd_sockmsg));

Can you please move this assert() into write_request(), avoiding to have 2 copies of it?

Quoted 7 lines

It was more relevant before update_iov() was split out.

But, there's exactly the same assertion in the write_request()'s caller, so I'd prefer to simply drop it if that's ok?

The writev()/sendmsg() won't malfunction if the first element is 0, and update_iov() will now cope too, so I don't think it's necessary.

Fine with me.

Juergen

Reviewed-by
#21 lore raw

Re: [PATCH v1.1 5/6] tools/libxs: Use writev()/sendmsg() instead of write()

Quoted 91 lines · Juergen Gross
Quoted 90 lines · Andrew Cooper
Quoted 78 lines · Jürgen Groß
Quoted 77 lines · Andrew Cooper

With the input data now conveniently arranged, use writev()/sendmsg() instead
of decomposing it into write() calls.

This causes all requests to be submitted with a single system call, rather
than at least two.  While in principle short writes can occur, the chances of
it happening are slim given that most xenbus comms are only a handful of bytes.

Nevertheless, provide {writev,sendmsg}_exact() wrappers which take care of
resubmitting on EINTR or short write.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Frediano Ziglio <[email protected]>

v1.1:
   * Fix iov overread, spotted by Frediano.  Factor the common updating logic
     out into update_iov().
---
   tools/libs/store/xs.c | 94
+++++++++++++++++++++++++++++++++++++++++--

   1 file changed, 91 insertions(+), 3 deletions(-)
tools/libs/store/xs.c +0 -0
index e820cccc2314..f80ac7558cbe 100644
@@ -563,6 +563,95 @@ static void *read_reply(
        return body;
    }

   +/*
+ * Update an iov/nr pair after an incomplete writev()/sendmsg().
+ *
+ * Awkwardly, nr has different widths and signs between writev() and
+ * sendmsg(), so we take it and return it by value, rather than by pointer.
+ */

tools/libs/store/xs.c +24 -0
+static size_t update_iov(struct iovec **p_iov, size_t nr, size_t res)
+{
+    struct iovec *iov = *p_iov;
+
+        /* Skip fully complete elements, including empty elements. */
+        while (nr && res >= iov->iov_len) {
+                res -= iov->iov_len;
+                nr--;
+                iov++;
+        }
+
+        /* Partial element, adjust base/len. */
+        if (res) {
+                iov->iov_len  -= res;
+                iov->iov_base += res;
+        }
+
+        *p_iov = iov;
+
+    return nr;
+}
+
+/*
+ * Wrapper around sendmsg() to resubmit on EINTR or short write.

Returns
+ * @true if all data was transmitted, or @false with errno for an error.
+ * Note: May alter @iov in place on resubmit.
+ */

tools/libs/store/xs.c +4 -0
+static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr)
+{
+    struct msghdr hdr = {
+        .msg_iov = iov,
Quoted 8 lines
Quoted 5 lines

+        .msg_iovlen = nr,
+    };
+
+    /* Sanity check first element isn't empty */ +    assert(iov->iov_len == sizeof(struct xsd_sockmsg));

Can you please move this assert() into write_request(), avoiding to have 2 copies of it?

It was more relevant before update_iov() was split out.

Quoted 7 lines
Quoted 5 lines

But, there's exactly the same assertion in the write_request()'s caller, so I'd prefer to simply drop it if that's ok?

The writev()/sendmsg() won't malfunction if the first element is 0, and update_iov() will now cope too, so I don't think it's necessary.

Fine with me.

Looks like xs_write_all() is now unused internally, but it's an exposed library function. I guess it can just be kept instead of bumping the library version.

Regards,
Jason

#22 lore raw

Re: [PATCH v1.1 5/6] tools/libxs: Use writev()/sendmsg() instead of write()

Quoted 101 lines · Jason Andryuk
Quoted 100 lines · Juergen Gross
Quoted 99 lines · Andrew Cooper
Quoted 83 lines · Jürgen Groß
Quoted 82 lines · Andrew Cooper

With the input data now conveniently arranged, use writev()/sendmsg() instead
of decomposing it into write() calls.

This causes all requests to be submitted with a single system call, rather
than at least two.  While in principle short writes can occur, the chances of
it happening are slim given that most xenbus comms are only a handful of
bytes.

Nevertheless, provide {writev,sendmsg}_exact() wrappers which take care of
resubmitting on EINTR or short write.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Frediano Ziglio <[email protected]>

v1.1:
   * Fix iov overread, spotted by Frediano.  Factor the common updating logic
     out into update_iov().
---
   tools/libs/store/xs.c | 94
+++++++++++++++++++++++++++++++++++++++++--

   1 file changed, 91 insertions(+), 3 deletions(-)
tools/libs/store/xs.c +0 -0
index e820cccc2314..f80ac7558cbe 100644
@@ -563,6 +563,95 @@ static void *read_reply(
        return body;
    }

   +/*
+ * Update an iov/nr pair after an incomplete writev()/sendmsg().
+ *
+ * Awkwardly, nr has different widths and signs between writev() and
+ * sendmsg(), so we take it and return it by value, rather than by pointer.
+ */
+static size_t update_iov(struct iovec **p_iov, size_t nr, size_t res)

tools/libs/store/xs.c +4 -0
+{
+    struct iovec *iov = *p_iov;
+
+        /* Skip fully complete elements, including empty

elements. */
+        while (nr && res >= iov->iov_len) {
+                res -= iov->iov_len;
+                nr--;
+                iov++;
+        }

tools/libs/store/xs.c +14 -0
+
+        /* Partial element, adjust base/len. */
+        if (res) {
+                iov->iov_len  -= res;
+                iov->iov_base += res;
+        }
+
+        *p_iov = iov;
+
+    return nr;
+}
+
+/*
+ * Wrapper around sendmsg() to resubmit on EINTR or short write.

Returns
+ * @true if all data was transmitted, or @false with errno for an error.
+ * Note: May alter @iov in place on resubmit.
+ */
+static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr)
+{
+    struct msghdr hdr = {
+        .msg_iov = iov,
+        .msg_iovlen = nr,

Quoted 8 lines
Quoted 4 lines

+    };
+
+    /* Sanity check first element isn't empty */ +    assert(iov->iov_len == sizeof(struct xsd_sockmsg));

Can you please move this assert() into write_request(), avoiding to have
2 copies of it?

It was more relevant before update_iov() was split out.

But, there's exactly the same assertion in the write_request()'s caller,
so I'd prefer to simply drop it if that's ok?

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

The writev()/sendmsg() won't malfunction if the first element is 0, and update_iov() will now cope too, so I don't think it's necessary.

Fine with me.

Thanks.

Quoted 4 lines

Looks like xs_write_all() is now unused internally, but it's an exposed library function.  I guess it can just be kept instead of bumping the library version.

I have a /dev/null shaped hole I'm itching to file it in, but that is going to need a soname bump.  It's just one of many dubious functions exposed...

One mess at a time.

~Andrew

#23 lore raw

PATCH 6/6 tools/libxs: Stop playing with SIGPIPE

In reply to Andrew Cooper

It's very rude for a library to play with signals behind the back of the application, no matter ones views on the default behaviour of SIGPIPE under POSIX. Even if the application doesn't care about the xenstored socket, it my care about others.

This logic has existed since xenstore/xenstored was originally added in commit 29c9e570b1ed ("Add xenstore daemon and library") in 2005.

It's also unnecessary. Pass MSG_NOSIGNAL when talking to xenstored over a pipe (to avoid sucumbing to SIGPIPE if xenstored has crashed), and forgo any playing with the signal disposition.

This has a side benefit of saving 2 syscalls per xenstore request.

---
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
 tools/libs/store/xs.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
tools/libs/store/xs.c +1 -9
index 9db5c02661f5..63d754e17014 100644
@@ -579,7 +579,7 @@ static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr)
 	assert(iov->iov_len == sizeof(struct xsd_sockmsg));
 
 	while (hdr.msg_iovlen) {
-		ssize_t res = sendmsg(fd, &hdr, 0);
+		ssize_t res = sendmsg(fd, &hdr, MSG_NOSIGNAL);
 
 		if (res < 0 && errno == EINTR)
 			continue;
@@ -680,7 +680,6 @@ static void *xs_talkv(struct xs_handle *h,
 	void *ret = NULL;
 	int saved_errno;
 	unsigned int i, msg_len;
-	struct sigaction ignorepipe, oldact;
 
 	/* Element 0 must be xsd_sockmsg */
 	assert(num_vecs >= 1);
@@ -697,11 +696,6 @@ static void *xs_talkv(struct xs_handle *h,
 
 	msg->len = msg_len;
 
-	ignorepipe.sa_handler = SIG_IGN;
-	sigemptyset(&ignorepipe.sa_mask);
-	ignorepipe.sa_flags = 0;
-	sigaction(SIGPIPE, &ignorepipe, &oldact);
-
 	mutex_lock(&h->request_mutex);
 
 	if (!write_request(h, iovec, num_vecs))
@@ -713,7 +707,6 @@ static void *xs_talkv(struct xs_handle *h,
 
 	mutex_unlock(&h->request_mutex);
 
-	sigaction(SIGPIPE, &oldact, NULL);
 	if (reply_type == XS_ERROR) {
 		saved_errno = get_error(ret);
 		free(ret);
@@ -732,7 +725,6 @@ static void *xs_talkv(struct xs_handle *h,
 	/* We're in a bad state, so close fd. */
 	saved_errno = errno;
 	mutex_unlock(&h->request_mutex);
-	sigaction(SIGPIPE, &oldact, NULL);
 close_fd:
 	close(h->fd);
 	h->fd = -1;
Signature · 1 line
2.39.2
Reviewed-by on v1 6/6
#24 lore raw

Re: [PATCH 6/6] tools/libxs: Stop playing with SIGPIPE

Quoted 8 lines · Andrew Cooper

It's very rude for a library to play with signals behind the back of the application, no matter ones views on the default behaviour of SIGPIPE under POSIX. Even if the application doesn't care about the xenstored socket, it my care about others.

This logic has existed since xenstore/xenstored was originally added in commit 29c9e570b1ed ("Add xenstore daemon and library") in 2005.

Quoted 7 lines

It's also unnecessary. Pass MSG_NOSIGNAL when talking to xenstored over a pipe (to avoid sucumbing to SIGPIPE if xenstored has crashed), and forgo any playing with the signal disposition.

This has a side benefit of saving 2 syscalls per xenstore request.

Juergen

Reviewed-by on v1 6/6
#25 lore raw

Re: [PATCH 6/6] tools/libxs: Stop playing with SIGPIPE

In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper

It's very rude for a library to play with signals behind the back of the application, no matter ones views on the default behaviour of SIGPIPE under POSIX. Even if the application doesn't care about the xenstored socket, it my care about others.

This logic has existed since xenstore/xenstored was originally added in commit 29c9e570b1ed ("Add xenstore daemon and library") in 2005.

Quoted 7 lines

It's also unnecessary. Pass MSG_NOSIGNAL when talking to xenstored over a pipe (to avoid sucumbing to SIGPIPE if xenstored has crashed), and forgo any playing with the signal disposition.

This has a side benefit of saving 2 syscalls per xenstore request.

#26 lore raw
In reply to Andrew Cooper
Quoted 29 lines · Andrew Cooper

While the purpose of this series is patch 6, it has a side effect of reducing the number of system calls substantally.

Using a strace of test-xenstore as an example, we go from this:

rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fda8278e2f0}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fda8278e2f0}, 8) = 0 write(3, "\v\0\0\0\0\0\0\0\0\0\0\0\30\0\0\0", 16) = 16
write(3, "xenstore-test/502673/a\0", 23) = 23
write(3, "a", 1) = 1
read(3, "\v\0\0\0\0\0\0\0\0\0\0\0\3\0\0\0", 16) = 16
read(3, "OK\0", 3) = 3
rt_sigaction(SIGPIPE, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fda8278e2f0}, NULL, 8) = 0

down to this:

sendmsg(3, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\v\0\0\0\0\0\0\0\0\0\0\0\30\0\0\0", iov_len=16}, {iov_base="xenstore-test/504021/a\0", iov_len=23}, {iov_base="a", iov_len=1}], msg_iovlen=3, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 40 read(3, "\v\0\0\0\0\0\0\0\0\0\0\0\3\0\0\0", 16) = 16
read(3, "OK\0", 3) = 3

I.e., it removes 2x rt_sigaction(), and turns all write()'s into a single writev() or sendmsg().

Reads are a little more problematic to deal with. Xenstored will produce a full package basically in one go, but libxenstore's reading is horrbly complicated by virtue of it being completely different depending on whether xs_watch() has created a secondary read thread or not.

Quoted 7 lines

Andrew Cooper (6):
tools/libxs: Fix length check in xs_talkv()
tools/libxs: Rework xs_talkv() to take xsd_sockmsg within the iovec tools/libxs: Rationalise the definition of struct xs_handle tools/libxs: Track whether we're using a socket or file tools/libxs: Use writev()/sendmsg() instead of write() tools/libxs: Stop playing with SIGPIPE

The title of the entire series containing "Fix" vs there being no single
Fixes: tag throughout, afaics, leave unclear to me whether any or all of this work wants/needs backporting. Please clarify.

Jan

#27 lore raw
Quoted 37 lines · Jan Beulich
Quoted 29 lines · Andrew Cooper

While the purpose of this series is patch 6, it has a side effect of reducing the number of system calls substantally.

Using a strace of test-xenstore as an example, we go from this:

rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fda8278e2f0}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fda8278e2f0}, 8) = 0 write(3, "\v\0\0\0\0\0\0\0\0\0\0\0\30\0\0\0", 16) = 16
write(3, "xenstore-test/502673/a\0", 23) = 23
write(3, "a", 1) = 1
read(3, "\v\0\0\0\0\0\0\0\0\0\0\0\3\0\0\0", 16) = 16
read(3, "OK\0", 3) = 3
rt_sigaction(SIGPIPE, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fda8278e2f0}, NULL, 8) = 0

down to this:

sendmsg(3, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\v\0\0\0\0\0\0\0\0\0\0\0\30\0\0\0", iov_len=16}, {iov_base="xenstore-test/504021/a\0", iov_len=23}, {iov_base="a", iov_len=1}], msg_iovlen=3, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 40 read(3, "\v\0\0\0\0\0\0\0\0\0\0\0\3\0\0\0", 16) = 16
read(3, "OK\0", 3) = 3

I.e., it removes 2x rt_sigaction(), and turns all write()'s into a single writev() or sendmsg().

Reads are a little more problematic to deal with. Xenstored will produce a full package basically in one go, but libxenstore's reading is horrbly complicated by virtue of it being completely different depending on whether xs_watch() has created a secondary read thread or not.

Quoted 7 lines

Andrew Cooper (6):
tools/libxs: Fix length check in xs_talkv()
tools/libxs: Rework xs_talkv() to take xsd_sockmsg within the iovec tools/libxs: Rationalise the definition of struct xs_handle tools/libxs: Track whether we're using a socket or file tools/libxs: Use writev()/sendmsg() instead of write() tools/libxs: Stop playing with SIGPIPE

Quoted 3 lines

The title of the entire series containing "Fix" vs there being no single
Fixes: tag throughout, afaics, leave unclear to me whether any or all of this work wants/needs backporting. Please clarify.

I was on the fence about whether I should have used Fixes: (2005 commit) in patch 6 or not.

Also I've had a spectacularly bad run of luck with the CLOEXEC fixes so leaving them to bake for a little longer in staging is no bad thing. That said, I am likely to backport them into the patchqueue imminently.

Personally, I think it is very buggy for libxenstore to play with SIGPIPE, but it also came from reading the strace of the CLOEXEC fixes and thinking "well that's clearly broken too" rather than from a real SIGPIPE problem.

~Andrew

P.S. Then again, the Xen libraries all get away with murder doing things that libraries must never do...  I think it's still unforgivable that libxentoollog adjusts stdout/stderr if you pass NULL into xc_open_handle(), and that we have 5 different trivial few-kb libraries which are co-dependent on each other when it should be a single one.