uv.h 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  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. /* See http://nikhilm.github.com/uvbook/ for an introduction. */
  22. #ifndef UV_H
  23. #define UV_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #ifdef _WIN32
  28. /* Windows - set up dll import/export decorators. */
  29. # if defined(BUILDING_UV_SHARED)
  30. /* Building shared library. */
  31. # define UV_EXTERN __declspec(dllexport)
  32. # elif defined(USING_UV_SHARED)
  33. /* Using shared library. */
  34. # define UV_EXTERN __declspec(dllimport)
  35. # else
  36. /* Building static library. */
  37. # define UV_EXTERN /* nothing */
  38. # endif
  39. #elif __GNUC__ >= 4
  40. # define UV_EXTERN __attribute__((visibility("default")))
  41. #else
  42. # define UV_EXTERN /* nothing */
  43. #endif
  44. #define UV_VERSION_MAJOR 0
  45. #define UV_VERSION_MINOR 10
  46. #if defined(_MSC_VER) && _MSC_VER < 1600
  47. # include "uv-private/stdint-msvc2008.h"
  48. #else
  49. # include <stdint.h>
  50. #endif
  51. #include <sys/types.h> /* size_t */
  52. #if defined(__SVR4) && !defined(__unix__)
  53. # define __unix__
  54. #endif
  55. #if defined(__unix__) || defined(__POSIX__) || \
  56. defined(__APPLE__) || defined(_AIX)
  57. # include "uv-private/uv-unix.h"
  58. #else
  59. # include "uv-private/uv-win.h"
  60. #endif
  61. /* Expand this list if necessary. */
  62. #define UV_ERRNO_MAP(XX) \
  63. XX( -1, UNKNOWN, "unknown error") \
  64. XX( 0, OK, "success") \
  65. XX( 1, EOF, "end of file") \
  66. XX( 2, EADDRINFO, "getaddrinfo error") \
  67. XX( 3, EACCES, "permission denied") \
  68. XX( 4, EAGAIN, "resource temporarily unavailable") \
  69. XX( 5, EADDRINUSE, "address already in use") \
  70. XX( 6, EADDRNOTAVAIL, "address not available") \
  71. XX( 7, EAFNOSUPPORT, "address family not supported") \
  72. XX( 8, EALREADY, "connection already in progress") \
  73. XX( 9, EBADF, "bad file descriptor") \
  74. XX( 10, EBUSY, "resource busy or locked") \
  75. XX( 11, ECONNABORTED, "software caused connection abort") \
  76. XX( 12, ECONNREFUSED, "connection refused") \
  77. XX( 13, ECONNRESET, "connection reset by peer") \
  78. XX( 14, EDESTADDRREQ, "destination address required") \
  79. XX( 15, EFAULT, "bad address in system call argument") \
  80. XX( 16, EHOSTUNREACH, "host is unreachable") \
  81. XX( 17, EINTR, "interrupted system call") \
  82. XX( 18, EINVAL, "invalid argument") \
  83. XX( 19, EISCONN, "socket is already connected") \
  84. XX( 20, EMFILE, "too many open files") \
  85. XX( 21, EMSGSIZE, "message too long") \
  86. XX( 22, ENETDOWN, "network is down") \
  87. XX( 23, ENETUNREACH, "network is unreachable") \
  88. XX( 24, ENFILE, "file table overflow") \
  89. XX( 25, ENOBUFS, "no buffer space available") \
  90. XX( 26, ENOMEM, "not enough memory") \
  91. XX( 27, ENOTDIR, "not a directory") \
  92. XX( 28, EISDIR, "illegal operation on a directory") \
  93. XX( 29, ENONET, "machine is not on the network") \
  94. XX( 31, ENOTCONN, "socket is not connected") \
  95. XX( 32, ENOTSOCK, "socket operation on non-socket") \
  96. XX( 33, ENOTSUP, "operation not supported on socket") \
  97. XX( 34, ENOENT, "no such file or directory") \
  98. XX( 35, ENOSYS, "function not implemented") \
  99. XX( 36, EPIPE, "broken pipe") \
  100. XX( 37, EPROTO, "protocol error") \
  101. XX( 38, EPROTONOSUPPORT, "protocol not supported") \
  102. XX( 39, EPROTOTYPE, "protocol wrong type for socket") \
  103. XX( 40, ETIMEDOUT, "connection timed out") \
  104. XX( 41, ECHARSET, "invalid Unicode character") \
  105. XX( 42, EAIFAMNOSUPPORT, "address family for hostname not supported") \
  106. XX( 44, EAISERVICE, "servname not supported for ai_socktype") \
  107. XX( 45, EAISOCKTYPE, "ai_socktype not supported") \
  108. XX( 46, ESHUTDOWN, "cannot send after transport endpoint shutdown") \
  109. XX( 47, EEXIST, "file already exists") \
  110. XX( 48, ESRCH, "no such process") \
  111. XX( 49, ENAMETOOLONG, "name too long") \
  112. XX( 50, EPERM, "operation not permitted") \
  113. XX( 51, ELOOP, "too many symbolic links encountered") \
  114. XX( 52, EXDEV, "cross-device link not permitted") \
  115. XX( 53, ENOTEMPTY, "directory not empty") \
  116. XX( 54, ENOSPC, "no space left on device") \
  117. XX( 55, EIO, "i/o error") \
  118. XX( 56, EROFS, "read-only file system") \
  119. XX( 57, ENODEV, "no such device") \
  120. XX( 58, ESPIPE, "invalid seek") \
  121. XX( 59, ECANCELED, "operation canceled") \
  122. #define UV_ERRNO_GEN(val, name, s) UV_##name = val,
  123. typedef enum {
  124. UV_ERRNO_MAP(UV_ERRNO_GEN)
  125. UV_MAX_ERRORS
  126. } uv_err_code;
  127. #undef UV_ERRNO_GEN
  128. #define UV_HANDLE_TYPE_MAP(XX) \
  129. XX(ASYNC, async) \
  130. XX(CHECK, check) \
  131. XX(FS_EVENT, fs_event) \
  132. XX(FS_POLL, fs_poll) \
  133. XX(HANDLE, handle) \
  134. XX(IDLE, idle) \
  135. XX(NAMED_PIPE, pipe) \
  136. XX(POLL, poll) \
  137. XX(PREPARE, prepare) \
  138. XX(PROCESS, process) \
  139. XX(STREAM, stream) \
  140. XX(TCP, tcp) \
  141. XX(TIMER, timer) \
  142. XX(TTY, tty) \
  143. XX(UDP, udp) \
  144. XX(SIGNAL, signal) \
  145. #define UV_REQ_TYPE_MAP(XX) \
  146. XX(REQ, req) \
  147. XX(CONNECT, connect) \
  148. XX(WRITE, write) \
  149. XX(SHUTDOWN, shutdown) \
  150. XX(UDP_SEND, udp_send) \
  151. XX(FS, fs) \
  152. XX(WORK, work) \
  153. XX(GETADDRINFO, getaddrinfo) \
  154. typedef enum {
  155. UV_UNKNOWN_HANDLE = 0,
  156. #define XX(uc, lc) UV_##uc,
  157. UV_HANDLE_TYPE_MAP(XX)
  158. #undef XX
  159. UV_FILE,
  160. UV_HANDLE_TYPE_MAX
  161. } uv_handle_type;
  162. typedef enum {
  163. UV_UNKNOWN_REQ = 0,
  164. #define XX(uc, lc) UV_##uc,
  165. UV_REQ_TYPE_MAP(XX)
  166. #undef XX
  167. UV_REQ_TYPE_PRIVATE
  168. UV_REQ_TYPE_MAX
  169. } uv_req_type;
  170. /* Handle types. */
  171. typedef struct uv_loop_s uv_loop_t;
  172. typedef struct uv_err_s uv_err_t;
  173. typedef struct uv_handle_s uv_handle_t;
  174. typedef struct uv_stream_s uv_stream_t;
  175. typedef struct uv_tcp_s uv_tcp_t;
  176. typedef struct uv_udp_s uv_udp_t;
  177. typedef struct uv_pipe_s uv_pipe_t;
  178. typedef struct uv_tty_s uv_tty_t;
  179. typedef struct uv_poll_s uv_poll_t;
  180. typedef struct uv_timer_s uv_timer_t;
  181. typedef struct uv_prepare_s uv_prepare_t;
  182. typedef struct uv_check_s uv_check_t;
  183. typedef struct uv_idle_s uv_idle_t;
  184. typedef struct uv_async_s uv_async_t;
  185. typedef struct uv_process_s uv_process_t;
  186. typedef struct uv_fs_event_s uv_fs_event_t;
  187. typedef struct uv_fs_poll_s uv_fs_poll_t;
  188. typedef struct uv_signal_s uv_signal_t;
  189. /* Request types. */
  190. typedef struct uv_req_s uv_req_t;
  191. typedef struct uv_getaddrinfo_s uv_getaddrinfo_t;
  192. typedef struct uv_shutdown_s uv_shutdown_t;
  193. typedef struct uv_write_s uv_write_t;
  194. typedef struct uv_connect_s uv_connect_t;
  195. typedef struct uv_udp_send_s uv_udp_send_t;
  196. typedef struct uv_fs_s uv_fs_t;
  197. typedef struct uv_work_s uv_work_t;
  198. /* None of the above. */
  199. typedef struct uv_cpu_info_s uv_cpu_info_t;
  200. typedef struct uv_interface_address_s uv_interface_address_t;
  201. typedef enum {
  202. UV_RUN_DEFAULT = 0,
  203. UV_RUN_ONCE,
  204. UV_RUN_NOWAIT
  205. } uv_run_mode;
  206. /*
  207. * Returns the libuv version packed into a single integer. 8 bits are used for
  208. * each component, with the patch number stored in the 8 least significant
  209. * bits. E.g. for libuv 1.2.3 this would return 0x010203.
  210. */
  211. UV_EXTERN unsigned int uv_version(void);
  212. /*
  213. * Returns the libuv version number as a string. For non-release versions
  214. * "-pre" is appended, so the version number could be "1.2.3-pre".
  215. */
  216. UV_EXTERN const char* uv_version_string(void);
  217. /*
  218. * This function must be called before any other functions in libuv.
  219. *
  220. * All functions besides uv_run() are non-blocking.
  221. *
  222. * All callbacks in libuv are made asynchronously. That is they are never
  223. * made by the function that takes them as a parameter.
  224. */
  225. UV_EXTERN uv_loop_t* uv_loop_new(void);
  226. UV_EXTERN void uv_loop_delete(uv_loop_t*);
  227. /*
  228. * Returns the default loop.
  229. */
  230. UV_EXTERN uv_loop_t* uv_default_loop(void);
  231. /*
  232. * This function runs the event loop. It will act differently depending on the
  233. * specified mode:
  234. * - UV_RUN_DEFAULT: Runs the event loop until the reference count drops to
  235. * zero. Always returns zero.
  236. * - UV_RUN_ONCE: Poll for new events once. Note that this function blocks if
  237. * there are no pending events. Returns zero when done (no active handles
  238. * or requests left), or non-zero if more events are expected (meaning you
  239. * should run the event loop again sometime in the future).
  240. * - UV_RUN_NOWAIT: Poll for new events once but don't block if there are no
  241. * pending events.
  242. */
  243. UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode);
  244. /*
  245. * This function will stop the event loop by forcing uv_run to end
  246. * as soon as possible, but not sooner than the next loop iteration.
  247. * If this function was called before blocking for i/o, the loop won't
  248. * block for i/o on this iteration.
  249. */
  250. UV_EXTERN void uv_stop(uv_loop_t*);
  251. /*
  252. * Manually modify the event loop's reference count. Useful if the user wants
  253. * to have a handle or timeout that doesn't keep the loop alive.
  254. */
  255. UV_EXTERN void uv_ref(uv_handle_t*);
  256. UV_EXTERN void uv_unref(uv_handle_t*);
  257. UV_EXTERN void uv_update_time(uv_loop_t*);
  258. UV_EXTERN uint64_t uv_now(uv_loop_t*);
  259. /*
  260. * Get backend file descriptor. Only kqueue, epoll and event ports are
  261. * supported.
  262. *
  263. * This can be used in conjunction with `uv_run(loop, UV_RUN_NOWAIT)` to
  264. * poll in one thread and run the event loop's event callbacks in another.
  265. *
  266. * Useful for embedding libuv's event loop in another event loop.
  267. * See test/test-embed.c for an example.
  268. *
  269. * Note that embedding a kqueue fd in another kqueue pollset doesn't work on
  270. * all platforms. It's not an error to add the fd but it never generates
  271. * events.
  272. */
  273. UV_EXTERN int uv_backend_fd(const uv_loop_t*);
  274. /*
  275. * Get the poll timeout. The return value is in milliseconds, or -1 for no
  276. * timeout.
  277. */
  278. UV_EXTERN int uv_backend_timeout(const uv_loop_t*);
  279. /*
  280. * Should return a buffer that libuv can use to read data into.
  281. *
  282. * `suggested_size` is a hint. Returning a buffer that is smaller is perfectly
  283. * okay as long as `buf.len > 0`.
  284. */
  285. typedef uv_buf_t (*uv_alloc_cb)(uv_handle_t* handle, size_t suggested_size);
  286. /*
  287. * `nread` is > 0 if there is data available, 0 if libuv is done reading for now
  288. * or -1 on error.
  289. *
  290. * Error details can be obtained by calling uv_last_error(). UV_EOF indicates
  291. * that the stream has been closed.
  292. *
  293. * The callee is responsible for closing the stream when an error happens.
  294. * Trying to read from the stream again is undefined.
  295. *
  296. * The callee is responsible for freeing the buffer, libuv does not reuse it.
  297. */
  298. typedef void (*uv_read_cb)(uv_stream_t* stream, ssize_t nread, uv_buf_t buf);
  299. /*
  300. * Just like the uv_read_cb except that if the pending parameter is true
  301. * then you can use uv_accept() to pull the new handle into the process.
  302. * If no handle is pending then pending will be UV_UNKNOWN_HANDLE.
  303. */
  304. typedef void (*uv_read2_cb)(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf,
  305. uv_handle_type pending);
  306. typedef void (*uv_write_cb)(uv_write_t* req, int status);
  307. typedef void (*uv_connect_cb)(uv_connect_t* req, int status);
  308. typedef void (*uv_shutdown_cb)(uv_shutdown_t* req, int status);
  309. typedef void (*uv_connection_cb)(uv_stream_t* server, int status);
  310. typedef void (*uv_close_cb)(uv_handle_t* handle);
  311. typedef void (*uv_poll_cb)(uv_poll_t* handle, int status, int events);
  312. typedef void (*uv_timer_cb)(uv_timer_t* handle, int status);
  313. /* TODO: do these really need a status argument? */
  314. typedef void (*uv_async_cb)(uv_async_t* handle, int status);
  315. typedef void (*uv_prepare_cb)(uv_prepare_t* handle, int status);
  316. typedef void (*uv_check_cb)(uv_check_t* handle, int status);
  317. typedef void (*uv_idle_cb)(uv_idle_t* handle, int status);
  318. typedef void (*uv_exit_cb)(uv_process_t*, int exit_status, int term_signal);
  319. typedef void (*uv_walk_cb)(uv_handle_t* handle, void* arg);
  320. typedef void (*uv_fs_cb)(uv_fs_t* req);
  321. typedef void (*uv_work_cb)(uv_work_t* req);
  322. typedef void (*uv_after_work_cb)(uv_work_t* req, int status);
  323. typedef void (*uv_getaddrinfo_cb)(uv_getaddrinfo_t* req,
  324. int status,
  325. struct addrinfo* res);
  326. /*
  327. * This will be called repeatedly after the uv_fs_event_t is initialized.
  328. * If uv_fs_event_t was initialized with a directory the filename parameter
  329. * will be a relative path to a file contained in the directory.
  330. * The events parameter is an ORed mask of enum uv_fs_event elements.
  331. */
  332. typedef void (*uv_fs_event_cb)(uv_fs_event_t* handle, const char* filename,
  333. int events, int status);
  334. typedef void (*uv_fs_poll_cb)(uv_fs_poll_t* handle,
  335. int status,
  336. const uv_statbuf_t* prev,
  337. const uv_statbuf_t* curr);
  338. typedef void (*uv_signal_cb)(uv_signal_t* handle, int signum);
  339. typedef enum {
  340. UV_LEAVE_GROUP = 0,
  341. UV_JOIN_GROUP
  342. } uv_membership;
  343. struct uv_err_s {
  344. /* read-only */
  345. uv_err_code code;
  346. /* private */
  347. int sys_errno_;
  348. };
  349. /*
  350. * Most functions return boolean: 0 for success and -1 for failure.
  351. * On error the user should then call uv_last_error() to determine
  352. * the error code.
  353. */
  354. UV_EXTERN uv_err_t uv_last_error(uv_loop_t*);
  355. UV_EXTERN const char* uv_strerror(uv_err_t err);
  356. UV_EXTERN const char* uv_err_name(uv_err_t err);
  357. #define UV_REQ_FIELDS \
  358. /* public */ \
  359. void* data; \
  360. /* read-only */ \
  361. uv_req_type type; \
  362. /* private */ \
  363. ngx_queue_t active_queue; \
  364. UV_REQ_PRIVATE_FIELDS \
  365. /* Abstract base class of all requests. */
  366. struct uv_req_s {
  367. UV_REQ_FIELDS
  368. };
  369. /* Platform-specific request types */
  370. UV_PRIVATE_REQ_TYPES
  371. /*
  372. * uv_shutdown_t is a subclass of uv_req_t
  373. *
  374. * Shutdown the outgoing (write) side of a duplex stream. It waits for
  375. * pending write requests to complete. The handle should refer to a
  376. * initialized stream. req should be an uninitialized shutdown request
  377. * struct. The cb is called after shutdown is complete.
  378. */
  379. UV_EXTERN int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle,
  380. uv_shutdown_cb cb);
  381. struct uv_shutdown_s {
  382. UV_REQ_FIELDS
  383. uv_stream_t* handle;
  384. uv_shutdown_cb cb;
  385. UV_SHUTDOWN_PRIVATE_FIELDS
  386. };
  387. #define UV_HANDLE_FIELDS \
  388. /* public */ \
  389. uv_close_cb close_cb; \
  390. void* data; \
  391. /* read-only */ \
  392. uv_loop_t* loop; \
  393. uv_handle_type type; \
  394. /* private */ \
  395. ngx_queue_t handle_queue; \
  396. UV_HANDLE_PRIVATE_FIELDS \
  397. /* The abstract base class of all handles. */
  398. struct uv_handle_s {
  399. UV_HANDLE_FIELDS
  400. };
  401. /*
  402. * Returns size of various handle types, useful for FFI
  403. * bindings to allocate correct memory without copying struct
  404. * definitions
  405. */
  406. UV_EXTERN size_t uv_handle_size(uv_handle_type type);
  407. /*
  408. * Returns size of request types, useful for dynamic lookup with FFI
  409. */
  410. UV_EXTERN size_t uv_req_size(uv_req_type type);
  411. /*
  412. * Returns 1 if the prepare/check/idle/timer handle has been started, 0
  413. * otherwise. For other handle types this always returns 1.
  414. */
  415. UV_EXTERN int uv_is_active(const uv_handle_t* handle);
  416. /*
  417. * Walk the list of open handles.
  418. */
  419. UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
  420. /*
  421. * Request handle to be closed. close_cb will be called asynchronously after
  422. * this call. This MUST be called on each handle before memory is released.
  423. *
  424. * Note that handles that wrap file descriptors are closed immediately but
  425. * close_cb will still be deferred to the next iteration of the event loop.
  426. * It gives you a chance to free up any resources associated with the handle.
  427. *
  428. * In-progress requests, like uv_connect_t or uv_write_t, are cancelled and
  429. * have their callbacks called asynchronously with status=-1 and the error code
  430. * set to UV_ECANCELED.
  431. */
  432. UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
  433. /*
  434. * Constructor for uv_buf_t.
  435. * Due to platform differences the user cannot rely on the ordering of the
  436. * base and len members of the uv_buf_t struct. The user is responsible for
  437. * freeing base after the uv_buf_t is done. Return struct passed by value.
  438. */
  439. UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len);
  440. /*
  441. * Utility function. Copies up to `size` characters from `src` to `dst`
  442. * and ensures that `dst` is properly NUL terminated unless `size` is zero.
  443. */
  444. UV_EXTERN size_t uv_strlcpy(char* dst, const char* src, size_t size);
  445. /*
  446. * Utility function. Appends `src` to `dst` and ensures that `dst` is
  447. * properly NUL terminated unless `size` is zero or `dst` does not
  448. * contain a NUL byte. `size` is the total length of `dst` so at most
  449. * `size - strlen(dst) - 1` characters will be copied from `src`.
  450. */
  451. UV_EXTERN size_t uv_strlcat(char* dst, const char* src, size_t size);
  452. #define UV_STREAM_FIELDS \
  453. /* number of bytes queued for writing */ \
  454. size_t write_queue_size; \
  455. uv_alloc_cb alloc_cb; \
  456. uv_read_cb read_cb; \
  457. uv_read2_cb read2_cb; \
  458. /* private */ \
  459. UV_STREAM_PRIVATE_FIELDS
  460. /*
  461. * uv_stream_t is a subclass of uv_handle_t
  462. *
  463. * uv_stream is an abstract class.
  464. *
  465. * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t, uv_tty_t, and
  466. * soon uv_file_t.
  467. */
  468. struct uv_stream_s {
  469. UV_HANDLE_FIELDS
  470. UV_STREAM_FIELDS
  471. };
  472. UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb);
  473. /*
  474. * This call is used in conjunction with uv_listen() to accept incoming
  475. * connections. Call uv_accept after receiving a uv_connection_cb to accept
  476. * the connection. Before calling uv_accept use uv_*_init() must be
  477. * called on the client. Non-zero return value indicates an error.
  478. *
  479. * When the uv_connection_cb is called it is guaranteed that uv_accept will
  480. * complete successfully the first time. If you attempt to use it more than
  481. * once, it may fail. It is suggested to only call uv_accept once per
  482. * uv_connection_cb call.
  483. */
  484. UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client);
  485. /*
  486. * Read data from an incoming stream. The callback will be made several
  487. * several times until there is no more data to read or uv_read_stop is
  488. * called. When we've reached EOF nread will be set to -1 and the error is
  489. * set to UV_EOF. When nread == -1 the buf parameter might not point to a
  490. * valid buffer; in that case buf.len and buf.base are both set to 0.
  491. * Note that nread might also be 0, which does *not* indicate an error or
  492. * eof; it happens when libuv requested a buffer through the alloc callback
  493. * but then decided that it didn't need that buffer.
  494. */
  495. UV_EXTERN int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb,
  496. uv_read_cb read_cb);
  497. UV_EXTERN int uv_read_stop(uv_stream_t*);
  498. /*
  499. * Extended read methods for receiving handles over a pipe. The pipe must be
  500. * initialized with ipc == 1.
  501. */
  502. UV_EXTERN int uv_read2_start(uv_stream_t*, uv_alloc_cb alloc_cb,
  503. uv_read2_cb read_cb);
  504. /*
  505. * Write data to stream. Buffers are written in order. Example:
  506. *
  507. * uv_buf_t a[] = {
  508. * { .base = "1", .len = 1 },
  509. * { .base = "2", .len = 1 }
  510. * };
  511. *
  512. * uv_buf_t b[] = {
  513. * { .base = "3", .len = 1 },
  514. * { .base = "4", .len = 1 }
  515. * };
  516. *
  517. * uv_write_t req1;
  518. * uv_write_t req2;
  519. *
  520. * // writes "1234"
  521. * uv_write(&req1, stream, a, 2);
  522. * uv_write(&req2, stream, b, 2);
  523. *
  524. */
  525. UV_EXTERN int uv_write(uv_write_t* req, uv_stream_t* handle,
  526. uv_buf_t bufs[], int bufcnt, uv_write_cb cb);
  527. /*
  528. * Extended write function for sending handles over a pipe. The pipe must be
  529. * initialized with ipc == 1.
  530. * send_handle must be a TCP socket or pipe, which is a server or a connection
  531. * (listening or connected state). Bound sockets or pipes will be assumed to
  532. * be servers.
  533. */
  534. UV_EXTERN int uv_write2(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[],
  535. int bufcnt, uv_stream_t* send_handle, uv_write_cb cb);
  536. /* uv_write_t is a subclass of uv_req_t */
  537. struct uv_write_s {
  538. UV_REQ_FIELDS
  539. uv_write_cb cb;
  540. uv_stream_t* send_handle;
  541. uv_stream_t* handle;
  542. UV_WRITE_PRIVATE_FIELDS
  543. };
  544. /*
  545. * Used to determine whether a stream is readable or writable.
  546. */
  547. UV_EXTERN int uv_is_readable(const uv_stream_t* handle);
  548. UV_EXTERN int uv_is_writable(const uv_stream_t* handle);
  549. /*
  550. * Used to determine whether a stream is closing or closed.
  551. *
  552. * N.B. is only valid between the initialization of the handle
  553. * and the arrival of the close callback, and cannot be used
  554. * to validate the handle.
  555. */
  556. UV_EXTERN int uv_is_closing(const uv_handle_t* handle);
  557. /*
  558. * uv_tcp_t is a subclass of uv_stream_t
  559. *
  560. * Represents a TCP stream or TCP server.
  561. */
  562. struct uv_tcp_s {
  563. UV_HANDLE_FIELDS
  564. UV_STREAM_FIELDS
  565. UV_TCP_PRIVATE_FIELDS
  566. };
  567. UV_EXTERN int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle);
  568. /*
  569. * Opens an existing file descriptor or SOCKET as a tcp handle.
  570. */
  571. UV_EXTERN int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock);
  572. /* Enable/disable Nagle's algorithm. */
  573. UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable);
  574. /*
  575. * Enable/disable TCP keep-alive.
  576. *
  577. * `delay` is the initial delay in seconds, ignored when `enable` is zero.
  578. */
  579. UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle,
  580. int enable,
  581. unsigned int delay);
  582. /*
  583. * Enable/disable simultaneous asynchronous accept requests that are
  584. * queued by the operating system when listening for new tcp connections.
  585. * This setting is used to tune a tcp server for the desired performance.
  586. * Having simultaneous accepts can significantly improve the rate of
  587. * accepting connections (which is why it is enabled by default) but
  588. * may lead to uneven load distribution in multi-process setups.
  589. */
  590. UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable);
  591. UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr_in);
  592. UV_EXTERN int uv_tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6);
  593. UV_EXTERN int uv_tcp_getsockname(uv_tcp_t* handle, struct sockaddr* name,
  594. int* namelen);
  595. UV_EXTERN int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name,
  596. int* namelen);
  597. /*
  598. * uv_tcp_connect, uv_tcp_connect6
  599. * These functions establish IPv4 and IPv6 TCP connections. Provide an
  600. * initialized TCP handle and an uninitialized uv_connect_t*. The callback
  601. * will be made when the connection is established.
  602. */
  603. UV_EXTERN int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle,
  604. struct sockaddr_in address, uv_connect_cb cb);
  605. UV_EXTERN int uv_tcp_connect6(uv_connect_t* req, uv_tcp_t* handle,
  606. struct sockaddr_in6 address, uv_connect_cb cb);
  607. /* uv_connect_t is a subclass of uv_req_t */
  608. struct uv_connect_s {
  609. UV_REQ_FIELDS
  610. uv_connect_cb cb;
  611. uv_stream_t* handle;
  612. UV_CONNECT_PRIVATE_FIELDS
  613. };
  614. /*
  615. * UDP support.
  616. */
  617. enum uv_udp_flags {
  618. /* Disables dual stack mode. Used with uv_udp_bind6(). */
  619. UV_UDP_IPV6ONLY = 1,
  620. /*
  621. * Indicates message was truncated because read buffer was too small. The
  622. * remainder was discarded by the OS. Used in uv_udp_recv_cb.
  623. */
  624. UV_UDP_PARTIAL = 2
  625. };
  626. /*
  627. * Called after a uv_udp_send() or uv_udp_send6(). status 0 indicates
  628. * success otherwise error.
  629. */
  630. typedef void (*uv_udp_send_cb)(uv_udp_send_t* req, int status);
  631. /*
  632. * Callback that is invoked when a new UDP datagram is received.
  633. *
  634. * handle UDP handle.
  635. * nread Number of bytes that have been received.
  636. * 0 if there is no more data to read. You may
  637. * discard or repurpose the read buffer.
  638. * -1 if a transmission error was detected.
  639. * buf uv_buf_t with the received data.
  640. * addr struct sockaddr_in or struct sockaddr_in6.
  641. * Valid for the duration of the callback only.
  642. * flags One or more OR'ed UV_UDP_* constants.
  643. * Right now only UV_UDP_PARTIAL is used.
  644. */
  645. typedef void (*uv_udp_recv_cb)(uv_udp_t* handle, ssize_t nread, uv_buf_t buf,
  646. struct sockaddr* addr, unsigned flags);
  647. /* uv_udp_t is a subclass of uv_handle_t */
  648. struct uv_udp_s {
  649. UV_HANDLE_FIELDS
  650. UV_UDP_PRIVATE_FIELDS
  651. };
  652. /* uv_udp_send_t is a subclass of uv_req_t */
  653. struct uv_udp_send_s {
  654. UV_REQ_FIELDS
  655. uv_udp_t* handle;
  656. uv_udp_send_cb cb;
  657. UV_UDP_SEND_PRIVATE_FIELDS
  658. };
  659. /*
  660. * Initialize a new UDP handle. The actual socket is created lazily.
  661. * Returns 0 on success.
  662. */
  663. UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle);
  664. /*
  665. * Opens an existing file descriptor or SOCKET as a udp handle.
  666. */
  667. UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);
  668. /*
  669. * Bind to a IPv4 address and port.
  670. *
  671. * Arguments:
  672. * handle UDP handle. Should have been initialized with `uv_udp_init`.
  673. * addr struct sockaddr_in with the address and port to bind to.
  674. * flags Unused.
  675. *
  676. * Returns:
  677. * 0 on success, -1 on error.
  678. */
  679. UV_EXTERN int uv_udp_bind(uv_udp_t* handle, struct sockaddr_in addr,
  680. unsigned flags);
  681. /*
  682. * Bind to a IPv6 address and port.
  683. *
  684. * Arguments:
  685. * handle UDP handle. Should have been initialized with `uv_udp_init`.
  686. * addr struct sockaddr_in with the address and port to bind to.
  687. * flags Should be 0 or UV_UDP_IPV6ONLY.
  688. *
  689. * Returns:
  690. * 0 on success, -1 on error.
  691. */
  692. UV_EXTERN int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr,
  693. unsigned flags);
  694. UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
  695. int* namelen);
  696. /*
  697. * Set membership for a multicast address
  698. *
  699. * Arguments:
  700. * handle UDP handle. Should have been initialized with
  701. * `uv_udp_init`.
  702. * multicast_addr multicast address to set membership for
  703. * interface_addr interface address
  704. * membership Should be UV_JOIN_GROUP or UV_LEAVE_GROUP
  705. *
  706. * Returns:
  707. * 0 on success, -1 on error.
  708. */
  709. UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle,
  710. const char* multicast_addr, const char* interface_addr,
  711. uv_membership membership);
  712. /*
  713. * Set IP multicast loop flag. Makes multicast packets loop back to
  714. * local sockets.
  715. *
  716. * Arguments:
  717. * handle UDP handle. Should have been initialized with
  718. * `uv_udp_init`.
  719. * on 1 for on, 0 for off
  720. *
  721. * Returns:
  722. * 0 on success, -1 on error.
  723. */
  724. UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);
  725. /*
  726. * Set the multicast ttl
  727. *
  728. * Arguments:
  729. * handle UDP handle. Should have been initialized with
  730. * `uv_udp_init`.
  731. * ttl 1 through 255
  732. *
  733. * Returns:
  734. * 0 on success, -1 on error.
  735. */
  736. UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
  737. /*
  738. * Set broadcast on or off
  739. *
  740. * Arguments:
  741. * handle UDP handle. Should have been initialized with
  742. * `uv_udp_init`.
  743. * on 1 for on, 0 for off
  744. *
  745. * Returns:
  746. * 0 on success, -1 on error.
  747. */
  748. UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on);
  749. /*
  750. * Set the time to live
  751. *
  752. * Arguments:
  753. * handle UDP handle. Should have been initialized with
  754. * `uv_udp_init`.
  755. * ttl 1 through 255
  756. *
  757. * Returns:
  758. * 0 on success, -1 on error.
  759. */
  760. UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl);
  761. /*
  762. * Send data. If the socket has not previously been bound with `uv_udp_bind`
  763. * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
  764. * and a random port number.
  765. *
  766. * Arguments:
  767. * req UDP request handle. Need not be initialized.
  768. * handle UDP handle. Should have been initialized with `uv_udp_init`.
  769. * bufs List of buffers to send.
  770. * bufcnt Number of buffers in `bufs`.
  771. * addr Address of the remote peer. See `uv_ip4_addr`.
  772. * send_cb Callback to invoke when the data has been sent out.
  773. *
  774. * Returns:
  775. * 0 on success, -1 on error.
  776. */
  777. UV_EXTERN int uv_udp_send(uv_udp_send_t* req, uv_udp_t* handle,
  778. uv_buf_t bufs[], int bufcnt, struct sockaddr_in addr,
  779. uv_udp_send_cb send_cb);
  780. /*
  781. * Send data. If the socket has not previously been bound with `uv_udp_bind6`,
  782. * it is bound to ::0 (the "all interfaces" address) and a random port number.
  783. *
  784. * Arguments:
  785. * req UDP request handle. Need not be initialized.
  786. * handle UDP handle. Should have been initialized with `uv_udp_init`.
  787. * bufs List of buffers to send.
  788. * bufcnt Number of buffers in `bufs`.
  789. * addr Address of the remote peer. See `uv_ip6_addr`.
  790. * send_cb Callback to invoke when the data has been sent out.
  791. *
  792. * Returns:
  793. * 0 on success, -1 on error.
  794. */
  795. UV_EXTERN int uv_udp_send6(uv_udp_send_t* req, uv_udp_t* handle,
  796. uv_buf_t bufs[], int bufcnt, struct sockaddr_in6 addr,
  797. uv_udp_send_cb send_cb);
  798. /*
  799. * Receive data. If the socket has not previously been bound with `uv_udp_bind`
  800. * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
  801. * and a random port number.
  802. *
  803. * Arguments:
  804. * handle UDP handle. Should have been initialized with `uv_udp_init`.
  805. * alloc_cb Callback to invoke when temporary storage is needed.
  806. * recv_cb Callback to invoke with received data.
  807. *
  808. * Returns:
  809. * 0 on success, -1 on error.
  810. */
  811. UV_EXTERN int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
  812. uv_udp_recv_cb recv_cb);
  813. /*
  814. * Stop listening for incoming datagrams.
  815. *
  816. * Arguments:
  817. * handle UDP handle. Should have been initialized with `uv_udp_init`.
  818. *
  819. * Returns:
  820. * 0 on success, -1 on error.
  821. */
  822. UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle);
  823. /*
  824. * uv_tty_t is a subclass of uv_stream_t
  825. *
  826. * Representing a stream for the console.
  827. */
  828. struct uv_tty_s {
  829. UV_HANDLE_FIELDS
  830. UV_STREAM_FIELDS
  831. UV_TTY_PRIVATE_FIELDS
  832. };
  833. /*
  834. * Initialize a new TTY stream with the given file descriptor. Usually the
  835. * file descriptor will be
  836. * 0 = stdin
  837. * 1 = stdout
  838. * 2 = stderr
  839. * The last argument, readable, specifies if you plan on calling
  840. * uv_read_start with this stream. stdin is readable, stdout is not.
  841. *
  842. * TTY streams which are not readable have blocking writes.
  843. */
  844. UV_EXTERN int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable);
  845. /*
  846. * Set mode. 0 for normal, 1 for raw.
  847. */
  848. UV_EXTERN int uv_tty_set_mode(uv_tty_t*, int mode);
  849. /*
  850. * To be called when the program exits. Resets TTY settings to default
  851. * values for the next process to take over.
  852. */
  853. UV_EXTERN void uv_tty_reset_mode(void);
  854. /*
  855. * Gets the current Window size. On success zero is returned.
  856. */
  857. UV_EXTERN int uv_tty_get_winsize(uv_tty_t*, int* width, int* height);
  858. /*
  859. * Used to detect what type of stream should be used with a given file
  860. * descriptor. Usually this will be used during initialization to guess the
  861. * type of the stdio streams.
  862. * For isatty() functionality use this function and test for UV_TTY.
  863. */
  864. UV_EXTERN uv_handle_type uv_guess_handle(uv_file file);
  865. /*
  866. * uv_pipe_t is a subclass of uv_stream_t
  867. *
  868. * Representing a pipe stream or pipe server. On Windows this is a Named
  869. * Pipe. On Unix this is a UNIX domain socket.
  870. */
  871. struct uv_pipe_s {
  872. UV_HANDLE_FIELDS
  873. UV_STREAM_FIELDS
  874. int ipc; /* non-zero if this pipe is used for passing handles */
  875. UV_PIPE_PRIVATE_FIELDS
  876. };
  877. /*
  878. * Initialize a pipe. The last argument is a boolean to indicate if
  879. * this pipe will be used for handle passing between processes.
  880. */
  881. UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc);
  882. /*
  883. * Opens an existing file descriptor or HANDLE as a pipe.
  884. */
  885. UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file);
  886. UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name);
  887. UV_EXTERN void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle,
  888. const char* name, uv_connect_cb cb);
  889. /*
  890. * This setting applies to Windows only.
  891. * Set the number of pending pipe instance handles when the pipe server
  892. * is waiting for connections.
  893. */
  894. UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count);
  895. /*
  896. * uv_poll_t is a subclass of uv_handle_t.
  897. *
  898. * The uv_poll watcher is used to watch file descriptors for readability and
  899. * writability, similar to the purpose of poll(2).
  900. *
  901. * The purpose of uv_poll is to enable integrating external libraries that
  902. * rely on the event loop to signal it about the socket status changes, like
  903. * c-ares or libssh2. Using uv_poll_t for any other other purpose is not
  904. * recommended; uv_tcp_t, uv_udp_t, etc. provide an implementation that is
  905. * much faster and more scalable than what can be achieved with uv_poll_t,
  906. * especially on Windows.
  907. *
  908. * It is possible that uv_poll occasionally signals that a file descriptor is
  909. * readable or writable even when it isn't. The user should therefore always
  910. * be prepared to handle EAGAIN or equivalent when it attempts to read from or
  911. * write to the fd.
  912. *
  913. * It is not okay to have multiple active uv_poll watchers for the same socket.
  914. * This can cause libuv to busyloop or otherwise malfunction.
  915. *
  916. * The user should not close a file descriptor while it is being polled by an
  917. * active uv_poll watcher. This can cause the poll watcher to report an error,
  918. * but it might also start polling another socket. However the fd can be safely
  919. * closed immediately after a call to uv_poll_stop() or uv_close().
  920. *
  921. * On windows only sockets can be polled with uv_poll. On unix any file
  922. * descriptor that would be accepted by poll(2) can be used with uv_poll.
  923. */
  924. struct uv_poll_s {
  925. UV_HANDLE_FIELDS
  926. uv_poll_cb poll_cb;
  927. UV_POLL_PRIVATE_FIELDS
  928. };
  929. enum uv_poll_event {
  930. UV_READABLE = 1,
  931. UV_WRITABLE = 2
  932. };
  933. /* Initialize the poll watcher using a file descriptor. */
  934. UV_EXTERN int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd);
  935. /* Initialize the poll watcher using a socket descriptor. On unix this is */
  936. /* identical to uv_poll_init. On windows it takes a SOCKET handle. */
  937. UV_EXTERN int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
  938. uv_os_sock_t socket);
  939. /*
  940. * Starts polling the file descriptor. `events` is a bitmask consisting made up
  941. * of UV_READABLE and UV_WRITABLE. As soon as an event is detected the callback
  942. * will be called with `status` set to 0, and the detected events set en the
  943. * `events` field.
  944. *
  945. * If an error happens while polling status may be set to -1 and the error
  946. * code can be retrieved with uv_last_error. The user should not close the
  947. * socket while uv_poll is active. If the user does that anyway, the callback
  948. * *may* be called reporting an error status, but this is not guaranteed.
  949. *
  950. * Calling uv_poll_start on an uv_poll watcher that is already active is fine.
  951. * Doing so will update the events mask that is being watched for.
  952. */
  953. UV_EXTERN int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb);
  954. /* Stops polling the file descriptor. */
  955. UV_EXTERN int uv_poll_stop(uv_poll_t* handle);
  956. /*
  957. * uv_prepare_t is a subclass of uv_handle_t.
  958. *
  959. * Every active prepare handle gets its callback called exactly once per loop
  960. * iteration, just before the system blocks to wait for completed i/o.
  961. */
  962. struct uv_prepare_s {
  963. UV_HANDLE_FIELDS
  964. UV_PREPARE_PRIVATE_FIELDS
  965. };
  966. UV_EXTERN int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare);
  967. UV_EXTERN int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb);
  968. UV_EXTERN int uv_prepare_stop(uv_prepare_t* prepare);
  969. /*
  970. * uv_check_t is a subclass of uv_handle_t.
  971. *
  972. * Every active check handle gets its callback called exactly once per loop
  973. * iteration, just after the system returns from blocking.
  974. */
  975. struct uv_check_s {
  976. UV_HANDLE_FIELDS
  977. UV_CHECK_PRIVATE_FIELDS
  978. };
  979. UV_EXTERN int uv_check_init(uv_loop_t*, uv_check_t* check);
  980. UV_EXTERN int uv_check_start(uv_check_t* check, uv_check_cb cb);
  981. UV_EXTERN int uv_check_stop(uv_check_t* check);
  982. /*
  983. * uv_idle_t is a subclass of uv_handle_t.
  984. *
  985. * Every active idle handle gets its callback called repeatedly until it is
  986. * stopped. This happens after all other types of callbacks are processed.
  987. * When there are multiple "idle" handles active, their callbacks are called
  988. * in turn.
  989. */
  990. struct uv_idle_s {
  991. UV_HANDLE_FIELDS
  992. UV_IDLE_PRIVATE_FIELDS
  993. };
  994. UV_EXTERN int uv_idle_init(uv_loop_t*, uv_idle_t* idle);
  995. UV_EXTERN int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb);
  996. UV_EXTERN int uv_idle_stop(uv_idle_t* idle);
  997. /*
  998. * uv_async_t is a subclass of uv_handle_t.
  999. *
  1000. * uv_async_send wakes up the event loop and calls the async handle's callback.
  1001. * There is no guarantee that every uv_async_send call leads to exactly one
  1002. * invocation of the callback; the only guarantee is that the callback function
  1003. * is called at least once after the call to async_send. Unlike all other
  1004. * libuv functions, uv_async_send can be called from another thread.
  1005. */
  1006. struct uv_async_s {
  1007. UV_HANDLE_FIELDS
  1008. UV_ASYNC_PRIVATE_FIELDS
  1009. };
  1010. UV_EXTERN int uv_async_init(uv_loop_t*, uv_async_t* async,
  1011. uv_async_cb async_cb);
  1012. /*
  1013. * This can be called from other threads to wake up a libuv thread.
  1014. *
  1015. * libuv is single threaded at the moment.
  1016. */
  1017. UV_EXTERN int uv_async_send(uv_async_t* async);
  1018. /*
  1019. * uv_timer_t is a subclass of uv_handle_t.
  1020. *
  1021. * Used to get woken up at a specified time in the future.
  1022. */
  1023. struct uv_timer_s {
  1024. UV_HANDLE_FIELDS
  1025. UV_TIMER_PRIVATE_FIELDS
  1026. };
  1027. UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* handle);
  1028. /*
  1029. * Start the timer. `timeout` and `repeat` are in milliseconds.
  1030. *
  1031. * If timeout is zero, the callback fires on the next tick of the event loop.
  1032. *
  1033. * If repeat is non-zero, the callback fires first after timeout milliseconds
  1034. * and then repeatedly after repeat milliseconds.
  1035. */
  1036. UV_EXTERN int uv_timer_start(uv_timer_t* handle,
  1037. uv_timer_cb cb,
  1038. uint64_t timeout,
  1039. uint64_t repeat);
  1040. UV_EXTERN int uv_timer_stop(uv_timer_t* handle);
  1041. /*
  1042. * Stop the timer, and if it is repeating restart it using the repeat value
  1043. * as the timeout. If the timer has never been started before it returns -1 and
  1044. * sets the error to UV_EINVAL.
  1045. */
  1046. UV_EXTERN int uv_timer_again(uv_timer_t* handle);
  1047. /*
  1048. * Set the repeat value in milliseconds. Note that if the repeat value is set
  1049. * from a timer callback it does not immediately take effect. If the timer was
  1050. * non-repeating before, it will have been stopped. If it was repeating, then
  1051. * the old repeat value will have been used to schedule the next timeout.
  1052. */
  1053. UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat);
  1054. UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle);
  1055. /*
  1056. * uv_getaddrinfo_t is a subclass of uv_req_t
  1057. *
  1058. * Request object for uv_getaddrinfo.
  1059. */
  1060. struct uv_getaddrinfo_s {
  1061. UV_REQ_FIELDS
  1062. /* read-only */
  1063. uv_loop_t* loop;
  1064. UV_GETADDRINFO_PRIVATE_FIELDS
  1065. };
  1066. /*
  1067. * Asynchronous getaddrinfo(3).
  1068. *
  1069. * Either node or service may be NULL but not both.
  1070. *
  1071. * hints is a pointer to a struct addrinfo with additional address type
  1072. * constraints, or NULL. Consult `man -s 3 getaddrinfo` for details.
  1073. *
  1074. * Returns 0 on success, -1 on error. Call uv_last_error() to get the error.
  1075. *
  1076. * If successful, your callback gets called sometime in the future with the
  1077. * lookup result, which is either:
  1078. *
  1079. * a) status == 0, the res argument points to a valid struct addrinfo, or
  1080. * b) status == -1, the res argument is NULL.
  1081. *
  1082. * On NXDOMAIN, the status code is -1 and uv_last_error() returns UV_ENOENT.
  1083. *
  1084. * Call uv_freeaddrinfo() to free the addrinfo structure.
  1085. */
  1086. UV_EXTERN int uv_getaddrinfo(uv_loop_t* loop,
  1087. uv_getaddrinfo_t* req,
  1088. uv_getaddrinfo_cb getaddrinfo_cb,
  1089. const char* node,
  1090. const char* service,
  1091. const struct addrinfo* hints);
  1092. /*
  1093. * Free the struct addrinfo. Passing NULL is allowed and is a no-op.
  1094. */
  1095. UV_EXTERN void uv_freeaddrinfo(struct addrinfo* ai);
  1096. /* uv_spawn() options */
  1097. typedef enum {
  1098. UV_IGNORE = 0x00,
  1099. UV_CREATE_PIPE = 0x01,
  1100. UV_INHERIT_FD = 0x02,
  1101. UV_INHERIT_STREAM = 0x04,
  1102. /* When UV_CREATE_PIPE is specified, UV_READABLE_PIPE and UV_WRITABLE_PIPE
  1103. * determine the direction of flow, from the child process' perspective. Both
  1104. * flags may be specified to create a duplex data stream.
  1105. */
  1106. UV_READABLE_PIPE = 0x10,
  1107. UV_WRITABLE_PIPE = 0x20
  1108. } uv_stdio_flags;
  1109. typedef struct uv_stdio_container_s {
  1110. uv_stdio_flags flags;
  1111. union {
  1112. uv_stream_t* stream;
  1113. int fd;
  1114. } data;
  1115. } uv_stdio_container_t;
  1116. typedef struct uv_process_options_s {
  1117. uv_exit_cb exit_cb; /* Called after the process exits. */
  1118. const char* file; /* Path to program to execute. */
  1119. /*
  1120. * Command line arguments. args[0] should be the path to the program. On
  1121. * Windows this uses CreateProcess which concatenates the arguments into a
  1122. * string this can cause some strange errors. See the note at
  1123. * windows_verbatim_arguments.
  1124. */
  1125. char** args;
  1126. /*
  1127. * This will be set as the environ variable in the subprocess. If this is
  1128. * NULL then the parents environ will be used.
  1129. */
  1130. char** env;
  1131. /*
  1132. * If non-null this represents a directory the subprocess should execute
  1133. * in. Stands for current working directory.
  1134. */
  1135. char* cwd;
  1136. /*
  1137. * Various flags that control how uv_spawn() behaves. See the definition of
  1138. * `enum uv_process_flags` below.
  1139. */
  1140. unsigned int flags;
  1141. /*
  1142. * The `stdio` field points to an array of uv_stdio_container_t structs that
  1143. * describe the file descriptors that will be made available to the child
  1144. * process. The convention is that stdio[0] points to stdin, fd 1 is used for
  1145. * stdout, and fd 2 is stderr.
  1146. *
  1147. * Note that on windows file descriptors greater than 2 are available to the
  1148. * child process only if the child processes uses the MSVCRT runtime.
  1149. */
  1150. int stdio_count;
  1151. uv_stdio_container_t* stdio;
  1152. /*
  1153. * Libuv can change the child process' user/group id. This happens only when
  1154. * the appropriate bits are set in the flags fields. This is not supported on
  1155. * windows; uv_spawn() will fail and set the error to UV_ENOTSUP.
  1156. */
  1157. uv_uid_t uid;
  1158. uv_gid_t gid;
  1159. } uv_process_options_t;
  1160. /*
  1161. * These are the flags that can be used for the uv_process_options.flags field.
  1162. */
  1163. enum uv_process_flags {
  1164. /*
  1165. * Set the child process' user id. The user id is supplied in the `uid` field
  1166. * of the options struct. This does not work on windows; setting this flag
  1167. * will cause uv_spawn() to fail.
  1168. */
  1169. UV_PROCESS_SETUID = (1 << 0),
  1170. /*
  1171. * Set the child process' group id. The user id is supplied in the `gid`
  1172. * field of the options struct. This does not work on windows; setting this
  1173. * flag will cause uv_spawn() to fail.
  1174. */
  1175. UV_PROCESS_SETGID = (1 << 1),
  1176. /*
  1177. * Do not wrap any arguments in quotes, or perform any other escaping, when
  1178. * converting the argument list into a command line string. This option is
  1179. * only meaningful on Windows systems. On unix it is silently ignored.
  1180. */
  1181. UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS = (1 << 2),
  1182. /*
  1183. * Spawn the child process in a detached state - this will make it a process
  1184. * group leader, and will effectively enable the child to keep running after
  1185. * the parent exits. Note that the child process will still keep the
  1186. * parent's event loop alive unless the parent process calls uv_unref() on
  1187. * the child's process handle.
  1188. */
  1189. UV_PROCESS_DETACHED = (1 << 3),
  1190. /*
  1191. * Hide the subprocess console window that would normally be created. This
  1192. * option is only meaningful on Windows systems. On unix it is silently
  1193. * ignored.
  1194. */
  1195. UV_PROCESS_WINDOWS_HIDE = (1 << 4)
  1196. };
  1197. /*
  1198. * uv_process_t is a subclass of uv_handle_t
  1199. */
  1200. struct uv_process_s {
  1201. UV_HANDLE_FIELDS
  1202. uv_exit_cb exit_cb;
  1203. int pid;
  1204. UV_PROCESS_PRIVATE_FIELDS
  1205. };
  1206. /* Initializes uv_process_t and starts the process. */
  1207. UV_EXTERN int uv_spawn(uv_loop_t*, uv_process_t*,
  1208. uv_process_options_t options);
  1209. /*
  1210. * Kills the process with the specified signal. The user must still
  1211. * call uv_close on the process.
  1212. */
  1213. UV_EXTERN int uv_process_kill(uv_process_t*, int signum);
  1214. /* Kills the process with the specified signal. */
  1215. UV_EXTERN uv_err_t uv_kill(int pid, int signum);
  1216. /*
  1217. * uv_work_t is a subclass of uv_req_t
  1218. */
  1219. struct uv_work_s {
  1220. UV_REQ_FIELDS
  1221. uv_loop_t* loop;
  1222. uv_work_cb work_cb;
  1223. uv_after_work_cb after_work_cb;
  1224. UV_WORK_PRIVATE_FIELDS
  1225. };
  1226. /* Queues a work request to execute asynchronously on the thread pool. */
  1227. UV_EXTERN int uv_queue_work(uv_loop_t* loop, uv_work_t* req,
  1228. uv_work_cb work_cb, uv_after_work_cb after_work_cb);
  1229. /* Cancel a pending request. Fails if the request is executing or has finished
  1230. * executing.
  1231. *
  1232. * Returns 0 on success, -1 on error. The loop error code is not touched.
  1233. *
  1234. * Only cancellation of uv_fs_t, uv_getaddrinfo_t and uv_work_t requests is
  1235. * currently supported.
  1236. *
  1237. * Cancelled requests have their callbacks invoked some time in the future.
  1238. * It's _not_ safe to free the memory associated with the request until your
  1239. * callback is called.
  1240. *
  1241. * Here is how cancellation is reported to your callback:
  1242. *
  1243. * - A uv_fs_t request has its req->errorno field set to UV_ECANCELED.
  1244. *
  1245. * - A uv_work_t or uv_getaddrinfo_t request has its callback invoked with
  1246. * status == -1 and uv_last_error(loop).code == UV_ECANCELED.
  1247. *
  1248. * This function is currently only implemented on UNIX platforms. On Windows,
  1249. * it always returns -1.
  1250. */
  1251. UV_EXTERN int uv_cancel(uv_req_t* req);
  1252. struct uv_cpu_info_s {
  1253. char* model;
  1254. int speed;
  1255. struct uv_cpu_times_s {
  1256. uint64_t user;
  1257. uint64_t nice;
  1258. uint64_t sys;
  1259. uint64_t idle;
  1260. uint64_t irq;
  1261. } cpu_times;
  1262. };
  1263. struct uv_interface_address_s {
  1264. char* name;
  1265. int is_internal;
  1266. union {
  1267. struct sockaddr_in address4;
  1268. struct sockaddr_in6 address6;
  1269. } address;
  1270. };
  1271. UV_EXTERN char** uv_setup_args(int argc, char** argv);
  1272. UV_EXTERN uv_err_t uv_get_process_title(char* buffer, size_t size);
  1273. UV_EXTERN uv_err_t uv_set_process_title(const char* title);
  1274. UV_EXTERN uv_err_t uv_resident_set_memory(size_t* rss);
  1275. UV_EXTERN uv_err_t uv_uptime(double* uptime);
  1276. /*
  1277. * This allocates cpu_infos array, and sets count. The array
  1278. * is freed using uv_free_cpu_info().
  1279. */
  1280. UV_EXTERN uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count);
  1281. UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count);
  1282. /*
  1283. * This allocates addresses array, and sets count. The array
  1284. * is freed using uv_free_interface_addresses().
  1285. */
  1286. UV_EXTERN uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
  1287. int* count);
  1288. UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses,
  1289. int count);
  1290. /*
  1291. * File System Methods.
  1292. *
  1293. * The uv_fs_* functions execute a blocking system call asynchronously (in a
  1294. * thread pool) and call the specified callback in the specified loop after
  1295. * completion. If the user gives NULL as the callback the blocking system
  1296. * call will be called synchronously. req should be a pointer to an
  1297. * uninitialized uv_fs_t object.
  1298. *
  1299. * uv_fs_req_cleanup() must be called after completion of the uv_fs_
  1300. * function to free any internal memory allocations associated with the
  1301. * request.
  1302. */
  1303. typedef enum {
  1304. UV_FS_UNKNOWN = -1,
  1305. UV_FS_CUSTOM,
  1306. UV_FS_OPEN,
  1307. UV_FS_CLOSE,
  1308. UV_FS_READ,
  1309. UV_FS_WRITE,
  1310. UV_FS_SENDFILE,
  1311. UV_FS_STAT,
  1312. UV_FS_LSTAT,
  1313. UV_FS_FSTAT,
  1314. UV_FS_FTRUNCATE,
  1315. UV_FS_UTIME,
  1316. UV_FS_FUTIME,
  1317. UV_FS_CHMOD,
  1318. UV_FS_FCHMOD,
  1319. UV_FS_FSYNC,
  1320. UV_FS_FDATASYNC,
  1321. UV_FS_UNLINK,
  1322. UV_FS_RMDIR,
  1323. UV_FS_MKDIR,
  1324. UV_FS_RENAME,
  1325. UV_FS_READDIR,
  1326. UV_FS_LINK,
  1327. UV_FS_SYMLINK,
  1328. UV_FS_READLINK,
  1329. UV_FS_CHOWN,
  1330. UV_FS_FCHOWN
  1331. } uv_fs_type;
  1332. /* uv_fs_t is a subclass of uv_req_t */
  1333. struct uv_fs_s {
  1334. UV_REQ_FIELDS
  1335. uv_fs_type fs_type;
  1336. uv_loop_t* loop;
  1337. uv_fs_cb cb;
  1338. ssize_t result;
  1339. void* ptr;
  1340. const char* path;
  1341. uv_err_code errorno;
  1342. uv_statbuf_t statbuf; /* Stores the result of uv_fs_stat and uv_fs_fstat. */
  1343. UV_FS_PRIVATE_FIELDS
  1344. };
  1345. UV_EXTERN void uv_fs_req_cleanup(uv_fs_t* req);
  1346. UV_EXTERN int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1347. uv_fs_cb cb);
  1348. UV_EXTERN int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1349. int flags, int mode, uv_fs_cb cb);
  1350. UV_EXTERN int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1351. void* buf, size_t length, int64_t offset, uv_fs_cb cb);
  1352. UV_EXTERN int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1353. uv_fs_cb cb);
  1354. UV_EXTERN int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1355. void* buf, size_t length, int64_t offset, uv_fs_cb cb);
  1356. UV_EXTERN int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1357. int mode, uv_fs_cb cb);
  1358. UV_EXTERN int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1359. uv_fs_cb cb);
  1360. UV_EXTERN int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req,
  1361. const char* path, int flags, uv_fs_cb cb);
  1362. UV_EXTERN int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1363. uv_fs_cb cb);
  1364. UV_EXTERN int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1365. uv_fs_cb cb);
  1366. UV_EXTERN int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1367. const char* new_path, uv_fs_cb cb);
  1368. UV_EXTERN int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1369. uv_fs_cb cb);
  1370. UV_EXTERN int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1371. uv_fs_cb cb);
  1372. UV_EXTERN int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1373. int64_t offset, uv_fs_cb cb);
  1374. UV_EXTERN int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd,
  1375. uv_file in_fd, int64_t in_offset, size_t length, uv_fs_cb cb);
  1376. UV_EXTERN int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1377. int mode, uv_fs_cb cb);
  1378. UV_EXTERN int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1379. double atime, double mtime, uv_fs_cb cb);
  1380. UV_EXTERN int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1381. double atime, double mtime, uv_fs_cb cb);
  1382. UV_EXTERN int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1383. uv_fs_cb cb);
  1384. UV_EXTERN int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1385. const char* new_path, uv_fs_cb cb);
  1386. /*
  1387. * This flag can be used with uv_fs_symlink on Windows
  1388. * to specify whether path argument points to a directory.
  1389. */
  1390. #define UV_FS_SYMLINK_DIR 0x0001
  1391. /*
  1392. * This flag can be used with uv_fs_symlink on Windows
  1393. * to specify whether the symlink is to be created using junction points.
  1394. */
  1395. #define UV_FS_SYMLINK_JUNCTION 0x0002
  1396. UV_EXTERN int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1397. const char* new_path, int flags, uv_fs_cb cb);
  1398. UV_EXTERN int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1399. uv_fs_cb cb);
  1400. UV_EXTERN int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1401. int mode, uv_fs_cb cb);
  1402. UV_EXTERN int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path,
  1403. int uid, int gid, uv_fs_cb cb);
  1404. UV_EXTERN int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file,
  1405. int uid, int gid, uv_fs_cb cb);
  1406. enum uv_fs_event {
  1407. UV_RENAME = 1,
  1408. UV_CHANGE = 2
  1409. };
  1410. struct uv_fs_event_s {
  1411. UV_HANDLE_FIELDS
  1412. char* filename;
  1413. UV_FS_EVENT_PRIVATE_FIELDS
  1414. };
  1415. /*
  1416. * uv_fs_stat() based polling file watcher.
  1417. */
  1418. struct uv_fs_poll_s {
  1419. UV_HANDLE_FIELDS
  1420. /* Private, don't touch. */
  1421. void* poll_ctx;
  1422. };
  1423. UV_EXTERN int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle);
  1424. /*
  1425. * Check the file at `path` for changes every `interval` milliseconds.
  1426. *
  1427. * Your callback i invoked with `status == -1` if `path` does not exist
  1428. * or is inaccessible. The watcher is *not* stopped but your callback is
  1429. * not called again until something changes (e.g. when the file is created
  1430. * or the error reason changes).
  1431. *
  1432. * When `status == 0`, your callback receives pointers to the old and new
  1433. * `uv_statbuf_t` structs. They are valid for the duration of the callback
  1434. * only!
  1435. *
  1436. * For maximum portability, use multi-second intervals. Sub-second intervals
  1437. * will not detect all changes on many file systems.
  1438. */
  1439. UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t* handle,
  1440. uv_fs_poll_cb poll_cb,
  1441. const char* path,
  1442. unsigned int interval);
  1443. UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle);
  1444. /*
  1445. * UNIX signal handling on a per-event loop basis. The implementation is not
  1446. * ultra efficient so don't go creating a million event loops with a million
  1447. * signal watchers.
  1448. *
  1449. * Note to Linux users: SIGRT0 and SIGRT1 (signals 32 and 33) are used by the
  1450. * NPTL pthreads library to manage threads. Installing watchers for those
  1451. * signals will lead to unpredictable behavior and is strongly discouraged.
  1452. * Future versions of libuv may simply reject them.
  1453. *
  1454. * Some signal support is available on Windows:
  1455. *
  1456. * SIGINT is normally delivered when the user presses CTRL+C. However, like
  1457. * on Unix, it is not generated when terminal raw mode is enabled.
  1458. *
  1459. * SIGBREAK is delivered when the user pressed CTRL+BREAK.
  1460. *
  1461. * SIGHUP is generated when the user closes the console window. On SIGHUP the
  1462. * program is given approximately 10 seconds to perform cleanup. After that
  1463. * Windows will unconditionally terminate it.
  1464. *
  1465. * SIGWINCH is raised whenever libuv detects that the console has been
  1466. * resized. SIGWINCH is emulated by libuv when the program uses an uv_tty_t
  1467. * handle to write to the console. SIGWINCH may not always be delivered in a
  1468. * timely manner; libuv will only detect size changes when the cursor is
  1469. * being moved. When a readable uv_tty_handle is used in raw mode, resizing
  1470. * the console buffer will also trigger a SIGWINCH signal.
  1471. *
  1472. * Watchers for other signals can be successfully created, but these signals
  1473. * are never generated. These signals are: SIGILL, SIGABRT, SIGFPE, SIGSEGV,
  1474. * SIGTERM and SIGKILL.
  1475. *
  1476. * Note that calls to raise() or abort() to programmatically raise a signal are
  1477. * not detected by libuv; these will not trigger a signal watcher.
  1478. */
  1479. struct uv_signal_s {
  1480. UV_HANDLE_FIELDS
  1481. uv_signal_cb signal_cb;
  1482. int signum;
  1483. UV_SIGNAL_PRIVATE_FIELDS
  1484. };
  1485. UV_EXTERN int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle);
  1486. UV_EXTERN int uv_signal_start(uv_signal_t* handle,
  1487. uv_signal_cb signal_cb,
  1488. int signum);
  1489. UV_EXTERN int uv_signal_stop(uv_signal_t* handle);
  1490. /*
  1491. * Gets load avg
  1492. * See: http://en.wikipedia.org/wiki/Load_(computing)
  1493. * (Returns [0,0,0] for windows and cygwin)
  1494. */
  1495. UV_EXTERN void uv_loadavg(double avg[3]);
  1496. /*
  1497. * Flags to be passed to uv_fs_event_init.
  1498. */
  1499. enum uv_fs_event_flags {
  1500. /*
  1501. * By default, if the fs event watcher is given a directory name, we will
  1502. * watch for all events in that directory. This flags overrides this behavior
  1503. * and makes fs_event report only changes to the directory entry itself. This
  1504. * flag does not affect individual files watched.
  1505. * This flag is currently not implemented yet on any backend.
  1506. */
  1507. UV_FS_EVENT_WATCH_ENTRY = 1,
  1508. /*
  1509. * By default uv_fs_event will try to use a kernel interface such as inotify
  1510. * or kqueue to detect events. This may not work on remote filesystems such
  1511. * as NFS mounts. This flag makes fs_event fall back to calling stat() on a
  1512. * regular interval.
  1513. * This flag is currently not implemented yet on any backend.
  1514. */
  1515. UV_FS_EVENT_STAT = 2,
  1516. /*
  1517. * By default, event watcher, when watching directory, is not registering
  1518. * (is ignoring) changes in it's subdirectories.
  1519. * This flag will override this behaviour on platforms that support it.
  1520. */
  1521. UV_FS_EVENT_RECURSIVE = 3
  1522. };
  1523. UV_EXTERN int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle,
  1524. const char* filename, uv_fs_event_cb cb, int flags);
  1525. /* Utility */
  1526. /* Convert string ip addresses to binary structures */
  1527. UV_EXTERN struct sockaddr_in uv_ip4_addr(const char* ip, int port);
  1528. UV_EXTERN struct sockaddr_in6 uv_ip6_addr(const char* ip, int port);
  1529. /* Convert binary addresses to strings */
  1530. UV_EXTERN int uv_ip4_name(struct sockaddr_in* src, char* dst, size_t size);
  1531. UV_EXTERN int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size);
  1532. /* Cross-platform IPv6-capable implementation of the 'standard' inet_ntop */
  1533. /* and inet_pton functions. On success they return UV_OK. If an error */
  1534. /* the target of the `dst` pointer is unmodified. */
  1535. UV_EXTERN uv_err_t uv_inet_ntop(int af, const void* src, char* dst, size_t size);
  1536. UV_EXTERN uv_err_t uv_inet_pton(int af, const char* src, void* dst);
  1537. /* Gets the executable path */
  1538. UV_EXTERN int uv_exepath(char* buffer, size_t* size);
  1539. /* Gets the current working directory */
  1540. UV_EXTERN uv_err_t uv_cwd(char* buffer, size_t size);
  1541. /* Changes the current working directory */
  1542. UV_EXTERN uv_err_t uv_chdir(const char* dir);
  1543. /* Gets memory info in bytes */
  1544. UV_EXTERN uint64_t uv_get_free_memory(void);
  1545. UV_EXTERN uint64_t uv_get_total_memory(void);
  1546. /*
  1547. * Returns the current high-resolution real time. This is expressed in
  1548. * nanoseconds. It is relative to an arbitrary time in the past. It is not
  1549. * related to the time of day and therefore not subject to clock drift. The
  1550. * primary use is for measuring performance between intervals.
  1551. *
  1552. * Note not every platform can support nanosecond resolution; however, this
  1553. * value will always be in nanoseconds.
  1554. */
  1555. UV_EXTERN extern uint64_t uv_hrtime(void);
  1556. /*
  1557. * Disables inheritance for file descriptors / handles that this process
  1558. * inherited from its parent. The effect is that child processes spawned by
  1559. * this process don't accidentally inherit these handles.
  1560. *
  1561. * It is recommended to call this function as early in your program as possible,
  1562. * before the inherited file descriptors can be closed or duplicated.
  1563. *
  1564. * Note that this function works on a best-effort basis: there is no guarantee
  1565. * that libuv can discover all file descriptors that were inherited. In general
  1566. * it does a better job on Windows than it does on unix.
  1567. *
  1568. * TODO(bb): insert snarky remark to annoy bnoordhuis and the folks at joyent.
  1569. */
  1570. UV_EXTERN void uv_disable_stdio_inheritance(void);
  1571. /*
  1572. * Opens a shared library. The filename is in utf-8. Returns 0 on success and
  1573. * -1 on error. Call `uv_dlerror(uv_lib_t*)` to get the error message.
  1574. */
  1575. UV_EXTERN int uv_dlopen(const char* filename, uv_lib_t* lib);
  1576. /*
  1577. * Close the shared library.
  1578. */
  1579. UV_EXTERN void uv_dlclose(uv_lib_t* lib);
  1580. /*
  1581. * Retrieves a data pointer from a dynamic library. It is legal for a symbol to
  1582. * map to NULL. Returns 0 on success and -1 if the symbol was not found.
  1583. */
  1584. UV_EXTERN int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr);
  1585. /*
  1586. * Returns the last uv_dlopen() or uv_dlsym() error message.
  1587. */
  1588. UV_EXTERN const char* uv_dlerror(uv_lib_t* lib);
  1589. /*
  1590. * The mutex functions return 0 on success, -1 on error
  1591. * (unless the return type is void, of course).
  1592. */
  1593. UV_EXTERN int uv_mutex_init(uv_mutex_t* handle);
  1594. UV_EXTERN void uv_mutex_destroy(uv_mutex_t* handle);
  1595. UV_EXTERN void uv_mutex_lock(uv_mutex_t* handle);
  1596. UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle);
  1597. UV_EXTERN void uv_mutex_unlock(uv_mutex_t* handle);
  1598. /*
  1599. * Same goes for the read/write lock functions.
  1600. */
  1601. UV_EXTERN int uv_rwlock_init(uv_rwlock_t* rwlock);
  1602. UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock);
  1603. UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock);
  1604. UV_EXTERN int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock);
  1605. UV_EXTERN void uv_rwlock_rdunlock(uv_rwlock_t* rwlock);
  1606. UV_EXTERN void uv_rwlock_wrlock(uv_rwlock_t* rwlock);
  1607. UV_EXTERN int uv_rwlock_trywrlock(uv_rwlock_t* rwlock);
  1608. UV_EXTERN void uv_rwlock_wrunlock(uv_rwlock_t* rwlock);
  1609. /*
  1610. * Same goes for the semaphore functions.
  1611. */
  1612. UV_EXTERN int uv_sem_init(uv_sem_t* sem, unsigned int value);
  1613. UV_EXTERN void uv_sem_destroy(uv_sem_t* sem);
  1614. UV_EXTERN void uv_sem_post(uv_sem_t* sem);
  1615. UV_EXTERN void uv_sem_wait(uv_sem_t* sem);
  1616. UV_EXTERN int uv_sem_trywait(uv_sem_t* sem);
  1617. /*
  1618. * Same goes for the condition variable functions.
  1619. */
  1620. UV_EXTERN int uv_cond_init(uv_cond_t* cond);
  1621. UV_EXTERN void uv_cond_destroy(uv_cond_t* cond);
  1622. UV_EXTERN void uv_cond_signal(uv_cond_t* cond);
  1623. UV_EXTERN void uv_cond_broadcast(uv_cond_t* cond);
  1624. /* Waits on a condition variable without a timeout.
  1625. *
  1626. * Note:
  1627. * 1. callers should be prepared to deal with spurious wakeups.
  1628. */
  1629. UV_EXTERN void uv_cond_wait(uv_cond_t* cond, uv_mutex_t* mutex);
  1630. /* Waits on a condition variable with a timeout in nano seconds.
  1631. * Returns 0 for success or -1 on timeout, * aborts when other errors happen.
  1632. *
  1633. * Note:
  1634. * 1. callers should be prepared to deal with spurious wakeups.
  1635. * 2. the granularity of timeout on Windows is never less than one millisecond.
  1636. * 3. uv_cond_timedwait takes a relative timeout, not an absolute time.
  1637. */
  1638. UV_EXTERN int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex,
  1639. uint64_t timeout);
  1640. UV_EXTERN int uv_barrier_init(uv_barrier_t* barrier, unsigned int count);
  1641. UV_EXTERN void uv_barrier_destroy(uv_barrier_t* barrier);
  1642. UV_EXTERN void uv_barrier_wait(uv_barrier_t* barrier);
  1643. /* Runs a function once and only once. Concurrent calls to uv_once() with the
  1644. * same guard will block all callers except one (it's unspecified which one).
  1645. * The guard should be initialized statically with the UV_ONCE_INIT macro.
  1646. */
  1647. UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void));
  1648. UV_EXTERN int uv_thread_create(uv_thread_t *tid,
  1649. void (*entry)(void *arg), void *arg);
  1650. UV_EXTERN unsigned long uv_thread_self(void);
  1651. UV_EXTERN int uv_thread_join(uv_thread_t *tid);
  1652. /* the presence of these unions force similar struct layout */
  1653. union uv_any_handle {
  1654. uv_handle_t handle;
  1655. uv_stream_t stream;
  1656. uv_tcp_t tcp;
  1657. uv_pipe_t pipe;
  1658. uv_prepare_t prepare;
  1659. uv_check_t check;
  1660. uv_idle_t idle;
  1661. uv_async_t async;
  1662. uv_timer_t timer;
  1663. uv_fs_event_t fs_event;
  1664. uv_fs_poll_t fs_poll;
  1665. uv_poll_t poll;
  1666. uv_process_t process;
  1667. uv_tty_t tty;
  1668. uv_udp_t udp;
  1669. };
  1670. union uv_any_req {
  1671. uv_req_t req;
  1672. uv_write_t write;
  1673. uv_connect_t connect;
  1674. uv_shutdown_t shutdown;
  1675. uv_fs_t fs_req;
  1676. uv_work_t work_req;
  1677. uv_udp_send_t udp_send_req;
  1678. uv_getaddrinfo_t getaddrinfo_req;
  1679. };
  1680. struct uv_loop_s {
  1681. /* User data - use this for whatever. */
  1682. void* data;
  1683. /* The last error */
  1684. uv_err_t last_err;
  1685. /* Loop reference counting */
  1686. unsigned int active_handles;
  1687. ngx_queue_t handle_queue;
  1688. ngx_queue_t active_reqs;
  1689. /* Internal flag to signal loop stop */
  1690. unsigned int stop_flag;
  1691. UV_LOOP_PRIVATE_FIELDS
  1692. };
  1693. /* Don't export the private CPP symbols. */
  1694. #undef UV_HANDLE_TYPE_PRIVATE
  1695. #undef UV_REQ_TYPE_PRIVATE
  1696. #undef UV_REQ_PRIVATE_FIELDS
  1697. #undef UV_STREAM_PRIVATE_FIELDS
  1698. #undef UV_TCP_PRIVATE_FIELDS
  1699. #undef UV_PREPARE_PRIVATE_FIELDS
  1700. #undef UV_CHECK_PRIVATE_FIELDS
  1701. #undef UV_IDLE_PRIVATE_FIELDS
  1702. #undef UV_ASYNC_PRIVATE_FIELDS
  1703. #undef UV_TIMER_PRIVATE_FIELDS
  1704. #undef UV_GETADDRINFO_PRIVATE_FIELDS
  1705. #undef UV_FS_REQ_PRIVATE_FIELDS
  1706. #undef UV_WORK_PRIVATE_FIELDS
  1707. #undef UV_FS_EVENT_PRIVATE_FIELDS
  1708. #undef UV_SIGNAL_PRIVATE_FIELDS
  1709. #undef UV_LOOP_PRIVATE_FIELDS
  1710. #undef UV_LOOP_PRIVATE_PLATFORM_FIELDS
  1711. #ifdef __cplusplus
  1712. }
  1713. #endif
  1714. #endif /* UV_H */