Reimplement itti_wait_tasks_end() to allow custom wait handlers

In the UE, we wish to handle ^C in the UE to send a message before
terminating, so we allow custom wait handlers to implement such
functionality. Use itti_wait_tasks_unblock() to unblock
itti_wait_tasks_end().

Also, implement itti_wait_tasks_end() using semaphores, as signals are
not async-signal-safe (and therefore should not be used in a signal
handler).
This commit is contained in:
Robert Schmidt
2023-03-28 14:12:05 +02:00
parent 4700dde2ae
commit a6486909ca
7 changed files with 29 additions and 17 deletions
+18 -9
View File
@@ -24,6 +24,7 @@
#include <vector>
#include <map>
#include <sys/eventfd.h>
#include <semaphore.h>
extern "C" {
@@ -434,24 +435,32 @@ extern "C" {
void itti_send_terminate_message(task_id_t task_id) {
}
pthread_mutex_t signal_mutex;
sem_t itti_sem_block;
void itti_wait_tasks_unblock()
{
int rc = sem_post(&itti_sem_block);
AssertFatal(rc == 0, "error in sem_post(): %d %s\n", errno, strerror(errno));
}
static void catch_sigterm(int) {
static const char msg[] = "\n** Caught SIGTERM, shutting down\n";
__attribute__((unused))
int unused = write(STDOUT_FILENO, msg, sizeof(msg) - 1);
pthread_mutex_unlock(&signal_mutex);
itti_wait_tasks_unblock();
}
void itti_wait_tasks_end(void) {
void itti_wait_tasks_end(void (*handler)(int))
{
int rc = sem_init(&itti_sem_block, 0, 0);
AssertFatal(rc == 0, "error in sem_init(): %d %s\n", errno, strerror(errno));
pthread_mutex_init(&signal_mutex, NULL);
pthread_mutex_lock(&signal_mutex);
if (handler == NULL) /* no handler given: install default */
handler = catch_sigterm;
signal(SIGTERM, handler);
signal(SIGINT, handler);
signal(SIGTERM, catch_sigterm);
signal(SIGINT, catch_sigterm);
pthread_mutex_lock(&signal_mutex);
rc = sem_wait(&itti_sem_block);
AssertFatal(rc == 0, "error in sem_wait(): %d %s\n", errno, strerror(errno));
}
void itti_update_lte_time(uint32_t frame, uint8_t slot) {}