linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Linux Trace Devel <linux-trace-devel@vger.kernel.org>
Cc: Johannes Berg <johannes@sipsolutions.net>
Subject: [PATCH] trace-cmd record: Verify that splice works before using it
Date: Wed, 8 Jun 2022 13:05:27 -0400	[thread overview]
Message-ID: <20220608130527.26e50cba@gandalf.local.home> (raw)

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Add a test to make sure that splice works on the source directory before
using it, and if not automatically switch over to read/write method.

Suggested-by: Johannes Berg <johannes@sipsolutions.net>
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=213659
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
Johannes, can you test this?
I want to make sure that it fixes the issue.

 lib/trace-cmd/trace-recorder.c | 38 ++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/lib/trace-cmd/trace-recorder.c b/lib/trace-cmd/trace-recorder.c
index c83337899098..c7ef13c851d2 100644
--- a/lib/trace-cmd/trace-recorder.c
+++ b/lib/trace-cmd/trace-recorder.c
@@ -220,6 +220,41 @@ tracecmd_create_buffer_recorder_fd2(int fd, int fd2,
int cpu, unsigned flags, return NULL;
 }
 
+static void verify_splice(const char *file, unsigned *flags)
+{
+	int brass[2];
+	int ret;
+	int fd;
+
+	fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
+	if (fd < 0)
+		return; /* Will fail by the caller too */
+
+	if (pipe(brass) < 0)
+		goto fail_pipe;
+
+	ret = splice(brass[0], NULL, fd, NULL, 0, SPLICE_F_NONBLOCK);
+	if (ret < 0)
+		goto fail_splice;
+
+ out_pipe:
+	close(brass[0]);
+	close(brass[1]);
+ out:
+	close(fd);
+	return;
+
+ fail_pipe:
+	tracecmd_warning("Failed opening pipe, trying read/write");
+	*flags |= TRACECMD_RECORD_NOSPLICE;
+	goto out;
+
+ fail_splice:
+	tracecmd_warning("Failed splice to file, trying read/write");
+	*flags |= TRACECMD_RECORD_NOSPLICE;
+	goto out_pipe;
+}
+
 struct tracecmd_recorder *
 tracecmd_create_buffer_recorder_fd(int fd, int cpu, unsigned flags, const
char *buffer) {
@@ -233,6 +268,9 @@ __tracecmd_create_buffer_recorder(const char *file, int
cpu, unsigned flags, struct tracecmd_recorder *recorder;
 	int fd;
 
+	if (!(flags & TRACECMD_RECORD_NOSPLICE))
+		verify_splice(file, &flags);
+
 	fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
 	if (fd < 0)
 		return NULL;
-- 
2.35.1


             reply	other threads:[~2022-06-08 17:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-08 17:05 Steven Rostedt [this message]
2023-01-19 21:03 ` [PATCH] trace-cmd record: Verify that splice works before using it Johannes Berg
2023-01-19 21:53   ` Steven Rostedt

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=20220608130527.26e50cba@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=johannes@sipsolutions.net \
    --cc=linux-trace-devel@vger.kernel.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).