Use a system property to define extra command-line arguments.

Currently, we define 4 hardcoded init services to launch dumpstate with
different command-line options (since dumpstate must be launched by
root):

- bugreport
- bugreportplus
- bugreportwear
- bugreportremote

This approach does not scale well; a better option is to have just one
service, and let the framework pass the extra arguments through a system
property.

BUG: 31649719

Test: manual

Change-Id: I537775a7c8ea4ab04dbdaaf2bb2f513cc29f966b
diff --git a/cmds/dumpstate/dumpstate.cpp b/cmds/dumpstate/dumpstate.cpp
index 691863b..1eac43d 100644
--- a/cmds/dumpstate/dumpstate.cpp
+++ b/cmds/dumpstate/dumpstate.cpp
@@ -57,6 +57,9 @@
 static char cmdline_buf[16384] = "(unknown)";
 static const char *dump_traces_path = NULL;
 
+// Command-line arguments as string
+static std::string args;
+
 // TODO: variables below should be part of dumpstate object
 static unsigned long id;
 static char build_type[PROPERTY_VALUE_MAX];
@@ -69,6 +72,7 @@
  * although it could be changed by the user using a system property */
 static std::string suffix;
 static bool dry_run = false;
+static char extra_options[PROPERTY_VALUE_MAX];
 
 #define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
 #define ALT_PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops-0"
@@ -104,6 +108,8 @@
  */
 static std::string VERSION_DEFAULT = "1.0";
 
+static constexpr char PROPERTY_EXTRA_OPTIONS[] = "dumpstate.options";
+
 bool is_user_build() {
     return 0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1);
 }
@@ -702,7 +708,8 @@
     dumpFile(nullptr, "/proc/version");
     printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
     printf("Bugreport format version: %s\n", version.c_str());
-    printf("Dumpstate info: id=%lu pid=%d dry_run=%d\n", id, getpid(), dry_run);
+    printf("Dumpstate info: id=%lu pid=%d dry_run=%d args=%s extra_options=%s\n", id, getpid(),
+           dry_run, args.c_str(), extra_options);
     printf("\n");
 }
 
@@ -1290,12 +1297,16 @@
         MYLOGI("Running on dry-run mode (to disable it, call 'setprop dumpstate.dry_run false')\n");
     }
 
-    std::string args;
+    // TODO: use helper function to convert argv into a string
     for (int i = 0; i < argc; i++) {
         args += argv[i];
-        args += " ";
+        if (i < argc - 1) {
+            args += " ";
+        }
     }
-    MYLOGD("Dumpstate command line: %s\n", args.c_str());
+
+    property_get(PROPERTY_EXTRA_OPTIONS, extra_options, "");
+    MYLOGI("Dumpstate args: %s (extra options: %s)\n", args.c_str(), extra_options);
 
     /* gets the sequential id */
     char last_id[PROPERTY_VALUE_MAX];
@@ -1344,6 +1355,28 @@
         }
     }
 
+    if (strlen(extra_options) > 0) {
+        // Framework uses a system property to override some command-line args.
+        // Currently, it contains the type of the requested bugreport.
+        if (strcmp(extra_options, "bugreportplus") == 0) {
+            MYLOGD("Running as bugreportplus: add -P, remove -p\n");
+            do_update_progress = 1;
+            do_fb = 0;
+        } else if (strcmp(extra_options, "bugreportremote") == 0) {
+            MYLOGD("Running as bugreportremote: add -q -R, remove -p\n");
+            do_vibrate = 0;
+            is_remote_mode = 1;
+            do_fb = 0;
+        } else if (strcmp(extra_options, "bugreportwear") == 0) {
+            MYLOGD("Running as bugreportwear: add -P\n");
+            do_update_progress = 1;
+        } else {
+            MYLOGE("Unknown extra option: %s\n", extra_options);
+        }
+        // Reset the property
+        property_set(PROPERTY_EXTRA_OPTIONS, "");
+    }
+
     if ((do_zip_file || do_add_date || do_update_progress || do_broadcast) && !use_outfile) {
         usage();
         exit(1);
diff --git a/cmds/dumpstate/dumpstate.rc b/cmds/dumpstate/dumpstate.rc
index 3448e91..2e72574 100644
--- a/cmds/dumpstate/dumpstate.rc
+++ b/cmds/dumpstate/dumpstate.rc
@@ -17,32 +17,3 @@
     class main
     disabled
     oneshot
-
-# bugreportplus is an enhanced version of bugreport that provides a better
-# user interface (like displaying progress and allowing user to enter details).
-# It's typically triggered by the power button or developer settings.
-service bugreportplus /system/bin/dumpstate -d -B -P -z \
-        -o /data/user_de/0/com.android.shell/files/bugreports/bugreport
-    class main
-    disabled
-    oneshot
-
-# bugreportremote is an altered version of bugreport that is supposed to be
-# called not by human user of the device, but by DevicePolicyManagerService only when the
-# Device Owner explicitly requests it, and shared with the Device Policy Controller (DPC) app only
-# if the user consents
-# it will disable vibrations, screenshot taking and will not track progress or
-# allow user to enter any details
-service bugreportremote /system/bin/dumpstate -d -q -B -R -z \
-        -o /data/user_de/0/com.android.shell/files/bugreports/remote/bugreport
-    class main
-    disabled
-    oneshot
-
-# bugreportwear is a wearable version of bugreport that displays progress and takes early
-# screenshot.
-service bugreportwear /system/bin/dumpstate -d -B -P -p -z \
-        -o /data/user_de/0/com.android.shell/files/bugreports/bugreport
-    class main
-    disabled
-    oneshot