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