ext/libuv/src/unix/stream.c in libuv-1.2.0 vs ext/libuv/src/unix/stream.c in libuv-1.3.0
- old
+ new
@@ -86,11 +86,16 @@
QUEUE_INIT(&stream->write_queue);
QUEUE_INIT(&stream->write_completed_queue);
stream->write_queue_size = 0;
if (loop->emfile_fd == -1) {
- err = uv__open_cloexec("/", O_RDONLY);
+ err = uv__open_cloexec("/dev/null", O_RDONLY);
+ if (err < 0)
+ /* In the rare case that "/dev/null" isn't mounted open "/"
+ * instead.
+ */
+ err = uv__open_cloexec("/", O_RDONLY);
if (err >= 0)
loop->emfile_fd = err;
}
#if defined(__APPLE__)
@@ -242,11 +247,11 @@
static void uv__stream_osx_cb_close(uv_handle_t* async) {
uv__stream_select_t* s;
s = container_of(async, uv__stream_select_t, async);
- free(s);
+ uv__free(s);
}
int uv__stream_try_select(uv_stream_t* stream, int* fd) {
/*
@@ -299,14 +304,14 @@
max_fd = *fd;
if (fds[1] > max_fd)
max_fd = fds[1];
- sread_sz = (max_fd + NBBY) / NBBY;
+ sread_sz = ROUND_UP(max_fd + 1, sizeof(uint32_t) * NBBY) / NBBY;
swrite_sz = sread_sz;
- s = malloc(sizeof(*s) + sread_sz + swrite_sz);
+ s = uv__malloc(sizeof(*s) + sread_sz + swrite_sz);
if (s == NULL) {
err = -ENOMEM;
goto failed_malloc;
}
@@ -361,11 +366,11 @@
uv__close(fds[1]);
uv_close((uv_handle_t*) &s->async, uv__stream_osx_cb_close);
return err;
failed_async_init:
- free(s);
+ uv__free(s);
failed_malloc:
uv__close(fds[0]);
uv__close(fds[1]);
@@ -373,10 +378,14 @@
}
#endif /* defined(__APPLE__) */
int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
+#if defined(__APPLE__)
+ int enable;
+#endif
+
assert(fd >= 0);
stream->flags |= flags;
if (stream->type == UV_TCP) {
if ((stream->flags & UV_TCP_NODELAY) && uv__tcp_nodelay(fd, 1))
@@ -385,10 +394,19 @@
/* TODO Use delay the user passed in. */
if ((stream->flags & UV_TCP_KEEPALIVE) && uv__tcp_keepalive(fd, 1, 60))
return -errno;
}
+#if defined(__APPLE__)
+ enable = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &enable, sizeof(enable)) &&
+ errno != ENOTSOCK &&
+ errno != EINVAL) {
+ return -errno;
+ }
+#endif
+
stream->io_watcher.fd = fd;
return 0;
}
@@ -585,11 +603,11 @@
server->accepted_fd = queued_fds->fds[0];
/* All read, free */
assert(queued_fds->offset > 0);
if (--queued_fds->offset == 0) {
- free(queued_fds);
+ uv__free(queued_fds);
server->queued_fds = NULL;
} else {
/* Shift rest */
memmove(queued_fds->fds,
queued_fds->fds + 1,
@@ -683,11 +701,11 @@
* they should stop writing - which they should if we got an error. Something
* to revisit in future revisions of the libuv API.
*/
if (req->error == 0) {
if (req->bufs != req->bufsml)
- free(req->bufs);
+ uv__free(req->bufs);
req->bufs = NULL;
}
/* Add it to the write_completed_queue where it will have its
* callback called in the near future.
@@ -894,11 +912,11 @@
uv__req_unregister(stream->loop, req);
if (req->bufs != NULL) {
stream->write_queue_size -= uv__write_req_size(req);
if (req->bufs != req->bufsml)
- free(req->bufs);
+ uv__free(req->bufs);
req->bufs = NULL;
}
/* NOTE: call callback AFTER freeing the request data. */
if (req->cb)
@@ -959,12 +977,12 @@
unsigned int queue_size;
queued_fds = stream->queued_fds;
if (queued_fds == NULL) {
queue_size = 8;
- queued_fds = malloc((queue_size - 1) * sizeof(*queued_fds->fds) +
- sizeof(*queued_fds));
+ queued_fds = uv__malloc((queue_size - 1) * sizeof(*queued_fds->fds) +
+ sizeof(*queued_fds));
if (queued_fds == NULL)
return -ENOMEM;
queued_fds->size = queue_size;
queued_fds->offset = 0;
stream->queued_fds = queued_fds;
@@ -1337,11 +1355,11 @@
req->send_handle = send_handle;
QUEUE_INIT(&req->queue);
req->bufs = req->bufsml;
if (nbufs > ARRAY_SIZE(req->bufsml))
- req->bufs = malloc(nbufs * sizeof(bufs[0]));
+ req->bufs = uv__malloc(nbufs * sizeof(bufs[0]));
if (req->bufs == NULL)
return -ENOMEM;
memcpy(req->bufs, bufs, nbufs * sizeof(bufs[0]));
@@ -1425,11 +1443,11 @@
/* Unqueue request, regardless of immediateness */
QUEUE_REMOVE(&req.queue);
uv__req_unregister(stream->loop, &req);
if (req.bufs != req.bufsml)
- free(req.bufs);
+ uv__free(req.bufs);
req.bufs = NULL;
/* Do not poll for writable, if we wasn't before calling this */
if (!has_pollout) {
uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLOUT);
@@ -1562,16 +1580,19 @@
/* Close all queued fds */
if (handle->queued_fds != NULL) {
queued_fds = handle->queued_fds;
for (i = 0; i < queued_fds->offset; i++)
uv__close(queued_fds->fds[i]);
- free(handle->queued_fds);
+ uv__free(handle->queued_fds);
handle->queued_fds = NULL;
}
assert(!uv__io_active(&handle->io_watcher, UV__POLLIN | UV__POLLOUT));
}
int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
- return UV_ENOSYS;
+ /* Don't need to check the file descriptor, uv__nonblock()
+ * will fail with EBADF if it's not valid.
+ */
+ return uv__nonblock(uv__stream_fd(handle), !blocking);
}