From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH 3/6] trace-cmd: New libtracefs API tracefs_write_file()
Date: Thu, 19 Dec 2019 13:34:59 +0200 [thread overview]
Message-ID: <20191219113502.28964-4-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20191219113502.28964-1-tz.stoyanov@gmail.com>
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(-)
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);
--
2.23.0
next prev parent reply other threads:[~2019-12-19 11:35 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-19 11:34 [PATCH 0/6] tracefs libraray Tzvetomir Stoyanov (VMware)
2019-12-19 11:34 ` [PATCH 1/6] trace-cmd: Introduce libtracefs library Tzvetomir Stoyanov (VMware)
2019-12-20 3:25 ` Steven Rostedt
2019-12-19 11:34 ` [PATCH 2/6] kernel-shark: Use new tracefs library Tzvetomir Stoyanov (VMware)
2019-12-20 3:37 ` Steven Rostedt
2019-12-20 3:55 ` Steven Rostedt
2019-12-20 9:27 ` Yordan Karadzhov (VMware)
2019-12-19 11:34 ` Tzvetomir Stoyanov (VMware) [this message]
2019-12-20 4:21 ` [PATCH 3/6] trace-cmd: New libtracefs API tracefs_write_file() Steven Rostedt
2020-01-06 12:10 ` Tzvetomir Stoyanov
2019-12-19 11:35 ` [PATCH 4/6] trace-cmd: New libtracefs APIs for ftrace instances Tzvetomir Stoyanov (VMware)
2019-12-19 11:35 ` [PATCH 5/6] trace-cmd,kernel-shark: New libtracefs APIs for ftrace events and systems Tzvetomir Stoyanov (VMware)
2019-12-19 11:35 ` [PATCH 6/6] trace-cmd,kernel-shark: New libtracefs APIs for loading ftrace events Tzvetomir Stoyanov (VMware)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191219113502.28964-4-tz.stoyanov@gmail.com \
--to=tz.stoyanov@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).