drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
listen_tcp.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <config.h>
21#include <drizzled/gettext.h>
22#include <drizzled/error.h>
23#include <drizzled/plugin/listen_tcp.h>
24#include <drizzled/errmsg_print.h>
25#include <drizzled/constrained_value.h>
26
27#include <cstdio>
28#include <unistd.h>
29#include <sys/socket.h>
30#include <fcntl.h>
31#include <netdb.h>
32#include <netinet/tcp.h>
33#include <cerrno>
34
35namespace drizzled {
36
37extern back_log_constraints back_log;
38extern uint32_t drizzled_bind_timeout;
39
41{
42 for (int retry= 0; retry < 10; retry++)
43 {
44 int new_fd= accept(fd, NULL, 0);
45 if (new_fd != -1)
46 return new_fd;
47 if (errno != EINTR && errno != EAGAIN)
48 break;
49 }
50 if ((accept_error_count++ & 255) == 0)
51 {
52 sql_perror(_("accept() failed with errno %d"));
53 }
54 if (errno == ENFILE || errno == EMFILE)
55 sleep(1);
56 return -1;
57}
58
59bool plugin::ListenTcp::getFileDescriptors(std::vector<int> &fds)
60{
61 int ret;
62 char host_buf[NI_MAXHOST];
63 char port_buf[NI_MAXSERV];
64 addrinfo hints;
65 addrinfo *ai_list;
66 uint32_t this_wait;
67 int flags= 1;
68
69 memset(&hints, 0, sizeof(struct addrinfo));
70 hints.ai_flags= AI_PASSIVE;
71 hints.ai_socktype= SOCK_STREAM;
72
73 snprintf(port_buf, NI_MAXSERV, "%d", getPort());
74 ret= getaddrinfo(getHost().empty() ? NULL : getHost().c_str(), port_buf, &hints, &ai_list);
75 if (ret != 0)
76 {
77 errmsg_printf(error::ERROR, _("getaddrinfo() failed with error %s"),
78 gai_strerror(ret));
79 return true;
80 }
81
82 /*
83 getaddrinfo() can return more than one address for a single host. For
84 example, "localhost" on a dual-stack system resolves to both ::1 and
85 127.0.0.1. We bind a socket for each address we can, but a failure to
86 bind one address is not fatal: we skip it and continue to the next. The
87 caller (Listen::setup()) treats binding zero sockets across all
88 protocols as the error condition, so a partially-successful resolve
89 (e.g. IPv6 listener up, IPv4 unavailable) still lets the server run. A
90 bind failure here is therefore logged at WARN, not propagated as a hard
91 error, unless it is a genuinely unrecoverable setup failure (a failed
92 setsockopt/fcntl on an otherwise-good socket).
93 */
94 bool unrecoverable= false;
95
96 for (addrinfo* ai= ai_list; ai != NULL && !unrecoverable; ai= ai->ai_next)
97 {
98 ret= getnameinfo(ai->ai_addr, ai->ai_addrlen, host_buf, NI_MAXHOST,
99 port_buf, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV);
100 if (ret != 0)
101 {
102 strcpy(host_buf, "-");
103 strcpy(port_buf, "-");
104 }
105
106 int fd= socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
107 if (fd == -1)
108 {
109 /*
110 Call to socket() can fail for some getaddrinfo results, try another.
111 */
112 continue;
113 }
114
115#ifdef IPV6_V6ONLY
116 /*
117 Always restrict AF_INET6 sockets to IPv6 only. Without this, an IPv6
118 wildcard or loopback bind can also occupy the matching IPv4 port,
119 causing a subsequent bind of the IPv4 address from the same resolve
120 (e.g. the 127.0.0.1 half of "localhost") to fail with EADDRINUSE
121 against our own IPv6 socket.
122 */
123 if (ai->ai_family == AF_INET6)
124 {
125 flags= 1;
126 ret= setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flags, sizeof(flags));
127 if (ret != 0)
128 {
129 sql_perror(_("setsockopt(IPV6_V6ONLY)"));
130 close(fd);
131 unrecoverable= true;
132 break;
133 }
134 }
135#endif
136
137 flags= 1;
138 ret= fcntl(fd, F_SETFD, FD_CLOEXEC);
139 if (ret != 0 || !(fcntl(fd, F_GETFD, 0) & FD_CLOEXEC))
140 {
141 sql_perror(_("fcntl(FD_CLOEXEC)"));
142 close(fd);
143 unrecoverable= true;
144 break;
145 }
146
147 ret= setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
148 if (ret != 0)
149 {
150 sql_perror(_("setsockopt(SO_REUSEADDR)"));
151 close(fd);
152 unrecoverable= true;
153 break;
154 }
155
156 ret= setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags));
157 if (ret != 0)
158 {
159 sql_perror(_("setsockopt(SO_KEEPALIVE)"));
160 close(fd);
161 unrecoverable= true;
162 break;
163 }
164
165 linger ling= {0, 0};
166 ret= setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
167 if (ret != 0)
168 {
169 sql_perror(_("setsockopt(SO_LINGER)"));
170 close(fd);
171 unrecoverable= true;
172 break;
173 }
174
175 ret= setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags));
176 if (ret != 0)
177 {
178 sql_perror(_("setsockopt(TCP_NODELAY)"));
179 close(fd);
180 unrecoverable= true;
181 break;
182 }
183
184 /*
185 Sometimes the port is not released fast enough when stopping and
186 restarting the server. This happens quite often with the test suite
187 on busy Linux systems. Retry to bind the address at these intervals:
188 Sleep intervals: 1, 2, 4, 6, 9, 13, 17, 22, ...
189 Retry at second: 1, 3, 7, 13, 22, 35, 52, 74, ...
190 Limit the sequence by drizzled_bind_timeout.
191 */
192 for (uint32_t waited= 0, retry= 1; ; retry++, waited+= this_wait)
193 {
194 if (((ret= ::bind(fd, ai->ai_addr, ai->ai_addrlen)) == 0) ||
195 (errno != EADDRINUSE) || (waited >= drizzled_bind_timeout))
196 {
197 break;
198 }
199
200 errmsg_printf(error::INFO, _("Retrying bind() on %u"), getPort());
201 this_wait= retry * retry / 3 + 1;
202 sleep(this_wait);
203 }
204
205 if (ret < 0)
206 {
207 /*
208 Could not bind this particular address. This is not fatal on its
209 own: another address from the same resolve may still succeed, and
210 the caller fails the startup only if no socket binds at all. Log,
211 release this fd, and move on to the next address.
212 */
213 std::string error_message;
214
215 error_message+= host_buf;
216 error_message+= ":";
217 error_message+= port_buf;
218 error_message+= _(" failed to bind, skipping");
219 sql_perror(error_message);
220
221 close(fd);
222 continue;
223 }
224
225 if (listen(fd, (int) back_log) < 0)
226 {
227 sql_perror("listen()");
228 close(fd);
229 continue;
230 }
231
232 fds.push_back(fd);
233
234 errmsg_printf(error::INFO, _("Listening on %s:%s"), host_buf, port_buf);
235 }
236
237 freeaddrinfo(ai_list);
238
239 return unrecoverable;
240}
241
242const std::string plugin::ListenTcp::getHost() const
243{
244 return "";
245}
246
247} /* namespace drizzled */
virtual in_port_t getPort() const =0
virtual bool getFileDescriptors(std::vector< int > &)
Definition listen_tcp.cc:59
virtual const std::string getHost() const
TODO: Rename this file - func.h is stupid.