1 /*  Copyright (c) 2015, 2020, 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 #ifndef MYSQL_SRV_SESSION_SERVICE_INCLUDED
23 #define MYSQL_SRV_SESSION_SERVICE_INCLUDED
24 
25 /**
26   @file include/mysql/service_srv_session.h
27   Header file for the Server session service. This service is to provide
28   of creating sessions with the server. These sessions can be furtherly used
29   together with the Command service to execute commands in the server.
30 
31   @note Session should not be used after being closed, unless MYSQL_SESSION
32         handle is set to NULL.
33 */
34 
35 #include "mysql/service_srv_session_bits.h" /* MYSQL_SESSION, srv_session_error_cb */
36 
37 #ifndef MYSQL_ABI_CHECK
38 #include "mysql/plugin.h" /* MYSQL_THD */
39 #endif
40 
41 extern "C" struct srv_session_service_st {
42   int (*init_session_thread)(const void *plugin);
43 
44   void (*deinit_session_thread)();
45 
46   MYSQL_SESSION(*open_session)
47   (srv_session_error_cb error_cb, void *plugix_ctx);
48 
49   int (*detach_session)(MYSQL_SESSION session);
50 
51   int (*close_session)(MYSQL_SESSION session);
52 
53   int (*server_is_available)();
54 
55   int (*attach_session)(MYSQL_SESSION session, MYSQL_THD *ret_previous_thd);
56 } * srv_session_service;
57 
58 #ifdef MYSQL_DYNAMIC_PLUGIN
59 
60 #define srv_session_init_thread(plugin) \
61   srv_session_service->init_session_thread((plugin))
62 
63 #define srv_session_deinit_thread() srv_session_service->deinit_session_thread()
64 
65 #define srv_session_open(cb, ctx) srv_session_service->open_session((cb), (ctx))
66 
67 #define srv_session_detach(session) \
68   srv_session_service->detach_session((session))
69 
70 #define srv_session_close(session) srv_session_service->close_session((session))
71 
72 #define srv_session_server_is_available() \
73   srv_session_service->server_is_available()
74 
75 #define srv_session_attach(session, thd) \
76   srv_session_service->attach_session((session), (thd))
77 
78 #else
79 
80 /**
81   Initializes the current physical thread to use with session service.
82 
83   Call this function ONLY in physical threads which are not initialized in
84   any way by the server.
85 
86   @param plugin  Pointer to the plugin structure, passed to the plugin over
87                  the plugin init function.
88 
89   @return
90     0  success
91     1  failure
92 */
93 int srv_session_init_thread(const void *plugin);
94 
95 /**
96   Deinitializes the current physical thread to use with session service.
97 
98 
99   Call this function ONLY in physical threads which were initialized using
100   srv_session_init_thread().
101 */
102 void srv_session_deinit_thread();
103 
104 /**
105   Opens a server session.
106 
107   In a thread not initialized by the server itself, this function should be
108   called only after srv_session_init_thread() has already been called.
109 
110   @param error_cb    session error callback
111   @param plugin_ctx  Plugin's context, opaque pointer that would
112                      be provided to callbacks. Might be NULL.
113   @return
114     session   on success
115     NULL      on failure
116 */
117 MYSQL_SESSION srv_session_open(srv_session_error_cb error_cb, void *plugin_ctx);
118 
119 /**
120   Detaches a session from current physical thread.
121 
122   Detaches a previously session. Which can only occur when the MYSQL_SESSION
123   was manually attached by "srv_session_attach".
124   Other srv_session calls automatically attached/detached the THD when the
125   MYSQL_SESSION is used (for example command_service_run_command()).
126 
127   @param session  Session to detach
128 
129   @returns
130     0  success
131     1  failure
132 */
133 int srv_session_detach(MYSQL_SESSION session);
134 
135 /**
136   Closes a previously opened session.
137 
138   @param session  Session to close
139 
140   @note This method close the session but session handle is not set to NULL.
141         Session handle should be set to NULL explicitly after calling this
142         method. Session should not be used otherwise.
143 
144   @return
145     0  success
146     1  failure
147 */
148 int srv_session_close(MYSQL_SESSION session);
149 
150 /**
151   Returns if the server is available (not booting or shutting down)
152 
153   @return
154     0  not available
155     1  available
156 */
157 int srv_session_server_is_available();
158 
159 /**
160   Attaches a session to current physical thread.
161 
162   Previously attached THD is detached and returned through ret_previous_thd.
163   THD associated with session is attached.
164 
165   @param session  Session to attach
166 
167   @returns
168     0  success
169     1  failure
170 */
171 int srv_session_attach(MYSQL_SESSION session, MYSQL_THD *ret_previous_thd);
172 
173 #endif
174 
175 #endif /* MYSQL_SRV_SESSION_SERVICE_INCLUDED */
176