]>
jfr.im git - irc/rqf/shadowircd.git/blob - libratbox/src/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
25 * $Id: devpoll.c 26092 2008-09-19 15:13:52Z androsyn $
27 #include <libratbox_config.h>
28 #include <ratbox_lib.h>
29 #include <commio-int.h>
32 #if defined(HAVE_DEVPOLL) && (HAVE_DEVPOLL_H)
33 #include <sys/devpoll.h>
36 static void devpoll_update_events(int, short, PF
*);
40 static void devpoll_update_events(int, short, PF
*);
41 static void devpoll_write_update(int, int);
45 rb_setup_fd_devpoll(rb_fde_t
*F
)
52 * Write an update to the devpoll filter.
53 * See, we end up having to do a seperate (?) remove before we do an
54 * add of a new polltype, so we have to have this function seperate from
58 devpoll_write_update(int fd
, int events
)
60 struct pollfd pollfds
[1]; /* Just to be careful */
63 /* Build the pollfd entry */
64 pollfds
[0].revents
= 0;
66 pollfds
[0].events
= events
;
68 /* Write the thing to our poll fd */
69 retval
= write(dpfd
, &pollfds
[0], sizeof(struct pollfd
));
70 if(retval
!= sizeof(struct pollfd
))
71 rb_lib_log("devpoll_write_update: dpfd write failed %d: %s", errno
,
77 devpoll_update_events(int fd
, short filter
, PF
* handler
)
79 int update_required
= 0;
80 int cur_mask
= fdmask
[fd
];
87 cur_handler
= F
->read_handler
;
89 fdmask
[fd
] |= POLLRDNORM
;
91 fdmask
[fd
] &= ~POLLRDNORM
;
93 fdmask
[fd
] |= POLLWRNORM
;
96 cur_handler
= F
->write_handler
;
98 fdmask
[fd
] |= POLLWRNORM
;
100 fdmask
[fd
] &= ~POLLWRNORM
;
102 fdmask
[fd
] |= POLLRDNORM
;
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.
147 rb_init_netio_devpoll(void)
149 dpfd
= open("/dev/poll", O_RDWR
);
154 maxfd
= getdtablesize() - 2; /* This makes more sense than HARD_FDLIMIT */
155 fdmask
= rb_malloc(sizeof(fdmask
) * maxfd
+ 1);
156 rb_open(dpfd
, RB_FD_UNKNOWN
, "/dev/poll file descriptor");
162 * This is a needed exported function which will be called to register
163 * and deregister interest in a pending IO state for a given FD.
166 rb_setselect_devpoll(rb_fde_t
*F
, unsigned int type
, PF
* handler
, void *client_data
)
168 lrb_assert(IsFDOpen(F
));
170 if(type
& RB_SELECT_READ
)
172 devpoll_update_events(fd
, RB_SELECT_READ
, handler
);
173 F
->read_handler
= handler
;
174 F
->read_data
= client_data
;
176 if(type
& RB_SELECT_WRITE
)
178 devpoll_update_events(fd
, RB_SELECT_WRITE
, handler
);
179 F
->write_handler
= handler
;
180 F
->write_data
= client_data
;
185 * Check all connections for new connections and input data that is to be
186 * processed. Also check for connections with data queued and whether we can
193 * Called to do the new-style IO, courtesy of squid (like most of this
194 * new IO code). This routine handles the stuff we've hidden in
195 * rb_setselect and fd_table[] and calls callbacks for IO ready
200 rb_select_devpoll(long delay
)
203 struct pollfd pollfds
[maxfd
];
204 struct dvpoll dopoll
;
210 dopoll
.dp_timeout
= delay
;
211 dopoll
.dp_nfds
= maxfd
;
212 dopoll
.dp_fds
= &pollfds
[0];
213 num
= ioctl(dpfd
, DP_POLL
, &dopoll
);
216 if(rb_ignore_errno(errno
))
226 for(i
= 0; i
< num
; i
++)
228 int fd
= dopoll
.dp_fds
[i
].fd
;
230 rb_fde_t
*F
= rb_find_fd(fd
);
231 if((dopoll
.dp_fds
[i
].revents
& (POLLRDNORM
| POLLIN
| POLLHUP
|
233 && (dopoll
.dp_fds
[i
].events
& (POLLRDNORM
| POLLIN
)))
235 if((hdl
= F
->read_handler
) != NULL
)
237 F
->read_handler
= NULL
;
238 hdl(F
, F
->read_data
);
240 * this call used to be with a NULL pointer, BUT
241 * in the devpoll case we only want to update the
242 * poll set *if* the handler changes state (active ->
243 * NULL or vice versa.)
245 devpoll_update_events(fd
, RB_SELECT_READ
, F
->read_handler
);
250 continue; /* Read handler closed us..go on to do something more useful */
251 if((dopoll
.dp_fds
[i
].revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
|
253 && (dopoll
.dp_fds
[i
].events
& (POLLWRNORM
| POLLOUT
)))
255 if((hdl
= F
->write_handler
) != NULL
)
257 F
->write_handler
= NULL
;
258 hdl(F
, F
->write_data
);
259 /* See above similar code in the read case */
260 devpoll_update_events(fd
,
261 RB_SELECT_WRITE
, F
->write_handler
);
269 /* XXX Get here, we broke! */
273 #else /* /dev/poll not supported */
275 rb_init_netio_devpoll(void)
281 rb_setselect_devpoll(rb_fde_t
*F
, unsigned int type
, PF
* handler
, void *client_data
)
288 rb_select_devpoll(long delay
)
295 rb_setup_fd_devpoll(rb_fde_t
*F
)