Change in libosmocore[master]: osmo_select_shutdown: allow finishing pending writes on sigterm

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

neels gerrit-no-reply at lists.osmocom.org
Fri Jun 4 14:36:42 UTC 2021


neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/24544 )


Change subject: osmo_select_shutdown: allow finishing pending writes on sigterm
......................................................................

osmo_select_shutdown: allow finishing pending writes on sigterm

So far various osmo programs have a curious sleep of two seconds upon
receiving SIGTERM. The idea presumably was to finish off pending writes
before halting the program. But a sleep(2) on program exit is annoying,
especially when there usually are no pending writes, and when osmo-bsc
is launched numerous times for tests.

Allow running osmo_select_main* to only service pending writes.
Introduce a global boolean flag to indicate a shutdown request, and a
second boolean flag to indicate shutdown is complete.

Change-Id: Ib94d4316924103459577087c2214188679db2227
---
M include/osmocom/core/select.h
M src/select.c
2 files changed, 47 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/44/24544/1

diff --git a/include/osmocom/core/select.h b/include/osmocom/core/select.h
index b410199..11742da 100644
--- a/include/osmocom/core/select.h
+++ b/include/osmocom/core/select.h
@@ -27,6 +27,9 @@
 #define BSC_FD_WRITE	OSMO_FD_WRITE
 #define BSC_FD_EXCEPT	OSMO_FD_EXCEPT
 
+extern bool osmo_select_shutdown;
+extern bool osmo_select_shutdown_done;
+
 /*! Structure representing a file dsecriptor */
 struct osmo_fd {
 	/*! linked list for internal management */
diff --git a/src/select.c b/src/select.c
index 71ee7f6..e3bbbb0 100644
--- a/src/select.c
+++ b/src/select.c
@@ -69,6 +69,39 @@
 static __thread struct poll_state g_poll;
 #endif /* FORCE_IO_SELECT */
 
+/*! A calling main program may request osmo_select_* to only service pending OSMO_FD_WRITE requests. Once all writes are
+ * done, osmo_select_shutdown_done is set to true. This allows for example to send all outbound packets before
+ * terminating the process.
+ *
+ * Usage example:
+ *
+ * static void signal_handler(int signum)
+ * {
+ *         fprintf(stdout, "signal %u received\n", signum);
+ *
+ *         switch (signum) {
+ *         case SIGINT:
+ *         case SIGTERM:
+ *                 // If SIGTERM was already sent before, just terminate immediately this time around.
+ *                 if (osmo_select_shutdown)
+ *                         exit(-1);
+ *                 osmo_select_shutdown = true;
+ *                 break;
+ *         [...]
+ * }
+ *
+ * main()
+ * {
+ *         [...]
+ *         while (!osmo_select_shutdown_done) {
+ *                 osmo_select_main_ctx(0);
+ *         }
+ * }
+ */
+bool osmo_select_shutdown = false;
+/*! Set to true when osmo_select_shutdown is true and osmo_select's poll loop found no pending write operations. */
+bool osmo_select_shutdown_done = false;
+
 /*! Set up an osmo-fd. Will not register it.
  *  \param[inout] ofd Osmo FD to be set-up
  *  \param[in] fd OS-level file descriptor number
@@ -316,6 +349,7 @@
 	struct osmo_fd *ufd;
 	unsigned int i;
 	int work = 0;
+	int shutdown_pending_writes = 0;
 
 	for (i = 0; i < n_fd; i++) {
 		struct pollfd *p = &g_poll.poll[i];
@@ -340,6 +374,11 @@
 		/* make sure we never report more than the user requested */
 		flags &= ufd->when;
 
+		if (osmo_select_shutdown) {
+			if (ufd->when & OSMO_FD_WRITE)
+				shutdown_pending_writes++;
+		}
+
 		if (flags) {
 			work = 1;
 			/* make sure to clear any log context before processing the next incoming message
@@ -351,6 +390,9 @@
 		}
 	}
 
+	if (osmo_select_shutdown && !shutdown_pending_writes)
+		osmo_select_shutdown_done = true;
+
 	return work;
 }
 
@@ -370,7 +412,8 @@
 		return 0;
 
 	/* fire timers */
-	osmo_timers_update();
+	if (!osmo_select_shutdown)
+		osmo_timers_update();
 
 	OSMO_ASSERT(osmo_ctx->select);
 

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/24544
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ib94d4316924103459577087c2214188679db2227
Gerrit-Change-Number: 24544
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210604/d8ba27d2/attachment.htm>


More information about the gerrit-log mailing list