blob: 70047f099bd0444068bfd507135541ddde91e57d [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file select.c
2 * select filedescriptor handling.
3 * Taken from:
Harald Welteec8b4502010-02-20 20:34:29 +01004 * userspace logging daemon for the iptables ULOG target
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02005 * of the linux 2.4 netfilter subsystem. */
6/*
Harald Welteb904e422020-10-18 21:24:13 +02007 * (C) 2000-2020 by Harald Welte <laforge@gnumonks.org>
Harald Welte6dda35a2022-11-09 16:54:50 +01008 * All Rights Reserved.
Harald Weltee08da972017-11-13 01:00:26 +09009 *
10 * SPDX-License-Identifier: GPL-2.0+
Harald Welteec8b4502010-02-20 20:34:29 +010011 *
12 * This program is free software; you can redistribute it and/or modify
Harald Welte9d92f0e2010-10-31 13:56:45 +010013 * 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 Welteec8b4502010-02-20 20:34:29 +010016 *
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 Welteec8b4502010-02-20 20:34:29 +010021 */
22
23#include <fcntl.h>
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080024#include <stdio.h>
Harald Welteea91a512017-07-13 14:28:30 +020025#include <unistd.h>
Harald Welte9e166e82014-03-10 17:28:33 +010026#include <string.h>
Philipp Maierb2888532016-12-09 14:07:18 +010027#include <stdbool.h>
Harald Welteea4d8932017-12-03 16:13:39 +010028#include <errno.h>
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080029
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010030#include <osmocom/core/select.h>
31#include <osmocom/core/linuxlist.h>
32#include <osmocom/core/timer.h>
Harald Welte52a83752019-02-21 16:37:10 +010033#include <osmocom/core/logging.h>
Harald Welte2d906112019-03-18 17:17:43 +010034#include <osmocom/core/talloc.h>
35#include <osmocom/core/utils.h>
Philipp Maierb1ef8f52021-12-06 16:31:02 +010036#include <osmocom/core/stat_item.h>
37#include <osmocom/core/stats_tcp.h>
Harald Welteec8b4502010-02-20 20:34:29 +010038
Pau Espin Pedrol88955fb2023-01-18 18:54:00 +010039#include "config.h"
Harald Welte54844802010-02-20 22:23:08 +010040
Harald Welteb904e422020-10-18 21:24:13 +020041#if defined(HAVE_SYS_SELECT_H) && defined(HAVE_POLL_H)
Harald Welte1d604d82017-05-17 15:32:35 +010042#include <sys/select.h>
Harald Welteb904e422020-10-18 21:24:13 +020043#include <poll.h>
Harald Welteec8b4502010-02-20 20:34:29 +010044
Harald Welteba6988b2011-08-17 12:46:48 +020045/*! \addtogroup select
46 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047 * select() loop abstraction
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020048 *
49 * \file select.c */
Harald Welteba6988b2011-08-17 12:46:48 +020050
Harald Welte7a010b12019-04-06 13:46:40 +020051/* 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 */
53static __thread int maxfd = 0;
54static __thread struct llist_head osmo_fds; /* TLS cannot use LLIST_HEAD() */
55static __thread int unregistered_count;
Harald Welteec8b4502010-02-20 20:34:29 +010056
Pau Espin Pedrolc46a15d2023-03-09 17:37:15 +010057/* Array of struct osmo_fd * (size "max_fd") ordered by "ofd->fd" */
58static __thread struct {
59 struct osmo_fd **table;
60 unsigned int size;
61} osmo_fd_lookup;
62
63static void osmo_fd_lookup_table_extend(unsigned int new_max_fd)
64{
65 unsigned int min_new_size;
66 unsigned int pw2;
67 unsigned int new_size;
68
69 /* First calculate the minimally required new size of the array in bytes: */
70 min_new_size = (new_max_fd + 1) * sizeof(struct osmo_fd *);
71 /* Now find the lower power of two of min_new_size (in bytes): */
72 pw2 = 32 - __builtin_clz(min_new_size);
73 /* get next (upper side) power of 2: */
74 pw2++;
75 OSMO_ASSERT(pw2 <= 31); /* Avoid shifting more than 31 bits */
76 new_size = 1 << (pw2 + 1);
77
78 /* Let's make it at least 1024B, to avoid reallocating quickly at startup */
79 if (new_size < 1024)
80 new_size = 1024;
81 if (new_size > osmo_fd_lookup.size) {
82 uint8_t *ptr = talloc_realloc_size(OTC_GLOBAL, osmo_fd_lookup.table, new_size);
83 OSMO_ASSERT(ptr);
84 memset(ptr + osmo_fd_lookup.size, 0, new_size - osmo_fd_lookup.size);
85 osmo_fd_lookup.table = (struct osmo_fd **)ptr;
86 osmo_fd_lookup.size = new_size;
87 }
88}
89
Harald Welteb904e422020-10-18 21:24:13 +020090#ifndef FORCE_IO_SELECT
91struct poll_state {
92 /* array of pollfd */
93 struct pollfd *poll;
94 /* number of entries in pollfd allocated */
95 unsigned int poll_size;
96 /* number of osmo_fd registered */
97 unsigned int num_registered;
98};
99static __thread struct poll_state g_poll;
100#endif /* FORCE_IO_SELECT */
101
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200102/*! See osmo_select_shutdown_request() */
103static int _osmo_select_shutdown_requested = 0;
104/*! See osmo_select_shutdown_request() */
105static bool _osmo_select_shutdown_done = false;
106
Harald Welte6c0a0e62017-08-12 11:43:14 +0200107/*! Set up an osmo-fd. Will not register it.
108 * \param[inout] ofd Osmo FD to be set-up
109 * \param[in] fd OS-level file descriptor number
Harald Welte16886992019-03-20 10:26:39 +0100110 * \param[in] when bit-mask of OSMO_FD_{READ,WRITE,EXECEPT}
Harald Welte6c0a0e62017-08-12 11:43:14 +0200111 * \param[in] cb Call-back function to be called
112 * \param[in] data Private context pointer
113 * \param[in] priv_nr Private number
114 */
115void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when,
116 int (*cb)(struct osmo_fd *fd, unsigned int what),
117 void *data, unsigned int priv_nr)
118{
119 ofd->fd = fd;
120 ofd->when = when;
121 ofd->cb = cb;
122 ofd->data = data;
123 ofd->priv_nr = priv_nr;
124}
Philipp Maierb2888532016-12-09 14:07:18 +0100125
Harald Welte7e657912020-10-18 22:07:21 +0200126/*! Update the 'when' field of osmo_fd. "ofd->when = (ofd->when & when_mask) | when".
127 * Use this function instead of directly modifying ofd->when, as the latter will be
128 * removed soon. */
129void osmo_fd_update_when(struct osmo_fd *ofd, unsigned int when_mask, unsigned int when)
130{
131 ofd->when &= when_mask;
132 ofd->when |= when;
133}
134
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200135/*! Check if a file descriptor is already registered
Philipp Maierb2888532016-12-09 14:07:18 +0100136 * \param[in] fd osmocom file descriptor to be checked
137 * \returns true if registered; otherwise false
138 */
139bool osmo_fd_is_registered(struct osmo_fd *fd)
140{
141 struct osmo_fd *entry;
142 llist_for_each_entry(entry, &osmo_fds, list) {
143 if (entry == fd) {
144 return true;
145 }
146 }
147
148 return false;
149}
150
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151/*! Register a new file descriptor with select loop abstraction
Harald Welteba6988b2011-08-17 12:46:48 +0200152 * \param[in] fd osmocom file descriptor to be registered
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200153 * \returns 0 on success; negative in case of error
Pau Espin Pedrol153519f2023-03-14 12:01:37 +0100154 *
155 * The API expects fd field of the struct osmo_fd to remain unchanged while
156 * registered, ie until osmo_fd_unregister() is called on it.
Harald Welteba6988b2011-08-17 12:46:48 +0200157 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200158int osmo_fd_register(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +0100159{
160 int flags;
161
162 /* make FD nonblocking */
163 flags = fcntl(fd->fd, F_GETFL);
164 if (flags < 0)
165 return flags;
166 flags |= O_NONBLOCK;
167 flags = fcntl(fd->fd, F_SETFL, flags);
168 if (flags < 0)
169 return flags;
170
Harald Welte32e1f232011-06-26 13:07:18 +0200171 /* set close-on-exec flag */
172 flags = fcntl(fd->fd, F_GETFD);
173 if (flags < 0)
174 return flags;
175 flags |= FD_CLOEXEC;
176 flags = fcntl(fd->fd, F_SETFD, flags);
177 if (flags < 0)
178 return flags;
179
Harald Welteec8b4502010-02-20 20:34:29 +0100180 /* Register FD */
Pau Espin Pedrolc46a15d2023-03-09 17:37:15 +0100181 if (fd->fd > maxfd) {
Harald Welteec8b4502010-02-20 20:34:29 +0100182 maxfd = fd->fd;
Pau Espin Pedrolc46a15d2023-03-09 17:37:15 +0100183 osmo_fd_lookup_table_extend(maxfd);
184 }
Harald Welteec8b4502010-02-20 20:34:29 +0100185
Pau Espin Pedrold05def02020-05-09 19:24:21 +0200186#ifdef OSMO_FD_CHECK
Philipp Maierb2888532016-12-09 14:07:18 +0100187 if (osmo_fd_is_registered(fd)) {
188 fprintf(stderr, "Adding a osmo_fd that is already in the list.\n");
189 return 0;
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +0800190 }
191#endif
Harald Welteb904e422020-10-18 21:24:13 +0200192#ifndef FORCE_IO_SELECT
193 if (g_poll.num_registered + 1 > g_poll.poll_size) {
194 struct pollfd *p;
195 unsigned int new_size = g_poll.poll_size ? g_poll.poll_size * 2 : 1024;
196 p = talloc_realloc(OTC_GLOBAL, g_poll.poll, struct pollfd, new_size);
197 if (!p)
198 return -ENOMEM;
199 memset(p + g_poll.poll_size, 0, new_size - g_poll.poll_size);
200 g_poll.poll = p;
201 g_poll.poll_size = new_size;
202 }
203 g_poll.num_registered++;
204#endif /* FORCE_IO_SELECT */
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +0800205
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200206 llist_add_tail(&fd->list, &osmo_fds);
Pau Espin Pedrolc46a15d2023-03-09 17:37:15 +0100207 osmo_fd_lookup.table[fd->fd] = fd;
Harald Welteec8b4502010-02-20 20:34:29 +0100208
209 return 0;
210}
211
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200212/*! Unregister a file descriptor from select loop abstraction
Harald Welteba6988b2011-08-17 12:46:48 +0200213 * \param[in] fd osmocom file descriptor to be unregistered
Pau Espin Pedrol153519f2023-03-14 12:01:37 +0100214 *
Pau Espin Pedrol25729cf2023-03-14 12:06:27 +0100215 * Caller is responsible for ensuring the fd is really registered before calling this API.
Pau Espin Pedrol153519f2023-03-14 12:01:37 +0100216 * This function must be called before changing the value of the fd field in
217 * the struct osmo_fd.
Harald Welteba6988b2011-08-17 12:46:48 +0200218 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200219void osmo_fd_unregister(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +0100220{
Philipp Maierb2888532016-12-09 14:07:18 +0100221 /* Note: when fd is inside the osmo_fds list (not registered before)
222 * this function will crash! If in doubt, check file descriptor with
223 * osmo_fd_is_registered() */
Harald Welteec8b4502010-02-20 20:34:29 +0100224 unregistered_count++;
225 llist_del(&fd->list);
Harald Welteb904e422020-10-18 21:24:13 +0200226#ifndef FORCE_IO_SELECT
227 g_poll.num_registered--;
228#endif /* FORCE_IO_SELECT */
Philipp Maierb1ef8f52021-12-06 16:31:02 +0100229
Pau Espin Pedrol6fd7f412023-03-14 12:14:36 +0100230 if (OSMO_UNLIKELY(fd->fd < 0 || fd->fd > maxfd)) {
231 /* Some old users used to incorrectly set fd = -1 *before* calling osmo_unregister().
232 * Hence, in order to keep backward compatibility it's not possible to assert() here.
233 * Instead, print an error message since this is actually a bug in the API user. */
234#ifdef OSMO_FD_CHECK
235 osmo_panic("osmo_fd_unregister(fd=%u) out of expected range (0..%u), fix your code!!!\n",
236 fd->fd, maxfd);
237#else
238 fprintf(stderr, "osmo_fd_unregister(fd=%u) out of expected range (0..%u), fix your code!!!\n",
239 fd->fd, maxfd);
240 return;
241#endif
242 }
243
244 osmo_fd_lookup.table[fd->fd] = NULL;
Philipp Maierb1ef8f52021-12-06 16:31:02 +0100245 /* If existent, free any statistical data */
246 osmo_stats_tcp_osmo_fd_unregister(fd);
Harald Welteec8b4502010-02-20 20:34:29 +0100247}
248
Harald Welteea91a512017-07-13 14:28:30 +0200249/*! Close a file descriptor, mark it as closed + unregister from select loop abstraction
250 * \param[in] fd osmocom file descriptor to be unregistered + closed
251 *
252 * If \a fd is registered, we unregister it from the select() loop
253 * abstraction. We then close the fd and set it to -1, as well as
254 * unsetting any 'when' flags */
255void osmo_fd_close(struct osmo_fd *fd)
256{
257 if (osmo_fd_is_registered(fd))
258 osmo_fd_unregister(fd);
259 if (fd->fd != -1)
260 close(fd->fd);
261 fd->fd = -1;
262 fd->when = 0;
263}
264
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200265/*! Populate the fd_sets and return the highest fd number
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100266 * \param[in] _rset The readfds to populate
267 * \param[in] _wset The wrtiefds to populate
268 * \param[in] _eset The errorfds to populate
269 *
270 * \returns The highest file descriptor seen or 0 on an empty list
271 */
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100272inline int osmo_fd_fill_fds(void *_rset, void *_wset, void *_eset)
Harald Welteec8b4502010-02-20 20:34:29 +0100273{
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100274 fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset;
275 struct osmo_fd *ufd;
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100276 int highfd = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100277
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200278 llist_for_each_entry(ufd, &osmo_fds, list) {
Harald Welte16886992019-03-20 10:26:39 +0100279 if (ufd->when & OSMO_FD_READ)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100280 FD_SET(ufd->fd, readset);
Harald Welteec8b4502010-02-20 20:34:29 +0100281
Harald Welte16886992019-03-20 10:26:39 +0100282 if (ufd->when & OSMO_FD_WRITE)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100283 FD_SET(ufd->fd, writeset);
Harald Welteec8b4502010-02-20 20:34:29 +0100284
Harald Welte16886992019-03-20 10:26:39 +0100285 if (ufd->when & OSMO_FD_EXCEPT)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100286 FD_SET(ufd->fd, exceptset);
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100287
288 if (ufd->fd > highfd)
289 highfd = ufd->fd;
Harald Welteec8b4502010-02-20 20:34:29 +0100290 }
291
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100292 return highfd;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100293}
Harald Welteec8b4502010-02-20 20:34:29 +0100294
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100295inline int osmo_fd_disp_fds(void *_rset, void *_wset, void *_eset)
296{
297 struct osmo_fd *ufd, *tmp;
298 int work = 0;
299 fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset;
Harald Welteec8b4502010-02-20 20:34:29 +0100300
Harald Welteec8b4502010-02-20 20:34:29 +0100301restart:
302 unregistered_count = 0;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200303 llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) {
Harald Welteec8b4502010-02-20 20:34:29 +0100304 int flags = 0;
305
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100306 if (FD_ISSET(ufd->fd, readset)) {
Harald Welte16886992019-03-20 10:26:39 +0100307 flags |= OSMO_FD_READ;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100308 FD_CLR(ufd->fd, readset);
Harald Welteec8b4502010-02-20 20:34:29 +0100309 }
310
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100311 if (FD_ISSET(ufd->fd, writeset)) {
Harald Welte16886992019-03-20 10:26:39 +0100312 flags |= OSMO_FD_WRITE;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100313 FD_CLR(ufd->fd, writeset);
Harald Welteec8b4502010-02-20 20:34:29 +0100314 }
315
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100316 if (FD_ISSET(ufd->fd, exceptset)) {
Harald Welte16886992019-03-20 10:26:39 +0100317 flags |= OSMO_FD_EXCEPT;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100318 FD_CLR(ufd->fd, exceptset);
Harald Welteec8b4502010-02-20 20:34:29 +0100319 }
320
321 if (flags) {
322 work = 1;
Harald Welte52a83752019-02-21 16:37:10 +0100323 /* make sure to clear any log context before processing the next incoming message
324 * as part of some file descriptor callback. This effectively prevents "context
325 * leaking" from processing of one message into processing of the next message as part
326 * of one iteration through the list of file descriptors here. See OS#3813 */
327 log_reset_context();
Harald Welteec8b4502010-02-20 20:34:29 +0100328 ufd->cb(ufd, flags);
329 }
Holger Hans Peter Freyther92e1e702014-04-09 17:24:00 +0200330 /* ugly, ugly hack. If more than one filedescriptor was
Harald Welteec8b4502010-02-20 20:34:29 +0100331 * unregistered, they might have been consecutive and
332 * llist_for_each_entry_safe() is no longer safe */
Holger Hans Peter Freyther23ba4742010-04-11 17:33:19 +0200333 /* this seems to happen with the last element of the list as well */
334 if (unregistered_count >= 1)
Harald Welteec8b4502010-02-20 20:34:29 +0100335 goto restart;
336 }
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100337
Harald Welteec8b4502010-02-20 20:34:29 +0100338 return work;
339}
340
Harald Welteb904e422020-10-18 21:24:13 +0200341
342#ifndef FORCE_IO_SELECT
343/* fill g_poll.poll and return the number of entries filled */
344static unsigned int poll_fill_fds(void)
345{
346 struct osmo_fd *ufd;
347 unsigned int i = 0;
348
349 llist_for_each_entry(ufd, &osmo_fds, list) {
350 struct pollfd *p;
351
352 if (!ufd->when)
353 continue;
354
355 p = &g_poll.poll[i++];
356
357 p->fd = ufd->fd;
358 p->events = 0;
359 p->revents = 0;
360
361 /* use the same mapping as the Linux kernel does in fs/select.c */
362 if (ufd->when & OSMO_FD_READ)
363 p->events |= POLLIN | POLLHUP | POLLERR;
364
365 if (ufd->when & OSMO_FD_WRITE)
366 p->events |= POLLOUT | POLLERR;
367
368 if (ufd->when & OSMO_FD_EXCEPT)
369 p->events |= POLLPRI;
370
371 }
372
373 return i;
374}
375
376/* iterate over first n_fd entries of g_poll.poll + dispatch */
Harald Welte0b08d512022-01-09 12:03:12 +0100377static int poll_disp_fds(unsigned int n_fd)
Harald Welteb904e422020-10-18 21:24:13 +0200378{
379 struct osmo_fd *ufd;
380 unsigned int i;
381 int work = 0;
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200382 int shutdown_pending_writes = 0;
Harald Welteb904e422020-10-18 21:24:13 +0200383
384 for (i = 0; i < n_fd; i++) {
385 struct pollfd *p = &g_poll.poll[i];
386 int flags = 0;
387
388 if (!p->revents)
389 continue;
390
391 ufd = osmo_fd_get_by_fd(p->fd);
392 if (!ufd) {
393 /* FD might have been unregistered meanwhile */
394 continue;
395 }
396 /* use the same mapping as the Linux kernel does in fs/select.c */
397 if (p->revents & (POLLIN | POLLHUP | POLLERR))
398 flags |= OSMO_FD_READ;
399 if (p->revents & (POLLOUT | POLLERR))
400 flags |= OSMO_FD_WRITE;
401 if (p->revents & POLLPRI)
402 flags |= OSMO_FD_EXCEPT;
403
404 /* make sure we never report more than the user requested */
405 flags &= ufd->when;
406
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200407 if (_osmo_select_shutdown_requested > 0) {
408 if (ufd->when & OSMO_FD_WRITE)
409 shutdown_pending_writes++;
410 }
411
Harald Welteb904e422020-10-18 21:24:13 +0200412 if (flags) {
413 work = 1;
414 /* make sure to clear any log context before processing the next incoming message
415 * as part of some file descriptor callback. This effectively prevents "context
416 * leaking" from processing of one message into processing of the next message as part
417 * of one iteration through the list of file descriptors here. See OS#3813 */
418 log_reset_context();
419 ufd->cb(ufd, flags);
420 }
421 }
422
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200423 if (_osmo_select_shutdown_requested > 0 && !shutdown_pending_writes)
424 _osmo_select_shutdown_done = true;
425
Harald Welteb904e422020-10-18 21:24:13 +0200426 return work;
427}
428
429static int _osmo_select_main(int polling)
430{
431 unsigned int n_poll;
432 int rc;
Oliver Smithd841bec2021-12-21 19:17:51 +0100433 int timeout = 0;
Harald Welteb904e422020-10-18 21:24:13 +0200434
435 /* prepare read and write fdsets */
436 n_poll = poll_fill_fds();
437
Oliver Smithd841bec2021-12-21 19:17:51 +0100438 if (!polling) {
Harald Welteb904e422020-10-18 21:24:13 +0200439 osmo_timers_prepare();
Oliver Smithd841bec2021-12-21 19:17:51 +0100440 timeout = osmo_timers_nearest_ms();
Harald Welteb904e422020-10-18 21:24:13 +0200441
Oliver Smithd841bec2021-12-21 19:17:51 +0100442 if (_osmo_select_shutdown_requested && timeout == -1)
443 timeout = 0;
444 }
445
446 rc = poll(g_poll.poll, n_poll, timeout);
Harald Welteb904e422020-10-18 21:24:13 +0200447 if (rc < 0)
448 return 0;
449
450 /* fire timers */
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200451 if (!_osmo_select_shutdown_requested)
452 osmo_timers_update();
Harald Welteb904e422020-10-18 21:24:13 +0200453
454 OSMO_ASSERT(osmo_ctx->select);
455
456 /* call registered callback functions */
457 return poll_disp_fds(n_poll);
458}
459#else /* FORCE_IO_SELECT */
460/* the old implementation based on select, used 2008-2020 */
Harald Welte2d906112019-03-18 17:17:43 +0100461static int _osmo_select_main(int polling)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100462{
463 fd_set readset, writeset, exceptset;
464 int rc;
465 struct timeval no_time = {0, 0};
466
467 FD_ZERO(&readset);
468 FD_ZERO(&writeset);
469 FD_ZERO(&exceptset);
470
471 /* prepare read and write fdsets */
472 osmo_fd_fill_fds(&readset, &writeset, &exceptset);
473
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100474 if (!polling)
475 osmo_timers_prepare();
476 rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest());
477 if (rc < 0)
478 return 0;
479
480 /* fire timers */
481 osmo_timers_update();
482
Harald Welte2d906112019-03-18 17:17:43 +0100483 OSMO_ASSERT(osmo_ctx->select);
484
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100485 /* call registered callback functions */
486 return osmo_fd_disp_fds(&readset, &writeset, &exceptset);
487}
Harald Welteb904e422020-10-18 21:24:13 +0200488#endif /* FORCE_IO_SELECT */
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100489
Harald Welte2d906112019-03-18 17:17:43 +0100490/*! select main loop integration
491 * \param[in] polling should we pollonly (1) or block on select (0)
492 * \returns 0 if no fd handled; 1 if fd handled; negative in case of error
493 */
494int osmo_select_main(int polling)
495{
496 int rc = _osmo_select_main(polling);
497#ifndef EMBEDDED
498 if (talloc_total_size(osmo_ctx->select) != 0) {
499 osmo_panic("You cannot use the 'select' volatile "
500 "context if you don't use osmo_select_main_ctx()!\n");
501 }
502#endif
503 return rc;
504}
505
506#ifndef EMBEDDED
507/*! select main loop integration with temporary select-dispatch talloc context
508 * \param[in] polling should we pollonly (1) or block on select (0)
509 * \returns 0 if no fd handled; 1 if fd handled; negative in case of error
510 */
511int osmo_select_main_ctx(int polling)
512{
513 int rc = _osmo_select_main(polling);
514 /* free all the children of the volatile 'select' scope context */
515 talloc_free_children(osmo_ctx->select);
516 return rc;
517}
518#endif
519
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200520/*! find an osmo_fd based on the integer fd
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200521 * \param[in] fd file descriptor to use as search key
522 * \returns \ref osmo_fd for \ref fd; NULL in case it doesn't exist */
Harald Welte6c33ae22016-03-19 21:17:58 +0100523struct osmo_fd *osmo_fd_get_by_fd(int fd)
524{
arehbeincd238062023-07-13 18:49:47 +0200525 if (fd > maxfd || fd < 0)
Pau Espin Pedrolc46a15d2023-03-09 17:37:15 +0100526 return NULL;
527 return osmo_fd_lookup.table[fd];
Harald Welte6c33ae22016-03-19 21:17:58 +0100528}
529
Harald Welte7a010b12019-04-06 13:46:40 +0200530/*! initialize the osmocom select abstraction for the current thread */
531void osmo_select_init(void)
532{
533 INIT_LLIST_HEAD(&osmo_fds);
Pau Espin Pedrolc46a15d2023-03-09 17:37:15 +0100534 osmo_fd_lookup_table_extend(0);
Harald Welte7a010b12019-04-06 13:46:40 +0200535}
536
Oliver Smithb46cfba2023-03-14 15:05:03 +0100537/* ensure main thread always has pre-initialized osmo_fds
538 * priority 102: must run after on_dso_load_ctx */
539static __attribute__((constructor(102))) void on_dso_load_select(void)
Harald Welte7a010b12019-04-06 13:46:40 +0200540{
541 osmo_select_init();
542}
543
Harald Welteea4d8932017-12-03 16:13:39 +0100544#ifdef HAVE_SYS_TIMERFD_H
545#include <sys/timerfd.h>
546
547/*! disable the osmocom-wrapped timerfd */
548int osmo_timerfd_disable(struct osmo_fd *ofd)
549{
550 const struct itimerspec its_null = {
551 .it_value = { 0, 0 },
552 .it_interval = { 0, 0 },
553 };
554 return timerfd_settime(ofd->fd, 0, &its_null, NULL);
555}
556
Alexander Chemerise8ec2142020-05-09 01:57:16 +0300557/*! schedule the osmocom-wrapped timerfd to occur first at \a first, then periodically at \a interval
Harald Welteea4d8932017-12-03 16:13:39 +0100558 * \param[in] ofd Osmocom wrapped timerfd
559 * \param[in] first Relative time at which the timer should first execute (NULL = \a interval)
560 * \param[in] interval Time interval at which subsequent timer shall fire
561 * \returns 0 on success; negative on error */
562int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first,
563 const struct timespec *interval)
564{
565 struct itimerspec its;
566
567 if (ofd->fd < 0)
568 return -EINVAL;
569
570 /* first expiration */
571 if (first)
572 its.it_value = *first;
573 else
574 its.it_value = *interval;
575 /* repeating interval */
576 its.it_interval = *interval;
577
578 return timerfd_settime(ofd->fd, 0, &its, NULL);
579}
580
581/*! setup osmocom-wrapped timerfd
582 * \param[inout] ofd Osmocom-wrapped timerfd on which to operate
583 * \param[in] cb Call-back function called when timerfd becomes readable
584 * \param[in] data Opaque data to be passed on to call-back
585 * \returns 0 on success; negative on error
586 *
587 * We simply initialize the data structures here, but do not yet
588 * schedule the timer.
589 */
590int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data)
591{
592 ofd->cb = cb;
593 ofd->data = data;
Harald Welte16886992019-03-20 10:26:39 +0100594 ofd->when = OSMO_FD_READ;
Harald Welteea4d8932017-12-03 16:13:39 +0100595
596 if (ofd->fd < 0) {
Harald Welte37608f92018-10-21 12:32:55 +0200597 int rc;
598
Harald Welteea4d8932017-12-03 16:13:39 +0100599 ofd->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
600 if (ofd->fd < 0)
601 return ofd->fd;
602
Harald Welte37608f92018-10-21 12:32:55 +0200603 rc = osmo_fd_register(ofd);
604 if (rc < 0) {
Harald Welte100e44e2020-04-18 21:16:43 +0200605 osmo_fd_unregister(ofd);
Harald Welte37608f92018-10-21 12:32:55 +0200606 close(ofd->fd);
607 ofd->fd = -1;
608 return rc;
609 }
Harald Welteea4d8932017-12-03 16:13:39 +0100610 }
611 return 0;
612}
613
614#endif /* HAVE_SYS_TIMERFD_H */
615
Harald Weltea70ac852020-04-17 19:20:01 +0200616#ifdef HAVE_SYS_SIGNALFD_H
617#include <sys/signalfd.h>
618
619static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
620{
621 struct osmo_signalfd *osfd = ofd->data;
622 struct signalfd_siginfo fdsi;
623 int rc;
624
625 rc = read(ofd->fd, &fdsi, sizeof(fdsi));
626 if (rc < 0) {
627 osmo_fd_unregister(ofd);
628 close(ofd->fd);
629 ofd->fd = -1;
630 return rc;
631 }
632
633 osfd->cb(osfd, &fdsi);
634
635 return 0;
636};
637
638/*! create a signalfd and register it with osmocom select loop.
639 * \param[in] ctx talloc context from which osmo_signalfd is to be allocated
640 * \param[in] set of signals to be accept via this file descriptor
641 * \param[in] cb call-back function to be called for each arriving signal
642 * \param[in] data opaque user-provided data to pass to callback
643 * \returns pointer to newly-allocated + registered osmo_signalfd; NULL on error */
644struct osmo_signalfd *
645osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data)
646{
647 struct osmo_signalfd *osfd = talloc_size(ctx, sizeof(*osfd));
648 int fd, rc;
649
650 if (!osfd)
651 return NULL;
652
653 osfd->data = data;
654 osfd->sigset = set;
655 osfd->cb = cb;
656
657 fd = signalfd(-1, &osfd->sigset, SFD_NONBLOCK);
658 if (fd < 0) {
659 talloc_free(osfd);
660 return NULL;
661 }
662
663 osmo_fd_setup(&osfd->ofd, fd, OSMO_FD_READ, signalfd_callback, osfd, 0);
664 rc = osmo_fd_register(&osfd->ofd);
665 if (rc < 0) {
666 close(fd);
667 talloc_free(osfd);
668 return NULL;
669 }
670
671 return osfd;
672}
673
674#endif /* HAVE_SYS_SIGNALFD_H */
Harald Welteea4d8932017-12-03 16:13:39 +0100675
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200676/*! Request osmo_select_* to only service pending OSMO_FD_WRITE requests. Once all writes are done,
677 * osmo_select_shutdown_done() returns true. This allows for example to send all outbound packets before terminating the
678 * process.
679 *
680 * Usage example:
681 *
682 * static void signal_handler(int signum)
683 * {
684 * fprintf(stdout, "signal %u received\n", signum);
685 *
686 * switch (signum) {
687 * case SIGINT:
688 * case SIGTERM:
689 * // If the user hits Ctrl-C the third time, just terminate immediately.
690 * if (osmo_select_shutdown_requested() >= 2)
691 * exit(-1);
692 * // Request write-only mode in osmo_select_main_ctx()
693 * osmo_select_shutdown_request();
694 * break;
695 * [...]
696 * }
697 *
698 * main()
699 * {
700 * signal(SIGINT, &signal_handler);
701 * signal(SIGTERM, &signal_handler);
702 *
703 * [...]
704 *
705 * // After the signal_handler issued osmo_select_shutdown_request(), osmo_select_shutdown_done() returns true
706 * // as soon as all write queues are empty.
707 * while (!osmo_select_shutdown_done()) {
708 * osmo_select_main_ctx(0);
709 * }
710 * }
711 */
Harald Weltee61d4592022-11-03 11:05:58 +0100712void osmo_select_shutdown_request(void)
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200713{
714 _osmo_select_shutdown_requested++;
715};
716
717/*! Return the number of times osmo_select_shutdown_request() was called before. */
Harald Weltee61d4592022-11-03 11:05:58 +0100718int osmo_select_shutdown_requested(void)
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200719{
720 return _osmo_select_shutdown_requested;
721};
722
723/*! Return true after osmo_select_shutdown_requested() was called, and after an osmo_select poll loop found no more
724 * pending OSMO_FD_WRITE on any registered socket. */
Harald Weltee61d4592022-11-03 11:05:58 +0100725bool osmo_select_shutdown_done(void) {
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200726 return _osmo_select_shutdown_done;
727};
728
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200729/*! @} */
Harald Welteba6988b2011-08-17 12:46:48 +0200730
Harald Welteec8b4502010-02-20 20:34:29 +0100731#endif /* _HAVE_SYS_SELECT_H */