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 |
| 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 Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 27 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 28 | /*! Structure representing a file dsecriptor */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 29 | struct osmo_fd { |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 30 | /*! linked list for internal management */ |
| 31 | struct llist_head list; |
| 32 | /*! actual operating-system level file decriptor */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 33 | int fd; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 34 | /*! bit-mask or of \ref OSMO_FD_READ, \ref OSMO_FD_WRITE and/or |
| 35 | * \ref OSMO_FD_EXCEPT */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 36 | unsigned int when; |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 37 | /*! call-back function to be called once file descriptor becomes |
| 38 | * available */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 39 | int (*cb)(struct osmo_fd *fd, unsigned int what); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 40 | /*! data pointer passed through to call-back function */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 41 | void *data; |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 42 | /*! private number, extending \a data */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 43 | unsigned int priv_nr; |
| 44 | }; |
| 45 | |
Harald Welte | 6c0a0e6 | 2017-08-12 11:43:14 +0200 | [diff] [blame] | 46 | void 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 Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 50 | bool osmo_fd_is_registered(struct osmo_fd *fd); |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 51 | int osmo_fd_register(struct osmo_fd *fd); |
| 52 | void osmo_fd_unregister(struct osmo_fd *fd); |
Harald Welte | ea91a51 | 2017-07-13 14:28:30 +0200 | [diff] [blame] | 53 | void osmo_fd_close(struct osmo_fd *fd); |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 54 | int osmo_select_main(int polling); |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 55 | int osmo_select_main_ctx(int polling); |
Harald Welte | 7a010b1 | 2019-04-06 13:46:40 +0200 | [diff] [blame] | 56 | void osmo_select_init(void); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 57 | |
Harald Welte | 6c33ae2 | 2016-03-19 21:17:58 +0100 | [diff] [blame] | 58 | struct osmo_fd *osmo_fd_get_by_fd(int fd); |
| 59 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 60 | /* |
| 61 | * foreign event loop integration |
| 62 | */ |
| 63 | int osmo_fd_fill_fds(void *readset, void *writeset, void *exceptset); |
| 64 | int osmo_fd_disp_fds(void *readset, void *writeset, void *exceptset); |
| 65 | |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 66 | /* timerfd integration */ |
| 67 | int osmo_timerfd_disable(struct osmo_fd *ofd); |
| 68 | int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first, |
| 69 | const struct timespec *interval); |
| 70 | int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data); |
| 71 | |
Harald Welte | a70ac85 | 2020-04-17 19:20:01 +0200 | [diff] [blame] | 72 | /* signalfd integration */ |
| 73 | struct osmo_signalfd; |
| 74 | struct signalfd_siginfo; |
| 75 | |
| 76 | typedef void osmo_signalfd_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi); |
| 77 | |
| 78 | struct osmo_signalfd { |
| 79 | struct osmo_fd ofd; |
| 80 | sigset_t sigset; |
| 81 | osmo_signalfd_cb *cb; |
| 82 | void *data; |
| 83 | }; |
| 84 | |
| 85 | struct osmo_signalfd * |
| 86 | osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data); |
| 87 | |
| 88 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 89 | /*! @} */ |