Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to only install essential signal handlers #49981

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ JL_DLLEXPORT void julia_init(JL_IMAGE_SEARCH rel)
jl_init_tasks();
jl_init_threading();
jl_init_threadinginfra();
if (jl_options.handle_signals == JL_OPTIONS_HANDLE_SIGNALS_ON)
if (jl_options.handle_signals != JL_OPTIONS_HANDLE_SIGNALS_OFF)
jl_install_default_signal_handlers();

jl_gc_init();
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -2485,6 +2485,7 @@ JL_DLLEXPORT int jl_generating_output(void) JL_NOTSAFEPOINT;

#define JL_OPTIONS_HANDLE_SIGNALS_ON 1
#define JL_OPTIONS_HANDLE_SIGNALS_OFF 0
#define JL_OPTIONS_HANDLE_SIGNALS_ESSENTIAL 2

#define JL_OPTIONS_USE_SYSIMAGE_NATIVE_CODE_YES 1
#define JL_OPTIONS_USE_SYSIMAGE_NATIVE_CODE_NO 0
Expand Down
125 changes: 65 additions & 60 deletions src/signals-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,80 +1034,85 @@ static void sigtrap_handler(int sig, siginfo_t *info, void *context)

void jl_install_default_signal_handlers(void)
{
struct sigaction actf;
memset(&actf, 0, sizeof(struct sigaction));
sigemptyset(&actf.sa_mask);
actf.sa_sigaction = fpe_handler;
actf.sa_flags = SA_SIGINFO;
if (sigaction(SIGFPE, &actf, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
if (jl_options.handle_signals == JL_OPTIONS_HANDLE_SIGNALS_ON) {
struct sigaction actf;
memset(&actf, 0, sizeof(struct sigaction));
sigemptyset(&actf.sa_mask);
actf.sa_sigaction = fpe_handler;
actf.sa_flags = SA_SIGINFO;
if (sigaction(SIGFPE, &actf, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
#if defined(_OS_DARWIN_) && defined(_CPU_AARCH64_)
struct sigaction acttrap;
memset(&acttrap, 0, sizeof(struct sigaction));
sigemptyset(&acttrap.sa_mask);
acttrap.sa_sigaction = sigtrap_handler;
acttrap.sa_flags = SA_SIGINFO;
if (sigaction(SIGTRAP, &acttrap, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
struct sigaction acttrap;
memset(&acttrap, 0, sizeof(struct sigaction));
sigemptyset(&acttrap.sa_mask);
acttrap.sa_sigaction = sigtrap_handler;
acttrap.sa_flags = SA_SIGINFO;
if (sigaction(SIGTRAP, &acttrap, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
#else
if (signal(SIGTRAP, SIG_IGN) == SIG_ERR) {
jl_error("fatal error: Couldn't set SIGTRAP");
}
if (signal(SIGTRAP, SIG_IGN) == SIG_ERR) {
jl_error("fatal error: Couldn't set SIGTRAP");
}
#endif
struct sigaction actint;
memset(&actint, 0, sizeof(struct sigaction));
sigemptyset(&actint.sa_mask);
actint.sa_handler = sigint_handler;
actint.sa_flags = 0;
if (sigaction(SIGINT, &actint, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
jl_error("fatal error: Couldn't set SIGPIPE");
}
#if !defined(HAVE_MACH)
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
sigemptyset(&act.sa_mask);
act.sa_sigaction = usr2_handler;
act.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTART;
if (sigaction(SIGUSR2, &act, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
#endif
struct sigaction actint;
memset(&actint, 0, sizeof(struct sigaction));
sigemptyset(&actint.sa_mask);
actint.sa_handler = sigint_handler;
actint.sa_flags = 0;
if (sigaction(SIGINT, &actint, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
jl_error("fatal error: Couldn't set SIGPIPE");
}

#if defined(HAVE_MACH)
allocate_mach_handler();
#else
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
sigemptyset(&act.sa_mask);
act.sa_sigaction = usr2_handler;
act.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTART;
if (sigaction(SIGUSR2, &act, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
#endif

allocate_segv_handler();

struct sigaction act_die;
memset(&act_die, 0, sizeof(struct sigaction));
sigemptyset(&act_die.sa_mask);
act_die.sa_sigaction = sigdie_handler;
act_die.sa_flags = SA_SIGINFO | SA_RESETHAND;
if (sigaction(SIGILL, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
if (sigaction(SIGABRT, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
if (sigaction(SIGSYS, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
// need to ensure the following signals are not SIG_IGN, even though they will be blocked
act_die.sa_flags = SA_SIGINFO | SA_RESTART | SA_RESETHAND;
if (jl_options.handle_signals == JL_OPTIONS_HANDLE_SIGNALS_ON) {
struct sigaction act_die;
memset(&act_die, 0, sizeof(struct sigaction));
sigemptyset(&act_die.sa_mask);
act_die.sa_sigaction = sigdie_handler;
act_die.sa_flags = SA_SIGINFO | SA_RESETHAND;
if (sigaction(SIGILL, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
if (sigaction(SIGABRT, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
if (sigaction(SIGSYS, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
// need to ensure the following signals are not SIG_IGN, even though they will be blocked
act_die.sa_flags = SA_SIGINFO | SA_RESTART | SA_RESETHAND;
#ifdef SIGINFO
if (sigaction(SIGINFO, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
if (sigaction(SIGINFO, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
#else
if (sigaction(SIGUSR1, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
if (sigaction(SIGUSR1, &act_die, NULL) < 0) {
jl_errorf("fatal error: sigaction: %s", strerror(errno));
}
#endif
}
}

JL_DLLEXPORT void jl_install_sigint_handler(void)
Expand Down
2 changes: 1 addition & 1 deletion src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ jl_task_t *jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi)
}
#endif

if (jl_options.handle_signals == JL_OPTIONS_HANDLE_SIGNALS_ON)
if (jl_options.handle_signals != JL_OPTIONS_HANDLE_SIGNALS_OFF)
jl_install_thread_signal_handler(ptls);

return ct;
Expand Down