aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-07-13 14:28:30 +0200
committerHarald Welte <laforge@gnumonks.org>2017-07-13 16:03:37 +0200
commitea91a51ebe8897772e3d1b45e4405c8f9e2fedb0 (patch)
tree43ede8b00e65f804d551a8bab60f9d5eb2c2b9d3
parentaa3ba46e0f9dcfb1236a10f6b8f09b1f33330778 (diff)
select: Add new osmo_fd_close() function
This is a convenience helper that will both close a fd, mark it as closed and unregister it from the event loop abstraction. In most cases, you probably actually want to use it instead of manually closing and calling osmo_fd_unregister(). Change-Id: Icd0933eed6a24edde7cdcb378e138897ecc5332c
-rw-r--r--include/osmocom/core/select.h1
-rw-r--r--src/select.c17
2 files changed, 18 insertions, 0 deletions
diff --git a/include/osmocom/core/select.h b/include/osmocom/core/select.h
index 9b5f372b..2abda2dc 100644
--- a/include/osmocom/core/select.h
+++ b/include/osmocom/core/select.h
@@ -39,6 +39,7 @@ struct osmo_fd {
bool osmo_fd_is_registered(struct osmo_fd *fd);
int osmo_fd_register(struct osmo_fd *fd);
void osmo_fd_unregister(struct osmo_fd *fd);
+void osmo_fd_close(struct osmo_fd *fd);
int osmo_select_main(int polling);
struct osmo_fd *osmo_fd_get_by_fd(int fd);
diff --git a/src/select.c b/src/select.c
index f7ee424c..0ba8bc60 100644
--- a/src/select.c
+++ b/src/select.c
@@ -24,6 +24,7 @@
#include <fcntl.h>
#include <stdio.h>
+#include <unistd.h>
#include <string.h>
#include <stdbool.h>
@@ -117,6 +118,22 @@ void osmo_fd_unregister(struct osmo_fd *fd)
llist_del(&fd->list);
}
+/*! Close a file descriptor, mark it as closed + unregister from select loop abstraction
+ * \param[in] fd osmocom file descriptor to be unregistered + closed
+ *
+ * If \a fd is registered, we unregister it from the select() loop
+ * abstraction. We then close the fd and set it to -1, as well as
+ * unsetting any 'when' flags */
+void osmo_fd_close(struct osmo_fd *fd)
+{
+ if (osmo_fd_is_registered(fd))
+ osmo_fd_unregister(fd);
+ if (fd->fd != -1)
+ close(fd->fd);
+ fd->fd = -1;
+ fd->when = 0;
+}
+
/*! Populate the fd_sets and return the highest fd number
* \param[in] _rset The readfds to populate
* \param[in] _wset The wrtiefds to populate