/src/CMake/Utilities/cmlibuv/src/unix/stream.c
Line | Count | Source |
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 | | |
22 | | #include "uv.h" |
23 | | #include "internal.h" |
24 | | |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | #include <assert.h> |
29 | | #include <errno.h> |
30 | | |
31 | | #include <sys/types.h> |
32 | | #include <sys/socket.h> |
33 | | #include <sys/uio.h> |
34 | | #include <sys/un.h> |
35 | | #include <unistd.h> |
36 | | #include <limits.h> /* IOV_MAX */ |
37 | | |
38 | | #if defined(__APPLE__) |
39 | | # include <sys/event.h> |
40 | | # include <sys/time.h> |
41 | | # include <sys/select.h> |
42 | | |
43 | | /* Forward declaration */ |
44 | | typedef struct uv__stream_select_s uv__stream_select_t; |
45 | | |
46 | | struct uv__stream_select_s { |
47 | | uv_stream_t* stream; |
48 | | uv_thread_t thread; |
49 | | uv_sem_t close_sem; |
50 | | uv_sem_t async_sem; |
51 | | uv_async_t async; |
52 | | int events; |
53 | | int fake_fd; |
54 | | int int_fd; |
55 | | int fd; |
56 | | fd_set* sread; |
57 | | size_t sread_sz; |
58 | | fd_set* swrite; |
59 | | size_t swrite_sz; |
60 | | }; |
61 | | #endif /* defined(__APPLE__) */ |
62 | | |
63 | | union uv__cmsg { |
64 | | struct cmsghdr hdr; |
65 | | /* This cannot be larger because of the IBMi PASE limitation that |
66 | | * the total size of control messages cannot exceed 256 bytes. |
67 | | */ |
68 | | char pad[256]; |
69 | | }; |
70 | | |
71 | | STATIC_ASSERT(256 == sizeof(union uv__cmsg)); |
72 | | |
73 | | static void uv__stream_connect(uv_stream_t*); |
74 | | static void uv__write(uv_stream_t* stream); |
75 | | static void uv__read(uv_stream_t* stream); |
76 | | static void uv__write_callbacks(uv_stream_t* stream); |
77 | | static size_t uv__write_req_size(uv_write_t* req); |
78 | | static void uv__drain(uv_stream_t* stream); |
79 | | |
80 | | |
81 | | void uv__stream_init(uv_loop_t* loop, |
82 | | uv_stream_t* stream, |
83 | 0 | uv_handle_type type) { |
84 | 0 | int err; |
85 | |
|
86 | 0 | uv__handle_init(loop, (uv_handle_t*)stream, type); |
87 | 0 | stream->read_cb = NULL; |
88 | 0 | stream->alloc_cb = NULL; |
89 | 0 | stream->close_cb = NULL; |
90 | 0 | stream->connection_cb = NULL; |
91 | 0 | stream->connect_req = NULL; |
92 | 0 | stream->shutdown_req = NULL; |
93 | 0 | stream->accepted_fd = -1; |
94 | 0 | stream->queued_fds = NULL; |
95 | 0 | stream->delayed_error = 0; |
96 | 0 | uv__queue_init(&stream->write_queue); |
97 | 0 | uv__queue_init(&stream->write_completed_queue); |
98 | 0 | stream->write_queue_size = 0; |
99 | |
|
100 | 0 | if (loop->emfile_fd == -1) { |
101 | 0 | err = uv__open_cloexec("/dev/null", O_RDONLY); |
102 | 0 | if (err < 0) |
103 | | /* In the rare case that "/dev/null" isn't mounted open "/" |
104 | | * instead. |
105 | | */ |
106 | 0 | err = uv__open_cloexec("/", O_RDONLY); |
107 | 0 | if (err >= 0) |
108 | 0 | loop->emfile_fd = err; |
109 | 0 | } |
110 | |
|
111 | | #if defined(__APPLE__) && !defined(CMAKE_BOOTSTRAP) |
112 | | stream->select = NULL; |
113 | | #endif /* defined(__APPLE_) */ |
114 | |
|
115 | 0 | uv__io_init(&stream->io_watcher, UV__STREAM_IO, -1); |
116 | 0 | } |
117 | | |
118 | | |
119 | 0 | static void uv__stream_osx_interrupt_select(uv_stream_t* stream) { |
120 | | #if defined(__APPLE__) && !defined(CMAKE_BOOTSTRAP) |
121 | | /* Notify select() thread about state change */ |
122 | | uv__stream_select_t* s; |
123 | | int r; |
124 | | |
125 | | s = stream->select; |
126 | | if (s == NULL) |
127 | | return; |
128 | | |
129 | | /* Interrupt select() loop |
130 | | * NOTE: fake_fd and int_fd are socketpair(), thus writing to one will |
131 | | * emit read event on other side |
132 | | */ |
133 | | do |
134 | | r = write(s->fake_fd, "x", 1); |
135 | | while (r == -1 && errno == EINTR); |
136 | | |
137 | | assert(r == 1); |
138 | | #else /* !defined(__APPLE__) */ |
139 | | /* No-op on any other platform */ |
140 | 0 | #endif /* !defined(__APPLE__) */ |
141 | 0 | } |
142 | | |
143 | | |
144 | | #if defined(__APPLE__) && !defined(CMAKE_BOOTSTRAP) |
145 | | static void uv__stream_osx_select(void* arg) { |
146 | | uv_stream_t* stream; |
147 | | uv__stream_select_t* s; |
148 | | char buf[1024]; |
149 | | int events; |
150 | | int fd; |
151 | | int r; |
152 | | int max_fd; |
153 | | |
154 | | stream = arg; |
155 | | s = stream->select; |
156 | | fd = s->fd; |
157 | | |
158 | | if (fd > s->int_fd) |
159 | | max_fd = fd; |
160 | | else |
161 | | max_fd = s->int_fd; |
162 | | |
163 | | for (;;) { |
164 | | /* Terminate on semaphore */ |
165 | | if (uv_sem_trywait(&s->close_sem) == 0) |
166 | | break; |
167 | | |
168 | | /* Watch fd using select(2) */ |
169 | | memset(s->sread, 0, s->sread_sz); |
170 | | memset(s->swrite, 0, s->swrite_sz); |
171 | | |
172 | | if (uv__io_active(&stream->io_watcher, POLLIN)) |
173 | | FD_SET(fd, s->sread); |
174 | | if (uv__io_active(&stream->io_watcher, POLLOUT)) |
175 | | FD_SET(fd, s->swrite); |
176 | | FD_SET(s->int_fd, s->sread); |
177 | | |
178 | | /* Wait indefinitely for fd events */ |
179 | | r = select(max_fd + 1, s->sread, s->swrite, NULL, NULL); |
180 | | if (r == -1) { |
181 | | if (errno == EINTR) |
182 | | continue; |
183 | | |
184 | | /* XXX: Possible?! */ |
185 | | abort(); |
186 | | } |
187 | | |
188 | | /* Ignore timeouts */ |
189 | | if (r == 0) |
190 | | continue; |
191 | | |
192 | | /* Empty socketpair's buffer in case of interruption */ |
193 | | if (FD_ISSET(s->int_fd, s->sread)) |
194 | | for (;;) { |
195 | | r = read(s->int_fd, buf, sizeof(buf)); |
196 | | |
197 | | if (r == sizeof(buf)) |
198 | | continue; |
199 | | |
200 | | if (r != -1) |
201 | | break; |
202 | | |
203 | | if (errno == EAGAIN || errno == EWOULDBLOCK) |
204 | | break; |
205 | | |
206 | | if (errno == EINTR) |
207 | | continue; |
208 | | |
209 | | abort(); |
210 | | } |
211 | | |
212 | | /* Handle events */ |
213 | | events = 0; |
214 | | if (FD_ISSET(fd, s->sread)) |
215 | | events |= POLLIN; |
216 | | if (FD_ISSET(fd, s->swrite)) |
217 | | events |= POLLOUT; |
218 | | |
219 | | assert(events != 0 || FD_ISSET(s->int_fd, s->sread)); |
220 | | if (events != 0) { |
221 | | ACCESS_ONCE(int, s->events) = events; |
222 | | |
223 | | uv_async_send(&s->async); |
224 | | uv_sem_wait(&s->async_sem); |
225 | | |
226 | | /* Should be processed at this stage */ |
227 | | assert((s->events == 0) || (stream->flags & UV_HANDLE_CLOSING)); |
228 | | } |
229 | | } |
230 | | } |
231 | | |
232 | | |
233 | | static void uv__stream_osx_select_cb(uv_async_t* handle) { |
234 | | uv__stream_select_t* s; |
235 | | uv_stream_t* stream; |
236 | | int events; |
237 | | |
238 | | s = container_of(handle, uv__stream_select_t, async); |
239 | | stream = s->stream; |
240 | | |
241 | | /* Get and reset stream's events */ |
242 | | events = s->events; |
243 | | ACCESS_ONCE(int, s->events) = 0; |
244 | | |
245 | | assert(events != 0); |
246 | | assert(events == (events & (POLLIN | POLLOUT))); |
247 | | |
248 | | /* Invoke callback on event-loop */ |
249 | | if ((events & POLLIN) && uv__io_active(&stream->io_watcher, POLLIN)) |
250 | | uv__stream_io(stream->loop, &stream->io_watcher, POLLIN); |
251 | | |
252 | | if ((events & POLLOUT) && uv__io_active(&stream->io_watcher, POLLOUT)) |
253 | | uv__stream_io(stream->loop, &stream->io_watcher, POLLOUT); |
254 | | |
255 | | if (stream->flags & UV_HANDLE_CLOSING) |
256 | | return; |
257 | | |
258 | | /* NOTE: It is important to do it here, otherwise `select()` might be called |
259 | | * before the actual `uv__read()`, leading to the blocking syscall |
260 | | */ |
261 | | uv_sem_post(&s->async_sem); |
262 | | } |
263 | | |
264 | | |
265 | | static void uv__stream_osx_cb_close(uv_handle_t* async) { |
266 | | uv__stream_select_t* s; |
267 | | |
268 | | s = container_of(async, uv__stream_select_t, async); |
269 | | uv__free(s); |
270 | | } |
271 | | |
272 | | |
273 | | int uv__stream_try_select(uv_stream_t* stream, int* fd) { |
274 | | /* |
275 | | * kqueue doesn't work with some files from /dev mount on osx. |
276 | | * select(2) in separate thread for those fds |
277 | | */ |
278 | | |
279 | | struct kevent filter[1]; |
280 | | struct kevent events[1]; |
281 | | struct timespec timeout; |
282 | | uv__stream_select_t* s; |
283 | | int fds[2]; |
284 | | int err; |
285 | | int ret; |
286 | | int kq; |
287 | | int old_fd; |
288 | | int max_fd; |
289 | | size_t sread_sz; |
290 | | size_t swrite_sz; |
291 | | |
292 | | kq = kqueue(); |
293 | | if (kq == -1) { |
294 | | perror("(libuv) kqueue()"); |
295 | | return UV__ERR(errno); |
296 | | } |
297 | | |
298 | | EV_SET(&filter[0], *fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0); |
299 | | |
300 | | /* Use small timeout, because we only want to capture EINVALs */ |
301 | | timeout.tv_sec = 0; |
302 | | timeout.tv_nsec = 1; |
303 | | |
304 | | do |
305 | | ret = kevent(kq, filter, 1, events, 1, &timeout); |
306 | | while (ret == -1 && errno == EINTR); |
307 | | |
308 | | uv__close(kq); |
309 | | |
310 | | if (ret == -1) |
311 | | return UV__ERR(errno); |
312 | | |
313 | | if (ret == 0 || (events[0].flags & EV_ERROR) == 0 || events[0].data != EINVAL) |
314 | | return 0; |
315 | | |
316 | | /* At this point we definitely know that this fd won't work with kqueue */ |
317 | | |
318 | | /* |
319 | | * Create fds for io watcher and to interrupt the select() loop. |
320 | | * NOTE: do it ahead of malloc below to allocate enough space for fd_sets |
321 | | */ |
322 | | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) |
323 | | return UV__ERR(errno); |
324 | | |
325 | | max_fd = *fd; |
326 | | if (fds[1] > max_fd) |
327 | | max_fd = fds[1]; |
328 | | |
329 | | sread_sz = ROUND_UP(max_fd + 1, sizeof(uint32_t) * NBBY) / NBBY; |
330 | | swrite_sz = sread_sz; |
331 | | |
332 | | s = uv__malloc(sizeof(*s) + sread_sz + swrite_sz); |
333 | | if (s == NULL) { |
334 | | err = UV_ENOMEM; |
335 | | goto failed_malloc; |
336 | | } |
337 | | |
338 | | s->events = 0; |
339 | | s->fd = *fd; |
340 | | s->sread = (fd_set*) ((char*) s + sizeof(*s)); |
341 | | s->sread_sz = sread_sz; |
342 | | s->swrite = (fd_set*) ((char*) s->sread + sread_sz); |
343 | | s->swrite_sz = swrite_sz; |
344 | | |
345 | | err = uv_async_init(stream->loop, &s->async, uv__stream_osx_select_cb); |
346 | | if (err) |
347 | | goto failed_async_init; |
348 | | |
349 | | s->async.flags |= UV_HANDLE_INTERNAL; |
350 | | uv__handle_unref(&s->async); |
351 | | |
352 | | err = uv_sem_init(&s->close_sem, 0); |
353 | | if (err != 0) |
354 | | goto failed_close_sem_init; |
355 | | |
356 | | err = uv_sem_init(&s->async_sem, 0); |
357 | | if (err != 0) |
358 | | goto failed_async_sem_init; |
359 | | |
360 | | s->fake_fd = fds[0]; |
361 | | s->int_fd = fds[1]; |
362 | | |
363 | | old_fd = *fd; |
364 | | s->stream = stream; |
365 | | stream->select = s; |
366 | | *fd = s->fake_fd; |
367 | | |
368 | | err = uv_thread_create(&s->thread, uv__stream_osx_select, stream); |
369 | | if (err != 0) |
370 | | goto failed_thread_create; |
371 | | |
372 | | return 0; |
373 | | |
374 | | failed_thread_create: |
375 | | s->stream = NULL; |
376 | | stream->select = NULL; |
377 | | *fd = old_fd; |
378 | | |
379 | | uv_sem_destroy(&s->async_sem); |
380 | | |
381 | | failed_async_sem_init: |
382 | | uv_sem_destroy(&s->close_sem); |
383 | | |
384 | | failed_close_sem_init: |
385 | | uv__close(fds[0]); |
386 | | uv__close(fds[1]); |
387 | | uv_close((uv_handle_t*) &s->async, uv__stream_osx_cb_close); |
388 | | return err; |
389 | | |
390 | | failed_async_init: |
391 | | uv__free(s); |
392 | | |
393 | | failed_malloc: |
394 | | uv__close(fds[0]); |
395 | | uv__close(fds[1]); |
396 | | |
397 | | return err; |
398 | | } |
399 | | #endif /* defined(__APPLE__) */ |
400 | | |
401 | | |
402 | 0 | int uv__stream_open(uv_stream_t* stream, int fd, int flags) { |
403 | | #if defined(__APPLE__) |
404 | | int enable; |
405 | | #endif |
406 | |
|
407 | 0 | if (!(stream->io_watcher.fd == -1 || stream->io_watcher.fd == fd)) |
408 | 0 | return UV_EBUSY; |
409 | | |
410 | 0 | assert(fd >= 0); |
411 | 0 | stream->flags |= flags; |
412 | |
|
413 | 0 | if (stream->type == UV_TCP) { |
414 | 0 | if ((stream->flags & UV_HANDLE_TCP_NODELAY) && uv__tcp_nodelay(fd, 1)) |
415 | 0 | return UV__ERR(errno); |
416 | | |
417 | | /* TODO Use delay the user passed in. */ |
418 | 0 | if ((stream->flags & UV_HANDLE_TCP_KEEPALIVE) && |
419 | 0 | uv__tcp_keepalive(fd, 1, 60, 1, 10)) { |
420 | 0 | return UV__ERR(errno); |
421 | 0 | } |
422 | 0 | } |
423 | | |
424 | | #if defined(__APPLE__) |
425 | | enable = 1; |
426 | | if (setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &enable, sizeof(enable)) && |
427 | | errno != ENOTSOCK && |
428 | | errno != EINVAL) { |
429 | | return UV__ERR(errno); |
430 | | } |
431 | | #endif |
432 | | |
433 | 0 | stream->io_watcher.fd = fd; |
434 | |
|
435 | 0 | return 0; |
436 | 0 | } |
437 | | |
438 | | |
439 | 0 | void uv__stream_flush_write_queue(uv_stream_t* stream, int error) { |
440 | 0 | uv_write_t* req; |
441 | 0 | struct uv__queue* q; |
442 | 0 | while (!uv__queue_empty(&stream->write_queue)) { |
443 | 0 | q = uv__queue_head(&stream->write_queue); |
444 | 0 | uv__queue_remove(q); |
445 | |
|
446 | 0 | req = uv__queue_data(q, uv_write_t, queue); |
447 | 0 | req->error = error; |
448 | |
|
449 | 0 | uv__queue_insert_tail(&stream->write_completed_queue, &req->queue); |
450 | 0 | } |
451 | 0 | } |
452 | | |
453 | | |
454 | 0 | void uv__stream_destroy(uv_stream_t* stream) { |
455 | 0 | assert(!uv__io_active(&stream->io_watcher, POLLIN | POLLOUT)); |
456 | 0 | assert(stream->flags & UV_HANDLE_CLOSED); |
457 | |
|
458 | 0 | if (stream->connect_req) { |
459 | 0 | uv__req_unregister(stream->loop); |
460 | 0 | stream->connect_req->cb(stream->connect_req, UV_ECANCELED); |
461 | 0 | stream->connect_req = NULL; |
462 | 0 | } |
463 | |
|
464 | 0 | uv__stream_flush_write_queue(stream, UV_ECANCELED); |
465 | 0 | uv__write_callbacks(stream); |
466 | 0 | uv__drain(stream); |
467 | |
|
468 | 0 | assert(stream->write_queue_size == 0); |
469 | 0 | } |
470 | | |
471 | | |
472 | | /* Implements a best effort approach to mitigating accept() EMFILE errors. |
473 | | * We have a spare file descriptor stashed away that we close to get below |
474 | | * the EMFILE limit. Next, we accept all pending connections and close them |
475 | | * immediately to signal the clients that we're overloaded - and we are, but |
476 | | * we still keep on trucking. |
477 | | * |
478 | | * There is one caveat: it's not reliable in a multi-threaded environment. |
479 | | * The file descriptor limit is per process. Our party trick fails if another |
480 | | * thread opens a file or creates a socket in the time window between us |
481 | | * calling close() and accept(). |
482 | | */ |
483 | 0 | static int uv__emfile_trick(uv_loop_t* loop, int accept_fd) { |
484 | 0 | int err; |
485 | 0 | int emfile_fd; |
486 | |
|
487 | 0 | if (loop->emfile_fd == -1) |
488 | 0 | return UV_EMFILE; |
489 | | |
490 | 0 | uv__close(loop->emfile_fd); |
491 | 0 | loop->emfile_fd = -1; |
492 | |
|
493 | 0 | do { |
494 | 0 | err = uv__accept(accept_fd); |
495 | 0 | if (err >= 0) |
496 | 0 | uv__close(err); |
497 | 0 | } while (err >= 0 || err == UV_EINTR); |
498 | |
|
499 | 0 | emfile_fd = uv__open_cloexec("/", O_RDONLY); |
500 | 0 | if (emfile_fd >= 0) |
501 | 0 | loop->emfile_fd = emfile_fd; |
502 | |
|
503 | 0 | return err; |
504 | 0 | } |
505 | | |
506 | | |
507 | 0 | void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) { |
508 | 0 | uv_stream_t* stream; |
509 | 0 | int err; |
510 | 0 | int fd; |
511 | |
|
512 | 0 | stream = container_of(w, uv_stream_t, io_watcher); |
513 | 0 | assert(events & POLLIN); |
514 | 0 | assert(stream->accepted_fd == -1); |
515 | 0 | assert(!(stream->flags & UV_HANDLE_CLOSING)); |
516 | |
|
517 | 0 | fd = uv__stream_fd(stream); |
518 | 0 | err = uv__accept(fd); |
519 | |
|
520 | 0 | if (err == UV_EMFILE || err == UV_ENFILE) |
521 | 0 | err = uv__emfile_trick(loop, fd); /* Shed load. */ |
522 | |
|
523 | 0 | if (err < 0) |
524 | 0 | return; |
525 | | |
526 | 0 | stream->accepted_fd = err; |
527 | 0 | stream->connection_cb(stream, 0); |
528 | |
|
529 | 0 | if (stream->accepted_fd != -1) |
530 | | /* The user hasn't yet accepted called uv_accept() */ |
531 | 0 | uv__io_stop(loop, &stream->io_watcher, POLLIN); |
532 | 0 | } |
533 | | |
534 | | |
535 | 0 | int uv_accept(uv_stream_t* server, uv_stream_t* client) { |
536 | 0 | int err; |
537 | |
|
538 | 0 | assert(server->loop == client->loop); |
539 | |
|
540 | 0 | if (server->accepted_fd == -1) |
541 | 0 | return UV_EAGAIN; |
542 | | |
543 | 0 | switch (client->type) { |
544 | 0 | case UV_NAMED_PIPE: |
545 | 0 | case UV_TCP: |
546 | 0 | err = uv__stream_open(client, |
547 | 0 | server->accepted_fd, |
548 | 0 | UV_HANDLE_READABLE | UV_HANDLE_WRITABLE); |
549 | 0 | if (err) { |
550 | | /* TODO handle error */ |
551 | 0 | uv__close(server->accepted_fd); |
552 | 0 | goto done; |
553 | 0 | } |
554 | 0 | break; |
555 | | |
556 | 0 | case UV_UDP: |
557 | 0 | err = uv_udp_open((uv_udp_t*) client, server->accepted_fd); |
558 | 0 | if (err) { |
559 | 0 | uv__close(server->accepted_fd); |
560 | 0 | goto done; |
561 | 0 | } |
562 | 0 | break; |
563 | | |
564 | 0 | default: |
565 | 0 | return UV_EINVAL; |
566 | 0 | } |
567 | | |
568 | 0 | client->flags |= UV_HANDLE_BOUND; |
569 | |
|
570 | 0 | done: |
571 | | /* Process queued fds */ |
572 | 0 | if (server->queued_fds != NULL) { |
573 | 0 | uv__stream_queued_fds_t* queued_fds; |
574 | |
|
575 | 0 | queued_fds = server->queued_fds; |
576 | | |
577 | | /* Read first */ |
578 | 0 | server->accepted_fd = queued_fds->fds[0]; |
579 | | |
580 | | /* All read, free */ |
581 | 0 | assert(queued_fds->offset > 0); |
582 | 0 | if (--queued_fds->offset == 0) { |
583 | 0 | uv__free(queued_fds); |
584 | 0 | server->queued_fds = NULL; |
585 | 0 | } else { |
586 | | /* Shift rest */ |
587 | 0 | memmove(queued_fds->fds, |
588 | 0 | queued_fds->fds + 1, |
589 | 0 | queued_fds->offset * sizeof(*queued_fds->fds)); |
590 | 0 | } |
591 | 0 | } else { |
592 | 0 | server->accepted_fd = -1; |
593 | 0 | if (err == 0) |
594 | 0 | uv__io_start(server->loop, &server->io_watcher, POLLIN); |
595 | 0 | } |
596 | 0 | return err; |
597 | 0 | } |
598 | | |
599 | | |
600 | 0 | int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) { |
601 | 0 | int err; |
602 | 0 | if (uv__is_closing(stream)) { |
603 | 0 | return UV_EINVAL; |
604 | 0 | } |
605 | 0 | switch (stream->type) { |
606 | 0 | case UV_TCP: |
607 | 0 | err = uv__tcp_listen((uv_tcp_t*)stream, backlog, cb); |
608 | 0 | break; |
609 | | |
610 | 0 | case UV_NAMED_PIPE: |
611 | 0 | err = uv__pipe_listen((uv_pipe_t*)stream, backlog, cb); |
612 | 0 | break; |
613 | | |
614 | 0 | default: |
615 | 0 | err = UV_EINVAL; |
616 | 0 | } |
617 | | |
618 | 0 | if (err == 0) |
619 | 0 | uv__handle_start(stream); |
620 | |
|
621 | 0 | return err; |
622 | 0 | } |
623 | | |
624 | | |
625 | 0 | static void uv__drain(uv_stream_t* stream) { |
626 | 0 | uv_shutdown_t* req; |
627 | 0 | int err; |
628 | |
|
629 | 0 | assert(uv__queue_empty(&stream->write_queue)); |
630 | 0 | if (!(stream->flags & UV_HANDLE_CLOSING)) { |
631 | 0 | uv__io_stop(stream->loop, &stream->io_watcher, POLLOUT); |
632 | 0 | uv__stream_osx_interrupt_select(stream); |
633 | 0 | } |
634 | |
|
635 | 0 | if (!uv__is_stream_shutting(stream)) |
636 | 0 | return; |
637 | | |
638 | 0 | req = stream->shutdown_req; |
639 | 0 | assert(req); |
640 | |
|
641 | 0 | if ((stream->flags & UV_HANDLE_CLOSING) || |
642 | 0 | !(stream->flags & UV_HANDLE_SHUT)) { |
643 | 0 | stream->shutdown_req = NULL; |
644 | 0 | uv__req_unregister(stream->loop); |
645 | |
|
646 | 0 | err = 0; |
647 | 0 | if (stream->flags & UV_HANDLE_CLOSING) |
648 | | /* The user destroyed the stream before we got to do the shutdown. */ |
649 | 0 | err = UV_ECANCELED; |
650 | 0 | else if (shutdown(uv__stream_fd(stream), SHUT_WR)) |
651 | 0 | err = UV__ERR(errno); |
652 | 0 | else /* Success. */ |
653 | 0 | stream->flags |= UV_HANDLE_SHUT; |
654 | |
|
655 | 0 | if (req->cb != NULL) |
656 | 0 | req->cb(req, err); |
657 | 0 | } |
658 | 0 | } |
659 | | |
660 | | |
661 | 0 | static ssize_t uv__writev(int fd, struct iovec* vec, size_t n) { |
662 | 0 | if (n == 1) |
663 | 0 | return write(fd, vec->iov_base, vec->iov_len); |
664 | 0 | else |
665 | 0 | return writev(fd, vec, n); |
666 | 0 | } |
667 | | |
668 | | |
669 | 0 | static size_t uv__write_req_size(uv_write_t* req) { |
670 | 0 | size_t size; |
671 | |
|
672 | 0 | assert(req->bufs != NULL); |
673 | 0 | size = uv__count_bufs(req->bufs + req->write_index, |
674 | 0 | req->nbufs - req->write_index); |
675 | 0 | assert(req->handle->write_queue_size >= size); |
676 | |
|
677 | 0 | return size; |
678 | 0 | } |
679 | | |
680 | | |
681 | | /* Returns 1 if all write request data has been written, or 0 if there is still |
682 | | * more data to write. |
683 | | * |
684 | | * Note: the return value only says something about the *current* request. |
685 | | * There may still be other write requests sitting in the queue. |
686 | | */ |
687 | | static int uv__write_req_update(uv_stream_t* stream, |
688 | | uv_write_t* req, |
689 | 0 | size_t n) { |
690 | 0 | uv_buf_t* buf; |
691 | 0 | size_t len; |
692 | |
|
693 | 0 | assert(n <= stream->write_queue_size); |
694 | 0 | stream->write_queue_size -= n; |
695 | |
|
696 | 0 | buf = req->bufs + req->write_index; |
697 | |
|
698 | 0 | do { |
699 | 0 | len = n < buf->len ? n : buf->len; |
700 | 0 | if (buf->len != 0) |
701 | 0 | buf->base += len; |
702 | 0 | buf->len -= len; |
703 | 0 | buf += (buf->len == 0); /* Advance to next buffer if this one is empty. */ |
704 | 0 | n -= len; |
705 | 0 | } while (n > 0); |
706 | |
|
707 | 0 | req->write_index = buf - req->bufs; |
708 | |
|
709 | 0 | return req->write_index == req->nbufs; |
710 | 0 | } |
711 | | |
712 | | |
713 | 0 | static void uv__write_req_finish(uv_write_t* req) { |
714 | 0 | uv_stream_t* stream = req->handle; |
715 | | |
716 | | /* Pop the req off tcp->write_queue. */ |
717 | 0 | uv__queue_remove(&req->queue); |
718 | | |
719 | | /* Only free when there was no error. On error, we touch up write_queue_size |
720 | | * right before making the callback. The reason we don't do that right away |
721 | | * is that a write_queue_size > 0 is our only way to signal to the user that |
722 | | * they should stop writing - which they should if we got an error. Something |
723 | | * to revisit in future revisions of the libuv API. |
724 | | */ |
725 | 0 | if (req->error == 0) { |
726 | 0 | if (req->bufs != req->bufsml) |
727 | 0 | uv__free(req->bufs); |
728 | 0 | req->bufs = NULL; |
729 | 0 | } |
730 | | |
731 | | /* Add it to the write_completed_queue where it will have its |
732 | | * callback called in the near future. |
733 | | */ |
734 | 0 | uv__queue_insert_tail(&stream->write_completed_queue, &req->queue); |
735 | 0 | uv__io_feed(stream->loop, &stream->io_watcher); |
736 | 0 | } |
737 | | |
738 | | |
739 | 0 | static int uv__handle_fd(uv_handle_t* handle) { |
740 | 0 | switch (handle->type) { |
741 | 0 | case UV_NAMED_PIPE: |
742 | 0 | case UV_TCP: |
743 | 0 | return ((uv_stream_t*) handle)->io_watcher.fd; |
744 | | |
745 | 0 | case UV_UDP: |
746 | 0 | return ((uv_udp_t*) handle)->io_watcher.fd; |
747 | | |
748 | 0 | default: |
749 | 0 | return -1; |
750 | 0 | } |
751 | 0 | } |
752 | | |
753 | | static int uv__try_write(uv_stream_t* stream, |
754 | | const uv_buf_t bufs[], |
755 | | unsigned int nbufs, |
756 | 0 | uv_stream_t* send_handle) { |
757 | 0 | struct iovec* iov; |
758 | 0 | int iovmax; |
759 | 0 | int iovcnt; |
760 | 0 | ssize_t n; |
761 | | |
762 | | /* |
763 | | * Cast to iovec. We had to have our own uv_buf_t instead of iovec |
764 | | * because Windows's WSABUF is not an iovec. |
765 | | */ |
766 | 0 | iov = (struct iovec*) bufs; |
767 | 0 | iovcnt = nbufs; |
768 | |
|
769 | 0 | iovmax = uv__getiovmax(); |
770 | | |
771 | | /* Limit iov count to avoid EINVALs from writev() */ |
772 | 0 | if (iovcnt > iovmax) |
773 | 0 | iovcnt = iovmax; |
774 | | |
775 | | /* |
776 | | * Now do the actual writev. Note that we've been updating the pointers |
777 | | * inside the iov each time we write. So there is no need to offset it. |
778 | | */ |
779 | 0 | if (send_handle != NULL) { |
780 | 0 | int fd_to_send; |
781 | 0 | struct msghdr msg; |
782 | 0 | union uv__cmsg cmsg; |
783 | |
|
784 | 0 | if (uv__is_closing(send_handle)) |
785 | 0 | return UV_EBADF; |
786 | | |
787 | 0 | fd_to_send = uv__handle_fd((uv_handle_t*) send_handle); |
788 | |
|
789 | 0 | memset(&cmsg, 0, sizeof(cmsg)); |
790 | |
|
791 | 0 | assert(fd_to_send >= 0); |
792 | |
|
793 | 0 | msg.msg_name = NULL; |
794 | 0 | msg.msg_namelen = 0; |
795 | 0 | msg.msg_iov = iov; |
796 | 0 | msg.msg_iovlen = iovcnt; |
797 | 0 | msg.msg_flags = 0; |
798 | |
|
799 | 0 | msg.msg_control = &cmsg.hdr; |
800 | 0 | msg.msg_controllen = CMSG_SPACE(sizeof(fd_to_send)); |
801 | |
|
802 | 0 | cmsg.hdr.cmsg_level = SOL_SOCKET; |
803 | 0 | cmsg.hdr.cmsg_type = SCM_RIGHTS; |
804 | 0 | cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(fd_to_send)); |
805 | 0 | memcpy(CMSG_DATA(&cmsg.hdr), &fd_to_send, sizeof(fd_to_send)); |
806 | |
|
807 | 0 | do |
808 | 0 | n = sendmsg(uv__stream_fd(stream), &msg, 0); |
809 | 0 | while (n == -1 && errno == EINTR); |
810 | 0 | } else { |
811 | 0 | do |
812 | 0 | n = uv__writev(uv__stream_fd(stream), iov, iovcnt); |
813 | 0 | while (n == -1 && errno == EINTR); |
814 | 0 | } |
815 | | |
816 | 0 | if (n >= 0) |
817 | 0 | return n; |
818 | | |
819 | 0 | if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS) |
820 | 0 | return UV_EAGAIN; |
821 | | |
822 | | #ifdef __APPLE__ |
823 | | /* macOS versions 10.10 and 10.15 - and presumbaly 10.11 to 10.14, too - |
824 | | * have a bug where a race condition causes the kernel to return EPROTOTYPE |
825 | | * because the socket isn't fully constructed. It's probably the result of |
826 | | * the peer closing the connection and that is why libuv translates it to |
827 | | * ECONNRESET. Previously, libuv retried until the EPROTOTYPE error went |
828 | | * away but some VPN software causes the same behavior except the error is |
829 | | * permanent, not transient, turning the retry mechanism into an infinite |
830 | | * loop. See https://github.com/libuv/libuv/pull/482. |
831 | | */ |
832 | | if (errno == EPROTOTYPE) |
833 | | return UV_ECONNRESET; |
834 | | #endif /* __APPLE__ */ |
835 | | |
836 | 0 | return UV__ERR(errno); |
837 | 0 | } |
838 | | |
839 | 0 | static void uv__write(uv_stream_t* stream) { |
840 | 0 | struct uv__queue* q; |
841 | 0 | uv_write_t* req; |
842 | 0 | ssize_t n; |
843 | 0 | int count; |
844 | |
|
845 | 0 | assert(uv__stream_fd(stream) >= 0); |
846 | | |
847 | | /* Prevent loop starvation when the consumer of this stream read as fast as |
848 | | * (or faster than) we can write it. This `count` mechanism does not need to |
849 | | * change even if we switch to edge-triggered I/O. |
850 | | */ |
851 | 0 | count = 32; |
852 | |
|
853 | 0 | for (;;) { |
854 | 0 | if (uv__queue_empty(&stream->write_queue)) |
855 | 0 | return; |
856 | | |
857 | 0 | q = uv__queue_head(&stream->write_queue); |
858 | 0 | req = uv__queue_data(q, uv_write_t, queue); |
859 | 0 | assert(req->handle == stream); |
860 | |
|
861 | 0 | n = uv__try_write(stream, |
862 | 0 | &(req->bufs[req->write_index]), |
863 | 0 | req->nbufs - req->write_index, |
864 | 0 | req->send_handle); |
865 | | |
866 | | /* Ensure the handle isn't sent again in case this is a partial write. */ |
867 | 0 | if (n >= 0) { |
868 | 0 | req->send_handle = NULL; |
869 | 0 | if (uv__write_req_update(stream, req, n)) { |
870 | 0 | uv__write_req_finish(req); |
871 | 0 | if (count-- > 0) |
872 | 0 | continue; /* Start trying to write the next request. */ |
873 | | |
874 | 0 | return; |
875 | 0 | } |
876 | 0 | } else if (n != UV_EAGAIN) |
877 | 0 | goto error; |
878 | | |
879 | | /* If this is a blocking stream, try again. */ |
880 | 0 | if (stream->flags & UV_HANDLE_BLOCKING_WRITES) |
881 | 0 | continue; |
882 | | |
883 | | /* We're not done. */ |
884 | 0 | uv__io_start(stream->loop, &stream->io_watcher, POLLOUT); |
885 | | |
886 | | /* Notify select() thread about state change */ |
887 | 0 | uv__stream_osx_interrupt_select(stream); |
888 | |
|
889 | 0 | return; |
890 | 0 | } |
891 | | |
892 | 0 | error: |
893 | 0 | req->error = n; |
894 | 0 | uv__write_req_finish(req); |
895 | 0 | uv__io_stop(stream->loop, &stream->io_watcher, POLLOUT); |
896 | 0 | uv__stream_osx_interrupt_select(stream); |
897 | 0 | } |
898 | | |
899 | | |
900 | 0 | static void uv__write_callbacks(uv_stream_t* stream) { |
901 | 0 | uv_write_t* req; |
902 | 0 | struct uv__queue* q; |
903 | 0 | struct uv__queue pq; |
904 | |
|
905 | 0 | if (uv__queue_empty(&stream->write_completed_queue)) |
906 | 0 | return; |
907 | | |
908 | 0 | uv__queue_move(&stream->write_completed_queue, &pq); |
909 | |
|
910 | 0 | while (!uv__queue_empty(&pq)) { |
911 | | /* Pop a req off write_completed_queue. */ |
912 | 0 | q = uv__queue_head(&pq); |
913 | 0 | req = uv__queue_data(q, uv_write_t, queue); |
914 | 0 | uv__queue_remove(q); |
915 | 0 | uv__req_unregister(stream->loop); |
916 | |
|
917 | 0 | if (req->bufs != NULL) { |
918 | 0 | stream->write_queue_size -= uv__write_req_size(req); |
919 | 0 | if (req->bufs != req->bufsml) |
920 | 0 | uv__free(req->bufs); |
921 | 0 | req->bufs = NULL; |
922 | 0 | } |
923 | | |
924 | | /* NOTE: call callback AFTER freeing the request data. */ |
925 | 0 | if (req->cb) |
926 | 0 | req->cb(req, req->error); |
927 | 0 | } |
928 | 0 | } |
929 | | |
930 | | |
931 | 0 | static void uv__stream_eof(uv_stream_t* stream, const uv_buf_t* buf) { |
932 | 0 | stream->flags |= UV_HANDLE_READ_EOF; |
933 | 0 | stream->flags &= ~UV_HANDLE_READING; |
934 | 0 | uv__io_stop(stream->loop, &stream->io_watcher, POLLIN); |
935 | 0 | uv__handle_stop(stream); |
936 | 0 | uv__stream_osx_interrupt_select(stream); |
937 | 0 | stream->read_cb(stream, UV_EOF, buf); |
938 | 0 | } |
939 | | |
940 | | |
941 | 0 | static int uv__stream_queue_fd(uv_stream_t* stream, int fd) { |
942 | 0 | uv__stream_queued_fds_t* queued_fds; |
943 | 0 | unsigned int queue_size; |
944 | |
|
945 | 0 | queued_fds = stream->queued_fds; |
946 | 0 | if (queued_fds == NULL) { |
947 | 0 | queue_size = 8; |
948 | 0 | queued_fds = uv__malloc((queue_size - 1) * sizeof(*queued_fds->fds) + |
949 | 0 | sizeof(*queued_fds)); |
950 | 0 | if (queued_fds == NULL) |
951 | 0 | return UV_ENOMEM; |
952 | 0 | queued_fds->size = queue_size; |
953 | 0 | queued_fds->offset = 0; |
954 | 0 | stream->queued_fds = queued_fds; |
955 | | |
956 | | /* Grow */ |
957 | 0 | } else if (queued_fds->size == queued_fds->offset) { |
958 | 0 | queue_size = queued_fds->size + 8; |
959 | 0 | queued_fds = uv__realloc(queued_fds, |
960 | 0 | (queue_size - 1) * sizeof(*queued_fds->fds) + |
961 | 0 | sizeof(*queued_fds)); |
962 | | |
963 | | /* |
964 | | * Allocation failure, report back. |
965 | | * NOTE: if it is fatal - sockets will be closed in uv__stream_close |
966 | | */ |
967 | 0 | if (queued_fds == NULL) |
968 | 0 | return UV_ENOMEM; |
969 | 0 | queued_fds->size = queue_size; |
970 | 0 | stream->queued_fds = queued_fds; |
971 | 0 | } |
972 | | |
973 | | /* Put fd in a queue */ |
974 | 0 | queued_fds->fds[queued_fds->offset++] = fd; |
975 | |
|
976 | 0 | return 0; |
977 | 0 | } |
978 | | |
979 | | |
980 | 0 | static int uv__stream_recv_cmsg(uv_stream_t* stream, struct msghdr* msg) { |
981 | 0 | struct cmsghdr* cmsg; |
982 | 0 | char* p; |
983 | 0 | char* pe; |
984 | 0 | int fd; |
985 | 0 | int err; |
986 | 0 | size_t count; |
987 | |
|
988 | 0 | err = 0; |
989 | 0 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
990 | 0 | if (cmsg->cmsg_type != SCM_RIGHTS) { |
991 | 0 | fprintf(stderr, "ignoring non-SCM_RIGHTS ancillary data: %d\n", |
992 | 0 | cmsg->cmsg_type); |
993 | 0 | continue; |
994 | 0 | } |
995 | | |
996 | 0 | assert(cmsg->cmsg_len >= CMSG_LEN(0)); |
997 | 0 | count = cmsg->cmsg_len - CMSG_LEN(0); |
998 | 0 | assert(count % sizeof(fd) == 0); |
999 | 0 | count /= sizeof(fd); |
1000 | |
|
1001 | 0 | p = (void*) CMSG_DATA(cmsg); |
1002 | 0 | pe = p + count * sizeof(fd); |
1003 | |
|
1004 | 0 | while (p < pe) { |
1005 | 0 | memcpy(&fd, p, sizeof(fd)); |
1006 | 0 | p += sizeof(fd); |
1007 | |
|
1008 | 0 | if (err == 0) { |
1009 | 0 | if (stream->accepted_fd == -1) |
1010 | 0 | stream->accepted_fd = fd; |
1011 | 0 | else |
1012 | 0 | err = uv__stream_queue_fd(stream, fd); |
1013 | 0 | } |
1014 | |
|
1015 | 0 | if (err != 0) |
1016 | 0 | uv__close(fd); |
1017 | 0 | } |
1018 | 0 | } |
1019 | |
|
1020 | 0 | return err; |
1021 | 0 | } |
1022 | | |
1023 | | |
1024 | 0 | static void uv__read(uv_stream_t* stream) { |
1025 | 0 | uv_buf_t buf; |
1026 | 0 | ssize_t nread; |
1027 | 0 | struct msghdr msg; |
1028 | 0 | union uv__cmsg cmsg; |
1029 | 0 | int count; |
1030 | 0 | int err; |
1031 | 0 | int is_ipc; |
1032 | | |
1033 | | /* Prevent loop starvation when the data comes in as fast as (or faster than) |
1034 | | * we can read it. XXX Need to rearm fd if we switch to edge-triggered I/O. |
1035 | | */ |
1036 | 0 | count = 32; |
1037 | |
|
1038 | 0 | is_ipc = stream->type == UV_NAMED_PIPE && ((uv_pipe_t*) stream)->ipc; |
1039 | | |
1040 | | /* XXX: Maybe instead of having UV_HANDLE_READING we just test if |
1041 | | * tcp->read_cb is NULL or not? |
1042 | | */ |
1043 | 0 | while (stream->read_cb |
1044 | 0 | && (stream->flags & UV_HANDLE_READING) |
1045 | 0 | && (count-- > 0)) { |
1046 | 0 | assert(stream->alloc_cb != NULL); |
1047 | |
|
1048 | 0 | buf = uv_buf_init(NULL, 0); |
1049 | 0 | stream->alloc_cb((uv_handle_t*)stream, 64 * 1024, &buf); |
1050 | 0 | if (buf.base == NULL || buf.len == 0) { |
1051 | | /* User indicates it can't or won't handle the read. */ |
1052 | 0 | stream->read_cb(stream, UV_ENOBUFS, &buf); |
1053 | 0 | return; |
1054 | 0 | } |
1055 | | |
1056 | 0 | assert(buf.base != NULL); |
1057 | 0 | assert(uv__stream_fd(stream) >= 0); |
1058 | |
|
1059 | 0 | if (!is_ipc) { |
1060 | 0 | do { |
1061 | 0 | nread = read(uv__stream_fd(stream), buf.base, buf.len); |
1062 | 0 | } |
1063 | 0 | while (nread < 0 && errno == EINTR); |
1064 | 0 | } else { |
1065 | | /* ipc uses recvmsg */ |
1066 | 0 | msg.msg_flags = 0; |
1067 | 0 | msg.msg_iov = (struct iovec*) &buf; |
1068 | 0 | msg.msg_iovlen = 1; |
1069 | 0 | msg.msg_name = NULL; |
1070 | 0 | msg.msg_namelen = 0; |
1071 | | /* Set up to receive a descriptor even if one isn't in the message */ |
1072 | 0 | msg.msg_controllen = sizeof(cmsg); |
1073 | 0 | msg.msg_control = &cmsg.hdr; |
1074 | |
|
1075 | 0 | do { |
1076 | 0 | nread = uv__recvmsg(uv__stream_fd(stream), &msg, 0); |
1077 | 0 | } |
1078 | 0 | while (nread < 0 && errno == EINTR); |
1079 | 0 | } |
1080 | |
|
1081 | 0 | if (nread < 0) { |
1082 | | /* Error */ |
1083 | 0 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
1084 | | /* Wait for the next one. */ |
1085 | 0 | if (stream->flags & UV_HANDLE_READING) { |
1086 | 0 | uv__io_start(stream->loop, &stream->io_watcher, POLLIN); |
1087 | 0 | uv__stream_osx_interrupt_select(stream); |
1088 | 0 | } |
1089 | 0 | stream->read_cb(stream, 0, &buf); |
1090 | | #if defined(__CYGWIN__) || defined(__MSYS__) |
1091 | | } else if (errno == ECONNRESET && stream->type == UV_NAMED_PIPE) { |
1092 | | uv__stream_eof(stream, &buf); |
1093 | | return; |
1094 | | #endif |
1095 | 0 | } else { |
1096 | | /* Error. User should call uv_close(). */ |
1097 | 0 | stream->flags &= ~(UV_HANDLE_READABLE | UV_HANDLE_WRITABLE); |
1098 | 0 | stream->read_cb(stream, UV__ERR(errno), &buf); |
1099 | 0 | if (stream->flags & UV_HANDLE_READING) { |
1100 | 0 | stream->flags &= ~UV_HANDLE_READING; |
1101 | 0 | uv__io_stop(stream->loop, &stream->io_watcher, POLLIN); |
1102 | 0 | uv__handle_stop(stream); |
1103 | 0 | uv__stream_osx_interrupt_select(stream); |
1104 | 0 | } |
1105 | 0 | } |
1106 | 0 | return; |
1107 | 0 | } else if (nread == 0) { |
1108 | 0 | uv__stream_eof(stream, &buf); |
1109 | 0 | return; |
1110 | 0 | } else { |
1111 | | /* Successful read */ |
1112 | 0 | ssize_t buflen = buf.len; |
1113 | |
|
1114 | 0 | if (is_ipc) { |
1115 | 0 | err = uv__stream_recv_cmsg(stream, &msg); |
1116 | 0 | if (err != 0) { |
1117 | 0 | stream->read_cb(stream, err, &buf); |
1118 | 0 | return; |
1119 | 0 | } |
1120 | 0 | } |
1121 | | |
1122 | | #if defined(__MVS__) |
1123 | | if (is_ipc && msg.msg_controllen > 0) { |
1124 | | uv_buf_t blankbuf; |
1125 | | int nread; |
1126 | | struct iovec *old; |
1127 | | |
1128 | | blankbuf.base = 0; |
1129 | | blankbuf.len = 0; |
1130 | | old = msg.msg_iov; |
1131 | | msg.msg_iov = (struct iovec*) &blankbuf; |
1132 | | nread = 0; |
1133 | | do { |
1134 | | nread = uv__recvmsg(uv__stream_fd(stream), &msg, 0); |
1135 | | err = uv__stream_recv_cmsg(stream, &msg); |
1136 | | if (err != 0) { |
1137 | | stream->read_cb(stream, err, &buf); |
1138 | | msg.msg_iov = old; |
1139 | | return; |
1140 | | } |
1141 | | } while (nread == 0 && msg.msg_controllen > 0); |
1142 | | msg.msg_iov = old; |
1143 | | } |
1144 | | #endif |
1145 | 0 | stream->read_cb(stream, nread, &buf); |
1146 | | |
1147 | | /* Save a system call and return if we didn't fill the buffer |
1148 | | * completely, on the assumption the next read() will fail with EOF. |
1149 | | * |
1150 | | * Devices like PTYs sometimes operate in a packet-like mode where |
1151 | | * they don't return all available data in a single read but we'll |
1152 | | * catch it on the next read because of level-triggered I/O. |
1153 | | */ |
1154 | 0 | if (nread < buflen) |
1155 | 0 | return; |
1156 | 0 | } |
1157 | 0 | } |
1158 | 0 | } |
1159 | | |
1160 | | |
1161 | 0 | int uv_shutdown(uv_shutdown_t* req, uv_stream_t* stream, uv_shutdown_cb cb) { |
1162 | 0 | assert(stream->type == UV_TCP || |
1163 | 0 | stream->type == UV_TTY || |
1164 | 0 | stream->type == UV_NAMED_PIPE); |
1165 | |
|
1166 | 0 | if (!(stream->flags & UV_HANDLE_WRITABLE) || |
1167 | 0 | stream->flags & UV_HANDLE_SHUT || |
1168 | 0 | uv__is_stream_shutting(stream) || |
1169 | 0 | uv__is_closing(stream)) { |
1170 | 0 | return UV_ENOTCONN; |
1171 | 0 | } |
1172 | | |
1173 | 0 | assert(uv__stream_fd(stream) >= 0); |
1174 | | |
1175 | | /* Initialize request. The `shutdown(2)` call will always be deferred until |
1176 | | * `uv__drain`, just before the callback is run. */ |
1177 | 0 | uv__req_init(stream->loop, req, UV_SHUTDOWN); |
1178 | 0 | req->handle = stream; |
1179 | 0 | req->cb = cb; |
1180 | 0 | stream->shutdown_req = req; |
1181 | 0 | stream->flags &= ~UV_HANDLE_WRITABLE; |
1182 | |
|
1183 | 0 | if (uv__queue_empty(&stream->write_queue)) |
1184 | 0 | uv__io_feed(stream->loop, &stream->io_watcher); |
1185 | |
|
1186 | 0 | return 0; |
1187 | 0 | } |
1188 | | |
1189 | | |
1190 | 0 | void uv__stream_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) { |
1191 | 0 | uv_stream_t* stream; |
1192 | |
|
1193 | 0 | stream = container_of(w, uv_stream_t, io_watcher); |
1194 | |
|
1195 | 0 | assert(stream->type == UV_TCP || |
1196 | 0 | stream->type == UV_NAMED_PIPE || |
1197 | 0 | stream->type == UV_TTY); |
1198 | 0 | assert(!(stream->flags & UV_HANDLE_CLOSING)); |
1199 | |
|
1200 | 0 | if (stream->connect_req) { |
1201 | 0 | uv__stream_connect(stream); |
1202 | 0 | return; |
1203 | 0 | } |
1204 | | |
1205 | 0 | assert(uv__stream_fd(stream) >= 0); |
1206 | |
|
1207 | 0 | if (events & (POLLIN | POLLERR)) |
1208 | 0 | uv__read(stream); |
1209 | |
|
1210 | 0 | if (uv__stream_fd(stream) == -1) |
1211 | 0 | return; /* read_cb closed stream. */ |
1212 | | |
1213 | | /* Short-circuit iff POLLHUP is set, the user is still interested in read |
1214 | | * events and uv__read() didn't see EOF. If the EOF flag is set, uv__read() |
1215 | | * called read_cb with err=UV_EOF and we don't have to do anything. |
1216 | | * |
1217 | | * POLLIN should not be set because, at least on Linux and possibly other |
1218 | | * operating systems, devices like PTYs sometimes produce partial reads even |
1219 | | * when more data is available. |
1220 | | */ |
1221 | 0 | if ((events & POLLHUP) && |
1222 | 0 | !(events & POLLIN) && |
1223 | 0 | (stream->flags & UV_HANDLE_READING) && |
1224 | 0 | !(stream->flags & UV_HANDLE_READ_EOF)) { |
1225 | 0 | uv_buf_t buf = { NULL, 0 }; |
1226 | 0 | uv__stream_eof(stream, &buf); |
1227 | 0 | } |
1228 | |
|
1229 | 0 | if (uv__stream_fd(stream) == -1) |
1230 | 0 | return; /* read_cb closed stream. */ |
1231 | | |
1232 | 0 | if (events & (POLLOUT | POLLERR | POLLHUP)) { |
1233 | 0 | uv__write(stream); |
1234 | 0 | uv__write_callbacks(stream); |
1235 | | |
1236 | | /* Write queue drained. */ |
1237 | 0 | if (uv__queue_empty(&stream->write_queue)) |
1238 | 0 | uv__drain(stream); |
1239 | 0 | } |
1240 | 0 | } |
1241 | | |
1242 | | |
1243 | | /** |
1244 | | * We get called here from directly following a call to connect(2). |
1245 | | * In order to determine if we've errored out or succeeded must call |
1246 | | * getsockopt. |
1247 | | */ |
1248 | 0 | static void uv__stream_connect(uv_stream_t* stream) { |
1249 | 0 | int error; |
1250 | 0 | uv_connect_t* req = stream->connect_req; |
1251 | 0 | socklen_t errorsize = sizeof(int); |
1252 | |
|
1253 | 0 | assert(stream->type == UV_TCP || stream->type == UV_NAMED_PIPE); |
1254 | 0 | assert(req); |
1255 | |
|
1256 | 0 | if (stream->delayed_error) { |
1257 | | /* To smooth over the differences between unixes errors that |
1258 | | * were reported synchronously on the first connect can be delayed |
1259 | | * until the next tick--which is now. |
1260 | | */ |
1261 | 0 | error = stream->delayed_error; |
1262 | 0 | stream->delayed_error = 0; |
1263 | 0 | } else { |
1264 | | /* Normal situation: we need to get the socket error from the kernel. */ |
1265 | 0 | assert(uv__stream_fd(stream) >= 0); |
1266 | 0 | getsockopt(uv__stream_fd(stream), |
1267 | 0 | SOL_SOCKET, |
1268 | 0 | SO_ERROR, |
1269 | 0 | &error, |
1270 | 0 | &errorsize); |
1271 | 0 | error = UV__ERR(error); |
1272 | 0 | } |
1273 | |
|
1274 | 0 | if (error == UV__ERR(EINPROGRESS)) |
1275 | 0 | return; |
1276 | | |
1277 | 0 | stream->connect_req = NULL; |
1278 | 0 | uv__req_unregister(stream->loop); |
1279 | |
|
1280 | 0 | if (error < 0 || uv__queue_empty(&stream->write_queue)) { |
1281 | 0 | uv__io_stop(stream->loop, &stream->io_watcher, POLLOUT); |
1282 | 0 | } |
1283 | |
|
1284 | 0 | if (req->cb) |
1285 | 0 | req->cb(req, error); |
1286 | |
|
1287 | 0 | if (uv__stream_fd(stream) == -1) |
1288 | 0 | return; |
1289 | | |
1290 | 0 | if (error < 0) { |
1291 | 0 | uv__stream_flush_write_queue(stream, UV_ECANCELED); |
1292 | 0 | uv__write_callbacks(stream); |
1293 | 0 | } |
1294 | 0 | } |
1295 | | |
1296 | | |
1297 | | static int uv__check_before_write(uv_stream_t* stream, |
1298 | | unsigned int nbufs, |
1299 | 0 | uv_stream_t* send_handle) { |
1300 | 0 | assert((stream->type == UV_TCP || |
1301 | 0 | stream->type == UV_NAMED_PIPE || |
1302 | 0 | stream->type == UV_TTY) && |
1303 | 0 | "uv_write (unix) does not yet support other types of streams"); |
1304 | | |
1305 | | /* We're not beholden to IOV_MAX but limit the buffer count to catch sign |
1306 | | * conversion bugs where a caller passes in a signed negative number that |
1307 | | * then gets converted to a really large unsigned number. |
1308 | | */ |
1309 | 0 | if (nbufs < 1 || nbufs > 1024*1024) |
1310 | 0 | return UV_EINVAL; |
1311 | | |
1312 | 0 | if (uv__stream_fd(stream) < 0) |
1313 | 0 | return UV_EBADF; |
1314 | | |
1315 | 0 | if (!(stream->flags & UV_HANDLE_WRITABLE)) |
1316 | 0 | return UV_EPIPE; |
1317 | | |
1318 | 0 | if (send_handle != NULL) { |
1319 | 0 | if (stream->type != UV_NAMED_PIPE || !((uv_pipe_t*)stream)->ipc) |
1320 | 0 | return UV_EINVAL; |
1321 | | |
1322 | | /* XXX We abuse uv_write2() to send over UDP handles to child processes. |
1323 | | * Don't call uv__stream_fd() on those handles, it's a macro that on OS X |
1324 | | * evaluates to a function that operates on a uv_stream_t with a couple of |
1325 | | * OS X specific fields. On other Unices it does (handle)->io_watcher.fd, |
1326 | | * which works but only by accident. |
1327 | | */ |
1328 | 0 | if (uv__handle_fd((uv_handle_t*) send_handle) < 0) |
1329 | 0 | return UV_EBADF; |
1330 | |
|
1331 | | #if defined(__CYGWIN__) || defined(__MSYS__) |
1332 | | /* Cygwin recvmsg always sets msg_controllen to zero, so we cannot send it. |
1333 | | See https://github.com/mirror/newlib-cygwin/blob/86fc4bf0/winsup/cygwin/fhandler_socket.cc#L1736-L1743 */ |
1334 | | return UV_ENOSYS; |
1335 | | #endif |
1336 | 0 | } |
1337 | | |
1338 | 0 | return 0; |
1339 | 0 | } |
1340 | | |
1341 | | int uv_write2(uv_write_t* req, |
1342 | | uv_stream_t* stream, |
1343 | | const uv_buf_t bufs[], |
1344 | | unsigned int nbufs, |
1345 | | uv_stream_t* send_handle, |
1346 | 0 | uv_write_cb cb) { |
1347 | 0 | int empty_queue; |
1348 | 0 | int err; |
1349 | |
|
1350 | 0 | err = uv__check_before_write(stream, nbufs, send_handle); |
1351 | 0 | if (err < 0) |
1352 | 0 | return err; |
1353 | | |
1354 | | /* It's legal for write_queue_size > 0 even when the write_queue is empty; |
1355 | | * it means there are error-state requests in the write_completed_queue that |
1356 | | * will touch up write_queue_size later, see also uv__write_req_finish(). |
1357 | | * We could check that write_queue is empty instead but that implies making |
1358 | | * a write() syscall when we know that the handle is in error mode. |
1359 | | */ |
1360 | 0 | empty_queue = (stream->write_queue_size == 0); |
1361 | | |
1362 | | /* Initialize the req */ |
1363 | 0 | uv__req_init(stream->loop, req, UV_WRITE); |
1364 | 0 | req->cb = cb; |
1365 | 0 | req->handle = stream; |
1366 | 0 | req->error = 0; |
1367 | 0 | req->send_handle = send_handle; |
1368 | 0 | uv__queue_init(&req->queue); |
1369 | |
|
1370 | 0 | req->bufs = req->bufsml; |
1371 | 0 | if (nbufs > ARRAY_SIZE(req->bufsml)) |
1372 | 0 | req->bufs = uv__malloc(nbufs * sizeof(bufs[0])); |
1373 | |
|
1374 | 0 | if (req->bufs == NULL) |
1375 | 0 | return UV_ENOMEM; |
1376 | | |
1377 | 0 | memcpy(req->bufs, bufs, nbufs * sizeof(bufs[0])); |
1378 | 0 | req->nbufs = nbufs; |
1379 | 0 | req->write_index = 0; |
1380 | 0 | stream->write_queue_size += uv__count_bufs(bufs, nbufs); |
1381 | | |
1382 | | /* Append the request to write_queue. */ |
1383 | 0 | uv__queue_insert_tail(&stream->write_queue, &req->queue); |
1384 | | |
1385 | | /* If the queue was empty when this function began, we should attempt to |
1386 | | * do the write immediately. Otherwise start the write_watcher and wait |
1387 | | * for the fd to become writable. |
1388 | | */ |
1389 | 0 | if (stream->connect_req) { |
1390 | | /* Still connecting, do nothing. */ |
1391 | 0 | } |
1392 | 0 | else if (empty_queue) { |
1393 | 0 | uv__write(stream); |
1394 | 0 | } |
1395 | 0 | else { |
1396 | | /* |
1397 | | * blocking streams should never have anything in the queue. |
1398 | | * if this assert fires then somehow the blocking stream isn't being |
1399 | | * sufficiently flushed in uv__write. |
1400 | | */ |
1401 | 0 | assert(!(stream->flags & UV_HANDLE_BLOCKING_WRITES)); |
1402 | 0 | uv__io_start(stream->loop, &stream->io_watcher, POLLOUT); |
1403 | 0 | uv__stream_osx_interrupt_select(stream); |
1404 | 0 | } |
1405 | |
|
1406 | 0 | return 0; |
1407 | 0 | } |
1408 | | |
1409 | | |
1410 | | /* The buffers to be written must remain valid until the callback is called. |
1411 | | * This is not required for the uv_buf_t array. |
1412 | | */ |
1413 | | int uv_write(uv_write_t* req, |
1414 | | uv_stream_t* handle, |
1415 | | const uv_buf_t bufs[], |
1416 | | unsigned int nbufs, |
1417 | 0 | uv_write_cb cb) { |
1418 | 0 | return uv_write2(req, handle, bufs, nbufs, NULL, cb); |
1419 | 0 | } |
1420 | | |
1421 | | |
1422 | | int uv_try_write(uv_stream_t* stream, |
1423 | | const uv_buf_t bufs[], |
1424 | 0 | unsigned int nbufs) { |
1425 | 0 | return uv_try_write2(stream, bufs, nbufs, NULL); |
1426 | 0 | } |
1427 | | |
1428 | | |
1429 | | int uv_try_write2(uv_stream_t* stream, |
1430 | | const uv_buf_t bufs[], |
1431 | | unsigned int nbufs, |
1432 | 0 | uv_stream_t* send_handle) { |
1433 | 0 | int err; |
1434 | | |
1435 | | /* Connecting or already writing some data */ |
1436 | 0 | if (stream->connect_req != NULL || stream->write_queue_size != 0) |
1437 | 0 | return UV_EAGAIN; |
1438 | | |
1439 | 0 | err = uv__check_before_write(stream, nbufs, NULL); |
1440 | 0 | if (err < 0) |
1441 | 0 | return err; |
1442 | | |
1443 | 0 | return uv__try_write(stream, bufs, nbufs, send_handle); |
1444 | 0 | } |
1445 | | |
1446 | | |
1447 | | int uv__read_start(uv_stream_t* stream, |
1448 | | uv_alloc_cb alloc_cb, |
1449 | 0 | uv_read_cb read_cb) { |
1450 | 0 | assert(stream->type == UV_TCP || stream->type == UV_NAMED_PIPE || |
1451 | 0 | stream->type == UV_TTY); |
1452 | | |
1453 | | /* The UV_HANDLE_READING flag is irrelevant of the state of the stream - it |
1454 | | * just expresses the desired state of the user. */ |
1455 | 0 | stream->flags |= UV_HANDLE_READING; |
1456 | 0 | stream->flags &= ~UV_HANDLE_READ_EOF; |
1457 | | |
1458 | | /* TODO: try to do the read inline? */ |
1459 | 0 | assert(uv__stream_fd(stream) >= 0); |
1460 | 0 | assert(alloc_cb); |
1461 | |
|
1462 | 0 | stream->read_cb = read_cb; |
1463 | 0 | stream->alloc_cb = alloc_cb; |
1464 | |
|
1465 | 0 | uv__io_start(stream->loop, &stream->io_watcher, POLLIN); |
1466 | 0 | uv__handle_start(stream); |
1467 | 0 | uv__stream_osx_interrupt_select(stream); |
1468 | |
|
1469 | 0 | return 0; |
1470 | 0 | } |
1471 | | |
1472 | | |
1473 | 0 | int uv_read_stop(uv_stream_t* stream) { |
1474 | 0 | if (!(stream->flags & UV_HANDLE_READING)) |
1475 | 0 | return 0; |
1476 | | |
1477 | 0 | stream->flags &= ~UV_HANDLE_READING; |
1478 | 0 | uv__io_stop(stream->loop, &stream->io_watcher, POLLIN); |
1479 | 0 | uv__handle_stop(stream); |
1480 | 0 | uv__stream_osx_interrupt_select(stream); |
1481 | |
|
1482 | 0 | stream->read_cb = NULL; |
1483 | 0 | stream->alloc_cb = NULL; |
1484 | 0 | return 0; |
1485 | 0 | } |
1486 | | |
1487 | | |
1488 | 0 | int uv_is_readable(const uv_stream_t* stream) { |
1489 | 0 | return !!(stream->flags & UV_HANDLE_READABLE); |
1490 | 0 | } |
1491 | | |
1492 | | |
1493 | 0 | int uv_is_writable(const uv_stream_t* stream) { |
1494 | 0 | return !!(stream->flags & UV_HANDLE_WRITABLE); |
1495 | 0 | } |
1496 | | |
1497 | | |
1498 | | #if defined(__APPLE__) && !defined(CMAKE_BOOTSTRAP) |
1499 | | int uv___stream_fd(const uv_stream_t* handle) { |
1500 | | const uv__stream_select_t* s; |
1501 | | |
1502 | | assert(handle->type == UV_TCP || |
1503 | | handle->type == UV_TTY || |
1504 | | handle->type == UV_NAMED_PIPE); |
1505 | | |
1506 | | s = handle->select; |
1507 | | if (s != NULL) |
1508 | | return s->fd; |
1509 | | |
1510 | | return handle->io_watcher.fd; |
1511 | | } |
1512 | | #endif /* defined(__APPLE__) */ |
1513 | | |
1514 | | |
1515 | 0 | void uv__stream_close(uv_stream_t* handle) { |
1516 | 0 | unsigned int i; |
1517 | 0 | uv__stream_queued_fds_t* queued_fds; |
1518 | |
|
1519 | | #if defined(__APPLE__) && !defined(CMAKE_BOOTSTRAP) |
1520 | | /* Terminate select loop first */ |
1521 | | if (handle->select != NULL) { |
1522 | | uv__stream_select_t* s; |
1523 | | |
1524 | | s = handle->select; |
1525 | | |
1526 | | uv_sem_post(&s->close_sem); |
1527 | | uv_sem_post(&s->async_sem); |
1528 | | uv__stream_osx_interrupt_select(handle); |
1529 | | uv_thread_join(&s->thread); |
1530 | | uv_sem_destroy(&s->close_sem); |
1531 | | uv_sem_destroy(&s->async_sem); |
1532 | | uv__close(s->fake_fd); |
1533 | | uv__close(s->int_fd); |
1534 | | uv_close((uv_handle_t*) &s->async, uv__stream_osx_cb_close); |
1535 | | |
1536 | | handle->select = NULL; |
1537 | | } |
1538 | | #endif /* defined(__APPLE__) */ |
1539 | |
|
1540 | 0 | uv__io_close(handle->loop, &handle->io_watcher); |
1541 | 0 | uv_read_stop(handle); |
1542 | 0 | uv__handle_stop(handle); |
1543 | 0 | handle->flags &= ~(UV_HANDLE_READABLE | UV_HANDLE_WRITABLE); |
1544 | |
|
1545 | 0 | if (handle->io_watcher.fd != -1) { |
1546 | | /* Don't close stdio file descriptors. Nothing good comes from it. */ |
1547 | 0 | if (handle->io_watcher.fd > STDERR_FILENO) |
1548 | 0 | uv__close(handle->io_watcher.fd); |
1549 | 0 | handle->io_watcher.fd = -1; |
1550 | 0 | } |
1551 | |
|
1552 | 0 | if (handle->accepted_fd != -1) { |
1553 | 0 | uv__close(handle->accepted_fd); |
1554 | 0 | handle->accepted_fd = -1; |
1555 | 0 | } |
1556 | | |
1557 | | /* Close all queued fds */ |
1558 | 0 | if (handle->queued_fds != NULL) { |
1559 | 0 | queued_fds = handle->queued_fds; |
1560 | 0 | for (i = 0; i < queued_fds->offset; i++) |
1561 | 0 | uv__close(queued_fds->fds[i]); |
1562 | 0 | uv__free(handle->queued_fds); |
1563 | 0 | handle->queued_fds = NULL; |
1564 | 0 | } |
1565 | |
|
1566 | 0 | assert(!uv__io_active(&handle->io_watcher, POLLIN | POLLOUT)); |
1567 | 0 | } |
1568 | | |
1569 | | |
1570 | 0 | int uv_stream_set_blocking(uv_stream_t* handle, int blocking) { |
1571 | | /* Don't need to check the file descriptor, uv__nonblock() |
1572 | | * will fail with EBADF if it's not valid. |
1573 | | */ |
1574 | 0 | return uv__nonblock(uv__stream_fd(handle), !blocking); |
1575 | 0 | } |