[PATCH] libosmocore[master]: select: add functionality to check socket state

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/.

dexter gerrit-no-reply at lists.osmocom.org
Fri Feb 3 11:41:13 UTC 2017


Hello Harald Welte, Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/1725

to look at the new patch set (#2).

select: add functionality to check socket state

osmo_fd_register() is used to register socket file descriptors,
after registering a socket, there is no way to test if the socket
is still registered or actually registered at all.

This commit adds a new function osmo_fd_register_check() that can
be used to check in advance, if the socket fd is registered,
before performing further operations.

Change-Id: I48ec7098d6bba586c81bf0d5c9088108e2c081c6
---
M include/osmocom/core/select.h
M src/select.c
2 files changed, 26 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/25/1725/2

diff --git a/include/osmocom/core/select.h b/include/osmocom/core/select.h
index 2753637..c2b51a4 100644
--- a/include/osmocom/core/select.h
+++ b/include/osmocom/core/select.h
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <osmocom/core/linuxlist.h>
+#include <stdbool.h>
 
 /*! \defgroup select Select loop abstraction
  *  @{
@@ -35,6 +36,7 @@
 	unsigned int priv_nr;
 };
 
+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);
 int osmo_select_main(int polling);
diff --git a/src/select.c b/src/select.c
index da27368..ab0734e 100644
--- a/src/select.c
+++ b/src/select.c
@@ -23,6 +23,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdbool.h>
 #include <sys/select.h>
 
 #include <osmocom/core/select.h>
@@ -44,6 +45,23 @@
 static int maxfd = 0;
 static LLIST_HEAD(osmo_fds);
 static int unregistered_count;
+
+
+/*! \brief Check if a file descriptor is already registered
+ *  \param[in] fd osmocom file descriptor to be checked
+ *  \returns true if registered; otherwise false
+ */
+bool osmo_fd_is_registered(struct osmo_fd *fd)
+{
+	struct osmo_fd *entry;
+	llist_for_each_entry(entry, &osmo_fds, list) {
+		if (entry == fd) {
+			return true;
+		}
+	}
+
+	return false;
+}
 
 /*! \brief Register a new file descriptor with select loop abstraction
  *  \param[in] fd osmocom file descriptor to be registered
@@ -76,12 +94,9 @@
 		maxfd = fd->fd;
 
 #ifdef BSC_FD_CHECK
-	struct osmo_fd *entry;
-	llist_for_each_entry(entry, &osmo_fds, list) {
-		if (entry == fd) {
-			fprintf(stderr, "Adding a osmo_fd that is already in the list.\n");
-			return 0;
-		}
+	if (osmo_fd_is_registered(fd)) {
+		fprintf(stderr, "Adding a osmo_fd that is already in the list.\n");
+		return 0;
 	}
 #endif
 
@@ -95,6 +110,9 @@
  */
 void osmo_fd_unregister(struct osmo_fd *fd)
 {
+	/* Note: when fd is inside the osmo_fds list (not registered before)
+	 * this function will crash! If in doubt, check file descriptor with
+	 * osmo_fd_is_registered() */
 	unregistered_count++;
 	llist_del(&fd->list);
 }

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

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I48ec7098d6bba586c81bf0d5c9088108e2c081c6
Gerrit-PatchSet: 2
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list