Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,15 @@ protected long decrementCount() {
}

@AutomaticallyRegisteredFeature
class IgnoreSIGPIPEFeature implements InternalFeature {
class IgnoreSignalsFeature implements InternalFeature {

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
RuntimeSupport.getRuntimeSupport().addStartupHook(new IgnoreSIGPIPEStartupHook());
RuntimeSupport.getRuntimeSupport().addStartupHook(new IgnoreSignalsStartupHook());
}
}

final class IgnoreSIGPIPEStartupHook implements RuntimeSupport.Hook {
final class IgnoreSignalsStartupHook implements RuntimeSupport.Hook {

@CEntryPoint(publishAs = Publish.NotPublished)
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class)
Expand All @@ -418,20 +418,24 @@ static void noopSignalHandler(@SuppressWarnings("unused") int sig) {
}

private static final CEntryPointLiteral<Signal.SignalDispatcher> NOOP_SIGNAL_HANDLER = //
CEntryPointLiteral.create(IgnoreSIGPIPEStartupHook.class, "noopSignalHandler", int.class);
CEntryPointLiteral.create(IgnoreSignalsStartupHook.class, "noopSignalHandler", int.class);

/**
* HotSpot ignores the SIGPIPE and SIGXFSZ signals (see <a
* href=https://github.com/openjdk/jdk/blob/fc76687c2fac39fcbf706c419bfa170b8efa5747/src/hotspot/os/posix/signals_posix.cpp#L608>signals_posix.cpp</a>).
* When signal handling is enabled we do the same thing.
* <p>
* Ignore SIGPIPE. Reading from a closed pipe, instead of delivering a process-wide signal whose
* default action is to terminate the process, will instead return an error code from the
* specific write operation.
*
* <p>
* From pipe(7): If all file descriptors referring to the read end of a pipe have been closed,
* then a write(2) will cause a SIGPIPE signal to be generated for the calling process. If the
* calling process is ignoring this signal, then write(2) fails with the error EPIPE.
*
* <p>
* Note that the handler must be an empty function and not SIG_IGN. The problem is SIG_IGN is
* inherited to subprocess but we only want to affect the current process.
*
* <p>
* From signal(7): A child created via fork(2) inherits a copy of its parent's signal
* dispositions. During an execve(2), the dispositions of handled signals are reset to the
* default; the dispositions of ignored signals are left unchanged.
Expand All @@ -440,15 +444,21 @@ static void noopSignalHandler(@SuppressWarnings("unused") int sig) {
public void execute(boolean isFirstIsolate) {
if (isFirstIsolate && SubstrateOptions.EnableSignalHandling.getValue()) {
synchronized (Target_jdk_internal_misc_Signal.class) {
int signum = Signal.SignalEnum.SIGPIPE.getCValue();
if (Util_jdk_internal_misc_Signal.isCurrentDispatcher(signum, Signal.SIG_DFL())) {
/*
* Replace with NOOP signal handler if a custom one has not already been
* installed.
*/
final SignalDispatcher signalResult = PosixUtils.installSignalHandler(signum, NOOP_SIGNAL_HANDLER.getFunctionPointer());
VMError.guarantee(signalResult != Signal.SIG_ERR(), "IgnoreSIGPIPEFeature.run: Could not ignore SIGPIPE");
}
installNoopHandler(Signal.SignalEnum.SIGPIPE);
installNoopHandler(Signal.SignalEnum.SIGXFSZ);
}
}
}

private static void installNoopHandler(Signal.SignalEnum signal) {
int signum = signal.getCValue();
if (Util_jdk_internal_misc_Signal.isCurrentDispatcher(signum, Signal.SIG_DFL())) {
/*
* Replace with no-op signal handler if a custom one has not already been installed.
*/
final SignalDispatcher signalResult = PosixUtils.installSignalHandler(signum, NOOP_SIGNAL_HANDLER.getFunctionPointer());
if (signalResult == Signal.SIG_ERR()) {
throw VMError.shouldNotReachHere(String.format("IgnoreSignalsStartupHook: Could not install signal: %s", signal));
}
}
}
Expand Down