Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file select.h |
| 2 | * select loop abstraction. |
| 3 | */ |
| 4 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 5 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 6 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 7 | #include <osmocom/core/linuxlist.h> |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 8 | #include <stdbool.h> |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 9 | #include <time.h> |
Harald Welte | a70ac85 | 2020-04-17 19:20:01 +0200 | [diff] [blame] | 10 | #include <signal.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 11 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 12 | /*! \defgroup select Select loop abstraction |
| 13 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 14 | * \file select.h */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 15 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 16 | /*! Indicate interest in reading from the file descriptor */ |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 17 | #define OSMO_FD_READ 0x0001 |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 18 | /*! Indicate interest in writing to the file descriptor */ |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 19 | #define OSMO_FD_WRITE 0x0002 |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 20 | /*! Indicate interest in exceptions from the file descriptor */ |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 21 | #define OSMO_FD_EXCEPT 0x0004 |
Harald Welte | 7e65791 | 2020-10-18 22:07:21 +0200 | [diff] [blame] | 22 | /*! Used as when_mask in osmo_fd_update_when() */ |
| 23 | #define OSMO_FD_MASK 0xFFFF |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 24 | |
| 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 Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 29 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 30 | /*! Structure representing a file dsecriptor */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 31 | struct osmo_fd { |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 32 | /*! linked list for internal management */ |
| 33 | struct llist_head list; |
| 34 | /*! actual operating-system level file decriptor */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | int fd; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 36 | /*! bit-mask or of \ref OSMO_FD_READ, \ref OSMO_FD_WRITE and/or |
| 37 | * \ref OSMO_FD_EXCEPT */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 38 | unsigned int when; |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 39 | /*! call-back function to be called once file descriptor becomes |
| 40 | * available */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 41 | int (*cb)(struct osmo_fd *fd, unsigned int what); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 42 | /*! data pointer passed through to call-back function */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 43 | void *data; |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 44 | /*! private number, extending \a data */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | unsigned int priv_nr; |
| 46 | }; |
| 47 | |
Harald Welte | 6c0a0e6 | 2017-08-12 11:43:14 +0200 | [diff] [blame] | 48 | void 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 Welte | 7e65791 | 2020-10-18 22:07:21 +0200 | [diff] [blame] | 52 | void osmo_fd_update_when(struct osmo_fd *ofd, unsigned int when_mask, unsigned int when); |
| 53 | |
| 54 | static inline void osmo_fd_read_enable(struct osmo_fd *ofd) { |
| 55 | osmo_fd_update_when(ofd, OSMO_FD_MASK, OSMO_FD_READ); |
| 56 | } |
| 57 | |
| 58 | static inline void osmo_fd_read_disable(struct osmo_fd *ofd) { |
| 59 | osmo_fd_update_when(ofd, ~OSMO_FD_READ, 0); |
| 60 | } |
| 61 | |
| 62 | static inline void osmo_fd_write_enable(struct osmo_fd *ofd) { |
| 63 | osmo_fd_update_when(ofd, OSMO_FD_MASK, OSMO_FD_WRITE); |
| 64 | } |
| 65 | |
| 66 | static inline void osmo_fd_write_disable(struct osmo_fd *ofd) { |
| 67 | osmo_fd_update_when(ofd, ~OSMO_FD_WRITE, 0); |
| 68 | } |
| 69 | |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 70 | bool osmo_fd_is_registered(struct osmo_fd *fd); |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 71 | int osmo_fd_register(struct osmo_fd *fd); |
| 72 | void osmo_fd_unregister(struct osmo_fd *fd); |
Harald Welte | ea91a51 | 2017-07-13 14:28:30 +0200 | [diff] [blame] | 73 | void osmo_fd_close(struct osmo_fd *fd); |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 74 | int osmo_select_main(int polling); |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 75 | int osmo_select_main_ctx(int polling); |
Harald Welte | 7a010b1 | 2019-04-06 13:46:40 +0200 | [diff] [blame] | 76 | void osmo_select_init(void); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 77 | |
Harald Welte | 6c33ae2 | 2016-03-19 21:17:58 +0100 | [diff] [blame] | 78 | struct osmo_fd *osmo_fd_get_by_fd(int fd); |
| 79 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 80 | /* |
| 81 | * foreign event loop integration |
| 82 | */ |
| 83 | int osmo_fd_fill_fds(void *readset, void *writeset, void *exceptset); |
| 84 | int osmo_fd_disp_fds(void *readset, void *writeset, void *exceptset); |
| 85 | |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 86 | /* timerfd integration */ |
| 87 | int osmo_timerfd_disable(struct osmo_fd *ofd); |
| 88 | int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first, |
| 89 | const struct timespec *interval); |
| 90 | int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data); |
| 91 | |
Harald Welte | a70ac85 | 2020-04-17 19:20:01 +0200 | [diff] [blame] | 92 | /* signalfd integration */ |
| 93 | struct osmo_signalfd; |
| 94 | struct signalfd_siginfo; |
| 95 | |
| 96 | typedef void osmo_signalfd_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi); |
| 97 | |
| 98 | struct osmo_signalfd { |
| 99 | struct osmo_fd ofd; |
| 100 | sigset_t sigset; |
| 101 | osmo_signalfd_cb *cb; |
| 102 | void *data; |
| 103 | }; |
| 104 | |
| 105 | struct osmo_signalfd * |
| 106 | osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data); |
| 107 | |
| 108 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 109 | /*! @} */ |