2018-02-03 18:22:01 -08:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-07-25 19:06:31 -07:00
|
|
|
|
2018-03-08 08:34:55 -08:00
|
|
|
#include <boost/system/system_error.hpp>
|
|
|
|
|
2018-01-10 21:32:32 -08:00
|
|
|
[[noreturn]] static void
|
|
|
|
ircd_terminate_handler()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-13 15:50:04 -08:00
|
|
|
ircd::_aborting_()
|
2018-01-10 21:32:32 -08:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
std::set_terminate(&ircd_terminate_handler);
|
|
|
|
}
|
|
|
|
|
2019-01-13 15:50:04 -08:00
|
|
|
void
|
2019-03-15 13:01:26 -07:00
|
|
|
#if !defined(NDEBUG) && !defined(RB_ASSERT)
|
2019-01-13 15:50:04 -08:00
|
|
|
__attribute__((noreturn))
|
|
|
|
#endif
|
|
|
|
ircd::panicking(const std::exception_ptr &eptr)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
2019-03-15 19:33:24 -07:00
|
|
|
"IRCd panic :%s", what(eptr)
|
2019-01-13 15:50:04 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called by the constructor of a panic exception when thown. We immediately
|
|
|
|
/// log a critical message, which is actually what triggers a termination when
|
|
|
|
/// assertions are enabled (!NDEBUG) otherwise a no-op.
|
|
|
|
void
|
2019-03-15 13:01:26 -07:00
|
|
|
#if !defined(NDEBUG) && !defined(RB_ASSERT)
|
2019-01-13 15:50:04 -08:00
|
|
|
__attribute__((noreturn))
|
|
|
|
#endif
|
|
|
|
ircd::panicking(const std::exception &e)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
2019-03-15 19:33:24 -07:00
|
|
|
"IRCd panic :%s", e.what()
|
2019-01-13 15:50:04 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-08 08:34:55 -08:00
|
|
|
std::string
|
|
|
|
ircd::string(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
return string(e.code());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::string(const boost::system::error_code &ec)
|
|
|
|
{
|
|
|
|
return ircd::string(128, [&ec]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return string(buf, ec);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-08 09:21:16 -08:00
|
|
|
std::string
|
|
|
|
ircd::string(const std::system_error &e)
|
|
|
|
{
|
|
|
|
return string(e.code());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::string(const std::error_code &ec)
|
|
|
|
{
|
|
|
|
return ircd::string(128, [&ec]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return string(buf, ec);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-08 08:34:55 -08:00
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
return string(buf, e.code());
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const boost::system::error_code &ec)
|
|
|
|
{
|
2018-09-19 00:02:47 -07:00
|
|
|
return string(buf, make_system_error(ec));
|
2018-03-08 09:21:16 -08:00
|
|
|
}
|
2018-03-08 08:34:55 -08:00
|
|
|
|
2018-03-08 09:21:16 -08:00
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const std::system_error &e)
|
|
|
|
{
|
|
|
|
return string(buf, e.code());
|
2018-03-08 08:34:55 -08:00
|
|
|
}
|
|
|
|
|
2018-03-08 09:21:16 -08:00
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const std::error_code &ec)
|
2018-03-08 08:34:55 -08:00
|
|
|
{
|
2018-03-08 09:21:16 -08:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s: %s", ec.category().name(), ec.message()
|
|
|
|
};
|
2018-03-08 08:34:55 -08:00
|
|
|
}
|
|
|
|
|
2019-04-03 17:47:59 -07:00
|
|
|
bool
|
|
|
|
ircd::is(const boost::system::error_code &ec,
|
|
|
|
const std::errc &errc)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return is(make_error_code(ec), errc);
|
|
|
|
}
|
|
|
|
|
2018-11-08 21:04:07 -08:00
|
|
|
bool
|
2018-11-09 00:29:31 -08:00
|
|
|
ircd::is(const std::error_code &ec,
|
|
|
|
const std::errc &errc)
|
|
|
|
noexcept
|
2018-11-08 21:04:07 -08:00
|
|
|
{
|
2019-04-03 17:47:59 -07:00
|
|
|
return ec == errc;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::system_category(const boost::system::error_code &ec)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return system_category(make_error_code(ec));
|
2018-11-08 21:04:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::system_category(const std::error_code &ec)
|
2018-11-09 00:29:31 -08:00
|
|
|
noexcept
|
2018-11-08 21:04:07 -08:00
|
|
|
{
|
|
|
|
return system_category(ec.category());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::system_category(const std::error_category &ec)
|
2018-11-09 00:29:31 -08:00
|
|
|
noexcept
|
2018-11-08 21:04:07 -08:00
|
|
|
{
|
|
|
|
return ec == std::system_category() ||
|
|
|
|
ec == std::generic_category() ||
|
2019-06-23 01:15:48 -06:00
|
|
|
ec == boost::system::system_category() ||
|
2019-04-03 17:47:59 -07:00
|
|
|
ec == boost::system::generic_category();
|
2018-11-08 21:04:07 -08:00
|
|
|
}
|
|
|
|
|
2018-03-08 09:21:16 -08:00
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const boost::system::system_error &e)
|
2018-01-10 21:32:32 -08:00
|
|
|
{
|
2018-03-08 09:21:16 -08:00
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(e.code()), e.what()
|
|
|
|
};
|
2018-01-10 21:32:32 -08:00
|
|
|
}
|
|
|
|
|
2018-03-08 09:21:16 -08:00
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const boost::system::error_code &ec)
|
|
|
|
{
|
|
|
|
return std::system_error
|
|
|
|
{
|
2019-04-11 07:50:48 -07:00
|
|
|
make_error_code(ec)
|
2018-03-08 09:21:16 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-08 18:07:22 -08:00
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const std::error_code &ec)
|
|
|
|
{
|
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(ec)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const std::errc &ec)
|
|
|
|
{
|
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(ec)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-08 09:21:16 -08:00
|
|
|
std::system_error
|
2018-01-10 21:32:32 -08:00
|
|
|
ircd::make_system_error(const int &code)
|
|
|
|
{
|
2018-03-08 09:21:16 -08:00
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(code)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
return std::error_code
|
|
|
|
{
|
|
|
|
e.code().value(), e.code().category()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const boost::system::error_code &ec)
|
|
|
|
{
|
|
|
|
return std::error_code
|
|
|
|
{
|
|
|
|
ec.value(), ec.category()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-23 03:23:59 -07:00
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const std::system_error &e)
|
|
|
|
{
|
|
|
|
return e.code();
|
|
|
|
}
|
|
|
|
|
2018-03-08 09:21:16 -08:00
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const int &code)
|
|
|
|
{
|
|
|
|
return std::error_code
|
|
|
|
{
|
2019-04-03 17:47:59 -07:00
|
|
|
code, std::generic_category()
|
2018-03-08 09:21:16 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const std::error_code &ec)
|
|
|
|
{
|
|
|
|
return ec;
|
2018-01-10 21:32:32 -08:00
|
|
|
}
|
|
|
|
|
2018-11-08 17:10:56 -08:00
|
|
|
//
|
|
|
|
// exception
|
|
|
|
//
|
|
|
|
|
2016-08-14 19:03:25 -07:00
|
|
|
ssize_t
|
2018-12-10 13:08:35 -08:00
|
|
|
ircd::exception::generate(const string_view &fmt,
|
2017-03-17 17:37:40 -07:00
|
|
|
const va_rtti &ap)
|
2016-07-25 19:06:31 -07:00
|
|
|
noexcept
|
|
|
|
{
|
2017-03-17 17:37:40 -07:00
|
|
|
return fmt::vsnprintf(buf, sizeof(buf), fmt, ap);
|
2016-08-14 19:03:25 -07:00
|
|
|
}
|
2016-07-25 19:06:31 -07:00
|
|
|
|
2016-08-14 19:03:25 -07:00
|
|
|
ssize_t
|
2017-03-17 17:37:40 -07:00
|
|
|
ircd::exception::generate(const char *const &name,
|
2018-12-10 13:08:35 -08:00
|
|
|
const string_view &fmt,
|
2017-03-17 17:37:40 -07:00
|
|
|
const va_rtti &ap)
|
2016-08-14 19:03:25 -07:00
|
|
|
noexcept
|
|
|
|
{
|
2016-07-25 19:06:31 -07:00
|
|
|
size_t size(0);
|
|
|
|
const bool empty(!fmt || !fmt[0] || fmt[0] == ' ');
|
2016-11-29 07:23:38 -08:00
|
|
|
size = strlcat(buf, name, sizeof(buf));
|
2019-03-15 19:33:24 -07:00
|
|
|
size = strlcat(buf, empty? "." : " :", sizeof(buf));
|
2016-07-25 19:06:31 -07:00
|
|
|
if(size < sizeof(buf))
|
2017-03-17 17:37:40 -07:00
|
|
|
size += fmt::vsnprintf(buf + size, sizeof(buf) - size, fmt, ap);
|
2016-07-25 19:06:31 -07:00
|
|
|
|
2016-08-14 19:03:25 -07:00
|
|
|
return size;
|
2016-07-25 19:06:31 -07:00
|
|
|
}
|
2017-11-25 17:20:42 -07:00
|
|
|
|
2019-05-27 02:55:32 -07:00
|
|
|
const char *
|
|
|
|
ircd::exception::what()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-11-08 17:10:56 -08:00
|
|
|
//
|
|
|
|
// terminate
|
|
|
|
//
|
|
|
|
|
2018-11-04 17:18:00 -08:00
|
|
|
ircd::terminate::terminate()
|
2017-11-25 17:20:42 -07:00
|
|
|
noexcept
|
|
|
|
{
|
2019-06-23 00:28:48 -06:00
|
|
|
ircd::terminate
|
|
|
|
{
|
|
|
|
std::current_exception()
|
|
|
|
};
|
|
|
|
|
|
|
|
__builtin_unreachable();
|
2017-11-25 17:20:42 -07:00
|
|
|
}
|
|
|
|
|
2019-06-23 06:59:05 -06:00
|
|
|
ircd::terminate::terminate(const string_view &str)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
static char buf[512];
|
|
|
|
strlcpy(buf, str);
|
|
|
|
fprintf(stderr, "\nIRCd Terminated :%s\n", buf);
|
|
|
|
::fflush(stderr);
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
|
2018-11-04 17:18:00 -08:00
|
|
|
ircd::terminate::terminate(std::exception_ptr eptr)
|
2017-11-25 17:20:42 -07:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(eptr) try
|
|
|
|
{
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-11-04 17:18:00 -08:00
|
|
|
terminate{e};
|
2017-11-25 17:20:42 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:57:25 -08:00
|
|
|
fputs("\nIRCd Terminated.\n", stderr);
|
2018-11-01 22:25:10 -07:00
|
|
|
::fflush(stderr);
|
2017-11-25 17:20:42 -07:00
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
|
2018-11-04 17:18:00 -08:00
|
|
|
ircd::terminate::terminate(const std::exception &e)
|
2017-11-25 17:20:42 -07:00
|
|
|
noexcept
|
|
|
|
{
|
2019-03-15 19:33:24 -07:00
|
|
|
fprintf(stderr, "\nIRCd Terminated :%s\n", e.what());
|
2018-11-01 22:25:10 -07:00
|
|
|
::fflush(stderr);
|
|
|
|
std::terminate();
|
2017-11-25 17:20:42 -07:00
|
|
|
}
|