Adds SharedFD implementations of creat() and dup2()

dup2's adaptation is name UNMANAGED_Dup2 because it has the same
potential issues as dup() for SharedFD.

Bug: 111517046
Test: builds
Change-Id: I42ef0c02810ba7a18323dd349f0972cedc05e36a
diff --git a/common/libs/fs/shared_fd.cpp b/common/libs/fs/shared_fd.cpp
index b4a401c..93d2027 100644
--- a/common/libs/fs/shared_fd.cpp
+++ b/common/libs/fs/shared_fd.cpp
@@ -274,6 +274,10 @@
   }
 }
 
+SharedFD SharedFD::Creat(const char* path, mode_t mode) {
+  return SharedFD::Open(path, O_CREAT|O_WRONLY|O_TRUNC, mode);
+}
+
 SharedFD SharedFD::Socket(int domain, int socket_type, int protocol) {
   int fd = TEMP_FAILURE_RETRY(socket(domain, socket_type, protocol));
   if (fd == -1) {
diff --git a/common/libs/fs/shared_fd.h b/common/libs/fs/shared_fd.h
index f5dd451..32a4a99 100644
--- a/common/libs/fs/shared_fd.h
+++ b/common/libs/fs/shared_fd.h
@@ -139,6 +139,7 @@
   static SharedFD GetControlSocket(const char* name);
   // Returns false on failure, true on success.
   static SharedFD Open(const char* pathname, int flags, mode_t mode = 0);
+  static SharedFD Creat(const char* pathname, mode_t mode);
   static bool Pipe(SharedFD* fd0, SharedFD* fd1);
   static SharedFD Event(int initval = 0, int flags = 0);
   static SharedFD Epoll(int flags = 0);
@@ -232,6 +233,13 @@
     return rval;
   }
 
+  int UNMANAGED_Dup2(int newfd) {
+    errno = 0;
+    int rval = TEMP_FAILURE_RETRY(dup2(fd_, newfd));
+    errno_ = errno;
+    return rval;
+  }
+
   int EpollCtl(int op, cvd::SharedFD new_fd, struct epoll_event* event) {
     errno = 0;
     int rval = TEMP_FAILURE_RETRY(epoll_ctl(fd_, op, new_fd->fd_, event));