David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 20 | #include <cstring> |
David Reiss | 08d2f11 | 2009-05-21 02:28:36 +0000 | [diff] [blame] | 21 | #include <sys/types.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | #include <sys/socket.h> |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 23 | #include <sys/un.h> |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 24 | #include <sys/poll.h> |
Mark Slee | dd56497 | 2007-08-21 02:39:57 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 26 | #include <netinet/in.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 27 | #include <netinet/tcp.h> |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 28 | #include <netdb.h> |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 30 | #include <errno.h> |
David Reiss | 5105b2e | 2009-05-21 02:28:27 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 32 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 33 | #include "TSocket.h" |
| 34 | #include "TServerSocket.h" |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 35 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 36 | |
David Reiss | 9b90344 | 2009-10-21 05:51:28 +0000 | [diff] [blame] | 37 | #ifndef AF_LOCAL |
| 38 | #define AF_LOCAL AF_UNIX |
| 39 | #endif |
| 40 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 41 | namespace apache { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 42 | |
Martin Kraemer | e6c4fa6 | 2007-07-09 19:08:25 +0000 | [diff] [blame] | 43 | using namespace std; |
David Reiss | d4a269c | 2007-08-23 02:37:19 +0000 | [diff] [blame] | 44 | using boost::shared_ptr; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 45 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 46 | TServerSocket::TServerSocket(int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 47 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 48 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 49 | acceptBacklog_(1024), |
| 50 | sendTimeout_(0), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 51 | recvTimeout_(0), |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 52 | retryLimit_(0), |
| 53 | retryDelay_(0), |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 54 | tcpSendBuffer_(0), |
| 55 | tcpRecvBuffer_(0), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 56 | intSock1_(-1), |
| 57 | intSock2_(-1) {} |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 58 | |
| 59 | TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) : |
| 60 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 61 | serverSocket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 62 | acceptBacklog_(1024), |
| 63 | sendTimeout_(sendTimeout), |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 64 | recvTimeout_(recvTimeout), |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 65 | retryLimit_(0), |
| 66 | retryDelay_(0), |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 67 | tcpSendBuffer_(0), |
| 68 | tcpRecvBuffer_(0), |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 69 | intSock1_(-1), |
| 70 | intSock2_(-1) {} |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 71 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 72 | TServerSocket::TServerSocket(string path) : |
| 73 | port_(0), |
| 74 | path_(path), |
| 75 | serverSocket_(-1), |
| 76 | acceptBacklog_(1024), |
| 77 | sendTimeout_(0), |
| 78 | recvTimeout_(0), |
| 79 | retryLimit_(0), |
| 80 | retryDelay_(0), |
| 81 | tcpSendBuffer_(0), |
| 82 | tcpRecvBuffer_(0), |
| 83 | intSock1_(-1), |
| 84 | intSock2_(-1) {} |
| 85 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 86 | TServerSocket::~TServerSocket() { |
| 87 | close(); |
| 88 | } |
| 89 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 90 | void TServerSocket::setSendTimeout(int sendTimeout) { |
| 91 | sendTimeout_ = sendTimeout; |
| 92 | } |
| 93 | |
| 94 | void TServerSocket::setRecvTimeout(int recvTimeout) { |
| 95 | recvTimeout_ = recvTimeout; |
| 96 | } |
| 97 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 98 | void TServerSocket::setRetryLimit(int retryLimit) { |
| 99 | retryLimit_ = retryLimit; |
| 100 | } |
| 101 | |
| 102 | void TServerSocket::setRetryDelay(int retryDelay) { |
| 103 | retryDelay_ = retryDelay; |
| 104 | } |
| 105 | |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 106 | void TServerSocket::setTcpSendBuffer(int tcpSendBuffer) { |
| 107 | tcpSendBuffer_ = tcpSendBuffer; |
| 108 | } |
| 109 | |
| 110 | void TServerSocket::setTcpRecvBuffer(int tcpRecvBuffer) { |
| 111 | tcpRecvBuffer_ = tcpRecvBuffer; |
| 112 | } |
| 113 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 114 | void TServerSocket::listen() { |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 115 | int sv[2]; |
| 116 | if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 117 | GlobalOutput.perror("TServerSocket::listen() socketpair() ", errno); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 118 | intSock1_ = -1; |
| 119 | intSock2_ = -1; |
| 120 | } else { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 121 | intSock1_ = sv[1]; |
| 122 | intSock2_ = sv[0]; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 125 | struct addrinfo hints, *res, *res0; |
| 126 | int error; |
| 127 | char port[sizeof("65536") + 1]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 128 | std::memset(&hints, 0, sizeof(hints)); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 129 | hints.ai_family = PF_UNSPEC; |
| 130 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 131 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 132 | sprintf(port, "%d", port_); |
| 133 | |
| 134 | // Wildcard address |
| 135 | error = getaddrinfo(NULL, port, &hints, &res0); |
| 136 | if (error) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 137 | GlobalOutput.printf("getaddrinfo %d: %s", error, gai_strerror(error)); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 138 | close(); |
| 139 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket."); |
| 140 | } |
| 141 | |
| 142 | // Pick the ipv6 address first since ipv4 addresses can be mapped |
| 143 | // into ipv6 space. |
| 144 | for (res = res0; res; res = res->ai_next) { |
| 145 | if (res->ai_family == AF_INET6 || res->ai_next == NULL) |
| 146 | break; |
| 147 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 148 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 149 | if (! path_.empty()) { |
| 150 | serverSocket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP); |
| 151 | } else { |
| 152 | serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 153 | } |
| 154 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 155 | if (serverSocket_ == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 156 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 157 | GlobalOutput.perror("TServerSocket::listen() socket() ", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 158 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 159 | throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Set reusaddress to prevent 2MSL delay on accept |
| 163 | int one = 1; |
| 164 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, |
| 165 | &one, sizeof(one))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 166 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 167 | GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_REUSEADDR ", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 168 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 169 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 172 | // Set TCP buffer sizes |
| 173 | if (tcpSendBuffer_ > 0) { |
| 174 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_SNDBUF, |
| 175 | &tcpSendBuffer_, sizeof(tcpSendBuffer_))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 176 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 177 | GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_SNDBUF ", errno_copy); |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 178 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 179 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_SNDBUF", errno_copy); |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | if (tcpRecvBuffer_ > 0) { |
| 184 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_RCVBUF, |
| 185 | &tcpRecvBuffer_, sizeof(tcpRecvBuffer_))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 186 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 187 | GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_RCVBUF ", errno_copy); |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 188 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 189 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_RCVBUF", errno_copy); |
Christopher Piro | 9cc63b5 | 2008-03-21 00:40:42 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 193 | // Defer accept |
| 194 | #ifdef TCP_DEFER_ACCEPT |
| 195 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT, |
| 196 | &one, sizeof(one))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 197 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 198 | GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_DEFER_ACCEPT ", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 199 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 200 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 201 | } |
| 202 | #endif // #ifdef TCP_DEFER_ACCEPT |
| 203 | |
David Reiss | 13aea46 | 2008-06-10 22:56:04 +0000 | [diff] [blame] | 204 | #ifdef IPV6_V6ONLY |
David Reiss | eee98be | 2010-03-09 05:20:10 +0000 | [diff] [blame] | 205 | if (res->ai_family == AF_INET6) { |
| 206 | int zero = 0; |
| 207 | if (-1 == setsockopt(serverSocket_, IPPROTO_IPV6, IPV6_V6ONLY, |
| 208 | &zero, sizeof(zero))) { |
| 209 | GlobalOutput.perror("TServerSocket::listen() IPV6_V6ONLY ", errno); |
| 210 | } |
David Reiss | 13aea46 | 2008-06-10 22:56:04 +0000 | [diff] [blame] | 211 | } |
| 212 | #endif // #ifdef IPV6_V6ONLY |
| 213 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 214 | // Turn linger off, don't want to block on calls to close |
| 215 | struct linger ling = {0, 0}; |
| 216 | if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER, |
| 217 | &ling, sizeof(ling))) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 218 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 219 | GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_LINGER ", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 220 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 221 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 224 | // Unix Sockets do not need that |
| 225 | if (path_.empty()) { |
| 226 | // TCP Nodelay, speed over bandwidth |
| 227 | if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY, |
| 228 | &one, sizeof(one))) { |
| 229 | int errno_copy = errno; |
| 230 | GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_NODELAY ", errno_copy); |
| 231 | close(); |
| 232 | throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY", errno_copy); |
| 233 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 236 | // Set NONBLOCK on the accept socket |
| 237 | int flags = fcntl(serverSocket_, F_GETFL, 0); |
| 238 | if (flags == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 239 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 240 | GlobalOutput.perror("TServerSocket::listen() fcntl() F_GETFL ", errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 241 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 242 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 243 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 244 | if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 245 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 246 | GlobalOutput.perror("TServerSocket::listen() fcntl() O_NONBLOCK ", errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 247 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 248 | } |
| 249 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 250 | // prepare the port information |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 251 | // we may want to try to bind more than once, since SO_REUSEADDR doesn't |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 252 | // always seem to work. The client can configure the retry variables. |
| 253 | int retries = 0; |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 254 | |
| 255 | if (! path_.empty()) { |
| 256 | // Unix Domain Socket |
| 257 | struct sockaddr_un address; |
| 258 | socklen_t len; |
| 259 | |
| 260 | if (path_.length() > sizeof(address.sun_path)) { |
| 261 | int errno_copy = errno; |
| 262 | GlobalOutput.perror("TSocket::listen() Unix Domain socket path too long", errno_copy); |
| 263 | throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path too long"); |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 266 | address.sun_family = AF_UNIX; |
Roger Meier | d11ca5a | 2010-10-18 08:22:57 +0000 | [diff] [blame] | 267 | snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str()); |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 268 | len = sizeof(address); |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 269 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 270 | do { |
| 271 | if (0 == bind(serverSocket_, (struct sockaddr *) &address, len)) { |
| 272 | break; |
| 273 | } |
| 274 | // use short circuit evaluation here to only sleep if we need to |
| 275 | } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0)); |
| 276 | } else { |
| 277 | do { |
| 278 | if (0 == bind(serverSocket_, res->ai_addr, res->ai_addrlen)) { |
| 279 | break; |
| 280 | } |
| 281 | // use short circuit evaluation here to only sleep if we need to |
| 282 | } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0)); |
| 283 | |
| 284 | // free addrinfo |
| 285 | freeaddrinfo(res0); |
| 286 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 287 | |
boz | 1ea81ce | 2007-05-14 23:04:33 +0000 | [diff] [blame] | 288 | // throw an error if we failed to bind properly |
| 289 | if (retries > retryLimit_) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 290 | char errbuf[1024]; |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 291 | if (! path_.empty()) { |
| 292 | sprintf(errbuf, "TServerSocket::listen() PATH %s", path_.c_str()); |
| 293 | } |
| 294 | else { |
| 295 | sprintf(errbuf, "TServerSocket::listen() BIND %d", port_); |
| 296 | } |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 297 | GlobalOutput(errbuf); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 298 | close(); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 299 | throw TTransportException(TTransportException::NOT_OPEN, "Could not bind"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // Call listen |
| 303 | if (-1 == ::listen(serverSocket_, acceptBacklog_)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 304 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 305 | GlobalOutput.perror("TServerSocket::listen() listen() ", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 306 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 307 | throw TTransportException(TTransportException::NOT_OPEN, "Could not listen", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // The socket is now listening! |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 313 | shared_ptr<TTransport> TServerSocket::acceptImpl() { |
Martin Kraemer | 10640d8 | 2007-02-03 01:59:12 +0000 | [diff] [blame] | 314 | if (serverSocket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 315 | throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 316 | } |
| 317 | |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 318 | struct pollfd fds[2]; |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 319 | |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 320 | int maxEintrs = 5; |
| 321 | int numEintrs = 0; |
| 322 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 323 | while (true) { |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 324 | std::memset(fds, 0 , sizeof(fds)); |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 325 | fds[0].fd = serverSocket_; |
| 326 | fds[0].events = POLLIN; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 327 | if (intSock2_ >= 0) { |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 328 | fds[1].fd = intSock2_; |
David Reiss | 2724c7a | 2008-04-18 17:48:03 +0000 | [diff] [blame] | 329 | fds[1].events = POLLIN; |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 330 | } |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 331 | int ret = poll(fds, 2, -1); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 332 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 333 | if (ret < 0) { |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 334 | // error cases |
boz | f83c9db | 2007-05-31 23:38:37 +0000 | [diff] [blame] | 335 | if (errno == EINTR && (numEintrs++ < maxEintrs)) { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 336 | // EINTR needs to be handled manually and we can tolerate |
Aditya Agarwal | 7859a57 | 2007-05-31 01:33:07 +0000 | [diff] [blame] | 337 | // a certain number |
| 338 | continue; |
| 339 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 340 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 341 | GlobalOutput.perror("TServerSocket::acceptImpl() poll() ", errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 342 | throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 343 | } else if (ret > 0) { |
| 344 | // Check for an interrupt signal |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 345 | if (intSock2_ >= 0 && (fds[1].revents & POLLIN)) { |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 346 | int8_t buf; |
| 347 | if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 348 | GlobalOutput.perror("TServerSocket::acceptImpl() recv() interrupt ", errno); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 349 | } |
| 350 | throw TTransportException(TTransportException::INTERRUPTED); |
| 351 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 352 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 353 | // Check for the actual server socket being ready |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 354 | if (fds[0].revents & POLLIN) { |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | } else { |
David Reiss | 1677ac9 | 2008-04-08 06:26:27 +0000 | [diff] [blame] | 358 | GlobalOutput("TServerSocket::acceptImpl() poll 0"); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 359 | throw TTransportException(TTransportException::UNKNOWN); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 363 | struct sockaddr_storage clientAddress; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 364 | int size = sizeof(clientAddress); |
| 365 | int clientSocket = ::accept(serverSocket_, |
| 366 | (struct sockaddr *) &clientAddress, |
| 367 | (socklen_t *) &size); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 368 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 369 | if (clientSocket < 0) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 370 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 371 | GlobalOutput.perror("TServerSocket::acceptImpl() ::accept() ", errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 372 | throw TTransportException(TTransportException::UNKNOWN, "accept()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 373 | } |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 374 | |
| 375 | // Make sure client socket is blocking |
| 376 | int flags = fcntl(clientSocket, F_GETFL, 0); |
| 377 | if (flags == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 378 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 379 | GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_GETFL ", errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 380 | throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_GETFL)", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 381 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 382 | |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 383 | if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 384 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 385 | GlobalOutput.perror("TServerSocket::acceptImpl() fcntl() F_SETFL ~O_NONBLOCK ", errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 386 | throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_SETFL)", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 387 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 388 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 389 | shared_ptr<TSocket> client(new TSocket(clientSocket)); |
| 390 | if (sendTimeout_ > 0) { |
| 391 | client->setSendTimeout(sendTimeout_); |
| 392 | } |
| 393 | if (recvTimeout_ > 0) { |
| 394 | client->setRecvTimeout(recvTimeout_); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 395 | } |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 396 | client->setCachedAddress((sockaddr*) &clientAddress, size); |
| 397 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 398 | return client; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 401 | void TServerSocket::interrupt() { |
| 402 | if (intSock1_ >= 0) { |
| 403 | int8_t byte = 0; |
| 404 | if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 405 | GlobalOutput.perror("TServerSocket::interrupt() send() ", errno); |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 410 | void TServerSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 411 | if (serverSocket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 412 | shutdown(serverSocket_, SHUT_RDWR); |
| 413 | ::close(serverSocket_); |
| 414 | } |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 415 | if (intSock1_ >= 0) { |
| 416 | ::close(intSock1_); |
| 417 | } |
| 418 | if (intSock2_ >= 0) { |
| 419 | ::close(intSock2_); |
| 420 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 421 | serverSocket_ = -1; |
Mark Slee | 561b536 | 2007-03-09 19:26:29 +0000 | [diff] [blame] | 422 | intSock1_ = -1; |
| 423 | intSock2_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 424 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 425 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 426 | }}} // apache::thrift::transport |