MySQL 9.1.0
Source Code Documentation
task_os.h
Go to the documentation of this file.
1/* Copyright (c) 2012, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
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, version 2.0, 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#ifndef TASK_OS_H
25#define TASK_OS_H
26
27#include "xcom/result.h"
28#include "xcom/task_debug.h"
29
30#ifdef _WIN32
31
32#include <MSWSock.h>
33#include <Ws2tcpip.h>
34#include <io.h>
35#include <winsock2.h>
36
37#define DIR_SEP '\\'
38#define SOCK_EINTR WSAEINTR
39#define SOCK_EAGAIN WSAEINPROGRESS
40#define SOCK_EWOULDBLOCK WSAEWOULDBLOCK
41#define SOCK_EINPROGRESS WSAEINPROGRESS
42#define SOCK_EALREADY WSAEALREADY
43#define SOCK_ECONNREFUSED WSAECONNREFUSED
44#define SOCK_ECONNRESET WSAECONNRESET
45#define SOCK_ERRNO task_errno
46#define SOCK_OPT_REUSEADDR SO_EXCLUSIVEADDRUSE
47#define GET_OS_ERR WSAGetLastError()
48#define SET_OS_ERR(x) WSASetLastError(x)
49#define CLOSESOCKET(x) closesocket(x)
50#define SOCK_SHUT_RDWR SD_BOTH
51
52static inline int hard_connect_err(int err) {
53 return err != 0 && from_errno(err) != WSAEINTR &&
54 from_errno(err) != WSAEINPROGRESS &&
56}
57
58static inline int hard_select_err(int err) {
59 return err != 0 && from_errno(err) != WSAEINTR;
60}
61
62#if (_WIN32_WINNT < 0x0600)
63#error "Need _WIN32_WINNT >= 0x0600"
64#endif
65
66typedef ULONG nfds_t;
67typedef struct pollfd pollfd;
68static inline int poll(pollfd *fds, nfds_t nfds, int timeout) {
69 return nfds == 0 ? 0 : WSAPoll(fds, nfds, timeout);
70}
71
72static inline int is_socket_error(int x) { return x == SOCKET_ERROR || x < 0; }
73
74#else
75#include <errno.h>
76#include <netdb.h>
77#include <netinet/in.h>
78#include <netinet/tcp.h>
79#include <sys/socket.h>
80#include <sys/types.h>
81#include <unistd.h>
82
83#define DIR_SEP '/'
84
85/* Solaris and Linux differ here */
86#ifndef IPPROTO_TCP
87#define IPPROTO_TCP SOL_TCP
88#endif
89
90#define SOCK_EINTR EINTR
91#define SOCK_EAGAIN EAGAIN
92#define SOCK_EWOULDBLOCK EWOULDBLOCK
93#define SOCK_EINPROGRESS EINPROGRESS
94#define SOCK_EALREADY EALREADY
95#define SOCK_ECONNREFUSED ECONNREFUSED
96#define SOCK_ECONNRESET ECONNRESET
97#define SOCK_ERRNO task_errno
98#define SOCK_OPT_REUSEADDR SO_REUSEADDR
99#define GET_OS_ERR errno
100#define SET_OS_ERR(x) errno = (x)
101#define CLOSESOCKET(x) close(x)
102#define SOCK_SHUT_RDWR (SHUT_RD | SHUT_WR)
103#define SOCK_SHUT_RW SHUT_WR
104#define SOCK_SHUT_RD SHUT_RD
105
106static inline int hard_connect_err(int err) {
107 return err != 0 && from_errno(err) != EINTR && from_errno(err) != EINPROGRESS;
108}
109
110static inline int hard_select_err(int err) {
111 return from_errno(err) != 0 && from_errno(err) != EINTR;
112}
113
114typedef struct pollfd pollfd;
115
116static inline int is_socket_error(int x) { return x < 0; }
117
118#endif
119
120extern void remove_and_wakeup(int fd);
121
122#if defined(_WIN32)
123
124static inline void shutdown_socket(int *sock) {
125 static LPFN_DISCONNECTEX DisconnectEx = nullptr;
126 if (DisconnectEx == nullptr) {
127 DWORD dwBytesReturned;
128 GUID guidDisconnectEx = WSAID_DISCONNECTEX;
129 WSAIoctl(*sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guidDisconnectEx,
130 sizeof(GUID), &DisconnectEx, sizeof(DisconnectEx),
131 &dwBytesReturned, nullptr, nullptr);
132 }
133 if (DisconnectEx != nullptr) {
134 DisconnectEx(*sock, (LPOVERLAPPED) nullptr, (DWORD)0, (DWORD)0);
135 } else {
137 }
138}
139
140static inline int xcom_getpeername(int sock, struct sockaddr *name,
141 socklen_t *namelen) {
142 int x, retval;
143 x = (int)*namelen;
144 retval = getpeername(sock, name, &x);
145 *namelen = (socklen_t)x;
146 return retval;
147}
148
149#else
150
151static inline void shutdown_socket(int *sock) {
153}
154
155static inline int xcom_getpeername(int s, struct sockaddr *name,
156 socklen_t *namelen) {
157 return getpeername(s, name, namelen);
158}
159
160#endif
161
162static inline result xcom_close_socket(int *sock) {
163 result res = {0, 0};
164 if (*sock != -1) {
165 IFDBG(D_FILEOP, FN; STRLIT("closing socket "); NDBG(*sock, d));
166 do {
167 SET_OS_ERR(0);
168 res.val = CLOSESOCKET(*sock);
170 } while (res.val == -1 && from_errno(res.funerr) == SOCK_EINTR);
171 }
172 return res;
173}
174
175static inline result xcom_shut_close_socket(int *sock) {
176 result res = {0, 0};
177 if (*sock >= 0) {
179 res = xcom_close_socket(sock);
180 }
181 return res;
182}
183
184#endif
#define IFDBG(mask, body)
Definition: gcs_debug.h:279
#define FN
Definition: gcs_debug.h:308
@ D_FILEOP
Definition: gcs_debug.h:187
#define STRLIT(x)
Definition: gcs_debug.h:316
#define NDBG(x, f)
Definition: gcs_debug.h:318
static MYSQL * sock
Definition: mysqlcheck.cc:57
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
MYSQL_STRINGS_EXPORT void shutdown()
Shutdown character set/collation library.
Definition: collations.cc:105
stdx::expected< size_t, std::error_code > poll(poll_fd *fds, size_t num_fds, std::chrono::milliseconds timeout)
Definition: poll.h:53
stdx::expected< void, error_type > getpeername(native_handle_type native_handle, struct sockaddr *addr, size_t *addr_len)
Definition: socket.h:423
static int to_errno(int err)
Definition: result.h:38
static int from_errno(int err)
Definition: result.h:42
struct sockaddr sockaddr
Definition: sock_probe_win32.h:63
case opt name
Definition: sslopt-case.h:29
Definition: result.h:30
int val
Definition: result.h:31
int funerr
Definition: result.h:32
void remove_and_wakeup(int fd)
Definition: task.cc:880
#define CLOSESOCKET(x)
Definition: task_os.h:101
static result xcom_shut_close_socket(int *sock)
Definition: task_os.h:175
#define SET_OS_ERR(x)
Definition: task_os.h:100
static int is_socket_error(int x)
Definition: task_os.h:116
#define SOCK_SHUT_RDWR
Definition: task_os.h:102
static int hard_select_err(int err)
Definition: task_os.h:110
struct pollfd pollfd
Definition: task_os.h:114
static int xcom_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
Definition: task_os.h:155
#define SOCK_EWOULDBLOCK
Definition: task_os.h:92
static int hard_connect_err(int err)
Definition: task_os.h:106
static void shutdown_socket(int *sock)
Definition: task_os.h:151
#define GET_OS_ERR
Definition: task_os.h:99
static result xcom_close_socket(int *sock)
Definition: task_os.h:162
#define SOCK_EINTR
Definition: task_os.h:90
#define SOCKET_ERROR
Definition: x_platform.h:284