Debian Codegraph @

vhditools/vhditools_signal.c

raw symbols SCIP
1 /*
2 * Signal handling functions
3 *
4 * Copyright (C) 2012-2025, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <types.h>
24
25 #if defined( HAVE_SIGNAL_H )
26 #include <signal.h>
27 #endif
28
29 #if defined( _MSC_VER )
30 #include <crtdbg.h>
31 #endif
32
33 #include "vhditools_libcerror.h"
34 #include "vhditools_signal.h"
35
36 #if defined( WINAPI )
37
38 void (*vhditools_signal_signal_handler)( vhditools_signal_t ) = NULL;
39
40 /* Signal handler for Ctrl+C or Ctrl+Break signals
41 */
42 BOOL WINAPI vhditools_signal_handler(
43 vhditools_signal_t signal )
44 {
45 static char *function = "vhditools_signal_handler";
46
47 switch( signal )
48 {
49 /* use Ctrl+C or Ctrl+Break to simulate SERVICE_CONTROL_STOP in debug mode
50 */
51 case CTRL_BREAK_EVENT:
52 case CTRL_C_EVENT:
53 if( vhditools_signal_signal_handler != NULL )
54 {
55 vhditools_signal_signal_handler(
56 signal );
57 }
58 return( TRUE );
59
60 default:
61 break;
62 }
63 return( FALSE );
64 }
65
66 #if defined( _MSC_VER )
67
68 /* Initialize memory usage and leakage debugging
69 */
70 void vhditools_signal_initialize_memory_debug(
71 void )
72 {
73 int flag = 0;
74
75 /* Get the current state of the flag and store it in a temporary variable
76 */
77 flag = _CrtSetDbgFlag(
78 _CRTDBG_REPORT_FLAG );
79
80 /* Turn on client block identifiers and automatic leak detection
81 */
82 flag |= ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
83
84 /* Set the new state for the flag
85 */
86 _CrtSetDbgFlag(
87 flag );
88 }
89
90 #endif /* defined( _MSC_VER ) */
91
92 /* Attaches a signal handler for Ctrl+C or Ctrl+Break signals
93 * Returns 1 if successful or -1 on error
94 */
95 int vhditools_signal_attach(
96 void (*signal_handler)( vhditools_signal_t ),
97 libcerror_error_t **error )
98 {
99 static char *function = "vhditools_signal_attach";
100
101 if( signal_handler == NULL )
102 {
103 libcerror_error_set(
104 error,
105 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
106 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
107 "%s: invalid signal handler.",
108 function );
109
110 return( -1 );
111 }
112 vhditools_signal_signal_handler = signal_handler;
113
114 if( SetConsoleCtrlHandler(
115 (PHANDLER_ROUTINE) vhditools_signal_handler,
116 TRUE ) == 0 )
117 {
118 libcerror_error_set(
119 error,
120 LIBCERROR_ERROR_DOMAIN_RUNTIME,
121 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
122 "%s: unable to attach signal handler.",
123 function );
124
125 return( -1 );
126 }
127 if( SetConsoleCtrlHandler(
128 NULL,
129 FALSE ) == 0 )
130 {
131 libcerror_error_set(
132 error,
133 LIBCERROR_ERROR_DOMAIN_RUNTIME,
134 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
135 "%s: unable to attach break signal.",
136 function );
137
138 return( -1 );
139 }
140 #if defined( _MSC_VER )
141 vhditools_signal_initialize_memory_debug();
142 #endif
143
144 SetErrorMode(
145 SEM_FAILCRITICALERRORS );
146
147 #if defined( LOCALE_SUPPORT )
148 /* Allow subsequent threads to have their own locale.
149 * If the application is single threaded this call has
150 * no practical effect.
151 */
152 _configthreadlocale(
153 _ENABLE_PER_THREAD_LOCALE );
154
155 /* Set the current thread locale to the user default
156 * ANSI code page.
157 */
158 setlocale(
159 LC_ALL,
160 "" );
161
162 /* Set the the code page used by multibyte functions
163 * to use the same code page as the previous call to setlocale.
164 */
165 _setmbcp(
166 _MB_CP_LOCALE );
167
168 #endif /* defined( LOCALE_SUPPORT ) */
169
170 return( 1 );
171 }
172
173 /* Detaches a signal handler for Ctrl+C or Ctrl+Break signals
174 * Returns 1 if successful or -1 on error
175 */
176 int vhditools_signal_detach(
177 libcerror_error_t **error )
178 {
179 static char *function = "vhditools_signal_detach";
180
181 if( SetConsoleCtrlHandler(
182 (PHANDLER_ROUTINE) vhditools_signal_handler,
183 FALSE ) == 0 )
184 {
185 libcerror_error_set(
186 error,
187 LIBCERROR_ERROR_DOMAIN_RUNTIME,
188 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
189 "%s: unable to detach signal handler.",
190 function );
191
192 return( -1 );
193 }
194 vhditools_signal_signal_handler = NULL;
195
196 return( 1 );
197 }
198
199 #else
200
201 /* Attaches a signal handler for SIGINT
202 * Returns 1 if successful or -1 on error
203 */
204 int vhditools_signal_attach(
205 void (*signal_handler)( vhditools_signal_t ),
206 libcerror_error_t **error )
207 {
208 static char *function = "vhditools_signal_attach";
209
210 if( signal_handler == NULL )
211 {
212 libcerror_error_set(
213 error,
214 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
215 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
216 "%s: invalid signal handler.",
217 function );
218
219 return( -1 );
220 }
221 if( signal(
222 SIGINT,
223 signal_handler ) == SIG_ERR )
224 {
225 libcerror_error_set(
226 error,
227 LIBCERROR_ERROR_DOMAIN_RUNTIME,
228 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
229 "%s: unable to attach signal handler.",
230 function );
231
232 return( -1 );
233 }
234 return( 1 );
235 }
236
237 /* Detaches a signal handler for SIGINT
238 * Returns 1 if successful or -1 on error
239 */
240 int vhditools_signal_detach(
241 libcerror_error_t **error )
242 {
243 static char *function = "vhditools_signal_detach";
244
245 if( signal(
246 SIGINT,
247 SIG_DFL ) == SIG_ERR )
248 {
249 libcerror_error_set(
250 error,
251 LIBCERROR_ERROR_DOMAIN_RUNTIME,
252 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
253 "%s: unable to detach signal handler.",
254 function );
255
256 return( -1 );
257 }
258 return( 1 );
259 }
260
261 #endif /* defined( WINAPI ) */
262