diff mbox series

[11/13,v2] libtracefs: Add pthread_mutex_lock() around tracefs_function_filter()

Message ID 20210330005248.836930575@goodmis.org (mailing list archive)
State Accepted
Commit 58d15641674ac6cf63c339dca4037b858f55430c
Headers show
Series libtracefs: Add tracefs_function_filter() | expand

Commit Message

Steven Rostedt March 30, 2021, 12:51 a.m. UTC
From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

In order to make tracefs_function_filter() thread safe, add a pthread
mutex around the entire function to only let one thread access it at a
time.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 src/tracefs-tools.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/src/tracefs-tools.c b/src/tracefs-tools.c
index 6d03b4856a63..7e191e207867 100644
--- a/src/tracefs-tools.c
+++ b/src/tracefs-tools.c
@@ -13,6 +13,7 @@ 
 #include <regex.h>
 #include <dirent.h>
 #include <limits.h>
+#include <pthread.h>
 #include <errno.h>
 
 #include "tracefs.h"
@@ -22,6 +23,8 @@ 
 #define TRACE_FILTER		"set_ftrace_filter"
 #define TRACE_FILTER_LIST	"available_filter_functions"
 
+static pthread_mutex_t filter_lock = PTHREAD_MUTEX_INITIALIZER;
+
 static const char * const options_map[] = {
 	"unknown",
 	"annotate",
@@ -867,15 +870,16 @@  int tracefs_function_filter(struct tracefs_instance *instance, const char **filt
 	char *ftrace_filter_path;
 	bool reset = flags & TRACEFS_FL_RESET;
 	int open_flags;
-	int ret;
+	int ret = 1;
 	int fd;
 
+	pthread_mutex_lock(&filter_lock);
 	if (!filters)
-		return 1;
+		goto out;
 
 	func_filters = make_func_filters(filters);
 	if (!func_filters)
-		return 1;
+		goto out;
 
 	/* Make sure errs is NULL to start with, realloc() depends on it. */
 	if (errs)
@@ -906,6 +910,8 @@  int tracefs_function_filter(struct tracefs_instance *instance, const char **filt
  out_free:
 	free_func_list(func_list);
 	free_func_filters(func_filters);
+ out:
+	pthread_mutex_unlock(&filter_lock);
 
 	return ret;
 }