blob: b0e8b0cec2a2b7f9195037d7b94894246df906a1 [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>
Harald Welte9e166e82014-03-10 17:28:33 +010024#include <string.h>
25#include <sys/select.h>
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080026
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010027#include <osmocom/core/select.h>
28#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/timer.h>
Harald Welteec8b4502010-02-20 20:34:29 +010030
Harald Welte54844802010-02-20 22:23:08 +010031#include "../config.h"
32
Harald Welteec8b4502010-02-20 20:34:29 +010033#ifdef HAVE_SYS_SELECT_H
34
Harald Welteba6988b2011-08-17 12:46:48 +020035/*! \addtogroup select
36 * @{
37 */
38
39/*! \file select.c
40 * \brief select loop abstraction
41 */
42
Harald Welteec8b4502010-02-20 20:34:29 +010043static int maxfd = 0;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020044static LLIST_HEAD(osmo_fds);
Harald Welteec8b4502010-02-20 20:34:29 +010045static int unregistered_count;
46
Harald Welteba6988b2011-08-17 12:46:48 +020047/*! \brief Register a new file descriptor with select loop abstraction
48 * \param[in] fd osmocom file descriptor to be registered
49 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020050int osmo_fd_register(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +010051{
52 int flags;
53
54 /* make FD nonblocking */
55 flags = fcntl(fd->fd, F_GETFL);
56 if (flags < 0)
57 return flags;
58 flags |= O_NONBLOCK;
59 flags = fcntl(fd->fd, F_SETFL, flags);
60 if (flags < 0)
61 return flags;
62
Harald Welte32e1f232011-06-26 13:07:18 +020063 /* set close-on-exec flag */
64 flags = fcntl(fd->fd, F_GETFD);
65 if (flags < 0)
66 return flags;
67 flags |= FD_CLOEXEC;
68 flags = fcntl(fd->fd, F_SETFD, flags);
69 if (flags < 0)
70 return flags;
71
Harald Welteec8b4502010-02-20 20:34:29 +010072 /* Register FD */
73 if (fd->fd > maxfd)
74 maxfd = fd->fd;
75
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080076#ifdef BSC_FD_CHECK
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020077 struct osmo_fd *entry;
78 llist_for_each_entry(entry, &osmo_fds, list) {
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080079 if (entry == fd) {
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020080 fprintf(stderr, "Adding a osmo_fd that is already in the list.\n");
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080081 return 0;
82 }
83 }
84#endif
85
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020086 llist_add_tail(&fd->list, &osmo_fds);
Harald Welteec8b4502010-02-20 20:34:29 +010087
88 return 0;
89}
90
Harald Welteba6988b2011-08-17 12:46:48 +020091/*! \brief Unregister a file descriptor from select loop abstraction
92 * \param[in] fd osmocom file descriptor to be unregistered
93 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020094void osmo_fd_unregister(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +010095{
96 unregistered_count++;
97 llist_del(&fd->list);
98}
99
Harald Welteba6988b2011-08-17 12:46:48 +0200100/*! \brief select main loop integration
101 * \param[in] polling should we pollonly (1) or block on select (0)
102 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200103int osmo_select_main(int polling)
Harald Welteec8b4502010-02-20 20:34:29 +0100104{
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200105 struct osmo_fd *ufd, *tmp;
Harald Welteec8b4502010-02-20 20:34:29 +0100106 fd_set readset, writeset, exceptset;
107 int work = 0, rc;
108 struct timeval no_time = {0, 0};
109
110 FD_ZERO(&readset);
111 FD_ZERO(&writeset);
112 FD_ZERO(&exceptset);
113
114 /* prepare read and write fdsets */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200115 llist_for_each_entry(ufd, &osmo_fds, list) {
Harald Welteec8b4502010-02-20 20:34:29 +0100116 if (ufd->when & BSC_FD_READ)
117 FD_SET(ufd->fd, &readset);
118
119 if (ufd->when & BSC_FD_WRITE)
120 FD_SET(ufd->fd, &writeset);
121
122 if (ufd->when & BSC_FD_EXCEPT)
123 FD_SET(ufd->fd, &exceptset);
124 }
125
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200126 osmo_timers_check();
Harald Welteec8b4502010-02-20 20:34:29 +0100127
128 if (!polling)
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200129 osmo_timers_prepare();
130 rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest());
Harald Welteec8b4502010-02-20 20:34:29 +0100131 if (rc < 0)
132 return 0;
133
134 /* fire timers */
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200135 osmo_timers_update();
Harald Welteec8b4502010-02-20 20:34:29 +0100136
137 /* call registered callback functions */
138restart:
139 unregistered_count = 0;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200140 llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) {
Harald Welteec8b4502010-02-20 20:34:29 +0100141 int flags = 0;
142
143 if (FD_ISSET(ufd->fd, &readset)) {
144 flags |= BSC_FD_READ;
145 FD_CLR(ufd->fd, &readset);
146 }
147
148 if (FD_ISSET(ufd->fd, &writeset)) {
149 flags |= BSC_FD_WRITE;
150 FD_CLR(ufd->fd, &writeset);
151 }
152
153 if (FD_ISSET(ufd->fd, &exceptset)) {
154 flags |= BSC_FD_EXCEPT;
155 FD_CLR(ufd->fd, &exceptset);
156 }
157
158 if (flags) {
159 work = 1;
160 ufd->cb(ufd, flags);
161 }
Holger Hans Peter Freyther92e1e702014-04-09 17:24:00 +0200162 /* ugly, ugly hack. If more than one filedescriptor was
Harald Welteec8b4502010-02-20 20:34:29 +0100163 * unregistered, they might have been consecutive and
164 * llist_for_each_entry_safe() is no longer safe */
Holger Hans Peter Freyther23ba4742010-04-11 17:33:19 +0200165 /* this seems to happen with the last element of the list as well */
166 if (unregistered_count >= 1)
Harald Welteec8b4502010-02-20 20:34:29 +0100167 goto restart;
168 }
169 return work;
170}
171
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200172/*! @} */
Harald Welteba6988b2011-08-17 12:46:48 +0200173
Harald Welteec8b4502010-02-20 20:34:29 +0100174#endif /* _HAVE_SYS_SELECT_H */