1 /* Copyright (c) 2005, 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 
23 #ifndef _my_plugin_h
24 #define _my_plugin_h
25 
26 /**
27   @file include/mysql/plugin.h
28 */
29 
30 #ifndef MYSQL_ABI_CHECK
31 #include <stddef.h>
32 
33 #include "mysql_version.h" /* MYSQL_VERSION_ID */
34 #ifdef __cplusplus
35 #include "sql/sql_plugin.h"  // plugin_thdvar_safe_update
36 #endif
37 #endif
38 
39 #include "status_var.h"
40 
41 /*
42   On Windows, exports from DLL need to be declared.
43   Also, plugin needs to be declared as extern "C" because MSVC
44   unlike other compilers, uses C++ mangling for variables not only
45   for functions.
46 */
47 #if defined(_MSC_VER)
48 #if defined(MYSQL_DYNAMIC_PLUGIN)
49 #ifdef __cplusplus
50 #define MYSQL_PLUGIN_EXPORT extern "C" __declspec(dllexport)
51 #else
52 #define MYSQL_PLUGIN_EXPORT __declspec(dllexport)
53 #endif
54 #else /* MYSQL_DYNAMIC_PLUGIN */
55 #ifdef __cplusplus
56 #define MYSQL_PLUGIN_EXPORT extern "C"
57 #else
58 #define MYSQL_PLUGIN_EXPORT
59 #endif
60 #endif /*MYSQL_DYNAMIC_PLUGIN */
61 #else  /*_MSC_VER */
62 #define MYSQL_PLUGIN_EXPORT
63 #endif
64 
65 #ifdef __cplusplus
66 class THD;
67 class Item;
68 #define MYSQL_THD THD *
69 #else
70 #define MYSQL_THD void *
71 #endif
72 
73 typedef void *MYSQL_PLUGIN;
74 
75 #ifndef MYSQL_ABI_CHECK
76 #include <mysql/services.h>
77 #endif
78 
79 #define MYSQL_XIDDATASIZE 128
80 /**
81   MYSQL_XID is binary compatible with the XID structure as
82   in the X/Open CAE Specification, Distributed Transaction Processing:
83   The XA Specification, X/Open Company Ltd., 1991.
84   http://www.opengroup.org/bookstore/catalog/c193.htm
85 
86   @see XID in sql/handler.h
87 */
88 struct MYSQL_XID {
89   long formatID;
90   long gtrid_length;
91   long bqual_length;
92   char data[MYSQL_XIDDATASIZE]; /* Not \0-terminated */
93 };
94 
95 /*************************************************************************
96   Plugin API. Common for all plugin types.
97 */
98 
99 #define MYSQL_PLUGIN_INTERFACE_VERSION 0x010A
100 
101 /*
102   The allowable types of plugins
103 */
104 #define MYSQL_UDF_PLUGIN 0                /* User-defined function        */
105 #define MYSQL_STORAGE_ENGINE_PLUGIN 1     /* Storage Engine               */
106 #define MYSQL_FTPARSER_PLUGIN 2           /* Full-text parser plugin      */
107 #define MYSQL_DAEMON_PLUGIN 3             /* The daemon/raw plugin type */
108 #define MYSQL_INFORMATION_SCHEMA_PLUGIN 4 /* The I_S plugin type */
109 #define MYSQL_AUDIT_PLUGIN 5              /* The Audit plugin type        */
110 #define MYSQL_REPLICATION_PLUGIN 6        /* The replication plugin type */
111 #define MYSQL_AUTHENTICATION_PLUGIN 7     /* The authentication plugin type */
112 #define MYSQL_VALIDATE_PASSWORD_PLUGIN 8  /* validate password plugin type */
113 #define MYSQL_GROUP_REPLICATION_PLUGIN 9  /* The Group Replication plugin */
114 #define MYSQL_KEYRING_PLUGIN 10           /* The Keyring plugin type   */
115 #define MYSQL_CLONE_PLUGIN 11             /* The Clone plugin type   */
116 #define MYSQL_MAX_PLUGIN_TYPE_NUM 12      /* The number of plugin types   */
117 
118 /* We use the following strings to define licenses for plugins */
119 #define PLUGIN_LICENSE_PROPRIETARY 0
120 #define PLUGIN_LICENSE_GPL 1
121 #define PLUGIN_LICENSE_BSD 2
122 
123 #define PLUGIN_LICENSE_PROPRIETARY_STRING "PROPRIETARY"
124 #define PLUGIN_LICENSE_GPL_STRING "GPL"
125 #define PLUGIN_LICENSE_BSD_STRING "BSD"
126 
127 #define PLUGIN_AUTHOR_ORACLE "Oracle Corporation"
128 
129 /*
130   Macros for beginning and ending plugin declarations.  Between
131   mysql_declare_plugin and mysql_declare_plugin_end there should
132   be a st_mysql_plugin struct for each plugin to be declared.
133 */
134 
135 #ifndef MYSQL_DYNAMIC_PLUGIN
136 #define __MYSQL_DECLARE_PLUGIN(NAME, VERSION, PSIZE, DECLS)         \
137   MYSQL_PLUGIN_EXPORT int VERSION = MYSQL_PLUGIN_INTERFACE_VERSION; \
138   MYSQL_PLUGIN_EXPORT int PSIZE = sizeof(struct st_mysql_plugin);   \
139   MYSQL_PLUGIN_EXPORT struct st_mysql_plugin DECLS[] = {
140 #else
141 #define __MYSQL_DECLARE_PLUGIN(NAME, VERSION, PSIZE, DECLS)  \
142   MYSQL_PLUGIN_EXPORT int _mysql_plugin_interface_version_ = \
143       MYSQL_PLUGIN_INTERFACE_VERSION;                        \
144   MYSQL_PLUGIN_EXPORT int _mysql_sizeof_struct_st_plugin_ =  \
145       sizeof(struct st_mysql_plugin);                        \
146   MYSQL_PLUGIN_EXPORT struct st_mysql_plugin _mysql_plugin_declarations_[] = {
147 #endif
148 
149 #define mysql_declare_plugin(NAME)                                        \
150   __MYSQL_DECLARE_PLUGIN(NAME, builtin_##NAME##_plugin_interface_version, \
151                          builtin_##NAME##_sizeof_struct_st_plugin,        \
152                          builtin_##NAME##_plugin)
153 
154 #define mysql_declare_plugin_end                 \
155   , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } \
156   }
157 
158 /*
159   Constants for plugin flags.
160  */
161 
162 #define PLUGIN_OPT_NO_INSTALL 1UL   /* Not dynamically loadable */
163 #define PLUGIN_OPT_NO_UNINSTALL 2UL /* Not dynamically unloadable */
164 #define PLUGIN_OPT_ALLOW_EARLY 4UL  /* allow --early-plugin-load */
165 
166 /*
167   declarations for server variables and command line options
168 */
169 
170 #define PLUGIN_VAR_BOOL 0x0001
171 #define PLUGIN_VAR_INT 0x0002
172 #define PLUGIN_VAR_LONG 0x0003
173 #define PLUGIN_VAR_LONGLONG 0x0004
174 #define PLUGIN_VAR_STR 0x0005
175 #define PLUGIN_VAR_ENUM 0x0006
176 #define PLUGIN_VAR_SET 0x0007
177 #define PLUGIN_VAR_DOUBLE 0x0008
178 #define PLUGIN_VAR_UNSIGNED 0x0080
179 #define PLUGIN_VAR_THDLOCAL 0x0100 /* Variable is per-connection */
180 #define PLUGIN_VAR_READONLY 0x0200 /* Server variable is read only */
181 #define PLUGIN_VAR_NOSYSVAR 0x0400 /* Configurable only by cmd-line */
182 
183 /**
184   plugin variable CAN'T be used through command line at all
185   neither "--option", nor "--option=value" will work
186   @note you should probably set a default variable value if you use this flag
187 */
188 #define PLUGIN_VAR_NOCMDOPT 0x0800
189 
190 /**
191   plugin variable *value* CAN'T be set via command line
192   you can invoke it with "--option" only, but "--option=value" will not work
193   @note you should probably set a default variable value if you use this flag
194 */
195 #define PLUGIN_VAR_NOCMDARG 0x1000
196 
197 /**
198   plugin variable CAN'T be used through command line without a value
199   "--option=value" must be used, only "--option" won't work
200 */
201 #define PLUGIN_VAR_RQCMDARG 0x0000
202 
203 /**
204   plugin variable can be set via command line, both with or without value
205   either "--option=value", or "--option" will work
206   @note you should probably set a default variable value if you use this flag
207 */
208 #define PLUGIN_VAR_OPCMDARG 0x2000
209 #define PLUGIN_VAR_NODEFAULT 0x4000 /* SET DEFAULT is prohibited */
210 #define PLUGIN_VAR_MEMALLOC 0x8000  /* String needs memory allocated */
211 #define PLUGIN_VAR_NOPERSIST                \
212   0x10000 /* SET PERSIST_ONLY is prohibited \
213              for read only variables */
214 
215 /**
216   There can be some variables which needs to be set before plugin is loaded but
217   not after plugin is loaded. ex: GR specific variables. Below flag must be set
218   for these kind of variables.
219 */
220 #define PLUGIN_VAR_PERSIST_AS_READ_ONLY 0x20000
221 #define PLUGIN_VAR_INVISIBLE 0x40000 /* Variable should not be shown */
222 
223 struct SYS_VAR;
224 struct st_mysql_value;
225 
226 /*
227   SYNOPSIS
228     (*mysql_var_check_func)()
229       thd               thread handle
230       var               dynamic variable being altered
231       save              pointer to temporary storage
232       value             user provided value
233   RETURN
234     0   user provided value is OK and the update func may be called.
235     any other value indicates error.
236 
237   This function should parse the user provided value and store in the
238   provided temporary storage any data as required by the update func.
239   There is sufficient space in the temporary storage to store a double.
240   Note that the update func may not be called if any other error occurs
241   so any memory allocated should be thread-local so that it may be freed
242   automatically at the end of the statement.
243 */
244 
245 typedef int (*mysql_var_check_func)(MYSQL_THD thd, SYS_VAR *var, void *save,
246                                     struct st_mysql_value *value);
247 
248 /*
249   SYNOPSIS
250     (*mysql_var_update_func)()
251       thd               thread handle
252       var               dynamic variable being altered
253       var_ptr           pointer to dynamic variable
254       save              pointer to temporary storage
255    RETURN
256      NONE
257 
258    This function should use the validated value stored in the temporary store
259    and persist it in the provided pointer to the dynamic variable.
260    For example, strings may require memory to be allocated.
261 */
262 typedef void (*mysql_var_update_func)(MYSQL_THD thd, SYS_VAR *var,
263                                       void *var_ptr, const void *save);
264 
265 /* the following declarations are for internal use only */
266 
267 #define PLUGIN_VAR_MASK                                                \
268   (PLUGIN_VAR_READONLY | PLUGIN_VAR_NOSYSVAR | PLUGIN_VAR_NOCMDOPT |   \
269    PLUGIN_VAR_NOCMDARG | PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_RQCMDARG |   \
270    PLUGIN_VAR_MEMALLOC | PLUGIN_VAR_NODEFAULT | PLUGIN_VAR_NOPERSIST | \
271    PLUGIN_VAR_PERSIST_AS_READ_ONLY | PLUGIN_VAR_INVISIBLE)
272 
273 #define MYSQL_PLUGIN_VAR_HEADER \
274   int flags;                    \
275   const char *name;             \
276   const char *comment;          \
277   mysql_var_check_func check;   \
278   mysql_var_update_func update
279 
280 #define MYSQL_SYSVAR_NAME(name) mysql_sysvar_##name
281 #define MYSQL_SYSVAR(name) ((SYS_VAR *)&(MYSQL_SYSVAR_NAME(name)))
282 
283 /*
284   for global variables, the value pointer is the first
285   element after the header, the default value is the second.
286   for thread variables, the value offset is the first
287   element after the header, the default value is the second.
288 */
289 
290 #define DECLARE_MYSQL_SYSVAR_BASIC(name, type) \
291   struct {                                     \
292     MYSQL_PLUGIN_VAR_HEADER;                   \
293     type *value;                               \
294     const type def_val;                        \
295   } MYSQL_SYSVAR_NAME(name)
296 
297 #define DECLARE_MYSQL_SYSVAR_SIMPLE(name, type) \
298   struct {                                      \
299     MYSQL_PLUGIN_VAR_HEADER;                    \
300     type *value;                                \
301     type def_val;                               \
302     type min_val;                               \
303     type max_val;                               \
304     type blk_sz;                                \
305   } MYSQL_SYSVAR_NAME(name)
306 
307 #define DECLARE_MYSQL_SYSVAR_TYPELIB(name, type) \
308   struct {                                       \
309     MYSQL_PLUGIN_VAR_HEADER;                     \
310     type *value;                                 \
311     type def_val;                                \
312     TYPELIB *typelib;                            \
313   } MYSQL_SYSVAR_NAME(name)
314 
315 #define DECLARE_THDVAR_FUNC(type) type *(*resolve)(MYSQL_THD thd, int offset)
316 
317 #define DECLARE_MYSQL_THDVAR_BASIC(name, type) \
318   struct {                                     \
319     MYSQL_PLUGIN_VAR_HEADER;                   \
320     int offset;                                \
321     const type def_val;                        \
322     DECLARE_THDVAR_FUNC(type);                 \
323   } MYSQL_SYSVAR_NAME(name)
324 
325 #define DECLARE_MYSQL_THDVAR_SIMPLE(name, type) \
326   struct {                                      \
327     MYSQL_PLUGIN_VAR_HEADER;                    \
328     int offset;                                 \
329     type def_val;                               \
330     type min_val;                               \
331     type max_val;                               \
332     type blk_sz;                                \
333     DECLARE_THDVAR_FUNC(type);                  \
334   } MYSQL_SYSVAR_NAME(name)
335 
336 #define DECLARE_MYSQL_THDVAR_TYPELIB(name, type) \
337   struct {                                       \
338     MYSQL_PLUGIN_VAR_HEADER;                     \
339     int offset;                                  \
340     type def_val;                                \
341     DECLARE_THDVAR_FUNC(type);                   \
342     TYPELIB *typelib;                            \
343   } MYSQL_SYSVAR_NAME(name)
344 
345 /*
346   the following declarations are for use by plugin implementors
347 */
348 
349 #define MYSQL_SYSVAR_BOOL(name, varname, opt, comment, check, update, def) \
350   DECLARE_MYSQL_SYSVAR_BASIC(name, bool) = {                               \
351       PLUGIN_VAR_BOOL | ((opt)&PLUGIN_VAR_MASK),                           \
352       #name,                                                               \
353       comment,                                                             \
354       check,                                                               \
355       update,                                                              \
356       &varname,                                                            \
357       def}
358 
359 #define MYSQL_SYSVAR_STR(name, varname, opt, comment, check, update, def) \
360   DECLARE_MYSQL_SYSVAR_BASIC(name, char *) = {                            \
361       PLUGIN_VAR_STR | ((opt)&PLUGIN_VAR_MASK),                           \
362       #name,                                                              \
363       comment,                                                            \
364       check,                                                              \
365       update,                                                             \
366       &varname,                                                           \
367       def}
368 
369 #define MYSQL_SYSVAR_INT(name, varname, opt, comment, check, update, def, min, \
370                          max, blk)                                             \
371   DECLARE_MYSQL_SYSVAR_SIMPLE(name, int) = {                                   \
372       PLUGIN_VAR_INT | ((opt)&PLUGIN_VAR_MASK),                                \
373       #name,                                                                   \
374       comment,                                                                 \
375       check,                                                                   \
376       update,                                                                  \
377       &varname,                                                                \
378       def,                                                                     \
379       min,                                                                     \
380       max,                                                                     \
381       blk}
382 
383 #define MYSQL_SYSVAR_UINT(name, varname, opt, comment, check, update, def, \
384                           min, max, blk)                                   \
385   DECLARE_MYSQL_SYSVAR_SIMPLE(name, unsigned int) = {                      \
386       PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED | ((opt)&PLUGIN_VAR_MASK),      \
387       #name,                                                               \
388       comment,                                                             \
389       check,                                                               \
390       update,                                                              \
391       &varname,                                                            \
392       def,                                                                 \
393       min,                                                                 \
394       max,                                                                 \
395       blk}
396 
397 #define MYSQL_SYSVAR_LONG(name, varname, opt, comment, check, update, def, \
398                           min, max, blk)                                   \
399   DECLARE_MYSQL_SYSVAR_SIMPLE(name, long) = {                              \
400       PLUGIN_VAR_LONG | ((opt)&PLUGIN_VAR_MASK),                           \
401       #name,                                                               \
402       comment,                                                             \
403       check,                                                               \
404       update,                                                              \
405       &varname,                                                            \
406       def,                                                                 \
407       min,                                                                 \
408       max,                                                                 \
409       blk}
410 
411 #define MYSQL_SYSVAR_ULONG(name, varname, opt, comment, check, update, def, \
412                            min, max, blk)                                   \
413   DECLARE_MYSQL_SYSVAR_SIMPLE(name, unsigned long) = {                      \
414       PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED | ((opt)&PLUGIN_VAR_MASK),      \
415       #name,                                                                \
416       comment,                                                              \
417       check,                                                                \
418       update,                                                               \
419       &varname,                                                             \
420       def,                                                                  \
421       min,                                                                  \
422       max,                                                                  \
423       blk}
424 
425 #define MYSQL_SYSVAR_LONGLONG(name, varname, opt, comment, check, update, def, \
426                               min, max, blk)                                   \
427   DECLARE_MYSQL_SYSVAR_SIMPLE(name, long long) = {                             \
428       PLUGIN_VAR_LONGLONG | ((opt)&PLUGIN_VAR_MASK),                           \
429       #name,                                                                   \
430       comment,                                                                 \
431       check,                                                                   \
432       update,                                                                  \
433       &varname,                                                                \
434       def,                                                                     \
435       min,                                                                     \
436       max,                                                                     \
437       blk}
438 
439 #define MYSQL_SYSVAR_ULONGLONG(name, varname, opt, comment, check, update, \
440                                def, min, max, blk)                         \
441   DECLARE_MYSQL_SYSVAR_SIMPLE(name, unsigned long long) = {                \
442       PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED | ((opt)&PLUGIN_VAR_MASK), \
443       #name,                                                               \
444       comment,                                                             \
445       check,                                                               \
446       update,                                                              \
447       &varname,                                                            \
448       def,                                                                 \
449       min,                                                                 \
450       max,                                                                 \
451       blk}
452 
453 #define MYSQL_SYSVAR_ENUM(name, varname, opt, comment, check, update, def, \
454                           typelib)                                         \
455   DECLARE_MYSQL_SYSVAR_TYPELIB(name, unsigned long) = {                    \
456       PLUGIN_VAR_ENUM | ((opt)&PLUGIN_VAR_MASK),                           \
457       #name,                                                               \
458       comment,                                                             \
459       check,                                                               \
460       update,                                                              \
461       &varname,                                                            \
462       def,                                                                 \
463       typelib}
464 
465 #define MYSQL_SYSVAR_SET(name, varname, opt, comment, check, update, def, \
466                          typelib)                                         \
467   DECLARE_MYSQL_SYSVAR_TYPELIB(name, unsigned long long) = {              \
468       PLUGIN_VAR_SET | ((opt)&PLUGIN_VAR_MASK),                           \
469       #name,                                                              \
470       comment,                                                            \
471       check,                                                              \
472       update,                                                             \
473       &varname,                                                           \
474       def,                                                                \
475       typelib}
476 
477 #define MYSQL_SYSVAR_DOUBLE(name, varname, opt, comment, check, update, def, \
478                             min, max, blk)                                   \
479   DECLARE_MYSQL_SYSVAR_SIMPLE(name, double) = {                              \
480       PLUGIN_VAR_DOUBLE | ((opt)&PLUGIN_VAR_MASK),                           \
481       #name,                                                                 \
482       comment,                                                               \
483       check,                                                                 \
484       update,                                                                \
485       &varname,                                                              \
486       def,                                                                   \
487       min,                                                                   \
488       max,                                                                   \
489       blk}
490 
491 #define MYSQL_THDVAR_BOOL(name, opt, comment, check, update, def)      \
492   DECLARE_MYSQL_THDVAR_BASIC(name, bool) = {                           \
493       PLUGIN_VAR_BOOL | PLUGIN_VAR_THDLOCAL | ((opt)&PLUGIN_VAR_MASK), \
494       #name,                                                           \
495       comment,                                                         \
496       check,                                                           \
497       update,                                                          \
498       -1,                                                              \
499       def,                                                             \
500       NULL}
501 
502 #define MYSQL_THDVAR_STR(name, opt, comment, check, update, def)      \
503   DECLARE_MYSQL_THDVAR_BASIC(name, char *) = {                        \
504       PLUGIN_VAR_STR | PLUGIN_VAR_THDLOCAL | ((opt)&PLUGIN_VAR_MASK), \
505       #name,                                                          \
506       comment,                                                        \
507       check,                                                          \
508       update,                                                         \
509       -1,                                                             \
510       def,                                                            \
511       NULL}
512 
513 #define MYSQL_THDVAR_INT(name, opt, comment, check, update, def, min, max, \
514                          blk)                                              \
515   DECLARE_MYSQL_THDVAR_SIMPLE(name, int) = {                               \
516       PLUGIN_VAR_INT | PLUGIN_VAR_THDLOCAL | ((opt)&PLUGIN_VAR_MASK),      \
517       #name,                                                               \
518       comment,                                                             \
519       check,                                                               \
520       update,                                                              \
521       -1,                                                                  \
522       def,                                                                 \
523       min,                                                                 \
524       max,                                                                 \
525       blk,                                                                 \
526       NULL}
527 
528 #define MYSQL_THDVAR_UINT(name, opt, comment, check, update, def, min, max, \
529                           blk)                                              \
530   DECLARE_MYSQL_THDVAR_SIMPLE(name, unsigned int) = {                       \
531       PLUGIN_VAR_INT | PLUGIN_VAR_THDLOCAL | PLUGIN_VAR_UNSIGNED |          \
532           ((opt)&PLUGIN_VAR_MASK),                                          \
533       #name,                                                                \
534       comment,                                                              \
535       check,                                                                \
536       update,                                                               \
537       -1,                                                                   \
538       def,                                                                  \
539       min,                                                                  \
540       max,                                                                  \
541       blk,                                                                  \
542       NULL}
543 
544 #define MYSQL_THDVAR_LONG(name, opt, comment, check, update, def, min, max, \
545                           blk)                                              \
546   DECLARE_MYSQL_THDVAR_SIMPLE(name, long) = {                               \
547       PLUGIN_VAR_LONG | PLUGIN_VAR_THDLOCAL | ((opt)&PLUGIN_VAR_MASK),      \
548       #name,                                                                \
549       comment,                                                              \
550       check,                                                                \
551       update,                                                               \
552       -1,                                                                   \
553       def,                                                                  \
554       min,                                                                  \
555       max,                                                                  \
556       blk,                                                                  \
557       NULL}
558 
559 #define MYSQL_THDVAR_ULONG(name, opt, comment, check, update, def, min, max, \
560                            blk)                                              \
561   DECLARE_MYSQL_THDVAR_SIMPLE(name, unsigned long) = {                       \
562       PLUGIN_VAR_LONG | PLUGIN_VAR_THDLOCAL | PLUGIN_VAR_UNSIGNED |          \
563           ((opt)&PLUGIN_VAR_MASK),                                           \
564       #name,                                                                 \
565       comment,                                                               \
566       check,                                                                 \
567       update,                                                                \
568       -1,                                                                    \
569       def,                                                                   \
570       min,                                                                   \
571       max,                                                                   \
572       blk,                                                                   \
573       NULL}
574 
575 #define MYSQL_THDVAR_LONGLONG(name, opt, comment, check, update, def, min, \
576                               max, blk)                                    \
577   DECLARE_MYSQL_THDVAR_SIMPLE(name, long long) = {                         \
578       PLUGIN_VAR_LONGLONG | PLUGIN_VAR_THDLOCAL | ((opt)&PLUGIN_VAR_MASK), \
579       #name,                                                               \
580       comment,                                                             \
581       check,                                                               \
582       update,                                                              \
583       -1,                                                                  \
584       def,                                                                 \
585       min,                                                                 \
586       max,                                                                 \
587       blk,                                                                 \
588       NULL}
589 
590 #define MYSQL_THDVAR_ULONGLONG(name, opt, comment, check, update, def, min, \
591                                max, blk)                                    \
592   DECLARE_MYSQL_THDVAR_SIMPLE(name, unsigned long long) = {                 \
593       PLUGIN_VAR_LONGLONG | PLUGIN_VAR_THDLOCAL | PLUGIN_VAR_UNSIGNED |     \
594           ((opt)&PLUGIN_VAR_MASK),                                          \
595       #name,                                                                \
596       comment,                                                              \
597       check,                                                                \
598       update,                                                               \
599       -1,                                                                   \
600       def,                                                                  \
601       min,                                                                  \
602       max,                                                                  \
603       blk,                                                                  \
604       NULL}
605 
606 #define MYSQL_THDVAR_ENUM(name, opt, comment, check, update, def, typelib) \
607   DECLARE_MYSQL_THDVAR_TYPELIB(name, unsigned long) = {                    \
608       PLUGIN_VAR_ENUM | PLUGIN_VAR_THDLOCAL | ((opt)&PLUGIN_VAR_MASK),     \
609       #name,                                                               \
610       comment,                                                             \
611       check,                                                               \
612       update,                                                              \
613       -1,                                                                  \
614       def,                                                                 \
615       NULL,                                                                \
616       typelib}
617 
618 #define MYSQL_THDVAR_SET(name, opt, comment, check, update, def, typelib) \
619   DECLARE_MYSQL_THDVAR_TYPELIB(name, unsigned long long) = {              \
620       PLUGIN_VAR_SET | PLUGIN_VAR_THDLOCAL | ((opt)&PLUGIN_VAR_MASK),     \
621       #name,                                                              \
622       comment,                                                            \
623       check,                                                              \
624       update,                                                             \
625       -1,                                                                 \
626       def,                                                                \
627       NULL,                                                               \
628       typelib}
629 
630 #define MYSQL_THDVAR_DOUBLE(name, opt, comment, check, update, def, min, max, \
631                             blk)                                              \
632   DECLARE_MYSQL_THDVAR_SIMPLE(name, double) = {                               \
633       PLUGIN_VAR_DOUBLE | PLUGIN_VAR_THDLOCAL | ((opt)&PLUGIN_VAR_MASK),      \
634       #name,                                                                  \
635       comment,                                                                \
636       check,                                                                  \
637       update,                                                                 \
638       -1,                                                                     \
639       def,                                                                    \
640       min,                                                                    \
641       max,                                                                    \
642       blk,                                                                    \
643       NULL}
644 
645 /* accessor macros */
646 
647 #define SYSVAR(name) (*(MYSQL_SYSVAR_NAME(name).value))
648 
649 /* when thd == null, result points to global value */
650 #define THDVAR(thd, name) \
651   (*(MYSQL_SYSVAR_NAME(name).resolve(thd, MYSQL_SYSVAR_NAME(name).offset)))
652 
653 #define THDVAR_SET(thd, name, value)                 \
654   plugin_thdvar_safe_update(thd, MYSQL_SYSVAR(name), \
655                             (char **)&THDVAR(thd, name), (const char *)value);
656 
657 /*
658   Plugin description structure.
659 */
660 
661 struct st_mysql_plugin {
662   int type;           /* the plugin type (a MYSQL_XXX_PLUGIN value)   */
663   void *info;         /* pointer to type-specific plugin descriptor   */
664   const char *name;   /* plugin name                                  */
665   const char *author; /* plugin author (for I_S.PLUGINS)              */
666   const char *descr;  /* general descriptive text (for I_S.PLUGINS)   */
667   int license;        /* the plugin license (PLUGIN_LICENSE_XXX)      */
668   /** Function to invoke when plugin is loaded. */
669   int (*init)(MYSQL_PLUGIN);
670   /** Function to invoke when plugin is uninstalled. */
671   int (*check_uninstall)(MYSQL_PLUGIN);
672   /** Function to invoke when plugin is unloaded. */
673   int (*deinit)(MYSQL_PLUGIN);
674   unsigned int version; /* plugin version (for I_S.PLUGINS)             */
675   SHOW_VAR *status_vars;
676   SYS_VAR **system_vars;
677   void *__reserved1;   /* reserved for dependency checking             */
678   unsigned long flags; /* flags for plugin */
679 };
680 
681 /*************************************************************************
682   API for Full-text parser plugin. (MYSQL_FTPARSER_PLUGIN)
683 */
684 #define MYSQL_FTPARSER_INTERFACE_VERSION 0x0101
685 
686 /*************************************************************************
687   API for Query Rewrite plugin. (MYSQL_QUERY_REWRITE_PLUGIN)
688 */
689 
690 #define MYSQL_REWRITE_PRE_PARSE_INTERFACE_VERSION 0x0010
691 #define MYSQL_REWRITE_POST_PARSE_INTERFACE_VERSION 0x0010
692 
693 /*************************************************************************
694   API for Storage Engine plugin. (MYSQL_DAEMON_PLUGIN)
695 */
696 
697 /* handlertons of different MySQL releases are incompatible */
698 #define MYSQL_DAEMON_INTERFACE_VERSION (MYSQL_VERSION_ID << 8)
699 
700 /*
701   Here we define only the descriptor structure, that is referred from
702   st_mysql_plugin.
703 */
704 
705 struct st_mysql_daemon {
706   int interface_version;
707 };
708 
709 /*************************************************************************
710   API for I_S plugin. (MYSQL_INFORMATION_SCHEMA_PLUGIN)
711 */
712 
713 /* handlertons of different MySQL releases are incompatible */
714 #define MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION (MYSQL_VERSION_ID << 8)
715 
716 /*
717   Here we define only the descriptor structure, that is referred from
718   st_mysql_plugin.
719 */
720 
721 struct st_mysql_information_schema {
722   int interface_version;
723 };
724 
725 /*************************************************************************
726   API for Storage Engine plugin. (MYSQL_STORAGE_ENGINE_PLUGIN)
727 */
728 
729 /* handlertons of different MySQL releases are incompatible */
730 #define MYSQL_HANDLERTON_INTERFACE_VERSION (MYSQL_VERSION_ID << 8)
731 
732 /*
733   The real API is in the sql/handler.h
734   Here we define only the descriptor structure, that is referred from
735   st_mysql_plugin.
736 */
737 
738 struct st_mysql_storage_engine {
739   int interface_version;
740 };
741 
742 struct handlerton;
743 
744 /*
745   API for Replication plugin. (MYSQL_REPLICATION_PLUGIN)
746 */
747 #define MYSQL_REPLICATION_INTERFACE_VERSION 0x0400
748 
749 /**
750    Replication plugin descriptor
751 */
752 struct Mysql_replication {
753   int interface_version;
754 };
755 
756 /*************************************************************************
757   st_mysql_value struct for reading values from mysqld.
758   Used by server variables framework to parse user-provided values.
759   Will be used for arguments when implementing UDFs.
760 
761   Note that val_str() returns a string in temporary memory
762   that will be freed at the end of statement. Copy the string
763   if you need it to persist.
764 */
765 
766 #define MYSQL_VALUE_TYPE_STRING 0
767 #define MYSQL_VALUE_TYPE_REAL 1
768 #define MYSQL_VALUE_TYPE_INT 2
769 
770 struct st_mysql_value {
771   int (*value_type)(struct st_mysql_value *);
772   const char *(*val_str)(struct st_mysql_value *, char *buffer, int *length);
773   int (*val_real)(struct st_mysql_value *, double *realbuf);
774   int (*val_int)(struct st_mysql_value *, long long *intbuf);
775   int (*is_unsigned)(struct st_mysql_value *);
776 };
777 
778 /*************************************************************************
779   Miscellaneous functions for plugin implementors
780 */
781 
782 #define thd_proc_info(thd, msg) \
783   set_thd_proc_info(thd, msg, __func__, __FILE__, __LINE__)
784 
785 #ifdef __cplusplus
786 extern "C" {
787 #endif
788 
789 int thd_in_lock_tables(const MYSQL_THD thd);
790 int thd_tablespace_op(const MYSQL_THD thd);
791 long long thd_test_options(const MYSQL_THD thd, long long test_options);
792 int thd_sql_command(const MYSQL_THD thd);
793 const char *set_thd_proc_info(MYSQL_THD thd, const char *info,
794                               const char *calling_func,
795                               const char *calling_file,
796                               const unsigned int calling_line);
797 void **thd_ha_data(const MYSQL_THD thd, const struct handlerton *hton);
798 void thd_storage_lock_wait(MYSQL_THD thd, long long value);
799 int thd_tx_isolation(const MYSQL_THD thd);
800 int thd_tx_is_read_only(const MYSQL_THD thd);
801 MYSQL_THD thd_tx_arbitrate(MYSQL_THD requestor, MYSQL_THD holder);
802 int thd_tx_priority(const MYSQL_THD thd);
803 int thd_tx_is_dd_trx(const MYSQL_THD thd);
804 char *thd_security_context(MYSQL_THD thd, char *buffer, size_t length,
805                            size_t max_query_len);
806 /* Increments the row counter, see THD::row_count */
807 void thd_inc_row_count(MYSQL_THD thd);
808 int thd_allow_batch(MYSQL_THD thd);
809 
810 /**
811   Mark transaction to rollback and mark error as fatal to a
812   sub-statement if in sub statement mode.
813 
814   @param thd  user thread connection handle
815   @param all  if all != 0, rollback the main transaction
816 */
817 
818 void thd_mark_transaction_to_rollback(MYSQL_THD thd, int all);
819 
820 /**
821   Create a temporary file.
822 
823   @details
824   The temporary file is created in a location specified by the mysql
825   server configuration (--tmpdir option).  The caller does not need to
826   delete the file, it will be deleted automatically.
827 
828   @param prefix  prefix for temporary file name
829   @retval -1    error
830   @retval >= 0  a file handle that can be passed to dup or my_close
831 */
832 int mysql_tmpfile(const char *prefix);
833 
834 /**
835   Check the killed state of a connection
836 
837   @details
838   In MySQL support for the KILL statement is cooperative. The KILL
839   statement only sets a "killed" flag. This function returns the value
840   of that flag.  A thread should check it often, especially inside
841   time-consuming loops, and gracefully abort the operation if it is
842   non-zero.
843 
844   @param v_thd  user thread connection handle
845   @retval 0  the connection is active
846   @retval 1  the connection has been killed
847 */
848 int thd_killed(const void *v_thd);
849 
850 /**
851   Set the killed status of the current statement.
852 
853   @param thd  user thread connection handle
854 */
855 void thd_set_kill_status(const MYSQL_THD thd);
856 
857 /**
858   Get binary log position for latest written entry.
859 
860   @note The file variable will be set to a buffer holding the name of
861   the file name currently, but this can change if a rotation
862   occur. Copy the string if you want to retain it.
863 
864   @param thd Use thread connection handle
865   @param file_var Pointer to variable that will hold the file name.
866   @param pos_var Pointer to variable that will hold the file position.
867  */
868 void thd_binlog_pos(const MYSQL_THD thd, const char **file_var,
869                     unsigned long long *pos_var);
870 
871 /**
872   Return the thread id of a user thread
873 
874   @param thd  user thread connection handle
875   @return  thread id
876 */
877 unsigned long thd_get_thread_id(const MYSQL_THD thd);
878 
879 /**
880   Get the XID for this connection's transaction
881 
882   @param thd  user thread connection handle
883   @param xid  location where identifier is stored
884 */
885 void thd_get_xid(const MYSQL_THD thd, MYSQL_XID *xid);
886 
887 /**
888   Provide a handler data getter to simplify coding
889 */
890 void *thd_get_ha_data(const MYSQL_THD thd, const struct handlerton *hton);
891 
892 /**
893   Provide a handler data setter to simplify coding
894 
895   @details
896   Set ha_data pointer (storage engine per-connection information).
897 
898   To avoid unclean deactivation (uninstall) of storage engine plugin
899   in the middle of transaction, additional storage engine plugin
900   lock is acquired.
901 
902   If ha_data is not null and storage engine plugin was not locked
903   by thd_set_ha_data() in this connection before, storage engine
904   plugin gets locked.
905 
906   If ha_data is null and storage engine plugin was locked by
907   thd_set_ha_data() in this connection before, storage engine
908   plugin lock gets released.
909 
910   If handlerton::close_connection() didn't reset ha_data, server does
911   it immediately after calling handlerton::close_connection().
912 */
913 void thd_set_ha_data(MYSQL_THD thd, const struct handlerton *hton,
914                      const void *ha_data);
915 
916 /**
917   Interface to remove the per thread openssl error queue.
918   This function is a no-op when openssl is not used.
919 */
920 
921 void remove_ssl_err_thread_state();
922 
923 /**
924   Interface to get the number of VCPUs.
925 */
926 unsigned int thd_get_num_vcpus();
927 #ifdef __cplusplus
928 }
929 #endif
930 
931 #endif /* _my_plugin_h */
932