Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file select.c |
| 2 | * select filedescriptor handling. |
| 3 | * Taken from: |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 4 | * userspace logging daemon for the iptables ULOG target |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 5 | * of the linux 2.4 netfilter subsystem. */ |
| 6 | /* |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 7 | * (C) 2000-2020 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * All Rights Reserverd. |
| 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0+ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9d92f0e | 2010-10-31 13:56:45 +0100 | [diff] [blame] | 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
Jaroslav Škarvada | 2b82c1c | 2015-11-11 16:02:54 +0100 | [diff] [blame] | 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 25 | * MA 02110-1301, USA. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include <fcntl.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 29 | #include <stdio.h> |
Harald Welte | ea91a51 | 2017-07-13 14:28:30 +0200 | [diff] [blame] | 30 | #include <unistd.h> |
Harald Welte | 9e166e8 | 2014-03-10 17:28:33 +0100 | [diff] [blame] | 31 | #include <string.h> |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 32 | #include <stdbool.h> |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 33 | #include <errno.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 34 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 35 | #include <osmocom/core/select.h> |
| 36 | #include <osmocom/core/linuxlist.h> |
| 37 | #include <osmocom/core/timer.h> |
Harald Welte | 52a8375 | 2019-02-21 16:37:10 +0100 | [diff] [blame] | 38 | #include <osmocom/core/logging.h> |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 39 | #include <osmocom/core/talloc.h> |
| 40 | #include <osmocom/core/utils.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 41 | |
Harald Welte | 5484480 | 2010-02-20 22:23:08 +0100 | [diff] [blame] | 42 | #include "../config.h" |
| 43 | |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 44 | #if defined(HAVE_SYS_SELECT_H) && defined(HAVE_POLL_H) |
Harald Welte | 1d604d8 | 2017-05-17 15:32:35 +0100 | [diff] [blame] | 45 | #include <sys/select.h> |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 46 | #include <poll.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 47 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 48 | /*! \addtogroup select |
| 49 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 50 | * select() loop abstraction |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 51 | * |
| 52 | * \file select.c */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 53 | |
Harald Welte | 7a010b1 | 2019-04-06 13:46:40 +0200 | [diff] [blame] | 54 | /* keep a set of file descriptors per-thread, so that each thread can have its own |
| 55 | * distinct set of file descriptors to interact with */ |
| 56 | static __thread int maxfd = 0; |
| 57 | static __thread struct llist_head osmo_fds; /* TLS cannot use LLIST_HEAD() */ |
| 58 | static __thread int unregistered_count; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 59 | |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 60 | #ifndef FORCE_IO_SELECT |
| 61 | struct poll_state { |
| 62 | /* array of pollfd */ |
| 63 | struct pollfd *poll; |
| 64 | /* number of entries in pollfd allocated */ |
| 65 | unsigned int poll_size; |
| 66 | /* number of osmo_fd registered */ |
| 67 | unsigned int num_registered; |
| 68 | }; |
| 69 | static __thread struct poll_state g_poll; |
| 70 | #endif /* FORCE_IO_SELECT */ |
| 71 | |
Harald Welte | 6c0a0e6 | 2017-08-12 11:43:14 +0200 | [diff] [blame] | 72 | /*! Set up an osmo-fd. Will not register it. |
| 73 | * \param[inout] ofd Osmo FD to be set-up |
| 74 | * \param[in] fd OS-level file descriptor number |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 75 | * \param[in] when bit-mask of OSMO_FD_{READ,WRITE,EXECEPT} |
Harald Welte | 6c0a0e6 | 2017-08-12 11:43:14 +0200 | [diff] [blame] | 76 | * \param[in] cb Call-back function to be called |
| 77 | * \param[in] data Private context pointer |
| 78 | * \param[in] priv_nr Private number |
| 79 | */ |
| 80 | void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when, |
| 81 | int (*cb)(struct osmo_fd *fd, unsigned int what), |
| 82 | void *data, unsigned int priv_nr) |
| 83 | { |
| 84 | ofd->fd = fd; |
| 85 | ofd->when = when; |
| 86 | ofd->cb = cb; |
| 87 | ofd->data = data; |
| 88 | ofd->priv_nr = priv_nr; |
| 89 | } |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 90 | |
Harald Welte | 7e65791 | 2020-10-18 22:07:21 +0200 | [diff] [blame] | 91 | /*! Update the 'when' field of osmo_fd. "ofd->when = (ofd->when & when_mask) | when". |
| 92 | * Use this function instead of directly modifying ofd->when, as the latter will be |
| 93 | * removed soon. */ |
| 94 | void osmo_fd_update_when(struct osmo_fd *ofd, unsigned int when_mask, unsigned int when) |
| 95 | { |
| 96 | ofd->when &= when_mask; |
| 97 | ofd->when |= when; |
| 98 | } |
| 99 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 100 | /*! Check if a file descriptor is already registered |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 101 | * \param[in] fd osmocom file descriptor to be checked |
| 102 | * \returns true if registered; otherwise false |
| 103 | */ |
| 104 | bool osmo_fd_is_registered(struct osmo_fd *fd) |
| 105 | { |
| 106 | struct osmo_fd *entry; |
| 107 | llist_for_each_entry(entry, &osmo_fds, list) { |
| 108 | if (entry == fd) { |
| 109 | return true; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 116 | /*! Register a new file descriptor with select loop abstraction |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 117 | * \param[in] fd osmocom file descriptor to be registered |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 118 | * \returns 0 on success; negative in case of error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 119 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 120 | int osmo_fd_register(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 121 | { |
| 122 | int flags; |
| 123 | |
| 124 | /* make FD nonblocking */ |
| 125 | flags = fcntl(fd->fd, F_GETFL); |
| 126 | if (flags < 0) |
| 127 | return flags; |
| 128 | flags |= O_NONBLOCK; |
| 129 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 130 | if (flags < 0) |
| 131 | return flags; |
| 132 | |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 133 | /* set close-on-exec flag */ |
| 134 | flags = fcntl(fd->fd, F_GETFD); |
| 135 | if (flags < 0) |
| 136 | return flags; |
| 137 | flags |= FD_CLOEXEC; |
| 138 | flags = fcntl(fd->fd, F_SETFD, flags); |
| 139 | if (flags < 0) |
| 140 | return flags; |
| 141 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 142 | /* Register FD */ |
| 143 | if (fd->fd > maxfd) |
| 144 | maxfd = fd->fd; |
| 145 | |
Pau Espin Pedrol | d05def0 | 2020-05-09 19:24:21 +0200 | [diff] [blame] | 146 | #ifdef OSMO_FD_CHECK |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 147 | if (osmo_fd_is_registered(fd)) { |
| 148 | fprintf(stderr, "Adding a osmo_fd that is already in the list.\n"); |
| 149 | return 0; |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 150 | } |
| 151 | #endif |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 152 | #ifndef FORCE_IO_SELECT |
| 153 | if (g_poll.num_registered + 1 > g_poll.poll_size) { |
| 154 | struct pollfd *p; |
| 155 | unsigned int new_size = g_poll.poll_size ? g_poll.poll_size * 2 : 1024; |
| 156 | p = talloc_realloc(OTC_GLOBAL, g_poll.poll, struct pollfd, new_size); |
| 157 | if (!p) |
| 158 | return -ENOMEM; |
| 159 | memset(p + g_poll.poll_size, 0, new_size - g_poll.poll_size); |
| 160 | g_poll.poll = p; |
| 161 | g_poll.poll_size = new_size; |
| 162 | } |
| 163 | g_poll.num_registered++; |
| 164 | #endif /* FORCE_IO_SELECT */ |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 165 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 166 | llist_add_tail(&fd->list, &osmo_fds); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 171 | /*! Unregister a file descriptor from select loop abstraction |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 172 | * \param[in] fd osmocom file descriptor to be unregistered |
| 173 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 174 | void osmo_fd_unregister(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 175 | { |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 176 | /* Note: when fd is inside the osmo_fds list (not registered before) |
| 177 | * this function will crash! If in doubt, check file descriptor with |
| 178 | * osmo_fd_is_registered() */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 179 | unregistered_count++; |
| 180 | llist_del(&fd->list); |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 181 | #ifndef FORCE_IO_SELECT |
| 182 | g_poll.num_registered--; |
| 183 | #endif /* FORCE_IO_SELECT */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Harald Welte | ea91a51 | 2017-07-13 14:28:30 +0200 | [diff] [blame] | 186 | /*! Close a file descriptor, mark it as closed + unregister from select loop abstraction |
| 187 | * \param[in] fd osmocom file descriptor to be unregistered + closed |
| 188 | * |
| 189 | * If \a fd is registered, we unregister it from the select() loop |
| 190 | * abstraction. We then close the fd and set it to -1, as well as |
| 191 | * unsetting any 'when' flags */ |
| 192 | void osmo_fd_close(struct osmo_fd *fd) |
| 193 | { |
| 194 | if (osmo_fd_is_registered(fd)) |
| 195 | osmo_fd_unregister(fd); |
| 196 | if (fd->fd != -1) |
| 197 | close(fd->fd); |
| 198 | fd->fd = -1; |
| 199 | fd->when = 0; |
| 200 | } |
| 201 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 202 | /*! Populate the fd_sets and return the highest fd number |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 203 | * \param[in] _rset The readfds to populate |
| 204 | * \param[in] _wset The wrtiefds to populate |
| 205 | * \param[in] _eset The errorfds to populate |
| 206 | * |
| 207 | * \returns The highest file descriptor seen or 0 on an empty list |
| 208 | */ |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 209 | inline int osmo_fd_fill_fds(void *_rset, void *_wset, void *_eset) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 210 | { |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 211 | fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset; |
| 212 | struct osmo_fd *ufd; |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 213 | int highfd = 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 214 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 215 | llist_for_each_entry(ufd, &osmo_fds, list) { |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 216 | if (ufd->when & OSMO_FD_READ) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 217 | FD_SET(ufd->fd, readset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 218 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 219 | if (ufd->when & OSMO_FD_WRITE) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 220 | FD_SET(ufd->fd, writeset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 221 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 222 | if (ufd->when & OSMO_FD_EXCEPT) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 223 | FD_SET(ufd->fd, exceptset); |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 224 | |
| 225 | if (ufd->fd > highfd) |
| 226 | highfd = ufd->fd; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 229 | return highfd; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 230 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 231 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 232 | inline int osmo_fd_disp_fds(void *_rset, void *_wset, void *_eset) |
| 233 | { |
| 234 | struct osmo_fd *ufd, *tmp; |
| 235 | int work = 0; |
| 236 | fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 237 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 238 | restart: |
| 239 | unregistered_count = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 240 | llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 241 | int flags = 0; |
| 242 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 243 | if (FD_ISSET(ufd->fd, readset)) { |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 244 | flags |= OSMO_FD_READ; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 245 | FD_CLR(ufd->fd, readset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 248 | if (FD_ISSET(ufd->fd, writeset)) { |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 249 | flags |= OSMO_FD_WRITE; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 250 | FD_CLR(ufd->fd, writeset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 253 | if (FD_ISSET(ufd->fd, exceptset)) { |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 254 | flags |= OSMO_FD_EXCEPT; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 255 | FD_CLR(ufd->fd, exceptset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | if (flags) { |
| 259 | work = 1; |
Harald Welte | 52a8375 | 2019-02-21 16:37:10 +0100 | [diff] [blame] | 260 | /* make sure to clear any log context before processing the next incoming message |
| 261 | * as part of some file descriptor callback. This effectively prevents "context |
| 262 | * leaking" from processing of one message into processing of the next message as part |
| 263 | * of one iteration through the list of file descriptors here. See OS#3813 */ |
| 264 | log_reset_context(); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 265 | ufd->cb(ufd, flags); |
| 266 | } |
Holger Hans Peter Freyther | 92e1e70 | 2014-04-09 17:24:00 +0200 | [diff] [blame] | 267 | /* ugly, ugly hack. If more than one filedescriptor was |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 268 | * unregistered, they might have been consecutive and |
| 269 | * llist_for_each_entry_safe() is no longer safe */ |
Holger Hans Peter Freyther | 23ba474 | 2010-04-11 17:33:19 +0200 | [diff] [blame] | 270 | /* this seems to happen with the last element of the list as well */ |
| 271 | if (unregistered_count >= 1) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 272 | goto restart; |
| 273 | } |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 274 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 275 | return work; |
| 276 | } |
| 277 | |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 278 | |
| 279 | #ifndef FORCE_IO_SELECT |
| 280 | /* fill g_poll.poll and return the number of entries filled */ |
| 281 | static unsigned int poll_fill_fds(void) |
| 282 | { |
| 283 | struct osmo_fd *ufd; |
| 284 | unsigned int i = 0; |
| 285 | |
| 286 | llist_for_each_entry(ufd, &osmo_fds, list) { |
| 287 | struct pollfd *p; |
| 288 | |
| 289 | if (!ufd->when) |
| 290 | continue; |
| 291 | |
| 292 | p = &g_poll.poll[i++]; |
| 293 | |
| 294 | p->fd = ufd->fd; |
| 295 | p->events = 0; |
| 296 | p->revents = 0; |
| 297 | |
| 298 | /* use the same mapping as the Linux kernel does in fs/select.c */ |
| 299 | if (ufd->when & OSMO_FD_READ) |
| 300 | p->events |= POLLIN | POLLHUP | POLLERR; |
| 301 | |
| 302 | if (ufd->when & OSMO_FD_WRITE) |
| 303 | p->events |= POLLOUT | POLLERR; |
| 304 | |
| 305 | if (ufd->when & OSMO_FD_EXCEPT) |
| 306 | p->events |= POLLPRI; |
| 307 | |
| 308 | } |
| 309 | |
| 310 | return i; |
| 311 | } |
| 312 | |
| 313 | /* iterate over first n_fd entries of g_poll.poll + dispatch */ |
| 314 | static int poll_disp_fds(int n_fd) |
| 315 | { |
| 316 | struct osmo_fd *ufd; |
| 317 | unsigned int i; |
| 318 | int work = 0; |
| 319 | |
| 320 | for (i = 0; i < n_fd; i++) { |
| 321 | struct pollfd *p = &g_poll.poll[i]; |
| 322 | int flags = 0; |
| 323 | |
| 324 | if (!p->revents) |
| 325 | continue; |
| 326 | |
| 327 | ufd = osmo_fd_get_by_fd(p->fd); |
| 328 | if (!ufd) { |
| 329 | /* FD might have been unregistered meanwhile */ |
| 330 | continue; |
| 331 | } |
| 332 | /* use the same mapping as the Linux kernel does in fs/select.c */ |
| 333 | if (p->revents & (POLLIN | POLLHUP | POLLERR)) |
| 334 | flags |= OSMO_FD_READ; |
| 335 | if (p->revents & (POLLOUT | POLLERR)) |
| 336 | flags |= OSMO_FD_WRITE; |
| 337 | if (p->revents & POLLPRI) |
| 338 | flags |= OSMO_FD_EXCEPT; |
| 339 | |
| 340 | /* make sure we never report more than the user requested */ |
| 341 | flags &= ufd->when; |
| 342 | |
| 343 | if (flags) { |
| 344 | work = 1; |
| 345 | /* make sure to clear any log context before processing the next incoming message |
| 346 | * as part of some file descriptor callback. This effectively prevents "context |
| 347 | * leaking" from processing of one message into processing of the next message as part |
| 348 | * of one iteration through the list of file descriptors here. See OS#3813 */ |
| 349 | log_reset_context(); |
| 350 | ufd->cb(ufd, flags); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return work; |
| 355 | } |
| 356 | |
| 357 | static int _osmo_select_main(int polling) |
| 358 | { |
| 359 | unsigned int n_poll; |
| 360 | int rc; |
| 361 | |
| 362 | /* prepare read and write fdsets */ |
| 363 | n_poll = poll_fill_fds(); |
| 364 | |
| 365 | if (!polling) |
| 366 | osmo_timers_prepare(); |
| 367 | |
| 368 | rc = poll(g_poll.poll, n_poll, polling ? 0 : osmo_timers_nearest_ms()); |
| 369 | if (rc < 0) |
| 370 | return 0; |
| 371 | |
| 372 | /* fire timers */ |
| 373 | osmo_timers_update(); |
| 374 | |
| 375 | OSMO_ASSERT(osmo_ctx->select); |
| 376 | |
| 377 | /* call registered callback functions */ |
| 378 | return poll_disp_fds(n_poll); |
| 379 | } |
| 380 | #else /* FORCE_IO_SELECT */ |
| 381 | /* the old implementation based on select, used 2008-2020 */ |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 382 | static int _osmo_select_main(int polling) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 383 | { |
| 384 | fd_set readset, writeset, exceptset; |
| 385 | int rc; |
| 386 | struct timeval no_time = {0, 0}; |
| 387 | |
| 388 | FD_ZERO(&readset); |
| 389 | FD_ZERO(&writeset); |
| 390 | FD_ZERO(&exceptset); |
| 391 | |
| 392 | /* prepare read and write fdsets */ |
| 393 | osmo_fd_fill_fds(&readset, &writeset, &exceptset); |
| 394 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 395 | if (!polling) |
| 396 | osmo_timers_prepare(); |
| 397 | rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest()); |
| 398 | if (rc < 0) |
| 399 | return 0; |
| 400 | |
| 401 | /* fire timers */ |
| 402 | osmo_timers_update(); |
| 403 | |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 404 | OSMO_ASSERT(osmo_ctx->select); |
| 405 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 406 | /* call registered callback functions */ |
| 407 | return osmo_fd_disp_fds(&readset, &writeset, &exceptset); |
| 408 | } |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 409 | #endif /* FORCE_IO_SELECT */ |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 410 | |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 411 | /*! select main loop integration |
| 412 | * \param[in] polling should we pollonly (1) or block on select (0) |
| 413 | * \returns 0 if no fd handled; 1 if fd handled; negative in case of error |
| 414 | */ |
| 415 | int osmo_select_main(int polling) |
| 416 | { |
| 417 | int rc = _osmo_select_main(polling); |
| 418 | #ifndef EMBEDDED |
| 419 | if (talloc_total_size(osmo_ctx->select) != 0) { |
| 420 | osmo_panic("You cannot use the 'select' volatile " |
| 421 | "context if you don't use osmo_select_main_ctx()!\n"); |
| 422 | } |
| 423 | #endif |
| 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | #ifndef EMBEDDED |
| 428 | /*! select main loop integration with temporary select-dispatch talloc context |
| 429 | * \param[in] polling should we pollonly (1) or block on select (0) |
| 430 | * \returns 0 if no fd handled; 1 if fd handled; negative in case of error |
| 431 | */ |
| 432 | int osmo_select_main_ctx(int polling) |
| 433 | { |
| 434 | int rc = _osmo_select_main(polling); |
| 435 | /* free all the children of the volatile 'select' scope context */ |
| 436 | talloc_free_children(osmo_ctx->select); |
| 437 | return rc; |
| 438 | } |
| 439 | #endif |
| 440 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 441 | /*! find an osmo_fd based on the integer fd |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 442 | * \param[in] fd file descriptor to use as search key |
| 443 | * \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] | 444 | struct osmo_fd *osmo_fd_get_by_fd(int fd) |
| 445 | { |
| 446 | struct osmo_fd *ofd; |
| 447 | |
| 448 | llist_for_each_entry(ofd, &osmo_fds, list) { |
| 449 | if (ofd->fd == fd) |
| 450 | return ofd; |
| 451 | } |
| 452 | return NULL; |
| 453 | } |
| 454 | |
Harald Welte | 7a010b1 | 2019-04-06 13:46:40 +0200 | [diff] [blame] | 455 | /*! initialize the osmocom select abstraction for the current thread */ |
| 456 | void osmo_select_init(void) |
| 457 | { |
| 458 | INIT_LLIST_HEAD(&osmo_fds); |
| 459 | } |
| 460 | |
| 461 | /* ensure main thread always has pre-initialized osmo_fds */ |
| 462 | static __attribute__((constructor)) void on_dso_load_select(void) |
| 463 | { |
| 464 | osmo_select_init(); |
| 465 | } |
| 466 | |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 467 | #ifdef HAVE_SYS_TIMERFD_H |
| 468 | #include <sys/timerfd.h> |
| 469 | |
| 470 | /*! disable the osmocom-wrapped timerfd */ |
| 471 | int osmo_timerfd_disable(struct osmo_fd *ofd) |
| 472 | { |
| 473 | const struct itimerspec its_null = { |
| 474 | .it_value = { 0, 0 }, |
| 475 | .it_interval = { 0, 0 }, |
| 476 | }; |
| 477 | return timerfd_settime(ofd->fd, 0, &its_null, NULL); |
| 478 | } |
| 479 | |
Alexander Chemeris | e8ec214 | 2020-05-09 01:57:16 +0300 | [diff] [blame] | 480 | /*! schedule the osmocom-wrapped timerfd to occur first at \a first, then periodically at \a interval |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 481 | * \param[in] ofd Osmocom wrapped timerfd |
| 482 | * \param[in] first Relative time at which the timer should first execute (NULL = \a interval) |
| 483 | * \param[in] interval Time interval at which subsequent timer shall fire |
| 484 | * \returns 0 on success; negative on error */ |
| 485 | int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first, |
| 486 | const struct timespec *interval) |
| 487 | { |
| 488 | struct itimerspec its; |
| 489 | |
| 490 | if (ofd->fd < 0) |
| 491 | return -EINVAL; |
| 492 | |
| 493 | /* first expiration */ |
| 494 | if (first) |
| 495 | its.it_value = *first; |
| 496 | else |
| 497 | its.it_value = *interval; |
| 498 | /* repeating interval */ |
| 499 | its.it_interval = *interval; |
| 500 | |
| 501 | return timerfd_settime(ofd->fd, 0, &its, NULL); |
| 502 | } |
| 503 | |
| 504 | /*! setup osmocom-wrapped timerfd |
| 505 | * \param[inout] ofd Osmocom-wrapped timerfd on which to operate |
| 506 | * \param[in] cb Call-back function called when timerfd becomes readable |
| 507 | * \param[in] data Opaque data to be passed on to call-back |
| 508 | * \returns 0 on success; negative on error |
| 509 | * |
| 510 | * We simply initialize the data structures here, but do not yet |
| 511 | * schedule the timer. |
| 512 | */ |
| 513 | int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data) |
| 514 | { |
| 515 | ofd->cb = cb; |
| 516 | ofd->data = data; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 517 | ofd->when = OSMO_FD_READ; |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 518 | |
| 519 | if (ofd->fd < 0) { |
Harald Welte | 37608f9 | 2018-10-21 12:32:55 +0200 | [diff] [blame] | 520 | int rc; |
| 521 | |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 522 | ofd->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); |
| 523 | if (ofd->fd < 0) |
| 524 | return ofd->fd; |
| 525 | |
Harald Welte | 37608f9 | 2018-10-21 12:32:55 +0200 | [diff] [blame] | 526 | rc = osmo_fd_register(ofd); |
| 527 | if (rc < 0) { |
Harald Welte | 100e44e | 2020-04-18 21:16:43 +0200 | [diff] [blame] | 528 | osmo_fd_unregister(ofd); |
Harald Welte | 37608f9 | 2018-10-21 12:32:55 +0200 | [diff] [blame] | 529 | close(ofd->fd); |
| 530 | ofd->fd = -1; |
| 531 | return rc; |
| 532 | } |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 533 | } |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | #endif /* HAVE_SYS_TIMERFD_H */ |
| 538 | |
Harald Welte | a70ac85 | 2020-04-17 19:20:01 +0200 | [diff] [blame] | 539 | #ifdef HAVE_SYS_SIGNALFD_H |
| 540 | #include <sys/signalfd.h> |
| 541 | |
| 542 | static int signalfd_callback(struct osmo_fd *ofd, unsigned int what) |
| 543 | { |
| 544 | struct osmo_signalfd *osfd = ofd->data; |
| 545 | struct signalfd_siginfo fdsi; |
| 546 | int rc; |
| 547 | |
| 548 | rc = read(ofd->fd, &fdsi, sizeof(fdsi)); |
| 549 | if (rc < 0) { |
| 550 | osmo_fd_unregister(ofd); |
| 551 | close(ofd->fd); |
| 552 | ofd->fd = -1; |
| 553 | return rc; |
| 554 | } |
| 555 | |
| 556 | osfd->cb(osfd, &fdsi); |
| 557 | |
| 558 | return 0; |
| 559 | }; |
| 560 | |
| 561 | /*! create a signalfd and register it with osmocom select loop. |
| 562 | * \param[in] ctx talloc context from which osmo_signalfd is to be allocated |
| 563 | * \param[in] set of signals to be accept via this file descriptor |
| 564 | * \param[in] cb call-back function to be called for each arriving signal |
| 565 | * \param[in] data opaque user-provided data to pass to callback |
| 566 | * \returns pointer to newly-allocated + registered osmo_signalfd; NULL on error */ |
| 567 | struct osmo_signalfd * |
| 568 | osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data) |
| 569 | { |
| 570 | struct osmo_signalfd *osfd = talloc_size(ctx, sizeof(*osfd)); |
| 571 | int fd, rc; |
| 572 | |
| 573 | if (!osfd) |
| 574 | return NULL; |
| 575 | |
| 576 | osfd->data = data; |
| 577 | osfd->sigset = set; |
| 578 | osfd->cb = cb; |
| 579 | |
| 580 | fd = signalfd(-1, &osfd->sigset, SFD_NONBLOCK); |
| 581 | if (fd < 0) { |
| 582 | talloc_free(osfd); |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | osmo_fd_setup(&osfd->ofd, fd, OSMO_FD_READ, signalfd_callback, osfd, 0); |
| 587 | rc = osmo_fd_register(&osfd->ofd); |
| 588 | if (rc < 0) { |
| 589 | close(fd); |
| 590 | talloc_free(osfd); |
| 591 | return NULL; |
| 592 | } |
| 593 | |
| 594 | return osfd; |
| 595 | } |
| 596 | |
| 597 | #endif /* HAVE_SYS_SIGNALFD_H */ |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 598 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 599 | /*! @} */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 600 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 601 | #endif /* _HAVE_SYS_SELECT_H */ |