blob: 4f5d7ed95bf6c3972979c901f49d0ae88e60b36d [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* select filedescriptor handling, taken from:
2 * userspace logging daemon for the iptables ULOG target
3 * of the linux 2.4 netfilter subsystem.
4 *
Harald Weltedc55db92009-02-03 12:58:59 +00005 * (C) 2000-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte52b1f982008-12-23 20:25:15 +00006 *
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>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010022#include <osmocore/select.h>
23#include <osmocore/linuxlist.h>
24#include <osmocore/timer.h>
Harald Welte52b1f982008-12-23 20:25:15 +000025
Harald Welte77562352010-02-20 20:14:01 +010026#ifdef HAVE_SYS_SELECT_H
27
Harald Welte52b1f982008-12-23 20:25:15 +000028static int maxfd = 0;
29static LLIST_HEAD(bsc_fds);
Harald Welte709f2fd2009-07-28 13:32:00 +020030static int unregistered_count;
Harald Welte52b1f982008-12-23 20:25:15 +000031
32int bsc_register_fd(struct bsc_fd *fd)
33{
34 int flags;
35
36 /* make FD nonblocking */
37 flags = fcntl(fd->fd, F_GETFL);
38 if (flags < 0)
Harald Welte8470bf22008-12-25 23:28:35 +000039 return flags;
Harald Welte52b1f982008-12-23 20:25:15 +000040 flags |= O_NONBLOCK;
41 flags = fcntl(fd->fd, F_SETFL, flags);
42 if (flags < 0)
Harald Welte8470bf22008-12-25 23:28:35 +000043 return flags;
Harald Welte52b1f982008-12-23 20:25:15 +000044
45 /* Register FD */
46 if (fd->fd > maxfd)
47 maxfd = fd->fd;
48
49 llist_add_tail(&fd->list, &bsc_fds);
50
51 return 0;
52}
53
54void bsc_unregister_fd(struct bsc_fd *fd)
55{
Harald Welte709f2fd2009-07-28 13:32:00 +020056 unregistered_count++;
Harald Welte52b1f982008-12-23 20:25:15 +000057 llist_del(&fd->list);
58}
59
Harald Welte04d3c922009-05-23 06:07:04 +000060int bsc_select_main(int polling)
Harald Welte52b1f982008-12-23 20:25:15 +000061{
Holger Freytherc6880a42009-01-02 21:53:34 +000062 struct bsc_fd *ufd, *tmp;
Harald Welte52b1f982008-12-23 20:25:15 +000063 fd_set readset, writeset, exceptset;
Harald Welte04d3c922009-05-23 06:07:04 +000064 int work = 0, rc;
65 struct timeval no_time = {0, 0};
Harald Welte52b1f982008-12-23 20:25:15 +000066
67 FD_ZERO(&readset);
68 FD_ZERO(&writeset);
69 FD_ZERO(&exceptset);
70
71 /* prepare read and write fdsets */
72 llist_for_each_entry(ufd, &bsc_fds, list) {
73 if (ufd->when & BSC_FD_READ)
74 FD_SET(ufd->fd, &readset);
75
76 if (ufd->when & BSC_FD_WRITE)
77 FD_SET(ufd->fd, &writeset);
78
79 if (ufd->when & BSC_FD_EXCEPT)
80 FD_SET(ufd->fd, &exceptset);
81 }
82
Harald Welte (local)fed176a2009-08-14 14:30:00 +020083 bsc_timer_check();
84
Harald Welte04d3c922009-05-23 06:07:04 +000085 if (!polling)
86 bsc_prepare_timers();
87 rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : bsc_nearest_timer());
88 if (rc < 0)
89 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +000090
Holger Freyther5f755982008-12-27 09:42:59 +000091 /* fire timers */
Harald Welteff117a82009-05-23 05:22:08 +000092 bsc_update_timers();
Harald Welte52b1f982008-12-23 20:25:15 +000093
Holger Freyther5f755982008-12-27 09:42:59 +000094 /* call registered callback functions */
Harald Welte709f2fd2009-07-28 13:32:00 +020095restart:
96 unregistered_count = 0;
Holger Freytherc6880a42009-01-02 21:53:34 +000097 llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) {
Harald Weltedc55db92009-02-03 12:58:59 +000098 int flags = 0;
Harald Welte52b1f982008-12-23 20:25:15 +000099
Andreas Eversberg1e1c6aa2010-01-23 10:52:38 +0100100 if (FD_ISSET(ufd->fd, &readset)) {
Harald Weltedc55db92009-02-03 12:58:59 +0000101 flags |= BSC_FD_READ;
Andreas Eversberg1e1c6aa2010-01-23 10:52:38 +0100102 FD_CLR(ufd->fd, &readset);
103 }
Harald Welte52b1f982008-12-23 20:25:15 +0000104
Andreas Eversberg1e1c6aa2010-01-23 10:52:38 +0100105 if (FD_ISSET(ufd->fd, &writeset)) {
Harald Weltedc55db92009-02-03 12:58:59 +0000106 flags |= BSC_FD_WRITE;
Andreas Eversberg1e1c6aa2010-01-23 10:52:38 +0100107 FD_CLR(ufd->fd, &writeset);
108 }
Holger Freyther5f755982008-12-27 09:42:59 +0000109
Andreas Eversberg1e1c6aa2010-01-23 10:52:38 +0100110 if (FD_ISSET(ufd->fd, &exceptset)) {
Harald Weltedc55db92009-02-03 12:58:59 +0000111 flags |= BSC_FD_EXCEPT;
Andreas Eversberg1e1c6aa2010-01-23 10:52:38 +0100112 FD_CLR(ufd->fd, &exceptset);
113 }
Holger Freyther5f755982008-12-27 09:42:59 +0000114
Harald Welte04d3c922009-05-23 06:07:04 +0000115 if (flags) {
116 work = 1;
Harald Weltedc55db92009-02-03 12:58:59 +0000117 ufd->cb(ufd, flags);
Harald Welte04d3c922009-05-23 06:07:04 +0000118 }
Harald Welte709f2fd2009-07-28 13:32:00 +0200119 /* ugly, ugly hack. If more than one filedescriptors were
120 * unregistered, they might have been consecutive and
121 * llist_for_each_entry_safe() is no longer safe */
122 if (unregistered_count > 1)
123 goto restart;
Harald Welte52b1f982008-12-23 20:25:15 +0000124 }
Harald Welte04d3c922009-05-23 06:07:04 +0000125 return work;
Harald Welte52b1f982008-12-23 20:25:15 +0000126}
Harald Welte77562352010-02-20 20:14:01 +0100127
128#endif /* _HAVE_SYS_SELECT_H */