Revert "Add SharedFD::DupAndClose() and use on all FDs passed by..."

Revert submission 1401569

Reason for revert: forrest test for b/165567209
Reverted Changes:
I8a236166b:Deduplicate input event transmission code
I9b657038b:Add SharedFD::DupAndClose() and use on all FDs pas...

Change-Id: I9a6f338e2406ac36561362019b83b45f2a97e7f8
diff --git a/common/libs/fs/shared_fd.cpp b/common/libs/fs/shared_fd.cpp
index 5693792..db633ed 100644
--- a/common/libs/fs/shared_fd.cpp
+++ b/common/libs/fs/shared_fd.cpp
@@ -217,14 +217,6 @@
   return SharedFD(std::shared_ptr<FileInstance>(new FileInstance(fd, error_num)));
 }
 
-SharedFD SharedFD::DupAndClose(int unmanaged_fd) {
-  auto ret = SharedFD::Dup(unmanaged_fd);
-  if (ret->IsOpen()) {
-    close(unmanaged_fd);
-  }
-  return ret;
-}
-
 bool SharedFD::Pipe(SharedFD* fd0, SharedFD* fd1) {
   int fds[2];
   int rval = pipe(fds);
diff --git a/common/libs/fs/shared_fd.h b/common/libs/fs/shared_fd.h
index 15e9d5b..c63daf3 100644
--- a/common/libs/fs/shared_fd.h
+++ b/common/libs/fs/shared_fd.h
@@ -117,9 +117,6 @@
                          socklen_t* addrlen);
   static SharedFD Accept(const FileInstance& listener);
   static SharedFD Dup(int unmanaged_fd);
-  // Same as Dup, but closes unmanaged_fd before returning (only if the dup()
-  // call succeeded)
-  static SharedFD DupAndClose(int unmanaged_fd);
   // All SharedFDs have the O_CLOEXEC flag after creation. To remove use the
   // Fcntl or Dup functions.
   static SharedFD Open(const std::string& pathname, int flags, mode_t mode = 0);
diff --git a/host/commands/config_server/main.cpp b/host/commands/config_server/main.cpp
index 8f42525..4c68efc 100644
--- a/host/commands/config_server/main.cpp
+++ b/host/commands/config_server/main.cpp
@@ -34,8 +34,7 @@
 
   CHECK(device_config) << "Could not open device config";
 
-  cuttlefish::SharedFD server_fd =
-      cuttlefish::SharedFD::DupAndClose(FLAGS_server_fd);
+  cuttlefish::SharedFD server_fd = cuttlefish::SharedFD::Dup(FLAGS_server_fd);
 
   CHECK(server_fd->IsOpen()) << "Inheriting logcat server: "
                              << server_fd->StrError();
diff --git a/host/commands/console_forwarder/main.cpp b/host/commands/console_forwarder/main.cpp
index c3adc43..d12d66b 100644
--- a/host/commands/console_forwarder/main.cpp
+++ b/host/commands/console_forwarder/main.cpp
@@ -234,14 +234,17 @@
     return -1;
   }
 
-  auto console_in = cuttlefish::SharedFD::DupAndClose(FLAGS_console_in_fd);
+  auto console_in = cuttlefish::SharedFD::Dup(FLAGS_console_in_fd);
+  close(FLAGS_console_in_fd);
   if (!console_in->IsOpen()) {
     LOG(ERROR) << "Error dupping fd " << FLAGS_console_in_fd << ": "
                << console_in->StrError();
     return -2;
   }
+  close(FLAGS_console_in_fd);
 
-  auto console_out = cuttlefish::SharedFD::DupAndClose(FLAGS_console_out_fd);
+  auto console_out = cuttlefish::SharedFD::Dup(FLAGS_console_out_fd);
+  close(FLAGS_console_out_fd);
   if (!console_out->IsOpen()) {
     LOG(ERROR) << "Error dupping fd " << FLAGS_console_out_fd << ": "
                << console_out->StrError();
diff --git a/host/commands/gnss_grpc_proxy/gnss_grpc_proxy.cpp b/host/commands/gnss_grpc_proxy/gnss_grpc_proxy.cpp
index 69171d0..9dfa3a2 100644
--- a/host/commands/gnss_grpc_proxy/gnss_grpc_proxy.cpp
+++ b/host/commands/gnss_grpc_proxy/gnss_grpc_proxy.cpp
@@ -112,14 +112,17 @@
 
 void RunServer() {
 
-  auto gnss_in = cuttlefish::SharedFD::DupAndClose(FLAGS_gnss_in_fd);
+  auto gnss_in = cuttlefish::SharedFD::Dup(FLAGS_gnss_in_fd);
+  close(FLAGS_gnss_in_fd);
   if (!gnss_in->IsOpen()) {
     LOG(ERROR) << "Error dupping fd " << FLAGS_gnss_in_fd << ": "
                << gnss_in->StrError();
     return;
   }
+  close(FLAGS_gnss_in_fd);
 
-  auto gnss_out = cuttlefish::SharedFD::DupAndClose(FLAGS_gnss_out_fd);
+  auto gnss_out = cuttlefish::SharedFD::Dup(FLAGS_gnss_out_fd);
+  close(FLAGS_gnss_out_fd);
   if (!gnss_out->IsOpen()) {
     LOG(ERROR) << "Error dupping fd " << FLAGS_gnss_out_fd << ": "
                << gnss_out->StrError();
diff --git a/host/commands/kernel_log_monitor/main.cc b/host/commands/kernel_log_monitor/main.cc
index 6009ca3..9c65b1b 100644
--- a/host/commands/kernel_log_monitor/main.cc
+++ b/host/commands/kernel_log_monitor/main.cc
@@ -83,7 +83,8 @@
     auto log_name = instance.kernel_log_pipe_name();
     pipe = cuttlefish::SharedFD::Open(log_name.c_str(), O_RDONLY);
   } else {
-    pipe = cuttlefish::SharedFD::DupAndClose(FLAGS_log_pipe_fd);
+    pipe = cuttlefish::SharedFD::Dup(FLAGS_log_pipe_fd);
+    close(FLAGS_log_pipe_fd);
   }
 
   if (!pipe->IsOpen()) {
diff --git a/host/commands/log_tee/log_tee.cpp b/host/commands/log_tee/log_tee.cpp
index 8666251..1cf502e 100644
--- a/host/commands/log_tee/log_tee.cpp
+++ b/host/commands/log_tee/log_tee.cpp
@@ -44,8 +44,9 @@
         cuttlefish::LogToStderrAndFiles({instance.launcher_log_path()}));
   }
 
-  auto log_fd = cuttlefish::SharedFD::DupAndClose(FLAGS_log_fd_in);
+  auto log_fd = cuttlefish::SharedFD::Dup(FLAGS_log_fd_in);
   CHECK(log_fd->IsOpen()) << "Failed to dup log_fd_in: " <<  log_fd->StrError();
+  close(FLAGS_log_fd_in);
 
   if (FLAGS_process_name.size() > 0) {
     android::base::SetDefaultTag(FLAGS_process_name);
diff --git a/host/commands/logcat_receiver/main.cpp b/host/commands/logcat_receiver/main.cpp
index 1e4a398..b1c4d93 100644
--- a/host/commands/logcat_receiver/main.cpp
+++ b/host/commands/logcat_receiver/main.cpp
@@ -50,7 +50,8 @@
     auto log_name = instance.logcat_pipe_name();
     pipe = cuttlefish::SharedFD::Open(log_name.c_str(), O_RDONLY);
   } else {
-    pipe = cuttlefish::SharedFD::DupAndClose(FLAGS_log_pipe_fd);
+    pipe = cuttlefish::SharedFD::Dup(FLAGS_log_pipe_fd);
+    close(FLAGS_log_pipe_fd);
   }
 
   if (!pipe->IsOpen()) {
diff --git a/host/commands/secure_env/secure_env.cpp b/host/commands/secure_env/secure_env.cpp
index 0a891d7..2ba3e54 100644
--- a/host/commands/secure_env/secure_env.cpp
+++ b/host/commands/secure_env/secure_env.cpp
@@ -98,15 +98,17 @@
 
   CHECK(FLAGS_keymaster_fd != -1)
       << "TODO(schuffelen): Add keymaster_fd alternative";
-  auto keymaster_server = cuttlefish::SharedFD::DupAndClose(FLAGS_keymaster_fd);
+  auto keymaster_server = cuttlefish::SharedFD::Dup(FLAGS_keymaster_fd);
   CHECK(keymaster_server->IsOpen()) << "Could not dup server fd: "
                                     << keymaster_server->StrError();
+  close(FLAGS_keymaster_fd);
 
   CHECK(FLAGS_gatekeeper_fd != -1)
       << "TODO(schuffelen): Add gatekeeper_fd alternative";
-  auto gatekeeper_server = cuttlefish::SharedFD::DupAndClose(FLAGS_gatekeeper_fd);
+  auto gatekeeper_server = cuttlefish::SharedFD::Dup(FLAGS_gatekeeper_fd);
   CHECK(gatekeeper_server->IsOpen()) << "Could not dup server fd: "
                                      << gatekeeper_server->StrError();
+  close(FLAGS_gatekeeper_fd);
 
 
   std::thread keymaster_thread([&keymaster_server, &keymaster]() {
diff --git a/host/commands/tombstone_receiver/main.cpp b/host/commands/tombstone_receiver/main.cpp
index 953b7c4..2e0ea69 100644
--- a/host/commands/tombstone_receiver/main.cpp
+++ b/host/commands/tombstone_receiver/main.cpp
@@ -59,7 +59,8 @@
   cuttlefish::DefaultSubprocessLogging(argv);
   google::ParseCommandLineFlags(&argc, &argv, true);
 
-  cuttlefish::SharedFD server_fd = cuttlefish::SharedFD::DupAndClose(FLAGS_server_fd);
+  cuttlefish::SharedFD server_fd = cuttlefish::SharedFD::Dup(FLAGS_server_fd);
+  close(FLAGS_server_fd);
 
   CHECK(server_fd->IsOpen()) << "Error inheriting tombstone server: "
                              << server_fd->StrError();
diff --git a/host/commands/vtpm_passthrough/vtpm_passthrough.cpp b/host/commands/vtpm_passthrough/vtpm_passthrough.cpp
index ef12107..2084987 100644
--- a/host/commands/vtpm_passthrough/vtpm_passthrough.cpp
+++ b/host/commands/vtpm_passthrough/vtpm_passthrough.cpp
@@ -74,7 +74,8 @@
   CHECK(!FLAGS_device.empty()) << "A device must be set.";
   CHECK(FLAGS_server_fd > -1) << "A server fd must be given.";
 
-  auto server = cuttlefish::SharedFD::DupAndClose(FLAGS_server_fd);
+  auto server = cuttlefish::SharedFD::Dup(FLAGS_server_fd);
+  close(FLAGS_server_fd);
   CHECK(server->IsOpen()) << "Could not dup vsock server fd: " << server->StrError();
 
   auto device = cuttlefish::SharedFD::Open(FLAGS_device.c_str(), O_RDWR);
diff --git a/host/frontend/vnc_server/virtual_inputs.cpp b/host/frontend/vnc_server/virtual_inputs.cpp
index bcfc9dc..39a38ae 100644
--- a/host/frontend/vnc_server/virtual_inputs.cpp
+++ b/host/frontend/vnc_server/virtual_inputs.cpp
@@ -257,11 +257,13 @@
 VirtualInputs::VirtualInputs() { AddKeyMappings(&keymapping_); }
 
 VirtualInputs* VirtualInputs::Get() {
-  auto touch_fd = cuttlefish::SharedFD::DupAndClose(FLAGS_touch_fd);
+  auto touch_fd = cuttlefish::SharedFD::Dup(FLAGS_touch_fd);
   CHECK(touch_fd->IsOpen()) << "Failed to dup touch fd: " << FLAGS_touch_fd;
-  auto keyboard_fd = cuttlefish::SharedFD::DupAndClose(FLAGS_keyboard_fd);
+  close(FLAGS_touch_fd);
+  auto keyboard_fd = cuttlefish::SharedFD::Dup(FLAGS_keyboard_fd);
   CHECK(keyboard_fd->IsOpen())
       << "Failed to dup keyboard fd: " << FLAGS_keyboard_fd;
+  close(FLAGS_keyboard_fd);
 
   return new SocketVirtualInputs(
       cuttlefish::TouchConnector::Create(touch_fd, FLAGS_write_virtio_input),
diff --git a/host/frontend/webrtc/main.cpp b/host/frontend/webrtc/main.cpp
index a544f53..01c7112 100644
--- a/host/frontend/webrtc/main.cpp
+++ b/host/frontend/webrtc/main.cpp
@@ -61,17 +61,20 @@
   cuttlefish::DefaultSubprocessLogging(argv);
   ::gflags::ParseCommandLineFlags(&argc, &argv, true);
 
-  auto touch_fd = cuttlefish::SharedFD::DupAndClose(FLAGS_touch_fd);
+  auto touch_fd = cuttlefish::SharedFD::Dup(FLAGS_touch_fd);
   CHECK(touch_fd->IsOpen()) << "Failed to dup touch fd: " << FLAGS_touch_fd;
-  auto keyboard_fd = cuttlefish::SharedFD::DupAndClose(FLAGS_keyboard_fd);
+  close(FLAGS_touch_fd);
+  auto keyboard_fd = cuttlefish::SharedFD::Dup(FLAGS_keyboard_fd);
   CHECK(keyboard_fd->IsOpen())
       << "Failed to dup keyboard fd: " << FLAGS_keyboard_fd;
+  close(FLAGS_keyboard_fd);
 
   // Accepting on these sockets here means the device won't register with the
   // operator as soon as it could, but rather wait until crosvm's input devices
   // have been initialized. That's OK though, because without those devices
   // there is no meaningful interaction the user can have with the device.
-  auto keyboard_connector = cuttlefish::KeyboardConnector::Create(keyboard_fd);
+  auto keyboard_connector =
+      cuttlefish::KeyboardConnector::Create(keyboard_fd);
   CHECK(keyboard_connector) << "Failed to instantiate keyboard connector";
   auto touch_connector = cuttlefish::TouchConnector::Create(touch_fd);
   CHECK(touch_connector) << "Failed to instantiate touch connector";