1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
*
* 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/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#include "signal_handler.h"
#include <cstdlib>
#include <iostream>
#include <signal.h>
#include <atomic>
#include <vector>
#include "app.h"
#include "file/path.h"
#ifdef MRTRIX_WINDOWS
# define STDERR_FILENO 2
#endif
namespace MR
{
namespace SignalHandler {
namespace {
std::vector<cleanup_function_type> cleanup_operations;
std::vector<std::string> marked_files;
std::atomic_flag flag = ATOMIC_FLAG_INIT;
void delete_temporary_files ()
{
for (const auto& i : marked_files)
std::remove (i.c_str());
marked_files.clear();
}
void handler (int i) noexcept
{
// Only process this once if using multi-threading:
if (!flag.test_and_set()) {
// Try to do a tempfile cleanup before printing the error, since the latter's not guaranteed to work...
// Don't use File::remove: may throw an exception
for (auto func : cleanup_operations)
func();
const char* sig = nullptr;
const char* msg = nullptr;
switch (i) {
#define __SIGNAL(SIG,MSG) case SIG: sig = #SIG; msg = MSG; break;
#include "signals.h"
#undef __SIGNAL
default:
sig = "UNKNOWN";
msg = "Unknown fatal system signal";
break;
}
// Don't use std::cerr << here: Use basic C string-handling functions and a write() call to STDERR_FILENO
// Don't attempt to use any terminal colouring
char str[256];
str[255] = '\0';
snprintf (str, 255, "\n%s: [SYSTEM FATAL CODE: %s (%d)] %s\n", App::NAME.c_str(), sig, i, msg);
if (write (STDERR_FILENO, str, strnlen(str,256)) == 0)
std::_Exit (i);
else
std::_Exit (i);
}
}
}
void init()
{
on_signal (delete_temporary_files);
//ENVVAR name: MRTRIX_NOSIGNALS
//ENVVAR If this variable is set to any value, disable MRtrix3's custom
//ENVVAR signal handlers. This may sometimes be useful when debugging.
//ENVVAR Note however that this prevents the
//ENVVAR deletion of temporary files when the command terminates
//ENVVAR abnormally.
if (getenv ("MRTRIX_NOSIGNALS"))
return;
#ifdef MRTRIX_WINDOWS
// Use signal() rather than sigaction() for Windows, as the latter is not supported
# define __SIGNAL(SIG,MSG) signal (SIG, handler)
#else
// Construct the signal structure
struct sigaction act;
act.sa_handler = &handler;
// Since we're _Exit()-ing for any of these signals, block them all
sigfillset (&act.sa_mask);
act.sa_flags = 0;
# define __SIGNAL(SIG,MSG) sigaction (SIG, &act, nullptr)
#endif
#include "signals.h"
}
void on_signal (cleanup_function_type func)
{
cleanup_operations.push_back (func);
std::atexit (func);
}
void mark_file_for_deletion (const std::string& filename)
{
while (!flag.test_and_set());
marked_files.push_back (filename);
flag.clear();
}
void unmark_file_for_deletion (const std::string& filename)
{
while (!flag.test_and_set());
auto i = marked_files.begin();
while (i != marked_files.end()) {
if (*i == filename)
i = marked_files.erase (i);
else
++i;
}
flag.clear();
}
}
}
|