1 /*  Copyright (c) 2015, 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, version 2.0,
5     as published by the Free Software Foundation.
6 
7     This program is also distributed with certain software (including
8     but not limited to OpenSSL) that is licensed under separate terms,
9     as designated in a particular file or component or in included license
10     documentation.  The authors of MySQL hereby grant you an additional
11     permission to link the program and your derivative works with the
12     separately licensed software that they have included with MySQL.
13 
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License, version 2.0, for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #include "mysql/service_srv_session_info.h"
24 #include "srv_session.h"
25 
26 /**
27   @file
28   Implementation of setters and getters of some properties of a session.
29 */
30 
31 #define VALID_SESSION(s) ((s) && Srv_session::is_valid((s)))
32 
33 /**
34   Returns the THD of a session.
35 
36   @param session  Session
37 */
38 extern "C"
srv_session_info_get_thd(Srv_session * session)39 THD* srv_session_info_get_thd(Srv_session *session)
40 {
41   return VALID_SESSION(session)? session->get_thd() : NULL;
42 }
43 
44 
45 /**
46   Returns the ID of a session.
47 
48   The value returned from THD::thread_id()
49 
50   @param session  Session
51 */
52 extern "C"
srv_session_info_get_session_id(Srv_session * session)53 my_thread_id srv_session_info_get_session_id(Srv_session *session)
54 {
55   return VALID_SESSION(session)? session->get_session_id() : 0;
56 }
57 
58 
59 /**
60   Returns the client port of a session.
61 
62   @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
63   This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
64 
65   @param session  Session
66 */
67 extern "C"
srv_session_info_get_client_port(Srv_session * session)68 uint16_t srv_session_info_get_client_port(Srv_session *session)
69 {
70   return VALID_SESSION(session)? session->get_client_port() : 0;
71 }
72 
73 
74 /**
75   Sets the client port of a session.
76 
77   @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
78   This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
79 
80   @param session  Session
81   @param port     Port number
82 
83   @return
84     0 success
85     1 failure
86 */
87 extern "C"
srv_session_info_set_client_port(Srv_session * session,uint16_t port)88 int srv_session_info_set_client_port(Srv_session *session, uint16_t port)
89 {
90   return VALID_SESSION(session)? session->set_client_port(port),0 : 1;
91 }
92 
93 
94 /**
95   Returns the current database of a session.
96 
97   @param session  Session
98 */
99 extern "C"
srv_session_info_get_current_db(Srv_session * session)100 LEX_CSTRING srv_session_info_get_current_db(Srv_session *session)
101 {
102   static LEX_CSTRING empty= { NULL, 0 };
103   return VALID_SESSION(session)? session->get_current_database() : empty;
104 }
105 
106 
107 /**
108   Sets the connection type of a session.
109 
110   @see enum_vio_type
111 
112   @note If NO_VIO_TYPE passed as type the call will fail.
113 
114   @return
115     0  success
116     1  failure
117 */
118 extern "C"
srv_session_info_set_connection_type(Srv_session * session,enum_vio_type type)119 int srv_session_info_set_connection_type(Srv_session *session,
120                                          enum_vio_type type)
121 {
122   return VALID_SESSION(session)? session->set_connection_type(type) : 1;
123 }
124 
125 
126 /**
127   Returns whether the session was killed
128 
129   @param session  Session
130 
131   @return
132     0  not killed
133     1  killed
134 */
135 extern "C"
srv_session_info_killed(Srv_session * session)136 int srv_session_info_killed(Srv_session *session)
137 {
138   return (!VALID_SESSION(session) || session->get_thd()->killed)? 1:0;
139 }
140 
141 /**
142   Returns the number opened sessions in thread initialized by srv_session
143   service.
144 */
srv_session_info_session_count()145 unsigned int srv_session_info_session_count()
146 {
147   return Srv_session::session_count();
148 }
149 
150 
151 /**
152   Returns the number opened sessions in thread initialized by srv_session
153   service.
154 
155   @param plugin Pointer to the plugin structure, passed to the plugin over
156                 the plugin init function.
157 */
srv_session_info_thread_count(const void * plugin)158 unsigned int srv_session_info_thread_count(const void *plugin)
159 {
160   return Srv_session::thread_count(plugin);
161 }
162