xref: /libuv/src/unix/internal.h (revision 1c778bd0)
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_fsync_or_fdatasync(uv_loop_t* loop,
348                                   uv_fs_t* req,
349                                   uint32_t fsync_flags);
350 int uv__iou_fs_link(uv_loop_t* loop, uv_fs_t* req);
351 int uv__iou_fs_mkdir(uv_loop_t* loop, uv_fs_t* req);
352 int uv__iou_fs_open(uv_loop_t* loop, uv_fs_t* req);
353 int uv__iou_fs_read_or_write(uv_loop_t* loop,
354                              uv_fs_t* req,
355                              int is_read);
356 int uv__iou_fs_rename(uv_loop_t* loop, uv_fs_t* req);
357 int uv__iou_fs_statx(uv_loop_t* loop,
358                      uv_fs_t* req,
359                      int is_fstat,
360                      int is_lstat);
361 int uv__iou_fs_symlink(uv_loop_t* loop, uv_fs_t* req);
362 int uv__iou_fs_unlink(uv_loop_t* loop, uv_fs_t* req);
363 #else
364 #define uv__iou_fs_close(loop, req) 0
365 #define uv__iou_fs_fsync_or_fdatasync(loop, req, fsync_flags) 0
366 #define uv__iou_fs_link(loop, req) 0
367 #define uv__iou_fs_mkdir(loop, req) 0
368 #define uv__iou_fs_open(loop, req) 0
369 #define uv__iou_fs_read_or_write(loop, req, is_read) 0
370 #define uv__iou_fs_rename(loop, req) 0
371 #define uv__iou_fs_statx(loop, req, is_fstat, is_lstat) 0
372 #define uv__iou_fs_symlink(loop, req) 0
373 #define uv__iou_fs_unlink(loop, req) 0
374 #endif
375 
376 #if defined(__APPLE__)
377 int uv___stream_fd(const uv_stream_t* handle);
378 #define uv__stream_fd(handle) (uv___stream_fd((const uv_stream_t*) (handle)))
379 #else
380 #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
381 #endif /* defined(__APPLE__) */
382 
383 int uv__make_pipe(int fds[2], int flags);
384 
385 #if defined(__APPLE__)
386 
387 int uv__fsevents_init(uv_fs_event_t* handle);
388 int uv__fsevents_close(uv_fs_event_t* handle);
389 void uv__fsevents_loop_delete(uv_loop_t* loop);
390 
391 #endif /* defined(__APPLE__) */
392 
UV_UNUSED(static void uv__update_time (uv_loop_t * loop))393 UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) {
394   /* Use a fast time source if available.  We only need millisecond precision.
395    */
396   loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
397 }
398 
UV_UNUSED(static char * uv__basename_r (const char * path))399 UV_UNUSED(static char* uv__basename_r(const char* path)) {
400   char* s;
401 
402   s = strrchr(path, '/');
403   if (s == NULL)
404     return (char*) path;
405 
406   return s + 1;
407 }
408 
UV_UNUSED(static int uv__fstat (int fd,struct stat * s))409 UV_UNUSED(static int uv__fstat(int fd, struct stat* s)) {
410   int rc;
411 
412   rc = fstat(fd, s);
413   if (rc >= 0)
414     uv__msan_unpoison(s, sizeof(*s));
415 
416   return rc;
417 }
418 
UV_UNUSED(static int uv__lstat (const char * path,struct stat * s))419 UV_UNUSED(static int uv__lstat(const char* path, struct stat* s)) {
420   int rc;
421 
422   rc = lstat(path, s);
423   if (rc >= 0)
424     uv__msan_unpoison(s, sizeof(*s));
425 
426   return rc;
427 }
428 
UV_UNUSED(static int uv__stat (const char * path,struct stat * s))429 UV_UNUSED(static int uv__stat(const char* path, struct stat* s)) {
430   int rc;
431 
432   rc = stat(path, s);
433   if (rc >= 0)
434     uv__msan_unpoison(s, sizeof(*s));
435 
436   return rc;
437 }
438 
439 #if defined(__linux__)
440 void uv__fs_post(uv_loop_t* loop, uv_fs_t* req);
441 ssize_t
442 uv__fs_copy_file_range(int fd_in,
443                        off_t* off_in,
444                        int fd_out,
445                        off_t* off_out,
446                        size_t len,
447                        unsigned int flags);
448 int uv__statx(int dirfd,
449               const char* path,
450               int flags,
451               unsigned int mask,
452               struct uv__statx* statxbuf);
453 void uv__statx_to_stat(const struct uv__statx* statxbuf, uv_stat_t* buf);
454 ssize_t uv__getrandom(void* buf, size_t buflen, unsigned flags);
455 unsigned uv__kernel_version(void);
456 #endif
457 
458 typedef int (*uv__peersockfunc)(int, struct sockaddr*, socklen_t*);
459 
460 int uv__getsockpeername(const uv_handle_t* handle,
461                         uv__peersockfunc func,
462                         struct sockaddr* name,
463                         int* namelen);
464 
465 #if defined(__sun)
466 #if !defined(_POSIX_VERSION) || _POSIX_VERSION < 200809L
467 size_t strnlen(const char* s, size_t maxlen);
468 #endif
469 #endif
470 
471 #if defined(__FreeBSD__)
472 ssize_t
473 uv__fs_copy_file_range(int fd_in,
474                        off_t* off_in,
475                        int fd_out,
476                        off_t* off_out,
477                        size_t len,
478                        unsigned int flags);
479 #endif
480 
481 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 1301000)
482 #define UV__CPU_AFFINITY_SUPPORTED 1
483 #else
484 #define UV__CPU_AFFINITY_SUPPORTED 0
485 #endif
486 
487 #ifdef __linux__
488 typedef struct {
489   long long quota_per_period;
490   long long period_length;
491   double proportions;
492 } uv__cpu_constraint;
493 
494 int uv__get_constrained_cpu(uv__cpu_constraint* constraint);
495 #endif
496 
497 #ifdef __sun
498 #ifdef SO_FLOW_NAME
499 /* Since it's impossible to detect the Solaris 11.4 version via OS macros,
500  * so we check the presence of the socket option SO_FLOW_NAME that was first
501  * introduced to Solaris 11.4 and define a custom macro for determining 11.4.
502  */
503 #define UV__SOLARIS_11_4 (1)
504 #else
505 #define UV__SOLARIS_11_4 (0)
506 #endif
507 #endif
508 
509 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
510 /* EVFILT_USER is available since OS X 10.6, DragonFlyBSD 4.0,
511  * FreeBSD 8.1, and NetBSD 10.0.
512  *
513  * Note that even though EVFILT_USER is defined on the current system,
514  * it may still fail to work at runtime somehow. In that case, we fall
515  * back to pipe-based signaling.
516  */
517 #define UV__KQUEUE_EVFILT_USER 1
518 /* Magic number of identifier used for EVFILT_USER during runtime detection.
519  * There are no Google hits for this number when I create it. That way,
520  * people will be directed here if this number gets printed due to some
521  * kqueue error and they google for help. */
522 #define UV__KQUEUE_EVFILT_USER_IDENT 0x1e7e7711
523 #else
524 #define UV__KQUEUE_EVFILT_USER 0
525 #endif
526 
527 #endif /* UV_UNIX_INTERNAL_H_ */
528