]>
jfr.im git - solanum.git/blob - libcharybdis/devpoll.c
2 * ircd-ratbox: A slightly useful ircd.
3 * s_bsd_devpoll.c: /dev/poll compatible network routines.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2001 Adrian Chadd <adrian@creative.net.au>
8 * Copyright (C) 2002-2005 ircd-ratbox development team
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * $Id: devpoll.c 3229 2007-03-05 17:23:07Z nenolod $
31 #include <sys/devpoll.h>
33 #include "libcharybdis.h"
35 #define POLL_LENGTH HARD_FDLIMIT
38 static void devpoll_update_events(int, short, PF
*);
40 static short fdmask
[POLL_LENGTH
];
41 static void devpoll_update_events(int, short, PF
*);
42 static void devpoll_write_update(int, int);
44 /* #define NOTYET 1 */
46 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
47 /* Private functions */
50 * Write an update to the devpoll filter.
51 * See, we end up having to do a seperate (?) remove before we do an
52 * add of a new polltype, so we have to have this function seperate from
56 devpoll_write_update(int fd
, int events
)
58 struct pollfd pollfds
[1]; /* Just to be careful */
61 /* Build the pollfd entry */
62 pollfds
[0].revents
= 0;
64 pollfds
[0].events
= events
;
66 /* Write the thing to our poll fd */
67 retval
= write(dpfd
, &pollfds
[0], sizeof(struct pollfd
));
68 if(retval
!= sizeof(struct pollfd
))
69 libcharybdis_log("devpoll_write_update: dpfd write failed %d: %s", errno
, strerror(errno
));
74 devpoll_update_events(int fd
, short filter
, PF
* handler
)
76 int update_required
= 0;
77 int cur_mask
= fdmask
[fd
];
80 fde_t
*F
= comm_locate_fd(fd
);
84 case COMM_SELECT_READ
:
85 cur_handler
= F
->read_handler
;
87 fdmask
[fd
] |= POLLRDNORM
;
89 fdmask
[fd
] &= ~POLLRDNORM
;
91 fdmask
[fd
] |= POLLWRNORM
;
93 case COMM_SELECT_WRITE
:
94 cur_handler
= F
->write_handler
;
96 fdmask
[fd
] |= POLLWRNORM
;
98 fdmask
[fd
] &= ~POLLWRNORM
;
100 fdmask
[fd
] |= POLLRDNORM
;
104 libcharybdis_log("devpoll_update_events called with unknown filter: %hd", filter
);
110 if(cur_handler
== NULL
&& handler
!= NULL
)
112 else if(cur_handler
!= NULL
&& handler
== NULL
)
114 if(cur_mask
!= fdmask
[fd
])
119 * Ok, we can call devpoll_write_update() here now to re-build the
120 * fd struct. If we end up with nothing on this fd, it won't write
125 devpoll_write_update(fd
, POLLREMOVE
);
126 devpoll_write_update(fd
, fdmask
[fd
]);
129 devpoll_write_update(fd
, POLLREMOVE
);
137 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
138 /* Public functions */
144 * This is a needed exported function which will be called to initialise
145 * the network loop code.
150 memset(&fdmask
, 0, sizeof(fdmask
));
151 dpfd
= open("/dev/poll", O_RDWR
);
155 "init_netio: Couldn't open /dev/poll - %d: %s\n",
156 errno
, strerror(errno
));
157 exit(115); /* Whee! */
164 * This is a needed exported function which will be called to register
165 * and deregister interest in a pending IO state for a given FD.
168 comm_setselect(int fd
, fdlist_t list
, unsigned int type
, PF
* handler
,
169 void *client_data
, time_t timeout
)
171 fde_t
*F
= comm_locate_fd(fd
);
173 s_assert(F
->flags
.open
);
175 /* Update the list, even though we're not using it .. */
178 if(type
& COMM_SELECT_READ
)
180 devpoll_update_events(fd
, COMM_SELECT_READ
, handler
);
181 F
->read_handler
= handler
;
182 F
->read_data
= client_data
;
184 if(type
& COMM_SELECT_WRITE
)
186 devpoll_update_events(fd
, COMM_SELECT_WRITE
, handler
);
187 F
->write_handler
= handler
;
188 F
->write_data
= client_data
;
191 F
->timeout
= CurrentTime
+ (timeout
/ 1000);
195 * Check all connections for new connections and input data that is to be
196 * processed. Also check for connections with data queued and whether we can
203 * Called to do the new-style IO, courtesy of squid (like most of this
204 * new IO code). This routine handles the stuff we've hidden in
205 * comm_setselect and fd_table[] and calls callbacks for IO ready
210 comm_select(unsigned long delay
)
213 struct pollfd pollfds
[POLL_LENGTH
];
214 struct dvpoll dopoll
;
220 dopoll
.dp_timeout
= delay
;
221 dopoll
.dp_nfds
= POLL_LENGTH
;
222 dopoll
.dp_fds
= &pollfds
[0];
223 num
= ioctl(dpfd
, DP_POLL
, &dopoll
);
226 if(ignoreErrno(errno
))
236 for (i
= 0; i
< num
; i
++)
238 int fd
= dopoll
.dp_fds
[i
].fd
;
240 fde_t
*F
= comm_locate_fd(fd
);
241 if((dopoll
.dp_fds
[i
].
242 revents
& (POLLRDNORM
| POLLIN
| POLLHUP
|
244 && (dopoll
.dp_fds
[i
].events
& (POLLRDNORM
| POLLIN
)))
246 if((hdl
= F
->read_handler
) != NULL
)
248 F
->read_handler
= NULL
;
249 hdl(fd
, F
->read_data
);
251 * this call used to be with a NULL pointer, BUT
252 * in the devpoll case we only want to update the
253 * poll set *if* the handler changes state (active ->
254 * NULL or vice versa.)
256 devpoll_update_events(fd
,
257 COMM_SELECT_READ
, F
->read_handler
);
260 libcharybdis_log("comm_select: Unhandled read event: fdmask: %x",
264 if(F
->flags
.open
== 0)
265 continue; /* Read handler closed us..go on to do something more useful */
266 if((dopoll
.dp_fds
[i
].
267 revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
|
269 && (dopoll
.dp_fds
[i
].events
& (POLLWRNORM
| POLLOUT
)))
271 if((hdl
= F
->write_handler
) != NULL
)
273 F
->write_handler
= NULL
;
274 hdl(fd
, F
->write_data
);
275 /* See above similar code in the read case */
276 devpoll_update_events(fd
,
277 COMM_SELECT_WRITE
, F
->write_handler
);
280 libcharybdis_log("comm_select: Unhandled write event: fdmask: %x",
284 if(dopoll
.dp_fds
[i
].revents
& POLLNVAL
)
286 libcharybdis_log("revents was Invalid for %d", fd
);
292 /* XXX Get here, we broke! */