Skip to content

Commit

Permalink
Fixed SGX main loop not handling signals properly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
italo-sampaio committed Jan 24, 2025
1 parent 1ec2d35 commit cc0bf99
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions firmware/src/sgx/src/untrusted/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cc0bf99

Please sign in to comment.