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 <cstring> |
| 8 #include <string> |
| 9 |
| 10 #include <errno.h> |
| 11 #include <sys/socket.h> |
| 12 #include <sys/stat.h> |
| 13 #include <sys/types.h> |
| 14 #include <sys/un.h> |
| 15 #include <unistd.h> |
| 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_server_socket_posix.h" |
| 26 |
| 27 namespace net { |
| 28 |
| 29 // static |
| 30 scoped_ptr<UnixDomainListenSocket> |
| 31 UnixDomainListenSocket::CreateAndListenInternal( |
| 32 const std::string& path, |
| 33 const std::string& fallback_path, |
| 34 StreamListenSocket::Delegate* del, |
| 35 const AuthCallback& auth_callback, |
| 36 bool use_abstract_namespace) { |
| 37 SocketDescriptor socket_fd = kInvalidSocket; |
| 38 int rv = UnixDomainServerSocket::CreateAndBind(path, |
| 39 use_abstract_namespace, |
| 40 &socket_fd); |
| 41 if (rv != OK && !fallback_path.empty()) |
| 42 rv = UnixDomainServerSocket::CreateAndBind(fallback_path, |
| 43 use_abstract_namespace, |
| 44 &socket_fd); |
| 45 if (rv != OK) |
| 46 return scoped_ptr<UnixDomainListenSocket>(); |
| 47 scoped_ptr<UnixDomainListenSocket> sock( |
| 48 new UnixDomainListenSocket(socket_fd, del, auth_callback)); |
| 49 sock->Listen(); |
| 50 return sock.Pass(); |
| 51 } |
| 52 |
| 53 // static |
| 54 scoped_ptr<UnixDomainListenSocket> UnixDomainListenSocket::CreateAndListen( |
| 55 const std::string& path, |
| 56 StreamListenSocket::Delegate* del, |
| 57 const AuthCallback& auth_callback) { |
| 58 return CreateAndListenInternal(path, "", del, auth_callback, false); |
| 59 } |
| 60 |
| 61 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) |
| 62 // static |
| 63 scoped_ptr<UnixDomainListenSocket> |
| 64 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
| 65 const std::string& path, |
| 66 const std::string& fallback_path, |
| 67 StreamListenSocket::Delegate* del, |
| 68 const AuthCallback& auth_callback) { |
| 69 return |
| 70 CreateAndListenInternal(path, fallback_path, del, auth_callback, true); |
| 71 } |
| 72 #endif |
| 73 |
| 74 UnixDomainListenSocket::UnixDomainListenSocket( |
| 75 SocketDescriptor s, |
| 76 StreamListenSocket::Delegate* del, |
| 77 const AuthCallback& auth_callback) |
| 78 : StreamListenSocket(s, del), |
| 79 auth_callback_(auth_callback) {} |
| 80 |
| 81 UnixDomainListenSocket::~UnixDomainListenSocket() {} |
| 82 |
| 83 void UnixDomainListenSocket::Accept() { |
| 84 SocketDescriptor conn = StreamListenSocket::AcceptSocket(); |
| 85 if (conn == kInvalidSocket) |
| 86 return; |
| 87 uid_t user_id; |
| 88 gid_t group_id; |
| 89 if (!UnixDomainServerSocket::GetPeerIds(conn, &user_id, &group_id) || |
| 90 !auth_callback_.Run(user_id, group_id)) { |
| 91 if (IGNORE_EINTR(close(conn)) < 0) |
| 92 LOG(ERROR) << "close() error"; |
| 93 return; |
| 94 } |
| 95 scoped_ptr<UnixDomainListenSocket> sock( |
| 96 new UnixDomainListenSocket(conn, socket_delegate_, auth_callback_)); |
| 97 // It's up to the delegate to AddRef if it wants to keep it around. |
| 98 sock->WatchSocket(WAITING_READ); |
| 99 socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>()); |
| 100 } |
| 101 |
| 102 UnixDomainListenSocketFactory::UnixDomainListenSocketFactory( |
| 103 const std::string& path, |
| 104 const UnixDomainListenSocket::AuthCallback& auth_callback) |
| 105 : path_(path), |
| 106 auth_callback_(auth_callback) {} |
| 107 |
| 108 UnixDomainListenSocketFactory::~UnixDomainListenSocketFactory() {} |
| 109 |
| 110 scoped_ptr<StreamListenSocket> UnixDomainListenSocketFactory::CreateAndListen( |
| 111 StreamListenSocket::Delegate* delegate) const { |
| 112 return UnixDomainListenSocket::CreateAndListen( |
| 113 path_, delegate, auth_callback_).PassAs<StreamListenSocket>(); |
| 114 } |
| 115 |
| 116 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) |
| 117 |
| 118 UnixDomainListenSocketWithAbstractNamespaceFactory:: |
| 119 UnixDomainListenSocketWithAbstractNamespaceFactory( |
| 120 const std::string& path, |
| 121 const std::string& fallback_path, |
| 122 const UnixDomainListenSocket::AuthCallback& auth_callback) |
| 123 : UnixDomainListenSocketFactory(path, auth_callback), |
| 124 fallback_path_(fallback_path) {} |
| 125 |
| 126 UnixDomainListenSocketWithAbstractNamespaceFactory:: |
| 127 ~UnixDomainListenSocketWithAbstractNamespaceFactory() {} |
| 128 |
| 129 scoped_ptr<StreamListenSocket> |
| 130 UnixDomainListenSocketWithAbstractNamespaceFactory::CreateAndListen( |
| 131 StreamListenSocket::Delegate* delegate) const { |
| 132 return UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
| 133 path_, fallback_path_, delegate, auth_callback_) |
| 134 .PassAs<StreamListenSocket>(); |
| 135 } |
| 136 |
| 137 #endif |
| 138 |
| 139 } // namespace net |
OLD | NEW |