This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH][LTO] Cleanup in lto-wrapper after signals


Copied from gcc.c, verified that interrupting removes temporary
files.

Bootstrapped and tested on x86_64-unknown-linux-gnu, ok?

Thanks,
Richard.

2010-06-02  Richard Guenther  <rguenther@suse.de>

	* lto-wrapper.c (lto_wrapper_exit): Rename to ...
	(lto_wrapper_cleanup): ... this.  Do not exit.
	(fatal): Adjust.  Exit here.
	(fatal_perror): Likewise.
	(fatal_signal): New function.
	(main): Set up signal handlers to cleanup temporary files.
	* Makefile.in (lto-wrapper.o): Adjust dependencies.

Index: gcc/lto-wrapper.c
===================================================================
*** gcc/lto-wrapper.c	(revision 160100)
--- gcc/lto-wrapper.c	(working copy)
*************** along with GCC; see the file COPYING3.
*** 40,51 ****
  #include "config.h"
  #include "system.h"
  #include <errno.h>
! #include "coretypes.h"
! #include "tm.h"
  #include "intl.h"
  #include "libiberty.h"
  #include "obstack.h"
  
  int debug;				/* true if -save-temps.  */
  int verbose;				/* true if -v.  */
  
--- 40,58 ----
  #include "config.h"
  #include "system.h"
  #include <errno.h>
! #include <signal.h>
! #if ! defined( SIGCHLD ) && defined( SIGCLD )
! #  define SIGCHLD SIGCLD
! #endif
! #include "defaults.h"
  #include "intl.h"
  #include "libiberty.h"
  #include "obstack.h"
  
+ #ifndef HAVE_KILL
+ #define kill(p,s) raise(s)
+ #endif
+ 
  int debug;				/* true if -save-temps.  */
  int verbose;				/* true if -v.  */
  
*************** static char *makefile;
*** 68,103 ****
  
  static void maybe_unlink_file (const char *);
  
! /* Delete tempfiles and exit function.  */
  
  static void
! lto_wrapper_exit (int status)
  {
    static bool cleanup_done = false;
!   if (!cleanup_done)
!     {
!       unsigned int i;
  
!       /* Setting cleanup_done prevents an infinite loop if one of the
!          calls to maybe_unlink_file fails. */
!       cleanup_done = true;
! 
!       if (ltrans_output_file)
!         maybe_unlink_file (ltrans_output_file);
!       if (flto_out)
!         maybe_unlink_file (flto_out);
!       if (args_name)
!         maybe_unlink_file (args_name);
!       if (makefile)
! 	maybe_unlink_file (makefile);
!       for (i = 0; i < nr; ++i)
! 	{
! 	  maybe_unlink_file (input_names[i]);
! 	  if (output_names[i])
! 	    maybe_unlink_file (output_names[i]);
! 	}
      }
!   exit (status);
  }
  
  /* Just die. CMSGID is the error message. */
--- 75,119 ----
  
  static void maybe_unlink_file (const char *);
  
! /* Delete tempfiles.  */
  
  static void
! lto_wrapper_cleanup (void)
  {
    static bool cleanup_done = false;
!   unsigned int i;
  
!   if (cleanup_done)
!     return;
! 
!   /* Setting cleanup_done prevents an infinite loop if one of the
!      calls to maybe_unlink_file fails. */
!   cleanup_done = true;
! 
!   if (ltrans_output_file)
!     maybe_unlink_file (ltrans_output_file);
!   if (flto_out)
!     maybe_unlink_file (flto_out);
!   if (args_name)
!     maybe_unlink_file (args_name);
!   if (makefile)
!     maybe_unlink_file (makefile);
!   for (i = 0; i < nr; ++i)
!     {
!       maybe_unlink_file (input_names[i]);
!       if (output_names[i])
! 	maybe_unlink_file (output_names[i]);
      }
! }
! 
! static void
! fatal_signal (int signum)
! {
!   signal (signum, SIG_DFL);
!   lto_wrapper_cleanup ();
!   /* Get the same signal again, this time not handled,
!      so its normal effect occurs.  */
!   kill (getpid (), signum);
  }
  
  /* Just die. CMSGID is the error message. */
*************** fatal (const char * cmsgid, ...)
*** 113,119 ****
    fprintf (stderr, "\n");
    va_end (ap);
  
!   lto_wrapper_exit (FATAL_EXIT_CODE);
  }
  
  
--- 129,136 ----
    fprintf (stderr, "\n");
    va_end (ap);
  
!   lto_wrapper_cleanup ();
!   exit (FATAL_EXIT_CODE);
  }
  
  
*************** fatal_perror (const char *cmsgid, ...)
*** 131,137 ****
    fprintf (stderr, ": %s\n", xstrerror (e));
    va_end (ap);
  
!   lto_wrapper_exit (FATAL_EXIT_CODE);
  }
  
  
--- 148,155 ----
    fprintf (stderr, ": %s\n", xstrerror (e));
    va_end (ap);
  
!   lto_wrapper_cleanup ();
!   exit (FATAL_EXIT_CODE);
  }
  
  
*************** main (int argc, char *argv[])
*** 597,602 ****
--- 615,638 ----
  {
    gcc_init_libintl ();
  
+   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
+     signal (SIGINT, fatal_signal);
+ #ifdef SIGHUP
+   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
+     signal (SIGHUP, fatal_signal);
+ #endif
+   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
+     signal (SIGTERM, fatal_signal);
+ #ifdef SIGPIPE
+   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
+     signal (SIGPIPE, fatal_signal);
+ #endif
+ #ifdef SIGCHLD
+   /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
+      receive the signal.  A different setting is inheritable */
+   signal (SIGCHLD, SIG_DFL);
+ #endif
+ 
    /* We may be called with all the arguments stored in some file and
       passed with @file.  Expand them into argv before processing.  */
    expandargv (&argc, &argv);
Index: gcc/Makefile.in
===================================================================
*** gcc/Makefile.in	(revision 160100)
--- gcc/Makefile.in	(working copy)
*************** lto-wrapper$(exeext): lto-wrapper.o intl
*** 2056,2062 ****
  	$(COMPILER) $(ALL_COMPILERFLAGS) $(LDFLAGS) -o T$@ lto-wrapper.o intl.o $(LIBS)
  	mv -f T$@ $@
  
! lto-wrapper.o: lto-wrapper.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) intl.h
  
  # A file used by all variants of C.
  
--- 2056,2063 ----
  	$(COMPILER) $(ALL_COMPILERFLAGS) $(LDFLAGS) -o T$@ lto-wrapper.o intl.o $(LIBS)
  	mv -f T$@ $@
  
! lto-wrapper.o: lto-wrapper.c $(CONFIG_H) $(SYSTEM_H) defaults.h intl.h \
! 	$(OBSTACK_H)
  
  # A file used by all variants of C.
  


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]