diff mbox series

[v2,35/48] multipathd: uxlsnr: use recv() for command length

Message ID 20211118225840.19810-36-mwilck@suse.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show
Series multipathd: uxlsnr overhaul | expand

Commit Message

Martin Wilck Nov. 18, 2021, 10:58 p.m. UTC
From: Martin Wilck <mwilck@suse.com>

If the peer uses libmpathcmd, we can be certain that the first
8 bytes are being sent in a single chunk of data. It's overkill
to try and receive the command length byte-by-byte.

Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 multipathd/uxlsnr.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

Comments

Benjamin Marzinski Nov. 25, 2021, 1:54 a.m. UTC | #1
On Thu, Nov 18, 2021 at 11:58:27PM +0100, mwilck@suse.com wrote:
> From: Martin Wilck <mwilck@suse.com>
> 
> If the peer uses libmpathcmd, we can be certain that the first
> 8 bytes are being sent in a single chunk of data. It's overkill
> to try and receive the command length byte-by-byte.
> 
Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> ---
>  multipathd/uxlsnr.c | 17 +++++++++--------
>  1 file changed, 9 insertions(+), 8 deletions(-)
> 
> diff --git a/multipathd/uxlsnr.c b/multipathd/uxlsnr.c
> index 24db377..6355279 100644
> --- a/multipathd/uxlsnr.c
> +++ b/multipathd/uxlsnr.c
> @@ -441,6 +441,7 @@ static int client_state_machine(struct client *c, struct vectors *vecs,
>  		if (!(revents & POLLIN))
>  			return STM_BREAK;
>  		if (c->cmd_len == 0) {
> +			size_t len;
>  			/*
>  			 * We got POLLIN; assume that at least the length can
>  			 * be read immediately.
> @@ -449,17 +450,17 @@ static int client_state_machine(struct client *c, struct vectors *vecs,
>  			c->expires.tv_sec += uxsock_timeout / 1000;
>  			c->expires.tv_nsec += (uxsock_timeout % 1000) * 1000000;
>  			normalize_timespec(&c->expires);
> -			n = mpath_recv_reply_len(c->fd, 0);
> -			if (n == -1) {
> -				condlog(1, "%s: cli[%d]: failed to receive reply len",
> -					__func__, c->fd);
> -				c->error = -ECONNRESET;
> -			} else if (n > _MAX_CMD_LEN) {
> -				condlog(1, "%s: cli[%d]: overlong command (%zd bytes)",
> +			n = recv(c->fd, &len, sizeof(len), 0);
> +			if (n < (ssize_t)sizeof(len)) {
> +				condlog(1, "%s: cli[%d]: failed to receive reply len: %zd",
>  					__func__, c->fd, n);
>  				c->error = -ECONNRESET;
> +			} else if (len <= 0 || len > _MAX_CMD_LEN) {
> +				condlog(1, "%s: cli[%d]: invalid command length (%zu bytes)",
> +					__func__, c->fd, len);
> +				c->error = -ECONNRESET;
>  			} else {
> -				c->cmd_len = n;
> +				c->cmd_len = len;
>  				condlog(4, "%s: cli[%d]: connected", __func__, c->fd);
>  			}
>  			/* poll for data */
> -- 
> 2.33.1

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel
diff mbox series

Patch

diff --git a/multipathd/uxlsnr.c b/multipathd/uxlsnr.c
index 24db377..6355279 100644
--- a/multipathd/uxlsnr.c
+++ b/multipathd/uxlsnr.c
@@ -441,6 +441,7 @@  static int client_state_machine(struct client *c, struct vectors *vecs,
 		if (!(revents & POLLIN))
 			return STM_BREAK;
 		if (c->cmd_len == 0) {
+			size_t len;
 			/*
 			 * We got POLLIN; assume that at least the length can
 			 * be read immediately.
@@ -449,17 +450,17 @@  static int client_state_machine(struct client *c, struct vectors *vecs,
 			c->expires.tv_sec += uxsock_timeout / 1000;
 			c->expires.tv_nsec += (uxsock_timeout % 1000) * 1000000;
 			normalize_timespec(&c->expires);
-			n = mpath_recv_reply_len(c->fd, 0);
-			if (n == -1) {
-				condlog(1, "%s: cli[%d]: failed to receive reply len",
-					__func__, c->fd);
-				c->error = -ECONNRESET;
-			} else if (n > _MAX_CMD_LEN) {
-				condlog(1, "%s: cli[%d]: overlong command (%zd bytes)",
+			n = recv(c->fd, &len, sizeof(len), 0);
+			if (n < (ssize_t)sizeof(len)) {
+				condlog(1, "%s: cli[%d]: failed to receive reply len: %zd",
 					__func__, c->fd, n);
 				c->error = -ECONNRESET;
+			} else if (len <= 0 || len > _MAX_CMD_LEN) {
+				condlog(1, "%s: cli[%d]: invalid command length (%zu bytes)",
+					__func__, c->fd, len);
+				c->error = -ECONNRESET;
 			} else {
-				c->cmd_len = n;
+				c->cmd_len = len;
 				condlog(4, "%s: cli[%d]: connected", __func__, c->fd);
 			}
 			/* poll for data */