#include "cupsd.h"
#ifdef HAVE_EPOLL
# include <sys/epoll.h>
# include <poll.h>
#elif defined(HAVE_KQUEUE)
# include <sys/event.h>
# include <sys/time.h>
#elif defined(HAVE_POLL)
# include <poll.h>
#else
# include <sys/select.h>
#endif
typedef struct _cupsd_fd_s
{
int fd,
use;
cupsd_selfunc_t read_cb,
write_cb;
void *data;
} _cupsd_fd_t;
static cups_array_t *cupsd_fds = NULL;
#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
static cups_array_t *cupsd_inactive_fds = NULL;
static int cupsd_in_select = 0;
#endif
#ifdef HAVE_KQUEUE
static int cupsd_kqueue_fd = -1,
cupsd_kqueue_changes = 0;
static struct kevent *cupsd_kqueue_events = NULL;
#elif defined(HAVE_POLL)
static int cupsd_alloc_pollfds = 0,
cupsd_update_pollfds = 0;
static struct pollfd *cupsd_pollfds = NULL;
# ifdef HAVE_EPOLL
static int cupsd_epoll_fd = -1;
static struct epoll_event *cupsd_epoll_events = NULL;
# endif
#else
static fd_set cupsd_global_input,
cupsd_global_output,
cupsd_current_input,
cupsd_current_output;
#endif
static int compare_fds(_cupsd_fd_t *a, _cupsd_fd_t *b);
static _cupsd_fd_t *find_fd(int fd);
#define release_fd(f) { \
(f)->use --; \
if (!(f)->use) free((f));\
}
#define retain_fd(f) (f)->use++
int
cupsdAddSelect(int fd,
cupsd_selfunc_t read_cb,
cupsd_selfunc_t write_cb,
void *data)
{
_cupsd_fd_t *fdptr;
#ifdef HAVE_EPOLL
int added;
#endif
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdAddSelect(fd=%d, read_cb=%p, write_cb=%p, data=%p)",
fd, read_cb, write_cb, data);
if (fd < 0)
return (0);
if ((fdptr = find_fd(fd)) == NULL)
{
if ((fdptr = calloc(1, sizeof(_cupsd_fd_t))) == NULL)
return (0);
fdptr->fd = fd;
fdptr->use = 1;
if (!cupsArrayAdd(cupsd_fds, fdptr))
{
cupsdLogMessage(CUPSD_LOG_EMERG, "Unable to add fd %d to array!", fd);
free(fdptr);
return (0);
}
#ifdef HAVE_EPOLL
added = 1;
}
else
added = 0;
#else
}
#endif
#ifdef HAVE_KQUEUE
{
struct kevent event;
struct timespec timeout;
timeout.tv_sec = 0;
timeout.tv_nsec = 0;
if (fdptr->read_cb != read_cb)
{
if (read_cb)
EV_SET(&event, fd, EVFILT_READ, EV_ADD, 0, 0, fdptr);
else
EV_SET(&event, fd, EVFILT_READ, EV_DELETE, 0, 0, fdptr);
if (kevent(cupsd_kqueue_fd, &event, 1, NULL, 0, &timeout))
{
cupsdLogMessage(CUPSD_LOG_EMERG, "kevent() returned %s",
strerror(errno));
return (0);
}
}
if (fdptr->write_cb != write_cb)
{
if (write_cb)
EV_SET(&event, fd, EVFILT_WRITE, EV_ADD, 0, 0, fdptr);
else
EV_SET(&event, fd, EVFILT_WRITE, EV_DELETE, 0, 0, fdptr);
if (kevent(cupsd_kqueue_fd, &event, 1, NULL, 0, &timeout))
{
cupsdLogMessage(CUPSD_LOG_EMERG, "kevent() returned %s",
strerror(errno));
return (0);
}
}
}
#elif defined(HAVE_POLL)
# ifdef HAVE_EPOLL
if (cupsd_epoll_fd >= 0)
{
struct epoll_event event;
event.events = 0;
if (read_cb)
event.events |= EPOLLIN;
if (write_cb)
event.events |= EPOLLOUT;
event.data.ptr = fdptr;
if (epoll_ctl(cupsd_epoll_fd, added ? EPOLL_CTL_ADD : EPOLL_CTL_MOD, fd,
&event))
{
close(cupsd_epoll_fd);
cupsd_epoll_fd = -1;
cupsd_update_pollfds = 1;
}
}
else
# endif
cupsd_update_pollfds = 1;
#else
if (read_cb)
FD_SET(fd, &cupsd_global_input);
else
{
FD_CLR(fd, &cupsd_global_input);
FD_CLR(fd, &cupsd_current_input);
}
if (write_cb)
FD_SET(fd, &cupsd_global_output);
else
{
FD_CLR(fd, &cupsd_global_output);
FD_CLR(fd, &cupsd_current_output);
}
#endif
fdptr->read_cb = read_cb;
fdptr->write_cb = write_cb;
fdptr->data = data;
return (1);
}
int
cupsdDoSelect(long timeout)
{
int nfds;
_cupsd_fd_t *fdptr;
#ifdef HAVE_KQUEUE
int i;
struct kevent *event;
struct timespec ktimeout;
cupsd_in_select = 1;
if (timeout >= 0 && timeout < 86400)
{
ktimeout.tv_sec = timeout;
ktimeout.tv_nsec = 0;
nfds = kevent(cupsd_kqueue_fd, NULL, 0, cupsd_kqueue_events, MaxFDs,
&ktimeout);
}
else
nfds = kevent(cupsd_kqueue_fd, NULL, 0, cupsd_kqueue_events, MaxFDs, NULL);
cupsd_kqueue_changes = 0;
for (i = nfds, event = cupsd_kqueue_events; i > 0; i --, event ++)
{
fdptr = (_cupsd_fd_t *)event->udata;
if (cupsArrayFind(cupsd_inactive_fds, fdptr))
continue;
retain_fd(fdptr);
if (fdptr->read_cb && event->filter == EVFILT_READ)
(*(fdptr->read_cb))(fdptr->data);
if (fdptr->use > 1 && fdptr->write_cb && event->filter == EVFILT_WRITE &&
!cupsArrayFind(cupsd_inactive_fds, fdptr))
(*(fdptr->write_cb))(fdptr->data);
release_fd(fdptr);
}
#elif defined(HAVE_POLL)
struct pollfd *pfd;
int count;
# ifdef HAVE_EPOLL
cupsd_in_select = 1;
if (cupsd_epoll_fd >= 0)
{
int i;
struct epoll_event *event;
if (timeout >= 0 && timeout < 86400)
nfds = epoll_wait(cupsd_epoll_fd, cupsd_epoll_events, MaxFDs,
timeout * 1000);
else
nfds = epoll_wait(cupsd_epoll_fd, cupsd_epoll_events, MaxFDs, -1);
if (nfds < 0 && errno != EINTR)
{
close(cupsd_epoll_fd);
cupsd_epoll_fd = -1;
}
else
{
for (i = nfds, event = cupsd_epoll_events; i > 0; i --, event ++)
{
fdptr = (_cupsd_fd_t *)event->data.ptr;
if (cupsArrayFind(cupsd_inactive_fds, fdptr))
continue;
retain_fd(fdptr);
if (fdptr->read_cb && (event->events & (EPOLLIN | EPOLLERR | EPOLLHUP)))
(*(fdptr->read_cb))(fdptr->data);
if (fdptr->use > 1 && fdptr->write_cb &&
(event->events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) &&
!cupsArrayFind(cupsd_inactive_fds, fdptr))
(*(fdptr->write_cb))(fdptr->data);
release_fd(fdptr);
}
goto release_inactive;
}
}
# endif
count = cupsArrayCount(cupsd_fds);
if (cupsd_update_pollfds)
{
cupsd_update_pollfds = 0;
if (count > cupsd_alloc_pollfds)
{
int allocfds = count + 16;
if (cupsd_pollfds)
pfd = realloc(cupsd_pollfds, (size_t)allocfds * sizeof(struct pollfd));
else
pfd = malloc((size_t)allocfds * sizeof(struct pollfd));
if (!pfd)
{
cupsdLogMessage(CUPSD_LOG_EMERG, "Unable to allocate %d bytes for polling.", (int)((size_t)allocfds * sizeof(struct pollfd)));
return (-1);
}
cupsd_pollfds = pfd;
cupsd_alloc_pollfds = allocfds;
}
for (fdptr = (_cupsd_fd_t *)cupsArrayFirst(cupsd_fds), pfd = cupsd_pollfds;
fdptr;
fdptr = (_cupsd_fd_t *)cupsArrayNext(cupsd_fds), pfd ++)
{
pfd->fd = fdptr->fd;
pfd->events = 0;
if (fdptr->read_cb)
pfd->events |= POLLIN;
if (fdptr->write_cb)
pfd->events |= POLLOUT;
}
}
if (timeout >= 0 && timeout < 86400)
nfds = poll(cupsd_pollfds, (nfds_t)count, timeout * 1000);
else
nfds = poll(cupsd_pollfds, (nfds_t)count, -1);
if (nfds > 0)
{
for (pfd = cupsd_pollfds; count > 0; pfd ++, count --)
{
if (!pfd->revents)
continue;
if ((fdptr = find_fd(pfd->fd)) == NULL)
continue;
retain_fd(fdptr);
if (fdptr->read_cb && (pfd->revents & (POLLIN | POLLERR | POLLHUP)))
(*(fdptr->read_cb))(fdptr->data);
if (fdptr->use > 1 && fdptr->write_cb &&
(pfd->revents & (POLLOUT | POLLERR | POLLHUP)))
(*(fdptr->write_cb))(fdptr->data);
release_fd(fdptr);
}
}
#else
struct timeval stimeout;
int maxfd;
if ((fdptr = (_cupsd_fd_t *)cupsArrayLast(cupsd_fds)) == NULL)
maxfd = 1;
else
maxfd = fdptr->fd + 1;
cupsd_current_input = cupsd_global_input;
cupsd_current_output = cupsd_global_output;
if (timeout >= 0 && timeout < 86400)
{
stimeout.tv_sec = timeout;
stimeout.tv_usec = 0;
nfds = select(maxfd, &cupsd_current_input, &cupsd_current_output, NULL,
&stimeout);
}
else
nfds = select(maxfd, &cupsd_current_input, &cupsd_current_output, NULL,
NULL);
if (nfds > 0)
{
for (fdptr = (_cupsd_fd_t *)cupsArrayFirst(cupsd_fds);
fdptr;
fdptr = (_cupsd_fd_t *)cupsArrayNext(cupsd_fds))
{
retain_fd(fdptr);
if (fdptr->read_cb && FD_ISSET(fdptr->fd, &cupsd_current_input))
(*(fdptr->read_cb))(fdptr->data);
if (fdptr->use > 1 && fdptr->write_cb &&
FD_ISSET(fdptr->fd, &cupsd_current_output))
(*(fdptr->write_cb))(fdptr->data);
release_fd(fdptr);
}
}
#endif
#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
# ifndef HAVE_KQUEUE
release_inactive:
# endif
cupsd_in_select = 0;
for (fdptr = (_cupsd_fd_t *)cupsArrayFirst(cupsd_inactive_fds);
fdptr;
fdptr = (_cupsd_fd_t *)cupsArrayNext(cupsd_inactive_fds))
{
cupsArrayRemove(cupsd_inactive_fds, fdptr);
release_fd(fdptr);
}
#endif
return (nfds);
}
#ifdef CUPSD_IS_SELECTING
int
cupsdIsSelecting(int fd)
{
return (find_fd(fd) != NULL);
}
#endif
void
cupsdRemoveSelect(int fd)
{
_cupsd_fd_t *fdptr;
#ifdef HAVE_EPOLL
struct epoll_event event;
#elif defined(HAVE_KQUEUE)
struct kevent event;
struct timespec timeout;
#elif defined(HAVE_POLL)
#endif
cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdRemoveSelect(fd=%d)", fd);
if (fd < 0)
return;
if ((fdptr = find_fd(fd)) == NULL)
return;
#ifdef HAVE_EPOLL
if (epoll_ctl(cupsd_epoll_fd, EPOLL_CTL_DEL, fd, &event))
{
close(cupsd_epoll_fd);
cupsd_epoll_fd = -1;
cupsd_update_pollfds = 1;
}
#elif defined(HAVE_KQUEUE)
timeout.tv_sec = 0;
timeout.tv_nsec = 0;
if (fdptr->read_cb)
{
EV_SET(&event, fd, EVFILT_READ, EV_DELETE, 0, 0, fdptr);
if (kevent(cupsd_kqueue_fd, &event, 1, NULL, 0, &timeout))
{
cupsdLogMessage(CUPSD_LOG_EMERG, "kevent() returned %s",
strerror(errno));
goto cleanup;
}
}
if (fdptr->write_cb)
{
EV_SET(&event, fd, EVFILT_WRITE, EV_DELETE, 0, 0, fdptr);
if (kevent(cupsd_kqueue_fd, &event, 1, NULL, 0, &timeout))
{
cupsdLogMessage(CUPSD_LOG_EMERG, "kevent() returned %s",
strerror(errno));
goto cleanup;
}
}
#elif defined(HAVE_POLL)
cupsd_update_pollfds = 1;
#else
FD_CLR(fd, &cupsd_global_input);
FD_CLR(fd, &cupsd_global_output);
FD_CLR(fd, &cupsd_current_input);
FD_CLR(fd, &cupsd_current_output);
#endif
#ifdef HAVE_KQUEUE
cleanup:
#endif
cupsArrayRemove(cupsd_fds, fdptr);
#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
if (cupsd_in_select)
cupsArrayAdd(cupsd_inactive_fds, fdptr);
else
#endif
release_fd(fdptr);
}
void
cupsdStartSelect(void)
{
cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartSelect()");
cupsd_fds = cupsArrayNew((cups_array_func_t)compare_fds, NULL);
#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
cupsd_inactive_fds = cupsArrayNew((cups_array_func_t)compare_fds, NULL);
#endif
#ifdef HAVE_EPOLL
cupsd_epoll_fd = epoll_create(MaxFDs);
cupsd_epoll_events = calloc((size_t)MaxFDs, sizeof(struct epoll_event));
cupsd_update_pollfds = 0;
#elif defined(HAVE_KQUEUE)
cupsd_kqueue_fd = kqueue();
cupsd_kqueue_changes = 0;
cupsd_kqueue_events = calloc((size_t)MaxFDs, sizeof(struct kevent));
#elif defined(HAVE_POLL)
cupsd_update_pollfds = 0;
#else
FD_ZERO(&cupsd_global_input);
FD_ZERO(&cupsd_global_output);
#endif
}
void
cupsdStopSelect(void)
{
_cupsd_fd_t *fdptr;
cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStopSelect()");
for (fdptr = (_cupsd_fd_t *)cupsArrayFirst(cupsd_fds);
fdptr;
fdptr = (_cupsd_fd_t *)cupsArrayNext(cupsd_fds))
free(fdptr);
cupsArrayDelete(cupsd_fds);
cupsd_fds = NULL;
#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
cupsArrayDelete(cupsd_inactive_fds);
cupsd_inactive_fds = NULL;
#endif
#ifdef HAVE_KQUEUE
if (cupsd_kqueue_events)
{
free(cupsd_kqueue_events);
cupsd_kqueue_events = NULL;
}
if (cupsd_kqueue_fd >= 0)
{
close(cupsd_kqueue_fd);
cupsd_kqueue_fd = -1;
}
cupsd_kqueue_changes = 0;
#elif defined(HAVE_POLL)
# ifdef HAVE_EPOLL
if (cupsd_epoll_events)
{
free(cupsd_epoll_events);
cupsd_epoll_events = NULL;
}
if (cupsd_epoll_fd >= 0)
{
close(cupsd_epoll_fd);
cupsd_epoll_fd = -1;
}
# endif
if (cupsd_pollfds)
{
free(cupsd_pollfds);
cupsd_pollfds = NULL;
cupsd_alloc_pollfds = 0;
}
cupsd_update_pollfds = 0;
#else
FD_ZERO(&cupsd_global_input);
FD_ZERO(&cupsd_global_output);
#endif
}
static int
compare_fds(_cupsd_fd_t *a,
_cupsd_fd_t *b)
{
return (a->fd - b->fd);
}
static _cupsd_fd_t *
find_fd(int fd)
{
_cupsd_fd_t *fdptr,
key;
cupsArraySave(cupsd_fds);
key.fd = fd;
fdptr = (_cupsd_fd_t *)cupsArrayFind(cupsd_fds, &key);
cupsArrayRestore(cupsd_fds);
return (fdptr);
}