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(_AIX)
53# include "uv/aix.h"
54#elif defined(__sun)
55# include "uv/sunos.h"
56#elif defined(__APPLE__)
57# include "uv/darwin.h"
58#elif defined(__DragonFly__) || \
59 defined(__FreeBSD__) || \
60 defined(__FreeBSD_kernel__) || \
61 defined(__OpenBSD__) || \
62 defined(__NetBSD__)
63# include "uv/bsd.h"
64#elif defined(__PASE__) || \
65 defined(__CYGWIN__) || \
66 defined(__MSYS__) || \
67 defined(__GNU__)
68# include "uv/posix.h"
69#elif defined(__HAIKU__)
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
85struct uv__io_s;
86struct uv_loop_s;
87
88typedef void (*uv__io_cb)(struct uv_loop_s* loop,
89 struct uv__io_s* w,
90 unsigned int events);
91typedef struct uv__io_s uv__io_t;
92
93struct uv__io_s {
94 uv__io_cb cb;
95 void* pending_queue[2];
96 void* watcher_queue[2];
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). */
120typedef struct uv_buf_t {
121 char* base;
122 size_t len;
123} uv_buf_t;
124
125typedef int uv_file;
126typedef int uv_os_sock_t;
127typedef int uv_os_fd_t;
128typedef pid_t uv_pid_t;
129
130#define UV_ONCE_INIT PTHREAD_ONCE_INIT
131
132typedef pthread_once_t uv_once_t;
133typedef pthread_t uv_thread_t;
134typedef pthread_mutex_t uv_mutex_t;
135typedef pthread_rwlock_t uv_rwlock_t;
136typedef UV_PLATFORM_SEM_T uv_sem_t;
137typedef pthread_cond_t uv_cond_t;
138typedef 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. */
145struct _uv_barrier {
146 uv_mutex_t mutex;
147 uv_cond_t cond;
148 unsigned threshold;
149 unsigned in;
150 unsigned out;
151};
152
153typedef 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
161typedef pthread_barrier_t uv_barrier_t;
162#endif
163
164/* Platform-specific definitions for uv_spawn support. */
165typedef gid_t uv_gid_t;
166typedef uid_t uv_uid_t;
167
168typedef 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
215typedef struct {
216 void* handle;
217 char* errmsg;
218} uv_lib_t;
219
220#define UV_LOOP_PRIVATE_FIELDS \
221 unsigned long flags; \
222 int backend_fd; \
223 void* pending_queue[2]; \
224 void* watcher_queue[2]; \
225 uv__io_t** watchers; \
226 unsigned int nwatchers; \
227 unsigned int nfds; \
228 void* wq[2]; \
229 uv_mutex_t wq_mutex; \
230 uv_async_t wq_async; \
231 uv_rwlock_t cloexec_lock; \
232 uv_handle_t* closing_handles; \
233 void* process_handles[2]; \
234 void* prepare_handles[2]; \
235 void* check_handles[2]; \
236 void* idle_handles[2]; \
237 void* async_handles[2]; \
238 void (*async_unused)(void); /* TODO(bnoordhuis) Remove in libuv v2. */ \
239 uv__io_t async_io_watcher; \
240 int async_wfd; \
241 struct { \
242 void* min; \
243 unsigned int nelts; \
244 } timer_heap; \
245 uint64_t timer_counter; \
246 uint64_t time; \
247 int signal_pipefd[2]; \
248 uv__io_t signal_io_watcher; \
249 uv_signal_t child_watcher; \
250 int emfile_fd; \
251 UV_PLATFORM_LOOP_FIELDS \
252
253#define UV_REQ_TYPE_PRIVATE /* empty */
254
255#define UV_REQ_PRIVATE_FIELDS /* empty */
256
257#define UV_PRIVATE_REQ_TYPES /* empty */
258
259#define UV_WRITE_PRIVATE_FIELDS \
260 void* queue[2]; \
261 unsigned int write_index; \
262 uv_buf_t* bufs; \
263 unsigned int nbufs; \
264 int error; \
265 uv_buf_t bufsml[4]; \
266
267#define UV_CONNECT_PRIVATE_FIELDS \
268 void* queue[2]; \
269
270#define UV_SHUTDOWN_PRIVATE_FIELDS /* empty */
271
272#define UV_UDP_SEND_PRIVATE_FIELDS \
273 void* queue[2]; \
274 struct sockaddr_storage addr; \
275 unsigned int nbufs; \
276 uv_buf_t* bufs; \
277 ssize_t status; \
278 uv_udp_send_cb send_cb; \
279 uv_buf_t bufsml[4]; \
280
281#define UV_HANDLE_PRIVATE_FIELDS \
282 uv_handle_t* next_closing; \
283 unsigned int flags; \
284
285#define UV_STREAM_PRIVATE_FIELDS \
286 uv_connect_t *connect_req; \
287 uv_shutdown_t *shutdown_req; \
288 uv__io_t io_watcher; \
289 void* write_queue[2]; \
290 void* write_completed_queue[2]; \
291 uv_connection_cb connection_cb; \
292 int delayed_error; \
293 int accepted_fd; \
294 void* queued_fds; \
295 UV_STREAM_PRIVATE_PLATFORM_FIELDS \
296
297#define UV_TCP_PRIVATE_FIELDS /* empty */
298
299#define UV_UDP_PRIVATE_FIELDS \
300 uv_alloc_cb alloc_cb; \
301 uv_udp_recv_cb recv_cb; \
302 uv__io_t io_watcher; \
303 void* write_queue[2]; \
304 void* write_completed_queue[2]; \
305
306#define UV_PIPE_PRIVATE_FIELDS \
307 const char* pipe_fname; /* strdup'ed */
308
309#define UV_POLL_PRIVATE_FIELDS \
310 uv__io_t io_watcher;
311
312#define UV_PREPARE_PRIVATE_FIELDS \
313 uv_prepare_cb prepare_cb; \
314 void* queue[2]; \
315
316#define UV_CHECK_PRIVATE_FIELDS \
317 uv_check_cb check_cb; \
318 void* queue[2]; \
319
320#define UV_IDLE_PRIVATE_FIELDS \
321 uv_idle_cb idle_cb; \
322 void* queue[2]; \
323
324#define UV_ASYNC_PRIVATE_FIELDS \
325 uv_async_cb async_cb; \
326 void* queue[2]; \
327 int pending; \
328
329#define UV_TIMER_PRIVATE_FIELDS \
330 uv_timer_cb timer_cb; \
331 void* heap_node[3]; \
332 uint64_t timeout; \
333 uint64_t repeat; \
334 uint64_t start_id;
335
336#define UV_GETADDRINFO_PRIVATE_FIELDS \
337 struct uv__work work_req; \
338 uv_getaddrinfo_cb cb; \
339 struct addrinfo* hints; \
340 char* hostname; \
341 char* service; \
342 struct addrinfo* addrinfo; \
343 int retcode;
344
345#define UV_GETNAMEINFO_PRIVATE_FIELDS \
346 struct uv__work work_req; \
347 uv_getnameinfo_cb getnameinfo_cb; \
348 struct sockaddr_storage storage; \
349 int flags; \
350 char host[NI_MAXHOST]; \
351 char service[NI_MAXSERV]; \
352 int retcode;
353
354#define UV_PROCESS_PRIVATE_FIELDS \
355 void* queue[2]; \
356 int status; \
357
358#define UV_FS_PRIVATE_FIELDS \
359 const char *new_path; \
360 uv_file file; \
361 int flags; \
362 mode_t mode; \
363 unsigned int nbufs; \
364 uv_buf_t* bufs; \
365 off_t off; \
366 uv_uid_t uid; \
367 uv_gid_t gid; \
368 double atime; \
369 double mtime; \
370 struct uv__work work_req; \
371 uv_buf_t bufsml[4]; \
372
373#define UV_WORK_PRIVATE_FIELDS \
374 struct uv__work work_req;
375
376#define UV_TTY_PRIVATE_FIELDS \
377 struct termios orig_termios; \
378 int mode;
379
380#define UV_SIGNAL_PRIVATE_FIELDS \
381 /* RB_ENTRY(uv_signal_s) tree_entry; */ \
382 struct { \
383 struct uv_signal_s* rbe_left; \
384 struct uv_signal_s* rbe_right; \
385 struct uv_signal_s* rbe_parent; \
386 int rbe_color; \
387 } tree_entry; \
388 /* Use two counters here so we don have to fiddle with atomics. */ \
389 unsigned int caught_signals; \
390 unsigned int dispatched_signals;
391
392#define UV_FS_EVENT_PRIVATE_FIELDS \
393 uv_fs_event_cb cb; \
394 UV_PLATFORM_FS_EVENT_FIELDS \
395
396/* fs open() flags supported on this platform: */
397#if defined(O_APPEND)
398# define UV_FS_O_APPEND O_APPEND
399#else
400# define UV_FS_O_APPEND 0
401#endif
402#if defined(O_CREAT)
403# define UV_FS_O_CREAT O_CREAT
404#else
405# define UV_FS_O_CREAT 0
406#endif
407#if defined(O_DIRECT)
408# define UV_FS_O_DIRECT O_DIRECT
409#else
410# define UV_FS_O_DIRECT 0
411#endif
412#if defined(O_DIRECTORY)
413# define UV_FS_O_DIRECTORY O_DIRECTORY
414#else
415# define UV_FS_O_DIRECTORY 0
416#endif
417#if defined(O_DSYNC)
418# define UV_FS_O_DSYNC O_DSYNC
419#else
420# define UV_FS_O_DSYNC 0
421#endif
422#if defined(O_EXCL)
423# define UV_FS_O_EXCL O_EXCL
424#else
425# define UV_FS_O_EXCL 0
426#endif
427#if defined(O_EXLOCK)
428# define UV_FS_O_EXLOCK O_EXLOCK
429#else
430# define UV_FS_O_EXLOCK 0
431#endif
432#if defined(O_NOATIME)
433# define UV_FS_O_NOATIME O_NOATIME
434#else
435# define UV_FS_O_NOATIME 0
436#endif
437#if defined(O_NOCTTY)
438# define UV_FS_O_NOCTTY O_NOCTTY
439#else
440# define UV_FS_O_NOCTTY 0
441#endif
442#if defined(O_NOFOLLOW)
443# define UV_FS_O_NOFOLLOW O_NOFOLLOW
444#else
445# define UV_FS_O_NOFOLLOW 0
446#endif
447#if defined(O_NONBLOCK)
448# define UV_FS_O_NONBLOCK O_NONBLOCK
449#else
450# define UV_FS_O_NONBLOCK 0
451#endif
452#if defined(O_RDONLY)
453# define UV_FS_O_RDONLY O_RDONLY
454#else
455# define UV_FS_O_RDONLY 0
456#endif
457#if defined(O_RDWR)
458# define UV_FS_O_RDWR O_RDWR
459#else
460# define UV_FS_O_RDWR 0
461#endif
462#if defined(O_SYMLINK)
463# define UV_FS_O_SYMLINK O_SYMLINK
464#else
465# define UV_FS_O_SYMLINK 0
466#endif
467#if defined(O_SYNC)
468# define UV_FS_O_SYNC O_SYNC
469#else
470# define UV_FS_O_SYNC 0
471#endif
472#if defined(O_TRUNC)
473# define UV_FS_O_TRUNC O_TRUNC
474#else
475# define UV_FS_O_TRUNC 0
476#endif
477#if defined(O_WRONLY)
478# define UV_FS_O_WRONLY O_WRONLY
479#else
480# define UV_FS_O_WRONLY 0
481#endif
482
483/* fs open() flags supported on other platforms: */
484#define UV_FS_O_RANDOM 0
485#define UV_FS_O_SHORT_LIVED 0
486#define UV_FS_O_SEQUENTIAL 0
487#define UV_FS_O_TEMPORARY 0
488
489#endif /* UV_UNIX_H */
490