1 |
adx |
30 |
/* |
2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
|
|
* s_bsd_devpoll.c: /dev/poll compatible network routines. |
4 |
|
|
* |
5 |
|
|
* Originally by Adrian Chadd <adrian@creative.net.au> |
6 |
|
|
* Copyright (C) 2002 Hybrid Development Team |
7 |
|
|
* |
8 |
|
|
* This program is free software; you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License as published by |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
11 |
|
|
* (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* This program is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
* GNU General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU General Public License |
19 |
|
|
* along with this program; if not, write to the Free Software |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
21 |
|
|
* USA |
22 |
|
|
* |
23 |
knight |
31 |
* $Id$ |
24 |
adx |
30 |
*/ |
25 |
|
|
|
26 |
|
|
#include "stdinc.h" |
27 |
|
|
#include <sys/ioctl.h> |
28 |
|
|
/* HPUX uses devpoll.h and not sys/devpoll.h */ |
29 |
|
|
#ifdef HAVE_DEVPOLL_H |
30 |
|
|
# include <devpoll.h> |
31 |
|
|
#else |
32 |
|
|
# ifdef HAVE_SYS_DEVPOLL_H |
33 |
|
|
# include <sys/devpoll.h> |
34 |
|
|
# else |
35 |
|
|
# error "No devpoll.h found! Try ./configuring and letting the script choose for you." |
36 |
|
|
# endif |
37 |
|
|
#endif |
38 |
|
|
#include "fdlist.h" |
39 |
|
|
#include "ircd.h" |
40 |
|
|
#include "s_bsd.h" |
41 |
|
|
#include "s_log.h" |
42 |
|
|
|
43 |
|
|
static fde_t dpfd; |
44 |
|
|
|
45 |
|
|
/* |
46 |
|
|
* init_netio |
47 |
|
|
* |
48 |
|
|
* This is a needed exported function which will be called to initialise |
49 |
|
|
* the network loop code. |
50 |
|
|
*/ |
51 |
|
|
void |
52 |
|
|
init_netio(void) |
53 |
|
|
{ |
54 |
|
|
int fd; |
55 |
|
|
|
56 |
|
|
if ((fd = open("/dev/poll", O_RDWR)) < 0) |
57 |
|
|
{ |
58 |
|
|
ilog(L_CRIT, "init_netio: Couldn't open /dev/poll - %d: %s", |
59 |
|
|
errno, strerror(errno)); |
60 |
|
|
exit(115); /* Whee! */ |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
fd_open(&dpfd, fd, 0, "/dev/poll file descriptor"); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/* |
67 |
|
|
* Write an update to the devpoll filter. |
68 |
|
|
* See, we end up having to do a seperate (?) remove before we do an |
69 |
|
|
* add of a new polltype, so we have to have this function seperate from |
70 |
|
|
* the others. |
71 |
|
|
*/ |
72 |
|
|
static void |
73 |
|
|
devpoll_write_update(int fd, int events) |
74 |
|
|
{ |
75 |
|
|
struct pollfd pfd; |
76 |
|
|
|
77 |
|
|
/* Build the pollfd entry */ |
78 |
|
|
pfd.revents = 0; |
79 |
|
|
pfd.fd = fd; |
80 |
|
|
pfd.events = events; |
81 |
|
|
|
82 |
|
|
/* Write the thing to our poll fd */ |
83 |
|
|
if (write(dpfd.fd, &pfd, sizeof(pfd)) != sizeof(pfd)) |
84 |
|
|
ilog(L_NOTICE, "devpoll_write_update: dpfd write failed %d: %s", |
85 |
|
|
errno, strerror(errno)); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/* |
89 |
|
|
* comm_setselect |
90 |
|
|
* |
91 |
|
|
* This is a needed exported function which will be called to register |
92 |
|
|
* and deregister interest in a pending IO state for a given FD. |
93 |
|
|
*/ |
94 |
|
|
void |
95 |
|
|
comm_setselect(fde_t *F, unsigned int type, PF *handler, |
96 |
|
|
void *client_data, time_t timeout) |
97 |
|
|
{ |
98 |
|
|
int new_events; |
99 |
|
|
|
100 |
|
|
if ((type & COMM_SELECT_READ)) |
101 |
|
|
{ |
102 |
|
|
F->read_handler = handler; |
103 |
|
|
F->read_data = client_data; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
if ((type & COMM_SELECT_WRITE)) |
107 |
|
|
{ |
108 |
|
|
F->write_handler = handler; |
109 |
|
|
F->write_data = client_data; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
new_events = (F->read_handler ? POLLRDNORM : 0) | |
113 |
|
|
(F->write_handler ? POLLWRNORM : 0); |
114 |
|
|
|
115 |
|
|
if (timeout != 0) |
116 |
|
|
F->timeout = CurrentTime + (timeout / 1000); |
117 |
|
|
|
118 |
|
|
if (new_events != F->evcache) |
119 |
|
|
{ |
120 |
|
|
devpoll_write_update(F->fd, POLLREMOVE); |
121 |
|
|
if ((F->evcache = new_events)) |
122 |
|
|
devpoll_write_update(F->fd, 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 |
|
|
int num, i; |
138 |
|
|
struct pollfd pollfds[128]; |
139 |
|
|
struct dvpoll dopoll; |
140 |
|
|
PF *hdl; |
141 |
|
|
fde_t *F; |
142 |
|
|
|
143 |
|
|
dopoll.dp_timeout = SELECT_DELAY; |
144 |
|
|
dopoll.dp_nfds = 128; |
145 |
|
|
dopoll.dp_fds = &pollfds[0]; |
146 |
|
|
num = ioctl(dpfd.fd, DP_POLL, &dopoll); |
147 |
|
|
|
148 |
|
|
set_time(); |
149 |
|
|
|
150 |
|
|
if (num < 0) |
151 |
|
|
{ |
152 |
|
|
#ifdef HAVE_USLEEP |
153 |
|
|
usleep(50000); /* avoid 99% CPU in comm_select */ |
154 |
|
|
#endif |
155 |
|
|
return; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
for (i = 0; i < num; i++) |
159 |
|
|
{ |
160 |
|
|
F = lookup_fd(dopoll.dp_fds[i].fd); |
161 |
|
|
if (F == NULL || !F->flags.open) |
162 |
|
|
continue; |
163 |
|
|
|
164 |
|
|
if ((dopoll.dp_fds[i].revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR))) |
165 |
|
|
if ((hdl = F->read_handler) != NULL) |
166 |
|
|
{ |
167 |
|
|
F->read_handler = NULL; |
168 |
|
|
hdl(F, F->read_data); |
169 |
|
|
if (!F->flags.open) |
170 |
|
|
continue; |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
if ((dopoll.dp_fds[i].revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR))) |
174 |
|
|
if ((hdl = F->write_handler) != NULL) |
175 |
|
|
{ |
176 |
|
|
F->write_handler = NULL; |
177 |
|
|
hdl(F, F->write_data); |
178 |
|
|
if (!F->flags.open) |
179 |
|
|
continue; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
comm_setselect(F, 0, NULL, NULL, 0); |
183 |
|
|
} |
184 |
|
|
} |