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