MySQL  8.0.19
Source Code Documentation
x_platform.h
Go to the documentation of this file.
1 /* Copyright (c) 2010, 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 X_PLATFORM_H
24 #define X_PLATFORM_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /*
31  * Abstraction layer for lower level OS stuff needed
32  */
33 
34 #ifdef XCOM_STANDALONE
35 /*
36  Disable MY_ATTRIBUTE for Sun Studio and Visual Studio.
37  Note that Sun Studio supports some __attribute__ variants,
38  but not unused which we use quite a lot.
39 */
40 #ifndef MY_ATTRIBUTE
41 #if defined(__GNUC__)
42 #define MY_ATTRIBUTE(A) __attribute__(A)
43 #else
44 #define MY_ATTRIBUTE(A)
45 #endif
46 #endif
47 #endif
48 
49 /*
50  * Platform independent functions
51  */
52 #if defined(_WIN32)
53 
54 #include <io.h> /* open() */
55 #include <process.h>
56 #include <rpc/types.h>
57 #include <string.h>
58 #include <sys/locking.h>
59 
60 #define NO_SIGPIPE
61 #define strdup _strdup
62 
63 #define bzero(p, n) memset(p, 0, n)
64 
65 #ifndef inline
66 #define inline _inline
67 #endif
68 #define xcom_close(fd) _close(fd)
69 #define xcom_fclose(f) fclose(f)
70 #define xcom_chdir(s) _chdir(s)
71 #define xcom_dup(s) _dup(s)
72 #define xcom_dup2(s, t) _dup2(s, t)
73 #define xcom_lseek(fd, of, h) _lseek(fd, of, h)
74 #define xcom_open(n, f, m) _open(n, f, m)
75 #define xcom_fopen(n, m) fopen(n, m)
76 #define xcom_fstat(f, b) _fstat(f, b)
77 
78 #define XCOM_F_TLOCK _LK_NBLCK /* Lock without retry in _locking() */
79 #define xcom_lockf(fd, f, sz) _locking(fd, f, sz)
80 
81 #define xcom_lrand48() rand()
82 #define xcom_srand48(x) srand(x)
83 #define xcom_drand48() ((double)rand() / RAND_MAX)
84 
85 #define xcom_write(fd, buf, l) _write(fd, buf, l)
86 #define xcom_read(fd, buf, c) _read(fd, buf, c)
87 
88 #define xcom_mktemp(s) _mktemp(s)
89 
90 /* WARNING: snprintf() != _snprintf() */
91 /* #define xcom_snprintf(argv) _snprintf(argv) */
92 #define xcom_strdup(x) _strdup(x)
93 
94 #define xcom_strtok(b, d, c) strtok_s(b, d, c)
95 
96 #define xcom_strcasecmp(a, b) _stricmp(a, b)
97 
98 #define xcom_execv _execv
99 
100 #define xcom_g_io_channel_socket_new g_io_channel_win32_new_socket
101 
102 /** Posix states that the optval argument should be (const) void*, but
103  on Windows it is (const) char* (See
104  http://msdn.microsoft.com/en-us/library/windows/desktop/ms740476(v=vs.85).aspx
105  and since we normally pass in int* a cast is required on Windows.
106 
107  With this typedef we can call xsockopt functions uniformly across
108  platorms. */
109 typedef char *xcom_sockoptptr_t;
110 
111 typedef int mode_t;
112 typedef SSIZE_T ssize_t;
113 
114 #ifdef XCOM_STANDALONE
115 #ifndef UINT64_MAX
116 #define UINT64_MAX _UI64_MAX
117 #endif #
118 #ifndef INT64_MAX
119 #define INT64_MAX _I64_MAX
120 #endif #
121 #ifndef INT64_MIN
122 #define INT64_MIN _I64_MIN
123 #endif #
124 #ifndef UINT32_MAX
125 #define UINT32_MAX _UI32_MAX
126 #endif #
127 #ifndef INT32_MAX
128 #define INT32_MAX _I32_MAX
129 #endif #
130 #ifndef INT32_MIN
131 #define INT32_MIN _I32_MIN
132 #endif #
133 #ifndef UINT16_MAX
134 #define UINT16_MAX _UI16_MAX
135 #endif #
136 #ifndef INT16_MAX
137 #define INT16_MAX _I16_MAX
138 #endif #
139 #ifndef INT16_MIN
140 #define INT16_MIN _I16_MIN
141 #endif #
142 
143 #ifndef HAVE_STDINT_H
144 #ifdef _LP64
145 #define PRIu64 "lu"
146 #else /* _ILP32 */
147 #define PRIu64 "llu"
148 #endif
149 #endif
150 
151 #endif // XCOM_STANDALONE
152 
153 #define XCOM_O_CREAT _O_CREAT
154 #define XCOM_O_WRONLY _O_WRONLY
155 #define XCOM_O_APPEND _O_APPEND
156 #define XCOM_O_LARGEFILE 0
157 #define XCOM_O_EXCL _O_EXCL
158 #define XCOM_O_RDWR _O_RDWR
159 #define XCOM_O_RDONLY _O_RDONLY
160 #define XCOM_O_RSYNC 0
161 #define XCOM_O_BINARY _O_BINARY
162 
163 /* Only global flags can be set on Windows */
164 #define XCOM_S_IRUSR _S_IREAD
165 #define XCOM_S_IWUSR _S_IWRITE
166 #define XCOM_S_IRGRP 0
167 #define XCOM_S_IWGRP 0
168 #define XCOM_S_IROTH 0
169 #define XCOM_S_IWOTH 0
170 
171 #define XCOM_FILE_WRITE_MODE "w"
172 
173 /**
174  * Process exitcode macros.
175  *
176  * EXCEPTION_ or STATUS_ exits on windows start with 0xCxxxxxxx. The below
177  * attempts to use 2nd highest bit to determine if normal or error exit.
178  *
179  * These macros are normally found on *ix systems, and is used the the
180  * xcom_exit_report message internally in the agent.
181  *
182  * NOTE:
183  * Macros must handle -1 from os_process SM explicitly to mimic *ix
184  */
185 
186 #define WEXITSTATUS(s) ((int)(((s) == -1 ? 0 : (s)&0x40000000)))
187 #define WIFEXITED(s) (((s) == -1 ? 0 : (s)&0x40000000) == 0)
188 #define WIFSIGNALED(s) (((s) == -1 ? 0 : (s)&0x40000000) != 0)
189 #define WTERMSIG(s) ((s) == -1 ? 0 : (s)&0x3FFFFFFF)
190 #define WCOREDUMP(s) (((s) == CORE_EXIT_VALUE_WIN32) ? TRUE : FALSE)
191 
192 typedef int mode_t;
193 typedef SSIZE_T ssize_t;
194 
195 #define XCOM_CLRSYSERR (errno = 0, WSASetLastError(0))
196 #define XCOM_ISSYSERR (errno != 0 || WSAGetLastError() != 0)
197 #define XCOM_SYSERRNUM (WSAGetLastError() ? WSAGetLastError() : (errno))
198 #define XCOM_SYSERRSTR \
199  (WSAGetLastError() ? g_strerror(WSAGetLastError()) : g_strerror(errno))
200 #define xcom_gmtime_r(time, res) gmtime_s(res, time)
201 #define xcom_localtime_r(time, res) localtime_s(res, time)
202 
203 static inline void thread_yield() { SwitchToThread(); }
204 #else /* defined (_WIN32)*/
205 
206 #include <fcntl.h>
207 #include <sched.h>
208 #include <string.h>
209 #include <strings.h>
210 #include <unistd.h>
211 
212 #ifndef O_LARGEFILE
213 #define O_LARGEFILE 0
214 #endif
215 
216 /* TODO: Decide in one prefix for system-independent names (xcom or
217  xcom). Or perhaps really something generic that doesn't depend on
218  the current product name */
219 #define xcom_close(fd) close(fd)
220 #define xcom_fclose(f) fclose(f)
221 #define xcom_chdir(s) chdir(s)
222 #define xcom_dup(s) dup(s)
223 #define xcom_dup2(s, t) dup2(s, t)
224 #define xcom_lseek(fd, of, h) lseek(fd, of, h)
225 #define xcom_open(n, f, m) open(n, f, m)
226 #define xcom_fopen(n, m) fopen(n, m)
227 #define xcom_fstat(f, b) fstat(f, b)
228 
229 #define XCOM_F_TLOCK F_TLOCK
230 #define xcom_lockf(fd, f, sz) lockf(fd, f, sz)
231 
232 #define xcom_lrand48() lrand48()
233 #define xcom_srand48(x) srand48(x)
234 #define xcom_drand48() drand48()
235 
236 #define xcom_write(fd, buf, l) write(fd, buf, l)
237 #define xcom_read(fd, buf, c) read(fd, buf, c)
238 #define xcom_execv execv
239 
240 #define xcom_mktemp(s) mktemp(s)
241 
242 /* WARNING: snprintf() != _snprintf() */
243 /* #define xcom_snprintf(argv) snprintf(argv) */
244 #define xcom_strdup(x) strdup(x)
245 
246 #define xcom_strtok(b, d, c) strtok_r(b, d, c)
247 
248 #define xcom_strcasecmp(a, b) strcasecmp(a, b)
249 
250 #define xcom_g_io_channel_socket_new g_io_channel_unix_new
251 
252 /** Posix states that the optval argument should be (const) void*, but
253  on Windows it is (const) char* and since we normally pass in int*
254  a cast is required on Windows.
255 
256  With this typedef we can call xsockopt functions uniformly across
257  platorms, allbeit with an unnecessary cast to void* on *nix. */
258 typedef void *xcom_sockoptptr_t;
259 
260 #define XCOM_O_CREAT O_CREAT
261 #define XCOM_O_WRONLY O_WRONLY
262 #define XCOM_O_APPEND O_APPEND
263 #define XCOM_O_LARGEFILE O_LARGEFILE
264 #define XCOM_O_EXCL O_EXCL
265 #define XCOM_O_RDWR O_RDWR
266 #define XCOM_O_RDONLY O_RDONLY
267 #define XCOM_O_RSYNC O_RSYNC
268 #define XCOM_O_BINARY 0 /** Empty define - applicable WIN32 only */
269 
270 #define XCOM_S_IRUSR S_IRUSR
271 #define XCOM_S_IWUSR S_IWUSR
272 #define XCOM_S_IRGRP S_IRGRP
273 #define XCOM_S_IWGRP S_IWGRP
274 #define XCOM_S_IROTH S_IROTH
275 #define XCOM_S_IWOTH S_IWOTH
276 
277 #define XCOM_FILE_WRITE_MODE "wF"
278 
279 #define SOCKET_ERROR -1
280 
281 #define XCOM_CLRSYSERR errno = 0
282 #define XCOM_ISSYSERR (errno != 0)
283 #define XCOM_SYSERRNUM ((errno) + 0)
284 #define XCOM_SYSERRSTR g_strerror(errno)
285 
286 #define xcom_gmtime_r(time, res) gmtime_r(time, res)
287 #define xcom_localtime_r(time, res) localtime_r(time, res)
288 
289 #define thread_yield sched_yield
290 
291 #endif /* defined (_WIN32) */
292 
293 #ifdef _WIN32
294 #define NEWLINE "\r\n"
295 #else
296 #define NEWLINE "\n"
297 #endif
298 
299 #ifdef __cplusplus
300 }
301 #endif
302 
303 #endif
types.h
string.h
xcom_sockoptptr_t
void * xcom_sockoptptr_t
Posix states that the optval argument should be (const) void*, but on Windows it is (const) char* and...
Definition: x_platform.h:258
thread_yield
#define thread_yield
Definition: x_platform.h:289