xref: /netbsd-src/external/mit/libuv/dist/src/unix/internal.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
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 #if defined(_POSIX_PATH_MAX)
66 # define UV__PATH_MAX _POSIX_PATH_MAX
67 #elif defined(PATH_MAX)
68 # define UV__PATH_MAX PATH_MAX
69 #else
70 # define UV__PATH_MAX 8192
71 #endif
72 
73 #if defined(__ANDROID__)
74 int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset);
75 # ifdef pthread_sigmask
76 # undef pthread_sigmask
77 # endif
78 # define pthread_sigmask(how, set, oldset) uv__pthread_sigmask(how, set, oldset)
79 #endif
80 
81 #define ACCESS_ONCE(type, var)                                                \
82   (*(volatile type*) &(var))
83 
84 #define ROUND_UP(a, b)                                                        \
85   ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
86 
87 #define UNREACHABLE()                                                         \
88   do {                                                                        \
89     assert(0 && "unreachable code");                                          \
90     abort();                                                                  \
91   }                                                                           \
92   while (0)
93 
94 #define SAVE_ERRNO(block)                                                     \
95   do {                                                                        \
96     int _saved_errno = errno;                                                 \
97     do { block; } while (0);                                                  \
98     errno = _saved_errno;                                                     \
99   }                                                                           \
100   while (0)
101 
102 /* The __clang__ and __INTEL_COMPILER checks are superfluous because they
103  * define __GNUC__. They are here to convey to you, dear reader, that these
104  * macros are enabled when compiling with clang or icc.
105  */
106 #if defined(__clang__) ||                                                     \
107     defined(__GNUC__) ||                                                      \
108     defined(__INTEL_COMPILER)
109 # define UV_UNUSED(declaration)     __attribute__((unused)) declaration
110 #else
111 # define UV_UNUSED(declaration)     declaration
112 #endif
113 
114 /* Leans on the fact that, on Linux, POLLRDHUP == EPOLLRDHUP. */
115 #ifdef POLLRDHUP
116 # define UV__POLLRDHUP POLLRDHUP
117 #else
118 # define UV__POLLRDHUP 0x2000
119 #endif
120 
121 #ifdef POLLPRI
122 # define UV__POLLPRI POLLPRI
123 #else
124 # define UV__POLLPRI 0
125 #endif
126 
127 #if !defined(O_CLOEXEC) && defined(__FreeBSD__)
128 /*
129  * It may be that we are just missing `__POSIX_VISIBLE >= 200809`.
130  * Try using fixed value const and give up, if it doesn't work
131  */
132 # define O_CLOEXEC 0x00100000
133 #endif
134 
135 typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
136 
137 /* loop flags */
138 enum {
139   UV_LOOP_BLOCK_SIGPROF = 1
140 };
141 
142 /* flags of excluding ifaddr */
143 enum {
144   UV__EXCLUDE_IFPHYS,
145   UV__EXCLUDE_IFADDR
146 };
147 
148 typedef enum {
149   UV_CLOCK_PRECISE = 0,  /* Use the highest resolution clock available. */
150   UV_CLOCK_FAST = 1      /* Use the fastest clock with <= 1ms granularity. */
151 } uv_clocktype_t;
152 
153 struct uv__stream_queued_fds_s {
154   unsigned int size;
155   unsigned int offset;
156   int fds[1];
157 };
158 
159 
160 #if defined(_AIX) || \
161     defined(__APPLE__) || \
162     defined(__DragonFly__) || \
163     defined(__FreeBSD__) || \
164     defined(__FreeBSD_kernel__) || \
165     defined(__linux__) || \
166     defined(__OpenBSD__) || \
167     defined(__NetBSD__)
168 #define uv__cloexec uv__cloexec_ioctl
169 #define uv__nonblock uv__nonblock_ioctl
170 #else
171 #define uv__cloexec uv__cloexec_fcntl
172 #define uv__nonblock uv__nonblock_fcntl
173 #endif
174 
175 /* On Linux, uv__nonblock_fcntl() and uv__nonblock_ioctl() do not commute
176  * when O_NDELAY is not equal to O_NONBLOCK.  Case in point: linux/sparc32
177  * and linux/sparc64, where O_NDELAY is O_NONBLOCK + another bit.
178  *
179  * Libuv uses uv__nonblock_fcntl() directly sometimes so ensure that it
180  * commutes with uv__nonblock().
181  */
182 #if defined(__linux__) && O_NDELAY != O_NONBLOCK
183 #undef uv__nonblock
184 #define uv__nonblock uv__nonblock_fcntl
185 #endif
186 
187 /* core */
188 int uv__cloexec_ioctl(int fd, int set);
189 int uv__cloexec_fcntl(int fd, int set);
190 int uv__nonblock_ioctl(int fd, int set);
191 int uv__nonblock_fcntl(int fd, int set);
192 int uv__close(int fd); /* preserves errno */
193 int uv__close_nocheckstdio(int fd);
194 int uv__close_nocancel(int fd);
195 int uv__socket(int domain, int type, int protocol);
196 ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags);
197 void uv__make_close_pending(uv_handle_t* handle);
198 int uv__getiovmax(void);
199 
200 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
201 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
202 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
203 void uv__io_close(uv_loop_t* loop, uv__io_t* w);
204 void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
205 int uv__io_active(const uv__io_t* w, unsigned int events);
206 int uv__io_check_fd(uv_loop_t* loop, int fd);
207 void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
208 int uv__io_fork(uv_loop_t* loop);
209 int uv__fd_exists(uv_loop_t* loop, int fd);
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 /* signal */
243 void uv__signal_close(uv_signal_t* handle);
244 void uv__signal_global_once_init(void);
245 void uv__signal_loop_cleanup(uv_loop_t* loop);
246 int uv__signal_loop_fork(uv_loop_t* loop);
247 
248 /* platform specific */
249 uint64_t uv__hrtime(uv_clocktype_t type);
250 int uv__kqueue_init(uv_loop_t* loop);
251 int uv__platform_loop_init(uv_loop_t* loop);
252 void uv__platform_loop_delete(uv_loop_t* loop);
253 void uv__platform_invalidate_fd(uv_loop_t* loop, int fd);
254 
255 /* various */
256 void uv__async_close(uv_async_t* handle);
257 void uv__check_close(uv_check_t* handle);
258 void uv__fs_event_close(uv_fs_event_t* handle);
259 void uv__idle_close(uv_idle_t* handle);
260 void uv__pipe_close(uv_pipe_t* handle);
261 void uv__poll_close(uv_poll_t* handle);
262 void uv__prepare_close(uv_prepare_t* handle);
263 void uv__process_close(uv_process_t* handle);
264 void uv__stream_close(uv_stream_t* handle);
265 void uv__tcp_close(uv_tcp_t* handle);
266 void uv__udp_close(uv_udp_t* handle);
267 void uv__udp_finish_close(uv_udp_t* handle);
268 uv_handle_type uv__handle_type(int fd);
269 FILE* uv__open_file(const char* path);
270 int uv__getpwuid_r(uv_passwd_t* pwd);
271 
272 /* random */
273 int uv__random_devurandom(void* buf, size_t buflen);
274 int uv__random_getrandom(void* buf, size_t buflen);
275 int uv__random_getentropy(void* buf, size_t buflen);
276 int uv__random_readpath(const char* path, void* buf, size_t buflen);
277 int uv__random_sysctl(void* buf, size_t buflen);
278 
279 #if defined(__APPLE__)
280 int uv___stream_fd(const uv_stream_t* handle);
281 #define uv__stream_fd(handle) (uv___stream_fd((const uv_stream_t*) (handle)))
282 #else
283 #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
284 #endif /* defined(__APPLE__) */
285 
286 #ifdef O_NONBLOCK
287 # define UV__F_NONBLOCK O_NONBLOCK
288 #else
289 # define UV__F_NONBLOCK 1
290 #endif
291 
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 #endif /* defined(__APPLE__) */
301 
302 UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) {
303   /* Use a fast time source if available.  We only need millisecond precision.
304    */
305   loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
306 }
307 
308 UV_UNUSED(static char* uv__basename_r(const char* path)) {
309   char* s;
310 
311   s = strrchr(path, '/');
312   if (s == NULL)
313     return (char*) path;
314 
315   return s + 1;
316 }
317 
318 #if defined(__linux__)
319 int uv__inotify_fork(uv_loop_t* loop, void* old_watchers);
320 #endif
321 
322 typedef int (*uv__peersockfunc)(int, struct sockaddr*, socklen_t*);
323 
324 int uv__getsockpeername(const uv_handle_t* handle,
325                         uv__peersockfunc func,
326                         struct sockaddr* name,
327                         int* namelen);
328 
329 #if defined(__linux__)            ||                                      \
330     defined(__FreeBSD__)          ||                                      \
331     defined(__FreeBSD_kernel__)
332 #define HAVE_MMSG 1
333 struct uv__mmsghdr {
334   struct msghdr msg_hdr;
335   unsigned int msg_len;
336 };
337 
338 int uv__recvmmsg(int fd,
339                  struct uv__mmsghdr* mmsg,
340                  unsigned int vlen,
341                  unsigned int flags,
342                  struct timespec* timeout);
343 int uv__sendmmsg(int fd,
344                  struct uv__mmsghdr* mmsg,
345                  unsigned int vlen,
346                  unsigned int flags);
347 #else
348 #define HAVE_MMSG 0
349 #endif
350 
351 
352 #endif /* UV_UNIX_INTERNAL_H_ */
353