diff mbox series

[v2,4/6] trace-cmd: Simplify and fix memory leaks in tracecmd_msg_collect_data

Message ID 20190108150015.21327-5-kaslevs@vmware.com (mailing list archive)
State Accepted
Headers show
Series Add VM guest kernel tracing using vsock sockets for transport | expand

Commit Message

Slavomir Kaslev Jan. 8, 2019, 3 p.m. UTC
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(-)

Comments

Steven Rostedt Jan. 8, 2019, 10:40 p.m. UTC | #1
On Tue,  8 Jan 2019 17:00:13 +0200
Slavomir Kaslev <kaslevs@vmware.com> wrote:

> 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.
>

I re-ordered the patch series and made this patch 3/6 and made the
original 3/6 the 4/6. The first 3 patches (including this one) are
being pulled into the main branch.

I'll keep the virt-server branch separate still.

-- Steve
diff mbox series

Patch

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;
 }