9cc87f0caa1204d54f4b3c76bffaf8c4f42f3b23
[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 <stdlib.h> /* abort */
29 #include <string.h> /* strrchr */
30 #include <fcntl.h>  /* O_CLOEXEC, may be */
31 #include <stdio.h>
32
33 #if defined(__STRICT_ANSI__)
34 # define inline __inline
35 #endif
36
37 #if defined(__linux__)
38 # include "linux-syscalls.h"
39 #endif /* __linux__ */
40
41 #if defined(__MVS__)
42 # include "os390-syscalls.h"
43 #endif /* __MVS__ */
44
45 #if defined(__sun)
46 # include <sys/port.h>
47 # include <port.h>
48 #endif /* __sun */
49
50 #if defined(_AIX)
51 # define reqevents events
52 # define rtnevents revents
53 # include <sys/poll.h>
54 #else
55 # include <poll.h>
56 #endif /* _AIX */
57
58 #if defined(__APPLE__) && !TARGET_OS_IPHONE
59 # include <AvailabilityMacros.h>
60 #endif
61
62 #if defined(CMAKE_BOOTSTRAP)
63 # undef pthread_atfork
64 # define pthread_atfork(prepare, parent, child) \
65      uv__pthread_atfork(prepare, parent, child)
66 int uv__pthread_atfork(void (*prepare)(void), void (*parent)(void),
67                        void (*child)(void));
68 # undef pthread_sigmask
69 # define pthread_sigmask(how, set, oldset) \
70      uv__pthread_sigmask(how, set, oldset)
71 int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset);
72 #elif defined(__ANDROID__)
73 int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset);
74 # ifdef pthread_sigmask
75 # undef pthread_sigmask
76 # endif
77 # define pthread_sigmask(how, set, oldset) uv__pthread_sigmask(how, set, oldset)
78 #endif
79
80 #define ACCESS_ONCE(type, var)                                                \
81   (*(volatile type*) &(var))
82
83 #define ROUND_UP(a, b)                                                        \
84   ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
85
86 #define UNREACHABLE()                                                         \
87   do {                                                                        \
88     assert(0 && "unreachable code");                                          \
89     abort();                                                                  \
90   }                                                                           \
91   while (0)
92
93 #define SAVE_ERRNO(block)                                                     \
94   do {                                                                        \
95     int _saved_errno = errno;                                                 \
96     do { block; } while (0);                                                  \
97     errno = _saved_errno;                                                     \
98   }                                                                           \
99   while (0)
100
101 /* The __clang__ and __INTEL_COMPILER checks are superfluous because they
102  * define __GNUC__. They are here to convey to you, dear reader, that these
103  * macros are enabled when compiling with clang or icc.
104  */
105 #if defined(__clang__) ||                                                     \
106     defined(__GNUC__) ||                                                      \
107     defined(__INTEL_COMPILER) ||                                              \
108     defined(__SUNPRO_C)
109 # define UV_DESTRUCTOR(declaration) __attribute__((destructor)) declaration
110 # define UV_UNUSED(declaration)     __attribute__((unused)) declaration
111 #else
112 # define UV_DESTRUCTOR(declaration) declaration
113 # define UV_UNUSED(declaration)     declaration
114 #endif
115
116 /* Leans on the fact that, on Linux, POLLRDHUP == EPOLLRDHUP. */
117 #ifdef POLLRDHUP
118 # define UV__POLLRDHUP POLLRDHUP
119 #else
120 # define UV__POLLRDHUP 0x2000
121 #endif
122
123 #ifdef POLLPRI
124 # define UV__POLLPRI POLLPRI
125 #else
126 # define UV__POLLPRI 0
127 #endif
128
129 #if !defined(O_CLOEXEC) && defined(__FreeBSD__)
130 /*
131  * It may be that we are just missing `__POSIX_VISIBLE >= 200809`.
132  * Try using fixed value const and give up, if it doesn't work
133  */
134 # define O_CLOEXEC 0x00100000
135 #endif
136
137 typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
138
139 /* handle flags */
140 enum {
141   UV_CLOSING              = 0x01,   /* uv_close() called but not finished. */
142   UV_CLOSED               = 0x02,   /* close(2) finished. */
143   UV_STREAM_READING       = 0x04,   /* uv_read_start() called. */
144   UV_STREAM_SHUTTING      = 0x08,   /* uv_shutdown() called but not complete. */
145   UV_STREAM_SHUT          = 0x10,   /* Write side closed. */
146   UV_STREAM_READABLE      = 0x20,   /* The stream is readable */
147   UV_STREAM_WRITABLE      = 0x40,   /* The stream is writable */
148   UV_STREAM_BLOCKING      = 0x80,   /* Synchronous writes. */
149   UV_STREAM_READ_PARTIAL  = 0x100,  /* read(2) read less than requested. */
150   UV_STREAM_READ_EOF      = 0x200,  /* read(2) read EOF. */
151   UV_TCP_NODELAY          = 0x400,  /* Disable Nagle. */
152   UV_TCP_KEEPALIVE        = 0x800,  /* Turn on keep-alive. */
153   UV_TCP_SINGLE_ACCEPT    = 0x1000, /* Only accept() when idle. */
154   UV_HANDLE_IPV6          = 0x10000, /* Handle is bound to a IPv6 socket. */
155   UV_UDP_PROCESSING       = 0x20000, /* Handle is running the send callback queue. */
156   UV_HANDLE_BOUND         = 0x40000  /* Handle is bound to an address and port */
157 };
158
159 /* loop flags */
160 enum {
161   UV_LOOP_BLOCK_SIGPROF = 1
162 };
163
164 /* flags of excluding ifaddr */
165 enum {
166   UV__EXCLUDE_IFPHYS,
167   UV__EXCLUDE_IFADDR
168 };
169
170 typedef enum {
171   UV_CLOCK_PRECISE = 0,  /* Use the highest resolution clock available. */
172   UV_CLOCK_FAST = 1      /* Use the fastest clock with <= 1ms granularity. */
173 } uv_clocktype_t;
174
175 struct uv__stream_queued_fds_s {
176   unsigned int size;
177   unsigned int offset;
178   int fds[1];
179 };
180
181
182 #if defined(_AIX) || \
183     defined(__APPLE__) || \
184     defined(__DragonFly__) || \
185     defined(__FreeBSD__) || \
186     defined(__FreeBSD_kernel__) || \
187     defined(__linux__) || \
188     defined(__OpenBSD__) || \
189     defined(__NetBSD__)
190 #define uv__cloexec uv__cloexec_ioctl
191 #define uv__nonblock uv__nonblock_ioctl
192 #else
193 #define uv__cloexec uv__cloexec_fcntl
194 #define uv__nonblock uv__nonblock_fcntl
195 #endif
196
197 /* core */
198 int uv__cloexec_ioctl(int fd, int set);
199 int uv__cloexec_fcntl(int fd, int set);
200 int uv__nonblock_ioctl(int fd, int set);
201 int uv__nonblock_fcntl(int fd, int set);
202 int uv__close(int fd);
203 int uv__close_nocheckstdio(int fd);
204 int uv__socket(int domain, int type, int protocol);
205 int uv__dup(int fd);
206 ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags);
207 void uv__make_close_pending(uv_handle_t* handle);
208 int uv__getiovmax(void);
209
210 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
211 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
212 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
213 void uv__io_close(uv_loop_t* loop, uv__io_t* w);
214 void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
215 int uv__io_active(const uv__io_t* w, unsigned int events);
216 int uv__io_check_fd(uv_loop_t* loop, int fd);
217 void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
218 int uv__io_fork(uv_loop_t* loop);
219
220 /* async */
221 void uv__async_stop(uv_loop_t* loop);
222 int uv__async_fork(uv_loop_t* loop);
223
224
225 /* loop */
226 void uv__run_idle(uv_loop_t* loop);
227 void uv__run_check(uv_loop_t* loop);
228 void uv__run_prepare(uv_loop_t* loop);
229
230 /* stream */
231 void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
232     uv_handle_type type);
233 int uv__stream_open(uv_stream_t*, int fd, int flags);
234 void uv__stream_destroy(uv_stream_t* stream);
235 #if defined(__APPLE__)
236 int uv__stream_try_select(uv_stream_t* stream, int* fd);
237 #endif /* defined(__APPLE__) */
238 void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
239 int uv__accept(int sockfd);
240 int uv__dup2_cloexec(int oldfd, int newfd);
241 int uv__open_cloexec(const char* path, int flags);
242
243 /* tcp */
244 int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
245 int uv__tcp_nodelay(int fd, int on);
246 int uv__tcp_keepalive(int fd, int on, unsigned int delay);
247
248 /* pipe */
249 int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
250
251 /* timer */
252 void uv__run_timers(uv_loop_t* loop);
253 int uv__next_timeout(const uv_loop_t* loop);
254
255 /* signal */
256 void uv__signal_close(uv_signal_t* handle);
257 void uv__signal_global_once_init(void);
258 void uv__signal_loop_cleanup(uv_loop_t* loop);
259 int uv__signal_loop_fork(uv_loop_t* loop);
260
261 /* platform specific */
262 uint64_t uv__hrtime(uv_clocktype_t type);
263 int uv__kqueue_init(uv_loop_t* loop);
264 int uv__platform_loop_init(uv_loop_t* loop);
265 void uv__platform_loop_delete(uv_loop_t* loop);
266 void uv__platform_invalidate_fd(uv_loop_t* loop, int fd);
267
268 /* various */
269 void uv__async_close(uv_async_t* handle);
270 void uv__check_close(uv_check_t* handle);
271 void uv__fs_event_close(uv_fs_event_t* handle);
272 void uv__idle_close(uv_idle_t* handle);
273 void uv__pipe_close(uv_pipe_t* handle);
274 void uv__poll_close(uv_poll_t* handle);
275 void uv__prepare_close(uv_prepare_t* handle);
276 void uv__process_close(uv_process_t* handle);
277 void uv__stream_close(uv_stream_t* handle);
278 void uv__tcp_close(uv_tcp_t* handle);
279 void uv__timer_close(uv_timer_t* handle);
280 void uv__udp_close(uv_udp_t* handle);
281 void uv__udp_finish_close(uv_udp_t* handle);
282 uv_handle_type uv__handle_type(int fd);
283 FILE* uv__open_file(const char* path);
284 int uv__getpwuid_r(uv_passwd_t* pwd);
285
286
287 #if defined(__APPLE__) && !defined(CMAKE_BOOTSTRAP)
288 int uv___stream_fd(const uv_stream_t* handle);
289 #define uv__stream_fd(handle) (uv___stream_fd((const uv_stream_t*) (handle)))
290 #else
291 #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
292 #endif /* defined(__APPLE__) */
293
294 #ifdef UV__O_NONBLOCK
295 # define UV__F_NONBLOCK UV__O_NONBLOCK
296 #else
297 # define UV__F_NONBLOCK 1
298 #endif
299
300 int uv__make_socketpair(int fds[2], int flags);
301 int uv__make_pipe(int fds[2], int flags);
302
303 #if defined(__APPLE__)
304
305 int uv__fsevents_init(uv_fs_event_t* handle);
306 int uv__fsevents_close(uv_fs_event_t* handle);
307 void uv__fsevents_loop_delete(uv_loop_t* loop);
308
309 /* OSX < 10.7 has no file events, polyfill them */
310 #ifndef MAC_OS_X_VERSION_10_7
311
312 static const int kFSEventStreamCreateFlagFileEvents = 0x00000010;
313 static const int kFSEventStreamEventFlagItemCreated = 0x00000100;
314 static const int kFSEventStreamEventFlagItemRemoved = 0x00000200;
315 static const int kFSEventStreamEventFlagItemInodeMetaMod = 0x00000400;
316 static const int kFSEventStreamEventFlagItemRenamed = 0x00000800;
317 static const int kFSEventStreamEventFlagItemModified = 0x00001000;
318 static const int kFSEventStreamEventFlagItemFinderInfoMod = 0x00002000;
319 static const int kFSEventStreamEventFlagItemChangeOwner = 0x00004000;
320 static const int kFSEventStreamEventFlagItemXattrMod = 0x00008000;
321 static const int kFSEventStreamEventFlagItemIsFile = 0x00010000;
322 static const int kFSEventStreamEventFlagItemIsDir = 0x00020000;
323 static const int kFSEventStreamEventFlagItemIsSymlink = 0x00040000;
324
325 #endif /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070 */
326
327 #endif /* defined(__APPLE__) */
328
329 UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) {
330   /* Use a fast time source if available.  We only need millisecond precision.
331    */
332   loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
333 }
334
335 UV_UNUSED(static char* uv__basename_r(const char* path)) {
336   char* s;
337
338   s = strrchr(path, '/');
339   if (s == NULL)
340     return (char*) path;
341
342   return s + 1;
343 }
344
345 #if defined(__linux__)
346 int uv__inotify_fork(uv_loop_t* loop, void* old_watchers);
347 #endif
348
349 #endif /* UV_UNIX_INTERNAL_H_ */