2018-02-04 03:22:01 +01: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-26 04:06:31 +02:00
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[noreturn, gnu::cold]]
|
|
|
|
static void
|
2018-01-11 06:32:32 +01:00
|
|
|
ircd_terminate_handler()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[gnu::cold]]
|
2018-01-11 06:32:32 +01:00
|
|
|
void
|
2019-01-14 00:50:04 +01:00
|
|
|
ircd::_aborting_()
|
2018-01-11 06:32:32 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
std::set_terminate(&ircd_terminate_handler);
|
|
|
|
}
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[gnu::cold]]
|
2019-01-14 00:50:04 +01:00
|
|
|
void
|
|
|
|
ircd::panicking(const std::exception_ptr &eptr)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
2019-03-16 03:33:24 +01:00
|
|
|
"IRCd panic :%s", what(eptr)
|
2019-01-14 00:50:04 +01: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.
|
2022-05-26 21:00:07 +02:00
|
|
|
[[gnu::cold]]
|
2019-01-14 00:50:04 +01:00
|
|
|
void
|
|
|
|
ircd::panicking(const std::exception &e)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
2019-03-16 03:33:24 +01:00
|
|
|
"IRCd panic :%s", e.what()
|
2019-01-14 00:50:04 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-08 17:34:55 +01: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 18:21:16 +01: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 17:34:55 +01: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 09:02:47 +02:00
|
|
|
return string(buf, make_system_error(ec));
|
2018-03-08 18:21:16 +01:00
|
|
|
}
|
2018-03-08 17:34:55 +01:00
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const std::system_error &e)
|
|
|
|
{
|
|
|
|
return string(buf, e.code());
|
2018-03-08 17:34:55 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const std::error_code &ec)
|
2018-03-08 17:34:55 +01:00
|
|
|
{
|
2018-03-08 18:21:16 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2020-10-22 11:01:52 +02:00
|
|
|
buf, "%s :%s", ec.category().name(), ec.message()
|
2018-03-08 18:21:16 +01:00
|
|
|
};
|
2018-03-08 17:34:55 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 02:47:59 +02:00
|
|
|
bool
|
|
|
|
ircd::is(const boost::system::error_code &ec,
|
|
|
|
const std::errc &errc)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return is(make_error_code(ec), errc);
|
|
|
|
}
|
|
|
|
|
2018-11-09 06:04:07 +01:00
|
|
|
bool
|
2018-11-09 09:29:31 +01:00
|
|
|
ircd::is(const std::error_code &ec,
|
|
|
|
const std::errc &errc)
|
|
|
|
noexcept
|
2018-11-09 06:04:07 +01:00
|
|
|
{
|
2019-09-25 04:57:03 +02:00
|
|
|
return system_category(ec) && ec.value() == int(errc);
|
2019-04-04 02:47:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::system_category(const boost::system::error_code &ec)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return system_category(make_error_code(ec));
|
2018-11-09 06:04:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::system_category(const std::error_code &ec)
|
2018-11-09 09:29:31 +01:00
|
|
|
noexcept
|
2018-11-09 06:04:07 +01:00
|
|
|
{
|
|
|
|
return system_category(ec.category());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::system_category(const std::error_category &ec)
|
2018-11-09 09:29:31 +01:00
|
|
|
noexcept
|
2018-11-09 06:04:07 +01:00
|
|
|
{
|
|
|
|
return ec == std::system_category() ||
|
|
|
|
ec == std::generic_category() ||
|
2019-06-23 09:15:48 +02:00
|
|
|
ec == boost::system::system_category() ||
|
2019-04-04 02:47:59 +02:00
|
|
|
ec == boost::system::generic_category();
|
2018-11-09 06:04:07 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const boost::system::system_error &e)
|
2018-01-11 06:32:32 +01:00
|
|
|
{
|
2018-03-08 18:21:16 +01:00
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(e.code()), e.what()
|
|
|
|
};
|
2018-01-11 06:32:32 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const boost::system::error_code &ec)
|
|
|
|
{
|
|
|
|
return std::system_error
|
|
|
|
{
|
2019-04-11 16:50:48 +02:00
|
|
|
make_error_code(ec)
|
2018-03-08 18:21:16 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-09 03:07:22 +01: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 18:21:16 +01:00
|
|
|
std::system_error
|
2018-01-11 06:32:32 +01:00
|
|
|
ircd::make_system_error(const int &code)
|
|
|
|
{
|
2018-03-08 18:21:16 +01:00
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(code)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const boost::system::system_error &e)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-03-08 18:21:16 +01:00
|
|
|
{
|
|
|
|
return std::error_code
|
|
|
|
{
|
|
|
|
e.code().value(), e.code().category()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const boost::system::error_code &ec)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-03-08 18:21:16 +01:00
|
|
|
{
|
|
|
|
return std::error_code
|
|
|
|
{
|
|
|
|
ec.value(), ec.category()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-23 12:23:59 +02:00
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const std::system_error &e)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-08-23 12:23:59 +02:00
|
|
|
{
|
|
|
|
return e.code();
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:16 +01:00
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const int &code)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-03-08 18:21:16 +01:00
|
|
|
{
|
|
|
|
return std::error_code
|
|
|
|
{
|
2019-04-04 02:47:59 +02:00
|
|
|
code, std::generic_category()
|
2018-03-08 18:21:16 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const std::error_code &ec)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-03-08 18:21:16 +01:00
|
|
|
{
|
|
|
|
return ec;
|
2018-01-11 06:32:32 +01:00
|
|
|
}
|
|
|
|
|
2018-11-09 02:10:56 +01:00
|
|
|
//
|
|
|
|
// exception
|
|
|
|
//
|
|
|
|
|
2020-02-28 21:12:51 +01:00
|
|
|
// Out-of-line placement
|
|
|
|
ircd::exception::~exception()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-15 04:03:25 +02:00
|
|
|
ssize_t
|
2018-12-10 22:08:35 +01:00
|
|
|
ircd::exception::generate(const string_view &fmt,
|
2017-03-18 01:37:40 +01:00
|
|
|
const va_rtti &ap)
|
2016-07-26 04:06:31 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2017-03-18 01:37:40 +01:00
|
|
|
return fmt::vsnprintf(buf, sizeof(buf), fmt, ap);
|
2016-08-15 04:03:25 +02:00
|
|
|
}
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2016-08-15 04:03:25 +02:00
|
|
|
ssize_t
|
2017-03-18 01:37:40 +01:00
|
|
|
ircd::exception::generate(const char *const &name,
|
2018-12-10 22:08:35 +01:00
|
|
|
const string_view &fmt,
|
2017-03-18 01:37:40 +01:00
|
|
|
const va_rtti &ap)
|
2016-08-15 04:03:25 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2016-07-26 04:06:31 +02:00
|
|
|
size_t size(0);
|
|
|
|
const bool empty(!fmt || !fmt[0] || fmt[0] == ' ');
|
2016-11-29 16:23:38 +01:00
|
|
|
size = strlcat(buf, name, sizeof(buf));
|
2019-03-16 03:33:24 +01:00
|
|
|
size = strlcat(buf, empty? "." : " :", sizeof(buf));
|
2016-07-26 04:06:31 +02:00
|
|
|
if(size < sizeof(buf))
|
2017-03-18 01:37:40 +01:00
|
|
|
size += fmt::vsnprintf(buf + size, sizeof(buf) - size, fmt, ap);
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2016-08-15 04:03:25 +02:00
|
|
|
return size;
|
2016-07-26 04:06:31 +02:00
|
|
|
}
|
2017-11-26 01:20:42 +01:00
|
|
|
|
2019-05-27 11:55:32 +02:00
|
|
|
const char *
|
|
|
|
ircd::exception::what()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-11-09 02:10:56 +01:00
|
|
|
//
|
|
|
|
// terminate
|
|
|
|
//
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[noreturn, gnu::cold]]
|
2020-12-13 15:03:47 +01:00
|
|
|
ircd::terminate::terminate(std::exception_ptr eptr)
|
2017-11-26 01:20:42 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2020-12-13 15:03:47 +01:00
|
|
|
fprintf(stderr, "\nIRCd Terminated :%s\n", what(eptr).data());
|
|
|
|
::fflush(stderr);
|
|
|
|
std::terminate();
|
2019-06-23 08:28:48 +02:00
|
|
|
__builtin_unreachable();
|
2017-11-26 01:20:42 +01:00
|
|
|
}
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[noreturn, gnu::cold]]
|
2020-12-13 15:03:47 +01:00
|
|
|
ircd::terminate::terminate(const std::exception &e)
|
2019-06-23 14:59:05 +02:00
|
|
|
noexcept
|
2020-12-13 15:03:47 +01:00
|
|
|
:terminate
|
2019-06-23 14:59:05 +02:00
|
|
|
{
|
2020-12-13 15:03:47 +01:00
|
|
|
std::make_exception_ptr(e)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
__builtin_unreachable();
|
2019-06-23 14:59:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[noreturn, gnu::cold]]
|
2020-12-13 15:03:47 +01:00
|
|
|
ircd::terminate::terminate()
|
2017-11-26 01:20:42 +01:00
|
|
|
noexcept
|
2020-12-13 15:03:47 +01:00
|
|
|
:terminate
|
2017-11-26 01:20:42 +01:00
|
|
|
{
|
2020-12-13 15:03:47 +01:00
|
|
|
std::current_exception()
|
|
|
|
}
|
|
|
|
{
|
|
|
|
__builtin_unreachable();
|
2017-11-26 01:20:42 +01:00
|
|
|
}
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[noreturn, gnu::cold]]
|
2020-12-13 15:03:47 +01:00
|
|
|
ircd::terminate::~terminate()
|
2017-11-26 01:20:42 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2020-12-13 15:03:47 +01:00
|
|
|
__builtin_unreachable();
|
2017-11-26 01:20:42 +01:00
|
|
|
}
|