[tip:perf/urgent] perf tools: Open perf.data with O_CLOEXEC flag

From: tip-bot for Jiri Olsa
Date: Wed Sep 13 2017 - 03:46:44 EST


Commit-ID: cd6379ebb55ae53e77f17e22ce830bf3fe826736
Gitweb: http://git.kernel.org/tip/cd6379ebb55ae53e77f17e22ce830bf3fe826736
Author: Jiri Olsa <jolsa@xxxxxxxxxx>
AuthorDate: Fri, 8 Sep 2017 10:46:20 +0200
Committer: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
CommitDate: Tue, 12 Sep 2017 12:34:23 -0300

perf tools: Open perf.data with O_CLOEXEC flag

Do not carry the perf.data file descriptor into the workload process and
close it when perf executes the workload.

Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: David Ahern <dsahern@xxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
Link: http://lkml.kernel.org/r/20170908084621.31595-2-jolsa@xxxxxxxxxx
[ Add definitions for O_CLOEXEC for older systems ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/util/data.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index e84bbc8..263f5a9 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -10,6 +10,16 @@
#include "util.h"
#include "debug.h"

+#ifndef O_CLOEXEC
+#ifdef __sparc__
+#define O_CLOEXEC 0x400000
+#elif defined(__alpha__) || defined(__hppa__)
+#define O_CLOEXEC 010000000
+#else
+#define O_CLOEXEC 02000000
+#endif
+#endif
+
static bool check_pipe(struct perf_data_file *file)
{
struct stat st;
@@ -96,7 +106,8 @@ static int open_file_write(struct perf_data_file *file)
if (check_backup(file))
return -1;

- fd = open(file->path, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
+ fd = open(file->path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
+ S_IRUSR|S_IWUSR);

if (fd < 0)
pr_err("failed to open %s : %s\n", file->path,