MySQL  8.0.16
Source Code Documentation
socket_connection.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License, version 2.0,
6  as published by the Free Software Foundation.
7 
8  This program is also distributed with certain software (including
9  but not limited to OpenSSL) that is licensed under separate terms,
10  as designated in a particular file or component or in included license
11  documentation. The authors of MySQL hereby grant you an additional
12  permission to link the program and your derivative works with the
13  separately licensed software that they have included with MySQL.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License, version 2.0, for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24 
25 #ifndef SOCKET_CONNECTION_INCLUDED
26 #define SOCKET_CONNECTION_INCLUDED
27 
28 #include "my_config.h"
29 
30 #include <sys/types.h>
31 #include <list>
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 #include "my_psi_config.h"
38 #include "mysql/psi/mysql_socket.h" // MYSQL_SOCKET
39 #ifdef HAVE_POLL_H
40 #include <poll.h>
41 #endif
42 
43 class Channel_info;
44 
45 extern const char *MY_BIND_ALL_ADDRESSES;
46 extern const char *ipv4_all_addresses;
47 extern const char *ipv6_all_addresses;
48 
49 #ifdef HAVE_PSI_STATEMENT_INTERFACE
51 #endif
52 
53 /**
54  Key Comparator for socket_map_t used in Mysqld_socket_listener
55 */
57  bool operator()(const MYSQL_SOCKET &s1, const MYSQL_SOCKET &s2) const {
58  return mysql_socket_getfd(s1) < mysql_socket_getfd(s2);
59  }
60 };
61 
62 // Enum denoting type of socket whether unix socket or tcp socket.
64 // Listen socket attributes.
65 struct Socket_attr {
66  explicit Socket_attr(Socket_type socket_type) : m_socket_type(socket_type) {}
67  Socket_attr(Socket_type socket_type, const std::string &network_namespace)
68  : m_socket_type(socket_type), m_network_namespace(network_namespace) {}
70  std::string m_network_namespace;
71 };
72 
73 /**
74  Typedef representing socket map type which hold the sockets and a
75  corresponding bool which is true if it is unix socket and false for tcp
76  socket.
77 */
78 typedef std::map<MYSQL_SOCKET, Socket_attr, Socket_lt_type> socket_map_t;
79 
80 // iterator type for socket map type.
81 typedef std::map<MYSQL_SOCKET, Socket_attr, Socket_lt_type>::const_iterator
83 
84 /**
85  Plain structure to collect together a host name/ip address and
86  a corresponding network namespace if set and pass these information
87  to different functions as a single unit.
88 */
90  std::string address, network_namespace;
91  Bind_address_info() = default;
92 
93  explicit Bind_address_info(const std::string &addr) : address(addr) {}
94 
95  Bind_address_info(const std::string &addr, const std::string &nspace)
96  : address(addr), network_namespace(nspace) {}
97 };
98 
99 /**
100  This class represents the Mysqld_socket_listener which prepare the
101  listener sockets to recieve connection events from the client. The
102  Mysqld_socket_listener may be composed of either or both a tcp socket
103  which listen on a default mysqld tcp port or a user specified port
104  via mysqld command-line and a unix socket which is bind to a mysqld
105  defaul pathname.
106 */
108  /*
109  Addresses to listen to and network namespace for
110  every address if set.
111  */
112  std::list<Bind_address_info> m_bind_addresses;
113  /*
114  Address to listen to an admin connection request
115  and network namespace if set.
116  */
118  uint m_tcp_port; // TCP port to bind to
119  uint m_admin_tcp_port; // TCP port to bind to for support admin connection
120  bool m_use_separate_thread_for_admin; // use a separate thread for listening
121  // to admin interface
122  uint m_backlog; // backlog specifying length of pending connection queue
123  uint m_port_timeout; // port timeout value
124  std::string m_unix_sockname; // unix socket pathname to bind to
125  bool m_unlink_sockname; // Unlink socket & lock file if true.
126  /*
127  Map indexed by MYSQL socket fds and correspoding bool to distinguish
128  between unix and tcp socket.
129  */
130  socket_map_t m_socket_map; // map indexed by mysql socket fd and index
132 
133 #ifdef HAVE_POLL
134  struct poll_info_t {
135  std::vector<struct pollfd> m_fds;
136  std::vector<MYSQL_SOCKET> m_pfs_fds;
137  };
138  // poll related info. used in poll for listening to connection events.
139  poll_info_t m_poll_info;
140 #else
141  struct select_info_t {
145  };
146  // select info for used in select for listening to connection events.
148 #endif // HAVE_POLL
149 
150  public:
151  /**
152  Constructor to setup a listener for listen to connect events from
153  clients.
154 
155  @param bind_addresses list of addresses to listen to
156  @param tcp_port TCP port to bind to
157  @param admin_bind_addr address to listen admin connection
158  @param admin_tcp_port TCP port for admin connection to bind
159  @param use_separate_thread_for_admin Listen to connection requests
160  on admin interface in a separate thread
161  @param backlog backlog specifying length of pending
162  connection queue used in listen.
163  @param port_timeout portname.
164  @param unix_sockname pathname for unix socket to bind to
165  */
166  Mysqld_socket_listener(const std::list<Bind_address_info> &bind_addresses,
167  uint tcp_port,
168  const Bind_address_info &admin_bind_addr,
169  uint admin_tcp_port,
170  bool use_separate_thread_for_admin, uint backlog,
171  uint port_timeout, std::string unix_sockname);
172 
173  /**
174  Set up a listener - set of sockets to listen for connection events
175  from clients.
176 
177  In case a server is started with the option
178  use_separate_thread_for_admin=true invocation of this method also spawns a
179  thread to handle incoming connection requests on admin interface.
180 
181  @retval false listener sockets setup to be used to listen for connect
182  events true failure in setting up the listener.
183  */
184  bool setup_listener();
185 
186  /**
187  The body of the event loop that listen for connection events from clients.
188 
189  @retval Channel_info Channel_info object abstracting the connected client
190  details for processing this connection.
191  */
193 
194  /**
195  Close the listener.
196 
197  In case a server is started with the option
198  use_separate_thread_for_admin=true this method also shutdowns a thread for
199  handling of incoming connection requests on admin interface and joins it.
200  */
201  void close_listener();
202 
204  if (!m_socket_map.empty()) close_listener();
205  }
206 
207  private:
208  /**
209  Add a socket to a set of sockets being waiting for a new
210  connection request.
211 
212  @param listen_socket Socket to listen for.
213  */
214  void add_socket_to_listener(MYSQL_SOCKET listen_socket);
215 
216  /**
217  Get a socket ready to accept incoming connection.
218  @param[out] is_unix_socket has the value true in case a new incoming
219  connection ready for acceptance pertains
220  to unix socket domain.
221  @param[out] is_admin_socket has the value true in case a new incoming
222  connection is waiting for acceptance on
223  admin interface.
224 
225  @return A socket ready to accept a new incoming connection.
226  */
227  MYSQL_SOCKET get_ready_socket(bool *is_unix_socket,
228  bool *is_admin_socket) const;
229 
230  /**
231  Set up connection events for poll or select.
232 
233  @param socket_map sockets to listen for connection requests.
234  */
235  void setup_connection_events(const socket_map_t &socket_map);
236 };
237 
239 
241 
243 
244 #endif // SOCKET_CONNECTION_INCLUDED.
void setup_connection_events(const socket_map_t &socket_map)
Set up connection events for poll or select.
Definition: socket_connection.cc:855
Plain structure to collect together a host name/ip address and a corresponding network namespace if s...
Definition: socket_connection.h:89
PSI_statement_info stmt_info_new_packet
Definition: init_net_server_extension.cc:48
bool m_use_separate_thread_for_admin
Definition: socket_connection.h:120
std::string m_network_namespace
Definition: socket_connection.h:70
Mysqld_socket_listener(const std::list< Bind_address_info > &bind_addresses, uint tcp_port, const Bind_address_info &admin_bind_addr, uint admin_tcp_port, bool use_separate_thread_for_admin, uint backlog, uint port_timeout, std::string unix_sockname)
Constructor to setup a listener for listen to connect events from clients.
Definition: socket_connection.cc:806
const char * ipv6_all_addresses
Definition: socket_connection.cc:268
Bind_address_info m_admin_bind_address
Definition: socket_connection.h:117
Socket_type
Definition: socket_connection.h:63
std::string network_namespace
Definition: socket_connection.h:90
Bind_address_info()=default
void add_socket_to_listener(MYSQL_SOCKET listen_socket)
Add a socket to a set of sockets being waiting for a new connection request.
Definition: socket_connection.cc:839
MYSQL_SOCKET get_ready_socket(bool *is_unix_socket, bool *is_admin_socket) const
Get a socket ready to accept incoming connection.
Definition: socket_connection.cc:1271
ulong get_connection_errors_select()
Definition: socket_connection.cc:99
uint m_tcp_port
Definition: socket_connection.h:118
Channel_info * listen_for_connection_event()
The body of the event loop that listen for connection events from clients.
Definition: socket_connection.cc:1327
socket_map_t m_socket_map
Definition: socket_connection.h:130
ulong get_connection_errors_accept()
Definition: socket_connection.cc:101
std::list< Bind_address_info > m_bind_addresses
Definition: socket_connection.h:112
This class represents the Mysqld_socket_listener which prepare the listener sockets to recieve connec...
Definition: socket_connection.h:107
Bind_address_info(const std::string &addr)
Definition: socket_connection.h:93
std::map< MYSQL_SOCKET, Socket_attr, Socket_lt_type >::const_iterator socket_map_const_iterator_t
Definition: socket_connection.h:82
std::string m_unix_sockname
Definition: socket_connection.h:124
~Mysqld_socket_listener()
Definition: socket_connection.h:203
fd_set m_read_fds
Definition: socket_connection.h:142
select_info_t m_select_info
Definition: socket_connection.h:147
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Statement instrument information.
Definition: psi_statement_bits.h:108
static uint tcp_port
Definition: mysqladmin.cc:67
unsigned int uint
Definition: uca-dump.cc:29
Performance schema instrumentation interface.
Definition: socket_connection.h:65
int my_socket
Definition: my_io_bits.h:59
Socket_attr(Socket_type socket_type, const std::string &network_namespace)
Definition: socket_connection.h:67
const char * MY_BIND_ALL_ADDRESSES
MY_BIND_ALL_ADDRESSES defines a special value for the bind-address option, which means that the serve...
Definition: socket_connection.cc:264
An instrumented socket.
Definition: mysql_socket_bits.h:34
my_socket m_max_used_connection
Definition: socket_connection.h:143
std::string address
Definition: socket_connection.h:90
const char * ipv4_all_addresses
Definition: socket_connection.cc:266
void close_listener()
Close the listener.
Definition: socket_connection.cc:1419
select_info_t()
Definition: socket_connection.h:144
bool setup_listener()
Set up a listener - set of sockets to listen for connection events from clients.
Definition: socket_connection.cc:1207
ulong get_connection_errors_tcpwrap()
Definition: socket_connection.cc:103
uint m_port_timeout
Definition: socket_connection.h:123
Key Comparator for socket_map_t used in Mysqld_socket_listener.
Definition: socket_connection.h:56
Socket_attr(Socket_type socket_type)
Definition: socket_connection.h:66
bool m_unlink_sockname
Definition: socket_connection.h:125
MYSQL_SOCKET m_admin_interface_listen_socket
Definition: socket_connection.h:131
Definition: socket_connection.h:141
Bind_address_info(const std::string &addr, const std::string &nspace)
Definition: socket_connection.h:95
This abstract base class represents connection channel information about a new connection.
Definition: channel_info.h:46
static my_socket mysql_socket_getfd(MYSQL_SOCKET mysql_socket)
MYSQL_SOCKET helper.
Definition: mysql_socket.h:128
Socket_type m_socket_type
Definition: socket_connection.h:69
fd_set m_client_fds
Definition: socket_connection.h:142
uint m_backlog
Definition: socket_connection.h:122
bool operator()(const MYSQL_SOCKET &s1, const MYSQL_SOCKET &s2) const
Definition: socket_connection.h:57
uint m_admin_tcp_port
Definition: socket_connection.h:119
unsigned long ulong
Definition: my_inttypes.h:46
std::map< MYSQL_SOCKET, Socket_attr, Socket_lt_type > socket_map_t
Typedef representing socket map type which hold the sockets and a corresponding bool which is true if...
Definition: socket_connection.h:78