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 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/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2016-11-14 07:58:04 -06:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
2020-06-08 05:41:03 -05:00
|
|
|
#include <signal.h>
|
2016-11-14 07:58:04 -06:00
|
|
|
|
2019-08-08 02:10:59 -05:00
|
|
|
namespace SigUtil
|
|
|
|
{
|
2020-04-24 02:46:54 -05:00
|
|
|
#ifndef IOS
|
2019-11-03 19:52:19 -06:00
|
|
|
/// Get the flag used to commence clean shutdown.
|
|
|
|
/// requestShutdown() is used to set the flag.
|
|
|
|
bool getShutdownRequestFlag();
|
2017-03-03 15:18:55 -06:00
|
|
|
|
2019-11-03 19:52:19 -06:00
|
|
|
/// Get the flag to stop pump loops forcefully.
|
2021-10-03 10:05:26 -05:00
|
|
|
/// If this returns true, getShutdownRequestFlag() must also return true.
|
2019-11-03 19:52:19 -06:00
|
|
|
bool getTerminationFlag();
|
2021-11-13 15:26:04 -06:00
|
|
|
/// Set the flag to stop pump loops forcefully and request shutting down.
|
2019-11-03 19:52:19 -06:00
|
|
|
void setTerminationFlag();
|
2019-05-14 14:37:11 -05:00
|
|
|
#if MOBILEAPP
|
2021-11-13 15:26:04 -06:00
|
|
|
/// Reset the flags to stop pump loops forcefully.
|
2019-11-03 19:52:19 -06:00
|
|
|
/// Only necessary in Mobile.
|
2021-11-13 15:26:04 -06:00
|
|
|
void resetTerminationFlags();
|
2019-05-14 14:37:11 -05:00
|
|
|
#endif
|
2020-04-24 02:46:54 -05:00
|
|
|
#else
|
|
|
|
// In the mobile apps we have no need to shut down the app.
|
|
|
|
inline constexpr bool getShutdownRequestFlag()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-14 14:37:11 -05:00
|
|
|
|
2020-04-24 02:46:54 -05:00
|
|
|
inline constexpr bool getTerminationFlag()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void setTerminationFlag()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-07-14 09:20:05 -05:00
|
|
|
extern "C" { typedef void (*GlobalDumpStateFn)(void); }
|
|
|
|
|
|
|
|
void checkDumpGlobalState(GlobalDumpStateFn dumpState);
|
2018-10-15 08:01:15 -05:00
|
|
|
|
2022-01-07 06:20:34 -06:00
|
|
|
/// Add a message to a round-robin buffer to be dumped on fatal signal
|
|
|
|
void addActivity(const std::string &message);
|
2021-07-09 10:57:09 -05:00
|
|
|
|
2022-02-15 20:04:37 -06:00
|
|
|
/// Called to flag that we are running in unattended mode, not interactive.
|
|
|
|
/// In unattended mode we know there is no one to attach a debugger on
|
|
|
|
/// faulting, so we do not wait unnecessarily. Otherwise, we wait for 60s.
|
|
|
|
void setUnattended();
|
|
|
|
|
2020-07-14 09:20:05 -05:00
|
|
|
#if !MOBILEAPP
|
2019-11-18 08:12:30 -06:00
|
|
|
/// Wait for the signal handler, if any,
|
2019-08-13 02:08:16 -05:00
|
|
|
/// and prevent _Exit while collecting backtrace.
|
2019-11-18 08:12:30 -06:00
|
|
|
void waitSigHandlerTrap();
|
2019-08-13 02:08:16 -05:00
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
/// Returns the name of the signal.
|
|
|
|
const char* signalName(int signo);
|
|
|
|
|
2017-03-02 12:12:52 -06:00
|
|
|
/// Register a wakeup function when changing
|
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
/// Trap signals to cleanup and exit the process gracefully.
|
|
|
|
void setTerminationSignals();
|
|
|
|
|
|
|
|
/// Trap all fatal signals to assist debugging.
|
2021-03-25 13:11:13 -05:00
|
|
|
void setFatalSignals(const std::string &versionInfo);
|
|
|
|
|
|
|
|
/// Update version info
|
|
|
|
void setVersionInfo(const std::string &versionInfo);
|
2016-11-14 07:58:04 -06:00
|
|
|
|
2017-03-03 15:18:55 -06:00
|
|
|
/// Trap generally useful signals
|
|
|
|
void setUserSignals();
|
|
|
|
|
2019-12-18 14:28:14 -06:00
|
|
|
/// Trap to unpause the process
|
|
|
|
void setDebuggerSignal();
|
|
|
|
|
2016-11-19 09:22:53 -06:00
|
|
|
/// Requests the server to initiate graceful shutdown.
|
|
|
|
/// Shutting down is a multi-stage process, because
|
|
|
|
/// it can be requested via signals.
|
|
|
|
/// Since we need to notify clients, we can't
|
|
|
|
/// invoke the sockets while in a signal handler.
|
|
|
|
/// This flags the server to notify clients first
|
|
|
|
/// then flags for shutdown.
|
|
|
|
void requestShutdown();
|
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
/// Kills a child process and returns true when
|
|
|
|
/// child pid is removed from the process table
|
|
|
|
/// after a certain (short) timeout.
|
2020-06-08 05:41:03 -05:00
|
|
|
bool killChild(const int pid, const int signal = SIGKILL);
|
2016-11-14 07:58:04 -06:00
|
|
|
|
2017-04-28 12:08:57 -05:00
|
|
|
/// Dump a signal-safe back-trace
|
|
|
|
void dumpBacktrace();
|
|
|
|
|
2018-10-15 08:01:15 -05:00
|
|
|
#endif // !MOBILEAPP
|
|
|
|
|
2019-11-18 08:12:30 -06:00
|
|
|
} // end namespace SigUtil
|
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|