[MERGED] libosmocore[master]: select: Add new osmo_fd_close() function

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Thu Jul 13 14:54:47 UTC 2017


Harald Welte has submitted this change and it was merged.

Change subject: select: Add new osmo_fd_close() function
......................................................................


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
---
M include/osmocom/core/select.h
M src/select.c
2 files changed, 18 insertions(+), 0 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/include/osmocom/core/select.h b/include/osmocom/core/select.h
index 9b5f372..2abda2d 100644
--- a/include/osmocom/core/select.h
+++ b/include/osmocom/core/select.h
@@ -39,6 +39,7 @@
 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 f7ee424..0ba8bc6 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 @@
 	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

-- 
To view, visit https://gerrit.osmocom.org/3228
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icd0933eed6a24edde7cdcb378e138897ecc5332c
Gerrit-PatchSet: 2
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list