MySQL 8.0.32
Source Code Documentation
dynamic_loader.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2022, Oracle and/or its affiliates.
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, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQL_HARNESS_DYNAMIC_LOADER_INCLUDED
26#define MYSQL_HARNESS_DYNAMIC_LOADER_INCLUDED
27
28#include <string>
29#include <system_error>
30
31#ifdef _WIN32
32#include <Windows.h> // HMODULE
33#endif
34
35#include "harness_export.h"
37
38namespace mysql_harness {
39/**
40 * error-codes of the DynamicLoader and DynamicLibrary.
41 *
42 * when set, the error-msg needs be retrieved from DynamicLoader().error_msg()
43 * or DynamicLibrary().error_msg()
44 */
46 kDlError = 1,
47};
48} // namespace mysql_harness
49
50namespace std {
51template <>
52struct is_error_code_enum<mysql_harness::DynamicLoaderErrc> : std::true_type {};
53} // namespace std
54
55namespace mysql_harness {
56
57/**
58 * make error_code from a DynamicLoaderErrc.
59 *
60 * ~~~{.cpp}
61 * throw std::system_error(make_error_code(DynamicLoaderErrc::kDlError))
62 * ~~~
63 */
64std::error_code make_error_code(DynamicLoaderErrc ec);
65
66// forward decl for the friend relationship
67class DynamicLoader;
68
69/**
70 * A DynamicLibrary.
71 */
72class HARNESS_EXPORT DynamicLibrary {
73 public:
74#ifdef _WIN32
75 using native_handle_type = HMODULE;
76#else
77 using native_handle_type = void *;
78#endif
79 // construct a DynamicLibrary that refers to the main executable.
80 DynamicLibrary() = default;
81
82 // disable copy constructor.
83 DynamicLibrary(const DynamicLibrary &) = delete;
84
85 // move constructor.
87 : filename_{std::move(rhs.filename_)},
88 handle_{std::exchange(rhs.handle_, nullptr)},
89 error_msg_{std::move(rhs.error_msg_)} {}
90
91 // disable copy assignment.
93
94 // move assignment.
96 filename_ = std::move(rhs.filename_);
97 handle_ = std::exchange(rhs.handle_, nullptr);
98 error_msg_ = std::move(rhs.error_msg_);
99 return *this;
100 }
101
102 /**
103 * destruct a DynamicLibrary.
104 *
105 * unloads dynamic library if it is loaded
106 */
107 ~DynamicLibrary() { unload(); }
108
109 /**
110 * unload a DynamicLibary if it is loaded.
111 */
112 void unload();
113
114 /**
115 * get the native handle to the shared object.
116 */
117 native_handle_type native_handle() const { return handle_; }
118
119 /**
120 * get a symbol from the dynamic library.
121 */
122 stdx::expected<void *, std::error_code> symbol(const std::string &name) const;
123
124 /**
125 * get error message if symbol() failed with DynamicLoaderErrc::DlError.
126 */
127 std::string error_msg() const { return error_msg_; }
128
129 /**
130 * get filename of the loaded module.
131 */
132 std::string filename() const { return filename_; }
133
134 friend class DynamicLoader;
135
136 private:
137 /**
138 * construct DynamicLibrary from native_handle.
139 *
140 * @param filename filename on the loaded library
141 * @param handle handle to the loaded library
142 */
144 : filename_{std::move(filename)}, handle_{handle} {}
145
146 std::string filename_;
147
149
150 mutable std::string error_msg_;
151};
152
153/**
154 * Loader for DynamicLibrary.
155 */
156class HARNESS_EXPORT DynamicLoader {
157 public:
158 DynamicLoader() : search_path_{} {}
159
160 /**
161 * construct DynamicLoader with search_path.
162 */
163 explicit DynamicLoader(std::string search_path)
164 : search_path_{std::move(search_path)} {}
165
166 /**
167 * load a shared library from path.
168 *
169 * @param name library name without suffix
170 * @return DynamicLibrary on success, std::error_code on failure
171 */
173 const std::string &name) const;
174
175 /**
176 * get error message if load() failed with DynamicLoaderErrc::DlError.
177 */
178 std::string error_msg() const { return error_msg_; }
179
180 /**
181 * get current search path.
182 */
183 std::string search_path() const { return search_path_; }
184
185 private:
186 std::string search_path_;
187
188 mutable std::string error_msg_;
189};
190
191} // namespace mysql_harness
192
193#endif
A DynamicLibrary.
Definition: dynamic_loader.h:72
DynamicLibrary(std::string filename, native_handle_type handle)
construct DynamicLibrary from native_handle.
Definition: dynamic_loader.h:143
DynamicLibrary(DynamicLibrary &&rhs)
Definition: dynamic_loader.h:86
DynamicLibrary & operator=(DynamicLibrary &&rhs)
Definition: dynamic_loader.h:95
void * native_handle_type
Definition: dynamic_loader.h:77
DynamicLibrary & operator=(const DynamicLibrary &)=delete
std::string error_msg() const
get error message if symbol() failed with DynamicLoaderErrc::DlError.
Definition: dynamic_loader.h:127
std::string filename() const
get filename of the loaded module.
Definition: dynamic_loader.h:132
DynamicLibrary(const DynamicLibrary &)=delete
~DynamicLibrary()
destruct a DynamicLibrary.
Definition: dynamic_loader.h:107
std::string error_msg_
Definition: dynamic_loader.h:150
std::string filename_
Definition: dynamic_loader.h:146
native_handle_type native_handle() const
get the native handle to the shared object.
Definition: dynamic_loader.h:117
Loader for DynamicLibrary.
Definition: dynamic_loader.h:156
DynamicLoader(std::string search_path)
construct DynamicLoader with search_path.
Definition: dynamic_loader.h:163
DynamicLoader()
Definition: dynamic_loader.h:158
std::string error_msg_
Definition: dynamic_loader.h:188
std::string search_path() const
get current search path.
Definition: dynamic_loader.h:183
std::string search_path_
Definition: dynamic_loader.h:186
std::string error_msg() const
get error message if load() failed with DynamicLoaderErrc::DlError.
Definition: dynamic_loader.h:178
Definition: expected.h:943
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
bool load(THD *, const dd::String_type &fname, dd::String_type *buf)
Read an sdi file from disk and store in a buffer.
Definition: sdi_file.cc:307
Definition: common.h:41
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:78
DynamicLoaderErrc
error-codes of the DynamicLoader and DynamicLibrary.
Definition: dynamic_loader.h:45
static int handle(int sql_errno, const char *sqlstate, const char *message, void *state)
Bridge function between the C++ API offered by this module and the C API of the parser service.
Definition: services.cc:64
Definition: varlen_sort.h:183
const char * filename
Definition: pfs_example_component_population.cc:66
case opt name
Definition: sslopt-case.h:32