liblogcat: avoid double close.

Bug: http://b/67960705
Test: mma
Change-Id: I0b9015f0b4e64631a3476966decce3b6210bb801
diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp
index 3d56472..07981fc 100644
--- a/logcat/logcat.cpp
+++ b/logcat/logcat.cpp
@@ -1831,11 +1831,10 @@
     }
     android::close_output(context);
     android::close_error(context);
+
     if (context->fds[1] >= 0) {
-        // NB: could be closed by the above fclose(s), ignore error.
-        int save_errno = errno;
+        // NB: this should be closed by close_output, but just in case...
         close(context->fds[1]);
-        errno = save_errno;
         context->fds[1] = -1;
     }
 
diff --git a/logcat/logcat_system.cpp b/logcat/logcat_system.cpp
index ea393bd..6dfd110 100644
--- a/logcat/logcat_system.cpp
+++ b/logcat/logcat_system.cpp
@@ -15,6 +15,7 @@
  */
 
 #include <ctype.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -98,8 +99,12 @@
         return NULL;
     }
 
-    FILE* retval = fdopen(fd, "reb");
-    if (!retval) android_logcat_destroy(ctx);
+    int duped = dup(fd);
+    FILE* retval = fdopen(duped, "reb");
+    if (!retval) {
+        close(duped);
+        android_logcat_destroy(ctx);
+    }
     return retval;
 }