MySQL  8.0.20
Source Code Documentation
task_os.h
Go to the documentation of this file.
1 /* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
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 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
31 
32 #ifdef _WIN32
33 
34 #include <MSWSock.h>
35 #include <Ws2tcpip.h>
36 #include <io.h>
37 #include <winsock2.h>
38 
39 #define DIR_SEP '\\'
40 #define SOCK_EINTR WSAEINTR
41 #define SOCK_EAGAIN WSAEINPROGRESS
42 #define SOCK_EWOULDBLOCK WSAEWOULDBLOCK
43 #define SOCK_EINPROGRESS WSAEINPROGRESS
44 #define SOCK_EALREADY WSAEALREADY
45 #define SOCK_ECONNREFUSED WSAECONNREFUSED
46 #define SOCK_ERRNO task_errno
47 #define SOCK_OPT_REUSEADDR SO_EXCLUSIVEADDRUSE
48 #define GET_OS_ERR WSAGetLastError()
49 #define SET_OS_ERR(x) WSASetLastError(x)
50 #define CLOSESOCKET(x) closesocket(x)
51 #define SOCK_SHUT_RDWR SD_BOTH
52 
53 static inline int hard_connect_err(int err) {
54  return err != 0 && from_errno(err) != WSAEINTR &&
55  from_errno(err) != WSAEINPROGRESS &&
57 }
58 
59 static inline int hard_select_err(int err) {
60  return err != 0 && from_errno(err) != WSAEINTR;
61 }
62 
63 #if (_WIN32_WINNT < 0x0600)
64 #error "Need _WIN32_WINNT >= 0x0600"
65 #endif
66 
67 typedef ULONG nfds_t;
68 typedef struct pollfd pollfd;
69 static inline int poll(pollfd *fds, nfds_t nfds, int timeout) {
70  return WSAPoll(fds, nfds, timeout);
71 }
72 
73 static inline int is_socket_error(int x) { return x == SOCKET_ERROR || x < 0; }
74 
75 #else
76 #include <errno.h>
77 #include <netdb.h>
78 #include <netinet/in.h>
79 #include <netinet/tcp.h>
80 #include <sys/socket.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_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 
103 static inline int hard_connect_err(int err) {
104  return err != 0 && from_errno(err) != EINTR && from_errno(err) != EINPROGRESS;
105 }
106 
107 static inline int hard_select_err(int err) {
108  return from_errno(err) != 0 && from_errno(err) != EINTR;
109 }
110 
111 typedef struct pollfd pollfd;
112 
113 static inline int is_socket_error(int x) { return x < 0; }
114 
115 #endif
116 
117 extern void remove_and_wakeup(int fd);
118 
119 static inline result close_socket(int *sock) {
120  result res = {0, 0};
121  if (*sock != -1) {
122  do {
123  SET_OS_ERR(0);
124  res.val = CLOSESOCKET(*sock);
125  res.funerr = to_errno(GET_OS_ERR);
126  } while (res.val == -1 && from_errno(res.funerr) == SOCK_EINTR);
127  remove_and_wakeup(*sock);
128  *sock = -1;
129  }
130  return res;
131 }
132 
133 #if defined(_WIN32)
134 
135 static inline void shutdown_socket(int *sock) {
136  static LPFN_DISCONNECTEX DisconnectEx = NULL;
137  if (DisconnectEx == NULL) {
138  DWORD dwBytesReturned;
139  GUID guidDisconnectEx = WSAID_DISCONNECTEX;
140  WSAIoctl(*sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guidDisconnectEx,
141  sizeof(GUID), &DisconnectEx, sizeof(DisconnectEx),
142  &dwBytesReturned, NULL, NULL);
143  }
144  if (DisconnectEx != NULL) {
145  (DisconnectEx(*sock, (LPOVERLAPPED)NULL, (DWORD)0, (DWORD)0) == TRUE) ? 0
146  : -1;
147  } else {
148  shutdown(*sock, SOCK_SHUT_RDWR);
149  }
150 }
151 
152 #else
153 
154 static inline void shutdown_socket(int *sock) {
155  shutdown(*sock, SOCK_SHUT_RDWR);
156 }
157 
158 #endif
159 
160 static inline result shut_close_socket(int *sock) {
161  result res = {0, 0};
162  if (*sock >= 0) {
163  shutdown_socket(sock);
164  res = close_socket(sock);
165  }
166  return res;
167 }
168 
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 #endif
Definition: result.h:33
#define SOCK_EWOULDBLOCK
Definition: task_os.h:92
static MYSQL * sock
Definition: mysqlcheck.cc:55
#define CLOSESOCKET(x)
Definition: task_os.h:100
#define SOCKET_ERROR
Definition: x_platform.h:279
struct pollfd pollfd
Definition: task_os.h:111
#define SET_OS_ERR(x)
Definition: task_os.h:99
static result close_socket(int *sock)
Definition: task_os.h:119
#define SOCK_SHUT_RDWR
Definition: task_os.h:101
#define SOCK_EINTR
Definition: task_os.h:90
bool shutdown()
Shuts down the data dictionary instance by deleting the instance of dd::Dictionary_impl* upon server ...
Definition: dd.cc:70
static int is_socket_error(int x)
Definition: task_os.h:113
static void shutdown_socket(int *sock)
Definition: task_os.h:154
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
#define GET_OS_ERR
Definition: task_os.h:98
int funerr
Definition: result.h:35
#define NULL
Definition: types.h:55
static int from_errno(int err)
Definition: result.h:45
#define TRUE
Definition: types.h:51
static int to_errno(int err)
Definition: result.h:41
static result shut_close_socket(int *sock)
Definition: task_os.h:160
static int hard_select_err(int err)
Definition: task_os.h:107
static int hard_connect_err(int err)
Definition: task_os.h:103
void remove_and_wakeup(int fd)
Definition: task.c:869
int val
Definition: result.h:34