2008-04-01 18:52:26 +02:00
|
|
|
/*
|
|
|
|
* ircd-ratbox: A slightly useful ircd.
|
|
|
|
* s_bsd_devpoll.c: /dev/poll compatible network routines.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
|
|
|
|
* Copyright (C) 1996-2002 Hybrid Development Team
|
|
|
|
* Copyright (C) 2001 Adrian Chadd <adrian@creative.net.au>
|
|
|
|
* Copyright (C) 2002-2005 ircd-ratbox development team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA
|
|
|
|
*
|
|
|
|
*/
|
2016-03-24 01:10:44 +01:00
|
|
|
|
2016-07-01 05:04:00 +02:00
|
|
|
#include <rb/rb.h>
|
|
|
|
#include <rb/commio_int.h>
|
2008-04-01 18:52:26 +02:00
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
#if defined(HAVE_DEVPOLL) && (HAVE_SYS_DEVPOLL_H)
|
2008-04-01 18:52:26 +02:00
|
|
|
#include <sys/devpoll.h>
|
|
|
|
|
|
|
|
static int dpfd;
|
|
|
|
static int maxfd;
|
|
|
|
static short *fdmask;
|
2008-12-22 10:49:01 +01:00
|
|
|
static void devpoll_update_events(rb_fde_t *, short, PF *);
|
2008-04-01 18:52:26 +02:00
|
|
|
static void devpoll_write_update(int, int);
|
|
|
|
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
int
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_setup_fd_devpoll(rb_fde_t *F)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
return 0;
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
|
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
/*
|
|
|
|
* Write an update to the devpoll filter.
|
|
|
|
* See, we end up having to do a seperate (?) remove before we do an
|
|
|
|
* add of a new polltype, so we have to have this function seperate from
|
|
|
|
* the others.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
devpoll_write_update(int fd, int events)
|
|
|
|
{
|
|
|
|
struct pollfd pollfds[1]; /* Just to be careful */
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/* Build the pollfd entry */
|
|
|
|
pollfds[0].revents = 0;
|
|
|
|
pollfds[0].fd = fd;
|
|
|
|
pollfds[0].events = events;
|
|
|
|
|
|
|
|
/* Write the thing to our poll fd */
|
|
|
|
retval = write(dpfd, &pollfds[0], sizeof(struct pollfd));
|
|
|
|
if(retval != sizeof(struct pollfd))
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_lib_log("devpoll_write_update: dpfd write failed %d: %s", errno,
|
|
|
|
strerror(errno));
|
2008-04-01 18:52:26 +02:00
|
|
|
/* Done! */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-22 10:49:01 +01:00
|
|
|
devpoll_update_events(rb_fde_t *F, short filter, PF * handler)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
|
|
|
int update_required = 0;
|
2008-12-22 10:49:01 +01:00
|
|
|
int fd = rb_get_fd(F);
|
2008-04-01 18:52:26 +02:00
|
|
|
int cur_mask = fdmask[fd];
|
|
|
|
PF *cur_handler;
|
|
|
|
fdmask[fd] = 0;
|
|
|
|
switch (filter)
|
|
|
|
{
|
|
|
|
case RB_SELECT_READ:
|
|
|
|
cur_handler = F->read_handler;
|
|
|
|
if(handler)
|
|
|
|
fdmask[fd] |= POLLRDNORM;
|
|
|
|
else
|
|
|
|
fdmask[fd] &= ~POLLRDNORM;
|
|
|
|
if(F->write_handler)
|
|
|
|
fdmask[fd] |= POLLWRNORM;
|
|
|
|
break;
|
|
|
|
case RB_SELECT_WRITE:
|
|
|
|
cur_handler = F->write_handler;
|
|
|
|
if(handler)
|
|
|
|
fdmask[fd] |= POLLWRNORM;
|
|
|
|
else
|
|
|
|
fdmask[fd] &= ~POLLWRNORM;
|
|
|
|
if(F->read_handler)
|
|
|
|
fdmask[fd] |= POLLRDNORM;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cur_handler == NULL && handler != NULL)
|
|
|
|
update_required++;
|
|
|
|
else if(cur_handler != NULL && handler == NULL)
|
|
|
|
update_required++;
|
|
|
|
if(cur_mask != fdmask[fd])
|
|
|
|
update_required++;
|
|
|
|
if(update_required)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Ok, we can call devpoll_write_update() here now to re-build the
|
|
|
|
* fd struct. If we end up with nothing on this fd, it won't write
|
|
|
|
* anything.
|
|
|
|
*/
|
|
|
|
if(fdmask[fd])
|
|
|
|
{
|
|
|
|
devpoll_write_update(fd, POLLREMOVE);
|
|
|
|
devpoll_write_update(fd, fdmask[fd]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
devpoll_write_update(fd, POLLREMOVE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
|
|
|
|
/* Public functions */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rb_init_netio
|
|
|
|
*
|
|
|
|
* This is a needed exported function which will be called to initialise
|
|
|
|
* the network loop code.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rb_init_netio_devpoll(void)
|
|
|
|
{
|
|
|
|
dpfd = open("/dev/poll", O_RDWR);
|
|
|
|
if(dpfd < 0)
|
|
|
|
{
|
|
|
|
return errno;
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
maxfd = getdtablesize() - 2; /* This makes more sense than HARD_FDLIMIT */
|
2008-04-01 18:52:26 +02:00
|
|
|
fdmask = rb_malloc(sizeof(fdmask) * maxfd + 1);
|
|
|
|
rb_open(dpfd, RB_FD_UNKNOWN, "/dev/poll file descriptor");
|
2008-12-22 10:49:01 +01:00
|
|
|
return 0;
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rb_setselect
|
|
|
|
*
|
|
|
|
* This is a needed exported function which will be called to register
|
|
|
|
* and deregister interest in a pending IO state for a given FD.
|
|
|
|
*/
|
|
|
|
void
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_setselect_devpoll(rb_fde_t *F, unsigned int type, PF * handler, void *client_data)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
|
|
|
lrb_assert(IsFDOpen(F));
|
|
|
|
|
|
|
|
if(type & RB_SELECT_READ)
|
|
|
|
{
|
2008-12-22 10:49:01 +01:00
|
|
|
devpoll_update_events(F, RB_SELECT_READ, handler);
|
2008-04-01 18:52:26 +02:00
|
|
|
F->read_handler = handler;
|
|
|
|
F->read_data = client_data;
|
|
|
|
}
|
|
|
|
if(type & RB_SELECT_WRITE)
|
|
|
|
{
|
2008-12-22 10:49:01 +01:00
|
|
|
devpoll_update_events(F, RB_SELECT_WRITE, handler);
|
2008-04-01 18:52:26 +02:00
|
|
|
F->write_handler = handler;
|
|
|
|
F->write_data = client_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check all connections for new connections and input data that is to be
|
|
|
|
* processed. Also check for connections with data queued and whether we can
|
|
|
|
* write it out.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rb_select
|
|
|
|
*
|
|
|
|
* Called to do the new-style IO, courtesy of squid (like most of this
|
|
|
|
* new IO code). This routine handles the stuff we've hidden in
|
|
|
|
* rb_setselect and fd_table[] and calls callbacks for IO ready
|
|
|
|
* events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_select_devpoll(long delay)
|
|
|
|
{
|
|
|
|
int num, i;
|
|
|
|
struct pollfd pollfds[maxfd];
|
|
|
|
struct dvpoll dopoll;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
for(;;)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
|
|
|
dopoll.dp_timeout = delay;
|
|
|
|
dopoll.dp_nfds = maxfd;
|
|
|
|
dopoll.dp_fds = &pollfds[0];
|
|
|
|
num = ioctl(dpfd, DP_POLL, &dopoll);
|
|
|
|
if(num >= 0)
|
|
|
|
break;
|
|
|
|
if(rb_ignore_errno(errno))
|
|
|
|
break;
|
|
|
|
rb_set_time();
|
|
|
|
return RB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_set_time();
|
|
|
|
if(num == 0)
|
|
|
|
continue;
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
for(i = 0; i < num; i++)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
|
|
|
int fd = dopoll.dp_fds[i].fd;
|
|
|
|
PF *hdl = NULL;
|
|
|
|
rb_fde_t *F = rb_find_fd(fd);
|
2008-12-03 00:49:39 +01:00
|
|
|
if((dopoll.dp_fds[i].revents & (POLLRDNORM | POLLIN | POLLHUP |
|
|
|
|
POLLERR))
|
2008-04-01 18:52:26 +02:00
|
|
|
&& (dopoll.dp_fds[i].events & (POLLRDNORM | POLLIN)))
|
|
|
|
{
|
|
|
|
if((hdl = F->read_handler) != NULL)
|
|
|
|
{
|
|
|
|
F->read_handler = NULL;
|
|
|
|
hdl(F, F->read_data);
|
|
|
|
/*
|
|
|
|
* this call used to be with a NULL pointer, BUT
|
|
|
|
* in the devpoll case we only want to update the
|
|
|
|
* poll set *if* the handler changes state (active ->
|
|
|
|
* NULL or vice versa.)
|
|
|
|
*/
|
2008-12-22 10:49:01 +01:00
|
|
|
devpoll_update_events(F, RB_SELECT_READ, F->read_handler);
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!IsFDOpen(F))
|
|
|
|
continue; /* Read handler closed us..go on to do something more useful */
|
2008-12-03 00:49:39 +01:00
|
|
|
if((dopoll.dp_fds[i].revents & (POLLWRNORM | POLLOUT | POLLHUP |
|
|
|
|
POLLERR))
|
2008-04-01 18:52:26 +02:00
|
|
|
&& (dopoll.dp_fds[i].events & (POLLWRNORM | POLLOUT)))
|
|
|
|
{
|
|
|
|
if((hdl = F->write_handler) != NULL)
|
|
|
|
{
|
|
|
|
F->write_handler = NULL;
|
|
|
|
hdl(F, F->write_data);
|
|
|
|
/* See above similar code in the read case */
|
2008-12-22 10:49:01 +01:00
|
|
|
devpoll_update_events(F,
|
2008-04-01 18:52:26 +02:00
|
|
|
RB_SELECT_WRITE, F->write_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RB_OK;
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
while(0);
|
2008-04-01 18:52:26 +02:00
|
|
|
/* XXX Get here, we broke! */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* /dev/poll not supported */
|
2008-12-03 00:49:39 +01:00
|
|
|
int
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_init_netio_devpoll(void)
|
|
|
|
{
|
|
|
|
return ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_setselect_devpoll(rb_fde_t *F, unsigned int type, PF * handler, void *client_data)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_select_devpoll(long delay)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_setup_fd_devpoll(rb_fde_t *F)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|