Rizin
unix-like reverse engineering framework and cli tools
internal.h
Go to the documentation of this file.
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 #if defined(PATH_MAX)
66 # define UV__PATH_MAX PATH_MAX
67 #else
68 # define UV__PATH_MAX 8192
69 #endif
70 
71 #if defined(__ANDROID__)
72 int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset);
73 # ifdef pthread_sigmask
74 # undef pthread_sigmask
75 # endif
76 # define pthread_sigmask(how, set, oldset) uv__pthread_sigmask(how, set, oldset)
77 #endif
78 
79 #define ACCESS_ONCE(type, var) \
80  (*(volatile type*) &(var))
81 
82 #define ROUND_UP(a, b) \
83  ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
84 
85 #define UNREACHABLE() \
86  do { \
87  assert(0 && "unreachable code"); \
88  abort(); \
89  } \
90  while (0)
91 
92 #define SAVE_ERRNO(block) \
93  do { \
94  int _saved_errno = errno; \
95  do { block; } while (0); \
96  errno = _saved_errno; \
97  } \
98  while (0)
99 
100 /* The __clang__ and __INTEL_COMPILER checks are superfluous because they
101  * define __GNUC__. They are here to convey to you, dear reader, that these
102  * macros are enabled when compiling with clang or icc.
103  */
104 #if defined(__clang__) || \
105  defined(__GNUC__) || \
106  defined(__INTEL_COMPILER)
107 # define UV_UNUSED(declaration) __attribute__((unused)) declaration
108 #else
109 # define UV_UNUSED(declaration) declaration
110 #endif
111 
112 /* Leans on the fact that, on Linux, POLLRDHUP == EPOLLRDHUP. */
113 #ifdef POLLRDHUP
114 # define UV__POLLRDHUP POLLRDHUP
115 #else
116 # define UV__POLLRDHUP 0x2000
117 #endif
118 
119 #ifdef POLLPRI
120 # define UV__POLLPRI POLLPRI
121 #else
122 # define UV__POLLPRI 0
123 #endif
124 
125 #if !defined(O_CLOEXEC) && defined(__FreeBSD__)
126 /*
127  * It may be that we are just missing `__POSIX_VISIBLE >= 200809`.
128  * Try using fixed value const and give up, if it doesn't work
129  */
130 # define O_CLOEXEC 0x00100000
131 #endif
132 
134 
135 /* loop flags */
136 enum {
138 };
139 
140 /* flags of excluding ifaddr */
141 enum {
144 };
145 
146 typedef enum {
147  UV_CLOCK_PRECISE = 0, /* Use the highest resolution clock available. */
148  UV_CLOCK_FAST = 1 /* Use the fastest clock with <= 1ms granularity. */
150 
152  unsigned int size;
153  unsigned int offset;
154  int fds[1];
155 };
156 
157 
158 #if defined(_AIX) || \
159  defined(__APPLE__) || \
160  defined(__DragonFly__) || \
161  defined(__FreeBSD__) || \
162  defined(__FreeBSD_kernel__) || \
163  defined(__linux__) || \
164  defined(__OpenBSD__) || \
165  defined(__NetBSD__)
166 #define uv__cloexec uv__cloexec_ioctl
167 #define uv__nonblock uv__nonblock_ioctl
168 #else
169 #define uv__cloexec uv__cloexec_fcntl
170 #define uv__nonblock uv__nonblock_fcntl
171 #endif
172 
173 /* On Linux, uv__nonblock_fcntl() and uv__nonblock_ioctl() do not commute
174  * when O_NDELAY is not equal to O_NONBLOCK. Case in point: linux/sparc32
175  * and linux/sparc64, where O_NDELAY is O_NONBLOCK + another bit.
176  *
177  * Libuv uses uv__nonblock_fcntl() directly sometimes so ensure that it
178  * commutes with uv__nonblock().
179  */
180 #if defined(__linux__) && O_NDELAY != O_NONBLOCK
181 #undef uv__nonblock
182 #define uv__nonblock uv__nonblock_fcntl
183 #endif
184 
185 /* core */
186 int uv__cloexec_ioctl(int fd, int set);
187 int uv__cloexec_fcntl(int fd, int set);
188 int uv__nonblock_ioctl(int fd, int set);
189 int uv__nonblock_fcntl(int fd, int set);
190 int uv__close(int fd); /* preserves errno */
191 int uv__close_nocheckstdio(int fd);
192 int uv__close_nocancel(int fd);
193 int uv__socket(int domain, int type, int protocol);
194 ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags);
196 int uv__getiovmax(void);
197 
198 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
199 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
200 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
203 int uv__io_active(const uv__io_t* w, unsigned int events);
204 int uv__io_check_fd(uv_loop_t* loop, int fd);
205 void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
207 int uv__fd_exists(uv_loop_t* loop, int fd);
208 
209 /* async */
212 
213 
214 /* loop */
218 
219 /* stream */
222 int uv__stream_open(uv_stream_t*, int fd, int flags);
224 #if defined(__APPLE__)
225 int uv__stream_try_select(uv_stream_t* stream, int* fd);
226 #endif /* defined(__APPLE__) */
227 void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
228 int uv__accept(int sockfd);
229 int uv__dup2_cloexec(int oldfd, int newfd);
230 int uv__open_cloexec(const char* path, int flags);
231 
232 /* tcp */
233 int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
234 int uv__tcp_nodelay(int fd, int on);
235 int uv__tcp_keepalive(int fd, int on, unsigned int delay);
236 
237 /* pipe */
239 
240 /* signal */
242 void uv__signal_global_once_init(void);
245 
246 /* platform specific */
252 
253 /* various */
267 FILE* uv__open_file(const char* path);
268 int uv__getpwuid_r(uv_passwd_t* pwd);
269 int uv__search_path(const char* prog, char* buf, size_t* buflen);
270 
271 /* random */
272 int uv__random_devurandom(void* buf, size_t buflen);
273 int uv__random_getrandom(void* buf, size_t buflen);
274 int uv__random_getentropy(void* buf, size_t buflen);
275 int uv__random_readpath(const char* path, void* buf, size_t buflen);
276 int uv__random_sysctl(void* buf, size_t buflen);
277 
278 #if defined(__APPLE__)
279 int uv___stream_fd(const uv_stream_t* handle);
280 #define uv__stream_fd(handle) (uv___stream_fd((const uv_stream_t*) (handle)))
281 #else
282 #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
283 #endif /* defined(__APPLE__) */
284 
285 #ifdef O_NONBLOCK
286 # define UV__F_NONBLOCK O_NONBLOCK
287 #else
288 # define UV__F_NONBLOCK 1
289 #endif
290 
291 int uv__make_pipe(int fds[2], int flags);
292 
293 #if defined(__APPLE__)
294 
298 
299 #endif /* defined(__APPLE__) */
300 
301 UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) {
302  /* Use a fast time source if available. We only need millisecond precision.
303  */
304  loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
305 }
306 
307 UV_UNUSED(static char* uv__basename_r(const char* path)) {
308  char* s;
309 
310  s = strrchr(path, '/');
311  if (s == NULL)
312  return (char*) path;
313 
314  return s + 1;
315 }
316 
317 #if defined(__linux__)
318 int uv__inotify_fork(uv_loop_t* loop, void* old_watchers);
319 #endif
320 
321 typedef int (*uv__peersockfunc)(int, struct sockaddr*, socklen_t*);
322 
324  uv__peersockfunc func,
325  struct sockaddr* name,
326  int* namelen);
327 
328 #if defined(__linux__) || \
329  defined(__FreeBSD__) || \
330  defined(__FreeBSD_kernel__)
331 #define HAVE_MMSG 1
332 struct uv__mmsghdr {
333  struct msghdr msg_hdr;
334  unsigned int msg_len;
335 };
336 
337 int uv__recvmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen);
338 int uv__sendmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen);
339 #else
340 #define HAVE_MMSG 0
341 #endif
342 
343 
344 #endif /* UV_UNIX_INTERNAL_H_ */
static mcore_handle handle
Definition: asm_mcore.c:8
#define NULL
Definition: cris-opc.c:27
#define w
Definition: crypto_rc6.c:13
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd domain
Definition: sflib.h:79
int uv__sendmmsg(int fd, struct uv__mmsghdr *mmsg, unsigned int vlen)
Definition: freebsd.c:267
int uv__recvmmsg(int fd, struct uv__mmsghdr *mmsg, unsigned int vlen)
Definition: freebsd.c:276
int uv__fsevents_init(uv_fs_event_t *handle)
Definition: fsevents.c:29
int uv__fsevents_close(uv_fs_event_t *handle)
Definition: fsevents.c:34
void uv__fsevents_loop_delete(uv_loop_t *loop)
Definition: fsevents.c:39
voidpf stream
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
int uv__inotify_fork(uv_loop_t *loop, void *old_watchers)
Definition: linux-inotify.c:86
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path oldfd
Definition: sflib.h:94
int type
Definition: mipsasm.c:17
string FILE
Definition: benchmark.py:21
int uv__pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
Definition: pthread-fixes.c:39
static RzSocket * s
Definition: rtr.c:28
static sockfd
Definition: sfsocketcall.h:114
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
static int
Definition: sfsocketcall.h:114
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119
unsigned int socklen_t
Definition: sftypes.h:219
unsigned long uint64_t
Definition: sftypes.h:28
int ssize_t
Definition: sftypes.h:39
int sigset_t
Definition: sftypes.h:63
Definition: z80asm.h:102
Definition: unix.h:96
unsigned int size
Definition: internal.h:152
unsigned int offset
Definition: internal.h:153
Definition: uv.h:844
Definition: uv.h:824
Definition: uv.h:834
Definition: uv.h:1780
Definition: uv.h:767
Definition: uv.h:793
Definition: uv.h:547
Definition: uv.h:638
uv_loop_t * loop
Definition: main.c:7
uv_timer_t timeout
Definition: main.c:9
#define UV_UNUSED(declaration)
Definition: internal.h:109
int uv__tcp_keepalive(int fd, int on, unsigned int delay)
Definition: tcp.c:380
void uv__io_stop(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: core.c:910
void uv__prepare_close(uv_prepare_t *handle)
uv_clocktype_t
Definition: internal.h:146
@ UV_CLOCK_FAST
Definition: internal.h:148
@ UV_CLOCK_PRECISE
Definition: internal.h:147
void uv__udp_close(uv_udp_t *handle)
Definition: udp.c:90
int uv__open_cloexec(const char *path, int flags)
Definition: core.c:1003
void uv__signal_loop_cleanup(uv_loop_t *loop)
Definition: signal.c:291
uv_handle_type uv__handle_type(int fd)
Definition: stream.c:958
int uv__async_fork(uv_loop_t *loop)
Definition: async.c:230
int uv__signal_loop_fork(uv_loop_t *loop)
Definition: signal.c:281
void uv__poll_close(uv_poll_t *handle)
Definition: poll.c:148
int uv__random_getrandom(void *buf, size_t buflen)
int uv__close_nocheckstdio(int fd)
Definition: core.c:550
void uv__fs_event_close(uv_fs_event_t *handle)
Definition: aix.c:863
void uv__async_stop(uv_loop_t *loop)
Definition: async.c:240
void uv__idle_close(uv_idle_t *handle)
int uv__stream_open(uv_stream_t *, int fd, int flags)
Definition: stream.c:406
int uv__nonblock_ioctl(int fd, int set)
Definition: core.c:578
void uv__server_io(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: stream.c:528
int uv_pipe_listen(uv_pipe_t *handle, int backlog, uv_connection_cb cb)
Definition: pipe.c:94
int uv__getiovmax(void)
Definition: core.c:215
void uv__platform_invalidate_fd(uv_loop_t *loop, int fd)
Definition: aix.c:1280
int uv__random_readpath(const char *path, void *buf, size_t buflen)
void uv__io_start(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: core.c:882
int uv__fd_exists(uv_loop_t *loop, int fd)
Definition: core.c:965
void uv__signal_global_once_init(void)
Definition: signal.c:111
void uv__io_poll(uv_loop_t *loop, int timeout)
Definition: aix.c:133
uint64_t uv__hrtime(uv_clocktype_t type)
Definition: aix-common.c:43
void uv__io_close(uv_loop_t *loop, uv__io_t *w)
Definition: core.c:942
int uv__io_check_fd(uv_loop_t *loop, int fd)
Definition: aix.c:115
int uv__getsockpeername(const uv_handle_t *handle, uv__peersockfunc func, struct sockaddr *name, int *namelen)
Definition: core.c:1490
void uv__run_prepare(uv_loop_t *loop)
int uv__io_active(const uv__io_t *w, unsigned int events)
Definition: core.c:958
int uv_tcp_listen(uv_tcp_t *tcp, int backlog, uv_connection_cb cb)
Definition: tcp.c:328
int uv__getpwuid_r(uv_passwd_t *pwd)
Definition: core.c:1146
int uv__random_sysctl(void *buf, size_t buflen)
Definition: netbsd.c:238
@ UV_LOOP_BLOCK_SIGPROF
Definition: internal.h:137
void uv__signal_close(uv_signal_t *handle)
Definition: signal.c:335
int uv__accept(int sockfd)
Definition: core.c:489
int uv__search_path(const char *prog, char *buf, size_t *buflen)
Definition: core.c:1540
void uv__check_close(uv_check_t *handle)
int uv__platform_loop_init(uv_loop_t *loop)
Definition: aix.c:81
int uv__socket(int domain, int type, int protocol)
Definition: core.c:436
ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags)
Definition: core.c:670
void uv__io_init(uv__io_t *w, uv__io_cb cb, int fd)
Definition: core.c:865
int uv__close_nocancel(int fd)
Definition: core.c:530
int uv__io_fork(uv_loop_t *loop)
Definition: aix.c:108
void uv__pipe_close(uv_pipe_t *handle)
Definition: pipe.c:120
int uv__dup2_cloexec(int oldfd, int newfd)
Definition: core.c:1031
int uv__random_getentropy(void *buf, size_t buflen)
int uv__nonblock_fcntl(int fd, int set)
Definition: core.c:608
void uv__udp_finish_close(uv_udp_t *handle)
Definition: udp.c:101
void uv__platform_loop_delete(uv_loop_t *loop)
Definition: aix.c:95
void uv__run_check(uv_loop_t *loop)
int uv__cloexec_ioctl(int fd, int set)
Definition: core.c:593
int(* uv__peersockfunc)(int, struct sockaddr *, socklen_t *)
Definition: internal.h:321
void uv__stream_destroy(uv_stream_t *stream)
Definition: stream.c:458
void uv__make_close_pending(uv_handle_t *handle)
Definition: core.c:208
int uv__make_pipe(int fds[2], int flags)
Definition: process.c:142
void uv__async_close(uv_async_t *handle)
Definition: async.c:115
void uv__process_close(uv_process_t *handle)
Definition: process.c:590
void uv__stream_close(uv_stream_t *handle)
Definition: stream.c:1633
int uv__kqueue_init(uv_loop_t *loop)
Definition: kqueue.c:51
int uv__cloexec_fcntl(int fd, int set)
Definition: core.c:639
void uv__io_feed(uv_loop_t *loop, uv__io_t *w)
Definition: core.c:952
@ UV__EXCLUDE_IFPHYS
Definition: internal.h:142
@ UV__EXCLUDE_IFADDR
Definition: internal.h:143
int uv__close(int fd)
Definition: core.c:569
void uv__run_idle(uv_loop_t *loop)
FILE * uv__open_file(const char *path)
Definition: core.c:473
void uv__tcp_close(uv_tcp_t *handle)
Definition: tcp.c:459
int uv__random_devurandom(void *buf, size_t buflen)
int uv__tcp_nodelay(int fd, int on)
Definition: tcp.c:373
void uv__stream_init(uv_loop_t *loop, uv_stream_t *stream, uv_handle_type type)
Definition: stream.c:85
ut64 buflen
Definition: core.c:76
void(* uv__io_cb)(struct uv_loop_s *loop, struct uv__io_s *w, unsigned int events)
Definition: unix.h:91
char * prog
Definition: untgz.c:125
void(* uv_connection_cb)(uv_stream_t *server, int status)
Definition: uv.h:318
uv_handle_type
Definition: uv.h:189
static const z80_opcode fd[]
Definition: z80_tab.h:997
static const char * cb[]
Definition: z80_tab.h:176