/src/unix/stream.c

http://github.com/joyent/libuv · C · 1056 lines · 692 code · 227 blank · 137 comment · 202 complexity · cc3dfe3aec8148b7bc24f2aba4665f7c MD5 · raw file

  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "internal.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <sys/uio.h>
  31. #include <sys/un.h>
  32. #include <unistd.h>
  33. static void uv__stream_connect(uv_stream_t*);
  34. static void uv__write(uv_stream_t* stream);
  35. static void uv__read(uv_stream_t* stream);
  36. static void uv__stream_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  37. /* Used by the accept() EMFILE party trick. */
  38. static int uv__open_cloexec(const char* path, int flags) {
  39. int fd;
  40. #if defined(__linux__)
  41. fd = open(path, flags | UV__O_CLOEXEC);
  42. if (fd != -1)
  43. return fd;
  44. if (errno != EINVAL)
  45. return -1;
  46. /* O_CLOEXEC not supported. */
  47. #endif
  48. fd = open(path, flags);
  49. if (fd != -1)
  50. uv__cloexec(fd, 1);
  51. return fd;
  52. }
  53. static size_t uv__buf_count(uv_buf_t bufs[], int bufcnt) {
  54. size_t total = 0;
  55. int i;
  56. for (i = 0; i < bufcnt; i++) {
  57. total += bufs[i].len;
  58. }
  59. return total;
  60. }
  61. void uv__stream_init(uv_loop_t* loop,
  62. uv_stream_t* stream,
  63. uv_handle_type type) {
  64. uv__handle_init(loop, (uv_handle_t*)stream, type);
  65. stream->read_cb = NULL;
  66. stream->read2_cb = NULL;
  67. stream->alloc_cb = NULL;
  68. stream->close_cb = NULL;
  69. stream->connection_cb = NULL;
  70. stream->connect_req = NULL;
  71. stream->shutdown_req = NULL;
  72. stream->accepted_fd = -1;
  73. stream->delayed_error = 0;
  74. ngx_queue_init(&stream->write_queue);
  75. ngx_queue_init(&stream->write_completed_queue);
  76. stream->write_queue_size = 0;
  77. if (loop->emfile_fd == -1)
  78. loop->emfile_fd = uv__open_cloexec("/", O_RDONLY);
  79. uv__io_init(&stream->io_watcher, uv__stream_io, -1);
  80. }
  81. int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
  82. socklen_t yes;
  83. assert(fd >= 0);
  84. stream->flags |= flags;
  85. if (stream->type == UV_TCP) {
  86. /* Reuse the port address if applicable. */
  87. yes = 1;
  88. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1)
  89. return uv__set_sys_error(stream->loop, errno);
  90. if ((stream->flags & UV_TCP_NODELAY) && uv__tcp_nodelay(fd, 1))
  91. return uv__set_sys_error(stream->loop, errno);
  92. /* TODO Use delay the user passed in. */
  93. if ((stream->flags & UV_TCP_KEEPALIVE) && uv__tcp_keepalive(fd, 1, 60))
  94. return uv__set_sys_error(stream->loop, errno);
  95. }
  96. stream->io_watcher.fd = fd;
  97. return 0;
  98. }
  99. void uv__stream_destroy(uv_stream_t* stream) {
  100. uv_write_t* req;
  101. ngx_queue_t* q;
  102. assert(!uv__io_active(&stream->io_watcher, UV__POLLIN | UV__POLLOUT));
  103. assert(stream->flags & UV_CLOSED);
  104. if (stream->connect_req) {
  105. uv__req_unregister(stream->loop, stream->connect_req);
  106. uv__set_artificial_error(stream->loop, UV_ECANCELED);
  107. stream->connect_req->cb(stream->connect_req, -1);
  108. stream->connect_req = NULL;
  109. }
  110. while (!ngx_queue_empty(&stream->write_queue)) {
  111. q = ngx_queue_head(&stream->write_queue);
  112. ngx_queue_remove(q);
  113. req = ngx_queue_data(q, uv_write_t, queue);
  114. uv__req_unregister(stream->loop, req);
  115. if (req->bufs != req->bufsml)
  116. free(req->bufs);
  117. if (req->cb) {
  118. uv__set_artificial_error(req->handle->loop, UV_ECANCELED);
  119. req->cb(req, -1);
  120. }
  121. }
  122. while (!ngx_queue_empty(&stream->write_completed_queue)) {
  123. q = ngx_queue_head(&stream->write_completed_queue);
  124. ngx_queue_remove(q);
  125. req = ngx_queue_data(q, uv_write_t, queue);
  126. uv__req_unregister(stream->loop, req);
  127. if (req->cb) {
  128. uv__set_sys_error(stream->loop, req->error);
  129. req->cb(req, req->error ? -1 : 0);
  130. }
  131. }
  132. if (stream->shutdown_req) {
  133. uv__req_unregister(stream->loop, stream->shutdown_req);
  134. uv__set_artificial_error(stream->loop, UV_ECANCELED);
  135. stream->shutdown_req->cb(stream->shutdown_req, -1);
  136. stream->shutdown_req = NULL;
  137. }
  138. }
  139. /* Implements a best effort approach to mitigating accept() EMFILE errors.
  140. * We have a spare file descriptor stashed away that we close to get below
  141. * the EMFILE limit. Next, we accept all pending connections and close them
  142. * immediately to signal the clients that we're overloaded - and we are, but
  143. * we still keep on trucking.
  144. *
  145. * There is one caveat: it's not reliable in a multi-threaded environment.
  146. * The file descriptor limit is per process. Our party trick fails if another
  147. * thread opens a file or creates a socket in the time window between us
  148. * calling close() and accept().
  149. */
  150. static int uv__emfile_trick(uv_loop_t* loop, int accept_fd) {
  151. int fd;
  152. int r;
  153. if (loop->emfile_fd == -1)
  154. return -1;
  155. close(loop->emfile_fd);
  156. for (;;) {
  157. fd = uv__accept(accept_fd);
  158. if (fd != -1) {
  159. close(fd);
  160. continue;
  161. }
  162. if (errno == EINTR)
  163. continue;
  164. if (errno == EAGAIN || errno == EWOULDBLOCK)
  165. r = 0;
  166. else
  167. r = -1;
  168. loop->emfile_fd = uv__open_cloexec("/", O_RDONLY);
  169. return r;
  170. }
  171. }
  172. void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
  173. static int use_emfile_trick = -1;
  174. uv_stream_t* stream;
  175. int fd;
  176. int r;
  177. stream = container_of(w, uv_stream_t, io_watcher);
  178. assert(events == UV__POLLIN);
  179. assert(stream->accepted_fd == -1);
  180. assert(!(stream->flags & UV_CLOSING));
  181. if (stream->accepted_fd == -1)
  182. uv__io_start(stream->loop, &stream->io_watcher, UV__POLLIN);
  183. /* connection_cb can close the server socket while we're
  184. * in the loop so check it on each iteration.
  185. */
  186. while (stream->io_watcher.fd != -1) {
  187. assert(stream->accepted_fd == -1);
  188. fd = uv__accept(stream->io_watcher.fd);
  189. if (fd == -1) {
  190. switch (errno) {
  191. #if EWOULDBLOCK != EAGAIN
  192. case EWOULDBLOCK:
  193. #endif
  194. case EAGAIN:
  195. return; /* Not an error. */
  196. case ECONNABORTED:
  197. continue; /* Ignore. */
  198. case EMFILE:
  199. case ENFILE:
  200. if (use_emfile_trick == -1) {
  201. const char* val = getenv("UV_ACCEPT_EMFILE_TRICK");
  202. use_emfile_trick = (val == NULL || atoi(val) != 0);
  203. }
  204. if (use_emfile_trick) {
  205. SAVE_ERRNO(r = uv__emfile_trick(loop, stream->io_watcher.fd));
  206. if (r == 0)
  207. continue;
  208. }
  209. /* Fall through. */
  210. default:
  211. uv__set_sys_error(loop, errno);
  212. stream->connection_cb(stream, -1);
  213. continue;
  214. }
  215. }
  216. stream->accepted_fd = fd;
  217. stream->connection_cb(stream, 0);
  218. if (stream->accepted_fd != -1) {
  219. /* The user hasn't yet accepted called uv_accept() */
  220. uv__io_stop(loop, &stream->io_watcher, UV__POLLIN);
  221. return;
  222. }
  223. if (stream->type == UV_TCP && (stream->flags & UV_TCP_SINGLE_ACCEPT)) {
  224. /* Give other processes a chance to accept connections. */
  225. struct timespec timeout = { 0, 1 };
  226. nanosleep(&timeout, NULL);
  227. }
  228. }
  229. }
  230. int uv_accept(uv_stream_t* server, uv_stream_t* client) {
  231. uv_stream_t* streamServer;
  232. uv_stream_t* streamClient;
  233. int saved_errno;
  234. int status;
  235. /* TODO document this */
  236. assert(server->loop == client->loop);
  237. saved_errno = errno;
  238. status = -1;
  239. streamServer = (uv_stream_t*)server;
  240. streamClient = (uv_stream_t*)client;
  241. if (streamServer->accepted_fd < 0) {
  242. uv__set_sys_error(server->loop, EAGAIN);
  243. goto out;
  244. }
  245. if (uv__stream_open(streamClient, streamServer->accepted_fd,
  246. UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
  247. /* TODO handle error */
  248. close(streamServer->accepted_fd);
  249. streamServer->accepted_fd = -1;
  250. goto out;
  251. }
  252. uv__io_start(streamServer->loop, &streamServer->io_watcher, UV__POLLIN);
  253. streamServer->accepted_fd = -1;
  254. status = 0;
  255. out:
  256. errno = saved_errno;
  257. return status;
  258. }
  259. int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) {
  260. int r;
  261. switch (stream->type) {
  262. case UV_TCP:
  263. r = uv_tcp_listen((uv_tcp_t*)stream, backlog, cb);
  264. break;
  265. case UV_NAMED_PIPE:
  266. r = uv_pipe_listen((uv_pipe_t*)stream, backlog, cb);
  267. break;
  268. default:
  269. assert(0);
  270. return -1;
  271. }
  272. if (r == 0)
  273. uv__handle_start(stream);
  274. return r;
  275. }
  276. uv_write_t* uv_write_queue_head(uv_stream_t* stream) {
  277. ngx_queue_t* q;
  278. uv_write_t* req;
  279. if (ngx_queue_empty(&stream->write_queue)) {
  280. return NULL;
  281. }
  282. q = ngx_queue_head(&stream->write_queue);
  283. if (!q) {
  284. return NULL;
  285. }
  286. req = ngx_queue_data(q, struct uv_write_s, queue);
  287. assert(req);
  288. return req;
  289. }
  290. static void uv__drain(uv_stream_t* stream) {
  291. uv_shutdown_t* req;
  292. assert(!uv_write_queue_head(stream));
  293. assert(stream->write_queue_size == 0);
  294. uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLOUT);
  295. /* Shutdown? */
  296. if ((stream->flags & UV_STREAM_SHUTTING) &&
  297. !(stream->flags & UV_CLOSING) &&
  298. !(stream->flags & UV_STREAM_SHUT)) {
  299. assert(stream->shutdown_req);
  300. req = stream->shutdown_req;
  301. stream->shutdown_req = NULL;
  302. uv__req_unregister(stream->loop, req);
  303. if (shutdown(stream->io_watcher.fd, SHUT_WR)) {
  304. /* Error. Report it. User should call uv_close(). */
  305. uv__set_sys_error(stream->loop, errno);
  306. if (req->cb) {
  307. req->cb(req, -1);
  308. }
  309. } else {
  310. uv__set_sys_error(stream->loop, 0);
  311. ((uv_handle_t*) stream)->flags |= UV_STREAM_SHUT;
  312. if (req->cb) {
  313. req->cb(req, 0);
  314. }
  315. }
  316. }
  317. }
  318. static size_t uv__write_req_size(uv_write_t* req) {
  319. size_t size;
  320. size = uv__buf_count(req->bufs + req->write_index,
  321. req->bufcnt - req->write_index);
  322. assert(req->handle->write_queue_size >= size);
  323. return size;
  324. }
  325. static void uv__write_req_finish(uv_write_t* req) {
  326. uv_stream_t* stream = req->handle;
  327. /* Pop the req off tcp->write_queue. */
  328. ngx_queue_remove(&req->queue);
  329. if (req->bufs != req->bufsml) {
  330. free(req->bufs);
  331. }
  332. req->bufs = NULL;
  333. /* Add it to the write_completed_queue where it will have its
  334. * callback called in the near future.
  335. */
  336. ngx_queue_insert_tail(&stream->write_completed_queue, &req->queue);
  337. uv__io_feed(stream->loop, &stream->io_watcher);
  338. }
  339. /* On success returns NULL. On error returns a pointer to the write request
  340. * which had the error.
  341. */
  342. static void uv__write(uv_stream_t* stream) {
  343. uv_write_t* req;
  344. struct iovec* iov;
  345. int iovcnt;
  346. ssize_t n;
  347. if (stream->flags & UV_CLOSING) {
  348. /* Handle was closed this tick. We've received a stale
  349. * 'is writable' callback from the event loop, ignore.
  350. */
  351. return;
  352. }
  353. start:
  354. assert(stream->io_watcher.fd >= 0);
  355. /* Get the request at the head of the queue. */
  356. req = uv_write_queue_head(stream);
  357. if (!req) {
  358. assert(stream->write_queue_size == 0);
  359. return;
  360. }
  361. assert(req->handle == stream);
  362. /*
  363. * Cast to iovec. We had to have our own uv_buf_t instead of iovec
  364. * because Windows's WSABUF is not an iovec.
  365. */
  366. assert(sizeof(uv_buf_t) == sizeof(struct iovec));
  367. iov = (struct iovec*) &(req->bufs[req->write_index]);
  368. iovcnt = req->bufcnt - req->write_index;
  369. /*
  370. * Now do the actual writev. Note that we've been updating the pointers
  371. * inside the iov each time we write. So there is no need to offset it.
  372. */
  373. if (req->send_handle) {
  374. struct msghdr msg;
  375. char scratch[64];
  376. struct cmsghdr *cmsg;
  377. int fd_to_send = req->send_handle->io_watcher.fd;
  378. assert(fd_to_send >= 0);
  379. msg.msg_name = NULL;
  380. msg.msg_namelen = 0;
  381. msg.msg_iov = iov;
  382. msg.msg_iovlen = iovcnt;
  383. msg.msg_flags = 0;
  384. msg.msg_control = (void*) scratch;
  385. msg.msg_controllen = CMSG_LEN(sizeof(fd_to_send));
  386. cmsg = CMSG_FIRSTHDR(&msg);
  387. cmsg->cmsg_level = SOL_SOCKET;
  388. cmsg->cmsg_type = SCM_RIGHTS;
  389. cmsg->cmsg_len = msg.msg_controllen;
  390. /* silence aliasing warning */
  391. {
  392. void* pv = CMSG_DATA(cmsg);
  393. int* pi = pv;
  394. *pi = fd_to_send;
  395. }
  396. do {
  397. n = sendmsg(stream->io_watcher.fd, &msg, 0);
  398. }
  399. while (n == -1 && errno == EINTR);
  400. } else {
  401. do {
  402. if (iovcnt == 1) {
  403. n = write(stream->io_watcher.fd, iov[0].iov_base, iov[0].iov_len);
  404. } else {
  405. n = writev(stream->io_watcher.fd, iov, iovcnt);
  406. }
  407. }
  408. while (n == -1 && errno == EINTR);
  409. }
  410. if (n < 0) {
  411. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  412. /* Error */
  413. req->error = errno;
  414. stream->write_queue_size -= uv__write_req_size(req);
  415. uv__write_req_finish(req);
  416. return;
  417. } else if (stream->flags & UV_STREAM_BLOCKING) {
  418. /* If this is a blocking stream, try again. */
  419. goto start;
  420. }
  421. } else {
  422. /* Successful write */
  423. while (n >= 0) {
  424. uv_buf_t* buf = &(req->bufs[req->write_index]);
  425. size_t len = buf->len;
  426. assert(req->write_index < req->bufcnt);
  427. if ((size_t)n < len) {
  428. buf->base += n;
  429. buf->len -= n;
  430. stream->write_queue_size -= n;
  431. n = 0;
  432. /* There is more to write. */
  433. if (stream->flags & UV_STREAM_BLOCKING) {
  434. /*
  435. * If we're blocking then we should not be enabling the write
  436. * watcher - instead we need to try again.
  437. */
  438. goto start;
  439. } else {
  440. /* Break loop and ensure the watcher is pending. */
  441. break;
  442. }
  443. } else {
  444. /* Finished writing the buf at index req->write_index. */
  445. req->write_index++;
  446. assert((size_t)n >= len);
  447. n -= len;
  448. assert(stream->write_queue_size >= len);
  449. stream->write_queue_size -= len;
  450. if (req->write_index == req->bufcnt) {
  451. /* Then we're done! */
  452. assert(n == 0);
  453. uv__write_req_finish(req);
  454. /* TODO: start trying to write the next request. */
  455. return;
  456. }
  457. }
  458. }
  459. }
  460. /* Either we've counted n down to zero or we've got EAGAIN. */
  461. assert(n == 0 || n == -1);
  462. /* Only non-blocking streams should use the write_watcher. */
  463. assert(!(stream->flags & UV_STREAM_BLOCKING));
  464. /* We're not done. */
  465. uv__io_start(stream->loop, &stream->io_watcher, UV__POLLOUT);
  466. }
  467. static void uv__write_callbacks(uv_stream_t* stream) {
  468. uv_write_t* req;
  469. ngx_queue_t* q;
  470. while (!ngx_queue_empty(&stream->write_completed_queue)) {
  471. /* Pop a req off write_completed_queue. */
  472. q = ngx_queue_head(&stream->write_completed_queue);
  473. req = ngx_queue_data(q, uv_write_t, queue);
  474. ngx_queue_remove(q);
  475. uv__req_unregister(stream->loop, req);
  476. /* NOTE: call callback AFTER freeing the request data. */
  477. if (req->cb) {
  478. uv__set_sys_error(stream->loop, req->error);
  479. req->cb(req, req->error ? -1 : 0);
  480. }
  481. }
  482. assert(ngx_queue_empty(&stream->write_completed_queue));
  483. /* Write queue drained. */
  484. if (!uv_write_queue_head(stream)) {
  485. uv__drain(stream);
  486. }
  487. }
  488. static uv_handle_type uv__handle_type(int fd) {
  489. struct sockaddr_storage ss;
  490. socklen_t len;
  491. memset(&ss, 0, sizeof(ss));
  492. len = sizeof(ss);
  493. if (getsockname(fd, (struct sockaddr*)&ss, &len))
  494. return UV_UNKNOWN_HANDLE;
  495. switch (ss.ss_family) {
  496. case AF_UNIX:
  497. return UV_NAMED_PIPE;
  498. case AF_INET:
  499. case AF_INET6:
  500. return UV_TCP;
  501. }
  502. return UV_UNKNOWN_HANDLE;
  503. }
  504. static void uv__read(uv_stream_t* stream) {
  505. uv_buf_t buf;
  506. ssize_t nread;
  507. struct msghdr msg;
  508. struct cmsghdr* cmsg;
  509. char cmsg_space[64];
  510. int count;
  511. /* Prevent loop starvation when the data comes in as fast as (or faster than)
  512. * we can read it. XXX Need to rearm fd if we switch to edge-triggered I/O.
  513. */
  514. count = 32;
  515. /* XXX: Maybe instead of having UV_STREAM_READING we just test if
  516. * tcp->read_cb is NULL or not?
  517. */
  518. while ((stream->read_cb || stream->read2_cb)
  519. && (stream->flags & UV_STREAM_READING)
  520. && (count-- > 0)) {
  521. assert(stream->alloc_cb);
  522. buf = stream->alloc_cb((uv_handle_t*)stream, 64 * 1024);
  523. assert(buf.len > 0);
  524. assert(buf.base);
  525. assert(stream->io_watcher.fd >= 0);
  526. if (stream->read_cb) {
  527. do {
  528. nread = read(stream->io_watcher.fd, buf.base, buf.len);
  529. }
  530. while (nread < 0 && errno == EINTR);
  531. } else {
  532. assert(stream->read2_cb);
  533. /* read2_cb uses recvmsg */
  534. msg.msg_flags = 0;
  535. msg.msg_iov = (struct iovec*) &buf;
  536. msg.msg_iovlen = 1;
  537. msg.msg_name = NULL;
  538. msg.msg_namelen = 0;
  539. /* Set up to receive a descriptor even if one isn't in the message */
  540. msg.msg_controllen = 64;
  541. msg.msg_control = (void *) cmsg_space;
  542. do {
  543. nread = recvmsg(stream->io_watcher.fd, &msg, 0);
  544. }
  545. while (nread < 0 && errno == EINTR);
  546. }
  547. if (nread < 0) {
  548. /* Error */
  549. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  550. /* Wait for the next one. */
  551. if (stream->flags & UV_STREAM_READING) {
  552. uv__io_start(stream->loop, &stream->io_watcher, UV__POLLIN);
  553. }
  554. uv__set_sys_error(stream->loop, EAGAIN);
  555. if (stream->read_cb) {
  556. stream->read_cb(stream, 0, buf);
  557. } else {
  558. stream->read2_cb((uv_pipe_t*)stream, 0, buf, UV_UNKNOWN_HANDLE);
  559. }
  560. return;
  561. } else {
  562. /* Error. User should call uv_close(). */
  563. uv__set_sys_error(stream->loop, errno);
  564. if (stream->read_cb) {
  565. stream->read_cb(stream, -1, buf);
  566. } else {
  567. stream->read2_cb((uv_pipe_t*)stream, -1, buf, UV_UNKNOWN_HANDLE);
  568. }
  569. assert(!uv__io_active(&stream->io_watcher, UV__POLLIN));
  570. return;
  571. }
  572. } else if (nread == 0) {
  573. /* EOF */
  574. uv__set_artificial_error(stream->loop, UV_EOF);
  575. uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLIN);
  576. if (!uv__io_active(&stream->io_watcher, UV__POLLOUT))
  577. uv__handle_stop(stream);
  578. if (stream->read_cb) {
  579. stream->read_cb(stream, -1, buf);
  580. } else {
  581. stream->read2_cb((uv_pipe_t*)stream, -1, buf, UV_UNKNOWN_HANDLE);
  582. }
  583. return;
  584. } else {
  585. /* Successful read */
  586. ssize_t buflen = buf.len;
  587. if (stream->read_cb) {
  588. stream->read_cb(stream, nread, buf);
  589. } else {
  590. assert(stream->read2_cb);
  591. /*
  592. * XXX: Some implementations can send multiple file descriptors in a
  593. * single message. We should be using CMSG_NXTHDR() to walk the
  594. * chain to get at them all. This would require changing the API to
  595. * hand these back up the caller, is a pain.
  596. */
  597. for (cmsg = CMSG_FIRSTHDR(&msg);
  598. msg.msg_controllen > 0 && cmsg != NULL;
  599. cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  600. if (cmsg->cmsg_type == SCM_RIGHTS) {
  601. if (stream->accepted_fd != -1) {
  602. fprintf(stderr, "(libuv) ignoring extra FD received\n");
  603. }
  604. /* silence aliasing warning */
  605. {
  606. void* pv = CMSG_DATA(cmsg);
  607. int* pi = pv;
  608. stream->accepted_fd = *pi;
  609. }
  610. } else {
  611. fprintf(stderr, "ignoring non-SCM_RIGHTS ancillary data: %d\n",
  612. cmsg->cmsg_type);
  613. }
  614. }
  615. if (stream->accepted_fd >= 0) {
  616. stream->read2_cb((uv_pipe_t*)stream, nread, buf,
  617. uv__handle_type(stream->accepted_fd));
  618. } else {
  619. stream->read2_cb((uv_pipe_t*)stream, nread, buf, UV_UNKNOWN_HANDLE);
  620. }
  621. }
  622. /* Return if we didn't fill the buffer, there is no more data to read. */
  623. if (nread < buflen) {
  624. return;
  625. }
  626. }
  627. }
  628. }
  629. int uv_shutdown(uv_shutdown_t* req, uv_stream_t* stream, uv_shutdown_cb cb) {
  630. assert((stream->type == UV_TCP || stream->type == UV_NAMED_PIPE) &&
  631. "uv_shutdown (unix) only supports uv_handle_t right now");
  632. assert(stream->io_watcher.fd >= 0);
  633. if (!(stream->flags & UV_STREAM_WRITABLE) ||
  634. stream->flags & UV_STREAM_SHUT ||
  635. stream->flags & UV_CLOSED ||
  636. stream->flags & UV_CLOSING) {
  637. uv__set_artificial_error(stream->loop, UV_ENOTCONN);
  638. return -1;
  639. }
  640. /* Initialize request */
  641. uv__req_init(stream->loop, req, UV_SHUTDOWN);
  642. req->handle = stream;
  643. req->cb = cb;
  644. stream->shutdown_req = req;
  645. stream->flags |= UV_STREAM_SHUTTING;
  646. uv__io_start(stream->loop, &stream->io_watcher, UV__POLLOUT);
  647. return 0;
  648. }
  649. static void uv__stream_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
  650. uv_stream_t* stream;
  651. stream = container_of(w, uv_stream_t, io_watcher);
  652. assert(stream->type == UV_TCP ||
  653. stream->type == UV_NAMED_PIPE ||
  654. stream->type == UV_TTY);
  655. assert(!(stream->flags & UV_CLOSING));
  656. if (stream->connect_req) {
  657. uv__stream_connect(stream);
  658. return;
  659. }
  660. if (events & UV__POLLIN) {
  661. assert(stream->io_watcher.fd >= 0);
  662. uv__read(stream);
  663. if (stream->io_watcher.fd == -1)
  664. return; /* read_cb closed stream. */
  665. }
  666. if (events & UV__POLLOUT) {
  667. assert(stream->io_watcher.fd >= 0);
  668. uv__write(stream);
  669. uv__write_callbacks(stream);
  670. }
  671. }
  672. /**
  673. * We get called here from directly following a call to connect(2).
  674. * In order to determine if we've errored out or succeeded must call
  675. * getsockopt.
  676. */
  677. static void uv__stream_connect(uv_stream_t* stream) {
  678. int error;
  679. uv_connect_t* req = stream->connect_req;
  680. socklen_t errorsize = sizeof(int);
  681. assert(stream->type == UV_TCP || stream->type == UV_NAMED_PIPE);
  682. assert(req);
  683. if (stream->delayed_error) {
  684. /* To smooth over the differences between unixes errors that
  685. * were reported synchronously on the first connect can be delayed
  686. * until the next tick--which is now.
  687. */
  688. error = stream->delayed_error;
  689. stream->delayed_error = 0;
  690. } else {
  691. /* Normal situation: we need to get the socket error from the kernel. */
  692. assert(stream->io_watcher.fd >= 0);
  693. getsockopt(stream->io_watcher.fd, SOL_SOCKET, SO_ERROR, &error, &errorsize);
  694. }
  695. if (error == EINPROGRESS)
  696. return;
  697. stream->connect_req = NULL;
  698. uv__req_unregister(stream->loop, req);
  699. if (req->cb) {
  700. uv__set_sys_error(stream->loop, error);
  701. req->cb(req, error ? -1 : 0);
  702. }
  703. }
  704. int uv_write2(uv_write_t* req,
  705. uv_stream_t* stream,
  706. uv_buf_t bufs[],
  707. int bufcnt,
  708. uv_stream_t* send_handle,
  709. uv_write_cb cb) {
  710. int empty_queue;
  711. assert(bufcnt > 0);
  712. assert((stream->type == UV_TCP || stream->type == UV_NAMED_PIPE ||
  713. stream->type == UV_TTY) &&
  714. "uv_write (unix) does not yet support other types of streams");
  715. if (stream->io_watcher.fd < 0) {
  716. uv__set_sys_error(stream->loop, EBADF);
  717. return -1;
  718. }
  719. if (send_handle) {
  720. if (stream->type != UV_NAMED_PIPE || !((uv_pipe_t*)stream)->ipc) {
  721. uv__set_sys_error(stream->loop, EOPNOTSUPP);
  722. return -1;
  723. }
  724. }
  725. empty_queue = (stream->write_queue_size == 0);
  726. /* Initialize the req */
  727. uv__req_init(stream->loop, req, UV_WRITE);
  728. req->cb = cb;
  729. req->handle = stream;
  730. req->error = 0;
  731. req->send_handle = send_handle;
  732. ngx_queue_init(&req->queue);
  733. if (bufcnt <= (int) ARRAY_SIZE(req->bufsml))
  734. req->bufs = req->bufsml;
  735. else
  736. req->bufs = malloc(sizeof(uv_buf_t) * bufcnt);
  737. memcpy(req->bufs, bufs, bufcnt * sizeof(uv_buf_t));
  738. req->bufcnt = bufcnt;
  739. req->write_index = 0;
  740. stream->write_queue_size += uv__buf_count(bufs, bufcnt);
  741. /* Append the request to write_queue. */
  742. ngx_queue_insert_tail(&stream->write_queue, &req->queue);
  743. /* If the queue was empty when this function began, we should attempt to
  744. * do the write immediately. Otherwise start the write_watcher and wait
  745. * for the fd to become writable.
  746. */
  747. if (stream->connect_req) {
  748. /* Still connecting, do nothing. */
  749. }
  750. else if (empty_queue) {
  751. uv__write(stream);
  752. }
  753. else {
  754. /*
  755. * blocking streams should never have anything in the queue.
  756. * if this assert fires then somehow the blocking stream isn't being
  757. * sufficiently flushed in uv__write.
  758. */
  759. assert(!(stream->flags & UV_STREAM_BLOCKING));
  760. uv__io_start(stream->loop, &stream->io_watcher, UV__POLLOUT);
  761. }
  762. return 0;
  763. }
  764. /* The buffers to be written must remain valid until the callback is called.
  765. * This is not required for the uv_buf_t array.
  766. */
  767. int uv_write(uv_write_t* req, uv_stream_t* stream, uv_buf_t bufs[], int bufcnt,
  768. uv_write_cb cb) {
  769. return uv_write2(req, stream, bufs, bufcnt, NULL, cb);
  770. }
  771. int uv__read_start_common(uv_stream_t* stream, uv_alloc_cb alloc_cb,
  772. uv_read_cb read_cb, uv_read2_cb read2_cb) {
  773. assert(stream->type == UV_TCP || stream->type == UV_NAMED_PIPE ||
  774. stream->type == UV_TTY);
  775. if (stream->flags & UV_CLOSING) {
  776. uv__set_sys_error(stream->loop, EINVAL);
  777. return -1;
  778. }
  779. /* The UV_STREAM_READING flag is irrelevant of the state of the tcp - it just
  780. * expresses the desired state of the user.
  781. */
  782. stream->flags |= UV_STREAM_READING;
  783. /* TODO: try to do the read inline? */
  784. /* TODO: keep track of tcp state. If we've gotten a EOF then we should
  785. * not start the IO watcher.
  786. */
  787. assert(stream->io_watcher.fd >= 0);
  788. assert(alloc_cb);
  789. stream->read_cb = read_cb;
  790. stream->read2_cb = read2_cb;
  791. stream->alloc_cb = alloc_cb;
  792. uv__io_start(stream->loop, &stream->io_watcher, UV__POLLIN);
  793. uv__handle_start(stream);
  794. return 0;
  795. }
  796. int uv_read_start(uv_stream_t* stream, uv_alloc_cb alloc_cb,
  797. uv_read_cb read_cb) {
  798. return uv__read_start_common(stream, alloc_cb, read_cb, NULL);
  799. }
  800. int uv_read2_start(uv_stream_t* stream, uv_alloc_cb alloc_cb,
  801. uv_read2_cb read_cb) {
  802. return uv__read_start_common(stream, alloc_cb, NULL, read_cb);
  803. }
  804. int uv_read_stop(uv_stream_t* stream) {
  805. uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLIN);
  806. uv__handle_stop(stream);
  807. stream->flags &= ~UV_STREAM_READING;
  808. stream->read_cb = NULL;
  809. stream->read2_cb = NULL;
  810. stream->alloc_cb = NULL;
  811. return 0;
  812. }
  813. int uv_is_readable(const uv_stream_t* stream) {
  814. return stream->flags & UV_STREAM_READABLE;
  815. }
  816. int uv_is_writable(const uv_stream_t* stream) {
  817. return stream->flags & UV_STREAM_WRITABLE;
  818. }
  819. void uv__stream_close(uv_stream_t* handle) {
  820. uv_read_stop(handle);
  821. uv__io_stop(handle->loop, &handle->io_watcher, UV__POLLOUT);
  822. close(handle->io_watcher.fd);
  823. handle->io_watcher.fd = -1;
  824. if (handle->accepted_fd >= 0) {
  825. close(handle->accepted_fd);
  826. handle->accepted_fd = -1;
  827. }
  828. assert(!uv__io_active(&handle->io_watcher, UV__POLLIN | UV__POLLOUT));
  829. }