Implement ScopedFD in terms of ScopedGeneric.

Move to a new file base/files/scoped_file.h. I will also add ScopedFILE to here (currently in file_util.h) later.

I think there is a crash in the old code in content/browser/zygote_host/zygote_host_impl_linux.cc that this patch should fix. The old ScopedFD took the address of something in a vector that is being modified.

I removed SafeScopedFD from content/common/sandbox_linux/sandbox_linux.cc since base's ScopedFD not CHECKs on close failure (this is a more recent addition).

Reland of https://codereview.chromium.org/191673003/
R=agl, viettrungluu

Review URL: https://codereview.chromium.org/202113004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257473 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 42f558fdef03f1ec2261f554151d8a4d168919e4
diff --git a/ipc/unix_domain_socket_util.cc b/ipc/unix_domain_socket_util.cc
index 10df9b2..c29ce92 100644
--- a/ipc/unix_domain_socket_util.cc
+++ b/ipc/unix_domain_socket_util.cc
@@ -13,6 +13,7 @@
 
 #include "base/file_util.h"
 #include "base/files/file_path.h"
+#include "base/files/scoped_file.h"
 #include "base/logging.h"
 #include "base/posix/eintr_wrapper.h"
 
@@ -45,15 +46,14 @@
   }
 
   // Create socket.
-  int fd = socket(AF_UNIX, SOCK_STREAM, 0);
-  if (fd < 0) {
+  base::ScopedFD fd(socket(AF_UNIX, SOCK_STREAM, 0));
+  if (!fd.is_valid()) {
     PLOG(ERROR) << "socket";
     return -1;
   }
-  file_util::ScopedFD scoped_fd(&fd);
 
   // Make socket non-blocking
-  if (HANDLE_EINTR(fcntl(fd, F_SETFL, O_NONBLOCK)) < 0) {
+  if (HANDLE_EINTR(fcntl(fd.get(), F_SETFL, O_NONBLOCK)) < 0) {
     PLOG(ERROR) << "fcntl(O_NONBLOCK)";
     return -1;
   }
@@ -64,7 +64,7 @@
   strncpy(unix_addr->sun_path, socket_name.c_str(), kMaxSocketNameLength);
   *unix_addr_len =
       offsetof(struct sockaddr_un, sun_path) + socket_name.length();
-  return *scoped_fd.release();
+  return fd.release();
 }
 
 }  // namespace
@@ -78,10 +78,10 @@
 
   struct sockaddr_un unix_addr;
   size_t unix_addr_len;
-  int fd = MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len);
-  if (fd < 0)
+  base::ScopedFD fd(
+      MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len));
+  if (!fd.is_valid())
     return false;
-  file_util::ScopedFD scoped_fd(&fd);
 
   // Make sure the path we need exists.
   if (!base::CreateDirectory(socket_dir)) {
@@ -96,20 +96,20 @@
   }
 
   // Bind the socket.
-  if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr),
+  if (bind(fd.get(), reinterpret_cast<const sockaddr*>(&unix_addr),
            unix_addr_len) < 0) {
     PLOG(ERROR) << "bind " << socket_path.value();
     return false;
   }
 
   // Start listening on the socket.
-  if (listen(fd, SOMAXCONN) < 0) {
+  if (listen(fd.get(), SOMAXCONN) < 0) {
     PLOG(ERROR) << "listen " << socket_path.value();
     unlink(socket_name.c_str());
     return false;
   }
 
-  *server_listen_fd = *scoped_fd.release();
+  *server_listen_fd = fd.release();
   return true;
 }
 
@@ -122,18 +122,18 @@
 
   struct sockaddr_un unix_addr;
   size_t unix_addr_len;
-  int fd = MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len);
-  if (fd < 0)
+  base::ScopedFD fd(
+      MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len));
+  if (!fd.is_valid())
     return false;
-  file_util::ScopedFD scoped_fd(&fd);
 
-  if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&unix_addr),
+  if (HANDLE_EINTR(connect(fd.get(), reinterpret_cast<sockaddr*>(&unix_addr),
                            unix_addr_len)) < 0) {
     PLOG(ERROR) << "connect " << socket_path.value();
     return false;
   }
 
-  *client_socket = *scoped_fd.release();
+  *client_socket = fd.release();
   return true;
 }
 
@@ -184,18 +184,17 @@
   DCHECK(server_socket);
   *server_socket = -1;
 
-  int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0));
-  if (accept_fd < 0)
+  base::ScopedFD accept_fd(HANDLE_EINTR(accept(server_listen_fd, NULL, 0)));
+  if (!accept_fd.is_valid())
     return IsRecoverableError(errno);
-  file_util::ScopedFD scoped_fd(&accept_fd);
-  if (HANDLE_EINTR(fcntl(accept_fd, F_SETFL, O_NONBLOCK)) < 0) {
-    PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd;
+  if (HANDLE_EINTR(fcntl(accept_fd.get(), F_SETFL, O_NONBLOCK)) < 0) {
+    PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd.get();
     // It's safe to keep listening on |server_listen_fd| even if the attempt to
     // set O_NONBLOCK failed on the client fd.
     return true;
   }
 
-  *server_socket = *scoped_fd.release();
+  *server_socket = accept_fd.release();
   return true;
 }