2011-09-22 23:20:33 +00:00
|
|
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "uv.h"
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/event.h>
|
2012-08-22 23:14:41 +00:00
|
|
|
#include <sys/time.h>
|
2011-09-27 22:49:08 +00:00
|
|
|
#include <unistd.h>
|
2011-09-22 23:20:33 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
static void uv__fs_event(uv_loop_t* loop, uv__io_t* w, unsigned int fflags);
|
2011-09-22 23:20:33 +00:00
|
|
|
|
|
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
int uv__kqueue_init(uv_loop_t* loop) {
|
|
|
|
|
loop->backend_fd = kqueue();
|
|
|
|
|
|
|
|
|
|
if (loop->backend_fd == -1)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
uv__cloexec(loop->backend_fd, 1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
2011-09-22 23:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
void uv__io_poll(uv_loop_t* loop, int timeout) {
|
|
|
|
|
struct kevent events[1024];
|
|
|
|
|
struct kevent* ev;
|
|
|
|
|
struct timespec spec;
|
|
|
|
|
unsigned int nevents;
|
|
|
|
|
unsigned int revents;
|
|
|
|
|
ngx_queue_t* q;
|
|
|
|
|
uint64_t base;
|
|
|
|
|
uint64_t diff;
|
|
|
|
|
uv__io_t* w;
|
|
|
|
|
int filter;
|
|
|
|
|
int fflags;
|
|
|
|
|
int count;
|
|
|
|
|
int nfds;
|
|
|
|
|
int fd;
|
|
|
|
|
int op;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (loop->nfds == 0) {
|
|
|
|
|
assert(ngx_queue_empty(&loop->watcher_queue));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nevents = 0;
|
|
|
|
|
|
|
|
|
|
while (!ngx_queue_empty(&loop->watcher_queue)) {
|
|
|
|
|
q = ngx_queue_head(&loop->watcher_queue);
|
|
|
|
|
ngx_queue_remove(q);
|
|
|
|
|
ngx_queue_init(q);
|
|
|
|
|
|
|
|
|
|
w = ngx_queue_data(q, uv__io_t, watcher_queue);
|
|
|
|
|
assert(w->pevents != 0);
|
|
|
|
|
assert(w->fd >= 0);
|
|
|
|
|
assert(w->fd < (int) loop->nwatchers);
|
|
|
|
|
|
2012-11-15 22:53:18 +00:00
|
|
|
if ((w->events & UV__POLLIN) == 0 && (w->pevents & UV__POLLIN) != 0) {
|
2012-08-22 23:14:41 +00:00
|
|
|
filter = EVFILT_READ;
|
|
|
|
|
fflags = 0;
|
|
|
|
|
op = EV_ADD;
|
|
|
|
|
|
|
|
|
|
if (w->cb == uv__fs_event) {
|
|
|
|
|
filter = EVFILT_VNODE;
|
|
|
|
|
fflags = NOTE_ATTRIB | NOTE_WRITE | NOTE_RENAME
|
|
|
|
|
| NOTE_DELETE | NOTE_EXTEND | NOTE_REVOKE;
|
|
|
|
|
op = EV_ADD | EV_ONESHOT; /* Stop the event from firing repeatedly. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EV_SET(events + nevents, w->fd, filter, op, fflags, 0, 0);
|
|
|
|
|
|
|
|
|
|
if (++nevents == ARRAY_SIZE(events)) {
|
|
|
|
|
if (kevent(loop->backend_fd, events, nevents, NULL, 0, NULL))
|
|
|
|
|
abort();
|
|
|
|
|
nevents = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-15 22:53:18 +00:00
|
|
|
if ((w->events & UV__POLLOUT) == 0 && (w->pevents & UV__POLLOUT) != 0) {
|
2012-08-22 23:14:41 +00:00
|
|
|
EV_SET(events + nevents, w->fd, EVFILT_WRITE, EV_ADD, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
if (++nevents == ARRAY_SIZE(events)) {
|
|
|
|
|
if (kevent(loop->backend_fd, events, nevents, NULL, 0, NULL))
|
|
|
|
|
abort();
|
|
|
|
|
nevents = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w->events = w->pevents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(timeout >= -1);
|
|
|
|
|
base = loop->time;
|
|
|
|
|
count = 48; /* Benchmarks suggest this gives the best throughput. */
|
|
|
|
|
|
|
|
|
|
for (;; nevents = 0) {
|
|
|
|
|
if (timeout != -1) {
|
|
|
|
|
spec.tv_sec = timeout / 1000;
|
|
|
|
|
spec.tv_nsec = (timeout % 1000) * 1000000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nfds = kevent(loop->backend_fd,
|
|
|
|
|
events,
|
|
|
|
|
nevents,
|
|
|
|
|
events,
|
|
|
|
|
ARRAY_SIZE(events),
|
|
|
|
|
timeout == -1 ? NULL : &spec);
|
|
|
|
|
|
2013-01-06 16:55:51 +00:00
|
|
|
/* Update loop->time unconditionally. It's tempting to skip the update when
|
|
|
|
|
* timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
|
|
|
|
|
* operating system didn't reschedule our process while in the syscall.
|
|
|
|
|
*/
|
|
|
|
|
SAVE_ERRNO(uv__update_time(loop));
|
|
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
if (nfds == 0) {
|
|
|
|
|
assert(timeout != -1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nfds == -1) {
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
|
abort();
|
|
|
|
|
|
|
|
|
|
if (timeout == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (timeout == -1)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* Interrupted by a signal. Update timeout and poll again. */
|
|
|
|
|
goto update_timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nevents = 0;
|
|
|
|
|
|
2013-10-25 08:56:37 +00:00
|
|
|
assert(loop->watchers != NULL);
|
|
|
|
|
loop->watchers[loop->nwatchers] = (void*) events;
|
|
|
|
|
loop->watchers[loop->nwatchers + 1] = (void*) (uintptr_t) nfds;
|
2012-08-22 23:14:41 +00:00
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
|
ev = events + i;
|
|
|
|
|
fd = ev->ident;
|
2013-10-25 08:56:37 +00:00
|
|
|
/* Skip invalidated events, see uv__platform_invalidate_fd */
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
continue;
|
osx: Fix a possible segfault in uv__io_poll
In our build infrastructure, I've seen a lot of segfaults recently that
were all only happening on OSX. Upon inspecting the coredumps, it
appearded that all segfaults happened at the same instruction, and upon
translating the assembly back to the source, I found that an array could
be indexed with a -1 index before the index was checked to be not -1.
As concrete evidence, here is the situation that I found caused the
segfault. The instruction in question along with the relevant register
values was:
mov (%r8,%r15,8),%r12
r8 = 0x7fb0ba800000
r15 = 0xffffffffffffffff
r8 + r15 * 8 == 0x7fb0ba7ffff8
It appears that the base of loop->watchers was page aligned, and by
going back one word I guess that the page wasn't mapped, causing our
segfaults.
2013-12-22 04:12:35 +00:00
|
|
|
w = loop->watchers[fd];
|
2013-10-25 08:56:37 +00:00
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
if (w == NULL) {
|
|
|
|
|
/* File descriptor that we've stopped watching, disarm it. */
|
|
|
|
|
/* TODO batch up */
|
|
|
|
|
struct kevent events[1];
|
|
|
|
|
|
|
|
|
|
EV_SET(events + 0, fd, ev->filter, EV_DELETE, 0, 0, 0);
|
|
|
|
|
if (kevent(loop->backend_fd, events, 1, NULL, 0, NULL))
|
|
|
|
|
if (errno != EBADF && errno != ENOENT)
|
|
|
|
|
abort();
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ev->filter == EVFILT_VNODE) {
|
2012-11-15 22:53:18 +00:00
|
|
|
assert(w->events == UV__POLLIN);
|
|
|
|
|
assert(w->pevents == UV__POLLIN);
|
2012-08-22 23:14:41 +00:00
|
|
|
w->cb(loop, w, ev->fflags); /* XXX always uv__fs_event() */
|
|
|
|
|
nevents++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
revents = 0;
|
|
|
|
|
|
|
|
|
|
if (ev->filter == EVFILT_READ) {
|
2013-10-25 21:06:12 +00:00
|
|
|
if (w->pevents & UV__POLLIN) {
|
2012-11-15 22:53:18 +00:00
|
|
|
revents |= UV__POLLIN;
|
2013-02-08 10:48:52 +00:00
|
|
|
w->rcount = ev->data;
|
|
|
|
|
} else {
|
2012-08-22 23:14:41 +00:00
|
|
|
/* TODO batch up */
|
|
|
|
|
struct kevent events[1];
|
|
|
|
|
EV_SET(events + 0, fd, ev->filter, EV_DELETE, 0, 0, 0);
|
2012-12-17 13:03:44 +00:00
|
|
|
if (kevent(loop->backend_fd, events, 1, NULL, 0, NULL))
|
|
|
|
|
if (errno != ENOENT)
|
|
|
|
|
abort();
|
2012-08-22 23:14:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ev->filter == EVFILT_WRITE) {
|
2013-10-25 21:06:12 +00:00
|
|
|
if (w->pevents & UV__POLLOUT) {
|
2012-11-15 22:53:18 +00:00
|
|
|
revents |= UV__POLLOUT;
|
2013-02-08 10:48:52 +00:00
|
|
|
w->wcount = ev->data;
|
|
|
|
|
} else {
|
2012-08-22 23:14:41 +00:00
|
|
|
/* TODO batch up */
|
|
|
|
|
struct kevent events[1];
|
|
|
|
|
EV_SET(events + 0, fd, ev->filter, EV_DELETE, 0, 0, 0);
|
2012-12-17 13:03:44 +00:00
|
|
|
if (kevent(loop->backend_fd, events, 1, NULL, 0, NULL))
|
|
|
|
|
if (errno != ENOENT)
|
|
|
|
|
abort();
|
2012-08-22 23:14:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ev->flags & EV_ERROR)
|
2012-11-15 22:53:18 +00:00
|
|
|
revents |= UV__POLLERR;
|
2012-08-22 23:14:41 +00:00
|
|
|
|
|
|
|
|
if (revents == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
w->cb(loop, w, revents);
|
|
|
|
|
nevents++;
|
|
|
|
|
}
|
2013-10-25 08:56:37 +00:00
|
|
|
loop->watchers[loop->nwatchers] = NULL;
|
|
|
|
|
loop->watchers[loop->nwatchers + 1] = NULL;
|
2012-08-22 23:14:41 +00:00
|
|
|
|
|
|
|
|
if (nevents != 0) {
|
|
|
|
|
if (nfds == ARRAY_SIZE(events) && --count != 0) {
|
|
|
|
|
/* Poll for more events but don't block this time. */
|
|
|
|
|
timeout = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timeout == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (timeout == -1)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
update_timeout:
|
|
|
|
|
assert(timeout > 0);
|
|
|
|
|
|
2013-01-06 16:55:51 +00:00
|
|
|
diff = loop->time - base;
|
2012-08-22 23:14:41 +00:00
|
|
|
if (diff >= (uint64_t) timeout)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
timeout -= diff;
|
|
|
|
|
}
|
2011-09-22 23:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-11-13 11:20:07 +00:00
|
|
|
void uv__platform_invalidate_fd(uv_loop_t* loop, int fd) {
|
|
|
|
|
struct kevent* events;
|
|
|
|
|
uintptr_t i;
|
|
|
|
|
uintptr_t nfds;
|
|
|
|
|
|
|
|
|
|
assert(loop->watchers != NULL);
|
|
|
|
|
|
|
|
|
|
events = (struct kevent*) loop->watchers[loop->nwatchers];
|
|
|
|
|
nfds = (uintptr_t) loop->watchers[loop->nwatchers + 1];
|
|
|
|
|
if (events == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Invalidate events with same file descriptor */
|
|
|
|
|
for (i = 0; i < nfds; i++)
|
|
|
|
|
if ((int) events[i].ident == fd)
|
|
|
|
|
events[i].ident = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
static void uv__fs_event(uv_loop_t* loop, uv__io_t* w, unsigned int fflags) {
|
2011-09-22 23:20:33 +00:00
|
|
|
uv_fs_event_t* handle;
|
2012-08-22 23:14:41 +00:00
|
|
|
struct kevent ev;
|
2011-09-22 23:20:33 +00:00
|
|
|
int events;
|
|
|
|
|
|
|
|
|
|
handle = container_of(w, uv_fs_event_t, event_watcher);
|
|
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
if (fflags & (NOTE_ATTRIB | NOTE_EXTEND))
|
2011-09-22 23:20:33 +00:00
|
|
|
events = UV_CHANGE;
|
|
|
|
|
else
|
|
|
|
|
events = UV_RENAME;
|
|
|
|
|
|
|
|
|
|
handle->cb(handle, NULL, events, 0);
|
|
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
if (handle->event_watcher.fd == -1)
|
2012-02-29 14:55:05 +00:00
|
|
|
return;
|
2011-09-22 23:20:33 +00:00
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
/* Watcher operates in one-shot mode, re-arm it. */
|
|
|
|
|
fflags = NOTE_ATTRIB | NOTE_WRITE | NOTE_RENAME
|
|
|
|
|
| NOTE_DELETE | NOTE_EXTEND | NOTE_REVOKE;
|
2011-09-22 23:20:33 +00:00
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
EV_SET(&ev, w->fd, EVFILT_VNODE, EV_ADD | EV_ONESHOT, fflags, 0, 0);
|
2011-09-22 23:20:33 +00:00
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
if (kevent(loop->backend_fd, &ev, 1, NULL, 0, NULL))
|
|
|
|
|
abort();
|
2011-09-22 23:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int uv_fs_event_init(uv_loop_t* loop,
|
|
|
|
|
uv_fs_event_t* handle,
|
|
|
|
|
const char* filename,
|
2011-11-05 00:42:08 +00:00
|
|
|
uv_fs_event_cb cb,
|
|
|
|
|
int flags) {
|
2012-08-13 12:37:31 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
struct stat statbuf;
|
|
|
|
|
#endif /* defined(__APPLE__) */
|
2012-08-22 23:14:41 +00:00
|
|
|
int fd;
|
2011-09-22 23:20:33 +00:00
|
|
|
|
|
|
|
|
/* TODO open asynchronously - but how do we report back errors? */
|
|
|
|
|
if ((fd = open(filename, O_RDONLY)) == -1) {
|
|
|
|
|
uv__set_sys_error(loop, errno);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);
|
2012-05-17 04:28:46 +00:00
|
|
|
uv__handle_start(handle); /* FIXME shouldn't start automatically */
|
2012-08-22 23:14:41 +00:00
|
|
|
uv__io_init(&handle->event_watcher, uv__fs_event, fd);
|
2011-09-22 23:20:33 +00:00
|
|
|
handle->filename = strdup(filename);
|
|
|
|
|
handle->cb = cb;
|
2012-08-13 12:37:31 +00:00
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
/* Nullify field to perform checks later */
|
2013-10-05 16:15:02 +00:00
|
|
|
handle->cf_eventstream = NULL;
|
2012-09-17 20:38:40 +00:00
|
|
|
handle->realpath = NULL;
|
|
|
|
|
handle->realpath_len = 0;
|
|
|
|
|
handle->cf_flags = flags;
|
2012-08-13 12:37:31 +00:00
|
|
|
|
|
|
|
|
if (fstat(fd, &statbuf))
|
|
|
|
|
goto fallback;
|
|
|
|
|
/* FSEvents works only with directories */
|
|
|
|
|
if (!(statbuf.st_mode & S_IFDIR))
|
|
|
|
|
goto fallback;
|
|
|
|
|
|
|
|
|
|
return uv__fsevents_init(handle);
|
|
|
|
|
|
|
|
|
|
fallback:
|
|
|
|
|
#endif /* defined(__APPLE__) */
|
|
|
|
|
|
2012-11-15 22:53:18 +00:00
|
|
|
uv__io_start(loop, &handle->event_watcher, UV__POLLIN);
|
2011-09-22 23:20:33 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-04-04 12:30:13 +00:00
|
|
|
void uv__fs_event_close(uv_fs_event_t* handle) {
|
2012-08-13 12:37:31 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
if (uv__fsevents_close(handle))
|
2012-11-15 22:53:18 +00:00
|
|
|
uv__io_stop(handle->loop, &handle->event_watcher, UV__POLLIN);
|
2012-08-13 12:37:31 +00:00
|
|
|
#else
|
2012-11-15 22:53:18 +00:00
|
|
|
uv__io_stop(handle->loop, &handle->event_watcher, UV__POLLIN);
|
2012-08-13 12:37:31 +00:00
|
|
|
#endif /* defined(__APPLE__) */
|
|
|
|
|
|
2012-05-17 04:28:46 +00:00
|
|
|
uv__handle_stop(handle);
|
2012-08-22 23:14:41 +00:00
|
|
|
|
2011-09-22 23:20:33 +00:00
|
|
|
free(handle->filename);
|
2012-08-22 23:14:41 +00:00
|
|
|
handle->filename = NULL;
|
|
|
|
|
|
|
|
|
|
close(handle->event_watcher.fd);
|
|
|
|
|
handle->event_watcher.fd = -1;
|
2011-09-22 23:20:33 +00:00
|
|
|
}
|