diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-17 19:02:35 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-17 19:02:35 +0000 |
commit | 42f558fdef03f1ec2261f554151d8a4d168919e4 (patch) | |
tree | a2f2403061c945c8983524036bc73550098de6b7 /ipc | |
parent | cb8d22b096bf5698bcf58725d6e4d7534e235a0a (diff) | |
download | chromium_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')
-rw-r--r-- | ipc/ipc_channel_factory.cc | 10 | ||||
-rw-r--r-- | ipc/unix_domain_socket_util.cc | 43 |
2 files changed, 26 insertions, 27 deletions
diff --git a/ipc/ipc_channel_factory.cc b/ipc/ipc_channel_factory.cc index 5c24284..244024c 100644 --- a/ipc/ipc_channel_factory.cc +++ b/ipc/ipc_channel_factory.cc @@ -5,6 +5,7 @@ #include "ipc/ipc_channel_factory.h" #include "base/file_util.h" +#include "base/files/scoped_file.h" #include "base/logging.h" #include "ipc/unix_domain_socket_util.h" @@ -51,21 +52,20 @@ void ChannelFactory::OnFileCanReadWithoutBlocking(int fd) { delegate_->OnListenError(); return; } + base::ScopedFD scoped_fd(new_fd); - if (new_fd < 0) { + if (!scoped_fd.is_valid()) { // The accept() failed, but not in such a way that the factory needs to be // shut down. return; } - file_util::ScopedFD scoped_fd(&new_fd); - // Verify that the IPC channel peer is running as the same user. - if (!IsPeerAuthorized(new_fd)) + if (!IsPeerAuthorized(scoped_fd.get())) return; ChannelHandle handle(std::string(), - base::FileDescriptor(*scoped_fd.release(), true)); + base::FileDescriptor(scoped_fd.release(), true)); delegate_->OnClientConnected(handle); } 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; } |