1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 #ifndef UV_UNIX_INTERNAL_H_
23 #define UV_UNIX_INTERNAL_H_
25 #include "uv-common.h"
28 #include <stdlib.h> /* abort */
29 #include <string.h> /* strrchr */
30 #include <fcntl.h> /* O_CLOEXEC, may be */
33 #if defined(__STRICT_ANSI__)
34 # define inline __inline
37 #if defined(__linux__)
38 # include "linux-syscalls.h"
39 #endif /* __linux__ */
42 # include "os390-syscalls.h"
46 # include <sys/port.h>
51 # define reqevents events
52 # define rtnevents revents
53 # include <sys/poll.h>
58 #if defined(__APPLE__) && !TARGET_OS_IPHONE
59 # include <AvailabilityMacros.h>
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),
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
77 # define pthread_sigmask(how, set, oldset) uv__pthread_sigmask(how, set, oldset)
80 #define ACCESS_ONCE(type, var) \
81 (*(volatile type*) &(var))
83 #define ROUND_UP(a, b) \
84 ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
86 #define UNREACHABLE() \
88 assert(0 && "unreachable code"); \
93 #define SAVE_ERRNO(block) \
95 int _saved_errno = errno; \
96 do { block; } while (0); \
97 errno = _saved_errno; \
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.
105 #if defined(__clang__) || \
106 defined(__GNUC__) || \
107 defined(__INTEL_COMPILER) || \
109 # define UV_DESTRUCTOR(declaration) __attribute__((destructor)) declaration
110 # define UV_UNUSED(declaration) __attribute__((unused)) declaration
112 # define UV_DESTRUCTOR(declaration) declaration
113 # define UV_UNUSED(declaration) declaration
116 /* Leans on the fact that, on Linux, POLLRDHUP == EPOLLRDHUP. */
118 # define UV__POLLRDHUP POLLRDHUP
120 # define UV__POLLRDHUP 0x2000
124 # define UV__POLLPRI POLLPRI
126 # define UV__POLLPRI 0
129 #if !defined(O_CLOEXEC) && defined(__FreeBSD__)
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
134 # define O_CLOEXEC 0x00100000
137 typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
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 */
161 UV_LOOP_BLOCK_SIGPROF = 1
164 /* flags of excluding ifaddr */
171 UV_CLOCK_PRECISE = 0, /* Use the highest resolution clock available. */
172 UV_CLOCK_FAST = 1 /* Use the fastest clock with <= 1ms granularity. */
175 struct uv__stream_queued_fds_s {
182 #if defined(_AIX) || \
183 defined(__APPLE__) || \
184 defined(__DragonFly__) || \
185 defined(__FreeBSD__) || \
186 defined(__FreeBSD_kernel__) || \
187 defined(__linux__) || \
188 defined(__OpenBSD__) || \
190 #define uv__cloexec uv__cloexec_ioctl
191 #define uv__nonblock uv__nonblock_ioctl
193 #define uv__cloexec uv__cloexec_fcntl
194 #define uv__nonblock uv__nonblock_fcntl
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);
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);
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);
221 void uv__async_stop(uv_loop_t* loop);
222 int uv__async_fork(uv_loop_t* 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);
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);
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);
249 int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
252 void uv__run_timers(uv_loop_t* loop);
253 int uv__next_timeout(const uv_loop_t* loop);
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);
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);
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);
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)))
291 #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
292 #endif /* defined(__APPLE__) */
294 #ifdef UV__O_NONBLOCK
295 # define UV__F_NONBLOCK UV__O_NONBLOCK
297 # define UV__F_NONBLOCK 1
300 int uv__make_socketpair(int fds[2], int flags);
301 int uv__make_pipe(int fds[2], int flags);
303 #if defined(__APPLE__)
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);
309 /* OSX < 10.7 has no file events, polyfill them */
310 #ifndef MAC_OS_X_VERSION_10_7
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;
325 #endif /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070 */
327 #endif /* defined(__APPLE__) */
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.
332 loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
335 UV_UNUSED(static char* uv__basename_r(const char* path)) {
338 s = strrchr(path, '/');
345 #if defined(__linux__)
346 int uv__inotify_fork(uv_loop_t* loop, void* old_watchers);
349 #endif /* UV_UNIX_INTERNAL_H_ */