Merge remote-tracking branch 'danlrobertson/safe_signal'

This commit is contained in:
Maxime Coste 2016-01-10 03:00:25 +00:00
commit 2f8612f8e7
4 changed files with 32 additions and 19 deletions

View File

@ -133,4 +133,14 @@ void EventManager::force_signal(int fd)
FD_SET(fd, &m_forced_fd); FD_SET(fd, &m_forced_fd);
} }
SignalHandler set_signal_wrapper(int signum, SignalHandler handler)
{
struct sigaction new_action, old_action;
sigemptyset(&new_action.sa_mask);
new_action.sa_handler = handler;
new_action.sa_flags = SA_RESTART;
sigaction(signum, &new_action, &old_action);
return old_action.sa_handler;
}
} }

View File

@ -14,6 +14,8 @@
namespace Kakoune namespace Kakoune
{ {
typedef void(*SignalHandler)(int);
enum class EventMode enum class EventMode
{ {
Normal, Normal,
@ -92,6 +94,8 @@ private:
TimePoint m_last; TimePoint m_last;
}; };
SignalHandler set_signal_wrapper(int signum, SignalHandler handler);
} }
#endif // event_manager_hh_INCLUDED #endif // event_manager_hh_INCLUDED

View File

@ -25,7 +25,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <locale> #include <locale>
#include <signal.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@ -294,16 +293,16 @@ std::unique_ptr<UserInterface> create_local_ui(bool dummy_ui)
{ {
LocalUI() LocalUI()
{ {
m_old_sighup = signal(SIGHUP, [](int) { m_old_sighup = set_signal_wrapper(SIGHUP, [](int) {
ClientManager::instance().remove_client(*local_client, false); ClientManager::instance().remove_client(*local_client, false);
}); });
m_old_sigtstp = signal(SIGTSTP, [](int) { m_old_sigtstp = set_signal_wrapper(SIGTSTP, [](int) {
if (ClientManager::instance().count() == 1 and if (ClientManager::instance().count() == 1 and
*ClientManager::instance().begin() == local_client) *ClientManager::instance().begin() == local_client)
{ {
// Suspend normally if we are the only client // Suspend normally if we are the only client
auto current = signal(SIGTSTP, static_cast<LocalUI&>(local_client->ui()).m_old_sigtstp); auto current = set_signal_wrapper(SIGTSTP, static_cast<LocalUI&>(local_client->ui()).m_old_sigtstp);
sigset_t unblock_sigtstp, old_mask; sigset_t unblock_sigtstp, old_mask;
sigemptyset(&unblock_sigtstp); sigemptyset(&unblock_sigtstp);
@ -314,7 +313,7 @@ std::unique_ptr<UserInterface> create_local_ui(bool dummy_ui)
sigprocmask(SIG_SETMASK, &old_mask, nullptr); sigprocmask(SIG_SETMASK, &old_mask, nullptr);
signal(SIGTSTP, current); set_signal_wrapper(SIGTSTP, current);
} }
else else
convert_to_client_pending = true; convert_to_client_pending = true;
@ -323,8 +322,8 @@ std::unique_ptr<UserInterface> create_local_ui(bool dummy_ui)
~LocalUI() ~LocalUI()
{ {
signal(SIGHUP, m_old_sighup); set_signal_wrapper(SIGHUP, m_old_sighup);
signal(SIGTSTP, m_old_sigtstp); set_signal_wrapper(SIGTSTP, m_old_sigtstp);
local_client = nullptr; local_client = nullptr;
if (not convert_to_client_pending and if (not convert_to_client_pending and
not ClientManager::instance().empty()) not ClientManager::instance().empty())
@ -432,7 +431,7 @@ int run_server(StringView session, StringView init_command,
session, child)); session, child));
exit(0); exit(0);
} }
signal(SIGTERM, [](int) { terminate = true; }); set_signal_wrapper(SIGTERM, [](int) { terminate = true; });
} }
StringRegistry string_registry; StringRegistry string_registry;
@ -662,13 +661,13 @@ int main(int argc, char* argv[])
{ {
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
signal(SIGSEGV, signal_handler); set_signal_wrapper(SIGSEGV, signal_handler);
signal(SIGFPE, signal_handler); set_signal_wrapper(SIGFPE, signal_handler);
signal(SIGQUIT, signal_handler); set_signal_wrapper(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler); set_signal_wrapper(SIGTERM, signal_handler);
signal(SIGPIPE, SIG_IGN); set_signal_wrapper(SIGPIPE, SIG_IGN);
signal(SIGINT, [](int){}); set_signal_wrapper(SIGINT, [](int){});
signal(SIGCHLD, [](int){}); set_signal_wrapper(SIGCHLD, [](int){});
Vector<String> params; Vector<String> params;
for (size_t i = 1; i < argc; ++i) for (size_t i = 1; i < argc; ++i)

View File

@ -250,8 +250,8 @@ NCursesUI::NCursesUI()
enable_mouse(true); enable_mouse(true);
signal(SIGWINCH, on_term_resize); set_signal_wrapper(SIGWINCH, on_term_resize);
signal(SIGCONT, on_term_resize); set_signal_wrapper(SIGCONT, on_term_resize);
check_resize(true); check_resize(true);
@ -262,8 +262,8 @@ NCursesUI::~NCursesUI()
{ {
enable_mouse(false); enable_mouse(false);
endwin(); endwin();
signal(SIGWINCH, SIG_DFL); set_signal_wrapper(SIGWINCH, SIG_DFL);
signal(SIGCONT, SIG_DFL); set_signal_wrapper(SIGCONT, SIG_DFL);
} }
void NCursesUI::Window::create(const CharCoord& p, const CharCoord& s) void NCursesUI::Window::create(const CharCoord& p, const CharCoord& s)