2018-01-14 03:03:04 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2017-09-21 04:31:54 +02:00
|
|
|
#include <ircd/asio.h>
|
2017-11-30 20:44:23 +01:00
|
|
|
#include <ircd/m/m.h>
|
2016-09-06 22:11:55 +02:00
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
2017-11-30 19:26:13 +01:00
|
|
|
enum runlevel _runlevel; // Current libircd runlevel
|
|
|
|
const enum runlevel &runlevel{_runlevel}; // Observer for current RL
|
|
|
|
runlevel_handler runlevel_changed; // user's callback
|
|
|
|
|
2017-12-29 23:53:39 +01:00
|
|
|
boost::asio::io_context *ios; // user's io service
|
2017-11-30 19:26:13 +01:00
|
|
|
struct strand *strand; // libircd event serializer
|
|
|
|
|
|
|
|
std::string _conf; // JSON read from configfile
|
|
|
|
const std::string &conf{_conf}; // Observer for conf data
|
|
|
|
|
|
|
|
ctx::ctx *main_context; // Main program loop
|
|
|
|
bool debugmode; // meaningful ifdef RB_DEBUG
|
2016-09-06 22:11:55 +02:00
|
|
|
|
2017-11-30 19:26:13 +01:00
|
|
|
std::string read_conf(std::string file);
|
|
|
|
void set_runlevel(const enum runlevel &);
|
2017-09-30 08:04:41 +02:00
|
|
|
void at_main_exit() noexcept;
|
2016-11-29 16:23:38 +01:00
|
|
|
void main();
|
2016-09-06 22:11:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-06 21:14:50 +01:00
|
|
|
/// Record of the ID of the thread static initialization took place on.
|
2017-10-12 02:38:24 +02:00
|
|
|
const std::thread::id
|
|
|
|
ircd::static_thread_id
|
|
|
|
{
|
|
|
|
std::this_thread::get_id()
|
|
|
|
};
|
|
|
|
|
2017-11-06 21:14:50 +01:00
|
|
|
/// "main" thread for IRCd; the one the main context landed on.
|
2017-10-12 02:38:24 +02:00
|
|
|
std::thread::id
|
|
|
|
ircd::thread_id
|
|
|
|
{};
|
|
|
|
|
2016-09-06 22:11:55 +02:00
|
|
|
void
|
2017-12-29 23:53:39 +01:00
|
|
|
ircd::init(boost::asio::io_context &ios,
|
2017-09-26 03:43:33 +02:00
|
|
|
runlevel_handler function)
|
|
|
|
{
|
|
|
|
init(ios, std::string{}, std::move(function));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets up the IRCd and its main context, then returns without blocking.
|
|
|
|
//
|
2017-12-29 23:53:39 +01:00
|
|
|
/// Pass your io_context instance, it will share it with the rest of your program.
|
2017-09-26 03:43:33 +02:00
|
|
|
/// An exception will be thrown on error.
|
|
|
|
///
|
|
|
|
/// This function will setup the main program loop of libircd. The execution will
|
2017-12-29 23:53:39 +01:00
|
|
|
/// occur when your io_context.run() or poll() is further invoked.
|
2017-09-26 03:43:33 +02:00
|
|
|
///
|
2017-11-06 21:14:50 +01:00
|
|
|
/// init() can only be called from a runlevel::HALT state
|
2017-09-26 03:43:33 +02:00
|
|
|
void
|
2017-12-29 23:53:39 +01:00
|
|
|
ircd::init(boost::asio::io_context &ios,
|
2016-09-09 19:48:33 +02:00
|
|
|
const std::string &configfile,
|
2017-09-26 03:43:33 +02:00
|
|
|
runlevel_handler runlevel_changed)
|
2017-09-30 08:04:41 +02:00
|
|
|
try
|
2016-09-06 22:11:55 +02:00
|
|
|
{
|
2017-11-06 21:14:50 +01:00
|
|
|
if(runlevel != runlevel::HALT)
|
2018-01-22 09:25:08 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Cannot init() IRCd from runlevel %s", reflect(runlevel)
|
|
|
|
};
|
2017-11-06 21:14:50 +01:00
|
|
|
|
2018-01-24 02:56:05 +01:00
|
|
|
// Sample the ID of this thread. Since this is the first transfer of
|
|
|
|
// control to libircd after static initialization we have nothing to
|
|
|
|
// consider a main thread yet. We need something set for many assetions
|
|
|
|
// to pass until ircd::main() is entered which will reset this to where
|
|
|
|
// ios.run() is really running.
|
2017-10-12 02:38:24 +02:00
|
|
|
ircd::thread_id = std::this_thread::get_id();
|
2017-11-06 21:14:50 +01:00
|
|
|
|
2017-12-29 23:53:39 +01:00
|
|
|
// Global ircd:: reference to the user's io_context and setup our main
|
2017-11-06 21:14:50 +01:00
|
|
|
// strand on that service.
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::ios = &ios;
|
|
|
|
ircd::strand = new struct strand(ios);
|
|
|
|
|
2017-11-06 21:14:50 +01:00
|
|
|
// Saves the user's runlevel_changed callback which we invoke.
|
|
|
|
ircd::runlevel_changed = std::move(runlevel_changed);
|
|
|
|
|
2016-09-06 22:11:55 +02:00
|
|
|
// The log is available, but it is console-only until conf opens files.
|
|
|
|
log::init();
|
2017-11-06 21:14:50 +01:00
|
|
|
log::mark("READY");
|
2016-09-06 22:11:55 +02:00
|
|
|
|
2018-01-17 12:55:36 +01:00
|
|
|
// This starts off the log with library information.
|
|
|
|
info::init();
|
2017-11-30 19:26:13 +01:00
|
|
|
|
|
|
|
// The configuration file is a user-converted Synapse homeserver.yaml
|
|
|
|
// converted into JSON. The configuration file is only truly meaningful
|
|
|
|
// the first time IRCd is ever run. Subsequently, only the database must
|
|
|
|
// be found. Real configuration is stored in the !config channel.
|
|
|
|
//
|
|
|
|
// This subroutine reads a file either at the user-supplied path or the
|
|
|
|
// default path specified in ircd::fs, vets for basic syntax issues, and
|
|
|
|
// then returns a string of JSON (the file's contents). The validity of
|
|
|
|
// the actual configuration is not known until specific subsystems are
|
|
|
|
// init'ed later.
|
|
|
|
//
|
|
|
|
// *NOTE* This expects *canonical JSON* right now. That means converting
|
|
|
|
// your homeserver.yaml may be a two step process: 1. YAML to JSON, 2.
|
|
|
|
// whitespace-stripping the JSON. Tools to do both of these things are
|
|
|
|
// first hits in a google search.
|
|
|
|
ircd::_conf = read_conf(configfile);
|
|
|
|
|
2017-11-06 21:14:50 +01:00
|
|
|
// Setup the main context, which is a new stack executing the function
|
|
|
|
// ircd::main(). The main_context is the first ircd::ctx to be spawned
|
|
|
|
// and will be the last to finish.
|
|
|
|
//
|
2017-12-29 23:53:39 +01:00
|
|
|
// The context::POST will delay this spawn until the next io_context
|
2017-11-06 21:14:50 +01:00
|
|
|
// event slice, so no context switch will occur here. Note that POST has
|
|
|
|
// to be used here because: A. This init() function is executing on the
|
|
|
|
// main stack, and context switches can only occur between context stacks,
|
2017-12-29 23:53:39 +01:00
|
|
|
// not between contexts and the main stack. B. The user's io_context may or
|
2017-11-06 21:14:50 +01:00
|
|
|
// may not even be running yet anyway.
|
|
|
|
//
|
2017-09-26 03:43:33 +02:00
|
|
|
ircd::context main_context
|
|
|
|
{
|
|
|
|
"main", 256_KiB, &ircd::main, context::POST
|
|
|
|
};
|
|
|
|
|
2017-11-06 21:14:50 +01:00
|
|
|
// The default behavior for ircd::context is to join the ctx on dtor. We
|
|
|
|
// can't have that here because this is strictly an asynchronous function
|
|
|
|
// on the main stack. Under normal circumstances, the mc will be entered
|
|
|
|
// and be able to delete this pointer itself when it finishes. Otherwise
|
|
|
|
// this must be manually deleted with assurance that mc will never enter.
|
2017-09-26 03:43:33 +02:00
|
|
|
ircd::main_context = main_context.detach();
|
2017-11-06 21:14:50 +01:00
|
|
|
|
|
|
|
// Finally, without prior exception, the commitment to runlevel::READY
|
|
|
|
// is made here. The user can now invoke their ios.run(), or, if they
|
|
|
|
// have already, IRCd will begin main execution shortly...
|
2017-09-26 03:43:33 +02:00
|
|
|
ircd::set_runlevel(runlevel::READY);
|
2016-09-06 22:11:55 +02:00
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
delete strand;
|
|
|
|
strand = nullptr;
|
|
|
|
throw;
|
|
|
|
}
|
2016-09-06 22:11:55 +02:00
|
|
|
|
2017-09-26 03:43:33 +02:00
|
|
|
/// Notifies IRCd to shutdown. A shutdown will occur asynchronously and this
|
2017-11-06 21:14:50 +01:00
|
|
|
/// function will return immediately. A runlevel change to HALT will be
|
|
|
|
/// indicated when IRCd has no more work for the ios. When the HALT state
|
|
|
|
/// is observed the user is free to destruct all resources related to libircd.
|
2017-09-26 03:43:33 +02:00
|
|
|
///
|
|
|
|
/// This function is the proper way to shutdown libircd after an init(), and while
|
2017-12-29 23:53:39 +01:00
|
|
|
/// your io_context.run() is invoked without stopping your io_context shared by
|
|
|
|
/// other activities unrelated to libircd. If your io_context has no other activities
|
2017-11-06 21:14:50 +01:00
|
|
|
/// the run() will then return immediately after IRCd posts its transition to
|
|
|
|
/// the HALT state.
|
2017-09-26 03:43:33 +02:00
|
|
|
///
|
|
|
|
bool
|
2017-11-06 21:14:50 +01:00
|
|
|
ircd::quit()
|
2017-09-26 03:43:33 +02:00
|
|
|
noexcept
|
2016-10-26 08:25:04 +02:00
|
|
|
{
|
2017-09-26 03:43:33 +02:00
|
|
|
if(runlevel != runlevel::RUN)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!main_context)
|
|
|
|
return false;
|
2016-10-26 08:25:04 +02:00
|
|
|
|
2017-09-26 03:43:33 +02:00
|
|
|
ctx::notify(*main_context);
|
|
|
|
return true;
|
2016-10-26 08:25:04 +02:00
|
|
|
}
|
|
|
|
|
2017-09-09 16:30:45 +02:00
|
|
|
/// Main context; Main program loop. Do not call this function directly.
|
2017-09-26 03:43:33 +02:00
|
|
|
///
|
|
|
|
/// This function manages the lifetime for all resources and subsystems
|
|
|
|
/// that don't/can't have their own static initialization. When this
|
|
|
|
/// function is entered, subsystem init objects are constructed on the
|
|
|
|
/// frame. The lifetime of those objects is the handle to the lifetime
|
|
|
|
/// of the subsystem, so destruction will shut down that subsystem.
|
|
|
|
///
|
|
|
|
/// The status of this function and IRCd overall can be observed through
|
|
|
|
/// the ircd::runlevel. The ircd::runlevel_changed callback can be set
|
|
|
|
/// to be notified on a runlevel change. The user should wait for a runlevel
|
2017-11-06 21:14:50 +01:00
|
|
|
/// of HALT before destroying IRCd related resources and stopping their
|
2017-12-29 23:53:39 +01:00
|
|
|
/// io_context from running more jobs.
|
2017-09-26 03:43:33 +02:00
|
|
|
///
|
2016-09-06 22:11:55 +02:00
|
|
|
void
|
2016-09-08 22:29:02 +02:00
|
|
|
ircd::main()
|
2016-11-29 16:23:38 +01:00
|
|
|
try
|
2016-09-06 22:11:55 +02:00
|
|
|
{
|
2018-01-24 02:56:05 +01:00
|
|
|
// Resamples the thread this context was executed on which should be where
|
|
|
|
// the user ran ios.run(). The user may have invoked ios.run() on multiple
|
|
|
|
// threads, but we consider this one thread a main thread for now...
|
|
|
|
ircd::thread_id = std::this_thread::get_id();
|
|
|
|
|
2017-09-26 03:43:33 +02:00
|
|
|
// When this function is entered IRCd will transition to START indicating
|
|
|
|
// that subsystems are initializing.
|
|
|
|
ircd::set_runlevel(runlevel::START);
|
2016-09-06 22:11:55 +02:00
|
|
|
|
2017-09-26 03:43:33 +02:00
|
|
|
// When this function completes, subsystems are done shutting down and IRCd
|
2017-11-06 21:14:50 +01:00
|
|
|
// transitions to HALT.
|
|
|
|
const unwind halted{[]
|
2017-09-30 08:04:41 +02:00
|
|
|
{
|
|
|
|
at_main_exit();
|
2017-11-06 21:14:50 +01:00
|
|
|
set_runlevel(runlevel::HALT);
|
2017-09-30 08:04:41 +02:00
|
|
|
}};
|
2017-09-26 03:43:33 +02:00
|
|
|
|
|
|
|
// These objects are the init()'s and fini()'s for each subsystem.
|
|
|
|
// Appearing here ties their life to the main context. Initialization can
|
|
|
|
// also occur in ircd::init() or static initialization itself if either are
|
|
|
|
// more appropriate.
|
2017-08-23 23:13:57 +02:00
|
|
|
|
2017-10-12 02:50:13 +02:00
|
|
|
fs::init _fs_; // Local filesystem
|
2017-08-23 23:13:57 +02:00
|
|
|
ctx::ole::init _ole_; // Thread OffLoad Engine
|
2017-10-01 04:14:45 +02:00
|
|
|
nacl::init _nacl_; // nacl crypto
|
2017-10-25 18:35:23 +02:00
|
|
|
openssl::init _ossl_; // openssl crypto
|
2017-09-30 08:04:41 +02:00
|
|
|
net::init _net_; // Networking
|
2017-09-21 04:31:54 +02:00
|
|
|
client::init _client_; // Client related
|
2017-11-25 23:26:38 +01:00
|
|
|
server::init _server_; // Server related
|
2017-09-08 11:19:26 +02:00
|
|
|
db::init _db_; // RocksDB
|
2017-08-23 23:13:57 +02:00
|
|
|
js::init _js_; // SpiderMonkey
|
2017-09-08 11:32:49 +02:00
|
|
|
m::init _matrix_; // Matrix
|
2016-10-26 08:25:04 +02:00
|
|
|
|
2017-11-01 23:51:24 +01:00
|
|
|
// Any deinits which have to be done with all subsystems intact
|
|
|
|
const unwind shutdown{[&]
|
|
|
|
{
|
2017-11-30 19:27:02 +01:00
|
|
|
_server_.interrupt();
|
2017-11-01 23:51:24 +01:00
|
|
|
_client_.interrupt();
|
|
|
|
}};
|
|
|
|
|
2017-09-26 03:43:33 +02:00
|
|
|
// When the call to wait() below completes, IRCd exits from the RUN state
|
|
|
|
// and enters one of the two states below depending on whether the unwind
|
|
|
|
// is taking place normally or because of an exception.
|
2017-11-06 21:14:50 +01:00
|
|
|
const unwind::nominal nominal
|
|
|
|
{
|
|
|
|
std::bind(&ircd::set_runlevel, runlevel::QUIT)
|
|
|
|
};
|
|
|
|
|
|
|
|
const unwind::exceptional exceptional
|
|
|
|
{
|
|
|
|
std::bind(&ircd::set_runlevel, runlevel::FAULT)
|
|
|
|
};
|
|
|
|
|
|
|
|
// IRCd will now transition to the RUN state indicating full functionality.
|
|
|
|
ircd::set_runlevel(runlevel::RUN);
|
2017-09-26 03:43:33 +02:00
|
|
|
|
|
|
|
// This call blocks until the main context is notified or interrupted etc.
|
|
|
|
// Waiting here will hold open this stack with all of the above objects
|
|
|
|
// living on it. Once this call completes this function effectively
|
|
|
|
// executes backwards from this point and shuts down IRCd.
|
|
|
|
ctx::wait();
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
2018-01-22 09:25:08 +01:00
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"IRCd main interrupted..."
|
|
|
|
};
|
2016-09-06 22:11:55 +02:00
|
|
|
}
|
2017-11-26 01:20:42 +01:00
|
|
|
#ifndef RB_DEBUG
|
2016-09-06 22:11:55 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-11-26 01:20:42 +01:00
|
|
|
// When not in debug mode this is a clean return to not crash through
|
|
|
|
// the embedder's ios.run() which would terminate the rest of their
|
|
|
|
// program. Instead they have the right to handle the error and try again.
|
2018-01-22 09:25:08 +01:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"IRCd main exited: %s", e.what()
|
|
|
|
};
|
2016-09-06 22:11:55 +02:00
|
|
|
}
|
2017-11-26 01:20:42 +01:00
|
|
|
#else
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
// In debug mode we terminate with a message and a coredump
|
|
|
|
ircd::terminate();
|
|
|
|
}
|
|
|
|
#endif // RB_DEBUG
|
2016-09-06 22:11:55 +02:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
void
|
|
|
|
ircd::at_main_exit()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
strand->post([]
|
|
|
|
{
|
|
|
|
delete strand;
|
|
|
|
strand = nullptr;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-06 21:14:50 +01:00
|
|
|
/// Sets the runlevel of IRCd and notifies users. This should never be called
|
|
|
|
/// manually/directly, as it doesn't trigger a runlevel change itself, it just
|
|
|
|
/// notifies of one.
|
|
|
|
///
|
2017-12-29 23:53:39 +01:00
|
|
|
/// The notification will be posted to the io_context. This is important to
|
2017-11-06 21:14:50 +01:00
|
|
|
/// prevent the callback from continuing execution on some ircd::ctx stack and
|
2017-12-29 23:53:39 +01:00
|
|
|
/// instead invoke their function on the main stack in their own io_context
|
2017-11-06 21:14:50 +01:00
|
|
|
/// event slice.
|
2016-09-09 19:48:33 +02:00
|
|
|
void
|
2017-09-26 03:43:33 +02:00
|
|
|
ircd::set_runlevel(const enum runlevel &new_runlevel)
|
|
|
|
try
|
2016-09-09 19:48:33 +02:00
|
|
|
{
|
2018-01-22 09:25:08 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"IRCd runlevel transition from '%s' to '%s'%s",
|
|
|
|
reflect(ircd::runlevel),
|
|
|
|
reflect(new_runlevel),
|
|
|
|
ircd::runlevel_changed? " (notifying user)" : ""
|
|
|
|
};
|
2017-09-26 03:43:33 +02:00
|
|
|
|
|
|
|
ircd::_runlevel = new_runlevel;
|
|
|
|
|
|
|
|
// This function will notify the user of the change to IRCd. The
|
2017-12-29 23:53:39 +01:00
|
|
|
// notification is posted to the io_context ensuring THERE IS NO
|
2017-09-26 03:43:33 +02:00
|
|
|
// CONTINUATION ON THIS STACK by the user.
|
|
|
|
if(ircd::runlevel_changed)
|
|
|
|
ios->post([new_runlevel]
|
|
|
|
{
|
2017-11-06 21:14:50 +01:00
|
|
|
if(new_runlevel == runlevel::HALT)
|
2018-01-22 09:25:08 +01:00
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
"IRCd %s", reflect(new_runlevel)
|
|
|
|
};
|
2017-11-01 23:51:24 +01:00
|
|
|
|
2017-09-26 03:43:33 +02:00
|
|
|
ircd::runlevel_changed(new_runlevel);
|
|
|
|
});
|
|
|
|
|
2017-11-06 21:14:50 +01:00
|
|
|
if(new_runlevel != runlevel::HALT)
|
2018-01-22 09:25:08 +01:00
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
"IRCd %s", reflect(new_runlevel)
|
|
|
|
};
|
2016-09-09 19:48:33 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-01-22 09:25:08 +01:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"IRCd runlevel change to '%s': %s", reflect(new_runlevel), e.what()
|
|
|
|
};
|
2017-09-26 03:43:33 +02:00
|
|
|
|
2017-11-26 01:20:42 +01:00
|
|
|
ircd::terminate();
|
2016-09-09 19:48:33 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 03:43:33 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::reflect(const enum runlevel &level)
|
2016-09-09 19:48:33 +02:00
|
|
|
{
|
2017-09-26 03:43:33 +02:00
|
|
|
switch(level)
|
|
|
|
{
|
2017-11-06 21:14:50 +01:00
|
|
|
case runlevel::HALT: return "HALT";
|
2017-09-26 03:43:33 +02:00
|
|
|
case runlevel::READY: return "READY";
|
|
|
|
case runlevel::START: return "START";
|
|
|
|
case runlevel::RUN: return "RUN";
|
2017-11-06 21:14:50 +01:00
|
|
|
case runlevel::QUIT: return "QUIT";
|
|
|
|
case runlevel::FAULT: return "FAULT";
|
2017-09-26 03:43:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "??????";
|
2016-09-09 19:48:33 +02:00
|
|
|
}
|
|
|
|
|
2017-11-30 19:26:13 +01:00
|
|
|
std::string
|
|
|
|
ircd::read_conf(std::string filename)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(!filename.empty())
|
2018-01-22 09:25:08 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"User supplied a configuration file path: `%s'", filename
|
|
|
|
};
|
2017-11-30 19:26:13 +01:00
|
|
|
|
|
|
|
if(filename.empty())
|
|
|
|
filename = fs::CPATH;
|
|
|
|
|
|
|
|
if(!fs::exists(filename))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::string read
|
|
|
|
{
|
|
|
|
fs::read(filename)
|
|
|
|
};
|
|
|
|
|
|
|
|
// ensure any trailing cruft is removed to not set off the validator
|
|
|
|
if(endswith(read, '\n'))
|
|
|
|
read.pop_back();
|
|
|
|
|
|
|
|
if(endswith(read, '\r'))
|
|
|
|
read.pop_back();
|
|
|
|
|
|
|
|
// grammar check; throws on error
|
|
|
|
json::valid(read);
|
|
|
|
|
|
|
|
const json::object object{read};
|
|
|
|
const size_t key_count{object.count()};
|
2018-01-22 09:25:08 +01:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
"Using configuration from: `%s': JSON object with %zu members in %zu bytes",
|
|
|
|
filename,
|
|
|
|
key_count,
|
|
|
|
read.size()
|
|
|
|
};
|
2017-11-30 19:26:13 +01:00
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-01-22 09:25:08 +01:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Configuration @ `%s': %s", filename, e.what()
|
|
|
|
};
|
|
|
|
|
2017-11-30 19:26:13 +01:00
|
|
|
throw;
|
|
|
|
}
|