| 1 |
|
/* |
| 2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
< |
* s_bsd_poll.c: POSIX poll() compatible network routines. |
| 2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
|
* |
| 4 |
< |
* Originally by Adrian Chadd <adrian@creative.net.au> |
| 6 |
< |
* Copyright (C) 2002 Hybrid Development Team |
| 4 |
> |
* Copyright (c) 2000-2018 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 |
| 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 |
| 18 |
> |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
|
* USA |
| 20 |
< |
* |
| 21 |
< |
* $Id$ |
| 20 |
> |
*/ |
| 21 |
> |
|
| 22 |
> |
/*! \file s_bsd_poll.c |
| 23 |
> |
* \brief POSIX poll() compatible network routines. |
| 24 |
> |
* \version $Id$ |
| 25 |
|
*/ |
| 26 |
|
|
| 27 |
|
#include "stdinc.h" |
| 28 |
+ |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_POLL |
| 29 |
|
#include <sys/poll.h> |
| 30 |
|
#include "fdlist.h" |
| 31 |
< |
#include "hook.h" |
| 31 |
> |
#include "list.h" |
| 32 |
> |
#include "memory.h" |
| 33 |
|
#include "ircd.h" |
| 34 |
|
#include "s_bsd.h" |
| 35 |
< |
#include "s_log.h" |
| 35 |
> |
#include "log.h" |
| 36 |
|
|
| 37 |
|
/* I hate linux -- adrian */ |
| 38 |
|
#ifndef POLLRDNORM |
| 42 |
|
#define POLLWRNORM POLLOUT |
| 43 |
|
#endif |
| 44 |
|
|
| 45 |
+ |
static int pollfds_size; |
| 46 |
|
static struct pollfd *pollfds; |
| 47 |
< |
static int pollmax = -1; /* highest FD number */ |
| 44 |
< |
static dlink_node *hookptr; |
| 47 |
> |
static int pollnum; |
| 48 |
|
|
| 46 |
– |
/* |
| 47 |
– |
* changing_fdlimit |
| 48 |
– |
* |
| 49 |
– |
* Resize pollfds array if necessary. |
| 50 |
– |
*/ |
| 51 |
– |
static void * |
| 52 |
– |
changing_fdlimit(va_list args) |
| 53 |
– |
{ |
| 54 |
– |
int old_fdlimit = hard_fdlimit; |
| 55 |
– |
|
| 56 |
– |
pass_callback(hookptr, va_arg(args, int)); |
| 57 |
– |
|
| 58 |
– |
if (hard_fdlimit != old_fdlimit) |
| 59 |
– |
pollfds = MyRealloc(pollfds, sizeof(struct pollfd) * hard_fdlimit); |
| 60 |
– |
|
| 61 |
– |
return NULL; |
| 62 |
– |
} |
| 49 |
|
|
| 50 |
|
/* |
| 51 |
< |
* init_netio |
| 51 |
> |
* comm_select_init |
| 52 |
|
* |
| 53 |
|
* This is a needed exported function which will be called to initialise |
| 54 |
|
* the network loop code. |
| 55 |
|
*/ |
| 56 |
|
void |
| 57 |
< |
init_netio(void) |
| 57 |
> |
comm_select_init(void) |
| 58 |
|
{ |
| 59 |
< |
int fd; |
| 60 |
< |
|
| 75 |
< |
pollfds = MyMalloc(sizeof(struct pollfd) * hard_fdlimit); |
| 76 |
< |
|
| 77 |
< |
for (fd = 0; fd < hard_fdlimit; fd++) |
| 78 |
< |
pollfds[fd].fd = -1; |
| 79 |
< |
|
| 80 |
< |
hookptr = install_hook(fdlimit_cb, changing_fdlimit); |
| 81 |
< |
} |
| 82 |
< |
|
| 83 |
< |
/* |
| 84 |
< |
* find a spare slot in the fd list. We can optimise this out later! |
| 85 |
< |
* -- adrian |
| 86 |
< |
*/ |
| 87 |
< |
static inline int |
| 88 |
< |
poll_findslot(void) |
| 89 |
< |
{ |
| 90 |
< |
int i; |
| 91 |
< |
|
| 92 |
< |
for (i = 0; i < hard_fdlimit; i++) |
| 93 |
< |
if (pollfds[i].fd == -1) |
| 94 |
< |
{ |
| 95 |
< |
/* MATCH!!#$*&$ */ |
| 96 |
< |
return i; |
| 97 |
< |
} |
| 98 |
< |
|
| 99 |
< |
assert(1 == 0); |
| 100 |
< |
/* NOTREACHED */ |
| 101 |
< |
return -1; |
| 59 |
> |
pollfds_size = hard_fdlimit; |
| 60 |
> |
pollfds = xcalloc(sizeof(struct pollfd) * pollfds_size); |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
/* |
| 67 |
|
* and deregister interest in a pending IO state for a given FD. |
| 68 |
|
*/ |
| 69 |
|
void |
| 70 |
< |
comm_setselect(fde_t *F, unsigned int type, PF *handler, |
| 71 |
< |
void *client_data, time_t timeout) |
| 72 |
< |
{ |
| 70 |
> |
comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *), |
| 71 |
> |
void *client_data, uintmax_t timeout) |
| 72 |
> |
{ |
| 73 |
|
int new_events; |
| 74 |
|
|
| 75 |
|
if ((type & COMM_SELECT_READ)) |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
new_events = (F->read_handler ? POLLRDNORM : 0) | |
| 88 |
< |
(F->write_handler ? POLLWRNORM : 0); |
| 88 |
> |
(F->write_handler ? POLLWRNORM : 0); |
| 89 |
|
|
| 90 |
< |
if (timeout != 0) |
| 90 |
> |
if (timeout) |
| 91 |
> |
{ |
| 92 |
|
F->timeout = CurrentTime + (timeout / 1000); |
| 93 |
+ |
F->timeout_handler = handler; |
| 94 |
+ |
F->timeout_data = client_data; |
| 95 |
+ |
} |
| 96 |
|
|
| 97 |
|
if (new_events != F->evcache) |
| 98 |
|
{ |
| 99 |
|
if (new_events == 0) |
| 100 |
|
{ |
| 101 |
< |
pollfds[F->comm_index].fd = -1; |
| 102 |
< |
pollfds[F->comm_index].revents = 0; |
| 101 |
> |
if (F->comm_index != pollnum - 1) |
| 102 |
> |
{ |
| 103 |
> |
fde_t *other = &fd_table[pollfds[pollnum - 1].fd]; |
| 104 |
|
|
| 105 |
< |
if (pollmax == F->comm_index) |
| 106 |
< |
while (pollmax >= 0 && pollfds[pollmax].fd == -1) |
| 107 |
< |
pollmax--; |
| 105 |
> |
pollfds[F->comm_index].fd = pollfds[pollnum - 1].fd; |
| 106 |
> |
pollfds[F->comm_index].events = pollfds[pollnum - 1].events; |
| 107 |
> |
pollfds[F->comm_index].revents = pollfds[pollnum - 1].revents; |
| 108 |
> |
|
| 109 |
> |
assert(other); |
| 110 |
> |
other->comm_index = F->comm_index; |
| 111 |
> |
} |
| 112 |
> |
|
| 113 |
> |
F->comm_index = -1; |
| 114 |
> |
--pollnum; |
| 115 |
|
} |
| 116 |
|
else |
| 117 |
|
{ |
| 118 |
|
if (F->evcache == 0) |
| 119 |
|
{ |
| 120 |
< |
F->comm_index = poll_findslot(); |
| 121 |
< |
if (F->comm_index > pollmax) |
| 122 |
< |
pollmax = F->comm_index; |
| 120 |
> |
if (pollnum >= pollfds_size) |
| 121 |
> |
{ |
| 122 |
> |
pollfds_size *= 2; |
| 123 |
> |
pollfds = xrealloc(pollfds, sizeof(struct pollfd) * pollfds_size); |
| 124 |
> |
} |
| 125 |
|
|
| 126 |
< |
pollfds[F->comm_index].fd = F->fd; |
| 126 |
> |
F->comm_index = pollnum++; |
| 127 |
> |
pollfds[F->comm_index].fd = F->fd; |
| 128 |
|
} |
| 129 |
+ |
|
| 130 |
|
pollfds[F->comm_index].events = new_events; |
| 131 |
|
pollfds[F->comm_index].revents = 0; |
| 132 |
|
} |
| 134 |
|
F->evcache = new_events; |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
< |
|
| 137 |
> |
|
| 138 |
|
/* |
| 139 |
|
* comm_select |
| 140 |
|
* |
| 146 |
|
void |
| 147 |
|
comm_select(void) |
| 148 |
|
{ |
| 149 |
< |
int num, ci, revents; |
| 150 |
< |
PF *hdl; |
| 176 |
< |
fde_t *F; |
| 149 |
> |
int num; |
| 150 |
> |
void (*hdl)(fde_t *, void *); |
| 151 |
|
|
| 152 |
< |
/* XXX kill that +1 later ! -- adrian */ |
| 179 |
< |
num = poll(pollfds, pollmax + 1, SELECT_DELAY); |
| 152 |
> |
num = poll(pollfds, pollnum, SELECT_DELAY); |
| 153 |
|
|
| 154 |
|
set_time(); |
| 155 |
|
|
| 156 |
|
if (num < 0) |
| 157 |
|
{ |
| 158 |
< |
#ifdef HAVE_USLEEP |
| 159 |
< |
usleep(50000); /* avoid 99% CPU in comm_select */ |
| 187 |
< |
#endif |
| 158 |
> |
const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 }; |
| 159 |
> |
nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */ |
| 160 |
|
return; |
| 161 |
|
} |
| 162 |
|
|
| 163 |
< |
for (ci = 0; ci <= pollmax && num > 0; ci++) |
| 163 |
> |
for (int ci = 0; ci < pollnum && num > 0; ++ci) |
| 164 |
|
{ |
| 165 |
< |
if ((revents = pollfds[ci].revents) == 0 || pollfds[ci].fd == -1) |
| 165 |
> |
int revents = pollfds[ci].revents; |
| 166 |
> |
if (revents == 0) |
| 167 |
|
continue; |
| 195 |
– |
num--; |
| 168 |
|
|
| 169 |
< |
F = lookup_fd(pollfds[ci].fd); |
| 170 |
< |
if (F == NULL || !F->flags.open) |
| 169 |
> |
--num; |
| 170 |
> |
|
| 171 |
> |
fde_t *F = &fd_table[pollfds[ci].fd]; |
| 172 |
> |
|
| 173 |
> |
if (F->flags.open == false) |
| 174 |
|
continue; |
| 175 |
|
|
| 176 |
|
if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) |
| 177 |
< |
if ((hdl = F->read_handler) != NULL) |
| 177 |
> |
{ |
| 178 |
> |
if ((hdl = F->read_handler)) |
| 179 |
|
{ |
| 180 |
|
F->read_handler = NULL; |
| 181 |
|
hdl(F, F->read_data); |
| 182 |
< |
if (!F->flags.open) |
| 183 |
< |
continue; |
| 182 |
> |
|
| 183 |
> |
if (F->flags.open == false) |
| 184 |
> |
continue; |
| 185 |
|
} |
| 186 |
+ |
} |
| 187 |
|
|
| 188 |
|
if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR)) |
| 189 |
< |
if ((hdl = F->write_handler) != NULL) |
| 189 |
> |
{ |
| 190 |
> |
if ((hdl = F->write_handler)) |
| 191 |
|
{ |
| 192 |
|
F->write_handler = NULL; |
| 193 |
|
hdl(F, F->write_data); |
| 194 |
< |
if (!F->flags.open) |
| 195 |
< |
continue; |
| 194 |
> |
|
| 195 |
> |
if (F->flags.open == false) |
| 196 |
> |
continue; |
| 197 |
|
} |
| 198 |
+ |
} |
| 199 |
|
|
| 200 |
|
comm_setselect(F, 0, NULL, NULL, 0); |
| 201 |
|
} |
| 202 |
|
} |
| 203 |
+ |
#endif |