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_H
23 #define UV_UNIX_H
24 
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <dirent.h>
29 
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #include <arpa/inet.h>
34 #include <netdb.h>  /* MAXHOSTNAMELEN on Solaris */
35 
36 #include <termios.h>
37 #include <pwd.h>
38 
39 #if !defined(__MVS__)
40 #include <semaphore.h>
41 #include <sys/param.h> /* MAXHOSTNAMELEN on Linux and the BSDs */
42 #endif
43 #include <pthread.h>
44 #include <signal.h>
45 
46 #include "uv/threadpool.h"
47 
48 #if defined(__linux__)
49 # include "uv/linux.h"
50 #elif defined (__MVS__)
51 # include "uv/os390.h"
52 #elif defined(__PASE__)  /* __PASE__ and _AIX are both defined on IBM i */
53 # include "uv/posix.h"  /* IBM i needs uv/posix.h, not uv/aix.h */
54 #elif defined(_AIX)
55 # include "uv/aix.h"
56 #elif defined(__sun)
57 # include "uv/sunos.h"
58 #elif defined(__APPLE__)
59 # include "uv/darwin.h"
60 #elif defined(__DragonFly__)       || \
61       defined(__FreeBSD__)         || \
62       defined(__OpenBSD__)         || \
63       defined(__NetBSD__)
64 # include "uv/bsd.h"
65 #elif defined(__CYGWIN__) || \
66       defined(__MSYS__)   || \
67       defined(__HAIKU__)  || \
68       defined(__QNX__)    || \
69       defined(__GNU__)
70 # include "uv/posix.h"
71 #endif
72 
73 #ifndef NI_MAXHOST
74 # define NI_MAXHOST 1025
75 #endif
76 
77 #ifndef NI_MAXSERV
78 # define NI_MAXSERV 32
79 #endif
80 
81 #ifndef UV_IO_PRIVATE_PLATFORM_FIELDS
82 # define UV_IO_PRIVATE_PLATFORM_FIELDS /* empty */
83 #endif
84 
85 struct uv__io_s;
86 struct uv_loop_s;
87 
88 typedef void (*uv__io_cb)(struct uv_loop_s* loop,
89                           struct uv__io_s* w,
90                           unsigned int events);
91 typedef struct uv__io_s uv__io_t;
92 
93 struct uv__io_s {
94   uv__io_cb cb;
95   struct uv__queue pending_queue;
96   struct uv__queue watcher_queue;
97   unsigned int pevents; /* Pending event mask i.e. mask at next tick. */
98   unsigned int events;  /* Current event mask. */
99   int fd;
100   UV_IO_PRIVATE_PLATFORM_FIELDS
101 };
102 
103 #ifndef UV_PLATFORM_SEM_T
104 # define UV_PLATFORM_SEM_T sem_t
105 #endif
106 
107 #ifndef UV_PLATFORM_LOOP_FIELDS
108 # define UV_PLATFORM_LOOP_FIELDS /* empty */
109 #endif
110 
111 #ifndef UV_PLATFORM_FS_EVENT_FIELDS
112 # define UV_PLATFORM_FS_EVENT_FIELDS /* empty */
113 #endif
114 
115 #ifndef UV_STREAM_PRIVATE_PLATFORM_FIELDS
116 # define UV_STREAM_PRIVATE_PLATFORM_FIELDS /* empty */
117 #endif
118 
119 /* Note: May be cast to struct iovec. See writev(2). */
120 typedef struct uv_buf_t {
121   char* base;
122   size_t len;
123 } uv_buf_t;
124 
125 typedef int uv_file;
126 typedef int uv_os_sock_t;
127 typedef int uv_os_fd_t;
128 typedef pid_t uv_pid_t;
129 
130 #define UV_ONCE_INIT PTHREAD_ONCE_INIT
131 
132 typedef pthread_once_t uv_once_t;
133 typedef pthread_t uv_thread_t;
134 typedef pthread_mutex_t uv_mutex_t;
135 typedef pthread_rwlock_t uv_rwlock_t;
136 typedef UV_PLATFORM_SEM_T uv_sem_t;
137 typedef pthread_cond_t uv_cond_t;
138 typedef pthread_key_t uv_key_t;
139 
140 /* Note: guard clauses should match uv_barrier_init's in src/unix/thread.c. */
141 #if defined(_AIX) || \
142     defined(__OpenBSD__) || \
143     !defined(PTHREAD_BARRIER_SERIAL_THREAD)
144 /* TODO(bnoordhuis) Merge into uv_barrier_t in v2. */
145 struct _uv_barrier {
146   uv_mutex_t mutex;
147   uv_cond_t cond;
148   unsigned threshold;
149   unsigned in;
150   unsigned out;
151 };
152 
153 typedef struct {
154   struct _uv_barrier* b;
155 # if defined(PTHREAD_BARRIER_SERIAL_THREAD)
156   /* TODO(bnoordhuis) Remove padding in v2. */
157   char pad[sizeof(pthread_barrier_t) - sizeof(struct _uv_barrier*)];
158 # endif
159 } uv_barrier_t;
160 #else
161 typedef pthread_barrier_t uv_barrier_t;
162 #endif
163 
164 /* Platform-specific definitions for uv_spawn support. */
165 typedef gid_t uv_gid_t;
166 typedef uid_t uv_uid_t;
167 
168 typedef struct dirent uv__dirent_t;
169 
170 #define UV_DIR_PRIVATE_FIELDS \
171   DIR* dir;
172 
173 #if defined(DT_UNKNOWN)
174 # define HAVE_DIRENT_TYPES
175 # if defined(DT_REG)
176 #  define UV__DT_FILE DT_REG
177 # else
178 #  define UV__DT_FILE -1
179 # endif
180 # if defined(DT_DIR)
181 #  define UV__DT_DIR DT_DIR
182 # else
183 #  define UV__DT_DIR -2
184 # endif
185 # if defined(DT_LNK)
186 #  define UV__DT_LINK DT_LNK
187 # else
188 #  define UV__DT_LINK -3
189 # endif
190 # if defined(DT_FIFO)
191 #  define UV__DT_FIFO DT_FIFO
192 # else
193 #  define UV__DT_FIFO -4
194 # endif
195 # if defined(DT_SOCK)
196 #  define UV__DT_SOCKET DT_SOCK
197 # else
198 #  define UV__DT_SOCKET -5
199 # endif
200 # if defined(DT_CHR)
201 #  define UV__DT_CHAR DT_CHR
202 # else
203 #  define UV__DT_CHAR -6
204 # endif
205 # if defined(DT_BLK)
206 #  define UV__DT_BLOCK DT_BLK
207 # else
208 #  define UV__DT_BLOCK -7
209 # endif
210 #endif
211 
212 /* Platform-specific definitions for uv_dlopen support. */
213 #define UV_DYNAMIC /* empty */
214 
215 typedef struct {
216   void* handle;
217   char* errmsg;
218 } uv_lib_t;
219 
220 #define UV_LOOP_PRIVATE_FIELDS                                                \
221   unsigned int magic;                                                         \
222   unsigned long flags;                                                        \
223   int backend_fd;                                                             \
224   struct uv__queue pending_queue;                                             \
225   struct uv__queue watcher_queue;                                             \
226   uv__io_t** watchers;                                                        \
227   unsigned int nwatchers;                                                     \
228   unsigned int nfds;                                                          \
229   struct uv__queue wq;                                                        \
230   uv_mutex_t wq_mutex;                                                        \
231   uv_async_t wq_async;                                                        \
232   uv_rwlock_t cloexec_lock;                                                   \
233   uv_handle_t* closing_handles;                                               \
234   struct uv__queue process_handles;                                           \
235   struct uv__queue prepare_handles;                                           \
236   struct uv__queue check_handles;                                             \
237   struct uv__queue idle_handles;                                              \
238   struct uv__queue async_handles;                                             \
239   void (*async_unused)(void);  /* TODO(bnoordhuis) Remove in libuv v2. */     \
240   uv__io_t async_io_watcher;                                                  \
241   int async_wfd;                                                              \
242   struct {                                                                    \
243     void* min;                                                                \
244     unsigned int nelts;                                                       \
245   } timer_heap;                                                               \
246   uint64_t timer_counter;                                                     \
247   uint64_t time;                                                              \
248   int signal_pipefd[2];                                                       \
249   uv__io_t signal_io_watcher;                                                 \
250   uv_signal_t child_watcher;                                                  \
251   int emfile_fd;                                                              \
252   UV_PLATFORM_LOOP_FIELDS                                                     \
253 
254 #define UV_REQ_TYPE_PRIVATE /* empty */
255 
256 #define UV_REQ_PRIVATE_FIELDS  /* empty */
257 
258 #define UV_PRIVATE_REQ_TYPES /* empty */
259 
260 #define UV_WRITE_PRIVATE_FIELDS                                               \
261   struct uv__queue queue;                                                     \
262   unsigned int write_index;                                                   \
263   uv_buf_t* bufs;                                                             \
264   unsigned int nbufs;                                                         \
265   int error;                                                                  \
266   uv_buf_t bufsml[4];                                                         \
267 
268 #define UV_CONNECT_PRIVATE_FIELDS                                             \
269   struct uv__queue queue;                                                     \
270 
271 #define UV_SHUTDOWN_PRIVATE_FIELDS /* empty */
272 
273 #define UV_UDP_SEND_PRIVATE_FIELDS                                            \
274   struct uv__queue queue;                                                     \
275   struct sockaddr_storage addr;                                               \
276   unsigned int nbufs;                                                         \
277   uv_buf_t* bufs;                                                             \
278   ssize_t status;                                                             \
279   uv_udp_send_cb send_cb;                                                     \
280   uv_buf_t bufsml[4];                                                         \
281 
282 #define UV_HANDLE_PRIVATE_FIELDS                                              \
283   uv_handle_t* next_closing;                                                  \
284   unsigned int flags;                                                         \
285 
286 #define UV_STREAM_PRIVATE_FIELDS                                              \
287   uv_connect_t *connect_req;                                                  \
288   uv_shutdown_t *shutdown_req;                                                \
289   uv__io_t io_watcher;                                                        \
290   struct uv__queue write_queue;                                               \
291   struct uv__queue write_completed_queue;                                     \
292   uv_connection_cb connection_cb;                                             \
293   int delayed_error;                                                          \
294   int accepted_fd;                                                            \
295   void* queued_fds;                                                           \
296   UV_STREAM_PRIVATE_PLATFORM_FIELDS                                           \
297 
298 #define UV_TCP_PRIVATE_FIELDS /* empty */
299 
300 #define UV_UDP_PRIVATE_FIELDS                                                 \
301   uv_alloc_cb alloc_cb;                                                       \
302   uv_udp_recv_cb recv_cb;                                                     \
303   uv__io_t io_watcher;                                                        \
304   struct uv__queue write_queue;                                               \
305   struct uv__queue write_completed_queue;                                     \
306 
307 #define UV_PIPE_PRIVATE_FIELDS                                                \
308   const char* pipe_fname; /* NULL or strdup'ed */
309 
310 #define UV_POLL_PRIVATE_FIELDS                                                \
311   uv__io_t io_watcher;
312 
313 #define UV_PREPARE_PRIVATE_FIELDS                                             \
314   uv_prepare_cb prepare_cb;                                                   \
315   struct uv__queue queue;                                                     \
316 
317 #define UV_CHECK_PRIVATE_FIELDS                                               \
318   uv_check_cb check_cb;                                                       \
319   struct uv__queue queue;                                                     \
320 
321 #define UV_IDLE_PRIVATE_FIELDS                                                \
322   uv_idle_cb idle_cb;                                                         \
323   struct uv__queue queue;                                                     \
324 
325 #define UV_ASYNC_PRIVATE_FIELDS                                               \
326   uv_async_cb async_cb;                                                       \
327   struct uv__queue queue;                                                     \
328   int pending;                                                                \
329 
330 #define UV_TIMER_PRIVATE_FIELDS                                               \
331   uv_timer_cb timer_cb;                                                       \
332   void* heap_node[3];                                                         \
333   uint64_t timeout;                                                           \
334   uint64_t repeat;                                                            \
335   uint64_t start_id;
336 
337 #define UV_GETADDRINFO_PRIVATE_FIELDS                                         \
338   struct uv__work work_req;                                                   \
339   uv_getaddrinfo_cb cb;                                                       \
340   struct addrinfo* hints;                                                     \
341   char* hostname;                                                             \
342   char* service;                                                              \
343   struct addrinfo* addrinfo;                                                  \
344   int retcode;
345 
346 #define UV_GETNAMEINFO_PRIVATE_FIELDS                                         \
347   struct uv__work work_req;                                                   \
348   uv_getnameinfo_cb getnameinfo_cb;                                           \
349   struct sockaddr_storage storage;                                            \
350   int flags;                                                                  \
351   char host[NI_MAXHOST];                                                      \
352   char service[NI_MAXSERV];                                                   \
353   int retcode;
354 
355 #define UV_PROCESS_PRIVATE_FIELDS                                             \
356   struct uv__queue queue;                                                     \
357   int status;                                                                 \
358 
359 #define UV_FS_PRIVATE_FIELDS                                                  \
360   const char *new_path;                                                       \
361   uv_file file;                                                               \
362   int flags;                                                                  \
363   mode_t mode;                                                                \
364   unsigned int nbufs;                                                         \
365   uv_buf_t* bufs;                                                             \
366   off_t off;                                                                  \
367   uv_uid_t uid;                                                               \
368   uv_gid_t gid;                                                               \
369   double atime;                                                               \
370   double mtime;                                                               \
371   struct uv__work work_req;                                                   \
372   uv_buf_t bufsml[4];                                                         \
373 
374 #define UV_WORK_PRIVATE_FIELDS                                                \
375   struct uv__work work_req;
376 
377 #define UV_TTY_PRIVATE_FIELDS                                                 \
378   struct termios orig_termios;                                                \
379   int mode;
380 
381 #define UV_SIGNAL_PRIVATE_FIELDS                                              \
382   /* RB_ENTRY(uv_signal_s) tree_entry; */                                     \
383   struct {                                                                    \
384     struct uv_signal_s* rbe_left;                                             \
385     struct uv_signal_s* rbe_right;                                            \
386     struct uv_signal_s* rbe_parent;                                           \
387     int rbe_color;                                                            \
388   } tree_entry;                                                               \
389   /* Use two counters here so we don have to fiddle with atomics. */          \
390   unsigned int caught_signals;                                                \
391   unsigned int dispatched_signals;
392 
393 #define UV_FS_EVENT_PRIVATE_FIELDS                                            \
394   uv_fs_event_cb cb;                                                          \
395   UV_PLATFORM_FS_EVENT_FIELDS                                                 \
396 
397 /* fs open() flags supported on this platform: */
398 #if defined(O_APPEND)
399 # define UV_FS_O_APPEND       O_APPEND
400 #else
401 # define UV_FS_O_APPEND       0
402 #endif
403 #if defined(O_CREAT)
404 # define UV_FS_O_CREAT        O_CREAT
405 #else
406 # define UV_FS_O_CREAT        0
407 #endif
408 
409 #if defined(__linux__) && defined(__arm__)
410 # define UV_FS_O_DIRECT       0x10000
411 #elif defined(__linux__) && defined(__m68k__)
412 # define UV_FS_O_DIRECT       0x10000
413 #elif defined(__linux__) && defined(__mips__)
414 # define UV_FS_O_DIRECT       0x08000
415 #elif defined(__linux__) && defined(__powerpc__)
416 # define UV_FS_O_DIRECT       0x20000
417 #elif defined(__linux__) && defined(__s390x__)
418 # define UV_FS_O_DIRECT       0x04000
419 #elif defined(__linux__) && defined(__x86_64__)
420 # define UV_FS_O_DIRECT       0x04000
421 #elif defined(__linux__) && defined(__loongarch__)
422 # define UV_FS_O_DIRECT       0x04000
423 #elif defined(O_DIRECT)
424 # define UV_FS_O_DIRECT       O_DIRECT
425 #else
426 # define UV_FS_O_DIRECT       0
427 #endif
428 
429 #if defined(O_DIRECTORY)
430 # define UV_FS_O_DIRECTORY    O_DIRECTORY
431 #else
432 # define UV_FS_O_DIRECTORY    0
433 #endif
434 #if defined(O_DSYNC)
435 # define UV_FS_O_DSYNC        O_DSYNC
436 #else
437 # define UV_FS_O_DSYNC        0
438 #endif
439 #if defined(O_EXCL)
440 # define UV_FS_O_EXCL         O_EXCL
441 #else
442 # define UV_FS_O_EXCL         0
443 #endif
444 #if defined(O_EXLOCK)
445 # define UV_FS_O_EXLOCK       O_EXLOCK
446 #else
447 # define UV_FS_O_EXLOCK       0
448 #endif
449 #if defined(O_NOATIME)
450 # define UV_FS_O_NOATIME      O_NOATIME
451 #else
452 # define UV_FS_O_NOATIME      0
453 #endif
454 #if defined(O_NOCTTY)
455 # define UV_FS_O_NOCTTY       O_NOCTTY
456 #else
457 # define UV_FS_O_NOCTTY       0
458 #endif
459 #if defined(O_NOFOLLOW)
460 # define UV_FS_O_NOFOLLOW     O_NOFOLLOW
461 #else
462 # define UV_FS_O_NOFOLLOW     0
463 #endif
464 #if defined(O_NONBLOCK)
465 # define UV_FS_O_NONBLOCK     O_NONBLOCK
466 #else
467 # define UV_FS_O_NONBLOCK     0
468 #endif
469 #if defined(O_RDONLY)
470 # define UV_FS_O_RDONLY       O_RDONLY
471 #else
472 # define UV_FS_O_RDONLY       0
473 #endif
474 #if defined(O_RDWR)
475 # define UV_FS_O_RDWR         O_RDWR
476 #else
477 # define UV_FS_O_RDWR         0
478 #endif
479 #if defined(O_SYMLINK)
480 # define UV_FS_O_SYMLINK      O_SYMLINK
481 #else
482 # define UV_FS_O_SYMLINK      0
483 #endif
484 #if defined(O_SYNC)
485 # define UV_FS_O_SYNC         O_SYNC
486 #else
487 # define UV_FS_O_SYNC         0
488 #endif
489 #if defined(O_TRUNC)
490 # define UV_FS_O_TRUNC        O_TRUNC
491 #else
492 # define UV_FS_O_TRUNC        0
493 #endif
494 #if defined(O_WRONLY)
495 # define UV_FS_O_WRONLY       O_WRONLY
496 #else
497 # define UV_FS_O_WRONLY       0
498 #endif
499 
500 /* fs open() flags supported on other platforms: */
501 #define UV_FS_O_FILEMAP       0
502 #define UV_FS_O_RANDOM        0
503 #define UV_FS_O_SHORT_LIVED   0
504 #define UV_FS_O_SEQUENTIAL    0
505 #define UV_FS_O_TEMPORARY     0
506 
507 #endif /* UV_UNIX_H */
508