diff mbox series

[8/8] trace-cmd input: Add validation updates to the copy of a handle

Message ID 20210301143857.841258346@goodmis.org (mailing list archive)
State Superseded
Headers show
Series trace-cmd: Fixes for trace-cmd restore | expand

Commit Message

Steven Rostedt March 1, 2021, 2:37 p.m. UTC
From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

When copying the input handle the file descriptor is changed. Change its
state along with that.

Currently the tracecmd_copy_headers() restores the original state, but does
not restore the file descriptor. That may need to change.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 lib/trace-cmd/trace-input.c | 52 +++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index 53c2722f46e7..15d6d2b3ca43 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -3491,12 +3491,14 @@  static int copy_header_files(struct tracecmd_input *handle, int fd)
 {
 	unsigned long long size;
 
-	/* The input handle has to have at least read the headers */
 	if (handle->file_state < TRACECMD_FILE_HEADERS)
 		return -1;
 
 	lseek64(handle->fd, handle->header_files_start, SEEK_SET);
 
+	/* Now that the file handle has moved, change its state */
+	handle->file_state = TRACECMD_FILE_HEADERS;
+
 	/* "header_page"  */
 	if (read_copy_data(handle, 12, fd) < 0)
 		return -1;
@@ -3526,8 +3528,7 @@  static int copy_ftrace_files(struct tracecmd_input *handle, int fd)
 	unsigned int count;
 	unsigned int i;
 
-	/* The input handle has to have at least read the ftrace events */
-	if (handle->file_state < TRACECMD_FILE_FTRACE_EVENTS)
+	if (handle->file_state != TRACECMD_FILE_FTRACE_EVENTS - 1)
 		return -1;
 
 	if (read_copy_size4(handle, fd, &count) < 0)
@@ -3542,6 +3543,8 @@  static int copy_ftrace_files(struct tracecmd_input *handle, int fd)
 			return -1;
 	}
 
+	handle->file_state = TRACECMD_FILE_FTRACE_EVENTS;
+
 	return 0;
 }
 
@@ -3553,8 +3556,7 @@  static int copy_event_files(struct tracecmd_input *handle, int fd)
 	unsigned int count;
 	unsigned int i,x;
 
-	/* The input handle has to have at least read all its events */
-	if (handle->file_state < TRACECMD_FILE_ALL_EVENTS)
+	if (handle->file_state != TRACECMD_FILE_ALL_EVENTS - 1)
 		return -1;
 
 	if (read_copy_size4(handle, fd, &systems) < 0)
@@ -3582,6 +3584,8 @@  static int copy_event_files(struct tracecmd_input *handle, int fd)
 		}
 	}
 
+	handle->file_state = TRACECMD_FILE_ALL_EVENTS;
+
 	return 0;
 }
 
@@ -3589,8 +3593,7 @@  static int copy_proc_kallsyms(struct tracecmd_input *handle, int fd)
 {
 	unsigned int size;
 
-	/* The input handle has to have at least has kallsyms */
-	if (handle->file_state < TRACECMD_FILE_KALLSYMS)
+	if (handle->file_state != TRACECMD_FILE_KALLSYMS - 1)
 		return -1;
 
 	if (read_copy_size4(handle, fd, &size) < 0)
@@ -3601,6 +3604,8 @@  static int copy_proc_kallsyms(struct tracecmd_input *handle, int fd)
 	if (read_copy_data(handle, size, fd) < 0)
 		return -1;
 
+	handle->file_state = TRACECMD_FILE_KALLSYMS;
+
 	return 0;
 }
 
@@ -3608,8 +3613,7 @@  static int copy_ftrace_printk(struct tracecmd_input *handle, int fd)
 {
 	unsigned int size;
 
-	/* The input handle has to have at least has printk stored */
-	if (handle->file_state < TRACECMD_FILE_PRINTK)
+	if (handle->file_state != TRACECMD_FILE_PRINTK - 1)
 		return -1;
 
 	if (read_copy_size4(handle, fd, &size) < 0)
@@ -3620,6 +3624,8 @@  static int copy_ftrace_printk(struct tracecmd_input *handle, int fd)
 	if (read_copy_data(handle, size, fd) < 0)
 		return -1;
 
+	handle->file_state = TRACECMD_FILE_PRINTK;
+
 	return 0;
 }
 
@@ -3627,8 +3633,7 @@  static int copy_command_lines(struct tracecmd_input *handle, int fd)
 {
 	unsigned long long size;
 
-	/* The input handle has to have at least read the cmdlines */
-	if (handle->file_state < TRACECMD_FILE_CMD_LINES)
+	if (handle->file_state != TRACECMD_FILE_CMD_LINES - 1)
 		return -1;
 
 	if (read_copy_size8(handle, fd, &size) < 0)
@@ -3639,38 +3644,47 @@  static int copy_command_lines(struct tracecmd_input *handle, int fd)
 	if (read_copy_data(handle, size, fd) < 0)
 		return -1;
 
+	handle->file_state = TRACECMD_FILE_CMD_LINES;
+
 	return 0;
 }
 
 int tracecmd_copy_headers(struct tracecmd_input *handle, int fd)
 {
+	int save_state = handle->file_state;
 	int ret;
 
+	/* Make sure that the input handle is up to cmd lines */
+	if (handle->file_state < TRACECMD_FILE_CMD_LINES)
+		return -1;
+
 	ret = copy_header_files(handle, fd);
 	if (ret < 0)
-		return -1;
+		goto out;
 
 	ret = copy_ftrace_files(handle, fd);
 	if (ret < 0)
-		return -1;
+		goto out;
 
 	ret = copy_event_files(handle, fd);
 	if (ret < 0)
-		return -1;
+		goto out;
 
 	ret = copy_proc_kallsyms(handle, fd);
 	if (ret < 0)
-		return -1;
+		goto out;
 
 	ret = copy_ftrace_printk(handle, fd);
 	if (ret < 0)
-		return -1;
+		goto out;
 
 	ret = copy_command_lines(handle, fd);
-	if (ret < 0)
-		return -1;
 
-	return 0;
+ out:
+	/* Restore the handle back to its original state */
+	handle->file_state = save_state;
+
+	return ret < 0 ? -1 : 0;
 }
 
 /**