1 /* select filedescriptor handling, taken from:
2 * userspace logging daemon for the iptables ULOG target
3 * of the linux 2.4 netfilter subsystem.
5 * (C) 2000-2009 by Harald Welte <laforge@gnumonks.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <osmocom/select.h>
23 #include <osmocom/linuxlist.h>
24 #include <osmocom/timer.h>
27 static LLIST_HEAD(bsc_fds);
28 static int unregistered_count;
30 int bsc_register_fd(struct bsc_fd *fd)
34 /* make FD nonblocking */
35 flags = fcntl(fd->fd, F_GETFL);
39 flags = fcntl(fd->fd, F_SETFL, flags);
47 llist_add_tail(&fd->list, &bsc_fds);
52 void bsc_unregister_fd(struct bsc_fd *fd)
58 int bsc_select_main(int polling)
60 struct bsc_fd *ufd, *tmp;
61 fd_set readset, writeset, exceptset;
63 struct timeval no_time = {0, 0};
69 /* prepare read and write fdsets */
70 llist_for_each_entry(ufd, &bsc_fds, list) {
71 if (ufd->when & BSC_FD_READ)
72 FD_SET(ufd->fd, &readset);
74 if (ufd->when & BSC_FD_WRITE)
75 FD_SET(ufd->fd, &writeset);
77 if (ufd->when & BSC_FD_EXCEPT)
78 FD_SET(ufd->fd, &exceptset);
85 rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : bsc_nearest_timer());
92 /* call registered callback functions */
94 unregistered_count = 0;
95 llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) {
98 if (FD_ISSET(ufd->fd, &readset)) {
100 FD_CLR(ufd->fd, &readset);
103 if (FD_ISSET(ufd->fd, &writeset)) {
104 flags |= BSC_FD_WRITE;
105 FD_CLR(ufd->fd, &writeset);
108 if (FD_ISSET(ufd->fd, &exceptset)) {
109 flags |= BSC_FD_EXCEPT;
110 FD_CLR(ufd->fd, &exceptset);
117 /* ugly, ugly hack. If more than one filedescriptors were
118 * unregistered, they might have been consecutive and
119 * llist_for_each_entry_safe() is no longer safe */
120 if (unregistered_count > 1)