559
559
/* Abort listening to new connections */
560
if (ip_sock != INVALID_SOCKET)
562
(void) shutdown(ip_sock, SHUT_RDWR);
563
(void) closesocket(ip_sock);
564
ip_sock= INVALID_SOCKET;
563
for (x= 0; x < pollfd_count; x++)
565
if (fds[x].fd != INVALID_SOCKET)
567
(void) shutdown(fds[x].fd, SHUT_RDWR);
568
(void) closesocket(fds[x].fd);
569
fds[x].fd= INVALID_SOCKET;
567
574
end_thr_alarm(0); // Abort old alarms.
644
651
static void close_server_sock()
646
653
#ifdef HAVE_CLOSE_SERVER_SOCK
649
if (tmp_sock != INVALID_SOCKET)
651
ip_sock=INVALID_SOCKET;
652
VOID(shutdown(tmp_sock, SHUT_RDWR));
657
for (x= 0; x < pollfd_count; x++)
659
if (fds[x].fd != INVALID_SOCKET)
661
(void) shutdown(fds[x].fd, SHUT_RDWR);
662
(void) closesocket(fds[x].fd);
663
fds[x].fd= INVALID_SOCKET;
1078
1091
static void network_init(void)
1083
1095
uint this_wait;
1085
1097
char port_buf[NI_MAXSERV];
1098
struct addrinfo *ai;
1099
struct addrinfo *next;
1100
struct addrinfo hints;
1087
1103
if (thread_scheduler.init())
1088
1104
unireg_abort(1); /* purecov: inspected */
1092
FD_ZERO(&clientFDs);
1093
if (mysqld_port != 0)
1095
struct addrinfo *ai;
1096
struct addrinfo hints;
1099
bzero(&hints, sizeof (hints));
1100
hints.ai_flags= AI_PASSIVE;
1101
hints.ai_socktype= SOCK_STREAM;
1102
hints.ai_family= AF_UNSPEC;
1104
snprintf(port_buf, NI_MAXSERV, "%d", mysqld_port);
1105
error= getaddrinfo(my_bind_addr_str, port_buf, &hints, &ai);
1108
sql_perror(ER(ER_IPSOCK_ERROR)); /* purecov: tested */
1109
unireg_abort(1); /* purecov: tested */
1113
ip_sock= socket(ai->ai_family, ai->ai_socktype,
1108
memset(fds, 0, sizeof(struct pollfd) * UINT8_MAX);
1109
memset(&hints, 0, sizeof (hints));
1110
hints.ai_flags= AI_PASSIVE;
1111
hints.ai_socktype= SOCK_STREAM;
1112
hints.ai_family= AF_INET;
1113
hints.ai_protocol= IPPROTO_TCP;
1115
snprintf(port_buf, NI_MAXSERV, "%d", mysqld_port);
1116
error= getaddrinfo(my_bind_addr_str, port_buf, &hints, &ai);
1119
sql_perror(ER(ER_IPSOCK_ERROR)); /* purecov: tested */
1120
unireg_abort(1); /* purecov: tested */
1123
for (next= ai, pollfd_count= 0; next; next= next->ai_next, pollfd_count++)
1127
ip_sock= socket(next->ai_family, next->ai_socktype, next->ai_protocol);
1116
1128
if (ip_sock == INVALID_SOCKET)
1118
1130
sql_perror(ER(ER_IPSOCK_ERROR)); /* purecov: tested */
1119
1131
unireg_abort(1); /* purecov: tested */
1123
We should not use SO_REUSEADDR on windows as this would enable a
1124
user to open two mysqld servers with the same TCP/IP port.
1127
(void) setsockopt(ip_sock,SOL_SOCKET,SO_REUSEADDR,(char*)&arg,sizeof(arg));
1131
For interoperability with older clients, IPv6 socket should
1132
listen on both IPv6 and IPv4 wildcard addresses.
1133
Turn off IPV6_V6ONLY option.
1135
if (ai->ai_family == AF_INET6)
1138
(void) setsockopt(ip_sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
1134
fds[pollfd_count].fd= ip_sock;
1135
fds[pollfd_count].events= POLLIN | POLLERR;
1143
1139
Sometimes the port is not released fast enough when stopping and
1144
1140
restarting the server. This happens quite often with the test suite
1150
1146
for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
1152
if (((ret= bind(ip_sock, ai->ai_addr, ai->ai_addrlen)) >= 0 ) ||
1148
if (((ret= bind(ip_sock, next->ai_addr, next->ai_addrlen)) >= 0 ) ||
1153
1149
(socket_errno != SOCKET_EADDRINUSE) ||
1154
1150
(waited >= mysqld_port_timeout))
1175
1171
/* Add the socket to our listeners */
1178
FD_SET(ip_sock, &clientFDs);
1179
ip_flags= fcntl(ip_sock, F_GETFL, 0);
1173
struct linger ling = {0, 0};
1181
if (!(test_flags & TEST_BLOCKING))
1183
fcntl(ip_sock, F_SETFL, ip_flags | O_NONBLOCK);
1184
fcntl(ip_sock, F_SETFL, ip_flags | O_NDELAY);
1176
(void) setsockopt(ip_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&flags, sizeof(flags));
1177
(void) setsockopt(ip_sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags));
1178
(void) setsockopt(ip_sock, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling));
1179
(void) setsockopt(ip_sock, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
2784
2778
void handle_connections_sockets()
2786
2781
my_socket sock,new_sock;
2787
2782
uint error_count=0;
2788
uint max_used_connection= (uint)ip_sock+1;
2791
2784
struct sockaddr_storage cAddr;
2792
int ip_flags=0, flags;
2793
2785
st_vio *vio_tmp;
2795
ip_flags= fcntl(ip_sock, F_GETFL, 0);
2797
2787
MAYBE_BROKEN_SYSCALL;
2798
2788
while (!abort_loop)
2801
if (select((int) max_used_connection, &readFDs,0,0,0) < 0)
2792
if ((number_of= poll(fds, pollfd_count, -1)) == -1)
2803
2794
if (socket_errno != SOCKET_EINTR)
2808
2799
MAYBE_BROKEN_SYSCALL
2805
#ifdef FIXME_IF_WE_WERE_KEEPING_THIS
2806
assert(number_of > 1); /* Not handling this at the moment */
2811
2809
if (abort_loop)
2813
2811
MAYBE_BROKEN_SYSCALL;
2815
for (x= 0, sock= -1; x < pollfd_count; x++)
2817
if (fds[x].revents == POLLIN)
2820
2825
for (uint retry=0; retry < MAX_ACCEPT_RETRY; retry++)
2822
2827
size_socket length= sizeof(struct sockaddr_storage);
2823
2828
new_sock= accept(sock, (struct sockaddr *)(&cAddr),
2825
if (new_sock != INVALID_SOCKET ||
2826
(socket_errno != SOCKET_EINTR && socket_errno != SOCKET_EAGAIN))
2830
if (new_sock != INVALID_SOCKET || (socket_errno != SOCKET_EINTR && socket_errno != SOCKET_EAGAIN))
2828
MAYBE_BROKEN_SYSCALL;
2829
if (!(test_flags & TEST_BLOCKING))
2831
if (retry == MAX_ACCEPT_RETRY - 1)
2832
fcntl(sock, F_SETFL, flags); // Try without O_NONBLOCK
2836
if (!(test_flags & TEST_BLOCKING))
2837
fcntl(sock, F_SETFL, flags);
2839
2835
if (new_sock == INVALID_SOCKET)