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