1/* Copyright (c) 2005, 2012, Oracle and/or its affiliates.
2 Copyright (c) 2009, 2017, MariaDB Corporation.
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 as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
17#ifndef _sql_plugin_h
18#define _sql_plugin_h
19
20/*
21 the following #define adds server-only members to enum_mysql_show_type,
22 that is defined in plugin.h
23*/
24#define SHOW_always_last SHOW_KEY_CACHE_LONG, \
25 SHOW_LONG_STATUS, SHOW_DOUBLE_STATUS, \
26 SHOW_HAVE, SHOW_MY_BOOL, SHOW_HA_ROWS, SHOW_SYS, \
27 SHOW_LONG_NOFLUSH, SHOW_LONGLONG_STATUS, SHOW_UINT32_STATUS, \
28 SHOW_LEX_STRING
29#include "mariadb.h"
30#undef SHOW_always_last
31
32#include "m_string.h" /* LEX_STRING */
33#include "my_alloc.h" /* MEM_ROOT */
34
35class sys_var;
36enum SHOW_COMP_OPTION { SHOW_OPTION_YES, SHOW_OPTION_NO, SHOW_OPTION_DISABLED};
37enum enum_plugin_load_option { PLUGIN_OFF, PLUGIN_ON, PLUGIN_FORCE,
38 PLUGIN_FORCE_PLUS_PERMANENT };
39extern const char *global_plugin_typelib_names[];
40
41extern ulong dlopen_count;
42
43#include <my_sys.h>
44#include "sql_list.h"
45
46#ifdef DBUG_OFF
47#define plugin_ref_to_int(A) A
48#define plugin_int_to_ref(A) A
49#else
50#define plugin_ref_to_int(A) (A ? A[0] : NULL)
51#define plugin_int_to_ref(A) &(A)
52#endif
53
54/*
55 the following flags are valid for plugin_init()
56*/
57#define PLUGIN_INIT_SKIP_DYNAMIC_LOADING 1U
58#define PLUGIN_INIT_SKIP_PLUGIN_TABLE 2U
59#define PLUGIN_INIT_SKIP_INITIALIZATION 4U
60
61#define INITIAL_LEX_PLUGIN_LIST_SIZE 16
62
63typedef enum enum_mysql_show_type SHOW_TYPE;
64typedef struct st_mysql_show_var SHOW_VAR;
65
66#define MYSQL_ANY_PLUGIN -1
67
68/*
69 different values of st_plugin_int::state
70 though they look like a bitmap, plugin may only
71 be in one of those eigenstates, not in a superposition of them :)
72 It's a bitmap, because it makes it easier to test
73 "whether the state is one of those..."
74*/
75#define PLUGIN_IS_FREED 1U
76#define PLUGIN_IS_DELETED 2U
77#define PLUGIN_IS_UNINITIALIZED 4U
78#define PLUGIN_IS_READY 8U
79#define PLUGIN_IS_DYING 16U
80#define PLUGIN_IS_DISABLED 32U
81
82struct st_ptr_backup {
83 void **ptr;
84 void *value;
85 void save(void **p) { ptr= p; value= *p; }
86 void save(const char **p) { save((void**)p); }
87 void restore() { *ptr= value; }
88};
89
90/* A handle for the dynamic library containing a plugin or plugins. */
91
92struct st_plugin_dl
93{
94 LEX_CSTRING dl;
95 void *handle;
96 struct st_maria_plugin *plugins;
97 st_ptr_backup *ptr_backup;
98 uint nbackups;
99 uint ref_count; /* number of plugins loaded from the library */
100 int mysqlversion;
101 int mariaversion;
102 bool allocated;
103};
104
105/* A handle of a plugin */
106
107struct st_plugin_int
108{
109 LEX_CSTRING name;
110 struct st_maria_plugin *plugin;
111 struct st_plugin_dl *plugin_dl;
112 st_ptr_backup *ptr_backup;
113 uint nbackups;
114 uint state;
115 uint ref_count; /* number of threads using the plugin */
116 uint locks_total; /* how many times the plugin was locked */
117 void *data; /* plugin type specific, e.g. handlerton */
118 MEM_ROOT mem_root; /* memory for dynamic plugin structures */
119 sys_var *system_vars; /* server variables for this plugin */
120 enum enum_plugin_load_option load_option; /* OFF, ON, FORCE, F+PERMANENT */
121};
122
123
124extern mysql_mutex_t LOCK_plugin;
125
126/*
127 See intern_plugin_lock() for the explanation for the
128 conditionally defined plugin_ref type
129*/
130#ifdef DBUG_OFF
131typedef struct st_plugin_int *plugin_ref;
132#define plugin_ref_to_int(A) A
133#define plugin_int_to_ref(A) A
134#define plugin_decl(pi) ((pi)->plugin)
135#define plugin_dlib(pi) ((pi)->plugin_dl)
136#define plugin_data(pi,cast) ((cast)((pi)->data))
137#define plugin_name(pi) (&((pi)->name))
138#define plugin_state(pi) ((pi)->state)
139#define plugin_load_option(pi) ((pi)->load_option)
140#define plugin_equals(p1,p2) ((p1) == (p2))
141#else
142typedef struct st_plugin_int **plugin_ref;
143#define plugin_ref_to_int(A) (A ? A[0] : NULL)
144#define plugin_int_to_ref(A) &(A)
145#define plugin_decl(pi) ((pi)[0]->plugin)
146#define plugin_dlib(pi) ((pi)[0]->plugin_dl)
147#define plugin_data(pi,cast) ((cast)((pi)[0]->data))
148#define plugin_name(pi) (&((pi)[0]->name))
149#define plugin_state(pi) ((pi)[0]->state)
150#define plugin_load_option(pi) ((pi)[0]->load_option)
151#define plugin_equals(p1,p2) ((p1) && (p2) && (p1)[0] == (p2)[0])
152#endif
153
154typedef int (*plugin_type_init)(struct st_plugin_int *);
155
156extern I_List<i_string> *opt_plugin_load_list_ptr;
157extern char *opt_plugin_dir_ptr;
158extern MYSQL_PLUGIN_IMPORT char opt_plugin_dir[FN_REFLEN];
159extern const LEX_CSTRING plugin_type_names[];
160extern ulong plugin_maturity;
161extern TYPELIB plugin_maturity_values;
162extern const char *plugin_maturity_names[];
163
164extern int plugin_init(int *argc, char **argv, int init_flags);
165extern void plugin_shutdown(void);
166void add_plugin_options(DYNAMIC_ARRAY *options, MEM_ROOT *mem_root);
167extern bool plugin_is_ready(const LEX_CSTRING *name, int type);
168#define my_plugin_lock_by_name(A,B,C) plugin_lock_by_name(A,B,C)
169#define my_plugin_lock(A,B) plugin_lock(A,B)
170extern plugin_ref plugin_lock(THD *thd, plugin_ref ptr);
171extern plugin_ref plugin_lock_by_name(THD *thd, const LEX_CSTRING *name,
172 int type);
173extern void plugin_unlock(THD *thd, plugin_ref plugin);
174extern void plugin_unlock_list(THD *thd, plugin_ref *list, uint count);
175extern bool mysql_install_plugin(THD *thd, const LEX_CSTRING *name,
176 const LEX_CSTRING *dl);
177extern bool mysql_uninstall_plugin(THD *thd, const LEX_CSTRING *name,
178 const LEX_CSTRING *dl);
179extern bool plugin_register_builtin(struct st_mysql_plugin *plugin);
180extern void plugin_thdvar_init(THD *thd);
181extern void plugin_thdvar_cleanup(THD *thd);
182sys_var *find_plugin_sysvar(st_plugin_int *plugin, st_mysql_sys_var *var);
183void plugin_opt_set_limits(struct my_option *, const struct st_mysql_sys_var *);
184extern SHOW_COMP_OPTION plugin_status(const char *name, size_t len, int type);
185extern bool check_valid_path(const char *path, size_t length);
186extern void plugin_mutex_init();
187
188typedef my_bool (plugin_foreach_func)(THD *thd,
189 plugin_ref plugin,
190 void *arg);
191#define plugin_foreach(A,B,C,D) plugin_foreach_with_mask(A,B,C,PLUGIN_IS_READY,D)
192extern bool plugin_foreach_with_mask(THD *thd, plugin_foreach_func *func,
193 int type, uint state_mask, void *arg);
194extern void sync_dynamic_session_variables(THD* thd, bool global_lock);
195
196extern bool plugin_dl_foreach(THD *thd, const LEX_CSTRING *dl,
197 plugin_foreach_func *func, void *arg);
198
199sys_var *find_sys_var_ex(THD *thd, const char *str, size_t length,
200 bool throw_error, bool locked);
201
202extern void sync_dynamic_session_variables(THD* thd, bool global_lock);
203#endif
204
205#ifdef WITH_WSREP
206extern void wsrep_plugins_pre_init();
207extern void wsrep_plugins_post_init();
208#endif /* WITH_WSREP */
209
210