blob: bc6019828fc5de7fd0bfacdf42161dbae0c5bbd6 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file select.h
2 * select loop abstraction.
3 */
4
Sylvain Munaut12ba7782014-06-16 10:13:40 +02005#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01006
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01007#include <osmocom/core/linuxlist.h>
Philipp Maierb2888532016-12-09 14:07:18 +01008#include <stdbool.h>
Harald Welteea4d8932017-12-03 16:13:39 +01009#include <time.h>
Harald Weltea70ac852020-04-17 19:20:01 +020010#include <signal.h>
Harald Welteec8b4502010-02-20 20:34:29 +010011
Harald Welteba6988b2011-08-17 12:46:48 +020012/*! \defgroup select Select loop abstraction
13 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020014 * \file select.h */
Harald Weltebd598e32011-08-16 23:26:52 +020015
Neels Hofmeyr87e45502017-06-20 00:17:59 +020016/*! Indicate interest in reading from the file descriptor */
Harald Welte16886992019-03-20 10:26:39 +010017#define OSMO_FD_READ 0x0001
Neels Hofmeyr87e45502017-06-20 00:17:59 +020018/*! Indicate interest in writing to the file descriptor */
Harald Welte16886992019-03-20 10:26:39 +010019#define OSMO_FD_WRITE 0x0002
Neels Hofmeyr87e45502017-06-20 00:17:59 +020020/*! Indicate interest in exceptions from the file descriptor */
Harald Welte16886992019-03-20 10:26:39 +010021#define OSMO_FD_EXCEPT 0x0004
22
23/* legacy naming dating back to early OpenBSC / bsc_hack of 2008 */
24#define BSC_FD_READ OSMO_FD_READ
25#define BSC_FD_WRITE OSMO_FD_WRITE
26#define BSC_FD_EXCEPT OSMO_FD_EXCEPT
Harald Welteec8b4502010-02-20 20:34:29 +010027
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028/*! Structure representing a file dsecriptor */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020029struct osmo_fd {
Harald Weltebd598e32011-08-16 23:26:52 +020030 /*! linked list for internal management */
31 struct llist_head list;
32 /*! actual operating-system level file decriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010033 int fd;
Harald Welte16886992019-03-20 10:26:39 +010034 /*! bit-mask or of \ref OSMO_FD_READ, \ref OSMO_FD_WRITE and/or
35 * \ref OSMO_FD_EXCEPT */
Harald Welteec8b4502010-02-20 20:34:29 +010036 unsigned int when;
Harald Weltebd598e32011-08-16 23:26:52 +020037 /*! call-back function to be called once file descriptor becomes
38 * available */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020039 int (*cb)(struct osmo_fd *fd, unsigned int what);
Harald Weltebd598e32011-08-16 23:26:52 +020040 /*! data pointer passed through to call-back function */
Harald Welteec8b4502010-02-20 20:34:29 +010041 void *data;
Harald Weltebd598e32011-08-16 23:26:52 +020042 /*! private number, extending \a data */
Harald Welteec8b4502010-02-20 20:34:29 +010043 unsigned int priv_nr;
44};
45
Harald Welte6c0a0e62017-08-12 11:43:14 +020046void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when,
47 int (*cb)(struct osmo_fd *fd, unsigned int what),
48 void *data, unsigned int priv_nr);
49
Philipp Maierb2888532016-12-09 14:07:18 +010050bool osmo_fd_is_registered(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020051int osmo_fd_register(struct osmo_fd *fd);
52void osmo_fd_unregister(struct osmo_fd *fd);
Harald Welteea91a512017-07-13 14:28:30 +020053void osmo_fd_close(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020054int osmo_select_main(int polling);
Harald Welte2d906112019-03-18 17:17:43 +010055int osmo_select_main_ctx(int polling);
Harald Welte7a010b12019-04-06 13:46:40 +020056void osmo_select_init(void);
Harald Weltebd598e32011-08-16 23:26:52 +020057
Harald Welte6c33ae22016-03-19 21:17:58 +010058struct osmo_fd *osmo_fd_get_by_fd(int fd);
59
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +010060/*
61 * foreign event loop integration
62 */
63int osmo_fd_fill_fds(void *readset, void *writeset, void *exceptset);
64int osmo_fd_disp_fds(void *readset, void *writeset, void *exceptset);
65
Harald Welteea4d8932017-12-03 16:13:39 +010066/* timerfd integration */
67int osmo_timerfd_disable(struct osmo_fd *ofd);
68int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first,
69 const struct timespec *interval);
70int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data);
71
Harald Weltea70ac852020-04-17 19:20:01 +020072/* signalfd integration */
73struct osmo_signalfd;
74struct signalfd_siginfo;
75
76typedef void osmo_signalfd_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi);
77
78struct osmo_signalfd {
79 struct osmo_fd ofd;
80 sigset_t sigset;
81 osmo_signalfd_cb *cb;
82 void *data;
83};
84
85struct osmo_signalfd *
86osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data);
87
88
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020089/*! @} */