dumpstate: save background traces when a BR is captured.

Adds a call to perfetto --save-for-bugreport to bugreport.
If a background trace labeled with bugreport_score > 0 is
being recorded when dumpstate is invoked, the tracing daemon
will stop the trace and serialize it into a fixed location
(/data/misc/perfetto-traces/bugreport/systrace.pftrace).
If no background tracing is happening, the command is a no-op.
The trace saving happens in two stages:
1. In the very early stage of dumpstate, the trace is
   snapshoted (if in progress). This prevents that further
   dumpstate activity pushes out interesting contents from
   the trace buffer.
2. In the later stages of dumpstate, the trace is attached
   to the bugreport.zip file and cleaned up.

Bug: 170334305
Change-Id: I4278459697a5ed20b20ee99032884874a82a66be
diff --git a/cmds/dumpstate/DumpstateUtil.h b/cmds/dumpstate/DumpstateUtil.h
index b099443..b00c46e 100644
--- a/cmds/dumpstate/DumpstateUtil.h
+++ b/cmds/dumpstate/DumpstateUtil.h
@@ -80,6 +80,7 @@
 
         int64_t timeout_ms_;
         bool always_;
+        bool close_all_fds_on_exec_;
         PrivilegeMode account_mode_;
         OutputMode output_mode_;
         std::string logging_message_;
@@ -112,6 +113,13 @@
         CommandOptionsBuilder& DropRoot();
         /* Sets the command's OutputMode as `REDIRECT_TO_STDERR` */
         CommandOptionsBuilder& RedirectStderr();
+        /* Closes all file descriptors before exec-ing the target process. This
+         * includes also stdio pipes, which are dup-ed on /dev/null. It prevents
+         * leaking opened FDs to the target process, which in turn can hit
+         * selinux denials in presence of auto_trans rules.
+         */
+        CommandOptionsBuilder& CloseAllFileDescriptorsOnExec();
+
         /* When not empty, logs a message before executing the command.
          * Must contain a `%s`, which will be replaced by the full command line, and end on `\n`. */
         CommandOptionsBuilder& Log(const std::string& message);
@@ -130,6 +138,8 @@
     int64_t TimeoutInMs() const;
     /* Checks whether the command should always be run, even on dry-run mode. */
     bool Always() const;
+    /* Checks whether all FDs should be closed prior to the exec() calls. */
+    bool ShouldCloseAllFileDescriptorsOnExec() const;
     /** Gets the PrivilegeMode of the command. */
     PrivilegeMode PrivilegeMode() const;
     /** Gets the OutputMode of the command. */