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 <stdint.h>
30 #include <stdlib.h> /* abort */
31 #include <string.h> /* strrchr */
32 #include <fcntl.h> /* O_CLOEXEC and O_NONBLOCK, if supported. */
33 #include <stdio.h>
34 #include <errno.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #if defined(__APPLE__) || defined(__DragonFly__) || \
39 defined(__FreeBSD__) || defined(__NetBSD__)
40 #include <sys/event.h>
41 #endif
42
43 #define uv__msan_unpoison(p, n) \
44 do { \
45 (void) (p); \
46 (void) (n); \
47 } while (0)
48
49 #if defined(__has_feature)
50 # if __has_feature(memory_sanitizer)
51 # include <sanitizer/msan_interface.h>
52 # undef uv__msan_unpoison
53 # define uv__msan_unpoison __msan_unpoison
54 # endif
55 #endif
56
57 #if defined(__STRICT_ANSI__)
58 # define inline __inline
59 #endif
60
61 #if defined(__MVS__)
62 # include "os390-syscalls.h"
63 #endif /* __MVS__ */
64
65 #if defined(__sun)
66 # include <sys/port.h>
67 # include <port.h>
68 #endif /* __sun */
69
70 #if defined(_AIX)
71 # define reqevents events
72 # define rtnevents revents
73 # include <sys/poll.h>
74 #else
75 # include <poll.h>
76 #endif /* _AIX */
77
78 #if defined(__APPLE__)
79 # include "darwin-syscalls.h"
80 # if !TARGET_OS_IPHONE
81 # include <AvailabilityMacros.h>
82 # endif
83 #endif
84
85 /*
86 * Define common detection for active Thread Sanitizer
87 * - clang uses __has_feature(thread_sanitizer)
88 * - gcc-7+ uses __SANITIZE_THREAD__
89 */
90 #if defined(__has_feature)
91 # if __has_feature(thread_sanitizer)
92 # define __SANITIZE_THREAD__ 1
93 # endif
94 #endif
95
96 #if defined(PATH_MAX)
97 # define UV__PATH_MAX PATH_MAX
98 #else
99 # define UV__PATH_MAX 8192
100 #endif
101
102 union uv__sockaddr {
103 struct sockaddr_in6 in6;
104 struct sockaddr_in in;
105 struct sockaddr addr;
106 };
107
108 #define ACCESS_ONCE(type, var) \
109 (*(volatile type*) &(var))
110
111 #define ROUND_UP(a, b) \
112 ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
113
114 #define UNREACHABLE() \
115 do { \
116 assert(0 && "unreachable code"); \
117 abort(); \
118 } \
119 while (0)
120
121 #define SAVE_ERRNO(block) \
122 do { \
123 int _saved_errno = errno; \
124 do { block; } while (0); \
125 errno = _saved_errno; \
126 } \
127 while (0)
128
129 /* The __clang__ and __INTEL_COMPILER checks are superfluous because they
130 * define __GNUC__. They are here to convey to you, dear reader, that these
131 * macros are enabled when compiling with clang or icc.
132 */
133 #if defined(__clang__) || \
134 defined(__GNUC__) || \
135 defined(__INTEL_COMPILER)
136 # define UV_UNUSED(declaration) __attribute__((unused)) declaration
137 #else
138 # define UV_UNUSED(declaration) declaration
139 #endif
140
141 /* Leans on the fact that, on Linux, POLLRDHUP == EPOLLRDHUP. */
142 #ifdef POLLRDHUP
143 # define UV__POLLRDHUP POLLRDHUP
144 #else
145 # define UV__POLLRDHUP 0x2000
146 #endif
147
148 #ifdef POLLPRI
149 # define UV__POLLPRI POLLPRI
150 #else
151 # define UV__POLLPRI 0
152 #endif
153
154 #if !defined(O_CLOEXEC) && defined(__FreeBSD__)
155 /*
156 * It may be that we are just missing `__POSIX_VISIBLE >= 200809`.
157 * Try using fixed value const and give up, if it doesn't work
158 */
159 # define O_CLOEXEC 0x00100000
160 #endif
161
162 typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
163
164 /* loop flags */
165 enum {
166 UV_LOOP_BLOCK_SIGPROF = 0x1,
167 UV_LOOP_REAP_CHILDREN = 0x2,
168 UV_LOOP_ENABLE_IO_URING_SQPOLL = 0x4
169 };
170
171 /* flags of excluding ifaddr */
172 enum {
173 UV__EXCLUDE_IFPHYS,
174 UV__EXCLUDE_IFADDR
175 };
176
177 typedef enum {
178 UV_CLOCK_PRECISE = 0, /* Use the highest resolution clock available. */
179 UV_CLOCK_FAST = 1 /* Use the fastest clock with <= 1ms granularity. */
180 } uv_clocktype_t;
181
182 struct uv__stream_queued_fds_s {
183 unsigned int size;
184 unsigned int offset;
185 int fds[1];
186 };
187
188 #ifdef __linux__
189 struct uv__statx_timestamp {
190 int64_t tv_sec;
191 uint32_t tv_nsec;
192 int32_t unused0;
193 };
194
195 struct uv__statx {
196 uint32_t stx_mask;
197 uint32_t stx_blksize;
198 uint64_t stx_attributes;
199 uint32_t stx_nlink;
200 uint32_t stx_uid;
201 uint32_t stx_gid;
202 uint16_t stx_mode;
203 uint16_t unused0;
204 uint64_t stx_ino;
205 uint64_t stx_size;
206 uint64_t stx_blocks;
207 uint64_t stx_attributes_mask;
208 struct uv__statx_timestamp stx_atime;
209 struct uv__statx_timestamp stx_btime;
210 struct uv__statx_timestamp stx_ctime;
211 struct uv__statx_timestamp stx_mtime;
212 uint32_t stx_rdev_major;
213 uint32_t stx_rdev_minor;
214 uint32_t stx_dev_major;
215 uint32_t stx_dev_minor;
216 uint64_t unused1[14];
217 };
218 #endif /* __linux__ */
219
220 #if defined(_AIX) || \
221 defined(__APPLE__) || \
222 defined(__DragonFly__) || \
223 defined(__FreeBSD__) || \
224 defined(__linux__) || \
225 defined(__OpenBSD__) || \
226 defined(__NetBSD__)
227 #define uv__nonblock uv__nonblock_ioctl
228 #define UV__NONBLOCK_IS_IOCTL 1
229 #else
230 #define uv__nonblock uv__nonblock_fcntl
231 #define UV__NONBLOCK_IS_IOCTL 0
232 #endif
233
234 /* On Linux, uv__nonblock_fcntl() and uv__nonblock_ioctl() do not commute
235 * when O_NDELAY is not equal to O_NONBLOCK. Case in point: linux/sparc32
236 * and linux/sparc64, where O_NDELAY is O_NONBLOCK + another bit.
237 *
238 * Libuv uses uv__nonblock_fcntl() directly sometimes so ensure that it
239 * commutes with uv__nonblock().
240 */
241 #if defined(__linux__) && O_NDELAY != O_NONBLOCK
242 #undef uv__nonblock
243 #define uv__nonblock uv__nonblock_fcntl
244 #endif
245
246 /* core */
247 int uv__cloexec(int fd, int set);
248 int uv__nonblock_ioctl(int fd, int set);
249 int uv__nonblock_fcntl(int fd, int set);
250 int uv__close(int fd); /* preserves errno */
251 int uv__close_nocheckstdio(int fd);
252 int uv__close_nocancel(int fd);
253 int uv__socket(int domain, int type, int protocol);
254 int uv__sock_reuseport(int fd);
255 ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags);
256 void uv__make_close_pending(uv_handle_t* handle);
257 int uv__getiovmax(void);
258
259 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
260 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
261 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
262 void uv__io_close(uv_loop_t* loop, uv__io_t* w);
263 void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
264 int uv__io_active(const uv__io_t* w, unsigned int events);
265 int uv__io_check_fd(uv_loop_t* loop, int fd);
266 void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
267 int uv__io_fork(uv_loop_t* loop);
268 int uv__fd_exists(uv_loop_t* loop, int fd);
269
270 /* async */
271 void uv__async_stop(uv_loop_t* loop);
272 int uv__async_fork(uv_loop_t* loop);
273
274
275 /* loop */
276 void uv__run_idle(uv_loop_t* loop);
277 void uv__run_check(uv_loop_t* loop);
278 void uv__run_prepare(uv_loop_t* loop);
279
280 /* stream */
281 void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
282 uv_handle_type type);
283 int uv__stream_open(uv_stream_t*, int fd, int flags);
284 void uv__stream_destroy(uv_stream_t* stream);
285 #if defined(__APPLE__)
286 int uv__stream_try_select(uv_stream_t* stream, int* fd);
287 #endif /* defined(__APPLE__) */
288 void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
289 int uv__accept(int sockfd);
290 int uv__dup2_cloexec(int oldfd, int newfd);
291 int uv__open_cloexec(const char* path, int flags);
292 int uv__slurp(const char* filename, char* buf, size_t len);
293
294 /* tcp */
295 int uv__tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
296 int uv__tcp_nodelay(int fd, int on);
297 int uv__tcp_keepalive(int fd, int on, unsigned int delay);
298
299 /* tty */
300 void uv__tty_close(uv_tty_t* handle);
301
302 /* pipe */
303 int uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
304
305 /* signal */
306 void uv__signal_close(uv_signal_t* handle);
307 void uv__signal_global_once_init(void);
308 void uv__signal_loop_cleanup(uv_loop_t* loop);
309 int uv__signal_loop_fork(uv_loop_t* loop);
310
311 /* platform specific */
312 uint64_t uv__hrtime(uv_clocktype_t type);
313 int uv__kqueue_init(uv_loop_t* loop);
314 int uv__platform_loop_init(uv_loop_t* loop);
315 void uv__platform_loop_delete(uv_loop_t* loop);
316 void uv__platform_invalidate_fd(uv_loop_t* loop, int fd);
317 int uv__process_init(uv_loop_t* loop);
318
319 /* various */
320 void uv__async_close(uv_async_t* handle);
321 void uv__check_close(uv_check_t* handle);
322 void uv__fs_event_close(uv_fs_event_t* handle);
323 void uv__idle_close(uv_idle_t* handle);
324 void uv__pipe_close(uv_pipe_t* handle);
325 void uv__poll_close(uv_poll_t* handle);
326 void uv__prepare_close(uv_prepare_t* handle);
327 void uv__process_close(uv_process_t* handle);
328 void uv__stream_close(uv_stream_t* handle);
329 void uv__tcp_close(uv_tcp_t* handle);
330 size_t uv__thread_stack_size(void);
331 void uv__udp_close(uv_udp_t* handle);
332 void uv__udp_finish_close(uv_udp_t* handle);
333 FILE* uv__open_file(const char* path);
334 int uv__search_path(const char* prog, char* buf, size_t* buflen);
335 void uv__wait_children(uv_loop_t* loop);
336
337 /* random */
338 int uv__random_devurandom(void* buf, size_t buflen);
339 int uv__random_getrandom(void* buf, size_t buflen);
340 int uv__random_getentropy(void* buf, size_t buflen);
341 int uv__random_readpath(const char* path, void* buf, size_t buflen);
342 int uv__random_sysctl(void* buf, size_t buflen);
343
344 /* io_uring */
345 #ifdef __linux__
346 int uv__iou_fs_close(uv_loop_t* loop, uv_fs_t* req);
347 int uv__iou_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req);
348 int uv__iou_fs_fsync_or_fdatasync(uv_loop_t* loop,
349 uv_fs_t* req,
350 uint32_t fsync_flags);
351 int uv__iou_fs_link(uv_loop_t* loop, uv_fs_t* req);
352 int uv__iou_fs_mkdir(uv_loop_t* loop, uv_fs_t* req);
353 int uv__iou_fs_open(uv_loop_t* loop, uv_fs_t* req);
354 int uv__iou_fs_read_or_write(uv_loop_t* loop,
355 uv_fs_t* req,
356 int is_read);
357 int uv__iou_fs_rename(uv_loop_t* loop, uv_fs_t* req);
358 int uv__iou_fs_statx(uv_loop_t* loop,
359 uv_fs_t* req,
360 int is_fstat,
361 int is_lstat);
362 int uv__iou_fs_symlink(uv_loop_t* loop, uv_fs_t* req);
363 int uv__iou_fs_unlink(uv_loop_t* loop, uv_fs_t* req);
364 #else
365 #define uv__iou_fs_close(loop, req) 0
366 #define uv__iou_fs_ftruncate(loop, req) 0
367 #define uv__iou_fs_fsync_or_fdatasync(loop, req, fsync_flags) 0
368 #define uv__iou_fs_link(loop, req) 0
369 #define uv__iou_fs_mkdir(loop, req) 0
370 #define uv__iou_fs_open(loop, req) 0
371 #define uv__iou_fs_read_or_write(loop, req, is_read) 0
372 #define uv__iou_fs_rename(loop, req) 0
373 #define uv__iou_fs_statx(loop, req, is_fstat, is_lstat) 0
374 #define uv__iou_fs_symlink(loop, req) 0
375 #define uv__iou_fs_unlink(loop, req) 0
376 #endif
377
378 #if defined(__APPLE__)
379 int uv___stream_fd(const uv_stream_t* handle);
380 #define uv__stream_fd(handle) (uv___stream_fd((const uv_stream_t*) (handle)))
381 #else
382 #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
383 #endif /* defined(__APPLE__) */
384
385 int uv__make_pipe(int fds[2], int flags);
386
387 #if defined(__APPLE__)
388
389 int uv__fsevents_init(uv_fs_event_t* handle);
390 int uv__fsevents_close(uv_fs_event_t* handle);
391 void uv__fsevents_loop_delete(uv_loop_t* loop);
392
393 #endif /* defined(__APPLE__) */
394
UV_UNUSED(static void uv__update_time (uv_loop_t * loop))395 UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) {
396 /* Use a fast time source if available. We only need millisecond precision.
397 */
398 loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
399 }
400
UV_UNUSED(static char * uv__basename_r (const char * path))401 UV_UNUSED(static char* uv__basename_r(const char* path)) {
402 char* s;
403
404 s = strrchr(path, '/');
405 if (s == NULL)
406 return (char*) path;
407
408 return s + 1;
409 }
410
UV_UNUSED(static int uv__fstat (int fd,struct stat * s))411 UV_UNUSED(static int uv__fstat(int fd, struct stat* s)) {
412 int rc;
413
414 rc = fstat(fd, s);
415 if (rc >= 0)
416 uv__msan_unpoison(s, sizeof(*s));
417
418 return rc;
419 }
420
UV_UNUSED(static int uv__lstat (const char * path,struct stat * s))421 UV_UNUSED(static int uv__lstat(const char* path, struct stat* s)) {
422 int rc;
423
424 rc = lstat(path, s);
425 if (rc >= 0)
426 uv__msan_unpoison(s, sizeof(*s));
427
428 return rc;
429 }
430
UV_UNUSED(static int uv__stat (const char * path,struct stat * s))431 UV_UNUSED(static int uv__stat(const char* path, struct stat* s)) {
432 int rc;
433
434 rc = stat(path, s);
435 if (rc >= 0)
436 uv__msan_unpoison(s, sizeof(*s));
437
438 return rc;
439 }
440
441 #if defined(__linux__)
442 void uv__fs_post(uv_loop_t* loop, uv_fs_t* req);
443 ssize_t
444 uv__fs_copy_file_range(int fd_in,
445 off_t* off_in,
446 int fd_out,
447 off_t* off_out,
448 size_t len,
449 unsigned int flags);
450 int uv__statx(int dirfd,
451 const char* path,
452 int flags,
453 unsigned int mask,
454 struct uv__statx* statxbuf);
455 void uv__statx_to_stat(const struct uv__statx* statxbuf, uv_stat_t* buf);
456 ssize_t uv__getrandom(void* buf, size_t buflen, unsigned flags);
457 unsigned uv__kernel_version(void);
458 #endif
459
460 typedef int (*uv__peersockfunc)(int, struct sockaddr*, socklen_t*);
461
462 int uv__getsockpeername(const uv_handle_t* handle,
463 uv__peersockfunc func,
464 struct sockaddr* name,
465 int* namelen);
466
467 #if defined(__sun)
468 #if !defined(_POSIX_VERSION) || _POSIX_VERSION < 200809L
469 size_t strnlen(const char* s, size_t maxlen);
470 #endif
471 #endif
472
473 #if defined(__FreeBSD__)
474 ssize_t
475 uv__fs_copy_file_range(int fd_in,
476 off_t* off_in,
477 int fd_out,
478 off_t* off_out,
479 size_t len,
480 unsigned int flags);
481 #endif
482
483 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 1301000)
484 #define UV__CPU_AFFINITY_SUPPORTED 1
485 #else
486 #define UV__CPU_AFFINITY_SUPPORTED 0
487 #endif
488
489 #ifdef __linux__
490 typedef struct {
491 long long quota_per_period;
492 long long period_length;
493 double proportions;
494 } uv__cpu_constraint;
495
496 int uv__get_constrained_cpu(uv__cpu_constraint* constraint);
497 #endif
498
499 #if defined(__sun) && !defined(__illumos__)
500 #ifdef SO_FLOW_NAME
501 /* Since it's impossible to detect the Solaris 11.4 version via OS macros,
502 * so we check the presence of the socket option SO_FLOW_NAME that was first
503 * introduced to Solaris 11.4 and define a custom macro for determining 11.4.
504 */
505 #define UV__SOLARIS_11_4 (1)
506 #else
507 #define UV__SOLARIS_11_4 (0)
508 #endif
509 #endif
510
511 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
512 /* EVFILT_USER is available since OS X 10.6, DragonFlyBSD 4.0,
513 * FreeBSD 8.1, and NetBSD 10.0.
514 *
515 * Note that even though EVFILT_USER is defined on the current system,
516 * it may still fail to work at runtime somehow. In that case, we fall
517 * back to pipe-based signaling.
518 */
519 #define UV__KQUEUE_EVFILT_USER 1
520 /* Magic number of identifier used for EVFILT_USER during runtime detection.
521 * There are no Google hits for this number when I create it. That way,
522 * people will be directed here if this number gets printed due to some
523 * kqueue error and they google for help. */
524 #define UV__KQUEUE_EVFILT_USER_IDENT 0x1e7e7711
525 #else
526 #define UV__KQUEUE_EVFILT_USER 0
527 #endif
528
529 #endif /* UV_UNIX_INTERNAL_H_ */
530