| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 2000-2014 ircd-hybrid development team |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 19 |
* USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/*! \file s_bsd_select.c |
| 23 |
* \brief select() compatible network routines. |
| 24 |
* \version $Id$ |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "stdinc.h" |
| 28 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_SELECT |
| 29 |
#include "list.h" |
| 30 |
#include "fdlist.h" |
| 31 |
#include "ircd.h" |
| 32 |
#include "s_bsd.h" |
| 33 |
#include "log.h" |
| 34 |
|
| 35 |
/* |
| 36 |
* Note that this is only a single list - multiple lists is kinda pointless |
| 37 |
* under select because the list size is a function of the highest FD :-) |
| 38 |
* -- adrian |
| 39 |
*/ |
| 40 |
|
| 41 |
static fd_set select_readfds, tmpreadfds; |
| 42 |
static fd_set select_writefds, tmpwritefds; |
| 43 |
static int highest_fd = -1; |
| 44 |
|
| 45 |
|
| 46 |
/* |
| 47 |
* init_netio |
| 48 |
* |
| 49 |
* This is a needed exported function which will be called to initialise |
| 50 |
* the network loop code. |
| 51 |
*/ |
| 52 |
void |
| 53 |
init_netio(void) |
| 54 |
{ |
| 55 |
FD_ZERO(&select_readfds); |
| 56 |
FD_ZERO(&select_writefds); |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* comm_setselect |
| 61 |
* |
| 62 |
* This is a needed exported function which will be called to register |
| 63 |
* and deregister interest in a pending IO state for a given FD. |
| 64 |
*/ |
| 65 |
void |
| 66 |
comm_setselect(fde_t *F, unsigned int type, PF *handler, |
| 67 |
void *client_data, time_t timeout) |
| 68 |
{ |
| 69 |
int new_events; |
| 70 |
|
| 71 |
if ((type & COMM_SELECT_READ)) |
| 72 |
{ |
| 73 |
F->read_handler = handler; |
| 74 |
F->read_data = client_data; |
| 75 |
} |
| 76 |
|
| 77 |
if ((type & COMM_SELECT_WRITE)) |
| 78 |
{ |
| 79 |
F->write_handler = handler; |
| 80 |
F->write_data = client_data; |
| 81 |
} |
| 82 |
|
| 83 |
new_events = (F->read_handler ? COMM_SELECT_READ : 0) | |
| 84 |
(F->write_handler ? COMM_SELECT_WRITE : 0); |
| 85 |
|
| 86 |
if (timeout != 0) |
| 87 |
{ |
| 88 |
F->timeout = CurrentTime + (timeout / 1000); |
| 89 |
F->timeout_handler = handler; |
| 90 |
F->timeout_data = client_data; |
| 91 |
} |
| 92 |
|
| 93 |
if (new_events != F->evcache) |
| 94 |
{ |
| 95 |
if ((new_events & COMM_SELECT_READ)) |
| 96 |
FD_SET(F->fd, &select_readfds); |
| 97 |
else |
| 98 |
{ |
| 99 |
FD_CLR(F->fd, &select_readfds); |
| 100 |
FD_CLR(F->fd, &tmpreadfds); |
| 101 |
} |
| 102 |
|
| 103 |
if ((new_events & COMM_SELECT_WRITE)) |
| 104 |
FD_SET(F->fd, &select_writefds); |
| 105 |
else |
| 106 |
{ |
| 107 |
FD_CLR(F->fd, &select_writefds); |
| 108 |
FD_CLR(F->fd, &tmpwritefds); |
| 109 |
} |
| 110 |
|
| 111 |
if (new_events == 0) |
| 112 |
{ |
| 113 |
if (highest_fd == F->fd) |
| 114 |
while (highest_fd >= 0 && (FD_ISSET(highest_fd, &select_readfds) || |
| 115 |
FD_ISSET(highest_fd, &select_writefds))) |
| 116 |
highest_fd--; |
| 117 |
} |
| 118 |
else if (F->evcache == 0) |
| 119 |
if (F->fd > highest_fd) |
| 120 |
highest_fd = F->fd; |
| 121 |
|
| 122 |
F->evcache = new_events; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/* |
| 127 |
* comm_select |
| 128 |
* |
| 129 |
* Called to do the new-style IO, courtesy of squid (like most of this |
| 130 |
* new IO code). This routine handles the stuff we've hidden in |
| 131 |
* comm_setselect and fd_table[] and calls callbacks for IO ready |
| 132 |
* events. |
| 133 |
*/ |
| 134 |
void |
| 135 |
comm_select(void) |
| 136 |
{ |
| 137 |
struct timeval to; |
| 138 |
int num, fd; |
| 139 |
fde_t *F; |
| 140 |
PF *hdl; |
| 141 |
|
| 142 |
/* Copy over the read/write sets so we don't have to rebuild em */ |
| 143 |
memcpy(&tmpreadfds, &select_readfds, sizeof(fd_set)); |
| 144 |
memcpy(&tmpwritefds, &select_writefds, sizeof(fd_set)); |
| 145 |
|
| 146 |
to.tv_sec = 0; |
| 147 |
to.tv_usec = SELECT_DELAY * 1000; |
| 148 |
num = select(highest_fd + 1, &tmpreadfds, &tmpwritefds, NULL, &to); |
| 149 |
|
| 150 |
set_time(); |
| 151 |
|
| 152 |
if (num < 0) |
| 153 |
{ |
| 154 |
#ifdef HAVE_USLEEP |
| 155 |
usleep(50000); |
| 156 |
#endif |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
for (fd = 0; fd <= highest_fd && num > 0; fd++) |
| 161 |
{ |
| 162 |
if (FD_ISSET(fd, &tmpreadfds) || FD_ISSET(fd, &tmpwritefds)) |
| 163 |
{ |
| 164 |
num--; |
| 165 |
|
| 166 |
F = lookup_fd(fd); |
| 167 |
if (F == NULL || !F->flags.open) |
| 168 |
continue; |
| 169 |
|
| 170 |
if (FD_ISSET(fd, &tmpreadfds)) |
| 171 |
{ |
| 172 |
if ((hdl = F->read_handler) != NULL) |
| 173 |
{ |
| 174 |
F->read_handler = NULL; |
| 175 |
hdl(F, F->read_data); |
| 176 |
if (!F->flags.open) |
| 177 |
continue; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if (FD_ISSET(fd, &tmpwritefds)) |
| 182 |
{ |
| 183 |
if ((hdl = F->write_handler) != NULL) |
| 184 |
{ |
| 185 |
F->write_handler = NULL; |
| 186 |
hdl(F, F->write_data); |
| 187 |
if (!F->flags.open) |
| 188 |
continue; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
comm_setselect(F, 0, NULL, NULL, 0); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
#endif |