1 /* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1335  USA */
15 
16 #ifndef SQL_CONNECT_INCLUDED
17 #define SQL_CONNECT_INCLUDED
18 
19 #include <my_sys.h>                          /* pthread_handler_t */
20 #include "mysql_com.h"                         /* enum_server_command */
21 #include "structs.h"
22 #include <mysql/psi/mysql_socket.h>
23 #include <hash.h>
24 #include "violite.h"
25 
26 /*
27   Object to hold connect information to be given to the newly created thread
28 */
29 
30 struct scheduler_functions;
31 
32 class CONNECT : public ilink {
33 public:
34   MYSQL_SOCKET sock;
35 #ifdef _WIN32
36   HANDLE pipe;
CONNECT(HANDLE pipe_arg)37   CONNECT(HANDLE pipe_arg): pipe(pipe_arg), vio_type(VIO_TYPE_NAMEDPIPE),
38     scheduler(thread_scheduler), thread_id(0), prior_thr_create_utime(0)
39   {
40     count++;
41   }
42 #endif
43   enum enum_vio_type vio_type;
44   scheduler_functions *scheduler;
45   my_thread_id thread_id;
46 
47   /* Own variables */
48   ulonglong    prior_thr_create_utime;
49 
50   static Atomic_counter<uint32_t> count;
51 
CONNECT(MYSQL_SOCKET sock_arg,enum enum_vio_type vio_type_arg,scheduler_functions * scheduler_arg)52   CONNECT(MYSQL_SOCKET sock_arg, enum enum_vio_type vio_type_arg,
53           scheduler_functions *scheduler_arg): sock(sock_arg),
54     vio_type(vio_type_arg), scheduler(scheduler_arg), thread_id(0),
55     prior_thr_create_utime(0)
56   {
57     count++;
58   }
~CONNECT()59   ~CONNECT()
60   {
61     count--;
62     DBUG_ASSERT(vio_type == VIO_CLOSED);
63   }
64   void close_and_delete();
65   void close_with_error(uint sql_errno,
66                         const char *message, uint close_error);
67   THD *create_thd(THD *thd);
68 };
69 
70 
71 class THD;
72 typedef struct user_conn USER_CONN;
73 
74 void init_max_user_conn(void);
75 void init_global_user_stats(void);
76 void init_global_table_stats(void);
77 void init_global_index_stats(void);
78 void init_global_client_stats(void);
79 void free_max_user_conn(void);
80 void free_global_user_stats(void);
81 void free_global_table_stats(void);
82 void free_global_index_stats(void);
83 void free_global_client_stats(void);
84 
85 pthread_handler_t handle_one_connection(void *arg);
86 void do_handle_one_connection(CONNECT *connect, bool put_in_cache);
87 bool init_new_connection_handler_thread();
88 void reset_mqh(LEX_USER *lu, bool get_them);
89 bool check_mqh(THD *thd, uint check_command);
90 void time_out_user_resource_limits(THD *thd, USER_CONN *uc);
91 #ifndef NO_EMBEDDED_ACCESS_CHECKS
92 void decrease_user_connections(USER_CONN *uc);
93 #else
94 #define decrease_user_connections(X) do { } while(0)       /* nothing */
95 #endif
96 bool thd_init_client_charset(THD *thd, uint cs_number);
97 void setup_connection_thread_globals(THD *thd);
98 bool thd_prepare_connection(THD *thd);
99 bool thd_is_connection_alive(THD *thd);
100 int thd_set_peer_addr(THD *thd, sockaddr_storage *addr,
101                       const char *ip, uint port,
102                       bool check_proxy_networks,
103                       uint *host_errors);
104 
105 bool login_connection(THD *thd);
106 void prepare_new_connection_state(THD* thd);
107 void end_connection(THD *thd);
108 void update_global_user_stats(THD* thd, bool create_user, time_t now);
109 int get_or_create_user_conn(THD *thd, const char *user,
110                             const char *host, const USER_RESOURCES *mqh);
111 int check_for_max_user_connections(THD *thd, USER_CONN *uc);
112 
113 extern HASH global_user_stats;
114 extern HASH global_client_stats;
115 extern HASH global_table_stats;
116 extern HASH global_index_stats;
117 
118 extern mysql_mutex_t LOCK_global_user_client_stats;
119 extern mysql_mutex_t LOCK_global_table_stats;
120 extern mysql_mutex_t LOCK_global_index_stats;
121 extern mysql_mutex_t LOCK_stats;
122 
123 #endif /* SQL_CONNECT_INCLUDED */
124