Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* 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 Welte | 9d92f0e | 2010-10-31 13:56:45 +0100 | [diff] [blame] | 8 | * 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 Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 11 | * |
| 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 |
Jaroslav Škarvada | 2b82c1c | 2015-11-11 16:02:54 +0100 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 20 | * MA 02110-1301, USA. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <fcntl.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 24 | #include <stdio.h> |
Harald Welte | 9e166e8 | 2014-03-10 17:28:33 +0100 | [diff] [blame] | 25 | #include <string.h> |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 26 | #include <stdbool.h> |
Harald Welte | 9e166e8 | 2014-03-10 17:28:33 +0100 | [diff] [blame] | 27 | #include <sys/select.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 28 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 29 | #include <osmocom/core/select.h> |
| 30 | #include <osmocom/core/linuxlist.h> |
| 31 | #include <osmocom/core/timer.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 32 | |
Harald Welte | 5484480 | 2010-02-20 22:23:08 +0100 | [diff] [blame] | 33 | #include "../config.h" |
| 34 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | #ifdef HAVE_SYS_SELECT_H |
| 36 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 37 | /*! \addtogroup select |
| 38 | * @{ |
| 39 | */ |
| 40 | |
| 41 | /*! \file select.c |
| 42 | * \brief select loop abstraction |
| 43 | */ |
| 44 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | static int maxfd = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 46 | static LLIST_HEAD(osmo_fds); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 47 | static int unregistered_count; |
| 48 | |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 49 | |
| 50 | /*! \brief Check if a file descriptor is already registered |
| 51 | * \param[in] fd osmocom file descriptor to be checked |
| 52 | * \returns true if registered; otherwise false |
| 53 | */ |
| 54 | bool osmo_fd_is_registered(struct osmo_fd *fd) |
| 55 | { |
| 56 | struct osmo_fd *entry; |
| 57 | llist_for_each_entry(entry, &osmo_fds, list) { |
| 58 | if (entry == fd) { |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 66 | /*! \brief Register a new file descriptor with select loop abstraction |
| 67 | * \param[in] fd osmocom file descriptor to be registered |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 68 | * \returns 0 on success; negative in case of error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 69 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 70 | int osmo_fd_register(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 71 | { |
| 72 | int flags; |
| 73 | |
| 74 | /* make FD nonblocking */ |
| 75 | flags = fcntl(fd->fd, F_GETFL); |
| 76 | if (flags < 0) |
| 77 | return flags; |
| 78 | flags |= O_NONBLOCK; |
| 79 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 80 | if (flags < 0) |
| 81 | return flags; |
| 82 | |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 83 | /* set close-on-exec flag */ |
| 84 | flags = fcntl(fd->fd, F_GETFD); |
| 85 | if (flags < 0) |
| 86 | return flags; |
| 87 | flags |= FD_CLOEXEC; |
| 88 | flags = fcntl(fd->fd, F_SETFD, flags); |
| 89 | if (flags < 0) |
| 90 | return flags; |
| 91 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 92 | /* Register FD */ |
| 93 | if (fd->fd > maxfd) |
| 94 | maxfd = fd->fd; |
| 95 | |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 96 | #ifdef BSC_FD_CHECK |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 97 | if (osmo_fd_is_registered(fd)) { |
| 98 | fprintf(stderr, "Adding a osmo_fd that is already in the list.\n"); |
| 99 | return 0; |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 100 | } |
| 101 | #endif |
| 102 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 103 | llist_add_tail(&fd->list, &osmo_fds); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 108 | /*! \brief Unregister a file descriptor from select loop abstraction |
| 109 | * \param[in] fd osmocom file descriptor to be unregistered |
| 110 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 111 | void osmo_fd_unregister(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 112 | { |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 113 | /* Note: when fd is inside the osmo_fds list (not registered before) |
| 114 | * this function will crash! If in doubt, check file descriptor with |
| 115 | * osmo_fd_is_registered() */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 116 | unregistered_count++; |
| 117 | llist_del(&fd->list); |
| 118 | } |
| 119 | |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 120 | /*! \brief Populate the fd_sets and return the highest fd number |
| 121 | * \param[in] _rset The readfds to populate |
| 122 | * \param[in] _wset The wrtiefds to populate |
| 123 | * \param[in] _eset The errorfds to populate |
| 124 | * |
| 125 | * \returns The highest file descriptor seen or 0 on an empty list |
| 126 | */ |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 127 | inline int osmo_fd_fill_fds(void *_rset, void *_wset, void *_eset) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 128 | { |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 129 | fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset; |
| 130 | struct osmo_fd *ufd; |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 131 | int highfd = 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 132 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 133 | llist_for_each_entry(ufd, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 134 | if (ufd->when & BSC_FD_READ) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 135 | FD_SET(ufd->fd, readset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 136 | |
| 137 | if (ufd->when & BSC_FD_WRITE) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 138 | FD_SET(ufd->fd, writeset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 139 | |
| 140 | if (ufd->when & BSC_FD_EXCEPT) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 141 | FD_SET(ufd->fd, exceptset); |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 142 | |
| 143 | if (ufd->fd > highfd) |
| 144 | highfd = ufd->fd; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 147 | return highfd; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 148 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 149 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 150 | inline int osmo_fd_disp_fds(void *_rset, void *_wset, void *_eset) |
| 151 | { |
| 152 | struct osmo_fd *ufd, *tmp; |
| 153 | int work = 0; |
| 154 | fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 155 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 156 | restart: |
| 157 | unregistered_count = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 158 | llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 159 | int flags = 0; |
| 160 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 161 | if (FD_ISSET(ufd->fd, readset)) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 162 | flags |= BSC_FD_READ; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 163 | FD_CLR(ufd->fd, readset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 166 | if (FD_ISSET(ufd->fd, writeset)) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 167 | flags |= BSC_FD_WRITE; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 168 | FD_CLR(ufd->fd, writeset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 171 | if (FD_ISSET(ufd->fd, exceptset)) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 172 | flags |= BSC_FD_EXCEPT; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 173 | FD_CLR(ufd->fd, exceptset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if (flags) { |
| 177 | work = 1; |
| 178 | ufd->cb(ufd, flags); |
| 179 | } |
Holger Hans Peter Freyther | 92e1e70 | 2014-04-09 17:24:00 +0200 | [diff] [blame] | 180 | /* ugly, ugly hack. If more than one filedescriptor was |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 181 | * unregistered, they might have been consecutive and |
| 182 | * llist_for_each_entry_safe() is no longer safe */ |
Holger Hans Peter Freyther | 23ba474 | 2010-04-11 17:33:19 +0200 | [diff] [blame] | 183 | /* this seems to happen with the last element of the list as well */ |
| 184 | if (unregistered_count >= 1) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 185 | goto restart; |
| 186 | } |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 187 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 188 | return work; |
| 189 | } |
| 190 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 191 | /*! \brief select main loop integration |
| 192 | * \param[in] polling should we pollonly (1) or block on select (0) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 193 | * \returns 0 if no fd handled; 1 if fd handled; negative in case of error |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 194 | */ |
| 195 | int osmo_select_main(int polling) |
| 196 | { |
| 197 | fd_set readset, writeset, exceptset; |
| 198 | int rc; |
| 199 | struct timeval no_time = {0, 0}; |
| 200 | |
| 201 | FD_ZERO(&readset); |
| 202 | FD_ZERO(&writeset); |
| 203 | FD_ZERO(&exceptset); |
| 204 | |
| 205 | /* prepare read and write fdsets */ |
| 206 | osmo_fd_fill_fds(&readset, &writeset, &exceptset); |
| 207 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 208 | if (!polling) |
| 209 | osmo_timers_prepare(); |
| 210 | rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest()); |
| 211 | if (rc < 0) |
| 212 | return 0; |
| 213 | |
| 214 | /* fire timers */ |
| 215 | osmo_timers_update(); |
| 216 | |
| 217 | /* call registered callback functions */ |
| 218 | return osmo_fd_disp_fds(&readset, &writeset, &exceptset); |
| 219 | } |
| 220 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 221 | /*! \brief find an osmo_fd based on the integer fd |
| 222 | * \param[in] fd file descriptor to use as search key |
| 223 | * \returns \ref osmo_fd for \ref fd; NULL in case it doesn't exist */ |
Harald Welte | 6c33ae2 | 2016-03-19 21:17:58 +0100 | [diff] [blame] | 224 | struct osmo_fd *osmo_fd_get_by_fd(int fd) |
| 225 | { |
| 226 | struct osmo_fd *ofd; |
| 227 | |
| 228 | llist_for_each_entry(ofd, &osmo_fds, list) { |
| 229 | if (ofd->fd == fd) |
| 230 | return ofd; |
| 231 | } |
| 232 | return NULL; |
| 233 | } |
| 234 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 235 | /*! @} */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 236 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 237 | #endif /* _HAVE_SYS_SELECT_H */ |