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
24 #include <osmocore/select.h>
25 #include <osmocore/linuxlist.h>
26 #include <osmocore/timer.h>
28 #include "../config.h"
30 #ifdef HAVE_SYS_SELECT_H
33 static LLIST_HEAD(bsc_fds);
34 static int unregistered_count;
36 int bsc_register_fd(struct bsc_fd *fd)
40 /* make FD nonblocking */
41 flags = fcntl(fd->fd, F_GETFL);
45 flags = fcntl(fd->fd, F_SETFL, flags);
55 llist_for_each_entry(entry, &bsc_fds, list) {
57 fprintf(stderr, "Adding a bsc_fd that is already in the list.\n");
63 llist_add_tail(&fd->list, &bsc_fds);
68 void bsc_unregister_fd(struct bsc_fd *fd)
74 int bsc_select_main(int polling)
76 struct bsc_fd *ufd, *tmp;
77 fd_set readset, writeset, exceptset;
79 struct timeval no_time = {0, 0};
85 /* prepare read and write fdsets */
86 llist_for_each_entry(ufd, &bsc_fds, list) {
87 if (ufd->when & BSC_FD_READ)
88 FD_SET(ufd->fd, &readset);
90 if (ufd->when & BSC_FD_WRITE)
91 FD_SET(ufd->fd, &writeset);
93 if (ufd->when & BSC_FD_EXCEPT)
94 FD_SET(ufd->fd, &exceptset);
100 bsc_prepare_timers();
101 rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : bsc_nearest_timer());
108 /* call registered callback functions */
110 unregistered_count = 0;
111 llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) {
114 if (FD_ISSET(ufd->fd, &readset)) {
115 flags |= BSC_FD_READ;
116 FD_CLR(ufd->fd, &readset);
119 if (FD_ISSET(ufd->fd, &writeset)) {
120 flags |= BSC_FD_WRITE;
121 FD_CLR(ufd->fd, &writeset);
124 if (FD_ISSET(ufd->fd, &exceptset)) {
125 flags |= BSC_FD_EXCEPT;
126 FD_CLR(ufd->fd, &exceptset);
133 /* ugly, ugly hack. If more than one filedescriptors were
134 * unregistered, they might have been consecutive and
135 * llist_for_each_entry_safe() is no longer safe */
136 /* this seems to happen with the last element of the list as well */
137 if (unregistered_count >= 1)
143 #endif /* _HAVE_SYS_SELECT_H */