Cancel dumpstate if client disconnects

Bug: 268200878
Test: manual
Change-Id: I834c0af0852c0e80ebdad8f2f90b698434e4d2ed
(cherry picked from commit 3d509846cfa9430575f9eb4dfb46957b04788638)
diff --git a/cmds/bugreportz/main.cpp b/cmds/bugreportz/main.cpp
index cd2652c..790556c 100644
--- a/cmds/bugreportz/main.cpp
+++ b/cmds/bugreportz/main.cpp
@@ -75,6 +75,23 @@
         return EXIT_FAILURE;
     }
 
+    // Wait a little while for dumpstatez to stop if it is running
+    bool dumpstate_running = false;
+    for (int i = 0; i < 20; i++) {
+        char buf[PROPERTY_VALUE_MAX];
+        property_get("init.svc.dumpstatez", buf, "");
+        dumpstate_running = strcmp(buf, "running") == 0;
+
+        if (!dumpstate_running) break;
+
+        sleep(1);
+    }
+
+    if (dumpstate_running) {
+        fprintf(stderr, "FAIL:dumpstatez service is already running\n");
+        return EXIT_FAILURE;
+    }
+
     // TODO: code below was copy-and-pasted from bugreport.cpp (except by the
     // timeout value);
     // should be reused instead.
diff --git a/cmds/dumpstate/dumpstate.cpp b/cmds/dumpstate/dumpstate.cpp
index baf8e42..2a7089e 100644
--- a/cmds/dumpstate/dumpstate.cpp
+++ b/cmds/dumpstate/dumpstate.cpp
@@ -2593,7 +2593,11 @@
 }
 
 static void register_sig_handler() {
-    signal(SIGPIPE, SIG_IGN);
+    signal(SIGPIPE, [](int) {
+        MYLOGE("Connection with client lost, canceling.");
+        ds.Cancel();
+        abort();
+    });
 }
 
 bool Dumpstate::FinishZipFile() {