Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Utilities / cmlibuv / src / unix / internal.h
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #ifndef UV_UNIX_INTERNAL_H_
23 #define UV_UNIX_INTERNAL_H_
24
25 #include "uv-common.h"
26
27 #include <assert.h>
28 #include <limits.h> /* _POSIX_PATH_MAX, PATH_MAX */
29 #include <stdlib.h> /* abort */
30 #include <string.h> /* strrchr */
31 #include <fcntl.h>  /* O_CLOEXEC and O_NONBLOCK, if supported. */
32 #include <stdio.h>
33 #include <errno.h>
34 #include <sys/socket.h>
35
36 #if defined(__STRICT_ANSI__)
37 # define inline __inline
38 #endif
39
40 #if defined(__linux__)
41 # include "linux-syscalls.h"
42 #endif /* __linux__ */
43
44 #if defined(__MVS__)
45 # include "os390-syscalls.h"
46 #endif /* __MVS__ */
47
48 #if defined(__sun)
49 # include <sys/port.h>
50 # include <port.h>
51 #endif /* __sun */
52
53 #if defined(_AIX)
54 # define reqevents events
55 # define rtnevents revents
56 # include <sys/poll.h>
57 #else
58 # include <poll.h>
59 #endif /* _AIX */
60
61 #if defined(__APPLE__) && !TARGET_OS_IPHONE
62 # include <AvailabilityMacros.h>
63 #endif
64
65 /*
66  * Define common detection for active Thread Sanitizer
67  * - clang uses __has_feature(thread_sanitizer)
68  * - gcc-7+ uses __SANITIZE_THREAD__
69  */
70 #if defined(__has_feature)
71 # if __has_feature(thread_sanitizer)
72 #  define __SANITIZE_THREAD__ 1
73 # endif
74 #endif
75
76 #if defined(PATH_MAX)
77 # define UV__PATH_MAX PATH_MAX
78 #else
79 # define UV__PATH_MAX 8192
80 #endif
81
82 #if defined(CMAKE_BOOTSTRAP)
83 # undef pthread_atfork
84 # define pthread_atfork(prepare, parent, child) \
85      uv__pthread_atfork(prepare, parent, child)
86 int uv__pthread_atfork(void (*prepare)(void), void (*parent)(void),
87                        void (*child)(void));
88 # undef pthread_sigmask
89 # define pthread_sigmask(how, set, oldset) \
90      uv__pthread_sigmask(how, set, oldset)
91 int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset);
92 #elif defined(__ANDROID__)
93 int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset);
94 # ifdef pthread_sigmask
95 # undef pthread_sigmask
96 # endif
97 # define pthread_sigmask(how, set, oldset) uv__pthread_sigmask(how, set, oldset)
98 #endif
99
100 #define ACCESS_ONCE(type, var)                                                \
101   (*(volatile type*) &(var))
102
103 #define ROUND_UP(a, b)                                                        \
104   ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
105
106 #define UNREACHABLE()                                                         \
107   do {                                                                        \
108     assert(0 && "unreachable code");                                          \
109     abort();                                                                  \
110   }                                                                           \
111   while (0)
112
113 #define SAVE_ERRNO(block)                                                     \
114   do {                                                                        \
115     int _saved_errno = errno;                                                 \
116     do { block; } while (0);                                                  \
117     errno = _saved_errno;                                                     \
118   }                                                                           \
119   while (0)
120
121 /* The __clang__ and __INTEL_COMPILER checks are superfluous because they
122  * define __GNUC__. They are here to convey to you, dear reader, that these
123  * macros are enabled when compiling with clang or icc.
124  */
125 #if defined(__clang__) ||                                                     \
126     defined(__GNUC__) ||                                                      \
127     defined(__INTEL_COMPILER)
128 # define UV_UNUSED(declaration)     __attribute__((unused)) declaration
129 #else
130 # define UV_UNUSED(declaration)     declaration
131 #endif
132
133 /* Leans on the fact that, on Linux, POLLRDHUP == EPOLLRDHUP. */
134 #ifdef POLLRDHUP
135 # define UV__POLLRDHUP POLLRDHUP
136 #else
137 # define UV__POLLRDHUP 0x2000
138 #endif
139
140 #ifdef POLLPRI
141 # define UV__POLLPRI POLLPRI
142 #else
143 # define UV__POLLPRI 0
144 #endif
145
146 #if !defined(O_CLOEXEC) && defined(__FreeBSD__)
147 /*
148  * It may be that we are just missing `__POSIX_VISIBLE >= 200809`.
149  * Try using fixed value const and give up, if it doesn't work
150  */
151 # define O_CLOEXEC 0x00100000
152 #endif
153
154 typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
155
156 /* loop flags */
157 enum {
158   UV_LOOP_BLOCK_SIGPROF = 0x1,
159   UV_LOOP_REAP_CHILDREN = 0x2
160 };
161
162 /* flags of excluding ifaddr */
163 enum {
164   UV__EXCLUDE_IFPHYS,
165   UV__EXCLUDE_IFADDR
166 };
167
168 typedef enum {
169   UV_CLOCK_PRECISE = 0,  /* Use the highest resolution clock available. */
170   UV_CLOCK_FAST = 1      /* Use the fastest clock with <= 1ms granularity. */
171 } uv_clocktype_t;
172
173 struct uv__stream_queued_fds_s {
174   unsigned int size;
175   unsigned int offset;
176   int fds[1];
177 };
178
179
180 #if defined(_AIX) || \
181     defined(__APPLE__) || \
182     defined(__DragonFly__) || \
183     defined(__FreeBSD__) || \
184     defined(__FreeBSD_kernel__) || \
185     defined(__linux__) || \
186     defined(__OpenBSD__) || \
187     defined(__NetBSD__)
188 #define uv__nonblock uv__nonblock_ioctl
189 #define UV__NONBLOCK_IS_IOCTL 1
190 #else
191 #define uv__nonblock uv__nonblock_fcntl
192 #define UV__NONBLOCK_IS_IOCTL 0
193 #endif
194
195 /* On Linux, uv__nonblock_fcntl() and uv__nonblock_ioctl() do not commute
196  * when O_NDELAY is not equal to O_NONBLOCK.  Case in point: linux/sparc32
197  * and linux/sparc64, where O_NDELAY is O_NONBLOCK + another bit.
198  *
199  * Libuv uses uv__nonblock_fcntl() directly sometimes so ensure that it
200  * commutes with uv__nonblock().
201  */
202 #if defined(__linux__) && O_NDELAY != O_NONBLOCK
203 #undef uv__nonblock
204 #define uv__nonblock uv__nonblock_fcntl
205 #endif
206
207 /* core */
208 int uv__cloexec(int fd, int set);
209 int uv__nonblock_ioctl(int fd, int set);
210 int uv__nonblock_fcntl(int fd, int set);
211 int uv__close(int fd); /* preserves errno */
212 int uv__close_nocheckstdio(int fd);
213 int uv__close_nocancel(int fd);
214 int uv__socket(int domain, int type, int protocol);
215 ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags);
216 void uv__make_close_pending(uv_handle_t* handle);
217 int uv__getiovmax(void);
218
219 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
220 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
221 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
222 void uv__io_close(uv_loop_t* loop, uv__io_t* w);
223 void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
224 int uv__io_active(const uv__io_t* w, unsigned int events);
225 int uv__io_check_fd(uv_loop_t* loop, int fd);
226 void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
227 int uv__io_fork(uv_loop_t* loop);
228 int uv__fd_exists(uv_loop_t* loop, int fd);
229
230 /* async */
231 void uv__async_stop(uv_loop_t* loop);
232 int uv__async_fork(uv_loop_t* loop);
233
234
235 /* loop */
236 void uv__run_idle(uv_loop_t* loop);
237 void uv__run_check(uv_loop_t* loop);
238 void uv__run_prepare(uv_loop_t* loop);
239
240 /* stream */
241 void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
242     uv_handle_type type);
243 int uv__stream_open(uv_stream_t*, int fd, int flags);
244 void uv__stream_destroy(uv_stream_t* stream);
245 #if defined(__APPLE__)
246 int uv__stream_try_select(uv_stream_t* stream, int* fd);
247 #endif /* defined(__APPLE__) */
248 void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
249 int uv__accept(int sockfd);
250 int uv__dup2_cloexec(int oldfd, int newfd);
251 int uv__open_cloexec(const char* path, int flags);
252 int uv__slurp(const char* filename, char* buf, size_t len);
253
254 /* tcp */
255 int uv__tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
256 int uv__tcp_nodelay(int fd, int on);
257 int uv__tcp_keepalive(int fd, int on, unsigned int delay);
258
259 /* pipe */
260 int uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
261
262 /* signal */
263 void uv__signal_close(uv_signal_t* handle);
264 void uv__signal_global_once_init(void);
265 void uv__signal_loop_cleanup(uv_loop_t* loop);
266 int uv__signal_loop_fork(uv_loop_t* loop);
267
268 /* platform specific */
269 uint64_t uv__hrtime(uv_clocktype_t type);
270 int uv__kqueue_init(uv_loop_t* loop);
271 int uv__epoll_init(uv_loop_t* loop);
272 int uv__platform_loop_init(uv_loop_t* loop);
273 void uv__platform_loop_delete(uv_loop_t* loop);
274 void uv__platform_invalidate_fd(uv_loop_t* loop, int fd);
275
276 /* various */
277 void uv__async_close(uv_async_t* handle);
278 void uv__check_close(uv_check_t* handle);
279 void uv__fs_event_close(uv_fs_event_t* handle);
280 void uv__idle_close(uv_idle_t* handle);
281 void uv__pipe_close(uv_pipe_t* handle);
282 void uv__poll_close(uv_poll_t* handle);
283 void uv__prepare_close(uv_prepare_t* handle);
284 void uv__process_close(uv_process_t* handle);
285 void uv__stream_close(uv_stream_t* handle);
286 void uv__tcp_close(uv_tcp_t* handle);
287 size_t uv__thread_stack_size(void);
288 void uv__udp_close(uv_udp_t* handle);
289 void uv__udp_finish_close(uv_udp_t* handle);
290 FILE* uv__open_file(const char* path);
291 int uv__getpwuid_r(uv_passwd_t* pwd);
292 int uv__search_path(const char* prog, char* buf, size_t* buflen);
293 void uv__wait_children(uv_loop_t* loop);
294
295 /* random */
296 int uv__random_devurandom(void* buf, size_t buflen);
297 int uv__random_getrandom(void* buf, size_t buflen);
298 int uv__random_getentropy(void* buf, size_t buflen);
299 int uv__random_readpath(const char* path, void* buf, size_t buflen);
300 int uv__random_sysctl(void* buf, size_t buflen);
301
302 #if defined(__APPLE__) && !defined(CMAKE_BOOTSTRAP)
303 int uv___stream_fd(const uv_stream_t* handle);
304 #define uv__stream_fd(handle) (uv___stream_fd((const uv_stream_t*) (handle)))
305 #else
306 #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
307 #endif /* defined(__APPLE__) */
308
309 int uv__make_pipe(int fds[2], int flags);
310
311 #if defined(__APPLE__)
312
313 int uv__fsevents_init(uv_fs_event_t* handle);
314 int uv__fsevents_close(uv_fs_event_t* handle);
315 void uv__fsevents_loop_delete(uv_loop_t* loop);
316
317 #endif /* defined(__APPLE__) */
318
319 UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) {
320   /* Use a fast time source if available.  We only need millisecond precision.
321    */
322   loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
323 }
324
325 UV_UNUSED(static char* uv__basename_r(const char* path)) {
326   char* s;
327
328   s = strrchr(path, '/');
329   if (s == NULL)
330     return (char*) path;
331
332   return s + 1;
333 }
334
335 #if defined(__linux__)
336 int uv__inotify_fork(uv_loop_t* loop, void* old_watchers);
337 #endif
338
339 typedef int (*uv__peersockfunc)(int, struct sockaddr*, socklen_t*);
340
341 int uv__getsockpeername(const uv_handle_t* handle,
342                         uv__peersockfunc func,
343                         struct sockaddr* name,
344                         int* namelen);
345
346 #if defined(__linux__)            ||                                      \
347     defined(__FreeBSD__)          ||                                      \
348     defined(__FreeBSD_kernel__)   ||                                       \
349     defined(__DragonFly__)
350 #define HAVE_MMSG 1
351 struct uv__mmsghdr {
352   struct msghdr msg_hdr;
353   unsigned int msg_len;
354 };
355
356 int uv__recvmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen);
357 int uv__sendmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen);
358 #else
359 #define HAVE_MMSG 0
360 #endif
361
362 #if defined(__sun)
363 #if !defined(_POSIX_VERSION) || _POSIX_VERSION < 200809L
364 size_t strnlen(const char* s, size_t maxlen);
365 #endif
366 #endif
367
368 #if defined(__FreeBSD__)
369 ssize_t
370 uv__fs_copy_file_range(int fd_in,
371                        off_t* off_in,
372                        int fd_out,
373                        off_t* off_out,
374                        size_t len,
375                        unsigned int flags);
376 #endif
377
378
379 #endif /* UV_UNIX_INTERNAL_H_ */