2016-05-04 04:02:26 +08:00
|
|
|
// The library for various signal related issues.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2005-10-06 19:54:16 +08:00
|
|
|
#include <errno.h>
|
2016-05-04 04:02:26 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
2006-07-31 04:26:59 +08:00
|
|
|
#ifdef HAVE_SIGINFO_H
|
|
|
|
#include <siginfo.h>
|
|
|
|
#endif
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <pthread.h>
|
2006-07-31 04:26:59 +08:00
|
|
|
|
2005-10-06 15:30:50 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "event.h"
|
2016-05-04 04:02:26 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2018-10-13 14:58:14 +08:00
|
|
|
#include "parser.h"
|
2005-10-06 15:30:50 +08:00
|
|
|
#include "proc.h"
|
2016-05-04 04:02:26 +08:00
|
|
|
#include "reader.h"
|
2019-05-26 10:05:24 +08:00
|
|
|
#include "signal.h"
|
2020-06-08 07:05:52 +08:00
|
|
|
#include "termsize.h"
|
Introduce topic monitoring
topic_monitor allows for querying changes posted to one or more topics,
initially sigchld. This will eventually replace the waitpid logic in
process_mark_finished_children().
Comment from the new header:
Topic monitoring support. Topics are conceptually "a thing that can
happen." For example, delivery of a SIGINT, a child process exits, etc. It
is possible to post to a topic, which means that that thing happened.
Associated with each topic is a current generation, which is a 64 bit
value. When you query a topic, you get back a generation. If on the next
query the generation has increased, then it indicates someone posted to
the topic.
For example, if you are monitoring a child process, you can query the
sigchld topic. If it has increased since your last query, it is possible
that your child process has exited.
Topic postings may be coalesced. That is there may be two posts to a given
topic, yet the generation only increases by 1. The only guarantee is that
after a topic post, the current generation value is larger than any value
previously queried.
Tying this all together is the topic_monitor_t. This provides the current
topic generations, and also provides the ability to perform a blocking
wait for any topic to change in a particular topic set. This is the real
power of topics: you can wait for a sigchld signal OR a thread exit.
2019-02-03 07:39:04 +08:00
|
|
|
#include "topic_monitor.h"
|
2016-05-04 04:02:26 +08:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2006-07-20 06:55:49 +08:00
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
/// Struct describing an entry for the lookup table used to convert between signal names and signal
|
|
|
|
/// ids, etc.
|
|
|
|
struct lookup_entry {
|
|
|
|
/// Signal id.
|
2012-11-19 08:30:30 +08:00
|
|
|
int signal;
|
2016-05-04 04:02:26 +08:00
|
|
|
/// Signal name.
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *name;
|
2016-05-04 04:02:26 +08:00
|
|
|
/// Signal description.
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *desc;
|
2005-10-06 15:30:50 +08:00
|
|
|
};
|
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
/// Lookup table used to convert between signal names and signal ids, etc.
|
2019-02-11 04:07:48 +08:00
|
|
|
static const struct lookup_entry signal_table[] = {
|
2006-05-15 00:39:36 +08:00
|
|
|
#ifdef SIGHUP
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGHUP, L"SIGHUP", N_(L"Terminal hung up")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGINT, L"SIGINT", N_(L"Quit request from job control (^C)")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGQUIT, L"SIGQUIT", N_(L"Quit request from job control with core dump (^\\)")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGILL
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGILL, L"SIGILL", N_(L"Illegal instruction")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTRAP
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGTRAP, L"SIGTRAP", N_(L"Trace or breakpoint trap")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGABRT
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGABRT, L"SIGABRT", N_(L"Abort")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGBUS
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGBUS, L"SIGBUS", N_(L"Misaligned address error")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGFPE
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGFPE, L"SIGFPE", N_(L"Floating point exception")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGKILL
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGKILL, L"SIGKILL", N_(L"Forced quit")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGUSR1, L"SIGUSR1", N_(L"User defined signal 1")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGUSR2, L"SIGUSR2", N_(L"User defined signal 2")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGSEGV, L"SIGSEGV", N_(L"Address boundary error")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGPIPE, L"SIGPIPE", N_(L"Broken pipe")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGALRM, L"SIGALRM", N_(L"Timer expired")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGTERM, L"SIGTERM", N_(L"Polite quit request")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGCHLD, L"SIGCHLD", N_(L"Child process status changed")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGCONT
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGCONT, L"SIGCONT", N_(L"Continue previously stopped process")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSTOP
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGSTOP, L"SIGSTOP", N_(L"Forced stop")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGTSTP, L"SIGTSTP", N_(L"Stop request from job control (^Z)")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGTTIN, L"SIGTTIN", N_(L"Stop from terminal input")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGTTOU, L"SIGTTOU", N_(L"Stop from terminal output")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGURG, L"SIGURG", N_(L"Urgent socket condition")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
2012-06-10 18:36:02 +08:00
|
|
|
#ifdef SIGXCPU
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGXCPU, L"SIGXCPU", N_(L"CPU time limit exceeded")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
2006-01-19 02:40:46 +08:00
|
|
|
#ifdef SIGXFSZ
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGXFSZ, L"SIGXFSZ", N_(L"File size limit exceeded")},
|
2006-01-19 02:40:46 +08:00
|
|
|
#endif
|
2006-05-15 00:39:36 +08:00
|
|
|
#ifdef SIGVTALRM
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGVTALRM, L"SIGVTALRM", N_(L"Virtual timer expired")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
2006-01-19 02:40:46 +08:00
|
|
|
#ifdef SIGPROF
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGPROF, L"SIGPROF", N_(L"Profiling timer expired")},
|
2006-01-19 02:40:46 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGWINCH
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGWINCH, L"SIGWINCH", N_(L"Window size change")},
|
2006-01-19 02:40:46 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGWIND
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGWIND, L"SIGWIND", N_(L"Window size change")},
|
2006-01-19 02:40:46 +08:00
|
|
|
#endif
|
2006-08-01 08:25:50 +08:00
|
|
|
#ifdef SIGIO
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGIO, L"SIGIO", N_(L"I/O on asynchronous file descriptor is possible")},
|
2006-05-15 00:39:36 +08:00
|
|
|
#endif
|
2005-10-06 15:30:50 +08:00
|
|
|
#ifdef SIGPWR
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGPWR, L"SIGPWR", N_(L"Power failure")},
|
2005-10-06 15:30:50 +08:00
|
|
|
#endif
|
2006-01-19 02:40:46 +08:00
|
|
|
#ifdef SIGSYS
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGSYS, L"SIGSYS", N_(L"Bad system call")},
|
2006-01-19 02:40:46 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGINFO
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGINFO, L"SIGINFO", N_(L"Information request")},
|
2006-01-19 02:40:46 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSTKFLT
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGSTKFLT, L"SISTKFLT", N_(L"Stack fault")},
|
2006-01-19 02:40:46 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGEMT
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGEMT, L"SIGEMT", N_(L"Emulator trap")},
|
2006-01-19 02:40:46 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGIOT
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGIOT, L"SIGIOT", N_(L"Abort (Alias for SIGABRT)")},
|
2006-01-19 02:40:46 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUNUSED
|
2016-05-04 04:02:26 +08:00
|
|
|
{SIGUNUSED, L"SIGUNUSED", N_(L"Unused signal")},
|
2012-11-19 08:30:30 +08:00
|
|
|
#endif
|
2019-02-11 04:07:48 +08:00
|
|
|
};
|
2005-10-06 15:30:50 +08:00
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
/// Test if \c name is a string describing the signal named \c canonical.
|
|
|
|
static int match_signal_name(const wchar_t *canonical, const wchar_t *name) {
|
2016-10-02 08:21:40 +08:00
|
|
|
if (wcsncasecmp(name, L"sig", 3) == 0) name += 3;
|
2005-10-06 15:30:50 +08:00
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
return wcscasecmp(canonical + 3, name) == 0;
|
2005-10-06 15:30:50 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
int wcs2sig(const wchar_t *str) {
|
2019-02-11 04:07:48 +08:00
|
|
|
for (const auto &data : signal_table) {
|
|
|
|
if (match_signal_name(data.name, str)) {
|
|
|
|
return data.signal;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 12:24:03 +08:00
|
|
|
int res = fish_wcstoi(str);
|
|
|
|
if (errno || res < 0) return -1;
|
|
|
|
return res;
|
2005-10-06 15:30:50 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
const wchar_t *sig2wcs(int sig) {
|
2019-02-11 04:07:48 +08:00
|
|
|
for (const auto &data : signal_table) {
|
|
|
|
if (data.signal == sig) {
|
|
|
|
return data.name;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2006-05-15 00:39:36 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return _(L"Unknown");
|
2005-10-06 15:30:50 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
const wchar_t *signal_get_desc(int sig) {
|
2019-02-11 04:07:48 +08:00
|
|
|
for (const auto &data : signal_table) {
|
|
|
|
if (data.signal == sig) {
|
|
|
|
return _(data.desc);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2006-05-15 00:39:36 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return _(L"Unknown");
|
2005-10-06 15:30:50 +08:00
|
|
|
}
|
|
|
|
|
2019-02-11 04:54:42 +08:00
|
|
|
/// Store the "main" pid. This allows us to reliably determine if we are in a forked child.
|
|
|
|
static const pid_t s_main_pid = getpid();
|
|
|
|
|
|
|
|
/// It's possible that we receive a signal after we have forked, but before we have reset the signal
|
|
|
|
/// handlers (or even run the pthread_atfork calls). In that event we will do something dumb like
|
|
|
|
/// swallow SIGINT. Ensure that doesn't happen. Check if we are the main fish process; if not reset
|
|
|
|
/// and re-raise the signal. \return whether we re-raised the signal.
|
|
|
|
static bool reraise_if_forked_child(int sig) {
|
|
|
|
// Don't use is_forked_child, that relies on atfork handlers which maybe have not run yet.
|
|
|
|
if (getpid() == s_main_pid) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
signal(sig, SIG_DFL);
|
|
|
|
raise(sig);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-13 02:35:27 +08:00
|
|
|
/// The cancellation signal we have received.
|
|
|
|
/// Of course this is modified from a signal handler.
|
2020-09-05 07:10:22 +08:00
|
|
|
static volatile relaxed_atomic_t<sig_atomic_t> s_cancellation_signal{0};
|
2020-07-13 02:35:27 +08:00
|
|
|
|
|
|
|
void signal_clear_cancel() { s_cancellation_signal = 0; }
|
|
|
|
|
|
|
|
int signal_check_cancel() { return s_cancellation_signal; }
|
|
|
|
|
2019-06-29 01:47:36 +08:00
|
|
|
/// The single signal handler. By centralizing signal handling we ensure that we can never install
|
|
|
|
/// the "wrong" signal handler (see #5969).
|
|
|
|
static void fish_signal_handler(int sig, siginfo_t *info, void *context) {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(info);
|
|
|
|
UNUSED(context);
|
2005-10-06 15:30:50 +08:00
|
|
|
|
2020-09-05 06:32:29 +08:00
|
|
|
// Ensure we preserve errno.
|
|
|
|
const int saved_errno = errno;
|
|
|
|
|
2019-06-29 01:47:36 +08:00
|
|
|
// Check if we are a forked child.
|
|
|
|
if (reraise_if_forked_child(sig)) {
|
2020-09-05 06:32:29 +08:00
|
|
|
errno = saved_errno;
|
2019-06-29 01:47:36 +08:00
|
|
|
return;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2006-05-14 18:16:23 +08:00
|
|
|
|
2019-06-29 01:47:36 +08:00
|
|
|
// Check if fish script cares about this.
|
|
|
|
const bool observed = event_is_signal_observed(sig);
|
|
|
|
if (observed) {
|
|
|
|
event_enqueue_signal(sig);
|
|
|
|
}
|
2005-10-06 19:54:16 +08:00
|
|
|
|
2019-06-29 01:47:36 +08:00
|
|
|
// Do some signal-specific stuff.
|
|
|
|
switch (sig) {
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
case SIGWINCH:
|
2020-06-08 10:35:47 +08:00
|
|
|
/// Respond to a winch signal by telling the termsize container.
|
2020-06-08 07:05:52 +08:00
|
|
|
termsize_container_t::handle_winch();
|
2019-06-29 01:47:36 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case SIGHUP:
|
|
|
|
/// Respond to a hup signal by exiting, unless it is caught by a shellscript function,
|
|
|
|
/// in which case we do nothing.
|
|
|
|
if (!observed) {
|
2020-07-06 11:09:53 +08:00
|
|
|
reader_sighup();
|
2019-06-29 01:47:36 +08:00
|
|
|
}
|
|
|
|
topic_monitor_t::principal().post(topic_t::sighupint);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGTERM:
|
|
|
|
/// Handle sigterm. The only thing we do is restore the front process ID, then die.
|
2020-06-01 05:11:39 +08:00
|
|
|
restore_term_foreground_process_group_for_exit();
|
2019-06-29 01:47:36 +08:00
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
raise(SIGTERM);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGINT:
|
|
|
|
/// Interactive mode ^C handler. Respond to int signal by setting interrupted-flag and
|
|
|
|
/// stopping all loops and conditionals.
|
2020-07-13 02:35:27 +08:00
|
|
|
s_cancellation_signal = SIGINT;
|
2019-06-29 01:47:36 +08:00
|
|
|
reader_handle_sigint();
|
|
|
|
topic_monitor_t::principal().post(topic_t::sighupint);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGCHLD:
|
|
|
|
// A child process stopped or exited.
|
|
|
|
topic_monitor_t::principal().post(topic_t::sigchld);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGALRM:
|
|
|
|
// We have a sigalarm handler that does nothing. This is used in the signal torture
|
|
|
|
// test, to verify that we behave correctly when receiving lots of irrelevant signals.
|
|
|
|
break;
|
|
|
|
}
|
2020-09-05 06:32:29 +08:00
|
|
|
errno = saved_errno;
|
2017-01-22 06:07:54 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
void signal_reset_handlers() {
|
2012-11-19 08:30:30 +08:00
|
|
|
struct sigaction act;
|
2016-05-04 04:02:26 +08:00
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
act.sa_handler = SIG_DFL;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2019-02-11 04:07:48 +08:00
|
|
|
for (const auto &data : signal_table) {
|
|
|
|
if (data.signal == SIGHUP) {
|
2017-05-04 11:29:01 +08:00
|
|
|
struct sigaction oact;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGHUP, nullptr, &oact);
|
2017-05-04 11:29:01 +08:00
|
|
|
if (oact.sa_handler == SIG_IGN) continue;
|
|
|
|
}
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(data.signal, &act, nullptr);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2005-10-06 19:54:16 +08:00
|
|
|
}
|
|
|
|
|
2016-12-29 10:52:33 +08:00
|
|
|
static void set_interactive_handlers() {
|
2017-05-04 11:29:01 +08:00
|
|
|
struct sigaction act, oact;
|
2017-08-27 08:11:39 +08:00
|
|
|
act.sa_flags = 0;
|
|
|
|
oact.sa_flags = 0;
|
2016-05-04 04:02:26 +08:00
|
|
|
sigemptyset(&act.sa_mask);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-12-29 10:52:33 +08:00
|
|
|
// Interactive mode. Ignore interactive signals. We are a shell, we know what is best for
|
|
|
|
// the user.
|
|
|
|
act.sa_handler = SIG_IGN;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGTSTP, &act, nullptr);
|
|
|
|
sigaction(SIGTTOU, &act, nullptr);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-12-29 10:52:33 +08:00
|
|
|
// We don't ignore SIGTTIN because we might send it to ourself.
|
2019-06-29 01:47:36 +08:00
|
|
|
act.sa_sigaction = &fish_signal_handler;
|
2016-12-29 10:52:33 +08:00
|
|
|
act.sa_flags = SA_SIGINFO;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGTTIN, &act, nullptr);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-12-29 10:52:33 +08:00
|
|
|
// SIGTERM restores the terminal controlling process before dying.
|
2019-06-29 01:47:36 +08:00
|
|
|
act.sa_sigaction = &fish_signal_handler;
|
2016-12-29 10:52:33 +08:00
|
|
|
act.sa_flags = SA_SIGINFO;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGTERM, &act, nullptr);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGHUP, nullptr, &oact);
|
2017-05-04 11:29:01 +08:00
|
|
|
if (oact.sa_handler == SIG_DFL) {
|
2019-06-29 01:47:36 +08:00
|
|
|
act.sa_sigaction = &fish_signal_handler;
|
2017-05-04 11:29:01 +08:00
|
|
|
act.sa_flags = SA_SIGINFO;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGHUP, &act, nullptr);
|
2017-05-04 11:29:01 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-01-22 06:07:54 +08:00
|
|
|
// SIGALARM as part of our signal torture test
|
2019-06-29 01:47:36 +08:00
|
|
|
act.sa_sigaction = &fish_signal_handler;
|
2017-01-22 06:07:54 +08:00
|
|
|
act.sa_flags = SA_SIGINFO;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGALRM, &act, nullptr);
|
2017-01-22 06:07:54 +08:00
|
|
|
|
2007-01-21 23:01:14 +08:00
|
|
|
#ifdef SIGWINCH
|
2019-06-29 01:47:36 +08:00
|
|
|
act.sa_sigaction = &fish_signal_handler;
|
2016-12-29 10:52:33 +08:00
|
|
|
act.sa_flags = SA_SIGINFO;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGWINCH, &act, nullptr);
|
2012-11-19 08:30:30 +08:00
|
|
|
#endif
|
2016-12-29 10:52:33 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-12-29 10:52:33 +08:00
|
|
|
/// Sets up appropriate signal handlers.
|
2019-05-28 05:52:48 +08:00
|
|
|
void signal_set_handlers(bool interactive) {
|
2016-12-29 10:52:33 +08:00
|
|
|
struct sigaction act;
|
2017-08-27 08:11:39 +08:00
|
|
|
act.sa_flags = 0;
|
2016-12-29 10:52:33 +08:00
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
|
|
|
|
// Ignore SIGPIPE. We'll detect failed writes and deal with them appropriately. We don't want
|
|
|
|
// this signal interrupting other syscalls or terminating us.
|
2019-05-26 15:32:11 +08:00
|
|
|
act.sa_sigaction = nullptr;
|
2016-12-29 10:52:33 +08:00
|
|
|
act.sa_handler = SIG_IGN;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGPIPE, &act, nullptr);
|
2016-12-29 10:52:33 +08:00
|
|
|
|
2019-05-27 04:18:06 +08:00
|
|
|
// Ignore SIGQUIT.
|
|
|
|
act.sa_handler = SIG_IGN;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGQUIT, &act, nullptr);
|
2019-05-27 04:18:06 +08:00
|
|
|
|
2019-05-26 15:32:11 +08:00
|
|
|
// Apply our SIGINT handler.
|
2019-06-29 01:47:36 +08:00
|
|
|
act.sa_sigaction = &fish_signal_handler;
|
2019-05-26 15:32:11 +08:00
|
|
|
act.sa_flags = SA_SIGINFO;
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(SIGINT, &act, nullptr);
|
2019-05-26 15:32:11 +08:00
|
|
|
|
2016-12-29 10:52:33 +08:00
|
|
|
// Whether or not we're interactive we want SIGCHLD to not interrupt restartable syscalls.
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
2019-06-29 01:47:36 +08:00
|
|
|
act.sa_sigaction = &fish_signal_handler;
|
2016-12-29 10:52:33 +08:00
|
|
|
act.sa_flags = SA_SIGINFO | SA_RESTART;
|
2019-11-19 10:34:50 +08:00
|
|
|
if (sigaction(SIGCHLD, &act, nullptr)) {
|
2016-12-29 10:52:33 +08:00
|
|
|
wperror(L"sigaction");
|
|
|
|
FATAL_EXIT();
|
|
|
|
}
|
|
|
|
|
2019-05-28 05:52:48 +08:00
|
|
|
if (interactive) {
|
2016-12-29 10:52:33 +08:00
|
|
|
set_interactive_handlers();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2005-10-06 15:30:50 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 05:52:48 +08:00
|
|
|
void signal_set_handlers_once(bool interactive) {
|
|
|
|
static std::once_flag s_noninter_once;
|
|
|
|
std::call_once(s_noninter_once, signal_set_handlers, false);
|
|
|
|
|
|
|
|
static std::once_flag s_inter_once;
|
|
|
|
if (interactive) std::call_once(s_inter_once, set_interactive_handlers);
|
|
|
|
}
|
|
|
|
|
2019-06-29 01:33:03 +08:00
|
|
|
void signal_handle(int sig) {
|
2012-11-19 08:30:30 +08:00
|
|
|
struct sigaction act;
|
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
// These should always be handled.
|
|
|
|
if ((sig == SIGINT) || (sig == SIGQUIT) || (sig == SIGTSTP) || (sig == SIGTTIN) ||
|
|
|
|
(sig == SIGTTOU) || (sig == SIGCHLD))
|
2012-11-19 08:30:30 +08:00
|
|
|
return;
|
|
|
|
|
2017-08-27 08:11:39 +08:00
|
|
|
act.sa_flags = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
sigemptyset(&act.sa_mask);
|
2019-06-29 01:33:03 +08:00
|
|
|
act.sa_flags = SA_SIGINFO;
|
2019-06-29 01:47:36 +08:00
|
|
|
act.sa_sigaction = &fish_signal_handler;
|
2019-06-29 01:33:03 +08:00
|
|
|
sigaction(sig, &act, nullptr);
|
2005-10-06 19:54:16 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 04:02:26 +08:00
|
|
|
void get_signals_with_handlers(sigset_t *set) {
|
2012-08-15 15:57:56 +08:00
|
|
|
sigemptyset(set);
|
2019-02-11 04:07:48 +08:00
|
|
|
for (const auto &data : signal_table) {
|
2012-08-15 15:57:56 +08:00
|
|
|
struct sigaction act = {};
|
2019-11-19 10:34:50 +08:00
|
|
|
sigaction(data.signal, nullptr, &act);
|
2017-05-04 11:29:01 +08:00
|
|
|
// If SIGHUP is being ignored (e.g., because were were run via `nohup`) don't reset it.
|
|
|
|
// We don't special case other signals because if they're being ignored that shouldn't
|
|
|
|
// affect processes we spawn. They should get the default behavior for those signals.
|
2019-02-11 04:07:48 +08:00
|
|
|
if (data.signal == SIGHUP && act.sa_handler == SIG_IGN) continue;
|
|
|
|
if (act.sa_handler != SIG_DFL) sigaddset(set, data.signal);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
|
|
|
|
2017-05-03 11:55:41 +08:00
|
|
|
/// Ensure we did not inherit any blocked signals. See issue #3964.
|
|
|
|
void signal_unblock_all() {
|
|
|
|
sigset_t iset;
|
|
|
|
sigemptyset(&iset);
|
2019-11-19 10:34:50 +08:00
|
|
|
sigprocmask(SIG_SETMASK, &iset, nullptr);
|
2017-05-03 11:55:41 +08:00
|
|
|
}
|
2019-05-26 10:05:24 +08:00
|
|
|
|
2020-08-07 10:00:53 +08:00
|
|
|
sigchecker_t::sigchecker_t(topic_t signal) : topic_(signal) {
|
2019-05-26 10:05:24 +08:00
|
|
|
// Call check() to update our generation.
|
|
|
|
check();
|
|
|
|
}
|
|
|
|
|
2020-06-21 00:23:36 +08:00
|
|
|
bool sigchecker_t::check() {
|
2019-05-26 10:05:24 +08:00
|
|
|
auto &tm = topic_monitor_t::principal();
|
2020-06-21 00:23:36 +08:00
|
|
|
generation_t gen = tm.generation_for_topic(topic_);
|
2019-05-26 10:05:24 +08:00
|
|
|
bool changed = this->gen_ != gen;
|
|
|
|
this->gen_ = gen;
|
|
|
|
return changed;
|
|
|
|
}
|
2019-05-26 10:19:03 +08:00
|
|
|
|
2020-06-21 00:23:36 +08:00
|
|
|
void sigchecker_t::wait() const {
|
2019-05-26 10:19:03 +08:00
|
|
|
auto &tm = topic_monitor_t::principal();
|
2020-08-07 10:00:53 +08:00
|
|
|
generation_list_t gens = generation_list_t::invalids();
|
|
|
|
gens.at(topic_) = this->gen_;
|
|
|
|
tm.check(&gens, true /* wait */);
|
2019-05-26 10:19:03 +08:00
|
|
|
}
|