#if !defined(LINT) && !defined(CODECENTER)
static const char rcsid[] = "$Id: ev_files.c,v 1.3.2.1.4.3 2005/07/28 07:43:19 marka Exp $";
#endif
#include "port_before.h"
#include "fd_setsize.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <isc/eventlib.h>
#include "eventlib_p.h"
#include "port_after.h"
static evFile *FindFD(const evContext_p *ctx, int fd, int eventmask);
int
evSelectFD(evContext opaqueCtx,
int fd,
int eventmask,
evFileFunc func,
void *uap,
evFileID *opaqueID
) {
evContext_p *ctx = opaqueCtx.opaque;
evFile *id;
int mode;
evPrintf(ctx, 1,
"evSelectFD(ctx %p, fd %d, mask 0x%x, func %p, uap %p)\n",
ctx, fd, eventmask, func, uap);
if (eventmask == 0 || (eventmask & ~EV_MASK_ALL) != 0)
EV_ERR(EINVAL);
#ifndef USE_POLL
if (fd > ctx->highestFD)
EV_ERR(EINVAL);
#endif
OK(mode = fcntl(fd, F_GETFL, NULL));
#ifdef USE_POLL
if (fd >= ctx->maxnfds && evPollfdRealloc(ctx, 1, fd) != 0)
EV_ERR(ENOMEM);
#endif
id = FindFD(ctx, fd, EV_MASK_ALL);
if (id == NULL) {
if (mode & PORT_NONBLOCK)
FD_SET(fd, &ctx->nonblockBefore);
else {
#ifdef USE_FIONBIO_IOCTL
int on = 1;
OK(ioctl(fd, FIONBIO, (char *)&on));
#else
OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK));
#endif
FD_CLR(fd, &ctx->nonblockBefore);
}
}
if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
EV_ERR(ETOOMANYREFS);
OKNEW(id);
id->func = func;
id->uap = uap;
id->fd = fd;
id->eventmask = eventmask;
if (ctx->files != NULL)
ctx->files->prev = id;
id->prev = NULL;
id->next = ctx->files;
ctx->files = id;
if (ctx->fdTable[fd] != NULL)
ctx->fdTable[fd]->fdprev = id;
id->fdprev = NULL;
id->fdnext = ctx->fdTable[fd];
ctx->fdTable[fd] = id;
if (eventmask & EV_READ)
FD_SET(fd, &ctx->rdNext);
if (eventmask & EV_WRITE)
FD_SET(fd, &ctx->wrNext);
if (eventmask & EV_EXCEPT)
FD_SET(fd, &ctx->exNext);
if (fd > ctx->fdMax)
ctx->fdMax = fd;
if (opaqueID)
opaqueID->opaque = id;
return (0);
}
int
evDeselectFD(evContext opaqueCtx, evFileID opaqueID) {
evContext_p *ctx = opaqueCtx.opaque;
evFile *del = opaqueID.opaque;
evFile *cur;
int mode, eventmask;
if (!del) {
evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n");
errno = EINVAL;
return (-1);
}
evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n",
del->fd, del->eventmask);
mode = fcntl(del->fd, F_GETFL, NULL);
if (mode == -1 && errno != EBADF)
EV_ERR(errno);
if (del->prev != NULL)
del->prev->next = del->next;
else
ctx->files = del->next;
if (del->next != NULL)
del->next->prev = del->prev;
if (del->fdprev != NULL)
del->fdprev->fdnext = del->fdnext;
else
ctx->fdTable[del->fd] = del->fdnext;
if (del->fdnext != NULL)
del->fdnext->fdprev = del->fdprev;
if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) &&
!FD_ISSET(del->fd, &ctx->nonblockBefore) &&
mode != -1) {
#ifdef USE_FIONBIO_IOCTL
int off = 0;
(void) ioctl(del->fd, FIONBIO, (char *)&off);
#else
(void) fcntl(del->fd, F_SETFL, mode & ~PORT_NONBLOCK);
#endif
}
eventmask = 0;
for ((void)NULL;
cur != NULL && eventmask != EV_MASK_ALL;
cur = cur->next)
if (cur->fd == del->fd)
eventmask |= cur->eventmask;
if (!(eventmask & EV_READ)) {
FD_CLR(del->fd, &ctx->rdNext);
if (FD_ISSET(del->fd, &ctx->rdLast)) {
FD_CLR(del->fd, &ctx->rdLast);
ctx->fdCount--;
}
}
if (!(eventmask & EV_WRITE)) {
FD_CLR(del->fd, &ctx->wrNext);
if (FD_ISSET(del->fd, &ctx->wrLast)) {
FD_CLR(del->fd, &ctx->wrLast);
ctx->fdCount--;
}
}
if (!(eventmask & EV_EXCEPT)) {
FD_CLR(del->fd, &ctx->exNext);
if (FD_ISSET(del->fd, &ctx->exLast)) {
FD_CLR(del->fd, &ctx->exLast);
ctx->fdCount--;
}
}
if (del->fd == ctx->fdMax) {
ctx->fdMax = -1;
for (cur = ctx->files; cur; cur = cur->next)
if (cur->fd > ctx->fdMax)
ctx->fdMax = cur->fd;
}
if (del == ctx->fdNext)
ctx->fdNext = del->next;
FREE(del);
return (0);
}
static evFile *
FindFD(const evContext_p *ctx, int fd, int eventmask) {
evFile *id;
for (id = ctx->fdTable[fd]; id != NULL; id = id->fdnext)
if (id->fd == fd && (id->eventmask & eventmask) != 0)
break;
return (id);
}