]>
jfr.im git - irc/rqf/shadowircd.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 3354 2007-04-03 09:21:31Z nenolod $
31 #include <sys/devpoll.h>
33 #include "libcharybdis.h"
35 #define POLL_LENGTH 1024
37 static void devpoll_update_events(int, short, PF
*);
39 static short fdmask
[POLL_LENGTH
];
40 static void devpoll_update_events(int, short, PF
*);
41 static void devpoll_write_update(int, int);
43 /* #define NOTYET 1 */
45 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
46 /* Private functions */
49 * Write an update to the devpoll filter.
50 * See, we end up having to do a seperate (?) remove before we do an
51 * add of a new polltype, so we have to have this function seperate from
55 devpoll_write_update(int fd
, int events
)
57 struct pollfd pollfds
[1]; /* Just to be careful */
60 /* Build the pollfd entry */
61 pollfds
[0].revents
= 0;
63 pollfds
[0].events
= events
;
65 /* Write the thing to our poll fd */
66 retval
= write(dpfd
, &pollfds
[0], sizeof(struct pollfd
));
67 if(retval
!= sizeof(struct pollfd
))
68 libcharybdis_log("devpoll_write_update: dpfd write failed %d: %s", errno
, strerror(errno
));
73 devpoll_update_events(int fd
, short filter
, PF
* handler
)
75 int update_required
= 0;
76 int cur_mask
= fdmask
[fd
];
79 fde_t
*F
= comm_locate_fd(fd
);
83 case COMM_SELECT_READ
:
84 cur_handler
= F
->read_handler
;
86 fdmask
[fd
] |= POLLRDNORM
;
88 fdmask
[fd
] &= ~POLLRDNORM
;
90 fdmask
[fd
] |= POLLWRNORM
;
92 case COMM_SELECT_WRITE
:
93 cur_handler
= F
->write_handler
;
95 fdmask
[fd
] |= POLLWRNORM
;
97 fdmask
[fd
] &= ~POLLWRNORM
;
99 fdmask
[fd
] |= POLLRDNORM
;
103 libcharybdis_log("devpoll_update_events called with unknown filter: %hd", filter
);
109 if(cur_handler
== NULL
&& handler
!= NULL
)
111 else if(cur_handler
!= NULL
&& handler
== NULL
)
113 if(cur_mask
!= fdmask
[fd
])
118 * Ok, we can call devpoll_write_update() here now to re-build the
119 * fd struct. If we end up with nothing on this fd, it won't write
124 devpoll_write_update(fd
, POLLREMOVE
);
125 devpoll_write_update(fd
, fdmask
[fd
]);
128 devpoll_write_update(fd
, POLLREMOVE
);
136 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
137 /* Public functions */
143 * This is a needed exported function which will be called to initialise
144 * the network loop code.
149 memset(&fdmask
, 0, sizeof(fdmask
));
150 dpfd
= open("/dev/poll", O_RDWR
);
154 "init_netio: Couldn't open /dev/poll - %d: %s\n",
155 errno
, strerror(errno
));
156 exit(115); /* Whee! */
163 * This is a needed exported function which will be called to register
164 * and deregister interest in a pending IO state for a given FD.
167 comm_setselect(int fd
, fdlist_t list
, unsigned int type
, PF
* handler
,
168 void *client_data
, time_t timeout
)
170 fde_t
*F
= comm_locate_fd(fd
);
172 s_assert(F
->flags
.open
);
174 /* Update the list, even though we're not using it .. */
177 if(type
& COMM_SELECT_READ
)
179 devpoll_update_events(fd
, COMM_SELECT_READ
, handler
);
180 F
->read_handler
= handler
;
181 F
->read_data
= client_data
;
183 if(type
& COMM_SELECT_WRITE
)
185 devpoll_update_events(fd
, COMM_SELECT_WRITE
, handler
);
186 F
->write_handler
= handler
;
187 F
->write_data
= client_data
;
190 F
->timeout
= CurrentTime
+ (timeout
/ 1000);
194 * Check all connections for new connections and input data that is to be
195 * processed. Also check for connections with data queued and whether we can
202 * Called to do the new-style IO, courtesy of squid (like most of this
203 * new IO code). This routine handles the stuff we've hidden in
204 * comm_setselect and fd_table[] and calls callbacks for IO ready
209 comm_select(unsigned long delay
)
212 struct pollfd pollfds
[POLL_LENGTH
];
213 struct dvpoll dopoll
;
219 dopoll
.dp_timeout
= delay
;
220 dopoll
.dp_nfds
= POLL_LENGTH
;
221 dopoll
.dp_fds
= &pollfds
[0];
222 num
= ioctl(dpfd
, DP_POLL
, &dopoll
);
225 if(ignoreErrno(errno
))
235 for (i
= 0; i
< num
; i
++)
237 int fd
= dopoll
.dp_fds
[i
].fd
;
239 fde_t
*F
= comm_locate_fd(fd
);
240 if((dopoll
.dp_fds
[i
].
241 revents
& (POLLRDNORM
| POLLIN
| POLLHUP
|
243 && (dopoll
.dp_fds
[i
].events
& (POLLRDNORM
| POLLIN
)))
245 if((hdl
= F
->read_handler
) != NULL
)
247 F
->read_handler
= NULL
;
248 hdl(fd
, F
->read_data
);
250 * this call used to be with a NULL pointer, BUT
251 * in the devpoll case we only want to update the
252 * poll set *if* the handler changes state (active ->
253 * NULL or vice versa.)
255 devpoll_update_events(fd
,
256 COMM_SELECT_READ
, F
->read_handler
);
259 libcharybdis_log("comm_select: Unhandled read event: fdmask: %x",
263 if(F
->flags
.open
== 0)
264 continue; /* Read handler closed us..go on to do something more useful */
265 if((dopoll
.dp_fds
[i
].
266 revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
|
268 && (dopoll
.dp_fds
[i
].events
& (POLLWRNORM
| POLLOUT
)))
270 if((hdl
= F
->write_handler
) != NULL
)
272 F
->write_handler
= NULL
;
273 hdl(fd
, F
->write_data
);
274 /* See above similar code in the read case */
275 devpoll_update_events(fd
,
276 COMM_SELECT_WRITE
, F
->write_handler
);
279 libcharybdis_log("comm_select: Unhandled write event: fdmask: %x",
283 if(dopoll
.dp_fds
[i
].revents
& POLLNVAL
)
285 libcharybdis_log("revents was Invalid for %d", fd
);
291 /* XXX Get here, we broke! */