simpleperf: let simpleperf running record/stat command die if parent exits.
Bug: 28114205
Change-Id: I310b9d7a4f52ad5bacf892eba34dbf531a3a7245
diff --git a/simpleperf/cmd_record.cpp b/simpleperf/cmd_record.cpp
index ff465f9..6957767 100644
--- a/simpleperf/cmd_record.cpp
+++ b/simpleperf/cmd_record.cpp
@@ -17,6 +17,7 @@
#include <libgen.h>
#include <poll.h>
#include <signal.h>
+#include <sys/prctl.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <set>
@@ -161,6 +162,8 @@
record_filename_("perf.data"),
sample_record_count_(0),
lost_record_count_(0) {
+ // Die if parent exits.
+ prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
signaled = false;
scoped_signal_handler_.reset(
new ScopedSignalHandler({SIGCHLD, SIGINT, SIGTERM}, signal_handler));
diff --git a/simpleperf/cmd_stat.cpp b/simpleperf/cmd_stat.cpp
index 59626af..f5792f8 100644
--- a/simpleperf/cmd_stat.cpp
+++ b/simpleperf/cmd_stat.cpp
@@ -18,6 +18,7 @@
#include <signal.h>
#include <stdio.h>
#include <string.h>
+#include <sys/prctl.h>
#include <algorithm>
#include <chrono>
@@ -290,6 +291,8 @@
system_wide_collection_(false),
child_inherit_(true),
csv_(false) {
+ // Die if parent exits.
+ prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
signaled = false;
scoped_signal_handler_.reset(
new ScopedSignalHandler({SIGCHLD, SIGINT, SIGTERM}, signal_handler));
diff --git a/simpleperf/workload.cpp b/simpleperf/workload.cpp
index b67fa8f..1d34c11 100644
--- a/simpleperf/workload.cpp
+++ b/simpleperf/workload.cpp
@@ -18,6 +18,7 @@
#include <errno.h>
#include <fcntl.h>
+#include <sys/prctl.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -91,6 +92,8 @@
}
static void ChildProcessFn(std::vector<std::string>& args, int start_signal_fd, int exec_child_fd) {
+ // Die if parent exits.
+ prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
std::vector<char*> argv(args.size() + 1);
for (size_t i = 0; i < args.size(); ++i) {
argv[i] = &args[i][0];