/src/unix/internal.h

http://github.com/joyent/libuv · C Header · 237 lines · 164 code · 40 blank · 33 comment · 5 complexity · 19ac55e37e5e7814bdffd173259b641f MD5 · raw 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. #ifndef UV_UNIX_INTERNAL_H_
  22. #define UV_UNIX_INTERNAL_H_
  23. #include "uv-common.h"
  24. #include <assert.h>
  25. #include <stdlib.h> /* abort */
  26. #if defined(__STRICT_ANSI__)
  27. # define inline __inline
  28. #endif
  29. #undef HAVE_PORTS_FS
  30. #if __linux__
  31. # include "linux/syscalls.h"
  32. #endif /* __linux__ */
  33. #if defined(__sun)
  34. # include <sys/port.h>
  35. # include <port.h>
  36. # ifdef PORT_SOURCE_FILE
  37. # define HAVE_PORTS_FS 1
  38. # endif
  39. # define futimes(fd, tv) futimesat(fd, (void*)0, tv)
  40. #endif /* __sun */
  41. #if defined(__APPLE__) && !TARGET_OS_IPHONE
  42. # include <CoreServices/CoreServices.h>
  43. #endif
  44. #define UNREACHABLE() \
  45. do { \
  46. assert(0 && "unreachable code"); \
  47. abort(); \
  48. } \
  49. while (0)
  50. #define SAVE_ERRNO(block) \
  51. do { \
  52. int _saved_errno = errno; \
  53. do { block; } while (0); \
  54. errno = _saved_errno; \
  55. } \
  56. while (0)
  57. #if defined(__linux__)
  58. # define UV__POLLIN UV__EPOLLIN
  59. # define UV__POLLOUT UV__EPOLLOUT
  60. # define UV__POLLERR UV__EPOLLERR
  61. # define UV__POLLHUP UV__EPOLLHUP
  62. #endif
  63. #if defined(__sun)
  64. # define UV__POLLIN POLLIN
  65. # define UV__POLLOUT POLLOUT
  66. # define UV__POLLERR POLLERR
  67. # define UV__POLLHUP POLLHUP
  68. #endif
  69. #ifndef UV__POLLIN
  70. # define UV__POLLIN 1
  71. #endif
  72. #ifndef UV__POLLOUT
  73. # define UV__POLLOUT 2
  74. #endif
  75. #ifndef UV__POLLERR
  76. # define UV__POLLERR 4
  77. #endif
  78. #ifndef UV__POLLHUP
  79. # define UV__POLLHUP 8
  80. #endif
  81. /* handle flags */
  82. enum {
  83. UV_CLOSING = 0x01, /* uv_close() called but not finished. */
  84. UV_CLOSED = 0x02, /* close(2) finished. */
  85. UV_STREAM_READING = 0x04, /* uv_read_start() called. */
  86. UV_STREAM_SHUTTING = 0x08, /* uv_shutdown() called but not complete. */
  87. UV_STREAM_SHUT = 0x10, /* Write side closed. */
  88. UV_STREAM_READABLE = 0x20, /* The stream is readable */
  89. UV_STREAM_WRITABLE = 0x40, /* The stream is writable */
  90. UV_STREAM_BLOCKING = 0x80, /* Synchronous writes. */
  91. UV_TCP_NODELAY = 0x100, /* Disable Nagle. */
  92. UV_TCP_KEEPALIVE = 0x200, /* Turn on keep-alive. */
  93. UV_TCP_SINGLE_ACCEPT = 0x400 /* Only accept() when idle. */
  94. };
  95. __attribute__((unused))
  96. static void uv__req_init(uv_loop_t* loop, uv_req_t* req, uv_req_type type) {
  97. req->type = type;
  98. uv__req_register(loop, req);
  99. }
  100. #define uv__req_init(loop, req, type) \
  101. uv__req_init((loop), (uv_req_t*)(req), (type))
  102. /* core */
  103. void uv__handle_init(uv_loop_t* loop, uv_handle_t* handle, uv_handle_type type);
  104. int uv__nonblock(int fd, int set);
  105. int uv__cloexec(int fd, int set);
  106. int uv__socket(int domain, int type, int protocol);
  107. int uv__dup(int fd);
  108. int uv_async_stop(uv_async_t* handle);
  109. void uv__make_close_pending(uv_handle_t* handle);
  110. void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
  111. void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  112. void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  113. void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
  114. int uv__io_active(const uv__io_t* w, unsigned int events);
  115. void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
  116. /* loop */
  117. int uv__loop_init(uv_loop_t* loop, int default_loop);
  118. void uv__loop_delete(uv_loop_t* loop);
  119. void uv__run_idle(uv_loop_t* loop);
  120. void uv__run_check(uv_loop_t* loop);
  121. void uv__run_prepare(uv_loop_t* loop);
  122. /* error */
  123. uv_err_code uv_translate_sys_error(int sys_errno);
  124. void uv_fatal_error(const int errorno, const char* syscall);
  125. /* stream */
  126. void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
  127. uv_handle_type type);
  128. int uv__stream_open(uv_stream_t*, int fd, int flags);
  129. void uv__stream_destroy(uv_stream_t* stream);
  130. void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  131. int uv__accept(int sockfd);
  132. /* tcp */
  133. int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
  134. int uv__tcp_nodelay(int fd, int on);
  135. int uv__tcp_keepalive(int fd, int on, unsigned int delay);
  136. /* pipe */
  137. int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
  138. /* timer */
  139. void uv__run_timers(uv_loop_t* loop);
  140. unsigned int uv__next_timeout(uv_loop_t* loop);
  141. /* signal */
  142. void uv__signal_close(uv_signal_t* handle);
  143. void uv__signal_global_once_init(void);
  144. void uv__signal_loop_cleanup();
  145. /* thread pool */
  146. void uv__work_submit(uv_loop_t* loop,
  147. struct uv__work *w,
  148. void (*work)(struct uv__work *w),
  149. void (*done)(struct uv__work *w));
  150. void uv__work_done(uv_async_t* handle, int status);
  151. /* platform specific */
  152. int uv__kqueue_init(uv_loop_t* loop);
  153. int uv__platform_loop_init(uv_loop_t* loop, int default_loop);
  154. void uv__platform_loop_delete(uv_loop_t* loop);
  155. /* various */
  156. void uv__async_close(uv_async_t* handle);
  157. void uv__check_close(uv_check_t* handle);
  158. void uv__fs_event_close(uv_fs_event_t* handle);
  159. void uv__idle_close(uv_idle_t* handle);
  160. void uv__pipe_close(uv_pipe_t* handle);
  161. void uv__poll_close(uv_poll_t* handle);
  162. void uv__prepare_close(uv_prepare_t* handle);
  163. void uv__process_close(uv_process_t* handle);
  164. void uv__stream_close(uv_stream_t* handle);
  165. void uv__tcp_close(uv_tcp_t* handle);
  166. void uv__timer_close(uv_timer_t* handle);
  167. void uv__udp_close(uv_udp_t* handle);
  168. void uv__udp_finish_close(uv_udp_t* handle);
  169. #ifdef UV__O_NONBLOCK
  170. # define UV__F_NONBLOCK UV__O_NONBLOCK
  171. #else
  172. # define UV__F_NONBLOCK 1
  173. #endif
  174. int uv__make_socketpair(int fds[2], int flags);
  175. int uv__make_pipe(int fds[2], int flags);
  176. #if defined(__APPLE__)
  177. typedef void (*cf_loop_signal_cb)(void*);
  178. void uv__cf_loop_signal(uv_loop_t* loop, cf_loop_signal_cb cb, void* arg);
  179. int uv__fsevents_init(uv_fs_event_t* handle);
  180. int uv__fsevents_close(uv_fs_event_t* handle);
  181. /* OSX < 10.7 has no file events, polyfill them */
  182. #ifndef MAC_OS_X_VERSION_10_7
  183. static const int kFSEventStreamCreateFlagFileEvents = 0x00000010;
  184. static const int kFSEventStreamEventFlagItemCreated = 0x00000100;
  185. static const int kFSEventStreamEventFlagItemRemoved = 0x00000200;
  186. static const int kFSEventStreamEventFlagItemInodeMetaMod = 0x00000400;
  187. static const int kFSEventStreamEventFlagItemRenamed = 0x00000800;
  188. static const int kFSEventStreamEventFlagItemModified = 0x00001000;
  189. static const int kFSEventStreamEventFlagItemFinderInfoMod = 0x00002000;
  190. static const int kFSEventStreamEventFlagItemChangeOwner = 0x00004000;
  191. static const int kFSEventStreamEventFlagItemXattrMod = 0x00008000;
  192. static const int kFSEventStreamEventFlagItemIsFile = 0x00010000;
  193. static const int kFSEventStreamEventFlagItemIsDir = 0x00020000;
  194. static const int kFSEventStreamEventFlagItemIsSymlink = 0x00040000;
  195. #endif /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070 */
  196. #endif /* defined(__APPLE__) */
  197. #endif /* UV_UNIX_INTERNAL_H_ */