Skip to content

Commit 3759832

Browse files
committed
Graceful shutdown for SIGTERM and SIGINT
1 parent 92697f8 commit 3759832

4 files changed

Lines changed: 20 additions & 16 deletions

File tree

CRT.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -951,29 +951,22 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
951951

952952
static bool CRT_retainScreenOnExit = false;
953953

954+
volatile sig_atomic_t terminate_requested = 0;
955+
volatile sig_atomic_t terminate_signal = 0;
956+
954957
int CRT_scrollHAmount = 5;
955958

956959
int CRT_scrollWheelVAmount = 10;
957960

958961
ColorScheme CRT_colorScheme = COLORSCHEME_DEFAULT;
959962

960-
ATTR_NORETURN
961963
static void CRT_handleSIGTERM(int sgn) {
962-
CRT_done();
963-
964-
if (!CRT_settings->changed)
965-
_exit(0);
966-
967-
const char* signal_str = strsignal(sgn);
968-
if (!signal_str)
969-
signal_str = "unknown reason";
964+
if (terminate_requested) {
965+
_exit(128 + sgn);
966+
}
970967

971-
char err_buf[512];
972-
snprintf(err_buf, sizeof(err_buf),
973-
"A signal %d (%s) was received, exiting without persisting settings to htoprc.\n",
974-
sgn, signal_str);
975-
full_write_str(STDERR_FILENO, err_buf);
976-
_exit(0);
968+
terminate_signal = (sig_atomic_t)sgn;
969+
terminate_requested = (sig_atomic_t)1;
977970
}
978971

979972
#ifndef NDEBUG

CRT.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ in the source distribution for its full text.
1212
#include "Macros.h"
1313
#include "ProvideCurses.h"
1414
#include "Settings.h"
15+
#include "signal.h"
1516

1617

1718
#define SCREEN_TAB_MARGIN_LEFT 2
@@ -202,6 +203,10 @@ extern int CRT_scrollWheelVAmount;
202203

203204
extern ColorScheme CRT_colorScheme;
204205

206+
extern volatile sig_atomic_t terminate_requested;
207+
208+
extern volatile sig_atomic_t terminate_signal;
209+
205210
#ifdef HAVE_GETMOUSE
206211
void CRT_setMouse(bool enabled);
207212
#else

CommandLine.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,12 @@ int CommandLine_run(int argc, char** argv) {
419419

420420
CRT_done();
421421

422+
if (CRT_terminated()) {
423+
int sgn = CRT_getSignal();
424+
fprintf(stderr, "\nInterrupted by signal %d, terminating.\n", sgn);
425+
exit(128 + sgn);
426+
}
427+
422428
if (settings->changed) {
423429
#ifndef NDEBUG
424430
if (!String_eq(settings->initialFilename, settings->filename))

ScreenManager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey, con
257257

258258
this->name = name;
259259

260-
while (!quit) {
260+
while (!quit && !terminate_requested) {
261261
if (this->header) {
262262
checkRecalculation(this, &oldTime, &sortTimeout, &redraw, &rescan, &timedOut, &force_redraw);
263263
}

0 commit comments

Comments
 (0)