2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-14 03:03:04 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-14 03:03:04 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-09-06 22:11:55 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
2018-12-01 01:46:34 +01:00
|
|
|
std::string _origin; // user's supplied param
|
2018-08-17 19:55:22 +02:00
|
|
|
std::string _hostname; // user's supplied param
|
2018-06-15 23:22:36 +02:00
|
|
|
ctx::ctx *main_context; // Main program loop
|
|
|
|
|
2018-03-16 23:16:14 +01:00
|
|
|
void main() noexcept;
|
2016-09-06 22:11:55 +02:00
|
|
|
}
|
|
|
|
|
2018-12-09 00:17:13 +01:00
|
|
|
decltype(ircd::debugmode)
|
|
|
|
ircd::debugmode
|
|
|
|
{
|
|
|
|
{ "name", "ircd.debugmode" },
|
|
|
|
{ "default", false },
|
2018-12-09 01:15:22 +01:00
|
|
|
{ "persist", false },
|
2018-12-09 00:17:13 +01:00
|
|
|
};
|
|
|
|
|
2017-09-26 03:43:33 +02:00
|
|
|
/// 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
|
2018-10-17 14:12:10 +02:00
|
|
|
ircd::init(boost::asio::io_context &user_ios,
|
2018-12-01 01:46:34 +01:00
|
|
|
const string_view &origin,
|
2018-08-13 23:36:12 +02:00
|
|
|
const string_view &hostname)
|
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-10-17 14:12:10 +02:00
|
|
|
ios::init(user_ios);
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2018-12-01 01:46:34 +01:00
|
|
|
// Save the params used for m::init later.
|
|
|
|
_origin = std::string{origin};
|
2018-08-17 19:55:22 +02:00
|
|
|
_hostname = std::string{hostname};
|
|
|
|
|
2018-03-02 09:35:02 +01:00
|
|
|
// The log is available. but it is console-only until conf opens files.
|
2016-09-06 22:11:55 +02:00
|
|
|
log::init();
|
2018-05-08 01:13:16 +02:00
|
|
|
log::mark("DEADSTART"); // 6600
|
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();
|
2018-04-20 21:41:03 +02:00
|
|
|
info::dump();
|
2017-11-30 19:26:13 +01:00
|
|
|
|
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...
|
2018-12-09 00:15:31 +01:00
|
|
|
ircd::runlevel_set(runlevel::READY);
|
2016-09-06 22:11:55 +02:00
|
|
|
}
|
2017-09-30 08:04:41 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2018-08-28 23:04:43 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"IRCd quit requested from runlevel:%s ctx:%p main_context:%p",
|
|
|
|
reflect(runlevel),
|
|
|
|
(const void *)ctx::current,
|
|
|
|
(const void *)main_context
|
|
|
|
};
|
2017-09-26 03:43:33 +02:00
|
|
|
|
2018-08-28 23:04:43 +02:00
|
|
|
if(main_context) switch(runlevel)
|
|
|
|
{
|
|
|
|
case runlevel::READY:
|
|
|
|
{
|
|
|
|
ctx::terminate(*main_context);
|
|
|
|
main_context = nullptr;
|
2018-12-09 00:15:31 +01:00
|
|
|
ircd::runlevel_set(runlevel::HALT);
|
2018-08-28 23:04:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-10-26 08:25:04 +02:00
|
|
|
|
2018-08-28 23:04:43 +02:00
|
|
|
case runlevel::START:
|
|
|
|
{
|
|
|
|
ctx::terminate(*main_context);
|
|
|
|
main_context = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case runlevel::RUN:
|
|
|
|
{
|
|
|
|
ctx::notify(*main_context);
|
|
|
|
main_context = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case runlevel::HALT:
|
|
|
|
case runlevel::QUIT:
|
|
|
|
case runlevel::FAULT:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-10-26 08:25:04 +02:00
|
|
|
}
|
|
|
|
|
2018-02-22 10:33:25 +01:00
|
|
|
/// Main context; Main program. 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()
|
2018-03-16 23:16:14 +01:00
|
|
|
noexcept 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...
|
2018-10-17 14:12:10 +02:00
|
|
|
ircd::ios::main_thread_id = std::this_thread::get_id();
|
2018-01-24 02:56:05 +01: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
|
|
|
{
|
2018-12-09 00:15:31 +01:00
|
|
|
runlevel_set(runlevel::HALT);
|
2017-09-30 08:04:41 +02:00
|
|
|
}};
|
2017-09-26 03:43:33 +02:00
|
|
|
|
2018-08-28 23:04:43 +02:00
|
|
|
// When this function is entered IRCd will transition to START indicating
|
|
|
|
// that subsystems are initializing.
|
2018-12-09 00:15:31 +01:00
|
|
|
ircd::runlevel_set(runlevel::START);
|
2018-08-28 23:04:43 +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
|
2018-03-17 07:13:37 +01:00
|
|
|
magic::init _magic_; // libmagic
|
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-08 11:19:26 +02:00
|
|
|
db::init _db_; // RocksDB
|
2018-02-14 05:38:55 +01:00
|
|
|
server::init _server_; // Server related
|
|
|
|
client::init _client_; // Client related
|
2017-08-23 23:13:57 +02:00
|
|
|
js::init _js_; // SpiderMonkey
|
2018-12-05 00:41:29 +01:00
|
|
|
ap::init _ap_; // ActivityPub
|
2018-08-17 19:55:22 +02:00
|
|
|
m::init _matrix_ // Matrix
|
|
|
|
{
|
2018-12-01 01:46:34 +01:00
|
|
|
string_view{_origin},
|
|
|
|
string_view{_hostname}
|
2018-08-17 19:55:22 +02:00
|
|
|
};
|
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{[&]
|
|
|
|
{
|
2018-05-15 02:24:34 +02:00
|
|
|
_matrix_.close();
|
2018-09-18 01:47:36 +02:00
|
|
|
server::interrupt_all();
|
2018-09-25 09:21:07 +02:00
|
|
|
client::terminate_all();
|
2018-09-18 01:47:36 +02:00
|
|
|
client::close_all();
|
2018-09-21 02:54:18 +02:00
|
|
|
server::close_all();
|
2018-09-18 01:47:36 +02:00
|
|
|
server::wait_all();
|
|
|
|
client::wait_all();
|
2017-11-01 23:51:24 +01:00
|
|
|
}};
|
|
|
|
|
2018-08-17 20:07:31 +02:00
|
|
|
// When the call to wait() below completes, IRCd exits from the RUN state.
|
|
|
|
const unwind nominal
|
2017-11-06 21:14:50 +01:00
|
|
|
{
|
2018-12-09 00:15:31 +01:00
|
|
|
std::bind(&ircd::runlevel_set, runlevel::QUIT)
|
2017-11-06 21:14:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// IRCd will now transition to the RUN state indicating full functionality.
|
2018-12-09 00:15:31 +01:00
|
|
|
ircd::runlevel_set(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();
|
|
|
|
}
|
2018-09-21 23:59:58 +02:00
|
|
|
catch(const m::error &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"IRCd main exited :%s %s", e.what(), e.content
|
|
|
|
};
|
|
|
|
}
|
2018-05-07 00:11:59 +02:00
|
|
|
catch(...)
|
2016-09-06 22:11:55 +02:00
|
|
|
{
|
2018-01-22 09:25:08 +01:00
|
|
|
log::critical
|
|
|
|
{
|
2018-09-21 23:59:58 +02:00
|
|
|
"IRCd main exited :%s", what(std::current_exception())
|
2018-01-22 09:25:08 +01:00
|
|
|
};
|
2016-09-06 22:11:55 +02:00
|
|
|
}
|
|
|
|
|
2018-04-15 01:19:15 +02:00
|
|
|
/// IRCd uptime in seconds
|
|
|
|
ircd::seconds
|
|
|
|
ircd::uptime()
|
|
|
|
{
|
|
|
|
return seconds(ircd::time() - info::startup_time);
|
|
|
|
}
|