| 1 |
adx |
30 |
/* |
| 2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
adx |
30 |
* |
| 4 |
michael |
8279 |
* Copyright (c) 2000-2018 ircd-hybrid development team |
| 5 |
adx |
30 |
* |
| 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 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
adx |
30 |
* USA |
| 20 |
|
|
*/ |
| 21 |
|
|
|
| 22 |
michael |
2916 |
/*! \file s_bsd_kqueue.c |
| 23 |
|
|
* \brief kqueue() compatible network routines. |
| 24 |
|
|
* \version $Id$ |
| 25 |
|
|
*/ |
| 26 |
|
|
|
| 27 |
adx |
30 |
#include "stdinc.h" |
| 28 |
stu |
908 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_KQUEUE |
| 29 |
adx |
30 |
#include <sys/event.h> |
| 30 |
|
|
#include "fdlist.h" |
| 31 |
|
|
#include "ircd.h" |
| 32 |
|
|
#include "s_bsd.h" |
| 33 |
michael |
1309 |
#include "log.h" |
| 34 |
adx |
30 |
|
| 35 |
michael |
6350 |
enum { KE_LENGTH = 128 }; |
| 36 |
adx |
30 |
|
| 37 |
michael |
8339 |
static int kqueue_fd; |
| 38 |
adx |
30 |
static struct kevent kq_fdlist[KE_LENGTH]; /* kevent buffer */ |
| 39 |
|
|
static int kqoff; /* offset into the buffer */ |
| 40 |
|
|
|
| 41 |
michael |
3984 |
|
| 42 |
adx |
30 |
/* |
| 43 |
michael |
7566 |
* netio_init |
| 44 |
adx |
30 |
* |
| 45 |
|
|
* This is a needed exported function which will be called to initialise |
| 46 |
|
|
* the network loop code. |
| 47 |
|
|
*/ |
| 48 |
|
|
void |
| 49 |
michael |
7566 |
netio_init(void) |
| 50 |
adx |
30 |
{ |
| 51 |
michael |
8339 |
if ((kqueue_fd = kqueue()) < 0) |
| 52 |
adx |
30 |
{ |
| 53 |
michael |
7566 |
ilog(LOG_TYPE_IRCD, "netio_init: couldn't open kqueue fd: %s", strerror(errno)); |
| 54 |
michael |
6553 |
exit(EXIT_FAILURE); /* Whee! */ |
| 55 |
adx |
30 |
} |
| 56 |
|
|
|
| 57 |
michael |
8339 |
fd_open(kqueue_fd, 0, "kqueue() file descriptor"); |
| 58 |
adx |
30 |
} |
| 59 |
|
|
|
| 60 |
|
|
/* |
| 61 |
|
|
* Write a single update to the kqueue list. |
| 62 |
|
|
*/ |
| 63 |
|
|
static void |
| 64 |
|
|
kq_update_events(int fd, int filter, int what) |
| 65 |
|
|
{ |
| 66 |
michael |
7760 |
const struct timespec zero_timespec = { .tv_sec = 0, .tv_nsec = 0 }; |
| 67 |
adx |
30 |
struct kevent *kep = kq_fdlist + kqoff; |
| 68 |
|
|
|
| 69 |
|
|
EV_SET(kep, (uintptr_t) fd, (short) filter, what, 0, 0, NULL); |
| 70 |
|
|
|
| 71 |
adx |
842 |
if (++kqoff == KE_LENGTH) |
| 72 |
adx |
30 |
{ |
| 73 |
michael |
2589 |
int i; |
| 74 |
|
|
|
| 75 |
|
|
for (i = 0; i < kqoff; ++i) |
| 76 |
michael |
8339 |
kevent(kqueue_fd, &kq_fdlist[i], 1, NULL, 0, &zero_timespec); |
| 77 |
adx |
30 |
kqoff = 0; |
| 78 |
|
|
} |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
|
/* |
| 82 |
|
|
* comm_setselect |
| 83 |
|
|
* |
| 84 |
|
|
* This is a needed exported function which will be called to register |
| 85 |
|
|
* and deregister interest in a pending IO state for a given FD. |
| 86 |
|
|
*/ |
| 87 |
|
|
void |
| 88 |
michael |
4461 |
comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *), |
| 89 |
michael |
7330 |
void *client_data, uintmax_t timeout) |
| 90 |
adx |
30 |
{ |
| 91 |
|
|
int new_events, diff; |
| 92 |
|
|
|
| 93 |
|
|
if ((type & COMM_SELECT_READ)) |
| 94 |
|
|
{ |
| 95 |
|
|
F->read_handler = handler; |
| 96 |
|
|
F->read_data = client_data; |
| 97 |
|
|
} |
| 98 |
|
|
|
| 99 |
|
|
if ((type & COMM_SELECT_WRITE)) |
| 100 |
|
|
{ |
| 101 |
|
|
F->write_handler = handler; |
| 102 |
|
|
F->write_data = client_data; |
| 103 |
|
|
} |
| 104 |
|
|
|
| 105 |
|
|
new_events = (F->read_handler ? COMM_SELECT_READ : 0) | |
| 106 |
michael |
2650 |
(F->write_handler ? COMM_SELECT_WRITE : 0); |
| 107 |
adx |
30 |
|
| 108 |
|
|
if (timeout != 0) |
| 109 |
michael |
2725 |
{ |
| 110 |
adx |
30 |
F->timeout = CurrentTime + (timeout / 1000); |
| 111 |
michael |
2725 |
F->timeout_handler = handler; |
| 112 |
|
|
F->timeout_data = client_data; |
| 113 |
|
|
} |
| 114 |
adx |
30 |
|
| 115 |
|
|
diff = new_events ^ F->evcache; |
| 116 |
|
|
|
| 117 |
|
|
if ((diff & COMM_SELECT_READ)) |
| 118 |
|
|
kq_update_events(F->fd, EVFILT_READ, |
| 119 |
|
|
(new_events & COMM_SELECT_READ) ? EV_ADD : EV_DELETE); |
| 120 |
|
|
if ((diff & COMM_SELECT_WRITE)) |
| 121 |
|
|
kq_update_events(F->fd, EVFILT_WRITE, |
| 122 |
|
|
(new_events & COMM_SELECT_WRITE) ? EV_ADD : EV_DELETE); |
| 123 |
|
|
|
| 124 |
|
|
F->evcache = new_events; |
| 125 |
|
|
} |
| 126 |
|
|
|
| 127 |
|
|
/* |
| 128 |
|
|
* comm_select |
| 129 |
|
|
* |
| 130 |
|
|
* Called to do the new-style IO, courtesy of squid (like most of this |
| 131 |
|
|
* new IO code). This routine handles the stuff we've hidden in |
| 132 |
|
|
* comm_setselect and fd_table[] and calls callbacks for IO ready |
| 133 |
|
|
* events. |
| 134 |
|
|
*/ |
| 135 |
|
|
void |
| 136 |
|
|
comm_select(void) |
| 137 |
|
|
{ |
| 138 |
|
|
int num, i; |
| 139 |
|
|
static struct kevent ke[KE_LENGTH]; |
| 140 |
|
|
struct timespec poll_time; |
| 141 |
michael |
4461 |
void (*hdl)(fde_t *, void *); |
| 142 |
adx |
30 |
|
| 143 |
|
|
/* |
| 144 |
|
|
* remember we are doing NANOseconds here, not micro/milli. God knows |
| 145 |
|
|
* why jlemon used a timespec, but hey, he wrote the interface, not I |
| 146 |
|
|
* -- Adrian |
| 147 |
|
|
*/ |
| 148 |
|
|
poll_time.tv_sec = 0; |
| 149 |
|
|
poll_time.tv_nsec = SELECT_DELAY * 1000000; |
| 150 |
|
|
num = kevent(kqfd.fd, kq_fdlist, kqoff, ke, KE_LENGTH, &poll_time); |
| 151 |
|
|
kqoff = 0; |
| 152 |
|
|
|
| 153 |
|
|
set_time(); |
| 154 |
|
|
|
| 155 |
|
|
if (num < 0) |
| 156 |
|
|
{ |
| 157 |
michael |
6542 |
const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 }; |
| 158 |
|
|
nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */ |
| 159 |
adx |
30 |
return; |
| 160 |
|
|
} |
| 161 |
|
|
|
| 162 |
|
|
for (i = 0; i < num; i++) |
| 163 |
|
|
{ |
| 164 |
michael |
8341 |
fde_t *F = &fd_table[ke[i].ident]; |
| 165 |
michael |
8339 |
|
| 166 |
|
|
if (F->flags.open == 0 || (ke[i].flags & EV_ERROR)) |
| 167 |
adx |
30 |
continue; |
| 168 |
|
|
|
| 169 |
|
|
if (ke[i].filter == EVFILT_READ) |
| 170 |
michael |
2650 |
{ |
| 171 |
adx |
30 |
if ((hdl = F->read_handler) != NULL) |
| 172 |
|
|
{ |
| 173 |
|
|
F->read_handler = NULL; |
| 174 |
|
|
hdl(F, F->read_data); |
| 175 |
michael |
8339 |
|
| 176 |
|
|
if (F->flags.open == 0) |
| 177 |
michael |
2614 |
continue; |
| 178 |
adx |
30 |
} |
| 179 |
michael |
2650 |
} |
| 180 |
adx |
30 |
|
| 181 |
|
|
if (ke[i].filter == EVFILT_WRITE) |
| 182 |
michael |
2650 |
{ |
| 183 |
adx |
30 |
if ((hdl = F->write_handler) != NULL) |
| 184 |
|
|
{ |
| 185 |
|
|
F->write_handler = NULL; |
| 186 |
|
|
hdl(F, F->write_data); |
| 187 |
michael |
8339 |
|
| 188 |
|
|
if (F->flags.open == 0) |
| 189 |
michael |
2614 |
continue; |
| 190 |
adx |
30 |
} |
| 191 |
michael |
2650 |
} |
| 192 |
adx |
30 |
|
| 193 |
|
|
comm_setselect(F, 0, NULL, NULL, 0); |
| 194 |
|
|
} |
| 195 |
|
|
} |
| 196 |
stu |
908 |
#endif |