2016-11-14 07:58:04 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2016-11-14 07:58:04 -06:00
|
|
|
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "SigUtil.hpp"
|
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
#include <execinfo.h>
|
|
|
|
#include <csignal>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <cassert>
|
|
|
|
#include <chrono>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <mutex>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <Socket.hpp>
|
2016-11-14 07:58:04 -06:00
|
|
|
#include "Common.hpp"
|
|
|
|
#include "Log.hpp"
|
|
|
|
|
|
|
|
std::atomic<bool> TerminationFlag(false);
|
2017-03-03 15:18:55 -06:00
|
|
|
std::atomic<bool> DumpGlobalState(false);
|
2018-10-15 08:01:15 -05:00
|
|
|
|
|
|
|
#ifndef MOBILEAPP
|
2017-03-04 11:33:46 -06:00
|
|
|
std::atomic<bool> ShutdownRequestFlag(false);
|
|
|
|
|
|
|
|
std::mutex SigHandlerTrap;
|
2016-11-19 09:22:53 -06:00
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
namespace SigUtil
|
|
|
|
{
|
|
|
|
const char *signalName(const int signo)
|
|
|
|
{
|
|
|
|
switch (signo)
|
|
|
|
{
|
|
|
|
#define CASE(x) case SIG##x: return "SIG" #x
|
|
|
|
CASE(HUP);
|
|
|
|
CASE(INT);
|
|
|
|
CASE(QUIT);
|
|
|
|
CASE(ILL);
|
|
|
|
CASE(ABRT);
|
|
|
|
CASE(FPE);
|
|
|
|
CASE(KILL);
|
|
|
|
CASE(SEGV);
|
|
|
|
CASE(PIPE);
|
|
|
|
CASE(ALRM);
|
|
|
|
CASE(TERM);
|
|
|
|
CASE(USR1);
|
|
|
|
CASE(USR2);
|
|
|
|
CASE(CHLD);
|
|
|
|
CASE(CONT);
|
|
|
|
CASE(STOP);
|
|
|
|
CASE(TSTP);
|
|
|
|
CASE(TTIN);
|
|
|
|
CASE(TTOU);
|
|
|
|
CASE(BUS);
|
|
|
|
#ifdef SIGPOLL
|
|
|
|
CASE(POLL);
|
|
|
|
#endif
|
|
|
|
CASE(PROF);
|
|
|
|
CASE(SYS);
|
|
|
|
CASE(TRAP);
|
|
|
|
CASE(URG);
|
|
|
|
CASE(VTALRM);
|
|
|
|
CASE(XCPU);
|
|
|
|
CASE(XFSZ);
|
|
|
|
#ifdef SIGEMT
|
|
|
|
CASE(EMT);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTKFLT
|
|
|
|
CASE(STKFLT);
|
|
|
|
#endif
|
|
|
|
#if defined(SIGIO) && SIGIO != SIGPOLL
|
|
|
|
CASE(IO);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
|
|
|
CASE(PWR);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGLOST
|
|
|
|
CASE(LOST);
|
|
|
|
#endif
|
|
|
|
CASE(WINCH);
|
|
|
|
#if defined(SIGINFO) && SIGINFO != SIGPWR
|
|
|
|
CASE(INFO);
|
|
|
|
#endif
|
|
|
|
#undef CASE
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void handleTerminationSignal(const int signal)
|
|
|
|
{
|
2017-03-04 11:33:46 -06:00
|
|
|
bool hardExit = false;
|
|
|
|
const char *domain;
|
|
|
|
if (!ShutdownRequestFlag && signal == SIGINT)
|
2016-11-14 07:58:04 -06:00
|
|
|
{
|
2017-03-04 11:33:46 -06:00
|
|
|
domain = " Shutdown signal received: ";
|
|
|
|
ShutdownRequestFlag = true;
|
2016-11-14 07:58:04 -06:00
|
|
|
}
|
2017-02-28 03:31:34 -06:00
|
|
|
else if (!TerminationFlag)
|
2016-11-14 07:58:04 -06:00
|
|
|
{
|
2017-03-04 11:33:46 -06:00
|
|
|
domain = " Forced-Termination signal received: ";
|
2016-11-14 07:58:04 -06:00
|
|
|
TerminationFlag = true;
|
|
|
|
}
|
2017-02-28 03:31:34 -06:00
|
|
|
else
|
|
|
|
{
|
2017-03-04 11:33:46 -06:00
|
|
|
domain = " ok, ok - hard-termination signal received: ";
|
|
|
|
hardExit = true;
|
|
|
|
}
|
|
|
|
Log::signalLogPrefix();
|
|
|
|
Log::signalLog(domain);
|
|
|
|
Log::signalLog(signalName(signal));
|
|
|
|
Log::signalLog("\n");
|
|
|
|
|
|
|
|
if (!hardExit)
|
|
|
|
SocketPoll::wakeupWorld();
|
|
|
|
else
|
|
|
|
{
|
2017-02-28 03:31:34 -06:00
|
|
|
::signal (signal, SIG_DFL);
|
|
|
|
::raise (signal);
|
|
|
|
}
|
2016-11-14 07:58:04 -06:00
|
|
|
}
|
|
|
|
|
2017-03-04 11:33:46 -06:00
|
|
|
void requestShutdown()
|
|
|
|
{
|
|
|
|
ShutdownRequestFlag = true;
|
|
|
|
SocketPoll::wakeupWorld();
|
|
|
|
}
|
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
void setTerminationSignals()
|
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = handleTerminationSignal;
|
|
|
|
|
|
|
|
sigaction(SIGINT, &action, nullptr);
|
|
|
|
sigaction(SIGTERM, &action, nullptr);
|
|
|
|
sigaction(SIGQUIT, &action, nullptr);
|
|
|
|
sigaction(SIGHUP, &action, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char FatalGdbString[256] = { '\0' };
|
|
|
|
|
|
|
|
static
|
|
|
|
void handleFatalSignal(const int signal)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(SigHandlerTrap);
|
|
|
|
|
|
|
|
Log::signalLogPrefix();
|
|
|
|
Log::signalLog(" Fatal signal received: ");
|
|
|
|
Log::signalLog(signalName(signal));
|
|
|
|
Log::signalLog("\n");
|
|
|
|
|
|
|
|
if (std::getenv("LOOL_DEBUG"))
|
|
|
|
{
|
|
|
|
Log::signalLog(FatalGdbString);
|
|
|
|
LOG_ERR("Sleeping 30s to allow debugging.");
|
|
|
|
sleep(30);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sigaction action;
|
|
|
|
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = SIG_DFL;
|
|
|
|
|
2016-12-22 08:04:07 -06:00
|
|
|
sigaction(signal, &action, nullptr);
|
2016-11-14 07:58:04 -06:00
|
|
|
|
2017-04-28 12:08:57 -05:00
|
|
|
dumpBacktrace();
|
|
|
|
|
|
|
|
// let default handler process the signal
|
|
|
|
kill(Poco::Process::id(), signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpBacktrace()
|
|
|
|
{
|
2016-11-14 07:58:04 -06:00
|
|
|
char header[32];
|
|
|
|
sprintf(header, "Backtrace %d:\n", getpid());
|
|
|
|
|
|
|
|
const int maxSlots = 50;
|
|
|
|
void *backtraceBuffer[maxSlots];
|
|
|
|
int numSlots = backtrace(backtraceBuffer, maxSlots);
|
|
|
|
if (numSlots > 0)
|
|
|
|
{
|
|
|
|
char **symbols = backtrace_symbols(backtraceBuffer, numSlots);
|
2016-12-22 08:04:07 -06:00
|
|
|
if (symbols != nullptr)
|
2016-11-14 07:58:04 -06:00
|
|
|
{
|
|
|
|
struct iovec ioVector[maxSlots*2+1];
|
2016-12-23 01:31:35 -06:00
|
|
|
ioVector[0].iov_base = static_cast<void*>(header);
|
|
|
|
ioVector[0].iov_len = std::strlen(static_cast<const char*>(ioVector[0].iov_base));
|
2016-11-14 07:58:04 -06:00
|
|
|
for (int i = 0; i < numSlots; i++)
|
|
|
|
{
|
|
|
|
ioVector[1+i*2+0].iov_base = symbols[i];
|
2016-12-23 01:31:35 -06:00
|
|
|
ioVector[1+i*2+0].iov_len = std::strlen(static_cast<const char *>(ioVector[1+i*2+0].iov_base));
|
|
|
|
ioVector[1+i*2+1].iov_base = const_cast<void*>(static_cast<const void*>("\n"));
|
2016-11-14 07:58:04 -06:00
|
|
|
ioVector[1+i*2+1].iov_len = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (writev(STDERR_FILENO, ioVector, numSlots*2+1) == -1)
|
|
|
|
{
|
|
|
|
LOG_SYS("Failed to dump backtrace to stderr.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::getenv("LOOL_DEBUG"))
|
|
|
|
{
|
|
|
|
LOG_ERR("Sleeping 30s to allow debugging.");
|
|
|
|
sleep(30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFatalSignals()
|
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = handleFatalSignal;
|
|
|
|
|
2016-12-22 08:04:07 -06:00
|
|
|
sigaction(SIGSEGV, &action, nullptr);
|
|
|
|
sigaction(SIGBUS, &action, nullptr);
|
|
|
|
sigaction(SIGABRT, &action, nullptr);
|
|
|
|
sigaction(SIGILL, &action, nullptr);
|
|
|
|
sigaction(SIGFPE, &action, nullptr);
|
2016-11-14 07:58:04 -06:00
|
|
|
|
|
|
|
// prepare this in advance just in case.
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << "\nFatal signal! Attach debugger with:\n"
|
|
|
|
<< "sudo gdb --pid=" << Poco::Process::id() << "\n or \n"
|
|
|
|
<< "sudo gdb --q --n --ex 'thread apply all backtrace full' --batch --pid="
|
|
|
|
<< Poco::Process::id() << "\n";
|
|
|
|
std::string streamStr = stream.str();
|
|
|
|
assert (sizeof (FatalGdbString) > strlen(streamStr.c_str()) + 1);
|
|
|
|
strncpy(FatalGdbString, streamStr.c_str(), sizeof(FatalGdbString));
|
|
|
|
}
|
|
|
|
|
2017-03-03 15:18:55 -06:00
|
|
|
static
|
|
|
|
void handleUserSignal(const int signal)
|
|
|
|
{
|
|
|
|
Log::signalLogPrefix();
|
|
|
|
Log::signalLog(" User signal received: ");
|
|
|
|
Log::signalLog(signalName(signal));
|
|
|
|
Log::signalLog("\n");
|
|
|
|
if (signal == SIGUSR1)
|
|
|
|
{
|
|
|
|
DumpGlobalState = true;
|
|
|
|
SocketPoll::wakeupWorld();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setUserSignals()
|
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = handleUserSignal;
|
|
|
|
|
|
|
|
sigaction(SIGUSR1, &action, nullptr);
|
|
|
|
}
|
|
|
|
|
2018-05-17 13:47:37 -05:00
|
|
|
/// Kill the given pid with SIGTERM. Returns true when the pid does not exist any more.
|
2016-11-14 07:58:04 -06:00
|
|
|
bool killChild(const int pid)
|
|
|
|
{
|
|
|
|
LOG_DBG("Killing PID: " << pid);
|
|
|
|
if (kill(pid, SIGTERM) == 0 || errno == ESRCH)
|
|
|
|
{
|
|
|
|
// Killed or doesn't exist.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_SYS("Error when trying to kill PID: " << pid << ". Will wait for termination.");
|
|
|
|
|
2018-02-07 03:17:19 -06:00
|
|
|
const int sleepMs = 50;
|
|
|
|
const int count = std::max(CHILD_REBALANCE_INTERVAL_MS / sleepMs, 2);
|
2016-11-14 07:58:04 -06:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
if (kill(pid, 0) == 0 || errno == ESRCH)
|
|
|
|
{
|
|
|
|
// Doesn't exist.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 08:01:15 -05:00
|
|
|
#endif // !MOBILEAPP
|
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|