blob: bed96498cc00ae7736405e32fe08ad2a77885339 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* select filedescriptor handling, taken from:
2 * userspace logging daemon for the iptables ULOG target
3 * of the linux 2.4 netfilter subsystem.
4 *
5 * (C) 2000-2009 by Harald Welte <laforge@gnumonks.org>
6 *
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
10 *
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.
15 *
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
19 */
20
21#include <fcntl.h>
22#include <openbsc/select.h>
23#include <openbsc/linuxlist.h>
24#include <openbsc/timer.h>
25
26static int maxfd = 0;
27static LLIST_HEAD(bsc_fds);
Harald Welteb01974f2009-07-28 13:32:00 +020028static int unregistered_count;
Harald Welte59b04682009-06-10 05:40:52 +080029
30int bsc_register_fd(struct bsc_fd *fd)
31{
32 int flags;
33
34 /* make FD nonblocking */
35 flags = fcntl(fd->fd, F_GETFL);
36 if (flags < 0)
37 return flags;
38 flags |= O_NONBLOCK;
39 flags = fcntl(fd->fd, F_SETFL, flags);
40 if (flags < 0)
41 return flags;
42
43 /* Register FD */
44 if (fd->fd > maxfd)
45 maxfd = fd->fd;
46
47 llist_add_tail(&fd->list, &bsc_fds);
48
49 return 0;
50}
51
52void bsc_unregister_fd(struct bsc_fd *fd)
53{
Harald Welteb01974f2009-07-28 13:32:00 +020054 unregistered_count++;
Harald Welte59b04682009-06-10 05:40:52 +080055 llist_del(&fd->list);
56}
57
58int bsc_select_main(int polling)
59{
60 struct bsc_fd *ufd, *tmp;
61 fd_set readset, writeset, exceptset;
62 int work = 0, rc;
63 struct timeval no_time = {0, 0};
64
65 FD_ZERO(&readset);
66 FD_ZERO(&writeset);
67 FD_ZERO(&exceptset);
68
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);
73
74 if (ufd->when & BSC_FD_WRITE)
75 FD_SET(ufd->fd, &writeset);
76
77 if (ufd->when & BSC_FD_EXCEPT)
78 FD_SET(ufd->fd, &exceptset);
79 }
80
Harald Welte (local)f91516e2009-08-14 14:30:00 +020081 bsc_timer_check();
82
Harald Welte59b04682009-06-10 05:40:52 +080083 if (!polling)
84 bsc_prepare_timers();
85 rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : bsc_nearest_timer());
86 if (rc < 0)
87 return 0;
88
89 /* fire timers */
90 bsc_update_timers();
91
92 /* call registered callback functions */
Harald Welteb01974f2009-07-28 13:32:00 +020093restart:
94 unregistered_count = 0;
Harald Welte59b04682009-06-10 05:40:52 +080095 llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) {
96 int flags = 0;
97
Andreas Eversbergf0cc5e32010-01-23 10:52:38 +010098 if (FD_ISSET(ufd->fd, &readset)) {
Harald Welte59b04682009-06-10 05:40:52 +080099 flags |= BSC_FD_READ;
Andreas Eversbergf0cc5e32010-01-23 10:52:38 +0100100 FD_CLR(ufd->fd, &readset);
101 }
Harald Welte59b04682009-06-10 05:40:52 +0800102
Andreas Eversbergf0cc5e32010-01-23 10:52:38 +0100103 if (FD_ISSET(ufd->fd, &writeset)) {
Harald Welte59b04682009-06-10 05:40:52 +0800104 flags |= BSC_FD_WRITE;
Andreas Eversbergf0cc5e32010-01-23 10:52:38 +0100105 FD_CLR(ufd->fd, &writeset);
106 }
Harald Welte59b04682009-06-10 05:40:52 +0800107
Andreas Eversbergf0cc5e32010-01-23 10:52:38 +0100108 if (FD_ISSET(ufd->fd, &exceptset)) {
Harald Welte59b04682009-06-10 05:40:52 +0800109 flags |= BSC_FD_EXCEPT;
Andreas Eversbergf0cc5e32010-01-23 10:52:38 +0100110 FD_CLR(ufd->fd, &exceptset);
111 }
Harald Welte59b04682009-06-10 05:40:52 +0800112
113 if (flags) {
114 work = 1;
115 ufd->cb(ufd, flags);
116 }
Harald Welteb01974f2009-07-28 13:32:00 +0200117 /* 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)
121 goto restart;
Harald Welte59b04682009-06-10 05:40:52 +0800122 }
123 return work;
124}