1
0

uv.h 74 KB

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