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