OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/socket/unix_domain_listen_socket_posix.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <sys/socket.h> |
| 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> |
| 11 #include <sys/un.h> |
| 12 #include <unistd.h> |
| 13 |
| 14 #include <cstring> |
| 15 #include <string> |
| 16 |
| 17 #include "base/bind.h" |
| 18 #include "base/callback.h" |
| 19 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/threading/platform_thread.h" |
| 21 #include "build/build_config.h" |
| 22 #include "net/base/net_errors.h" |
| 23 #include "net/base/net_util.h" |
| 24 #include "net/socket/socket_descriptor.h" |
| 25 #include "net/socket/unix_domain_client_socket_posix.h" |
| 26 |
| 27 namespace net { |
| 28 |
| 29 namespace { |
| 30 |
| 31 int CreateAndBind(const std::string& socket_path, |
| 32 bool use_abstract_namespace, |
| 33 SocketDescriptor* socket_fd) { |
| 34 DCHECK(socket_fd); |
| 35 |
| 36 SockaddrStorage address; |
| 37 if (!UnixDomainClientSocket::FillAddress(socket_path, |
| 38 use_abstract_namespace, |
| 39 &address)) { |
| 40 return ERR_ADDRESS_INVALID; |
| 41 } |
| 42 |
| 43 SocketDescriptor fd = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0); |
| 44 if (fd == kInvalidSocket) |
| 45 return errno ? MapSystemError(errno) : ERR_UNEXPECTED; |
| 46 |
| 47 if (bind(fd, address.addr, address.addr_len) < 0) { |
| 48 int rv = MapSystemError(errno); |
| 49 close(fd); |
| 50 PLOG(ERROR) << "Could not bind unix domain socket to " << socket_path |
| 51 << (use_abstract_namespace ? " (with abstract namespace)" : ""); |
| 52 return rv; |
| 53 } |
| 54 |
| 55 *socket_fd = fd; |
| 56 return OK; |
| 57 } |
| 58 |
| 59 } // namespace |
| 60 |
| 61 // static |
| 62 scoped_ptr<UnixDomainListenSocket> |
| 63 UnixDomainListenSocket::CreateAndListenInternal( |
| 64 const std::string& path, |
| 65 const std::string& fallback_path, |
| 66 StreamListenSocket::Delegate* del, |
| 67 const AuthCallback& auth_callback, |
| 68 bool use_abstract_namespace) { |
| 69 SocketDescriptor socket_fd = kInvalidSocket; |
| 70 int rv = CreateAndBind(path, use_abstract_namespace, &socket_fd); |
| 71 if (rv != OK && !fallback_path.empty()) |
| 72 rv = CreateAndBind(fallback_path, use_abstract_namespace, &socket_fd); |
| 73 if (rv != OK) |
| 74 return scoped_ptr<UnixDomainListenSocket>(); |
| 75 scoped_ptr<UnixDomainListenSocket> sock( |
| 76 new UnixDomainListenSocket(socket_fd, del, auth_callback)); |
| 77 sock->Listen(); |
| 78 return sock.Pass(); |
| 79 } |
| 80 |
| 81 // static |
| 82 scoped_ptr<UnixDomainListenSocket> UnixDomainListenSocket::CreateAndListen( |
| 83 const std::string& path, |
| 84 StreamListenSocket::Delegate* del, |
| 85 const AuthCallback& auth_callback) { |
| 86 return CreateAndListenInternal(path, "", del, auth_callback, false); |
| 87 } |
| 88 |
| 89 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) |
| 90 // static |
| 91 scoped_ptr<UnixDomainListenSocket> |
| 92 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
| 93 const std::string& path, |
| 94 const std::string& fallback_path, |
| 95 StreamListenSocket::Delegate* del, |
| 96 const AuthCallback& auth_callback) { |
| 97 return |
| 98 CreateAndListenInternal(path, fallback_path, del, auth_callback, true); |
| 99 } |
| 100 #endif |
| 101 |
| 102 UnixDomainListenSocket::UnixDomainListenSocket( |
| 103 SocketDescriptor s, |
| 104 StreamListenSocket::Delegate* del, |
| 105 const AuthCallback& auth_callback) |
| 106 : StreamListenSocket(s, del), |
| 107 auth_callback_(auth_callback) {} |
| 108 |
| 109 UnixDomainListenSocket::~UnixDomainListenSocket() {} |
| 110 |
| 111 void UnixDomainListenSocket::Accept() { |
| 112 SocketDescriptor conn = StreamListenSocket::AcceptSocket(); |
| 113 if (conn == kInvalidSocket) |
| 114 return; |
| 115 uid_t user_id; |
| 116 gid_t group_id; |
| 117 if (!UnixDomainServerSocket::GetPeerIds(conn, &user_id, &group_id) || |
| 118 !auth_callback_.Run(user_id, group_id)) { |
| 119 if (IGNORE_EINTR(close(conn)) < 0) |
| 120 LOG(ERROR) << "close() error"; |
| 121 return; |
| 122 } |
| 123 scoped_ptr<UnixDomainListenSocket> sock( |
| 124 new UnixDomainListenSocket(conn, socket_delegate_, auth_callback_)); |
| 125 // It's up to the delegate to AddRef if it wants to keep it around. |
| 126 sock->WatchSocket(WAITING_READ); |
| 127 socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>()); |
| 128 } |
| 129 |
| 130 UnixDomainListenSocketFactory::UnixDomainListenSocketFactory( |
| 131 const std::string& path, |
| 132 const UnixDomainListenSocket::AuthCallback& auth_callback) |
| 133 : path_(path), |
| 134 auth_callback_(auth_callback) {} |
| 135 |
| 136 UnixDomainListenSocketFactory::~UnixDomainListenSocketFactory() {} |
| 137 |
| 138 scoped_ptr<StreamListenSocket> UnixDomainListenSocketFactory::CreateAndListen( |
| 139 StreamListenSocket::Delegate* delegate) const { |
| 140 return UnixDomainListenSocket::CreateAndListen( |
| 141 path_, delegate, auth_callback_).PassAs<StreamListenSocket>(); |
| 142 } |
| 143 |
| 144 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) |
| 145 |
| 146 UnixDomainListenSocketWithAbstractNamespaceFactory:: |
| 147 UnixDomainListenSocketWithAbstractNamespaceFactory( |
| 148 const std::string& path, |
| 149 const std::string& fallback_path, |
| 150 const UnixDomainListenSocket::AuthCallback& auth_callback) |
| 151 : UnixDomainListenSocketFactory(path, auth_callback), |
| 152 fallback_path_(fallback_path) {} |
| 153 |
| 154 UnixDomainListenSocketWithAbstractNamespaceFactory:: |
| 155 ~UnixDomainListenSocketWithAbstractNamespaceFactory() {} |
| 156 |
| 157 scoped_ptr<StreamListenSocket> |
| 158 UnixDomainListenSocketWithAbstractNamespaceFactory::CreateAndListen( |
| 159 StreamListenSocket::Delegate* delegate) const { |
| 160 return UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
| 161 path_, fallback_path_, delegate, auth_callback_) |
| 162 .PassAs<StreamListenSocket>(); |
| 163 } |
| 164 |
| 165 #endif |
| 166 |
| 167 } // namespace net |
OLD | NEW |