libgcj/7532: shutdown hooks not run upon abnormal termination
jmr@fulcrummicro.com
jmr@fulcrummicro.com
Wed Aug 7 15:36:00 GMT 2002
>Number: 7532
>Category: libgcj
>Synopsis: shutdown hooks not run upon abnormal termination
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: unassigned
>State: open
>Class: change-request
>Submitter-Id: net
>Arrival-Date: Wed Aug 07 15:36:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: Jesse Rosenstock
>Release: 3.3 20020806 (experimental)
>Organization:
>Environment:
System: Linux churchill 2.4.3-12 #1 Fri Jun 8 15:05:56 EDT 2001 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc/configure --prefix=/scratch/app/gcc --enable-threads=posix --enable-shared --enable-languages=c++,java
>Description:
Shutdown hooks added with Runtime.addShutdownHook() are not run
if the program terminates abnormally, ie due to Ctrl-C.
>How-To-Repeat:
W.java:
public class W {
public static void main(String[] args) throws Exception {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
System.err.println("shutting down");
}
}));
final Object o = new Object();
synchronized (o) {
o.wait();
}
}
}
; gcj -C W.java
; gij W
<Ctrl-C>
<nothing is printed>
; java W
<Ctrl-C>
shutting down
>Fix:
Here's a patch that installs a signal handler to catch SIGHUP, SIGINT, and
SIGTERM. The handler calls System.exit() so the shutdown hooks run.
Installation of the signal handlers can be disabled with
--reduce-signal-usage.
Index: gij.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/gij.cc,v
retrieving revision 1.18
diff -c -r1.18 gij.cc
*** gij.cc 27 Feb 2002 05:32:13 -0000 1.18
--- gij.cc 7 Aug 2002 22:13:46 -0000
***************
*** 32,37 ****
--- 32,39 ----
printf (" --help print this help, then exit\n");
printf (" --ms=NUMBER set initial heap size\n");
printf (" --mx=NUMBER set maximum heap size\n");
+ printf (" --reduce-signal-usage\n");
+ printf (" reduce use of OS signals\n");
printf (" --version print version number, then exit\n");
printf ("\nSee http://gcc.gnu.org/java/ for information on reporting bugs\n");
exit (0);
***************
*** 115,120 ****
--- 117,124 ----
goto no_arg;
_Jv_SetMaximumHeapSize (argv[++i]);
}
+ else if (! strcmp (arg, "-reduce-signal-usage"))
+ _Jv_SetReduceSignalUsage (true);
else
{
fprintf (stderr, "gij: unrecognized option -- `%s'\n", argv[i]);
Index: posix.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/posix.cc,v
retrieving revision 1.7
diff -c -r1.7 posix.cc
*** posix.cc 7 Apr 2002 11:26:58 -0000 1.7
--- posix.cc 7 Aug 2002 22:13:46 -0000
***************
*** 17,22 ****
--- 17,23 ----
#include <signal.h>
#include <jvm.h>
+ #include <java/lang/System.h>
#include <java/lang/Thread.h>
#include <java/io/InterruptedIOException.h>
#include <java/util/Properties.h>
***************
*** 48,53 ****
--- 49,60 ----
#endif
}
+ static void
+ shutdown (int signum)
+ {
+ ::java::lang::System::exit (128 + signum);
+ }
+
// Platform-specific VM initialization.
void
_Jv_platform_initialize (void)
***************
*** 59,66 ****
--- 66,88 ----
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
sigaction (SIGPIPE, &act, NULL);
+
+ if (! ::gcj::reduceSignalUsage)
+ {
+ act.sa_handler = &shutdown;
+ sigaction (SIGHUP, &act, NULL);
+ sigaction (SIGINT, &act, NULL);
+ sigaction (SIGTERM, &act, NULL);
+ }
#else
signal (SIGPIPE, SIG_IGN);
+
+ if (! ::gcj::reduceSignalUsage)
+ {
+ signal (SIGHUP, &shutdown);
+ signal (SIGINT, &shutdown);
+ signal (SIGTERM, &shutdown);
+ }
#endif
}
Index: prims.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/prims.cc,v
retrieving revision 1.72
diff -c -r1.72 prims.cc
*** prims.cc 10 Mar 2002 03:53:12 -0000 1.72
--- prims.cc 7 Aug 2002 22:13:46 -0000
***************
*** 874,879 ****
--- 874,880 ----
_Jv_Utf8Const *finit_name;
bool runtimeInitialized = false;
+ bool reduceSignalUsage = false;
}
jint
***************
*** 1051,1056 ****
--- 1052,1063 ----
{
size_t size = parse_heap_size (arg);
_Jv_GCSetMaximumHeapSize (size);
+ }
+
+ void
+ _Jv_SetReduceSignalUsage (bool reduce_signal_usage)
+ {
+ ::gcj::reduceSignalUsage = reduce_signal_usage;
}
Index: include/jvm.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/include/jvm.h,v
retrieving revision 1.51
diff -c -r1.51 jvm.h
*** include/jvm.h 11 Apr 2002 15:57:56 -0000 1.51
--- include/jvm.h 7 Aug 2002 22:13:47 -0000
***************
*** 148,153 ****
--- 148,158 ----
/* Set to true by _Jv_CreateJavaVM. */
extern bool runtimeInitialized;
+
+ /* Controls whether or not the usage of signals by the runtime should
+ be reduced. If set to true, shutdown hooks registered with
+ Runtime.addShutdownHook() may not run upon abnormal termination. */
+ extern bool reduceSignalUsage;
};
/* Type of pointer used as finalizer. */
***************
*** 225,230 ****
--- 230,241 ----
number which can optionally have "k" or "m" appended and calls
_Jv_GCSetMaximumHeapSize. */
void _Jv_SetMaximumHeapSize (const char *arg);
+
+ /* External interface to controlling whether or not the usage of signals
+ by the runtime should be reduced. If set to true, shutdown hooks
+ registered with Runtime.addShutdownHook() may not run upon abnormal
+ termination. */
+ void _Jv_SetReduceSignalUsage (bool reduce_signal_usage);
extern "C" void JvRunMain (jclass klass, int argc, const char **argv);
void _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the Gcc-bugs
mailing list