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

Handling of SIGTERM in kiwix-serve #488

Merged
merged 1 commit into from
Oct 18, 2021
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
35 changes: 34 additions & 1 deletion src/server/kiwix-serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# include <windows.h>
#else
# include <unistd.h>
# include <signal.h>
#endif

#ifdef __APPLE__
Expand Down Expand Up @@ -97,8 +98,40 @@ string loadCustomTemplate (string customIndexPath) {
return indexTemplateString;
}

volatile sig_atomic_t waiting = false;

#ifndef _WIN32
void handle_sigterm(int signum)
{
if ( waiting == false ) {
_exit(signum);
}
waiting = false;
}

typedef void (*SignalHandler)(int);

void set_signal_handler(int sig, SignalHandler handler)
{
struct sigaction sa;
sigaction(sig, NULL, &sa);
sa.sa_handler = handler;
sigaction(sig, &sa, NULL);
}

void setup_sighandlers()
{
set_signal_handler(SIGTERM, &handle_sigterm);
set_signal_handler(SIGINT, &handle_sigterm);
}
#endif

int main(int argc, char** argv)
{
#ifndef _WIN32
setup_sighandlers();
#endif

std::string rootLocation = "";
kiwix::Library library;
unsigned int nb_threads = DEFAULT_THREADS;
Expand Down Expand Up @@ -298,7 +331,7 @@ int main(int argc, char** argv)
}

/* Run endless (until PPID dies) */
bool waiting = true;
waiting = true;
do {
if (PPID > 0) {
#ifdef _WIN32
Expand Down