From 7bfda49772dc0783a2118b8ac9a9cbc5b554fe5c Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Thu, 2 Dec 2004 18:22:48 +0000 Subject: [PATCH] Clean up temp files also when killed by a signal. --- tools/winebuild/main.c | 10 ++++++++++ tools/winegcc/winegcc.c | 25 +++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/tools/winebuild/main.c b/tools/winebuild/main.c index 46a4b610267..3a1c443ab5b 100644 --- a/tools/winebuild/main.c +++ b/tools/winebuild/main.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -128,6 +129,11 @@ static void cleanup(void) if (output_file_name) unlink( output_file_name ); } +/* clean things up when aborting on a signal */ +static void exit_on_signal( int sig ) +{ + exit(1); /* this will call atexit functions */ +} /******************************************************************* * command-line option handling @@ -415,6 +421,10 @@ int main(int argc, char **argv) { DLLSPEC *spec = alloc_dll_spec(); + signal( SIGHUP, exit_on_signal ); + signal( SIGTERM, exit_on_signal ); + signal( SIGINT, exit_on_signal ); + output_file = stdout; argv = parse_options( argc, argv, spec ); diff --git a/tools/winegcc/winegcc.c b/tools/winegcc/winegcc.c index 40f749f33e6..ddab65c4345 100644 --- a/tools/winegcc/winegcc.c +++ b/tools/winegcc/winegcc.c @@ -90,6 +90,7 @@ #include #include +#include #include #include #include @@ -137,6 +138,7 @@ static const char* app_loader_template = static int keep_generated = 0; static strarray* tmp_files; +static sigset_t signal_mask; struct options { @@ -169,10 +171,21 @@ static void clean_temp_files() unlink(tmp_files->base[i]); } +/* clean things up when aborting on a signal */ +static void exit_on_signal( int sig ) +{ + exit(1); /* this will call the atexit functions */ +} + char* get_temp_file(const char* prefix, const char* suffix) { + int fd; + sigset_t old_set; char* tmp = strmake("%s-XXXXXX%s", prefix, suffix); - int fd = mkstemps( tmp, strlen(suffix) ); + + /* block signals while manipulating the temp files list */ + sigprocmask( SIG_BLOCK, &signal_mask, &old_set ); + fd = mkstemps( tmp, strlen(suffix) ); if (fd == -1) { /* could not create it in current directory, try in /tmp */ @@ -183,7 +196,7 @@ char* get_temp_file(const char* prefix, const char* suffix) } close( fd ); strarray_add(tmp_files, tmp); - + sigprocmask( SIG_SETMASK, &old_set, NULL ); return tmp; } @@ -687,6 +700,14 @@ int main(int argc, char **argv) char* lang = 0; char* str; + signal( SIGHUP, exit_on_signal ); + signal( SIGTERM, exit_on_signal ); + signal( SIGINT, exit_on_signal ); + sigemptyset( &signal_mask ); + sigaddset( &signal_mask, SIGHUP ); + sigaddset( &signal_mask, SIGTERM ); + sigaddset( &signal_mask, SIGINT ); + /* setup tmp file removal at exit */ tmp_files = strarray_alloc(); atexit(clean_temp_files);