#include "portab.h"
-static char UNUSED id[] = "$Id: io.c,v 1.2 2005/07/09 20:23:00 fw Exp $";
+static char UNUSED id[] = "$Id: io.c,v 1.14 2006/05/10 17:33:36 fw Exp $";
#include <assert.h>
#include <stdlib.h>
#include "io.h"
#include "log.h"
+/* Enables extra debug messages in event add/delete/callback code. */
+/* #define DEBUG_IO */
typedef struct {
void (*callback)(int, short);
#endif
#endif
+static bool library_initialized;
#ifdef IO_USE_EPOLL
#include <sys/epoll.h>
static int io_masterfd;
-static bool io_event_new_epoll(int fd, short what);
-static bool io_event_change_epoll(int fd, short what);
+static bool io_event_change_epoll(int fd, short what, const int action);
static int io_dispatch_epoll(struct timeval *tv);
#endif
static int io_masterfd;
static int io_dispatch_kqueue(struct timeval *tv);
-static bool io_event_add_kqueue(int, short);
+static bool io_event_change_kqueue(int, short, const int action);
#endif
#ifdef IO_USE_SELECT
io_event_get(int fd)
{
io_event *i;
+
assert(fd >= 0);
- i = (io_event *) array_get(&io_events, sizeof(io_event), fd);
- assert(i);
+
+ i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
+
+ assert(i != NULL);
return i;
}
bool
io_library_init(unsigned int eventsize)
{
+ bool ret;
#ifdef IO_USE_EPOLL
int ecreate_hint = (int)eventsize;
if (ecreate_hint <= 0)
ecreate_hint = 128;
#endif
+ if (library_initialized)
+ return true;
#ifdef IO_USE_SELECT
#ifdef FD_SETSIZE
eventsize = FD_SETSIZE - 1;
#endif
#endif
- if (eventsize && !array_alloc(&io_events, sizeof(io_event), eventsize))
+ if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
eventsize = 0;
#ifdef IO_USE_EPOLL
io_masterfd = epoll_create(ecreate_hint);
Log(LOG_INFO,
- "io subsystem: using epoll (hint size %d), initial io_event maxfd: %u, io_masterfd %d",
+ "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
ecreate_hint, eventsize, io_masterfd);
- return io_masterfd >= 0;
+ ret = io_masterfd >= 0;
+ if (ret) library_initialized = true;
+
+ return ret;
#endif
#ifdef IO_USE_SELECT
- Log(LOG_INFO, "io subsystem: using select, initial io_event maxfd: %u",
+ Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
eventsize);
FD_ZERO(&readers);
FD_ZERO(&writers);
#ifdef FD_SETSIZE
- if (Conf_MaxConnections >= FD_SETSIZE) {
+ if (Conf_MaxConnections >= (int)FD_SETSIZE) {
Log(LOG_WARNING,
- "Conf_MaxConnections (%d) exceeds limit (%u), changed Conf_MaxConnections to %u",
+ "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
Conf_MaxConnections = FD_SETSIZE - 1;
}
#else
Log(LOG_WARNING,
- "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform");
-#endif
+ "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
+#endif /* FD_SETSIZE */
+ library_initialized = true;
return true;
-#endif
+#endif /* SELECT */
#ifdef IO_USE_KQUEUE
io_masterfd = kqueue();
Log(LOG_INFO,
- "io subsystem: using kqueue, initial io_event maxfd: %u, io_masterfd %d",
+ "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
eventsize, io_masterfd);
- return io_masterfd >= 0;
+ ret = io_masterfd >= 0;
+ if (ret) library_initialized = true;
+
+ return ret;
#endif
}
#ifdef IO_USE_KQUEUE
array_free(&io_evcache);
#endif
- return true;
+ library_initialized = false;
}
bool
io_event_create(int fd, short what, void (*cbfunc) (int, short))
{
+ bool ret;
io_event *i;
assert(fd >= 0);
#endif /* FD_SETSIZE */
#endif /* IO_USE_SELECT */
- i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
+ i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
if (!i) {
Log(LOG_WARNING,
"array_alloc failed: could not allocate space for %d io_event structures",
i->fd = fd;
i->callback = cbfunc;
+ i->what = 0;
#ifdef IO_USE_EPOLL
- i->what = what;
- return io_event_new_epoll(fd, what);
+ ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
#endif
#ifdef IO_USE_KQUEUE
- i->what = what;
- return io_event_add_kqueue(fd, what);
+ ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
#endif
#ifdef IO_USE_SELECT
- i->what = 0;
- return io_event_add(fd, what);
+ ret = io_event_add(fd, what);
#endif
+ if (ret) i->what = what;
+ return ret;
}
#ifdef IO_USE_EPOLL
static bool
-io_event_new_epoll(int fd, short what)
+io_event_change_epoll(int fd, short what, const int action)
{
struct epoll_event ev = { 0, {0} };
ev.data.fd = fd;
if (what & IO_WANTWRITE)
ev.events |= EPOLLOUT;
- return epoll_ctl(io_masterfd, EPOLL_CTL_ADD, fd, &ev) == 0;
-}
-
-
-static bool
-io_event_change_epoll(int fd, short what)
-{
- struct epoll_event ev = { 0, {0} };
- ev.data.fd = fd;
-
- if (what & IO_WANTREAD)
- ev.events = EPOLLIN | EPOLLPRI;
- if (what & IO_WANTWRITE)
- ev.events |= EPOLLOUT;
-
- return epoll_ctl(io_masterfd, EPOLL_CTL_MOD, fd, &ev) == 0;
+ return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
}
#endif
events = array_start(&io_evcache);
- assert(events);
+ assert(events != NULL);
ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
if (ret)
static bool
-io_event_add_kqueue(int fd, short what)
+io_event_change_kqueue(int fd, short what, const int action)
{
struct kevent kev;
- short filter = 0;
- unsigned int len = array_length(&io_evcache, sizeof kev);
- bool ret;
-
- if (what & IO_WANTREAD)
- filter = EVFILT_READ;
-
- if (what & IO_WANTWRITE)
- filter |= EVFILT_WRITE;
-
- if (len >= 100) {
- ret = io_event_kqueue_commit_cache();
- if (ret)
- array_trunc(&io_evcache);
+ bool ret = true;
+
+ if (what & IO_WANTREAD) {
+ EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
+ ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
+ if (!ret)
+ ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
+ }
+
+ if (ret && (what & IO_WANTWRITE)) {
+ EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
+ ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
+ if (!ret)
+ ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
}
- EV_SET(&kev, fd, filter, EV_ADD | EV_ENABLE, 0, 0, NULL);
- return array_catb(&io_evcache, (char*) &kev, sizeof (kev));
+ if (array_length(&io_evcache, sizeof kev) >= 100)
+ io_event_kqueue_commit_cache();
+ return ret;
}
#endif
{
io_event *i = io_event_get(fd);
- assert(i);
-
- if (!i)
- return false;
- if (i->what == what)
- return true;
-#ifdef DEBUG
+ if (!i) return false;
+ if (i->what == what) return true;
+#ifdef DEBUG_IO
Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
#endif
-
i->what |= what;
-
#ifdef IO_USE_EPOLL
- return io_event_change_epoll(fd, i->what);
+ return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
#endif
#ifdef IO_USE_KQUEUE
- return io_event_add_kqueue(fd, what);
+ return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
#endif
#ifdef IO_USE_SELECT
bool
io_close(int fd)
{
- io_event *i = io_event_get(fd);
- if (i) {
- memset(i, 0, sizeof(io_event));
- i->fd = -1;
- }
+ io_event *i;
#ifdef IO_USE_SELECT
FD_CLR(fd, &writers);
FD_CLR(fd, &readers);
- if (fd == select_maxfd)
- select_maxfd--;
+ if (fd == select_maxfd) {
+ while (select_maxfd>0) {
+ --select_maxfd; /* find largest fd */
+ i = io_event_get(select_maxfd);
+ if (i && (i->fd >= 0)) break;
+ }
+ }
#endif
+ i = io_event_get(fd);
#ifdef IO_USE_KQUEUE
if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
io_event_kqueue_commit_cache();
+
+ /* both kqueue and epoll remove fd from all sets automatically on the last close
+ * of the descriptor. since we don't know if this is the last close we'll have
+ * to remove the set explicitly. */
+ if (i) {
+ io_event_change_kqueue(fd, i->what, EV_DELETE);
+ io_event_kqueue_commit_cache();
+ }
+#endif
+#ifdef IO_USE_EPOLL
+ io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
#endif
- return close(fd) == 0; /* both epoll an kqueue will remove fd from all sets automatically */
+ if (i) {
+ memset(i, 0, sizeof(io_event));
+ i->fd = -1;
+ }
+ return close(fd) == 0;
}
bool
io_event_del(int fd, short what)
{
-#ifdef IO_USE_KQUEUE
- struct kevent kev;
- short filter = 0;
-#endif
io_event *i = io_event_get(fd);
-#ifdef DEBUG
+#ifdef DEBUG_IO
Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
#endif
- assert(i);
- if (!i)
- return false;
+ if (!i) return false;
i->what &= ~what;
#ifdef IO_USE_EPOLL
- return io_event_change_epoll(fd, i->what);
+ return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
#endif
#ifdef IO_USE_KQUEUE
- if (what & IO_WANTREAD)
- filter = EVFILT_READ;
-
- if (what & IO_WANTWRITE)
- filter |= EVFILT_WRITE;
-
- EV_SET(&kev, fd, filter, EV_DELETE, 0, 0, NULL);
- return kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
+ return io_event_change_kqueue(fd, what, EV_DISABLE);
#endif
-
#ifdef IO_USE_SELECT
if (what & IO_WANTWRITE)
FD_CLR(i->fd, &writers);
{
int i, total = 0, ret;
struct kevent kev[100];
- struct kevents *newevents;
+ struct kevent *newevents;
struct timespec ts;
int newevents_len;
- short type;
ts.tv_sec = tv->tv_sec;
ts.tv_nsec = tv->tv_usec * 1000;
do {
- newevents_len = array_length(&io_evcache, sizeof (struct kevent));
+ newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
assert(newevents_len >= 0);
if (newevents_len < 0)
newevents_len = 0;
#ifdef DEBUG
if (newevents_len)
- assert(newevents);
+ assert(newevents != NULL);
#endif
-
- ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
+ ret = kevent(io_masterfd, newevents, newevents_len, kev,
+ 100, &ts);
if ((newevents_len>0) && ret != -1)
array_trunc(&io_evcache);
return total;
for (i = 0; i < ret; i++) {
- type = 0;
- if (kev[i].flags & EV_EOF)
- type = IO_ERROR;
-
- if (kev[i].filter & EV_ERROR)
- type = IO_ERROR;
-
- if (kev[i].filter & EVFILT_READ)
- type |= IO_WANTREAD;
-
- if (kev[i].filter & EVFILT_WRITE)
- type |= IO_WANTWRITE;
-
- io_docallback(kev[i].ident, type);
+ if (kev[i].flags & EV_EOF) {
+#ifdef DEBUG
+ LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
+ kev[i].filter, kev[i].ident);
+#endif
+ io_docallback((int)kev[i].ident, IO_ERROR);
+ continue;
+ }
+
+ switch (kev[i].filter) {
+ case EVFILT_READ:
+ io_docallback((int)kev[i].ident, IO_WANTREAD);
+ break;
+ case EVFILT_WRITE:
+ io_docallback((int)kev[i].ident, IO_WANTWRITE);
+ break;
+ default:
+#ifdef DEBUG
+ LogDebug("Unknown kev.filter number %d for fd %d",
+ kev[i].filter, kev[i].ident); /* Fall through */
+#endif
+ case EV_ERROR:
+ io_docallback((int)kev[i].ident, IO_ERROR);
+ break;
+ }
}
-
ts.tv_sec = 0;
ts.tv_nsec = 0;
-
} while (ret == 100);
return total;
io_docallback(int fd, short what)
{
io_event *i;
-#ifdef DEBUG
+#ifdef DEBUG_IO
Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
#endif
i = io_event_get(fd);
- assert(i);
- if (i->callback) /* callback might be 0 if previous callback function called io_close on this fd */
+ if (i->callback) { /* callback might be NULL if a previous callback function
+ called io_close on this fd */
i->callback(fd, (what & IO_ERROR) ? i->what : what);
- /* if error indicator is set, we return the event(s) the app asked for */
+ }
+ /* if error indicator is set, we return the event(s) that were registered */
}