| 1 |
/**************************************************************************** |
| 2 |
* * |
| 3 |
* GNAT COMPILER COMPONENTS * |
| 4 |
* * |
| 5 |
* C T R L _ C * |
| 6 |
* * |
| 7 |
* C Implementation File * |
| 8 |
* * |
| 9 |
* Copyright (C) 2002-2025, Free Software Foundation, Inc. * |
| 10 |
* * |
| 11 |
* GNAT is free software; you can redistribute it and/or modify it under * |
| 12 |
* terms of the GNU General Public License as published by the Free Soft- * |
| 13 |
* ware Foundation; either version 3, or (at your option) any later ver- * |
| 14 |
* sion. GNAT is distributed in the hope that it will be useful, but WITH- * |
| 15 |
* OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * |
| 16 |
* or FITNESS FOR A PARTICULAR PURPOSE. * |
| 17 |
* * |
| 18 |
* As a special exception under Section 7 of GPL version 3, you are granted * |
| 19 |
* additional permissions described in the GCC Runtime Library Exception, * |
| 20 |
* version 3.1, as published by the Free Software Foundation. * |
| 21 |
* * |
| 22 |
* You should have received a copy of the GNU General Public License and * |
| 23 |
* a copy of the GCC Runtime Library Exception along with this program; * |
| 24 |
* see the files COPYING3 and COPYING.RUNTIME respectively. If not, see * |
| 25 |
* <http://www.gnu.org/licenses/>. * |
| 26 |
* * |
| 27 |
* GNAT was originally developed by the GNAT team at New York University. * |
| 28 |
* Extensive contributions were provided by Ada Core Technologies Inc. * |
| 29 |
* * |
| 30 |
****************************************************************************/ |
| 31 |
|
| 32 |
#ifndef IN_RTS |
| 33 |
#include "config.h" |
| 34 |
#include "system.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
/* Services to intercept Ctrl-C */ |
| 38 |
|
| 39 |
/* __gnat_install_int_handler will install the specified handler. |
| 40 |
If called for the first time, it will also save the original handler */ |
| 41 |
void __gnat_install_int_handler (void (*) (void)); |
| 42 |
|
| 43 |
/* __gnat_uninstall_int_handler will reinstall the original handler */ |
| 44 |
void __gnat_uninstall_int_handler (void); |
| 45 |
|
| 46 |
/* POSIX implementation */ |
| 47 |
|
| 48 |
#if (defined (__unix__) || defined (_AIX) || defined (__APPLE__)) \ |
| 49 |
|| defined (VMS) && !defined (__vxworks) |
| 50 |
|
| 51 |
#ifdef VMS |
| 52 |
/* On VMS _gnat_handle_vms_condition gets control first, and it has to |
| 53 |
resignal the Ctrl/C in order for sigaction to gain control and execute |
| 54 |
the user handler routine, but in doing so propagates the condition |
| 55 |
causing the program to terminate. So instead we install a dummy handler |
| 56 |
routine and put the real user handler in a special global variable so |
| 57 |
that __gnat_handle_vms_condition can declare an AST to asynchronously |
| 58 |
execute the Ctrl/C user handler at some future time and allow |
| 59 |
__gnat_handle_vms_condition to return and not be held up waiting for |
| 60 |
the potentially unbounded time required to execute the Crtl/C handler. */ |
| 61 |
void |
| 62 |
dummy_handler () {} |
| 63 |
|
| 64 |
/* Lives in init.c. */ |
| 65 |
extern void (*__gnat_ctrl_c_handler) (void); |
| 66 |
#endif |
| 67 |
|
| 68 |
#include <signal.h> |
| 69 |
|
| 70 |
void (*sigint_intercepted) (void) = 0; |
| 71 |
|
| 72 |
struct sigaction original_act; |
| 73 |
|
| 74 |
static void |
| 75 |
__gnat_int_handler (int sig __attribute__ ((unused))) |
| 76 |
{ |
| 77 |
if (sigint_intercepted != 0) |
| 78 |
sigint_intercepted (); |
| 79 |
} |
| 80 |
|
| 81 |
/* Install handler and save original handler. */ |
| 82 |
|
| 83 |
void |
| 84 |
__gnat_install_int_handler (void (*proc) (void)) |
| 85 |
{ |
| 86 |
struct sigaction act; |
| 87 |
|
| 88 |
if (sigint_intercepted == 0) |
| 89 |
{ |
| 90 |
act.sa_handler = __gnat_int_handler; |
| 91 |
#if defined (__Lynx__) || defined (VMS) || defined(__DJGPP__) |
| 92 |
/* LynxOS, VMS and DJGPP do not support SA_RESTART. */ |
| 93 |
act.sa_flags = 0; |
| 94 |
#else |
| 95 |
act.sa_flags = SA_RESTART; |
| 96 |
#endif |
| 97 |
sigemptyset (&act.sa_mask); |
| 98 |
sigaction (SIGINT, &act, &original_act); |
| 99 |
} |
| 100 |
|
| 101 |
#ifdef VMS |
| 102 |
sigint_intercepted = &dummy_handler; |
| 103 |
__gnat_ctrl_c_handler = proc; |
| 104 |
#else |
| 105 |
sigint_intercepted = proc; |
| 106 |
#endif |
| 107 |
} |
| 108 |
|
| 109 |
/* Restore original handler */ |
| 110 |
|
| 111 |
void |
| 112 |
__gnat_uninstall_int_handler (void) |
| 113 |
{ |
| 114 |
if (sigint_intercepted != 0) |
| 115 |
{ |
| 116 |
sigaction (SIGINT, &original_act, 0); |
| 117 |
sigint_intercepted = 0; |
| 118 |
} |
| 119 |
#ifdef VMS |
| 120 |
if (__gnat_ctrl_c_handler) |
| 121 |
__gnat_ctrl_c_handler = 0; |
| 122 |
#endif |
| 123 |
} |
| 124 |
|
| 125 |
/* Windows implementation */ |
| 126 |
|
| 127 |
#elif defined (__MINGW32__) |
| 128 |
|
| 129 |
#define WIN32_LEAN_AND_MEAN |
| 130 |
#include "mingw32.h" |
| 131 |
#include <windows.h> |
| 132 |
|
| 133 |
void (*sigint_intercepted) (void) = NULL; |
| 134 |
|
| 135 |
static BOOL WINAPI |
| 136 |
__gnat_int_handler (DWORD dwCtrlType) |
| 137 |
{ |
| 138 |
switch (dwCtrlType) |
| 139 |
{ |
| 140 |
case CTRL_C_EVENT: |
| 141 |
case CTRL_BREAK_EVENT: |
| 142 |
if (sigint_intercepted != 0) |
| 143 |
{ |
| 144 |
sigint_intercepted (); |
| 145 |
return TRUE; |
| 146 |
} |
| 147 |
break; |
| 148 |
|
| 149 |
case CTRL_CLOSE_EVENT: |
| 150 |
case CTRL_LOGOFF_EVENT: |
| 151 |
case CTRL_SHUTDOWN_EVENT: |
| 152 |
break; |
| 153 |
} |
| 154 |
|
| 155 |
return FALSE; |
| 156 |
} |
| 157 |
|
| 158 |
void |
| 159 |
__gnat_install_int_handler (void (*proc) (void)) |
| 160 |
{ |
| 161 |
if (sigint_intercepted == NULL) |
| 162 |
SetConsoleCtrlHandler (__gnat_int_handler, TRUE); |
| 163 |
|
| 164 |
sigint_intercepted = proc; |
| 165 |
} |
| 166 |
|
| 167 |
void |
| 168 |
__gnat_uninstall_int_handler (void) |
| 169 |
{ |
| 170 |
if (sigint_intercepted != NULL) |
| 171 |
SetConsoleCtrlHandler (__gnat_int_handler, FALSE); |
| 172 |
|
| 173 |
sigint_intercepted = NULL; |
| 174 |
} |
| 175 |
|
| 176 |
/* Default implementation: do nothing */ |
| 177 |
|
| 178 |
#else |
| 179 |
|
| 180 |
void |
| 181 |
__gnat_install_int_handler (void (*proc) (void) __attribute__ ((unused))) |
| 182 |
{ |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
__gnat_uninstall_int_handler (void) |
| 187 |
{ |
| 188 |
} |
| 189 |
#endif |