blob: fc14851218418d810424d0152507529c3ae9aba3 [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
Harald Welte7e657912020-10-18 22:07:21 +020022/*! Used as when_mask in osmo_fd_update_when() */
23#define OSMO_FD_MASK 0xFFFF
Harald Welte16886992019-03-20 10:26:39 +010024
25/* legacy naming dating back to early OpenBSC / bsc_hack of 2008 */
26#define BSC_FD_READ OSMO_FD_READ
27#define BSC_FD_WRITE OSMO_FD_WRITE
28#define BSC_FD_EXCEPT OSMO_FD_EXCEPT
Harald Welteec8b4502010-02-20 20:34:29 +010029
Neels Hofmeyr87e45502017-06-20 00:17:59 +020030/*! Structure representing a file dsecriptor */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020031struct osmo_fd {
Harald Weltebd598e32011-08-16 23:26:52 +020032 /*! linked list for internal management */
33 struct llist_head list;
34 /*! actual operating-system level file decriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010035 int fd;
Harald Welte16886992019-03-20 10:26:39 +010036 /*! bit-mask or of \ref OSMO_FD_READ, \ref OSMO_FD_WRITE and/or
37 * \ref OSMO_FD_EXCEPT */
Harald Welteec8b4502010-02-20 20:34:29 +010038 unsigned int when;
Harald Weltebd598e32011-08-16 23:26:52 +020039 /*! call-back function to be called once file descriptor becomes
40 * available */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020041 int (*cb)(struct osmo_fd *fd, unsigned int what);
Harald Weltebd598e32011-08-16 23:26:52 +020042 /*! data pointer passed through to call-back function */
Harald Welteec8b4502010-02-20 20:34:29 +010043 void *data;
Harald Weltebd598e32011-08-16 23:26:52 +020044 /*! private number, extending \a data */
Harald Welteec8b4502010-02-20 20:34:29 +010045 unsigned int priv_nr;
46};
47
Harald Welte6c0a0e62017-08-12 11:43:14 +020048void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when,
49 int (*cb)(struct osmo_fd *fd, unsigned int what),
50 void *data, unsigned int priv_nr);
51
Harald Welte7e657912020-10-18 22:07:21 +020052void osmo_fd_update_when(struct osmo_fd *ofd, unsigned int when_mask, unsigned int when);
53
54static inline void osmo_fd_read_enable(struct osmo_fd *ofd) {
55 osmo_fd_update_when(ofd, OSMO_FD_MASK, OSMO_FD_READ);
56}
57
58static inline void osmo_fd_read_disable(struct osmo_fd *ofd) {
59 osmo_fd_update_when(ofd, ~OSMO_FD_READ, 0);
60}
61
62static inline void osmo_fd_write_enable(struct osmo_fd *ofd) {
63 osmo_fd_update_when(ofd, OSMO_FD_MASK, OSMO_FD_WRITE);
64}
65
66static inline void osmo_fd_write_disable(struct osmo_fd *ofd) {
67 osmo_fd_update_when(ofd, ~OSMO_FD_WRITE, 0);
68}
69
Philipp Maierb2888532016-12-09 14:07:18 +010070bool osmo_fd_is_registered(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020071int osmo_fd_register(struct osmo_fd *fd);
72void osmo_fd_unregister(struct osmo_fd *fd);
Harald Welteea91a512017-07-13 14:28:30 +020073void osmo_fd_close(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020074int osmo_select_main(int polling);
Harald Welte2d906112019-03-18 17:17:43 +010075int osmo_select_main_ctx(int polling);
Harald Welte7a010b12019-04-06 13:46:40 +020076void osmo_select_init(void);
Harald Weltebd598e32011-08-16 23:26:52 +020077
Harald Welte6c33ae22016-03-19 21:17:58 +010078struct osmo_fd *osmo_fd_get_by_fd(int fd);
79
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +010080/*
81 * foreign event loop integration
82 */
83int osmo_fd_fill_fds(void *readset, void *writeset, void *exceptset);
84int osmo_fd_disp_fds(void *readset, void *writeset, void *exceptset);
85
Harald Welteea4d8932017-12-03 16:13:39 +010086/* timerfd integration */
87int osmo_timerfd_disable(struct osmo_fd *ofd);
88int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first,
89 const struct timespec *interval);
90int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data);
91
Harald Weltea70ac852020-04-17 19:20:01 +020092/* signalfd integration */
93struct osmo_signalfd;
94struct signalfd_siginfo;
95
96typedef void osmo_signalfd_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi);
97
98struct osmo_signalfd {
99 struct osmo_fd ofd;
100 sigset_t sigset;
101 osmo_signalfd_cb *cb;
102 void *data;
103};
104
105struct osmo_signalfd *
106osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data);
107
Harald Weltee61d4592022-11-03 11:05:58 +0100108void osmo_select_shutdown_request(void);
109int osmo_select_shutdown_requested(void);
110bool osmo_select_shutdown_done(void);
Harald Weltea70ac852020-04-17 19:20:01 +0200111
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200112/*! @} */