All of lore.kernel.org
 help / color / mirror / Atom feed
From: Slavomir Kaslev <kaslevs@vmware.com>
To: linux-trace-devel@vger.kernel.org
Cc: rostedt@goodmis.org, slavomir.kaslev@gmail.com,
	tstoyanov@vmware.com, ykaradzhov@vmware.com
Subject: [PATCH 8/8] trace-cmd: Acknowledge unexpected protocol messages
Date: Mon,  4 Feb 2019 09:08:55 +0200	[thread overview]
Message-ID: <20190204070855.8921-9-kaslevs@vmware.com> (raw)
In-Reply-To: <20190204070855.8921-1-kaslevs@vmware.com>

Send MSG_NOT_SUPP message back on unexpected incoming messages. This allows us
to add new commands in the future and be able to detect and handle if we're
talking with an older version of trace-cmd.

Signed-off-by: Slavomir Kaslev <kaslevs@vmware.com>
---
 tracecmd/trace-msg.c | 49 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 41 insertions(+), 8 deletions(-)

diff --git a/tracecmd/trace-msg.c b/tracecmd/trace-msg.c
index 5079d43..51d0ac8 100644
--- a/tracecmd/trace-msg.c
+++ b/tracecmd/trace-msg.c
@@ -75,7 +75,8 @@ struct tracecmd_msg_header {
 	C(TINIT,	1,	sizeof(struct tracecmd_msg_tinit)),	\
 	C(RINIT,	2,	sizeof(struct tracecmd_msg_rinit)),	\
 	C(SEND_DATA,	3,	0),					\
-	C(FIN_DATA,	4,	0),
+	C(FIN_DATA,	4,	0),					\
+	C(NOT_SUPP,	5,	0),
 
 #undef C
 #define C(a,b,c)	MSG_##a = b
@@ -370,6 +371,25 @@ static int tracecmd_msg_wait_for_msg(int fd, struct tracecmd_msg *msg)
 	return 0;
 }
 
+static int tracecmd_msg_send_notsupp(struct tracecmd_msg_handle *msg_handle)
+{
+	struct tracecmd_msg msg;
+
+	tracecmd_msg_init(MSG_NOT_SUPP, &msg);
+	return tracecmd_msg_send(msg_handle->fd, &msg);
+}
+
+static int handle_unexpected_msg(struct tracecmd_msg_handle *msg_handle,
+				 struct tracecmd_msg *msg)
+{
+	/* Don't send MSG_NOT_SUPP back if we just received one */
+	if (ntohl(msg->hdr.cmd) == MSG_NOT_SUPP)
+		return 0;
+
+	return tracecmd_msg_send_notsupp(msg_handle);
+
+}
+
 int tracecmd_msg_send_init_data(struct tracecmd_msg_handle *msg_handle,
 				unsigned int **client_ports)
 {
@@ -397,7 +417,7 @@ int tracecmd_msg_send_init_data(struct tracecmd_msg_handle *msg_handle,
 		goto out;
 
 	if (ntohl(msg.hdr.cmd) != MSG_RINIT) {
-		ret = -EINVAL;
+		ret = -EOPNOTSUPP;
 		goto error;
 	}
 
@@ -413,6 +433,8 @@ int tracecmd_msg_send_init_data(struct tracecmd_msg_handle *msg_handle,
 
 error:
 	error_operation(&msg);
+	if (ret == -EOPNOTSUPP)
+		handle_unexpected_msg(msg_handle, &msg);
 out:
 	msg_free(&msg);
 	return ret;
@@ -461,7 +483,6 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle)
 	int ret;
 	int offset = 0;
 	u32 size;
-	u32 cmd;
 
 	ret = tracecmd_msg_recv_wait(msg_handle->fd, &msg);
 	if (ret < 0) {
@@ -470,9 +491,8 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle)
 		return ret;
 	}
 
-	cmd = ntohl(msg.hdr.cmd);
-	if (cmd != MSG_TINIT) {
-		ret = -EINVAL;
+	if (ntohl(msg.hdr.cmd) != MSG_TINIT) {
+		ret = -EOPNOTSUPP;
 		goto error;
 	}
 
@@ -524,10 +544,14 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle)
 		}
 	}
 
+	msg_free(&msg);
 	return pagesize;
 
 error:
 	error_operation(&msg);
+	if (ret == -EOPNOTSUPP)
+		handle_unexpected_msg(msg_handle, &msg);
+	msg_free(&msg);
 	return ret;
 }
 
@@ -627,8 +651,12 @@ int tracecmd_msg_read_data(struct tracecmd_msg_handle *msg_handle, int ofd)
 		if (cmd == MSG_FIN_DATA) {
 			/* Finish receiving data */
 			break;
-		} else if (cmd != MSG_SEND_DATA)
-			goto error;
+		} else if (cmd != MSG_SEND_DATA) {
+			ret = handle_unexpected_msg(msg_handle, &msg);
+			if (ret < 0)
+				goto error;
+			goto next;
+		}
 
 		n = ntohl(msg.hdr.size) - MSG_HDR_LEN - ntohl(msg.hdr.cmd_size);
 		t = n;
@@ -646,6 +674,7 @@ int tracecmd_msg_read_data(struct tracecmd_msg_handle *msg_handle, int ofd)
 			s = n - t;
 		}
 
+next:
 		msg_free(&msg);
 	}
 
@@ -683,6 +712,10 @@ int tracecmd_msg_wait_close(struct tracecmd_msg_handle *msg_handle)
 			return 0;
 
 		error_operation(&msg);
+		ret = handle_unexpected_msg(msg_handle, &msg);
+		if (ret < 0)
+			goto error;
+
 		msg_free(&msg);
 	}
 
-- 
2.19.1


      parent reply	other threads:[~2019-02-04  7:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-04  7:08 [PATCH 0/8] trace-cmd protocol fixes Slavomir Kaslev
2019-02-04  7:08 ` [PATCH 1/8] trace-cmd: Remove unused global variable Slavomir Kaslev
2019-02-04  7:08 ` [PATCH 2/8] trace-cmd: Rename error_operation_for_server Slavomir Kaslev
2019-02-04  7:08 ` [PATCH 3/8] trace-cmd: Remove tracecmd_msg_handle/tracecmd_msg_server distinction Slavomir Kaslev
2019-02-04  7:08 ` [PATCH 4/8] trace-cmd: Check if connection is done when reading data in tracecmd_msg_read_data Slavomir Kaslev
2019-02-04  7:08 ` [PATCH 5/8] trace-cmd: Fix a memory leak in tracecmd_msg_send_init_data Slavomir Kaslev
2019-02-04  7:08 ` [PATCH 6/8] trace-cmd: Make tracecmd_msg_send_close return error code if any Slavomir Kaslev
2019-02-05 15:14   ` Steven Rostedt
2019-02-07 12:52     ` Slavomir Kaslev
2019-02-07 14:41       ` Steven Rostedt
2019-02-08 19:34     ` Steven Rostedt
2019-02-04  7:08 ` [PATCH 7/8] trace-cmd: Add tracecmd_msg_wait_close function Slavomir Kaslev
2019-02-04  7:08 ` Slavomir Kaslev [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190204070855.8921-9-kaslevs@vmware.com \
    --to=kaslevs@vmware.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=slavomir.kaslev@gmail.com \
    --cc=tstoyanov@vmware.com \
    --cc=ykaradzhov@vmware.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.