| Index: net/socket/unix_domain_socket_posix.cc
|
| diff --git a/net/socket/unix_domain_socket_posix.cc b/net/socket/unix_domain_socket_posix.cc
|
| index 3141f7166b22abb033b8278065ac0b4313e21c70..c4b3efc7db749e41c5960abec22b3c182d964159 100644
|
| --- a/net/socket/unix_domain_socket_posix.cc
|
| +++ b/net/socket/unix_domain_socket_posix.cc
|
| @@ -4,9 +4,6 @@
|
|
|
| #include "net/socket/unix_domain_socket_posix.h"
|
|
|
| -#include <cstring>
|
| -#include <string>
|
| -
|
| #include <errno.h>
|
| #include <sys/socket.h>
|
| #include <sys/stat.h>
|
| @@ -14,6 +11,9 @@
|
| #include <sys/un.h>
|
| #include <unistd.h>
|
|
|
| +#include <cstring>
|
| +#include <string>
|
| +
|
| #include "base/bind.h"
|
| #include "base/callback.h"
|
| #include "base/posix/eintr_wrapper.h"
|
| @@ -22,57 +22,64 @@
|
| #include "net/base/net_errors.h"
|
| #include "net/base/net_util.h"
|
| #include "net/socket/socket_descriptor.h"
|
| +#include "net/socket/unix_domain_client_socket_posix.h"
|
|
|
| namespace net {
|
|
|
| namespace {
|
|
|
| -bool NoAuthenticationCallback(uid_t, gid_t) {
|
| - return true;
|
| -}
|
| +int CreateAndBind(const std::string& socket_path,
|
| + bool use_abstract_namespace,
|
| + SocketDescriptor* socket_fd) {
|
| + DCHECK(socket_fd);
|
|
|
| -bool GetPeerIds(int socket, uid_t* user_id, gid_t* group_id) {
|
| -#if defined(OS_LINUX) || defined(OS_ANDROID)
|
| - struct ucred user_cred;
|
| - socklen_t len = sizeof(user_cred);
|
| - if (getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &user_cred, &len) == -1)
|
| - return false;
|
| - *user_id = user_cred.uid;
|
| - *group_id = user_cred.gid;
|
| -#else
|
| - if (getpeereid(socket, user_id, group_id) == -1)
|
| - return false;
|
| -#endif
|
| - return true;
|
| -}
|
| + SockaddrStorage address;
|
| + if (!UnixDomainClientSocket::FillAddress(socket_path,
|
| + use_abstract_namespace,
|
| + &address)) {
|
| + return ERR_ADDRESS_INVALID;
|
| + }
|
|
|
| -} // namespace
|
| + SocketDescriptor fd = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0);
|
| + if (fd == kInvalidSocket)
|
| + return errno ? MapSystemError(errno) : ERR_UNEXPECTED;
|
|
|
| -// static
|
| -UnixDomainSocket::AuthCallback UnixDomainSocket::NoAuthentication() {
|
| - return base::Bind(NoAuthenticationCallback);
|
| + if (bind(fd, address.addr, address.addr_len) < 0) {
|
| + int rv = MapSystemError(errno);
|
| + close(fd);
|
| + PLOG(ERROR) << "Could not bind unix domain socket to " << socket_path
|
| + << (use_abstract_namespace ? " (with abstract namespace)" : "");
|
| + return rv;
|
| + }
|
| +
|
| + *socket_fd = fd;
|
| + return OK;
|
| }
|
|
|
| +} // namespace
|
| +
|
| // static
|
| -scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListenInternal(
|
| +scoped_ptr<UnixDomainListenSocket>
|
| +UnixDomainListenSocket::CreateAndListenInternal(
|
| const std::string& path,
|
| const std::string& fallback_path,
|
| StreamListenSocket::Delegate* del,
|
| const AuthCallback& auth_callback,
|
| bool use_abstract_namespace) {
|
| - SocketDescriptor s = CreateAndBind(path, use_abstract_namespace);
|
| - if (s == kInvalidSocket && !fallback_path.empty())
|
| - s = CreateAndBind(fallback_path, use_abstract_namespace);
|
| - if (s == kInvalidSocket)
|
| - return scoped_ptr<UnixDomainSocket>();
|
| - scoped_ptr<UnixDomainSocket> sock(
|
| - new UnixDomainSocket(s, del, auth_callback));
|
| + SocketDescriptor socket_fd = kInvalidSocket;
|
| + int rv = CreateAndBind(path, use_abstract_namespace, &socket_fd);
|
| + if (rv != OK && !fallback_path.empty())
|
| + rv = CreateAndBind(fallback_path, use_abstract_namespace, &socket_fd);
|
| + if (rv != OK)
|
| + return scoped_ptr<UnixDomainListenSocket>();
|
| + scoped_ptr<UnixDomainListenSocket> sock(
|
| + new UnixDomainListenSocket(socket_fd, del, auth_callback));
|
| sock->Listen();
|
| return sock.Pass();
|
| }
|
|
|
| // static
|
| -scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListen(
|
| +scoped_ptr<UnixDomainListenSocket> UnixDomainListenSocket::CreateAndListen(
|
| const std::string& path,
|
| StreamListenSocket::Delegate* del,
|
| const AuthCallback& auth_callback) {
|
| @@ -81,8 +88,8 @@ scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListen(
|
|
|
| #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED)
|
| // static
|
| -scoped_ptr<UnixDomainSocket>
|
| -UnixDomainSocket::CreateAndListenWithAbstractNamespace(
|
| +scoped_ptr<UnixDomainListenSocket>
|
| +UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
|
| const std::string& path,
|
| const std::string& fallback_path,
|
| StreamListenSocket::Delegate* del,
|
| @@ -92,101 +99,65 @@ UnixDomainSocket::CreateAndListenWithAbstractNamespace(
|
| }
|
| #endif
|
|
|
| -UnixDomainSocket::UnixDomainSocket(
|
| +UnixDomainListenSocket::UnixDomainListenSocket(
|
| SocketDescriptor s,
|
| StreamListenSocket::Delegate* del,
|
| const AuthCallback& auth_callback)
|
| : StreamListenSocket(s, del),
|
| auth_callback_(auth_callback) {}
|
|
|
| -UnixDomainSocket::~UnixDomainSocket() {}
|
| -
|
| -// static
|
| -SocketDescriptor UnixDomainSocket::CreateAndBind(const std::string& path,
|
| - bool use_abstract_namespace) {
|
| - sockaddr_un addr;
|
| - static const size_t kPathMax = sizeof(addr.sun_path);
|
| - if (use_abstract_namespace + path.size() + 1 /* '\0' */ > kPathMax)
|
| - return kInvalidSocket;
|
| - const SocketDescriptor s = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0);
|
| - if (s == kInvalidSocket)
|
| - return kInvalidSocket;
|
| - memset(&addr, 0, sizeof(addr));
|
| - addr.sun_family = AF_UNIX;
|
| - socklen_t addr_len;
|
| - if (use_abstract_namespace) {
|
| - // Convert the path given into abstract socket name. It must start with
|
| - // the '\0' character, so we are adding it. |addr_len| must specify the
|
| - // length of the structure exactly, as potentially the socket name may
|
| - // have '\0' characters embedded (although we don't support this).
|
| - // Note that addr.sun_path is already zero initialized.
|
| - memcpy(addr.sun_path + 1, path.c_str(), path.size());
|
| - addr_len = path.size() + offsetof(struct sockaddr_un, sun_path) + 1;
|
| - } else {
|
| - memcpy(addr.sun_path, path.c_str(), path.size());
|
| - addr_len = sizeof(sockaddr_un);
|
| - }
|
| - if (bind(s, reinterpret_cast<sockaddr*>(&addr), addr_len)) {
|
| - LOG(ERROR) << "Could not bind unix domain socket to " << path;
|
| - if (use_abstract_namespace)
|
| - LOG(ERROR) << " (with abstract namespace enabled)";
|
| - if (IGNORE_EINTR(close(s)) < 0)
|
| - LOG(ERROR) << "close() error";
|
| - return kInvalidSocket;
|
| - }
|
| - return s;
|
| -}
|
| +UnixDomainListenSocket::~UnixDomainListenSocket() {}
|
|
|
| -void UnixDomainSocket::Accept() {
|
| +void UnixDomainListenSocket::Accept() {
|
| SocketDescriptor conn = StreamListenSocket::AcceptSocket();
|
| if (conn == kInvalidSocket)
|
| return;
|
| uid_t user_id;
|
| gid_t group_id;
|
| - if (!GetPeerIds(conn, &user_id, &group_id) ||
|
| + if (!UnixDomainServerSocket::GetPeerIds(conn, &user_id, &group_id) ||
|
| !auth_callback_.Run(user_id, group_id)) {
|
| if (IGNORE_EINTR(close(conn)) < 0)
|
| LOG(ERROR) << "close() error";
|
| return;
|
| }
|
| - scoped_ptr<UnixDomainSocket> sock(
|
| - new UnixDomainSocket(conn, socket_delegate_, auth_callback_));
|
| + scoped_ptr<UnixDomainListenSocket> sock(
|
| + new UnixDomainListenSocket(conn, socket_delegate_, auth_callback_));
|
| // It's up to the delegate to AddRef if it wants to keep it around.
|
| sock->WatchSocket(WAITING_READ);
|
| socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>());
|
| }
|
|
|
| -UnixDomainSocketFactory::UnixDomainSocketFactory(
|
| +UnixDomainListenSocketFactory::UnixDomainListenSocketFactory(
|
| const std::string& path,
|
| - const UnixDomainSocket::AuthCallback& auth_callback)
|
| + const UnixDomainListenSocket::AuthCallback& auth_callback)
|
| : path_(path),
|
| auth_callback_(auth_callback) {}
|
|
|
| -UnixDomainSocketFactory::~UnixDomainSocketFactory() {}
|
| +UnixDomainListenSocketFactory::~UnixDomainListenSocketFactory() {}
|
|
|
| -scoped_ptr<StreamListenSocket> UnixDomainSocketFactory::CreateAndListen(
|
| +scoped_ptr<StreamListenSocket> UnixDomainListenSocketFactory::CreateAndListen(
|
| StreamListenSocket::Delegate* delegate) const {
|
| - return UnixDomainSocket::CreateAndListen(
|
| + return UnixDomainListenSocket::CreateAndListen(
|
| path_, delegate, auth_callback_).PassAs<StreamListenSocket>();
|
| }
|
|
|
| #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED)
|
|
|
| -UnixDomainSocketWithAbstractNamespaceFactory::
|
| -UnixDomainSocketWithAbstractNamespaceFactory(
|
| +UnixDomainListenSocketWithAbstractNamespaceFactory::
|
| +UnixDomainListenSocketWithAbstractNamespaceFactory(
|
| const std::string& path,
|
| const std::string& fallback_path,
|
| - const UnixDomainSocket::AuthCallback& auth_callback)
|
| - : UnixDomainSocketFactory(path, auth_callback),
|
| + const UnixDomainListenSocket::AuthCallback& auth_callback)
|
| + : UnixDomainListenSocketFactory(path, auth_callback),
|
| fallback_path_(fallback_path) {}
|
|
|
| -UnixDomainSocketWithAbstractNamespaceFactory::
|
| -~UnixDomainSocketWithAbstractNamespaceFactory() {}
|
| +UnixDomainListenSocketWithAbstractNamespaceFactory::
|
| +~UnixDomainListenSocketWithAbstractNamespaceFactory() {}
|
|
|
| scoped_ptr<StreamListenSocket>
|
| -UnixDomainSocketWithAbstractNamespaceFactory::CreateAndListen(
|
| +UnixDomainListenSocketWithAbstractNamespaceFactory::CreateAndListen(
|
| StreamListenSocket::Delegate* delegate) const {
|
| - return UnixDomainSocket::CreateAndListenWithAbstractNamespace(
|
| + return UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
|
| path_, fallback_path_, delegate, auth_callback_)
|
| .PassAs<StreamListenSocket>();
|
| }
|
|
|