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