linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Slavomir Kaslev <kaslevs@vmware.com>
To: linux-trace-devel@vger.kernel.org
Cc: rostedt@goodmis.org, ykaradzhov@vmware.com, tstoyanov@vmware.com
Subject: [PATCH v2 4/6] trace-cmd: Simplify and fix memory leaks in tracecmd_msg_collect_data
Date: Tue,  8 Jan 2019 17:00:13 +0200	[thread overview]
Message-ID: <20190108150015.21327-5-kaslevs@vmware.com> (raw)
In-Reply-To: <20190108150015.21327-1-kaslevs@vmware.com>

This patch addresses leaking tracecmd_msg messages in tracecmd_msg_collect_data.

It also splits out tracecmd_msg_read_data from tracecmd_msg_collect_data which
only reads incomming data and doesn't wait for a final MSG_CLOSE message and is
used by the VM tracing agent.

Signed-off-by: Slavomir Kaslev <kaslevs@vmware.com>
---
 include/trace-cmd/trace-cmd.h |  1 +
 tracecmd/trace-msg.c          | 28 ++++++++++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h
index 9ae17cf..c1b18fa 100644
--- a/include/trace-cmd/trace-cmd.h
+++ b/include/trace-cmd/trace-cmd.h
@@ -332,6 +332,7 @@ void tracecmd_msg_send_close_msg(struct tracecmd_msg_handle *msg_handle);
 int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle);
 int tracecmd_msg_send_port_array(struct tracecmd_msg_handle *msg_handle,
 				 int *ports);
+int tracecmd_msg_read_data(struct tracecmd_msg_handle *msg_handle, int ofd);
 int tracecmd_msg_collect_data(struct tracecmd_msg_handle *msg_handle, int ofd);
 bool tracecmd_msg_done(struct tracecmd_msg_handle *msg_handle);
 void tracecmd_msg_set_done(struct tracecmd_msg_handle *msg_handle);
diff --git a/tracecmd/trace-msg.c b/tracecmd/trace-msg.c
index 1a2ec40..edde582 100644
--- a/tracecmd/trace-msg.c
+++ b/tracecmd/trace-msg.c
@@ -626,7 +626,7 @@ int tracecmd_msg_finish_sending_data(struct tracecmd_msg_handle *msg_handle)
 	return 0;
 }
 
-int tracecmd_msg_collect_data(struct tracecmd_msg_handle *msg_handle, int ofd)
+int tracecmd_msg_read_data(struct tracecmd_msg_handle *msg_handle, int ofd)
 {
 	struct tracecmd_msg msg;
 	int t, n, cmd;
@@ -659,13 +659,34 @@ int tracecmd_msg_collect_data(struct tracecmd_msg_handle *msg_handle, int ofd)
 				if (errno == EINTR)
 					continue;
 				warning("writing to file");
-				return -errno;
+				ret = -errno;
+				goto error;
 			}
 			t -= s;
 			s = n - t;
 		}
+
+		msg_free(&msg);
 	}
 
+	return 0;
+
+error:
+	error_operation_for_server(&msg);
+	msg_free(&msg);
+	return ret;
+}
+
+int tracecmd_msg_collect_data(struct tracecmd_msg_handle *msg_handle, int ofd)
+{
+	struct tracecmd_msg msg;
+	u32 cmd;
+	int ret;
+
+	ret = tracecmd_msg_read_data(msg_handle, ofd);
+	if (ret)
+		goto error;
+
 	/* check the finish message of the client */
 	while (!tracecmd_msg_done(msg_handle)) {
 		ret = tracecmd_msg_recv(msg_handle->fd, &msg);
@@ -683,11 +704,14 @@ int tracecmd_msg_collect_data(struct tracecmd_msg_handle *msg_handle, int ofd)
 			ret = -EINVAL;
 			goto error;
 		}
+
+		msg_free(&msg);
 	}
 
 	return 0;
 
 error:
 	error_operation_for_server(&msg);
+	msg_free(&msg);
 	return ret;
 }
-- 
2.19.1

  parent reply	other threads:[~2019-01-08 15:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-08 15:00 [PATCH v2 0/6] Add VM guest kernel tracing using vsock sockets for transport Slavomir Kaslev
2019-01-08 15:00 ` [PATCH v2 1/6] trace-cmd: Fix indentation Slavomir Kaslev
2019-01-08 15:00 ` [PATCH v2 2/6] trace-cmd: Fix warnings reported by gcc 8.2 Slavomir Kaslev
2019-01-08 22:32   ` Steven Rostedt
2019-01-08 15:00 ` [PATCH v2 3/6] trace-cmd: Add tracecmd_create_recorder_virt function Slavomir Kaslev
2019-01-08 15:00 ` Slavomir Kaslev [this message]
2019-01-08 22:40   ` [PATCH v2 4/6] trace-cmd: Simplify and fix memory leaks in tracecmd_msg_collect_data Steven Rostedt
2019-01-08 15:00 ` [PATCH v2 5/6] trace-cmd: Add TRACE_REQ and TRACE_RESP messages Slavomir Kaslev
2019-01-08 23:01   ` Steven Rostedt
2019-01-08 15:00 ` [PATCH v2 6/6] trace-cmd: Add VM kernel tracing using vsock socket for transport Slavomir Kaslev
2019-01-09 21:20 ` [PATCH v2 0/6] Add VM guest kernel tracing using vsock sockets " Steven Rostedt

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=20190108150015.21327-5-kaslevs@vmware.com \
    --to=kaslevs@vmware.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).