2007-01-25 07:40:21 +01:00
|
|
|
/*
|
2008-07-28 21:02:54 +02:00
|
|
|
* charybdis: A slightly useful ircd.
|
2007-01-25 07:40:21 +01:00
|
|
|
* ircd.c: Starts up and runs the ircd.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
|
|
|
|
* Copyright (C) 1996-2002 Hybrid Development Team
|
2008-07-28 21:02:54 +02:00
|
|
|
* Copyright (C) 2002-2008 ircd-ratbox development team
|
2013-07-10 05:04:45 +02:00
|
|
|
* Copyright (C) 2005-2013 charybdis development team
|
2007-01-25 07:40:21 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA
|
|
|
|
*/
|
|
|
|
|
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-09-19 08:10:38 +02:00
|
|
|
extern const uint boost_version[3];
|
2017-10-19 10:05:41 +02:00
|
|
|
struct tc_version extern const tc_version;
|
2017-09-19 08:10:38 +02:00
|
|
|
|
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
|
|
|
/// Provides tcmalloc version information if tcmalloc is linked in to IRCd.
|
2017-10-19 10:05:41 +02:00
|
|
|
struct ircd::tc_version
|
|
|
|
{
|
2017-11-06 21:14:50 +01:00
|
|
|
int major{0}, minor{0};
|
|
|
|
char patch[64] {0};
|
|
|
|
std::string version {"unavailable"};
|
|
|
|
}
|
|
|
|
const ircd::tc_version;
|
|
|
|
|
|
|
|
/*
|
|
|
|
const char* tc_version(int* major, int* minor, const char** patch);
|
|
|
|
ircd::tc_version::tc_version()
|
|
|
|
:version{::tc_version(&major, &minor, reinterpret_cast<const char **>(&patch))}
|
|
|
|
{}
|
|
|
|
*/
|
2017-10-19 10:05:41 +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
|
|
|
|
{};
|
|
|
|
|
2017-11-06 21:14:50 +01:00
|
|
|
/// Boost version indicator for compiled header files.
|
|
|
|
const uint
|
|
|
|
ircd::boost_version[3]
|
|
|
|
{
|
|
|
|
BOOST_VERSION / 100000,
|
|
|
|
BOOST_VERSION / 100 % 1000,
|
|
|
|
BOOST_VERSION % 100,
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
throw error("Cannot init() IRCd from runlevel %s",
|
|
|
|
reflect(runlevel));
|
|
|
|
|
|
|
|
// Samples 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...
|
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
|
|
|
|
2017-11-30 19:26:13 +01:00
|
|
|
// This message flashes information about our dependencies which are being
|
|
|
|
// assumed for this execution.
|
|
|
|
log::info("%s. boost %u.%u.%u. rocksdb %s. sodium %s. %s.",
|
|
|
|
PACKAGE_STRING,
|
|
|
|
boost_version[0],
|
|
|
|
boost_version[1],
|
|
|
|
boost_version[2],
|
|
|
|
db::version,
|
|
|
|
nacl::version(),
|
|
|
|
openssl::version());
|
|
|
|
|
|
|
|
// This message flashes information about IRCd itself for this execution
|
|
|
|
log::info("%s %ld %s. configured: %s. compiled: %s %s",
|
|
|
|
BRANDING_VERSION,
|
|
|
|
__cplusplus,
|
|
|
|
__VERSION__,
|
|
|
|
RB_DATE_CONFIGURED,
|
|
|
|
__TIMESTAMP__,
|
|
|
|
RB_DEBUG_LEVEL? "(DEBUG MODE)" : "");
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
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
|
|
|
{
|
2017-09-26 03:43:33 +02:00
|
|
|
log::debug("IRCd runlevel transition from '%s' to '%s'%s",
|
|
|
|
reflect(ircd::runlevel),
|
|
|
|
reflect(new_runlevel),
|
|
|
|
ircd::runlevel_changed? " (notifying user)" : "");
|
|
|
|
|
|
|
|
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)
|
2017-11-01 23:51:24 +01:00
|
|
|
log::notice("IRCd %s", reflect(new_runlevel));
|
|
|
|
|
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)
|
2017-11-01 23:51:24 +01:00
|
|
|
log::notice("IRCd %s", reflect(new_runlevel));
|
2016-09-09 19:48:33 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-09-26 03:43:33 +02:00
|
|
|
log::critical("IRCd runlevel change to '%s': %s",
|
|
|
|
reflect(new_runlevel),
|
|
|
|
e.what());
|
|
|
|
|
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())
|
|
|
|
log::debug("User supplied a configuration file path: `%s'", filename);
|
|
|
|
|
|
|
|
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()};
|
|
|
|
log::info("Using configuration from: `%s': JSON object with %zu members in %zu bytes",
|
|
|
|
filename,
|
|
|
|
key_count,
|
|
|
|
read.size());
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error("Configuration @ `%s': %s",
|
|
|
|
filename,
|
|
|
|
e.what());
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2016-09-11 08:05:38 +02:00
|
|
|
// namespace ircd {
|
2016-07-13 07:17:21 +02:00
|
|
|
|
2008-04-13 17:54:23 +02:00
|
|
|
/* /quote set variables */
|
2016-09-11 08:05:38 +02:00
|
|
|
/*
|
2008-04-05 20:02:59 +02:00
|
|
|
struct SetOptions GlobalSetOptions;
|
2008-04-13 17:54:23 +02:00
|
|
|
|
|
|
|
struct Counter Count;
|
2008-04-04 17:54:37 +02:00
|
|
|
struct ServerStatistics ServerStats;
|
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
struct ev_entry *check_splitmode_ev;
|
|
|
|
|
2008-04-08 13:50:39 +02:00
|
|
|
int maxconnections;
|
2016-09-10 21:57:33 +02:00
|
|
|
//struct LocalUser meLocalUser; // That's also part of me
|
2008-04-08 13:50:39 +02:00
|
|
|
|
2008-06-28 12:36:25 +02:00
|
|
|
rb_dlink_list global_client_list;
|
2016-09-11 08:05:38 +02:00
|
|
|
*/
|
2008-04-13 17:54:23 +02:00
|
|
|
|
2008-06-28 12:36:25 +02:00
|
|
|
/* unknown/client pointer lists */
|
2016-10-26 11:39:28 +02:00
|
|
|
//rb_dlink_list unknown_list; /* unknown clients ON this server only */
|
|
|
|
//rb_dlink_list lclient_list; /* local clients only ON this server */
|
|
|
|
//rb_dlink_list serv_list; /* local servers to this server ONLY */
|
|
|
|
//rb_dlink_list global_serv_list; /* global servers on the network */
|
|
|
|
//rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */
|
|
|
|
//rb_dlink_list oper_list; /* network opers */
|
2008-04-08 14:01:16 +02:00
|
|
|
|
2016-09-11 08:05:38 +02:00
|
|
|
/*
|
2016-04-26 00:21:38 +02:00
|
|
|
char * const *myargv;
|
2016-03-28 02:05:38 +02:00
|
|
|
volatile sig_atomic_t dorehash = false;
|
|
|
|
volatile sig_atomic_t dorehashbans = false;
|
|
|
|
volatile sig_atomic_t doremotd = false;
|
2016-03-09 09:00:40 +01:00
|
|
|
bool kline_queued = false;
|
2016-08-26 06:37:06 +02:00
|
|
|
bool server_state_foreground = true;
|
2016-03-09 09:19:31 +01:00
|
|
|
bool opers_see_all_users = false;
|
2016-03-19 06:57:32 +01:00
|
|
|
bool ircd_ssl_ok = false;
|
|
|
|
bool ircd_zlib_ok = true;
|
2008-04-13 17:54:23 +02:00
|
|
|
|
|
|
|
int testing_conf = 0;
|
2016-08-16 01:21:01 +02:00
|
|
|
//time_t startup_time;
|
2008-04-13 17:54:23 +02:00
|
|
|
|
2012-02-04 08:47:46 +01:00
|
|
|
int default_server_capabs;
|
2008-04-13 17:54:23 +02:00
|
|
|
|
2008-07-27 14:25:00 +02:00
|
|
|
int splitmode;
|
|
|
|
int splitchecking;
|
|
|
|
int split_users;
|
|
|
|
int split_servers;
|
|
|
|
int eob_count;
|
2016-09-11 08:05:38 +02:00
|
|
|
*/
|
2008-04-06 16:52:42 +02:00
|
|
|
|
2016-09-11 08:05:38 +02:00
|
|
|
// }
|
2016-09-06 22:11:55 +02:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
/*
|
2007-01-25 07:40:21 +01:00
|
|
|
static void
|
|
|
|
check_rehash(void *unused)
|
|
|
|
{
|
|
|
|
if(dorehash)
|
|
|
|
{
|
2016-03-23 14:52:32 +01:00
|
|
|
rehash(true);
|
2016-03-09 09:06:21 +01:00
|
|
|
dorehash = false;
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(dorehashbans)
|
|
|
|
{
|
2016-03-23 14:52:32 +01:00
|
|
|
rehash_bans();
|
2016-03-09 09:06:21 +01:00
|
|
|
dorehashbans = false;
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(doremotd)
|
|
|
|
{
|
2016-08-26 13:50:12 +02:00
|
|
|
sendto_realops_snomask(sno::GENERAL, L_ALL,
|
2007-01-25 07:40:21 +01:00
|
|
|
"Got signal SIGUSR1, reloading ircd motd file");
|
2016-08-14 01:42:12 +02:00
|
|
|
cache::motd::cache_user();
|
2016-03-09 09:06:21 +01:00
|
|
|
doremotd = false;
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
*/
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initalialize_global_set_options
|
|
|
|
*
|
|
|
|
* inputs - none
|
|
|
|
* output - none
|
2014-03-03 05:25:47 +01:00
|
|
|
* side effects - This sets all global set options needed
|
2007-01-25 07:40:21 +01:00
|
|
|
*/
|
2016-10-26 11:39:28 +02:00
|
|
|
/*
|
2007-01-25 07:40:21 +01:00
|
|
|
static void
|
|
|
|
initialize_global_set_options(void)
|
|
|
|
{
|
|
|
|
memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
|
2016-09-01 17:50:48 +02:00
|
|
|
// memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry));
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2008-04-13 17:54:23 +02:00
|
|
|
GlobalSetOptions.maxclients = ServerInfo.default_max_clients;
|
|
|
|
|
|
|
|
if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0))
|
2008-04-05 21:56:15 +02:00
|
|
|
GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER;
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
GlobalSetOptions.autoconn = 1;
|
|
|
|
|
|
|
|
GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
|
|
|
|
GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
|
|
|
|
|
2013-10-29 10:07:19 +01:00
|
|
|
GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
split_servers = ConfigChannel.default_split_server_count;
|
|
|
|
split_users = ConfigChannel.default_split_user_count;
|
|
|
|
|
|
|
|
if(split_users && split_servers
|
|
|
|
&& (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
|
|
|
|
{
|
|
|
|
splitmode = 1;
|
|
|
|
splitchecking = 1;
|
|
|
|
}
|
|
|
|
|
2013-10-29 10:07:19 +01:00
|
|
|
GlobalSetOptions.ident_timeout = ConfigFileEntry.default_ident_timeout;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2008-04-20 06:40:40 +02:00
|
|
|
rb_strlcpy(GlobalSetOptions.operstring,
|
2007-01-25 07:40:21 +01:00
|
|
|
ConfigFileEntry.default_operstring,
|
|
|
|
sizeof(GlobalSetOptions.operstring));
|
2008-04-20 06:40:40 +02:00
|
|
|
rb_strlcpy(GlobalSetOptions.adminstring,
|
2007-01-25 07:40:21 +01:00
|
|
|
ConfigFileEntry.default_adminstring,
|
|
|
|
sizeof(GlobalSetOptions.adminstring));
|
|
|
|
}
|
2016-10-26 11:39:28 +02:00
|
|
|
*/
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
// initialise operhash fairly early.
|
|
|
|
//init_operhash();
|
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
//me.localClient = &meLocalUser;
|
2016-08-31 12:55:27 +02:00
|
|
|
//rb_dlinkAddTail(&me, &me.node, &global_client_list);
|
|
|
|
|
|
|
|
//init_builtin_capabs();
|
|
|
|
//default_server_capabs = CAP_MASK;
|
|
|
|
//newconf_init();
|
|
|
|
//init_s_conf();
|
|
|
|
//init_s_newconf();
|
|
|
|
//init_hash();
|
|
|
|
//init_host_hash();
|
|
|
|
//client::init();
|
|
|
|
//init_hook();
|
|
|
|
//chan::init();
|
|
|
|
//initclass();
|
|
|
|
//whowas::init();
|
|
|
|
//init_reject();
|
|
|
|
//cache::init();
|
|
|
|
//init_monitor();
|
|
|
|
//chan::mode::init();
|
|
|
|
//init_authd(); /* Start up authd. */
|
|
|
|
//init_dns(); /* Start up DNS query system */
|
|
|
|
|
|
|
|
//privilegeset_set_new("default", "", 0);
|
|
|
|
|
|
|
|
//read_conf_files(true); /* cold start init conf files */
|
|
|
|
|
|
|
|
//mods::autoload();
|
|
|
|
//supported::init();
|
|
|
|
//init_bandb();
|
|
|
|
//init_ssld();
|
|
|
|
//init_wsockd();
|
|
|
|
//rehash_bans();
|
|
|
|
|
|
|
|
//default_server_capabs &= ~CAP_ZIP; // Set up default_server_capabs
|
|
|
|
//initialize_global_set_options();
|
2007-01-25 07:40:21 +01:00
|
|
|
/*
|
2016-08-31 12:55:27 +02:00
|
|
|
if(ServerInfo.name == NULL)
|
|
|
|
throw error("no server name specified in serverinfo block.");
|
|
|
|
|
|
|
|
rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name));
|
|
|
|
|
|
|
|
if(ServerInfo.sid[0] == '\0')
|
|
|
|
throw error("no server sid specified in serverinfo block.");
|
|
|
|
|
|
|
|
rb_strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
|
|
|
|
//client::init_uid();
|
|
|
|
|
|
|
|
// serverinfo{} description must exist. If not, error out.
|
|
|
|
if(ServerInfo.description == NULL)
|
|
|
|
throw error("no server description specified in serverinfo block.");
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info));
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
if(ServerInfo.ssl_cert != NULL)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-08-31 12:55:27 +02:00
|
|
|
// just do the rb_setup_ssl_server to validate the config
|
|
|
|
if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list))
|
|
|
|
{
|
|
|
|
ilog(L_MAIN, "WARNING: Unable to setup SSL.");
|
|
|
|
ircd_ssl_ok = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ircd_ssl_ok = true;
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
2016-08-31 12:55:27 +02:00
|
|
|
*/
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
//me.from = &me;
|
|
|
|
//me.servptr = &me;
|
|
|
|
//set_me(me);
|
|
|
|
//make_serv(me);
|
|
|
|
//add_to_client_hash(me.name, &me);
|
|
|
|
//add_to_id_hash(me.id, &me);
|
|
|
|
//nameinfo(serv(me)) = cache::serv::connect(me.name, me.info);
|
2008-07-28 21:02:54 +02:00
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
//rb_dlinkAddAlloc(&me, &global_serv_list);
|
|
|
|
|
|
|
|
//check_class();
|
|
|
|
//cache::help::load();
|
|
|
|
|
|
|
|
//configure_authd();
|
|
|
|
|
|
|
|
/* We want try_connections to be called as soon as possible now! -- adrian */
|
|
|
|
/* No, 'cause after a restart it would cause all sorts of nick collides */
|
|
|
|
/* um. by waiting even longer, that just means we have even *more*
|
|
|
|
* nick collisions. what a stupid idea. set an event for the IO loop --fl
|
|
|
|
*/
|
|
|
|
// rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
|
|
|
|
// rb_event_addonce("try_connections_startup", try_connections, NULL, 2);
|
|
|
|
// rb_event_add("check_rehash", check_rehash, NULL, 3);
|
|
|
|
// rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */
|
|
|
|
|
|
|
|
// if(splitmode)
|
|
|
|
// check_splitmode_ev = rb_event_add("check_splitmode", chan::check_splitmode, NULL, 5);
|