blob: 4b002ae5cf8c31eb984c52016721cacd47c49351 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/* 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
Harald Welte9d92f0e2010-10-31 13:56:45 +01008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
Harald Welteec8b4502010-02-20 20:34:29 +010011 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <fcntl.h>
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080023#include <stdio.h>
24
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010025#include <osmocom/core/select.h>
26#include <osmocom/core/linuxlist.h>
27#include <osmocom/core/timer.h>
Harald Welteec8b4502010-02-20 20:34:29 +010028
Harald Welte54844802010-02-20 22:23:08 +010029#include "../config.h"
30
Harald Welteec8b4502010-02-20 20:34:29 +010031#ifdef HAVE_SYS_SELECT_H
32
33static int maxfd = 0;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020034static LLIST_HEAD(osmo_fds);
Harald Welteec8b4502010-02-20 20:34:29 +010035static int unregistered_count;
36
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020037int osmo_fd_register(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +010038{
39 int flags;
40
41 /* make FD nonblocking */
42 flags = fcntl(fd->fd, F_GETFL);
43 if (flags < 0)
44 return flags;
45 flags |= O_NONBLOCK;
46 flags = fcntl(fd->fd, F_SETFL, flags);
47 if (flags < 0)
48 return flags;
49
Harald Welte32e1f232011-06-26 13:07:18 +020050 /* set close-on-exec flag */
51 flags = fcntl(fd->fd, F_GETFD);
52 if (flags < 0)
53 return flags;
54 flags |= FD_CLOEXEC;
55 flags = fcntl(fd->fd, F_SETFD, flags);
56 if (flags < 0)
57 return flags;
58
Harald Welteec8b4502010-02-20 20:34:29 +010059 /* Register FD */
60 if (fd->fd > maxfd)
61 maxfd = fd->fd;
62
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080063#ifdef BSC_FD_CHECK
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020064 struct osmo_fd *entry;
65 llist_for_each_entry(entry, &osmo_fds, list) {
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080066 if (entry == fd) {
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020067 fprintf(stderr, "Adding a osmo_fd that is already in the list.\n");
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080068 return 0;
69 }
70 }
71#endif
72
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020073 llist_add_tail(&fd->list, &osmo_fds);
Harald Welteec8b4502010-02-20 20:34:29 +010074
75 return 0;
76}
77
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020078void osmo_fd_unregister(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +010079{
80 unregistered_count++;
81 llist_del(&fd->list);
82}
83
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020084int osmo_select_main(int polling)
Harald Welteec8b4502010-02-20 20:34:29 +010085{
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020086 struct osmo_fd *ufd, *tmp;
Harald Welteec8b4502010-02-20 20:34:29 +010087 fd_set readset, writeset, exceptset;
88 int work = 0, rc;
89 struct timeval no_time = {0, 0};
90
91 FD_ZERO(&readset);
92 FD_ZERO(&writeset);
93 FD_ZERO(&exceptset);
94
95 /* prepare read and write fdsets */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020096 llist_for_each_entry(ufd, &osmo_fds, list) {
Harald Welteec8b4502010-02-20 20:34:29 +010097 if (ufd->when & BSC_FD_READ)
98 FD_SET(ufd->fd, &readset);
99
100 if (ufd->when & BSC_FD_WRITE)
101 FD_SET(ufd->fd, &writeset);
102
103 if (ufd->when & BSC_FD_EXCEPT)
104 FD_SET(ufd->fd, &exceptset);
105 }
106
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200107 osmo_timers_check();
Harald Welteec8b4502010-02-20 20:34:29 +0100108
109 if (!polling)
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200110 osmo_timers_prepare();
111 rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest());
Harald Welteec8b4502010-02-20 20:34:29 +0100112 if (rc < 0)
113 return 0;
114
115 /* fire timers */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200116 osmo_timers_update();
Harald Welteec8b4502010-02-20 20:34:29 +0100117
118 /* call registered callback functions */
119restart:
120 unregistered_count = 0;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200121 llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) {
Harald Welteec8b4502010-02-20 20:34:29 +0100122 int flags = 0;
123
124 if (FD_ISSET(ufd->fd, &readset)) {
125 flags |= BSC_FD_READ;
126 FD_CLR(ufd->fd, &readset);
127 }
128
129 if (FD_ISSET(ufd->fd, &writeset)) {
130 flags |= BSC_FD_WRITE;
131 FD_CLR(ufd->fd, &writeset);
132 }
133
134 if (FD_ISSET(ufd->fd, &exceptset)) {
135 flags |= BSC_FD_EXCEPT;
136 FD_CLR(ufd->fd, &exceptset);
137 }
138
139 if (flags) {
140 work = 1;
141 ufd->cb(ufd, flags);
142 }
143 /* ugly, ugly hack. If more than one filedescriptors were
144 * unregistered, they might have been consecutive and
145 * llist_for_each_entry_safe() is no longer safe */
Holger Hans Peter Freyther23ba4742010-04-11 17:33:19 +0200146 /* this seems to happen with the last element of the list as well */
147 if (unregistered_count >= 1)
Harald Welteec8b4502010-02-20 20:34:29 +0100148 goto restart;
149 }
150 return work;
151}
152
153#endif /* _HAVE_SYS_SELECT_H */