Fixed and improve RunCommand() when running as root.

Improved it by not try to run 'su root' on user builds.
Fixed the logic that creates the args.

BUG: 29319732
BUG: 26379932
Test: manual

Change-Id: I6b39abc8ee907f638905913425218c0a50d767c6
diff --git a/cmds/dumpstate/utils.cpp b/cmds/dumpstate/utils.cpp
index e036dfa..476392e 100644
--- a/cmds/dumpstate/utils.cpp
+++ b/cmds/dumpstate/utils.cpp
@@ -772,28 +772,26 @@
         MYLOGE("No arguments on command '%s'\n", title.c_str());
         return -1;
     }
-    DurationReporter durationReporter(title);
 
     int size = fullCommand.size() + 1;  // null terminated
+    int startingIndex = 0;
     if (options.RootMode() == SU_ROOT) {
-        size += 2;  // "su" "root"
+        startingIndex = 2;  // "su" "root"
+        size += startingIndex;
     }
 
-    const char* args[size];
-
-    if (!title.empty()) {
-        printf("------ %s (", title.c_str());
-    }
+    std::vector<const char*> args;
+    args.resize(size);
 
     std::string commandString;
-    int i = 0;
     if (options.RootMode() == SU_ROOT) {
         args[0] = SU_PATH;
         commandString += SU_PATH;
         args[1] = "root";
         commandString += " root ";
     }
-    for (auto arg = fullCommand.begin(); arg < fullCommand.end(); arg++) {
+    int i = startingIndex;
+    for (auto arg = fullCommand.begin(); arg != fullCommand.end(); ++arg) {
         args[i++] = arg->c_str();
         commandString += arg->c_str();
         if (arg != fullCommand.end() - 1) {
@@ -804,11 +802,17 @@
     const char* path = args[0];
     const char* command = commandString.c_str();
 
+    if (options.RootMode() == SU_ROOT && ds.IsUserBuild()) {
+        printf("Skipping '%s' on user build.\n", command);
+        return 0;
+    }
+
     if (!title.empty()) {
-        printf("%s) ------\n", command);
+        printf("------ %s (%s) ------\n", title.c_str(), command);
     }
 
     fflush(stdout);
+    DurationReporter durationReporter(title);
 
     const std::string& loggingMessage = options.LoggingMessage();
     if (!loggingMessage.empty()) {
@@ -861,7 +865,7 @@
         sigact.sa_handler = SIG_IGN;
         sigaction(SIGPIPE, &sigact, NULL);
 
-        execvp(path, (char**)args);
+        execvp(path, (char**)args.data());
         // execvp's result will be handled after waitpid_with_timeout() below, but
         // if it failed, it's safer to exit dumpstate.
         MYLOGD("execvp on command '%s' failed (error: %s)\n", command, strerror(errno));