Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure proper shutdown when crashing in a thread #2332

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ void generate_backtrace(void)
#endif
}

/**
* @brief Terminates the program due to an error.
*
* This function sets the exit code to indicate failure and raises a SIGTERM
* signal to terminate the main process. It is intended to be called when a
* critical error occurs that requires the program to exit.
*/
static void terminate_error(void)
{
exit_code = EXIT_FAILURE;
raise(SIGTERM);
}

static void __attribute__((noreturn)) signal_handler(int sig, siginfo_t *si, void *context)
{
(void)context;
Expand Down Expand Up @@ -260,15 +273,27 @@ static void __attribute__((noreturn)) signal_handler(int sig, siginfo_t *si, voi
log_info("Asking parent pihole-FTL (PID %i) to shut down", (int)mpid);
kill(mpid, SIGRTMIN+2);
log_info("FTL fork terminated!");

// Terminate fork indicating failure
exit(EXIT_FAILURE);
}
else if(gettid() != getpid())
{
// This is a thread, signal to the main process to shut down
log_info("Shutting down thread...");
terminate_error();

// Exit the thread here, it failed anyway
pthread_exit(NULL);
}
else
{
// This is the main process
cleanup(EXIT_FAILURE);
}

// Terminate process indicating failure
exit(EXIT_FAILURE);
// Terminate process indicating failure
exit(EXIT_FAILURE);
}
}

static void SIGRT_handler(int signum, siginfo_t *si, void *context)
Expand Down Expand Up @@ -301,8 +326,7 @@ static void SIGRT_handler(int signum, siginfo_t *si, void *context)
else if(rtsig == 2)
{
// Terminate FTL indicating failure
exit_code = EXIT_FAILURE;
raise(SIGTERM);
terminate_error();
}
else if(rtsig == 3)
{
Expand Down
Loading