]>
jfr.im git - solanum.git/blob - librb/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
27 #include <librb_config.h>
29 #include <commio-int.h>
32 #if defined(HAVE_DEVPOLL) && (HAVE_SYS_DEVPOLL_H)
33 #include <sys/devpoll.h>
38 static void devpoll_update_events(rb_fde_t
*, short, PF
*);
39 static void devpoll_write_update(int, int);
43 rb_setup_fd_devpoll(rb_fde_t
*F
)
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 rb_lib_log("devpoll_write_update: dpfd write failed %d: %s", errno
,
75 devpoll_update_events(rb_fde_t
*F
, short filter
, PF
* handler
)
77 int update_required
= 0;
78 int fd
= rb_get_fd(F
);
79 int cur_mask
= fdmask
[fd
];
85 cur_handler
= F
->read_handler
;
87 fdmask
[fd
] |= POLLRDNORM
;
89 fdmask
[fd
] &= ~POLLRDNORM
;
91 fdmask
[fd
] |= POLLWRNORM
;
94 cur_handler
= F
->write_handler
;
96 fdmask
[fd
] |= POLLWRNORM
;
98 fdmask
[fd
] &= ~POLLWRNORM
;
100 fdmask
[fd
] |= POLLRDNORM
;
107 if(cur_handler
== NULL
&& handler
!= NULL
)
109 else if(cur_handler
!= NULL
&& handler
== NULL
)
111 if(cur_mask
!= fdmask
[fd
])
116 * Ok, we can call devpoll_write_update() here now to re-build the
117 * fd struct. If we end up with nothing on this fd, it won't write
122 devpoll_write_update(fd
, POLLREMOVE
);
123 devpoll_write_update(fd
, fdmask
[fd
]);
126 devpoll_write_update(fd
, POLLREMOVE
);
134 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
135 /* Public functions */
141 * This is a needed exported function which will be called to initialise
142 * the network loop code.
145 rb_init_netio_devpoll(void)
147 dpfd
= open("/dev/poll", O_RDWR
);
152 maxfd
= getdtablesize() - 2; /* This makes more sense than HARD_FDLIMIT */
153 fdmask
= rb_malloc(sizeof(fdmask
) * maxfd
+ 1);
154 rb_open(dpfd
, RB_FD_UNKNOWN
, "/dev/poll file descriptor");
161 * This is a needed exported function which will be called to register
162 * and deregister interest in a pending IO state for a given FD.
165 rb_setselect_devpoll(rb_fde_t
*F
, unsigned int type
, PF
* handler
, void *client_data
)
167 lrb_assert(IsFDOpen(F
));
169 if(type
& RB_SELECT_READ
)
171 devpoll_update_events(F
, RB_SELECT_READ
, handler
);
172 F
->read_handler
= handler
;
173 F
->read_data
= client_data
;
175 if(type
& RB_SELECT_WRITE
)
177 devpoll_update_events(F
, RB_SELECT_WRITE
, handler
);
178 F
->write_handler
= handler
;
179 F
->write_data
= client_data
;
184 * Check all connections for new connections and input data that is to be
185 * processed. Also check for connections with data queued and whether we can
192 * Called to do the new-style IO, courtesy of squid (like most of this
193 * new IO code). This routine handles the stuff we've hidden in
194 * rb_setselect and fd_table[] and calls callbacks for IO ready
199 rb_select_devpoll(long delay
)
202 struct pollfd pollfds
[maxfd
];
203 struct dvpoll dopoll
;
209 dopoll
.dp_timeout
= delay
;
210 dopoll
.dp_nfds
= maxfd
;
211 dopoll
.dp_fds
= &pollfds
[0];
212 num
= ioctl(dpfd
, DP_POLL
, &dopoll
);
215 if(rb_ignore_errno(errno
))
225 for(i
= 0; i
< num
; i
++)
227 int fd
= dopoll
.dp_fds
[i
].fd
;
229 rb_fde_t
*F
= rb_find_fd(fd
);
230 if((dopoll
.dp_fds
[i
].revents
& (POLLRDNORM
| POLLIN
| POLLHUP
|
232 && (dopoll
.dp_fds
[i
].events
& (POLLRDNORM
| POLLIN
)))
234 if((hdl
= F
->read_handler
) != NULL
)
236 F
->read_handler
= NULL
;
237 hdl(F
, F
->read_data
);
239 * this call used to be with a NULL pointer, BUT
240 * in the devpoll case we only want to update the
241 * poll set *if* the handler changes state (active ->
242 * NULL or vice versa.)
244 devpoll_update_events(F
, RB_SELECT_READ
, F
->read_handler
);
249 continue; /* Read handler closed us..go on to do something more useful */
250 if((dopoll
.dp_fds
[i
].revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
|
252 && (dopoll
.dp_fds
[i
].events
& (POLLWRNORM
| POLLOUT
)))
254 if((hdl
= F
->write_handler
) != NULL
)
256 F
->write_handler
= NULL
;
257 hdl(F
, F
->write_data
);
258 /* See above similar code in the read case */
259 devpoll_update_events(F
,
260 RB_SELECT_WRITE
, F
->write_handler
);
268 /* XXX Get here, we broke! */
272 #else /* /dev/poll not supported */
274 rb_init_netio_devpoll(void)
280 rb_setselect_devpoll(rb_fde_t
*F
, unsigned int type
, PF
* handler
, void *client_data
)
287 rb_select_devpoll(long delay
)
294 rb_setup_fd_devpoll(rb_fde_t
*F
)