From cc0bf997ff1d608746d1e962174c064f0ec917b8 Mon Sep 17 00:00:00 2001 From: Italo Sampaio Date: Fri, 24 Jan 2025 09:35:29 -0300 Subject: [PATCH] Fixed SGX main loop not handling signals properly Main loop blocks while waiting for client data. For this reason, we can't just check for the stop condition synchronously. In any case, we still need to ensure that the finalise logic is only called once. This commit introduces a global counter to ensure finalise_with is only called once. --- firmware/src/sgx/src/untrusted/main.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/firmware/src/sgx/src/untrusted/main.c b/firmware/src/sgx/src/untrusted/main.c index a9fed8a5..6f73de8e 100644 --- a/firmware/src/sgx/src/untrusted/main.c +++ b/firmware/src/sgx/src/untrusted/main.c @@ -47,6 +47,9 @@ static struct argp_option options[] = { {"port", 'p', "PORT", 0, "Port to listen on", 0}, {0}}; +// Global counter to avoid multiple calls to finalise_with +static sig_atomic_t G_signal_counter = 0; + // Argument definitions for argp struct arguments { char *bind; @@ -103,17 +106,22 @@ static void finalise_with(int exit_code) { exit(exit_code); } -static void finalise(int signum) { +static void signal_handler(int signum) { (void)signum; // Suppress unused parameter warning + if (G_signal_counter++ > 0) { + // Signal has already been handled + return; + } + finalise_with(0); } static void set_signal_handlers() { - signal(SIGINT, finalise); - signal(SIGTERM, finalise); - signal(SIGHUP, finalise); - signal(SIGABRT, finalise); + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGHUP, signal_handler); + signal(SIGABRT, signal_handler); } int main(int argc, char **argv) {