1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2001-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 |
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file s_bsd_devpoll.c |
23 |
* \brief /dev/poll compatible network routines. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_DEVPOLL |
29 |
#include <sys/ioctl.h> |
30 |
/* HPUX uses devpoll.h and not sys/devpoll.h */ |
31 |
#ifdef HAVE_DEVPOLL_H |
32 |
# include <devpoll.h> |
33 |
#else |
34 |
# ifdef HAVE_SYS_DEVPOLL_H |
35 |
# include <sys/devpoll.h> |
36 |
# else |
37 |
# error "No devpoll.h found! Try ./configuring and letting the script choose for you." |
38 |
# endif |
39 |
#endif |
40 |
#include "fdlist.h" |
41 |
#include "ircd.h" |
42 |
#include "s_bsd.h" |
43 |
#include "log.h" |
44 |
|
45 |
static int devpoll_fd; |
46 |
|
47 |
/* |
48 |
* comm_select_init |
49 |
* |
50 |
* This is a needed exported function which will be called to initialise |
51 |
* the network loop code. |
52 |
*/ |
53 |
void |
54 |
comm_select_init(void) |
55 |
{ |
56 |
if ((devpoll_fd = open("/dev/poll", O_RDWR)) < 0) |
57 |
{ |
58 |
ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open /dev/poll: %s", |
59 |
strerror(errno)); |
60 |
exit(EXIT_FAILURE); /* Whee! */ |
61 |
} |
62 |
|
63 |
fd_open(devpoll_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(devpoll_fd, &pfd, sizeof(pfd)) != sizeof(pfd)) |
84 |
ilog(LOG_TYPE_IRCD, "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, void (*handler)(fde_t *, void *), |
96 |
void *client_data, uintmax_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 ? POLLIN : 0) | |
113 |
(F->write_handler ? POLLOUT : 0); |
114 |
|
115 |
if (timeout) |
116 |
{ |
117 |
F->timeout = CurrentTime + (timeout / 1000); |
118 |
F->timeout_handler = handler; |
119 |
F->timeout_data = client_data; |
120 |
} |
121 |
|
122 |
if (new_events != F->evcache) |
123 |
{ |
124 |
devpoll_write_update(F->fd, POLLREMOVE); |
125 |
if ((F->evcache = new_events)) |
126 |
devpoll_write_update(F->fd, new_events); |
127 |
} |
128 |
} |
129 |
|
130 |
/* |
131 |
* comm_select |
132 |
* |
133 |
* Called to do the new-style IO, courtesy of squid (like most of this |
134 |
* new IO code). This routine handles the stuff we've hidden in |
135 |
* comm_setselect and fd_table[] and calls callbacks for IO ready |
136 |
* events. |
137 |
*/ |
138 |
void |
139 |
comm_select(void) |
140 |
{ |
141 |
int num; |
142 |
struct pollfd pollfds[128]; |
143 |
struct dvpoll dopoll; |
144 |
void (*hdl)(fde_t *, void *); |
145 |
|
146 |
dopoll.dp_timeout = SELECT_DELAY; |
147 |
dopoll.dp_nfds = 128; |
148 |
dopoll.dp_fds = &pollfds[0]; |
149 |
num = ioctl(devpoll_fd, DP_POLL, &dopoll); |
150 |
|
151 |
set_time(); |
152 |
|
153 |
if (num < 0) |
154 |
{ |
155 |
const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 }; |
156 |
nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */ |
157 |
return; |
158 |
} |
159 |
|
160 |
for (int i = 0; i < num; ++i) |
161 |
{ |
162 |
fde_t *F = &fd_table[dopoll.dp_fds[i].fd]; |
163 |
|
164 |
if (F->flags.open == 0) |
165 |
continue; |
166 |
|
167 |
if ((dopoll.dp_fds[i].revents & POLLIN)) |
168 |
{ |
169 |
if ((hdl = F->read_handler)) |
170 |
{ |
171 |
F->read_handler = NULL; |
172 |
hdl(F, F->read_data); |
173 |
|
174 |
if (F->flags.open == 0) |
175 |
continue; |
176 |
} |
177 |
} |
178 |
|
179 |
if ((dopoll.dp_fds[i].revents & POLLOUT)) |
180 |
{ |
181 |
if ((hdl = F->write_handler)) |
182 |
{ |
183 |
F->write_handler = NULL; |
184 |
hdl(F, F->write_data); |
185 |
|
186 |
if (F->flags.open == 0) |
187 |
continue; |
188 |
} |
189 |
} |
190 |
|
191 |
comm_setselect(F, 0, NULL, NULL, 0); |
192 |
} |
193 |
} |
194 |
#endif |