Blob Blame History Raw
From edf0ae950c5b9d3c5eed29a40f5669cf657995e6 Mon Sep 17 00:00:00 2001
From: Andrea Claudi <aclaudi@redhat.com>
Date: Tue, 30 Apr 2019 15:43:03 +0200
Subject: [PATCH] bpf: minor cleanups for bpf_trace_pipe

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1465646
Upstream Status: iproute2.git commit 1b736dc469dca

commit 1b736dc469dcabd4180848a1f1b3d1fef2b84dbc
Author: Daniel Borkmann <daniel@iogearbox.net>
Date:   Tue Sep 5 02:24:31 2017 +0200

    bpf: minor cleanups for bpf_trace_pipe

    Just minor nits, e.g. no need to fflush() and instead of returning
    right away, just break and close the fd.

    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
---
 lib/bpf.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/lib/bpf.c b/lib/bpf.c
index e072cba214067..f0e6c6fb732ee 100644
--- a/lib/bpf.c
+++ b/lib/bpf.c
@@ -461,9 +461,9 @@ int bpf_trace_pipe(void)
 		"/trace",
 		0,
 	};
+	int fd_in, fd_out = STDERR_FILENO;
 	char tpipe[PATH_MAX];
 	const char *mnt;
-	int fd;
 
 	mnt = bpf_find_mntpt("tracefs", TRACEFS_MAGIC, tracefs_mnt,
 			     sizeof(tracefs_mnt), tracefs_known_mnts);
@@ -474,8 +474,8 @@ int bpf_trace_pipe(void)
 
 	snprintf(tpipe, sizeof(tpipe), "%s/trace_pipe", mnt);
 
-	fd = open(tpipe, O_RDONLY);
-	if (fd < 0)
+	fd_in = open(tpipe, O_RDONLY);
+	if (fd_in < 0)
 		return -1;
 
 	fprintf(stderr, "Running! Hang up with ^C!\n\n");
@@ -483,15 +483,14 @@ int bpf_trace_pipe(void)
 		static char buff[4096];
 		ssize_t ret;
 
-		ret = read(fd, buff, sizeof(buff) - 1);
-		if (ret > 0) {
-			if (write(STDERR_FILENO, buff, ret) != ret)
-				return -1;
-			fflush(stderr);
-		}
+		ret = read(fd_in, buff, sizeof(buff));
+		if (ret > 0 && write(fd_out, buff, ret) == ret)
+			continue;
+		break;
 	}
 
-	return 0;
+	close(fd_in);
+	return -1;
 }
 
 static int bpf_gen_global(const char *bpf_sub_dir)
-- 
2.20.1