Add DupCloexec to atomically dup and set FD_CLOEXEC.

Bug: http://b/113558485
Test: treehugger
Change-Id: I1ad0fb32f1990d92c717d2208a66cfbc0b9ef361
diff --git a/libartbase/base/file_utils.cc b/libartbase/base/file_utils.cc
index a63f326..1d106b2 100644
--- a/libartbase/base/file_utils.cc
+++ b/libartbase/base/file_utils.cc
@@ -279,4 +279,12 @@
   return android::base::StartsWith(full_path, framework_path);
 }
 
+int DupCloexec(int fd) {
+#if defined(__linux__)
+  return fcntl(fd, F_DUPFD_CLOEXEC, 0);
+#else
+  return dup(fd);
+#endif
+}
+
 }  // namespace art
diff --git a/libartbase/base/file_utils.h b/libartbase/base/file_utils.h
index 063393b..c249bcc 100644
--- a/libartbase/base/file_utils.h
+++ b/libartbase/base/file_utils.h
@@ -78,6 +78,9 @@
 // Return whether the location is on system/framework (i.e. android_root/framework).
 bool LocationIsOnSystemFramework(const char* location);
 
+// dup(2), except setting the O_CLOEXEC flag atomically, when possible.
+int DupCloexec(int fd);
+
 }  // namespace art
 
 #endif  // ART_LIBARTBASE_BASE_FILE_UTILS_H_