1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <drizzled/gettext.h>
#include <drizzled/error.h>
#include <drizzled/plugin/listen_tcp.h>
#include <drizzled/errmsg_print.h>
#include <drizzled/constrained_value.h>
#include <cstdio>
#include <unistd.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <cerrno>
#define MAX_ACCEPT_RETRY 10 // Test accept this many times
namespace drizzled
{
extern back_log_constraints back_log;
extern uint32_t drizzled_bind_timeout;
int plugin::ListenTcp::acceptTcp(int fd)
{
int new_fd;
uint32_t retry;
for (retry= 0; retry < MAX_ACCEPT_RETRY; retry++)
{
new_fd= accept(fd, NULL, 0);
if (new_fd != -1 || (errno != EINTR && errno != EAGAIN))
break;
}
if (new_fd == -1)
{
if ((accept_error_count++ & 255) == 0)
{
sql_perror(_("accept() failed with errno %d"));
}
if (errno == ENFILE || errno == EMFILE)
sleep(1);
return -1;
}
return new_fd;
}
bool plugin::ListenTcp::getFileDescriptors(std::vector<int> &fds)
{
int ret;
char host_buf[NI_MAXHOST];
char port_buf[NI_MAXSERV];
struct addrinfo hints;
struct addrinfo *ai;
struct addrinfo *ai_list;
int fd= -1;
uint32_t waited;
uint32_t this_wait;
uint32_t retry;
struct linger ling= {0, 0};
int flags= 1;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags= AI_PASSIVE;
hints.ai_socktype= SOCK_STREAM;
snprintf(port_buf, NI_MAXSERV, "%d", getPort());
ret= getaddrinfo(getHost().empty() ? NULL : getHost().c_str(), port_buf, &hints, &ai_list);
if (ret != 0)
{
errmsg_printf(error::ERROR, _("getaddrinfo() failed with error %s"),
gai_strerror(ret));
return true;
}
for (ai= ai_list; ai != NULL; ai= ai->ai_next)
{
ret= getnameinfo(ai->ai_addr, ai->ai_addrlen, host_buf, NI_MAXHOST,
port_buf, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV);
if (ret != 0)
{
strcpy(host_buf, "-");
strcpy(port_buf, "-");
}
fd= socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd == -1)
{
/*
Call to socket() can fail for some getaddrinfo results, try another.
*/
continue;
}
#ifdef IPV6_V6ONLY
if (ai->ai_family == AF_INET6)
{
flags= 1;
ret= setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flags, sizeof(flags));
if (ret != 0)
{
sql_perror(_("setsockopt(IPV6_V6ONLY)"));
return true;
}
}
#endif
ret= fcntl(fd, F_SETFD, FD_CLOEXEC);
if (ret != 0 || !(fcntl(fd, F_GETFD, 0) & FD_CLOEXEC))
{
sql_perror(_("fcntl(FD_CLOEXEC)"));
return true;
}
ret= setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
if (ret != 0)
{
sql_perror(_("setsockopt(SO_REUSEADDR)"));
return true;
}
ret= setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags));
if (ret != 0)
{
sql_perror(_("setsockopt(SO_KEEPALIVE)"));
return true;
}
ret= setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
if (ret != 0)
{
sql_perror(_("setsockopt(SO_LINGER)"));
return true;
}
ret= setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags));
if (ret != 0)
{
sql_perror(_("setsockopt(TCP_NODELAY)"));
return true;
}
/*
Sometimes the port is not released fast enough when stopping and
restarting the server. This happens quite often with the test suite
on busy Linux systems. Retry to bind the address at these intervals:
Sleep intervals: 1, 2, 4, 6, 9, 13, 17, 22, ...
Retry at second: 1, 3, 7, 13, 22, 35, 52, 74, ...
Limit the sequence by drizzled_bind_timeout.
*/
for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
{
if (((ret= ::bind(fd, ai->ai_addr, ai->ai_addrlen)) == 0) ||
(errno != EADDRINUSE) || (waited >= drizzled_bind_timeout))
{
break;
}
errmsg_printf(error::INFO, _("Retrying bind() on %u"), getPort());
this_wait= retry * retry / 3 + 1;
sleep(this_wait);
}
if (ret < 0)
{
std::string error_message;
error_message+= host_buf;
error_message+= ":";
error_message+= port_buf;
error_message+= _(" failed to bind");
sql_perror(error_message);
return true;
}
if (listen(fd, (int) back_log) < 0)
{
sql_perror("listen()");
return true;
}
fds.push_back(fd);
errmsg_printf(error::INFO, _("Listening on %s:%s"), host_buf, port_buf);
}
freeaddrinfo(ai_list);
return false;
}
const std::string plugin::ListenTcp::getHost(void) const
{
return "";
}
} /* namespace drizzled */
|