| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 69 |
|
| 70 |
void vhditools_signal_initialize_memory_debug( |
| 71 |
void ) |
| 72 |
{ |
| 73 |
int flag = 0; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
flag = _CrtSetDbgFlag( |
| 78 |
_CRTDBG_REPORT_FLAG ); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
flag |= ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
_CrtSetDbgFlag( |
| 87 |
flag ); |
| 88 |
} |
| 89 |
|
| 90 |
#endif |
| 91 |
|
| 92 |
|
| 93 |
|
| 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 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
_configthreadlocale( |
| 153 |
_ENABLE_PER_THREAD_LOCALE ); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
setlocale( |
| 159 |
LC_ALL, |
| 160 |
"" ); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
_setmbcp( |
| 166 |
_MB_CP_LOCALE ); |
| 167 |
|
| 168 |
#endif |
| 169 |
|
| 170 |
return( 1 ); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 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 |
|
| 202 |
|
| 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 |
|
| 238 |
|
| 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 |
| 262 |
|