Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: implement kqueue file watcher API
Browse files Browse the repository at this point in the history
kqueue fds are not embeddable into other pollsets (select, poll, kqueue).
Hack the libev event loop to receive kqueue events with filter flags intact.
  • Loading branch information
bnoordhuis committed Oct 4, 2011
1 parent a35591b commit 8e9a338
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 99 deletions.
3 changes: 3 additions & 0 deletions config-unix.mk
Expand Up @@ -54,6 +54,7 @@ EIO_CONFIG=config_darwin.h
CPPFLAGS += -Isrc/ares/config_darwin
LINKFLAGS+=-framework CoreServices
OBJS += src/unix/darwin.o
OBJS += src/unix/kqueue.o
endif

ifeq (Linux,$(uname_S))
Expand All @@ -71,6 +72,7 @@ EIO_CONFIG=config_freebsd.h
CPPFLAGS += -Isrc/ares/config_freebsd
LINKFLAGS+=
OBJS += src/unix/freebsd.o
OBJS += src/unix/kqueue.o
endif

ifeq (NetBSD,$(uname_S))
Expand All @@ -79,6 +81,7 @@ EIO_CONFIG=config_netbsd.h
CPPFLAGS += -Isrc/ares/config_netbsd
LINKFLAGS+=
OBJS += src/unix/netbsd.o
OBJS += src/unix/kqueue.o
endif

ifneq (,$(findstring CYGWIN,$(uname_S)))
Expand Down
1 change: 1 addition & 0 deletions include/uv-private/ev.h
Expand Up @@ -207,6 +207,7 @@ enum {
EV_NONE = 0x00, /* no events */
EV_READ = 0x01, /* ev_io detected read will not block */
EV_WRITE = 0x02, /* ev_io detected write will not block */
EV_LIBUV_KQUEUE_HACK = 0x40,
EV__IOFDSET = 0x80, /* internal use only */
EV_IO = EV_READ, /* alias for type-detection */
EV_TIMER = 0x00000100, /* timer timed out */
Expand Down
29 changes: 0 additions & 29 deletions include/uv-private/uv-linux.h

This file was deleted.

33 changes: 24 additions & 9 deletions include/uv-private/uv-unix.h
Expand Up @@ -27,10 +27,6 @@
#include "ev.h"
#include "eio.h"

#if defined(__linux__)
#include "uv-private/uv-linux.h"
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
Expand All @@ -47,11 +43,6 @@ typedef struct {

typedef int uv_file;

/* Stub. Remove it once all platforms support the file watcher API. */
#ifndef UV_FS_EVENT_PRIVATE_FIELDS
#define UV_FS_EVENT_PRIVATE_FIELDS /* empty */
#endif

#define UV_LOOP_PRIVATE_FIELDS \
ares_channel channel; \
/* \
Expand Down Expand Up @@ -188,4 +179,28 @@ typedef int uv_file;
struct termios orig_termios; \
int mode;

/* UV_FS_EVENT_PRIVATE_FIELDS */
#if defined(__linux__)

#define UV_FS_EVENT_PRIVATE_FIELDS \
ev_io read_watcher; \
uv_fs_event_cb cb; \

#elif (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060) \
|| defined(__FreeBSD__) \
|| defined(__OpenBSD__) \
|| defined(__NetBSD__)

#define UV_FS_EVENT_PRIVATE_FIELDS \
ev_io event_watcher; \
uv_fs_event_cb cb; \
int fflags; \

#else

/* Stub for platforms where the file watcher isn't implemented yet. */
#define UV_FS_EVENT_PRIVATE_FIELDS

#endif

#endif /* UV_UNIX_H */
2 changes: 1 addition & 1 deletion src/unix/core.c
Expand Up @@ -172,7 +172,7 @@ void uv_loop_delete(uv_loop_t* loop) {
uv_loop_t* uv_default_loop() {
if (!default_loop_ptr) {
default_loop_ptr = &default_loop_struct;
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
#if HAVE_KQUEUE
default_loop_struct.ev = ev_default_loop(EVBACKEND_KQUEUE);
#else
default_loop_struct.ev = ev_default_loop(EVFLAG_AUTO);
Expand Down
13 changes: 0 additions & 13 deletions src/unix/darwin.c
Expand Up @@ -106,16 +106,3 @@ void uv_loadavg(double avg[3]) {
avg[1] = (double) info.ldavg[1] / info.fscale;
avg[2] = (double) info.ldavg[2] / info.fscale;
}

int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,
const char* filename,
uv_fs_event_cb cb) {
uv__set_sys_error(loop, ENOSYS);
return -1;
}


void uv__fs_event_destroy(uv_fs_event_t* handle) {
assert(0 && "implement me");
}
3 changes: 2 additions & 1 deletion src/unix/ev/ev.c
Expand Up @@ -2663,7 +2663,8 @@ ev_io_start (EV_P_ ev_io *w)
return;

assert (("libev: ev_io_start called with negative fd", fd >= 0));
assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
assert (("libev: ev_io_start called with illegal event mask",
!(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE | EV_LIBUV_KQUEUE_HACK))));

EV_FREQUENT_CHECK;

Expand Down
15 changes: 15 additions & 0 deletions src/unix/ev/ev_kqueue.c
Expand Up @@ -43,6 +43,9 @@
#include <string.h>
#include <errno.h>

extern void
uv__kqueue_hack (EV_P_ int fflags, ev_io *w);

void inline_speed
kqueue_change (EV_P_ int fd, int filter, int flags, int fflags)
{
Expand Down Expand Up @@ -80,6 +83,10 @@ kqueue_modify (EV_P_ int fd, int oev, int nev)

if (nev & EV_WRITE)
kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, NOTE_EOF);

if (nev & EV_LIBUV_KQUEUE_HACK)
kqueue_change (EV_A_ fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_ONESHOT,
NOTE_ATTRIB | NOTE_WRITE | NOTE_RENAME | NOTE_DELETE | NOTE_EXTEND | NOTE_REVOKE);
}

static void
Expand Down Expand Up @@ -114,6 +121,13 @@ kqueue_poll (EV_P_ ev_tstamp timeout)
{
int fd = kqueue_events [i].ident;

if (kqueue_events [i].filter == EVFILT_VNODE)
{
/* pass kqueue filter flags to libuv */
ev_io *w = (ev_io *)(anfds [fd].head);
uv__kqueue_hack (EV_A_ kqueue_events [i].fflags, w);
}

if (expect_false (kqueue_events [i].flags & EV_ERROR))
{
int err = kqueue_events [i].data;
Expand All @@ -140,6 +154,7 @@ kqueue_poll (EV_P_ ev_tstamp timeout)
fd,
kqueue_events [i].filter == EVFILT_READ ? EV_READ
: kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE
: kqueue_events [i].filter == EVFILT_VNODE ? EV_LIBUV_KQUEUE_HACK
: 0
);
}
Expand Down
13 changes: 0 additions & 13 deletions src/unix/freebsd.c
Expand Up @@ -104,16 +104,3 @@ void uv_loadavg(double avg[3]) {
avg[1] = (double) info.ldavg[1] / info.fscale;
avg[2] = (double) info.ldavg[2] / info.fscale;
}

int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,
const char* filename,
uv_fs_event_cb cb) {
uv__set_sys_error(loop, ENOSYS);
return -1;
}


void uv__fs_event_destroy(uv_fs_event_t* handle) {
assert(0 && "implement me");
}
22 changes: 13 additions & 9 deletions src/unix/internal.h
Expand Up @@ -32,33 +32,37 @@
#include <linux/version.h>
#include <features.h>

#undef HAVE_FUTIMES
#undef HAVE_PIPE2
#undef HAVE_ACCEPT4

/* futimes() requires linux >= 2.6.22 and glib >= 2.6 */
#if LINUX_VERSION_CODE >= 0x20616 && __GLIBC_PREREQ(2, 6)
#define HAVE_FUTIMES
#define HAVE_FUTIMES 1
#endif

/* pipe2() requires linux >= 2.6.27 and glibc >= 2.9 */
#if LINUX_VERSION_CODE >= 0x2061B && __GLIBC_PREREQ(2, 9)
#define HAVE_PIPE2
#define HAVE_PIPE2 1
#endif

/* accept4() requires linux >= 2.6.28 and glib >= 2.10 */
#if LINUX_VERSION_CODE >= 0x2061C && __GLIBC_PREREQ(2, 10)
#define HAVE_ACCEPT4
#define HAVE_ACCEPT4 1
#endif

#endif /* __linux__ */

#ifdef __APPLE__
# define HAVE_FUTIMES
# define HAVE_FUTIMES 1
#endif

#ifdef __FreeBSD__
# define HAVE_FUTIMES
# define HAVE_FUTIMES 1
#endif

/* FIXME exact copy of the #ifdef guard in uv-unix.h */
#if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060) \
|| defined(__FreeBSD__) \
|| defined(__OpenBSD__) \
|| defined(__NetBSD__)
# define HAVE_KQUEUE 1
#endif

#define container_of(ptr, type, member) \
Expand Down
121 changes: 121 additions & 0 deletions src/unix/kqueue.c
@@ -0,0 +1,121 @@
/* 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>
#include <fcntl.h>
#include <time.h>

static void uv__fs_event(EV_P_ ev_io* w, int revents);


static void uv__fs_event_start(uv_fs_event_t* handle) {
ev_io_init(&handle->event_watcher,
uv__fs_event,
handle->fd,
EV_LIBUV_KQUEUE_HACK);
ev_io_start(handle->loop->ev, &handle->event_watcher);
}


static void uv__fs_event_stop(uv_fs_event_t* handle) {
ev_io_stop(handle->loop->ev, &handle->event_watcher);
}


static void uv__fs_event(EV_P_ ev_io* w, int revents) {
uv_fs_event_t* handle;
int events;

assert(revents == EV_LIBUV_KQUEUE_HACK);

handle = container_of(w, uv_fs_event_t, event_watcher);

if (handle->fflags & (NOTE_ATTRIB | NOTE_EXTEND))
events = UV_CHANGE;
else
events = UV_RENAME;

handle->cb(handle, NULL, events, 0);

uv__fs_event_stop(handle);

/* File watcher operates in one-shot mode, re-arm it. */
if (handle->fd != -1)
uv__fs_event_start(handle);
}


/* Called by libev, don't touch. */
void uv__kqueue_hack(EV_P_ int fflags, ev_io *w) {
uv_fs_event_t* handle;

handle = container_of(w, uv_fs_event_t, event_watcher);
handle->fflags = fflags;
}


int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,
const char* filename,
uv_fs_event_cb cb) {
#if HAVE_KQUEUE
int fd;

if (cb == NULL) {
uv__set_sys_error(loop, EINVAL);
return -1;
}

/* 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);
handle->filename = strdup(filename);
handle->fflags = 0;
handle->cb = cb;
handle->fd = fd;
uv__fs_event_start(handle);

return 0;
#else
uv__set_sys_error(loop, ENOSYS);
return -1;
#endif
}


void uv__fs_event_destroy(uv_fs_event_t* handle) {
free(handle->filename);
uv__close(handle->fd);
handle->fd = -1;
}
13 changes: 0 additions & 13 deletions src/unix/netbsd.c
Expand Up @@ -110,16 +110,3 @@ double uv_get_total_memory(void) {

return (double) info;
}

int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,
const char* filename,
uv_fs_event_cb cb) {
uv__set_sys_error(loop, ENOSYS);
return -1;
}


void uv__fs_event_destroy(uv_fs_event_t* handle) {
assert(0 && "implement me");
}

0 comments on commit 8e9a338

Please sign in to comment.