summaryrefslogtreecommitdiffstats
path: root/ipc/unix_domain_socket_util.cc
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-17 19:02:35 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-17 19:02:35 +0000
commit42f558fdef03f1ec2261f554151d8a4d168919e4 (patch)
treea2f2403061c945c8983524036bc73550098de6b7 /ipc/unix_domain_socket_util.cc
parentcb8d22b096bf5698bcf58725d6e4d7534e235a0a (diff)
downloadchromium_src-42f558fdef03f1ec2261f554151d8a4d168919e4.zip
chromium_src-42f558fdef03f1ec2261f554151d8a4d168919e4.tar.gz
chromium_src-42f558fdef03f1ec2261f554151d8a4d168919e4.tar.bz2
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
Diffstat (limited to 'ipc/unix_domain_socket_util.cc')
-rw-r--r--ipc/unix_domain_socket_util.cc43
1 files changed, 21 insertions, 22 deletions
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 @@ int MakeUnixAddrForPath(const std::string& socket_name,
}
// 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 @@ int MakeUnixAddrForPath(const std::string& socket_name,
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 @@ bool CreateServerUnixDomainSocket(const base::FilePath& socket_path,
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 @@ bool CreateServerUnixDomainSocket(const base::FilePath& socket_path,
}
// 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 @@ bool CreateClientUnixDomainSocket(const base::FilePath& socket_path,
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 @@ bool ServerAcceptConnection(int server_listen_fd, int* server_socket) {
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;
}