diff mbox series

[3/6] trace-cmd: New libtracefs API tracefs_write_file()

Message ID 20191219113502.28964-4-tz.stoyanov@gmail.com (mailing list archive)
State Superseded
Headers show
Series tracefs libraray | expand

Commit Message

Tzvetomir Stoyanov (VMware) Dec. 19, 2019, 11:34 a.m. UTC
Moved write_file() static function from trace-record.c to
libtracefs API. The new API will be useful in future libtracefs
extension.
All die() calls in its implementation are replaced with warning().
A check is added to all current callers of tracefs_write_file(),
in case of a error die() is called, to keep the existing behavior.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 include/tracefs/tracefs.h   |  2 ++
 lib/tracefs/tracefs-utils.c | 41 ++++++++++++++++++++++++++++++++
 tracecmd/trace-record.c     | 47 ++++++++++++-------------------------
 3 files changed, 58 insertions(+), 32 deletions(-)

Comments

Steven Rostedt Dec. 20, 2019, 4:21 a.m. UTC | #1
On Thu, 19 Dec 2019 13:34:59 +0200
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:

> Moved write_file() static function from trace-record.c to
> libtracefs API. The new API will be useful in future libtracefs
> extension.
> All die() calls in its implementation are replaced with warning().
> A check is added to all current callers of tracefs_write_file(),
> in case of a error die() is called, to keep the existing behavior.

I'm curious to why this is being made as a library function, as its
really just a helper function. Not sure it needs to be part of a
library. It doesn't seem to be anything specific to tracefs. Or am I
missing something?

-- Steve


> 
> Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
> ---
>  include/tracefs/tracefs.h   |  2 ++
>  lib/tracefs/tracefs-utils.c | 41 ++++++++++++++++++++++++++++++++
>  tracecmd/trace-record.c     | 47 ++++++++++++-------------------------
>  3 files changed, 58 insertions(+), 32 deletions(-)
>
Tzvetomir Stoyanov (VMware) Jan. 6, 2020, 12:10 p.m. UTC | #2
On Fri, Dec 20, 2019 at 6:21 AM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Thu, 19 Dec 2019 13:34:59 +0200
> "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:
>
> > Moved write_file() static function from trace-record.c to
> > libtracefs API. The new API will be useful in future libtracefs
> > extension.
> > All die() calls in its implementation are replaced with warning().
> > A check is added to all current callers of tracefs_write_file(),
> > in case of a error die() is called, to keep the existing behavior.
>
> I'm curious to why this is being made as a library function, as its
> really just a helper function. Not sure it needs to be part of a
> library. It doesn't seem to be anything specific to tracefs. Or am I
> missing something?
>

My intention was to share the code between trace-cmd and the library,
but may be it is not a good idea to do it in such way. I'll remove
tracefs_write_file()
API and will add it as a local function.

> -- Steve
>
>
> >
> > Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
> > ---
> >  include/tracefs/tracefs.h   |  2 ++
> >  lib/tracefs/tracefs-utils.c | 41 ++++++++++++++++++++++++++++++++
> >  tracecmd/trace-record.c     | 47 ++++++++++++-------------------------
> >  3 files changed, 58 insertions(+), 32 deletions(-)
> >
diff mbox series

Patch

diff --git a/include/tracefs/tracefs.h b/include/tracefs/tracefs.h
index e844c75..6120dbe 100644
--- a/include/tracefs/tracefs.h
+++ b/include/tracefs/tracefs.h
@@ -17,4 +17,6 @@  const char *tracefs_get_tracing_dir(void);
 /* tracefs_find_tracing_dir must be freed */
 char *tracefs_find_tracing_dir(void);
 
+int tracefs_write_file(const char *file, const char *str, const char *type);
+
 #endif /* _TRACE_FS_H */
diff --git a/lib/tracefs/tracefs-utils.c b/lib/tracefs/tracefs-utils.c
index c695b8b..42a3f46 100644
--- a/lib/tracefs/tracefs-utils.c
+++ b/lib/tracefs/tracefs-utils.c
@@ -7,6 +7,8 @@ 
 #include <sys/mount.h>
 #include <sys/stat.h>
 #include <linux/limits.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "tracefs.h"
 
@@ -178,3 +180,42 @@  void tracefs_put_tracing_file(char *name)
 {
 	free(name);
 }
+
+/*
+ * tracefs_write_file - Write in trace file
+ * @file: Full name of the trace file.
+ * @str: A null-terminated string, that will be written in the file.
+ * @type: A null-terminated string, describing the current write operation.
+ *        Used for logging purposes. If not NULL, in case of an error the
+ *        content of the file is dumped to stderror.
+ *
+ * Returns the number of written bytes, or -1 in case of an error
+ */
+int tracefs_write_file(const char *file, const char *str, const char *type)
+{
+	char buf[BUFSIZ];
+	int ret;
+	int fd;
+
+	fd = open(file, O_WRONLY | O_TRUNC);
+	if (fd < 0) {
+		warning("Failed to open '%s'", file);
+		return -1;
+	}
+	ret = write(fd, str, strlen(str));
+	close(fd);
+	if (ret < 0 && type) {
+		/* write failed */
+		fd = open(file, O_RDONLY);
+		if (fd < 0) {
+			warning("Failed to write in '%s'", file);
+			return -1;
+		}
+
+		while ((ret = read(fd, buf, BUFSIZ)) > 0)
+			fprintf(stderr, "%.*s", ret, buf);
+		warning("Failed %s of %s\n", type, file);
+		close(fd);
+	}
+	return ret;
+}
diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index 5355813..be21f3d 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -832,31 +832,6 @@  get_instance_dir(struct buffer_instance *instance)
 	return path;
 }
 
-static int write_file(const char *file, const char *str, const char *type)
-{
-	char buf[BUFSIZ];
-	int fd;
-	int ret;
-
-	fd = open(file, O_WRONLY | O_TRUNC);
-	if (fd < 0)
-		die("opening to '%s'", file);
-	ret = write(fd, str, strlen(str));
-	close(fd);
-	if (ret < 0 && type) {
-		/* write failed */
-		fd = open(file, O_RDONLY);
-		if (fd < 0)
-			die("writing to '%s'", file);
-		/* the filter has the error */
-		while ((ret = read(fd, buf, BUFSIZ)) > 0)
-			fprintf(stderr, "%.*s", ret, buf);
-		die("Failed %s of %s\n", type, file);
-		close(fd);
-	}
-	return ret;
-}
-
 static int
 write_instance_file(struct buffer_instance *instance,
 		    const char *file, const char *str, const char *type)
@@ -867,8 +842,11 @@  write_instance_file(struct buffer_instance *instance,
 
 	path = get_instance_file(instance, file);
 	ret = stat(path, &st);
-	if (ret == 0)
-		ret = write_file(path, str, type);
+	if (ret == 0) {
+		ret = tracefs_write_file(path, str, type);
+		if (ret < 0)
+			die("Failed to write file");
+	}
 	tracefs_put_tracing_file(path);
 
 	return ret;
@@ -2010,7 +1988,8 @@  static int find_trigger(const char *file, char *buf, int size, int fields)
 
 static void write_filter(const char *file, const char *filter)
 {
-	write_file(file, filter, "filter");
+	if (tracefs_write_file(file, filter, "filter") < 0)
+		die("Failed to write file");
 }
 
 static void clear_filter(const char *file)
@@ -2020,12 +1999,14 @@  static void clear_filter(const char *file)
 
 static void write_trigger(const char *file, const char *trigger)
 {
-	write_file(file, trigger, "trigger");
+	if (tracefs_write_file(file, trigger, "trigger") < 0)
+		die("Failed to write file");
 }
 
 static void write_func_filter(const char *file, const char *trigger)
 {
-	write_file(file, trigger, "function filter");
+	if (tracefs_write_file(file, trigger, "function filter") < 0)
+		die("Failed to write file");
 }
 
 static void clear_trigger(const char *file)
@@ -2117,8 +2098,10 @@  static void update_reset_files(void)
 		reset = reset_files;
 		reset_files = reset->next;
 
-		if (!keep)
-			write_file(reset->path, reset->reset, "reset");
+		if (!keep) {
+			if (tracefs_write_file(reset->path, reset->reset, "reset") < 0)
+				die("Failed to write file");
+		}
 		free(reset->path);
 		free(reset->reset);
 		free(reset);