- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathoperations_factory.cc
230 lines (181 loc) · 6.72 KB
/
operations_factory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (c) 2015, 2025, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0,
* as published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms,
* as designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include"plugin/x/src/operations_factory.h"
#include<fcntl.h>
#include<cerrno>
#ifndef _WIN32
#include<netdb.h>
#endif
#include<cinttypes>
#include<cstdint>
#include<string>
#include"my_io.h"// NOLINT(build/include_subdir)
#include"my_systime.h"// NOLINT(build/include_subdir)
#ifdef HAVE_SYS_UN_H
#include<sys/types.h>
#include<sys/un.h>
#include<csignal>
#endif
#include"plugin/x/src/config/config.h"
namespacexpl {
namespacedetails {
classSocket : publiciface::Socket {
public:
explicitSocket(MYSQL_SOCKET mysql_socket) : m_mysql_socket(mysql_socket) {}
Socket(PSI_socket_key key [[maybe_unused]], int domain, int type,
int protocol)
: m_mysql_socket(mysql_socket_socket(key, domain, type, protocol)) {}
~Socket() override { close(); }
intbind(conststructsockaddr *addr, socklen_t len) override {
returnmysql_socket_bind(m_mysql_socket, addr, len);
}
MYSQL_SOCKET accept(PSI_socket_key key [[maybe_unused]],
structsockaddr *addr, socklen_t *addr_len) override {
returnmysql_socket_accept(key, m_mysql_socket, addr, addr_len);
}
intlisten(int backlog) override {
returnmysql_socket_listen(m_mysql_socket, backlog);
}
my_socket get_socket_fd() override {
returnmysql_socket_getfd(m_mysql_socket);
}
MYSQL_SOCKET get_socket_mysql() override { return m_mysql_socket; }
intset_socket_opt(int level, int optname, const SOCKBUF_T *optval,
socklen_t optlen) override {
returnmysql_socket_setsockopt(m_mysql_socket, level, optname, optval,
optlen);
}
voidclose() override {
if (INVALID_SOCKET != get_socket_fd()) {
mysql_socket_close(m_mysql_socket);
m_mysql_socket = MYSQL_INVALID_SOCKET;
}
}
voidset_socket_thread_owner() override {
mysql_socket_set_thread_owner(m_mysql_socket);
}
private:
MYSQL_SOCKET m_mysql_socket;
};
classFile : publiciface::File {
public:
File(constchar *name, int access, int permission)
: m_file_descriptor(::open(name, access, permission)) {}
~File() override { close(); }
intclose() override {
if (INVALID_FILE_DESCRIPTOR != m_file_descriptor) {
constint result = ::close(m_file_descriptor);
m_file_descriptor = INVALID_FILE_DESCRIPTOR;
return result;
}
return0;
}
intread(void *buffer, int nbyte) override {
return ::read(m_file_descriptor, buffer, nbyte);
}
intwrite(void *buffer, int nbyte) override {
return ::write(m_file_descriptor, buffer, nbyte);
}
boolis_valid() override {
return INVALID_FILE_DESCRIPTOR != m_file_descriptor;
}
intfsync() override {
#if defined(HAVE_SYS_UN_H)
return ::fsync(m_file_descriptor);
#else
return0;
#endif// defined(HAVE_SYS_UN_H)
}
private:
int m_file_descriptor;
staticconstint INVALID_FILE_DESCRIPTOR;
};
constint File::INVALID_FILE_DESCRIPTOR = -1;
classSystem : publiciface::System {
int32_tunlink(constchar *name [[maybe_unused]]) override {
returnHAVE_UNIX_SOCKET(::unlink(name), 0);
}
int32_tget_errno() override { return errno; }
int32_tget_ppid() override { returnHAVE_UNIX_SOCKET(::getppid(), 0); }
int32_tget_pid() override { returnHAVE_UNIX_SOCKET(::getpid(), 0); }
int32_tkill(int32_t pid [[maybe_unused]],
int32_t signal [[maybe_unused]]) override {
returnHAVE_UNIX_SOCKET(::kill(pid, signal), 0);
}
int32_tget_socket_errno() override { return socket_errno; }
voidset_socket_errno(constint32_t err) override {
#if defined(_WIN32)
// socket_errno resolved on windows to WASGetLastError which can't be set.
WSASetLastError(err);
#else
socket_errno = err;
#endif// defined(_WIN32)
}
voidget_socket_error_and_message(int32_t *out_err,
std::string *out_strerr) override {
*out_err = socket_errno;
#ifdef _WIN32
char *s = nullptr;
if (0 == FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, *out_err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&s), 0, nullptr)) {
char text[256];
snprintf(text, sizeof(text), "Error %" PRIi32, *out_err);
*out_strerr = text;
} else {
*out_strerr = s;
LocalFree(s);
}
#else
*out_strerr = strerror(*out_err);
#endif
}
voidfreeaddrinfo(addrinfo *ai) override { return ::freeaddrinfo(ai); }
int32_tgetaddrinfo(constchar *node, constchar *service,
const addrinfo *hints, addrinfo **res) override {
return ::getaddrinfo(node, service, hints, res);
}
voidsleep(uint32_t seconds) override { ::sleep(seconds); }
};
} // namespace details
std::shared_ptr<iface::Socket> Operations_factory::create_socket(
PSI_socket_key key, int domain, int type, int protocol) {
return std::make_shared<details::Socket>(key, domain, type, protocol);
}
std::shared_ptr<iface::Socket> Operations_factory::create_socket(
MYSQL_SOCKET mysql_socket) {
return std::make_shared<details::Socket>(mysql_socket);
}
std::shared_ptr<iface::File> Operations_factory::open_file(constchar *name,
int access,
int permission) {
return std::make_shared<details::File>(name, access, permission);
}
std::shared_ptr<iface::System> Operations_factory::create_system_interface() {
return std::make_shared<details::System>();
}
} // namespace xpl