diff mbox series

[v2,4/4] libtracefs: Add tracefs_tracing_dir_is_mounted() API

Message ID 20221114204522.2433500-5-rostedt@goodmis.org (mailing list archive)
State Accepted
Commit 7c2b882ed11639736070beb97d5748942813742a
Headers show
Series Add some helpful APIS | expand

Commit Message

Steven Rostedt Nov. 14, 2022, 8:45 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Add the tracefs_tracing_dir_is_mounted() API that passes in a mount flag
and an address to a char pointer (or NULL). It returns 1 if the
tracing_dir was already mounted and 0 if not. If the mount flag is set,
then it will try to mount it and return -1 if it fails. If path points to
the address of a char*, then it will be set to the path that was mounted,
or was already mounted.

This is useful for wanting to unmount the tracing_dir if the tool was
the one that mounted it.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 Documentation/libtracefs-files.txt | 14 ++++++-
 Documentation/libtracefs.txt       |  1 +
 include/tracefs.h                  |  1 +
 src/tracefs-utils.c                | 62 ++++++++++++++++++++++++------
 4 files changed, 64 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/Documentation/libtracefs-files.txt b/Documentation/libtracefs-files.txt
index 2a3f5449839d..d22e75900098 100644
--- a/Documentation/libtracefs-files.txt
+++ b/Documentation/libtracefs-files.txt
@@ -3,8 +3,8 @@  libtracefs(3)
 
 NAME
 ----
-tracefs_get_tracing_file, tracefs_put_tracing_file, tracefs_tracing_dir, tracefs_debug_dir, tracefs_set_tracing_dir -
-Find and set locations of trace directory and files.
+tracefs_get_tracing_file, tracefs_put_tracing_file, tracefs_tracing_dir, tracefs_debug_dir, tracefs_set_tracing_dir,
+tracefs_tracing_dir_is_mounted - Find and set locations of trace directory and files.
 
 SYNOPSIS
 --------
@@ -17,6 +17,7 @@  void *tracefs_put_tracing_file*(char pass:[*]_name_);
 const char pass:[*]*tracefs_tracing_dir*(void);
 const char pass:[*]*tracefs_debug_dir*(void);
 int *tracefs_set_tracing_dir*(char pass:[*]_tracing_dir_)
+int *tracefs_tracing_dir_is_mounted*(bool _mount_, const char pass:[**]_path_);
 --
 
 DESCRIPTION
@@ -52,6 +53,12 @@  that it will return where the debugfs file system is mounted. If it
 is not mounted it will try to mount it. The return string must _not_
 be freed.
 
+*tracefs_tracing_dir_is_mounted()* returns 1 if the tracing directory is
+already mounted and 0 if it is not. If _mount_ is true, it will try to
+mount it if it is not, and returns 0 if it succesfully mounted it and -1
+if it did not. If _path_ is set, it will be assigned to the path where it
+mounted it. _path_ is internal and should not be freed.
+
 RETURN VALUE
 ------------
 The *tracefs_set_tracing_dir()* function returns 0 on success, -1 otherwise.
@@ -65,6 +72,9 @@  in case of an error. The returned string must _not_ be freed.
 The *tracefs_debug_dir()* function returns a constant string or NULL
 in case of an error. The returned string must _not_ be freed.
 
+The *tracefs_tracing_dir_is_mounted()* returns 1 if the tracing directory
+is already mounted, 0 if it is not, and -1 on error.
+
 EXAMPLE
 -------
 [source,c]
diff --git a/Documentation/libtracefs.txt b/Documentation/libtracefs.txt
index 73c4997efeef..cc53388438e5 100644
--- a/Documentation/libtracefs.txt
+++ b/Documentation/libtracefs.txt
@@ -17,6 +17,7 @@  Locations of tracing files and directories:
 	const char pass:[*]*tracefs_tracing_dir*(void);
 	const char pass:[*]*tracefs_debug_dir*(void);
 	int *tracefs_set_tracing_dir*(char pass:[*]_tracing_dir_)
+	int *tracefs_tracing_dir_is_mounted*(bool _mount_, const char pass:[**]_path_);
 
 Trace instances:
 	struct tracefs_instance pass:[*]*tracefs_instance_create*(const char pass:[*]_name_);
diff --git a/include/tracefs.h b/include/tracefs.h
index 5ff0c6f134d3..10f84a25c31e 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -17,6 +17,7 @@  void tracefs_put_tracing_file(char *name);
 const char *tracefs_tracing_dir(void);
 const char *tracefs_debug_dir(void);
 int tracefs_set_tracing_dir(char *tracing_dir);
+int tracefs_tracing_dir_is_mounted(bool mount, const char **path);
 
 /* ftrace instances */
 struct tracefs_instance;
diff --git a/src/tracefs-utils.c b/src/tracefs-utils.c
index a38da6616498..777912e46821 100644
--- a/src/tracefs-utils.c
+++ b/src/tracefs-utils.c
@@ -85,14 +85,7 @@  static int mount_debugfs(void)
 	return ret;
 }
 
-/**
- * trace_find_tracing_dir - Find tracing directory
- * @debugfs: Boolean to just return the debugfs directory
- *
- * Returns string containing the full path to the system's tracing directory.
- * The string must be freed by free()
- */
-__hidden char *trace_find_tracing_dir(bool debugfs)
+static char *find_tracing_dir(bool debugfs, bool mount)
 {
 	char *debug_str = NULL;
 	char fspath[PATH_MAX+1];
@@ -127,18 +120,19 @@  __hidden char *trace_find_tracing_dir(bool debugfs)
 
 	if (debugfs) {
 		if (strcmp(type, "debugfs") != 0) {
-			if (mount_debugfs() < 0)
+			if (!mount || mount_debugfs() < 0)
 				return NULL;
 			strcpy(fspath, DEBUGFS_PATH);
 		}
 	} else if (strcmp(type, "tracefs") != 0) {
-		if (mount_tracefs() < 0) {
+		if (!mount || mount_tracefs() < 0) {
 			if (debug_str) {
 				strncpy(fspath, debug_str, PATH_MAX);
 				fspath[PATH_MAX] = 0;
 			} else {
-				if (mount_debugfs() < 0) {
-					tracefs_warning("debugfs not mounted, please mount");
+				if (!mount || mount_debugfs() < 0) {
+					if (mount)
+						tracefs_warning("debugfs not mounted, please mount");
 					free(debug_str);
 					return NULL;
 				}
@@ -165,6 +159,50 @@  __hidden char *trace_find_tracing_dir(bool debugfs)
 	return tracing_dir;
 }
 
+/**
+ * tracefs_tracing_dir_is_mounted - test if the tracing dir is already mounted
+ * @mount: Mount it if it is not already mounted
+ * @path: the path to the tracing directory if mounted or was mounted
+ *
+ * Returns 1 if the tracing directory is already mounted and 0 if it is not.
+ * If @mount is set and it fails to mount, it returns -1.
+ *
+ * If path is not NULL, and the tracing directory is or was mounted, it holds
+ * the path to the tracing directory. It must not be freed.
+ */
+int tracefs_tracing_dir_is_mounted(bool mount, const char **path)
+{
+	const char *dir;
+
+	dir = find_tracing_dir(false, false);
+	if (dir) {
+		if (path)
+			*path = dir;
+		return 1;
+	}
+	if (!mount)
+		return 0;
+
+	dir = find_tracing_dir(false, mount);
+	if (!dir)
+		return -1;
+	if (path)
+		*path = dir;
+	return 0;
+}
+
+/**
+ * trace_find_tracing_dir - Find tracing directory
+ * @debugfs: Boolean to just return the debugfs directory
+ *
+ * Returns string containing the full path to the system's tracing directory.
+ * The string must be freed by free()
+ */
+__hidden char *trace_find_tracing_dir(bool debugfs)
+{
+	return find_tracing_dir(debugfs, false);
+}
+
 /**
  * tracefs_set_tracing_dir - Set location of the tracing directory
  * @tracing_dir: full path to the system's tracing directory mount point.