uv.h 73 KB

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