1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * C T R L _ C *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2002-2013, 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 #ifdef IN_RTS
33 #include "tconfig.h"
34 #include "tsystem.h"
35 #include <sys/stat.h>
36 #else
37 #include "config.h"
38 #include "system.h"
39 #endif
40
41 /* Services to intercept Ctrl-C */
42
43 /* __gnat_install_int_handler will install the specified handler.
44 If called for the first time, it will also save the original handler */
45 void __gnat_install_int_handler (void (*) (void));
46
47 /* __gnat_uninstall_int_handler will reinstall the original handler */
48 void __gnat_uninstall_int_handler (void);
49
50 /* POSIX implementation */
51
52 #if (defined (__unix__) || defined (_AIX) || defined (__APPLE__)) \
53 || defined (VMS) && !defined (__vxworks)
54
55 #ifdef VMS
56 /* On VMS _gnat_handle_vms_condition gets control first, and it has to
57 resignal the Ctrl/C in order for sigaction to gain control and execute
58 the user handler routine, but in doing so propagates the condition
59 causing the program to terminate. So instead we install a dummy handler
60 routine and put the real user handler in a special global variable so
61 that __gnat_handle_vms_condition can declare an AST to asynchronously
62 execute the Ctrl/C user handler at some future time and allow
63 __gnat_handle_vms_condition to return and not be held up waiting for
64 the potentially unbounded time required to execute the Crtl/C handler. */
65 void
dummy_handler()66 dummy_handler () {}
67
68 /* Lives in init.c. */
69 extern void (*__gnat_ctrl_c_handler) (void);
70 #endif
71
72 #include <signal.h>
73
74 void (*sigint_intercepted) (void) = 0;
75
76 struct sigaction original_act;
77
78 static void
__gnat_int_handler(int sig)79 __gnat_int_handler (int sig __attribute__ ((unused)))
80 {
81 if (sigint_intercepted != 0)
82 sigint_intercepted ();
83 }
84
85 /* Install handler and save original handler. */
86
87 void
__gnat_install_int_handler(void (* proc)(void))88 __gnat_install_int_handler (void (*proc) (void))
89 {
90 struct sigaction act;
91
92 if (sigint_intercepted == 0)
93 {
94 act.sa_handler = __gnat_int_handler;
95 #if defined (__Lynx__) || defined (VMS)
96 /* LynxOS and VMS do not support SA_RESTART. */
97 act.sa_flags = 0;
98 #else
99 act.sa_flags = SA_RESTART;
100 #endif
101 sigemptyset (&act.sa_mask);
102 sigaction (SIGINT, &act, &original_act);
103 }
104
105 #ifdef VMS
106 sigint_intercepted = &dummy_handler;
107 __gnat_ctrl_c_handler = proc;
108 #else
109 sigint_intercepted = proc;
110 #endif
111 }
112
113 /* Restore original handler */
114
115 void
__gnat_uninstall_int_handler(void)116 __gnat_uninstall_int_handler (void)
117 {
118 if (sigint_intercepted != 0)
119 {
120 sigaction (SIGINT, &original_act, 0);
121 sigint_intercepted = 0;
122 }
123 #ifdef VMS
124 if (__gnat_ctrl_c_handler)
125 __gnat_ctrl_c_handler = 0;
126 #endif
127 }
128
129 /* Windows implementation */
130
131 #elif defined (__MINGW32__)
132
133 #include "mingw32.h"
134 #include <windows.h>
135
136 void (*sigint_intercepted) (void) = NULL;
137
138 static BOOL WINAPI
__gnat_int_handler(DWORD dwCtrlType)139 __gnat_int_handler (DWORD dwCtrlType)
140 {
141 switch (dwCtrlType)
142 {
143 case CTRL_C_EVENT:
144 case CTRL_BREAK_EVENT:
145 if (sigint_intercepted != 0)
146 {
147 sigint_intercepted ();
148 return TRUE;
149 }
150 break;
151
152 case CTRL_CLOSE_EVENT:
153 case CTRL_LOGOFF_EVENT:
154 case CTRL_SHUTDOWN_EVENT:
155 break;
156 }
157
158 return FALSE;
159 }
160
161 void
__gnat_install_int_handler(void (* proc)(void))162 __gnat_install_int_handler (void (*proc) (void))
163 {
164 if (sigint_intercepted == NULL)
165 SetConsoleCtrlHandler (__gnat_int_handler, TRUE);
166
167 sigint_intercepted = proc;
168 }
169
170 void
__gnat_uninstall_int_handler(void)171 __gnat_uninstall_int_handler (void)
172 {
173 if (sigint_intercepted != NULL)
174 SetConsoleCtrlHandler (__gnat_int_handler, FALSE);
175
176 sigint_intercepted = NULL;
177 }
178
179 /* Default implementation: do nothing */
180
181 #else
182
183 void
__gnat_install_int_handler(void (* proc)(void))184 __gnat_install_int_handler (void (*proc) (void) __attribute__ ((unused)))
185 {
186 }
187
188 void
__gnat_uninstall_int_handler(void)189 __gnat_uninstall_int_handler (void)
190 {
191 }
192 #endif
193