update_engine: clean up sleep process in SubprocessTest.CancelTest

SubprocessTest.CancelTest terminates a 'sh' process, which is put into
sleep by executing `sleep 60`. Upon receiving SIGTERM, the 'sleep'
process isn't reaped and thus becomes orphaned. As a remedy, this CL
modifies the shell command to trap SIGTERM and kill the 'sleep' process
upon receiving SIGTERM.

BUG=chromium:678643
TEST=Verified that no orphaned 'sleep' process is left after running
SubprocessTest.CancelTest.

Reviewed-on: https://chromium-review.googlesource.com/427059
Commit-Ready: Ben Chan <benchan@chromium.org>
Tested-by: Ben Chan <benchan@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>

(cherry picked from commit efb56850475b27ab9197c6010a3f3910b078cdc2)

Change-Id: I7a94b317b524c6de81e94a1f01c29aeb9b3de85c
diff --git a/common/subprocess_unittest.cc b/common/subprocess_unittest.cc
index 7dbdf98..cbc9a85 100644
--- a/common/subprocess_unittest.cc
+++ b/common/subprocess_unittest.cc
@@ -236,7 +236,18 @@
       kBinPath "/sh",
       "-c",
       base::StringPrintf(
-          "echo -n  X >\"%s\"; sleep 60; echo -n  Y >\"%s\"; exit 1",
+          // The 'sleep' launched below could be left behind as an orphaned
+          // process when the 'sh' process is terminated by SIGTERM. As a
+          // remedy, trap SIGTERM and kill the 'sleep' process, which requires
+          // launching 'sleep' in background and then waiting for it.
+          "cleanup() { kill \"${sleep_pid}\"; exit 0; }; "
+          "trap cleanup TERM; "
+          "sleep 60 & "
+          "sleep_pid=$!; "
+          "printf X >\"%s\"; "
+          "wait; "
+          "printf Y >\"%s\"; "
+          "exit 1",
           fifo_path.c_str(),
           fifo_path.c_str())};
   uint32_t tag = Subprocess::Get().Exec(cmd, base::Bind(&CallbackBad));