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 #include <stddef.h>
26 
27 #include "mysql/psi/mysql_socket.h"
28 #include "mysql/psi/mysql_thread.h"
29 #include "mysql_com.h"
30 #include "mysqld_error.h"                   // ER_*
31 #include "sql/conn_handler/channel_info.h"  // Channel_info
32 #include "sql/conn_handler/connection_handler_impl.h"
33 #include "sql/conn_handler/connection_handler_manager.h"  // Connection_handler_manager
34 #include "sql/mysqld.h"              // connection_errors_internal
35 #include "sql/mysqld_thd_manager.h"  // Global_THD_manager
36 #include "sql/protocol_classic.h"
37 #include "sql/sql_class.h"             // THD
38 #include "sql/sql_connect.h"           // close_connection
39 #include "sql/sql_parse.h"             // do_command
40 #include "sql/sql_thd_internal_api.h"  // thd_set_thread_stack
41 
add_connection(Channel_info * channel_info)42 bool One_thread_connection_handler::add_connection(Channel_info *channel_info) {
43   if (my_thread_init()) {
44     connection_errors_internal++;
45     channel_info->send_error_and_close_channel(ER_OUT_OF_RESOURCES, 0, false);
46     Connection_handler_manager::dec_connection_count();
47     return true;
48   }
49 
50   THD *thd = channel_info->create_thd();
51   if (thd == nullptr) {
52     connection_errors_internal++;
53     channel_info->send_error_and_close_channel(ER_OUT_OF_RESOURCES, 0, false);
54     Connection_handler_manager::dec_connection_count();
55     return true;
56   }
57 
58   thd->set_new_thread_id();
59 
60   /*
61     handle_one_connection() is normally the only way a thread would
62     start and would always be on the very high end of the stack ,
63     therefore, the thread stack always starts at the address of the
64     first local variable of handle_one_connection, which is thd. We
65     need to know the start of the stack so that we could check for
66     stack overruns.
67   */
68   thd_set_thread_stack(thd, (char *)&thd);
69   thd->store_globals();
70 
71   mysql_thread_set_psi_id(thd->thread_id());
72   mysql_socket_set_thread_owner(
73       thd->get_protocol_classic()->get_vio()->mysql_socket);
74 
75   Global_THD_manager *thd_manager = Global_THD_manager::get_instance();
76   thd_manager->add_thd(thd);
77 
78   bool error = false;
79   if (thd_prepare_connection(thd))
80     error = true;  // Returning true causes inc_aborted_connects() to be called.
81   else {
82     delete channel_info;
83     while (thd_connection_alive(thd)) {
84       if (do_command(thd)) break;
85     }
86     end_connection(thd);
87   }
88   close_connection(thd, 0, false, false);
89   thd->release_resources();
90   thd_manager->remove_thd(thd);
91   Connection_handler_manager::dec_connection_count();
92   delete thd;
93   return error;
94 }
95