From 5e63f11fab2dd3c59f100ced8d65c494ca7e6f57 Mon Sep 17 00:00:00 2001 From: Italo Sampaio <100376888+italo-sampaio@users.noreply.github.com> Date: Tue, 21 Jan 2025 10:25:52 -0300 Subject: [PATCH] Moves finalise logic out of signal handler (#268) Signal handler now only sets a flag that is checked in main --- firmware/src/sgx/src/untrusted/main.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/firmware/src/sgx/src/untrusted/main.c b/firmware/src/sgx/src/untrusted/main.c index 222c7add..25a0e4df 100644 --- a/firmware/src/sgx/src/untrusted/main.c +++ b/firmware/src/sgx/src/untrusted/main.c @@ -54,6 +54,9 @@ struct arguments { char *enclave_path; }; +// Global flag to indicate that the application should stop +static sig_atomic_t G_stop_requested = 0; + // Argp individual option parsing function static error_t parse_opt(int key, char *arg, struct argp_state *state) { struct arguments *arguments = state->input; @@ -106,7 +109,9 @@ static void finalise_with(int exit_code) { static void finalise(int signum) { (void)signum; // Suppress unused parameter warning - finalise_with(0); + // Note: Do not add any finalise logic directly here, just set the flag + // and let the main loop handle it + G_stop_requested = 1; } static void set_signal_handlers() { @@ -157,6 +162,10 @@ int main(int argc, char **argv) { unsigned int tx = 0; while (true) { + if (G_stop_requested) { + break; + } + rx = io_exchange(tx); if (rx) { @@ -164,9 +173,11 @@ int main(int argc, char **argv) { } } - LOG("Exited main loop unexpectedly\n"); + finalise_with(0); + return 0; main_error: + LOG("Exited main loop unexpectedly\n"); finalise_with(1); return 1; }