summaryrefslogtreecommitdiffstats
path: root/src/libs/7zip/unix/CPP/7zip/UI/Console/ConsoleClose.cpp
blob: 9bb2082bfda4fe77991fb1f6a5e728a2a95c5dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// ConsoleClose.cpp

#include "StdAfx.h"

#include "ConsoleClose.h"

#include <signal.h>

static int g_BreakCounter = 0;
static const int kBreakAbortThreshold = 2;

namespace NConsoleClose {

static void HandlerRoutine(int)
{
  g_BreakCounter++;
  if (g_BreakCounter < kBreakAbortThreshold)
    return ;
  exit(EXIT_FAILURE);
}

bool TestBreakSignal()
{
  return (g_BreakCounter > 0);
}

void CheckCtrlBreak()
{
  if (TestBreakSignal())
    throw CCtrlBreakException();
}

CCtrlHandlerSetter::CCtrlHandlerSetter()
{
   memo_sig_int = signal(SIGINT,HandlerRoutine); // CTRL-C
   if (memo_sig_int == SIG_ERR)
    throw "SetConsoleCtrlHandler fails (SIGINT)";
   memo_sig_term = signal(SIGTERM,HandlerRoutine); // for kill -15 (before "kill -9")
   if (memo_sig_term == SIG_ERR)
    throw "SetConsoleCtrlHandler fails (SIGTERM)";
}

CCtrlHandlerSetter::~CCtrlHandlerSetter()
{
   signal(SIGINT,memo_sig_int); // CTRL-C
   signal(SIGTERM,memo_sig_term); // kill {pid}
}

}