1370b324cSopenharmony_ci// ConsoleClose.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include "ConsoleClose.h"
6370b324cSopenharmony_ci
7370b324cSopenharmony_ci#ifndef UNDER_CE
8370b324cSopenharmony_ci
9370b324cSopenharmony_ci#ifdef _WIN32
10370b324cSopenharmony_ci#include "../../../Common/MyWindows.h"
11370b324cSopenharmony_ci#else
12370b324cSopenharmony_ci#include <stdlib.h>
13370b324cSopenharmony_ci#include <signal.h>
14370b324cSopenharmony_ci#endif
15370b324cSopenharmony_ci
16370b324cSopenharmony_cinamespace NConsoleClose {
17370b324cSopenharmony_ci
18370b324cSopenharmony_ciunsigned g_BreakCounter = 0;
19370b324cSopenharmony_cistatic const unsigned kBreakAbortThreshold = 2;
20370b324cSopenharmony_ci
21370b324cSopenharmony_ci#ifdef _WIN32
22370b324cSopenharmony_ci
23370b324cSopenharmony_cistatic BOOL WINAPI HandlerRoutine(DWORD ctrlType)
24370b324cSopenharmony_ci{
25370b324cSopenharmony_ci  if (ctrlType == CTRL_LOGOFF_EVENT)
26370b324cSopenharmony_ci  {
27370b324cSopenharmony_ci    // printf("\nCTRL_LOGOFF_EVENT\n");
28370b324cSopenharmony_ci    return TRUE;
29370b324cSopenharmony_ci  }
30370b324cSopenharmony_ci
31370b324cSopenharmony_ci  g_BreakCounter++;
32370b324cSopenharmony_ci  if (g_BreakCounter < kBreakAbortThreshold)
33370b324cSopenharmony_ci    return TRUE;
34370b324cSopenharmony_ci  return FALSE;
35370b324cSopenharmony_ci  /*
36370b324cSopenharmony_ci  switch (ctrlType)
37370b324cSopenharmony_ci  {
38370b324cSopenharmony_ci    case CTRL_C_EVENT:
39370b324cSopenharmony_ci    case CTRL_BREAK_EVENT:
40370b324cSopenharmony_ci      if (g_BreakCounter < kBreakAbortThreshold)
41370b324cSopenharmony_ci      return TRUE;
42370b324cSopenharmony_ci  }
43370b324cSopenharmony_ci  return FALSE;
44370b324cSopenharmony_ci  */
45370b324cSopenharmony_ci}
46370b324cSopenharmony_ci
47370b324cSopenharmony_ciCCtrlHandlerSetter::CCtrlHandlerSetter()
48370b324cSopenharmony_ci{
49370b324cSopenharmony_ci  if (!SetConsoleCtrlHandler(HandlerRoutine, TRUE))
50370b324cSopenharmony_ci    throw "SetConsoleCtrlHandler fails";
51370b324cSopenharmony_ci}
52370b324cSopenharmony_ci
53370b324cSopenharmony_ciCCtrlHandlerSetter::~CCtrlHandlerSetter()
54370b324cSopenharmony_ci{
55370b324cSopenharmony_ci  if (!SetConsoleCtrlHandler(HandlerRoutine, FALSE))
56370b324cSopenharmony_ci  {
57370b324cSopenharmony_ci    // warning for throw in destructor.
58370b324cSopenharmony_ci    // throw "SetConsoleCtrlHandler fails";
59370b324cSopenharmony_ci  }
60370b324cSopenharmony_ci}
61370b324cSopenharmony_ci
62370b324cSopenharmony_ci#else // _WIN32
63370b324cSopenharmony_ci
64370b324cSopenharmony_cistatic void HandlerRoutine(int)
65370b324cSopenharmony_ci{
66370b324cSopenharmony_ci  g_BreakCounter++;
67370b324cSopenharmony_ci  if (g_BreakCounter < kBreakAbortThreshold)
68370b324cSopenharmony_ci    return;
69370b324cSopenharmony_ci  exit(EXIT_FAILURE);
70370b324cSopenharmony_ci}
71370b324cSopenharmony_ci
72370b324cSopenharmony_ciCCtrlHandlerSetter::CCtrlHandlerSetter()
73370b324cSopenharmony_ci{
74370b324cSopenharmony_ci  memo_sig_int = signal(SIGINT, HandlerRoutine); // CTRL-C
75370b324cSopenharmony_ci  if (memo_sig_int == SIG_ERR)
76370b324cSopenharmony_ci    throw "SetConsoleCtrlHandler fails (SIGINT)";
77370b324cSopenharmony_ci  memo_sig_term = signal(SIGTERM, HandlerRoutine); // for kill -15 (before "kill -9")
78370b324cSopenharmony_ci  if (memo_sig_term == SIG_ERR)
79370b324cSopenharmony_ci    throw "SetConsoleCtrlHandler fails (SIGTERM)";
80370b324cSopenharmony_ci}
81370b324cSopenharmony_ci
82370b324cSopenharmony_ciCCtrlHandlerSetter::~CCtrlHandlerSetter()
83370b324cSopenharmony_ci{
84370b324cSopenharmony_ci  signal(SIGINT, memo_sig_int); // CTRL-C
85370b324cSopenharmony_ci  signal(SIGTERM, memo_sig_term); // kill {pid}
86370b324cSopenharmony_ci}
87370b324cSopenharmony_ci
88370b324cSopenharmony_ci#endif // _WIN32
89370b324cSopenharmony_ci
90370b324cSopenharmony_ci/*
91370b324cSopenharmony_civoid CheckCtrlBreak()
92370b324cSopenharmony_ci{
93370b324cSopenharmony_ci  if (TestBreakSignal())
94370b324cSopenharmony_ci    throw CCtrlBreakException();
95370b324cSopenharmony_ci}
96370b324cSopenharmony_ci*/
97370b324cSopenharmony_ci
98370b324cSopenharmony_ci}
99370b324cSopenharmony_ci
100370b324cSopenharmony_ci#endif
101