1 /* Copyright (c) 2005, 2021, Oracle and/or its affiliates.
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 Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 #ifndef _sql_plugin_h
24 #define _sql_plugin_h
25 
26 #include <my_global.h>
27 #include <vector>
28 #include <mysql/plugin.h>
29 
30 #include "mysql/mysql_lex_string.h"         // LEX_STRING
31 #include "my_alloc.h"                       /* MEM_ROOT */
32 #include "my_getopt.h"                      /* my_option */
33 #include "sql_const.h"                      /* SHOW_COMP_OPTION */
34 
35 extern const char *global_plugin_typelib_names[];
36 extern mysql_mutex_t LOCK_plugin_delete;
37 
38 #include <my_sys.h>
39 #include "sql_list.h"
40 #include "sql_cmd.h"
41 #include "sql_plugin_ref.h"
42 
43 #ifdef NDEBUG
44 #define plugin_ref_to_int(A) A
45 #define plugin_int_to_ref(A) A
46 #else
47 #define plugin_ref_to_int(A) (A ? A[0] : NULL)
48 #define plugin_int_to_ref(A) &(A)
49 #endif
50 
51 /*
52   the following flags are valid for plugin_init()
53 */
54 #define PLUGIN_INIT_SKIP_DYNAMIC_LOADING 1
55 #define PLUGIN_INIT_SKIP_PLUGIN_TABLE    2
56 #define PLUGIN_INIT_SKIP_INITIALIZATION  4
57 
58 typedef struct st_mysql_show_var SHOW_VAR;
59 typedef struct st_mysql_lex_string LEX_STRING;
60 
61 #define MYSQL_ANY_PLUGIN         -1
62 
63 /*
64   different values of st_plugin_int::state
65   though they look like a bitmap, plugin may only
66   be in one of those eigenstates, not in a superposition of them :)
67   It's a bitmap, because it makes it easier to test
68   "whether the state is one of those..."
69 */
70 #define PLUGIN_IS_FREED         1
71 #define PLUGIN_IS_DELETED       2
72 #define PLUGIN_IS_UNINITIALIZED 4
73 #define PLUGIN_IS_READY         8
74 #define PLUGIN_IS_DYING         16
75 #define PLUGIN_IS_DISABLED      32
76 
77 /* A handle for the dynamic library containing a plugin or plugins. */
78 
79 struct st_plugin_dl
80 {
81   LEX_STRING dl;
82   void *handle;
83   struct st_mysql_plugin *plugins;
84   int version;
85   uint ref_count;            /* number of plugins loaded from the library */
86 };
87 
88 
89 /**
90    This class implements the INSTALL PLUGIN statement.
91 */
92 
93 class Sql_cmd_install_plugin : public Sql_cmd
94 {
95 public:
Sql_cmd_install_plugin(const LEX_STRING & comment,const LEX_STRING & ident)96   Sql_cmd_install_plugin(const LEX_STRING& comment,
97                          const LEX_STRING& ident)
98   : m_comment(comment), m_ident(ident)
99   { }
100 
sql_command_code()101   virtual enum_sql_command sql_command_code() const
102   { return SQLCOM_INSTALL_PLUGIN; }
103 
104   /**
105     Install a new plugin by inserting a row into the
106     mysql.plugin table, creating a cache entry and
107     initializing plugin's internal data.
108 
109     @param thd  Thread context
110 
111     @returns false if success, true otherwise
112   */
113   virtual bool execute(THD *thd);
114 
115 private:
116   LEX_STRING m_comment;
117   LEX_STRING m_ident;
118 };
119 
120 
121 /**
122    This class implements the UNINSTALL PLUGIN statement.
123 */
124 
125 class Sql_cmd_uninstall_plugin : public Sql_cmd
126 {
127 public:
Sql_cmd_uninstall_plugin(const LEX_STRING & comment)128   explicit Sql_cmd_uninstall_plugin(const LEX_STRING& comment)
129   : m_comment(comment)
130   { }
131 
sql_command_code()132   virtual enum_sql_command sql_command_code() const
133   { return SQLCOM_UNINSTALL_PLUGIN; }
134 
135   /**
136     Uninstall a plugin by removing a row from the
137     mysql.plugin table, deleting a cache entry and
138     deinitializing plugin's internal data.
139 
140     @param thd  Thread context
141 
142     @returns false if success, true otherwise
143   */
144   virtual bool execute(THD *thd);
145 
146 private:
147   LEX_STRING m_comment;
148 };
149 
150 
151 typedef int (*plugin_type_init)(struct st_plugin_int *);
152 
153 extern I_List<i_string> *opt_plugin_load_list_ptr;
154 extern I_List<i_string> *opt_early_plugin_load_list_ptr;
155 extern char *opt_plugin_dir_ptr;
156 extern char opt_plugin_dir[FN_REFLEN];
157 extern const LEX_STRING plugin_type_names[];
158 
159 extern bool plugin_register_early_plugins(int *argc, char **argv, int flags);
160 extern bool plugin_register_builtin_and_init_core_se(int *argc, char **argv);
161 extern bool plugin_register_dynamic_and_init_all(int *argc,
162                                                  char **argv, int init_flags);
163 extern void plugin_shutdown(void);
164 extern void memcached_shutdown(void);
165 void add_plugin_options(std::vector<my_option> *options, MEM_ROOT *mem_root);
166 extern bool plugin_is_ready(const LEX_CSTRING &name, int type);
167 #define my_plugin_lock_by_name(A,B,C) plugin_lock_by_name(A,B,C)
168 #define my_plugin_lock_by_name_ci(A,B,C) plugin_lock_by_name(A,B,C)
169 #define my_plugin_lock(A,B) plugin_lock(A,B)
170 #define my_plugin_lock_ci(A,B) plugin_lock(A,B)
171 extern plugin_ref plugin_lock(THD *thd, plugin_ref *ptr);
172 extern plugin_ref plugin_lock_by_name(THD *thd, const LEX_CSTRING &name,
173                                       int type);
174 extern void plugin_unlock(THD *thd, plugin_ref plugin);
175 extern void plugin_unlock_list(THD *thd, plugin_ref *list, size_t count);
176 extern bool plugin_register_builtin(struct st_mysql_plugin *plugin);
177 extern void plugin_thdvar_init(THD *thd, bool enable_plugins);
178 extern void plugin_thdvar_cleanup(THD *thd, bool enable_plugins);
179 extern "C" void plugin_thdvar_safe_update(THD *thd, st_mysql_sys_var *var,
180                                           char **dest, const char *value);
181 extern SHOW_COMP_OPTION plugin_status(const char *name, size_t len, int type);
182 extern bool check_valid_path(const char *path, size_t length);
183 extern void alloc_and_copy_thd_dynamic_variables(THD *thd, bool global_lock);
184 
185 typedef my_bool (plugin_foreach_func)(THD *thd,
186                                       plugin_ref plugin,
187                                       void *arg);
188 #define plugin_foreach(A,B,C,D) plugin_foreach_with_mask(A,B,C,PLUGIN_IS_READY,D)
189 extern bool plugin_foreach_with_mask(THD *thd, plugin_foreach_func *func,
190                                      int type, uint state_mask, void *arg);
191 extern bool plugin_foreach_with_mask(THD *thd, plugin_foreach_func **funcs,
192                                      int type, uint state_mask, void *arg);
193 int lock_plugin_data();
194 int unlock_plugin_data();
195 
196 /**
197   Initialize one plugin.
198 */
199 bool plugin_early_load_one(int *argc, char **argv, const char *plugin);
200 
201 /**
202   Create deep copy of system_variables instance.
203 */
204 extern
205 struct system_variables *
206 copy_system_variables(THD *thd, bool enable_plugins);
207 extern void free_system_variables(struct system_variables *v, bool enable_plugins);
208 #endif
209