]>
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
26 #include <libratbox_config.h>
27 #include <ratbox_lib.h>
28 #include <commio-int.h>
31 #if defined(HAVE_DEVPOLL) && (HAVE_SYS_DEVPOLL_H)
32 #include <sys/devpoll.h>
37 static void devpoll_update_events(rb_fde_t
*, short, PF
*);
38 static void devpoll_write_update(int, int);
42 rb_setup_fd_devpoll(rb_fde_t
*F
)
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 rb_lib_log("devpoll_write_update: dpfd write failed %d: %s", errno
,
74 devpoll_update_events(rb_fde_t
*F
, short filter
, PF
* handler
)
76 int update_required
= 0;
77 int fd
= rb_get_fd(F
);
78 int cur_mask
= fdmask
[fd
];
84 cur_handler
= F
->read_handler
;
86 fdmask
[fd
] |= POLLRDNORM
;
88 fdmask
[fd
] &= ~POLLRDNORM
;
90 fdmask
[fd
] |= POLLWRNORM
;
93 cur_handler
= F
->write_handler
;
95 fdmask
[fd
] |= POLLWRNORM
;
97 fdmask
[fd
] &= ~POLLWRNORM
;
99 fdmask
[fd
] |= POLLRDNORM
;
106 if(cur_handler
== NULL
&& handler
!= NULL
)
108 else if(cur_handler
!= NULL
&& handler
== NULL
)
110 if(cur_mask
!= fdmask
[fd
])
115 * Ok, we can call devpoll_write_update() here now to re-build the
116 * fd struct. If we end up with nothing on this fd, it won't write
121 devpoll_write_update(fd
, POLLREMOVE
);
122 devpoll_write_update(fd
, fdmask
[fd
]);
125 devpoll_write_update(fd
, POLLREMOVE
);
133 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
134 /* Public functions */
140 * This is a needed exported function which will be called to initialise
141 * the network loop code.
144 rb_init_netio_devpoll(void)
146 dpfd
= open("/dev/poll", O_RDWR
);
151 maxfd
= getdtablesize() - 2; /* This makes more sense than HARD_FDLIMIT */
152 fdmask
= rb_malloc(sizeof(fdmask
) * maxfd
+ 1);
153 rb_open(dpfd
, RB_FD_UNKNOWN
, "/dev/poll file descriptor");
160 * This is a needed exported function which will be called to register
161 * and deregister interest in a pending IO state for a given FD.
164 rb_setselect_devpoll(rb_fde_t
*F
, unsigned int type
, PF
* handler
, void *client_data
)
166 lrb_assert(IsFDOpen(F
));
168 if(type
& RB_SELECT_READ
)
170 devpoll_update_events(F
, RB_SELECT_READ
, handler
);
171 F
->read_handler
= handler
;
172 F
->read_data
= client_data
;
174 if(type
& RB_SELECT_WRITE
)
176 devpoll_update_events(F
, RB_SELECT_WRITE
, handler
);
177 F
->write_handler
= handler
;
178 F
->write_data
= client_data
;
183 * Check all connections for new connections and input data that is to be
184 * processed. Also check for connections with data queued and whether we can
191 * Called to do the new-style IO, courtesy of squid (like most of this
192 * new IO code). This routine handles the stuff we've hidden in
193 * rb_setselect and fd_table[] and calls callbacks for IO ready
198 rb_select_devpoll(long delay
)
201 struct pollfd pollfds
[maxfd
];
202 struct dvpoll dopoll
;
208 dopoll
.dp_timeout
= delay
;
209 dopoll
.dp_nfds
= maxfd
;
210 dopoll
.dp_fds
= &pollfds
[0];
211 num
= ioctl(dpfd
, DP_POLL
, &dopoll
);
214 if(rb_ignore_errno(errno
))
224 for(i
= 0; i
< num
; i
++)
226 int fd
= dopoll
.dp_fds
[i
].fd
;
228 rb_fde_t
*F
= rb_find_fd(fd
);
229 if((dopoll
.dp_fds
[i
].revents
& (POLLRDNORM
| POLLIN
| POLLHUP
|
231 && (dopoll
.dp_fds
[i
].events
& (POLLRDNORM
| POLLIN
)))
233 if((hdl
= F
->read_handler
) != NULL
)
235 F
->read_handler
= NULL
;
236 hdl(F
, F
->read_data
);
238 * this call used to be with a NULL pointer, BUT
239 * in the devpoll case we only want to update the
240 * poll set *if* the handler changes state (active ->
241 * NULL or vice versa.)
243 devpoll_update_events(F
, RB_SELECT_READ
, F
->read_handler
);
248 continue; /* Read handler closed us..go on to do something more useful */
249 if((dopoll
.dp_fds
[i
].revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
|
251 && (dopoll
.dp_fds
[i
].events
& (POLLWRNORM
| POLLOUT
)))
253 if((hdl
= F
->write_handler
) != NULL
)
255 F
->write_handler
= NULL
;
256 hdl(F
, F
->write_data
);
257 /* See above similar code in the read case */
258 devpoll_update_events(F
,
259 RB_SELECT_WRITE
, F
->write_handler
);
267 /* XXX Get here, we broke! */
271 #else /* /dev/poll not supported */
273 rb_init_netio_devpoll(void)
279 rb_setselect_devpoll(rb_fde_t
*F
, unsigned int type
, PF
* handler
, void *client_data
)
286 rb_select_devpoll(long delay
)
293 rb_setup_fd_devpoll(rb_fde_t
*F
)