update_engine: Stop watching stdout when child closes it.

When a child process closes its end of the redirected pipe, we stop
seeing EAGAIN or EWOULDBLOCK errors when calling read() and instead
read() will return 0. Separatedly, we watch for the SIGCHLD signal
and will eventually stop checking the childs output, but meanwhile
we will continuously run OnStdoutReady() with the CPU at 100%.

This patch fixes this condition by stop watching the file descriptor
once read() returns 0.

BUG=None
TEST=`strace update_engine_unittest --v=1` when running this subprocess:
sh -c "echo this is stdout; exec >/dev/null 2>/dev/null; sleep 10"
doesn't keep polling stdout_fd and the CPU at 100%.

Change-Id: Idd42d85f006213bfd42ad8da15e86d02caceac78
Reviewed-on: https://chromium-review.googlesource.com/285770
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/subprocess.cc b/subprocess.cc
index 6a34641..db66eb9 100644
--- a/subprocess.cc
+++ b/subprocess.cc
@@ -72,6 +72,11 @@
       if (errno != EWOULDBLOCK && errno != EAGAIN) {
         PLOG(ERROR) << "Error reading fd " << record->stdout_fd;
       }
+    } else if (rc == 0) {
+      // A value of 0 means that the child closed its end of the pipe and there
+      // is nothing else to read from stdout.
+      MessageLoop::current()->CancelTask(record->task_id);
+      record->task_id = MessageLoop::kTaskIdNull;
     } else {
       record->stdout.append(buf, rc);
     }