blob: 71ee7f6025cd1ad85eb713061f5d984b4f58a79b [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 Weltee08da972017-11-13 01:00:26 +09008 * All Rights Reserverd.
9 *
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.
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 Škarvada2b82c1c2015-11-11 16:02:54 +010024 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 * MA 02110-1301, USA.
Harald Welteec8b4502010-02-20 20:34:29 +010026 */
27
28#include <fcntl.h>
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080029#include <stdio.h>
Harald Welteea91a512017-07-13 14:28:30 +020030#include <unistd.h>
Harald Welte9e166e82014-03-10 17:28:33 +010031#include <string.h>
Philipp Maierb2888532016-12-09 14:07:18 +010032#include <stdbool.h>
Harald Welteea4d8932017-12-03 16:13:39 +010033#include <errno.h>
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080034
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010035#include <osmocom/core/select.h>
36#include <osmocom/core/linuxlist.h>
37#include <osmocom/core/timer.h>
Harald Welte52a83752019-02-21 16:37:10 +010038#include <osmocom/core/logging.h>
Harald Welte2d906112019-03-18 17:17:43 +010039#include <osmocom/core/talloc.h>
40#include <osmocom/core/utils.h>
Harald Welteec8b4502010-02-20 20:34:29 +010041
Harald Welte54844802010-02-20 22:23:08 +010042#include "../config.h"
43
Harald Welteb904e422020-10-18 21:24:13 +020044#if defined(HAVE_SYS_SELECT_H) && defined(HAVE_POLL_H)
Harald Welte1d604d82017-05-17 15:32:35 +010045#include <sys/select.h>
Harald Welteb904e422020-10-18 21:24:13 +020046#include <poll.h>
Harald Welteec8b4502010-02-20 20:34:29 +010047
Harald Welteba6988b2011-08-17 12:46:48 +020048/*! \addtogroup select
49 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 * select() loop abstraction
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020051 *
52 * \file select.c */
Harald Welteba6988b2011-08-17 12:46:48 +020053
Harald Welte7a010b12019-04-06 13:46:40 +020054/* 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 */
56static __thread int maxfd = 0;
57static __thread struct llist_head osmo_fds; /* TLS cannot use LLIST_HEAD() */
58static __thread int unregistered_count;
Harald Welteec8b4502010-02-20 20:34:29 +010059
Harald Welteb904e422020-10-18 21:24:13 +020060#ifndef FORCE_IO_SELECT
61struct 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};
69static __thread struct poll_state g_poll;
70#endif /* FORCE_IO_SELECT */
71
Harald Welte6c0a0e62017-08-12 11:43:14 +020072/*! 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 Welte16886992019-03-20 10:26:39 +010075 * \param[in] when bit-mask of OSMO_FD_{READ,WRITE,EXECEPT}
Harald Welte6c0a0e62017-08-12 11:43:14 +020076 * \param[in] cb Call-back function to be called
77 * \param[in] data Private context pointer
78 * \param[in] priv_nr Private number
79 */
80void 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 Maierb2888532016-12-09 14:07:18 +010090
Harald Welte7e657912020-10-18 22:07:21 +020091/*! 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. */
94void 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 Hofmeyr87e45502017-06-20 00:17:59 +0200100/*! Check if a file descriptor is already registered
Philipp Maierb2888532016-12-09 14:07:18 +0100101 * \param[in] fd osmocom file descriptor to be checked
102 * \returns true if registered; otherwise false
103 */
104bool 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 Hofmeyr87e45502017-06-20 00:17:59 +0200116/*! Register a new file descriptor with select loop abstraction
Harald Welteba6988b2011-08-17 12:46:48 +0200117 * \param[in] fd osmocom file descriptor to be registered
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200118 * \returns 0 on success; negative in case of error
Harald Welteba6988b2011-08-17 12:46:48 +0200119 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200120int osmo_fd_register(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +0100121{
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 Welte32e1f232011-06-26 13:07:18 +0200133 /* 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 Welteec8b4502010-02-20 20:34:29 +0100142 /* Register FD */
143 if (fd->fd > maxfd)
144 maxfd = fd->fd;
145
Pau Espin Pedrold05def02020-05-09 19:24:21 +0200146#ifdef OSMO_FD_CHECK
Philipp Maierb2888532016-12-09 14:07:18 +0100147 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 Freyther43558312010-08-06 06:48:43 +0800150 }
151#endif
Harald Welteb904e422020-10-18 21:24:13 +0200152#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 Freyther43558312010-08-06 06:48:43 +0800165
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200166 llist_add_tail(&fd->list, &osmo_fds);
Harald Welteec8b4502010-02-20 20:34:29 +0100167
168 return 0;
169}
170
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200171/*! Unregister a file descriptor from select loop abstraction
Harald Welteba6988b2011-08-17 12:46:48 +0200172 * \param[in] fd osmocom file descriptor to be unregistered
173 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200174void osmo_fd_unregister(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +0100175{
Philipp Maierb2888532016-12-09 14:07:18 +0100176 /* 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 Welteec8b4502010-02-20 20:34:29 +0100179 unregistered_count++;
180 llist_del(&fd->list);
Harald Welteb904e422020-10-18 21:24:13 +0200181#ifndef FORCE_IO_SELECT
182 g_poll.num_registered--;
183#endif /* FORCE_IO_SELECT */
Harald Welteec8b4502010-02-20 20:34:29 +0100184}
185
Harald Welteea91a512017-07-13 14:28:30 +0200186/*! 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 */
192void 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 Hofmeyr87e45502017-06-20 00:17:59 +0200202/*! Populate the fd_sets and return the highest fd number
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100203 * \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 Freyther61f28882016-03-21 09:55:05 +0100209inline int osmo_fd_fill_fds(void *_rset, void *_wset, void *_eset)
Harald Welteec8b4502010-02-20 20:34:29 +0100210{
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100211 fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset;
212 struct osmo_fd *ufd;
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100213 int highfd = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100214
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200215 llist_for_each_entry(ufd, &osmo_fds, list) {
Harald Welte16886992019-03-20 10:26:39 +0100216 if (ufd->when & OSMO_FD_READ)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100217 FD_SET(ufd->fd, readset);
Harald Welteec8b4502010-02-20 20:34:29 +0100218
Harald Welte16886992019-03-20 10:26:39 +0100219 if (ufd->when & OSMO_FD_WRITE)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100220 FD_SET(ufd->fd, writeset);
Harald Welteec8b4502010-02-20 20:34:29 +0100221
Harald Welte16886992019-03-20 10:26:39 +0100222 if (ufd->when & OSMO_FD_EXCEPT)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100223 FD_SET(ufd->fd, exceptset);
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100224
225 if (ufd->fd > highfd)
226 highfd = ufd->fd;
Harald Welteec8b4502010-02-20 20:34:29 +0100227 }
228
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100229 return highfd;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100230}
Harald Welteec8b4502010-02-20 20:34:29 +0100231
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100232inline 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 Welteec8b4502010-02-20 20:34:29 +0100237
Harald Welteec8b4502010-02-20 20:34:29 +0100238restart:
239 unregistered_count = 0;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200240 llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) {
Harald Welteec8b4502010-02-20 20:34:29 +0100241 int flags = 0;
242
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100243 if (FD_ISSET(ufd->fd, readset)) {
Harald Welte16886992019-03-20 10:26:39 +0100244 flags |= OSMO_FD_READ;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100245 FD_CLR(ufd->fd, readset);
Harald Welteec8b4502010-02-20 20:34:29 +0100246 }
247
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100248 if (FD_ISSET(ufd->fd, writeset)) {
Harald Welte16886992019-03-20 10:26:39 +0100249 flags |= OSMO_FD_WRITE;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100250 FD_CLR(ufd->fd, writeset);
Harald Welteec8b4502010-02-20 20:34:29 +0100251 }
252
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100253 if (FD_ISSET(ufd->fd, exceptset)) {
Harald Welte16886992019-03-20 10:26:39 +0100254 flags |= OSMO_FD_EXCEPT;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100255 FD_CLR(ufd->fd, exceptset);
Harald Welteec8b4502010-02-20 20:34:29 +0100256 }
257
258 if (flags) {
259 work = 1;
Harald Welte52a83752019-02-21 16:37:10 +0100260 /* 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 Welteec8b4502010-02-20 20:34:29 +0100265 ufd->cb(ufd, flags);
266 }
Holger Hans Peter Freyther92e1e702014-04-09 17:24:00 +0200267 /* ugly, ugly hack. If more than one filedescriptor was
Harald Welteec8b4502010-02-20 20:34:29 +0100268 * unregistered, they might have been consecutive and
269 * llist_for_each_entry_safe() is no longer safe */
Holger Hans Peter Freyther23ba4742010-04-11 17:33:19 +0200270 /* this seems to happen with the last element of the list as well */
271 if (unregistered_count >= 1)
Harald Welteec8b4502010-02-20 20:34:29 +0100272 goto restart;
273 }
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100274
Harald Welteec8b4502010-02-20 20:34:29 +0100275 return work;
276}
277
Harald Welteb904e422020-10-18 21:24:13 +0200278
279#ifndef FORCE_IO_SELECT
280/* fill g_poll.poll and return the number of entries filled */
281static 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 */
314static 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
357static 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 Welte2d906112019-03-18 17:17:43 +0100382static int _osmo_select_main(int polling)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100383{
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 Freyther61f28882016-03-21 09:55:05 +0100395 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 Welte2d906112019-03-18 17:17:43 +0100404 OSMO_ASSERT(osmo_ctx->select);
405
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100406 /* call registered callback functions */
407 return osmo_fd_disp_fds(&readset, &writeset, &exceptset);
408}
Harald Welteb904e422020-10-18 21:24:13 +0200409#endif /* FORCE_IO_SELECT */
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100410
Harald Welte2d906112019-03-18 17:17:43 +0100411/*! 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 */
415int 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 */
432int 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 Hofmeyr87e45502017-06-20 00:17:59 +0200441/*! find an osmo_fd based on the integer fd
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200442 * \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 Welte6c33ae22016-03-19 21:17:58 +0100444struct 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 Welte7a010b12019-04-06 13:46:40 +0200455/*! initialize the osmocom select abstraction for the current thread */
456void osmo_select_init(void)
457{
458 INIT_LLIST_HEAD(&osmo_fds);
459}
460
461/* ensure main thread always has pre-initialized osmo_fds */
462static __attribute__((constructor)) void on_dso_load_select(void)
463{
464 osmo_select_init();
465}
466
Harald Welteea4d8932017-12-03 16:13:39 +0100467#ifdef HAVE_SYS_TIMERFD_H
468#include <sys/timerfd.h>
469
470/*! disable the osmocom-wrapped timerfd */
471int 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 Chemerise8ec2142020-05-09 01:57:16 +0300480/*! schedule the osmocom-wrapped timerfd to occur first at \a first, then periodically at \a interval
Harald Welteea4d8932017-12-03 16:13:39 +0100481 * \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 */
485int 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 */
513int 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 Welte16886992019-03-20 10:26:39 +0100517 ofd->when = OSMO_FD_READ;
Harald Welteea4d8932017-12-03 16:13:39 +0100518
519 if (ofd->fd < 0) {
Harald Welte37608f92018-10-21 12:32:55 +0200520 int rc;
521
Harald Welteea4d8932017-12-03 16:13:39 +0100522 ofd->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
523 if (ofd->fd < 0)
524 return ofd->fd;
525
Harald Welte37608f92018-10-21 12:32:55 +0200526 rc = osmo_fd_register(ofd);
527 if (rc < 0) {
Harald Welte100e44e2020-04-18 21:16:43 +0200528 osmo_fd_unregister(ofd);
Harald Welte37608f92018-10-21 12:32:55 +0200529 close(ofd->fd);
530 ofd->fd = -1;
531 return rc;
532 }
Harald Welteea4d8932017-12-03 16:13:39 +0100533 }
534 return 0;
535}
536
537#endif /* HAVE_SYS_TIMERFD_H */
538
Harald Weltea70ac852020-04-17 19:20:01 +0200539#ifdef HAVE_SYS_SIGNALFD_H
540#include <sys/signalfd.h>
541
542static 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 */
567struct osmo_signalfd *
568osmo_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 Welteea4d8932017-12-03 16:13:39 +0100598
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200599/*! @} */
Harald Welteba6988b2011-08-17 12:46:48 +0200600
Harald Welteec8b4502010-02-20 20:34:29 +0100601#endif /* _HAVE_SYS_SELECT_H */