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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/*
Copyright (c) 2003-2007 MySQL AB, 2009 Sun Microsystems, Inc.
Use is subject to license terms.
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
*/
#if defined(__GNUC__) && defined(USE_PRAGMA_IMPLEMENTATION)
#pragma implementation
#endif
#include "listener.h"
#include <my_global.h>
#include <mysql.h>
#include <violite.h>
#include <sys/stat.h>
#ifndef __WIN__
#include <sys/un.h>
#endif
#include "log.h"
#include "mysql_connection.h"
#include "options.h"
#include "portability.h"
#include "priv.h"
#include "thread_registry.h"
static void set_non_blocking(int socket)
{
#ifndef __WIN__
int flags= fcntl(socket, F_GETFL, 0);
fcntl(socket, F_SETFL, flags | O_NONBLOCK);
#else
u_long arg= 1;
ioctlsocket(socket, FIONBIO, &arg);
#endif
}
static void set_no_inherit(int socket)
{
#ifndef __WIN__
int flags= fcntl(socket, F_GETFD, 0);
fcntl(socket, F_SETFD, flags | FD_CLOEXEC);
#endif
}
const int Listener::LISTEN_BACK_LOG_SIZE= 5; /* standard backlog size */
Listener::Listener(Thread_registry *thread_registry_arg,
User_map *user_map_arg)
:thread_registry(thread_registry_arg),
user_map(user_map_arg),
total_connection_count(0),
num_sockets(0)
{
}
/*
Listener::run() - listen all supported sockets and spawn a thread
to handle incoming connection.
Using 'die' in case of syscall failure is OK now - we don't hold any
resources and 'die' kills the signal thread automatically. To be rewritten
one day.
See also comments in mysqlmanager.cc to picture general Instance Manager
architecture.
*/
void Listener::run()
{
int i, n= 0;
#ifndef __WIN__
struct sockaddr_un unix_socket_address;
#endif
log_info("Listener: started.");
thread_registry->register_thread(&thread_info);
FD_ZERO(&read_fds);
/* I. prepare 'listen' sockets */
if (create_tcp_socket())
goto err;
#ifndef __WIN__
if (create_unix_socket(unix_socket_address))
goto err;
#endif
/* II. Listen sockets and spawn childs */
for (i= 0; i < num_sockets; i++)
n= max(n, sockets[i]);
n++;
timeval tv;
while (!thread_registry->is_shutdown())
{
fd_set read_fds_arg= read_fds;
/*
We should reintialize timer as on linux it is modified
to reflect amount of time not slept.
*/
tv.tv_sec= 0;
tv.tv_usec= 100000;
/*
When using valgrind 2.0 this syscall doesn't get kicked off by a
signal during shutdown. This results in failing assert
(Thread_registry::~Thread_registry). Valgrind 2.2 works fine.
*/
int rc= select(n, &read_fds_arg, 0, 0, &tv);
if (rc == 0 || rc == -1)
{
if (rc == -1 && errno != EINTR)
log_error("Listener: select() failed: %s.",
(const char *) strerror(errno));
continue;
}
for (int socket_index= 0; socket_index < num_sockets; socket_index++)
{
/* Assuming that rc > 0 as we asked to wait forever */
if (FD_ISSET(sockets[socket_index], &read_fds_arg))
{
int client_fd= accept(sockets[socket_index], 0, 0);
/* accept may return -1 (failure or spurious wakeup) */
if (client_fd >= 0) // connection established
{
set_no_inherit(client_fd);
struct st_vio *vio=
vio_new(client_fd,
socket_index == 0 ? VIO_TYPE_SOCKET : VIO_TYPE_TCPIP,
socket_index == 0 ? 1 : 0);
if (vio != NULL)
handle_new_mysql_connection(vio);
else
{
shutdown(client_fd, SHUT_RDWR);
closesocket(client_fd);
}
}
}
}
}
/* III. Release all resources and exit */
log_info("Listener: shutdown requested, exiting...");
for (i= 0; i < num_sockets; i++)
closesocket(sockets[i]);
#ifndef __WIN__
unlink(unix_socket_address.sun_path);
#endif
thread_registry->unregister_thread(&thread_info);
log_info("Listener: finished.");
return;
err:
log_error("Listener: failed to initialize. Initiate shutdown...");
// we have to close the ip sockets in case of error
for (i= 0; i < num_sockets; i++)
closesocket(sockets[i]);
thread_registry->set_error_status();
thread_registry->unregister_thread(&thread_info);
thread_registry->request_shutdown();
return;
}
int Listener::create_tcp_socket()
{
/* value to be set by setsockopt */
int arg= 1;
int ip_socket= socket(AF_INET, SOCK_STREAM, 0);
if (ip_socket == INVALID_SOCKET)
{
log_error("Listener: socket(AF_INET) failed: %s.",
(const char *) strerror(errno));
return -1;
}
struct sockaddr_in ip_socket_address;
bzero(&ip_socket_address, sizeof(ip_socket_address));
ulong im_bind_addr;
if (Options::Main::bind_address != 0)
{
im_bind_addr= (ulong) inet_addr(Options::Main::bind_address);
if (im_bind_addr == (ulong) INADDR_NONE)
im_bind_addr= htonl(INADDR_ANY);
}
else
im_bind_addr= htonl(INADDR_ANY);
uint im_port= Options::Main::port_number;
ip_socket_address.sin_family= AF_INET;
ip_socket_address.sin_addr.s_addr= im_bind_addr;
ip_socket_address.sin_port= (unsigned short)
htons((unsigned short) im_port);
setsockopt(ip_socket, SOL_SOCKET, SO_REUSEADDR, (char*) &arg, sizeof(arg));
if (bind(ip_socket, (struct sockaddr *) &ip_socket_address,
sizeof(ip_socket_address)))
{
log_error("Listener: bind(ip socket) failed: %s.",
(const char *) strerror(errno));
closesocket(ip_socket);
return -1;
}
if (listen(ip_socket, LISTEN_BACK_LOG_SIZE))
{
log_error("Listener: listen(ip socket) failed: %s.",
(const char *) strerror(errno));
closesocket(ip_socket);
return -1;
}
/* set the socket nonblocking */
set_non_blocking(ip_socket);
/* make sure that instances won't be listening our sockets */
set_no_inherit(ip_socket);
FD_SET(ip_socket, &read_fds);
sockets[num_sockets++]= ip_socket;
log_info("Listener: accepting connections on ip socket (port: %d)...",
(int) im_port);
return 0;
}
#ifndef __WIN__
int Listener::
create_unix_socket(struct sockaddr_un &unix_socket_address)
{
int unix_socket= socket(AF_UNIX, SOCK_STREAM, 0);
if (unix_socket == INVALID_SOCKET)
{
log_error("Listener: socket(AF_UNIX) failed: %s.",
(const char *) strerror(errno));
return -1;
}
bzero(&unix_socket_address, sizeof(unix_socket_address));
unix_socket_address.sun_family= AF_UNIX;
strmake(unix_socket_address.sun_path, Options::Main::socket_file_name,
sizeof(unix_socket_address.sun_path) - 1);
unlink(unix_socket_address.sun_path); // in case we have stale socket file
/*
POSIX specifies default permissions for a pathname created by bind
to be 0777. We need everybody to have access to the socket.
*/
mode_t old_mask= umask(0);
if (bind(unix_socket, (struct sockaddr *) &unix_socket_address,
sizeof(unix_socket_address)))
{
log_error("Listener: bind(unix socket) failed for '%s': %s.",
(const char *) unix_socket_address.sun_path,
(const char *) strerror(errno));
close(unix_socket);
return -1;
}
umask(old_mask);
if (listen(unix_socket, LISTEN_BACK_LOG_SIZE))
{
log_error("Listener: listen(unix socket) failed: %s.",
(const char *) strerror(errno));
close(unix_socket);
return -1;
}
/* set the socket nonblocking */
set_non_blocking(unix_socket);
/* make sure that instances won't be listening our sockets */
set_no_inherit(unix_socket);
log_info("Listener: accepting connections on unix socket '%s'...",
(const char *) unix_socket_address.sun_path);
sockets[num_sockets++]= unix_socket;
FD_SET(unix_socket, &read_fds);
return 0;
}
#endif
/*
Create new mysql connection. Created thread is responsible for deletion of
the Mysql_connection and Vio instances passed to it.
SYNOPSIS
handle_new_mysql_connection()
*/
void Listener::handle_new_mysql_connection(struct st_vio *vio)
{
Mysql_connection *mysql_connection=
new Mysql_connection(thread_registry, user_map,
vio, ++total_connection_count);
if (mysql_connection == NULL || mysql_connection->start(Thread::DETACHED))
{
log_error("Listener: can not start connection handler.");
delete mysql_connection;
vio_delete(vio);
}
/* The connection will delete itself when the thread is finished */
}
|