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.
|
2012-02-18 04:54:44 +01:00
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
#include <ircd/ircd.h>
|
2018-01-10 09:56:33 +01:00
|
|
|
#include <RB_INC_SYS_RESOURCE_H
|
2018-11-03 12:05:12 +01:00
|
|
|
#include <ircd/asio.h>
|
2016-09-22 00:07:14 +02:00
|
|
|
#include "lgetopt.h"
|
2018-02-22 22:33:53 +01:00
|
|
|
#include "construct.h"
|
2016-07-01 05:04:00 +02:00
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
static bool startup_checks();
|
2018-12-09 00:17:13 +01:00
|
|
|
static void applyargs();
|
2018-01-10 09:56:33 +01:00
|
|
|
static void enable_coredumps();
|
2018-08-13 23:36:12 +02:00
|
|
|
static int print_version();
|
2016-09-07 02:16:44 +02:00
|
|
|
|
|
|
|
const char *const fatalerrstr
|
|
|
|
{R"(
|
|
|
|
***
|
|
|
|
*** A fatal error has occurred. Please contact the developer with the message below.
|
|
|
|
*** Create a coredump by reproducing the error using the -debug command-line option.
|
|
|
|
***
|
2018-08-13 23:35:16 +02:00
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
%s
|
|
|
|
)"};
|
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
const char *const usererrstr
|
|
|
|
{R"(
|
|
|
|
***
|
|
|
|
*** A fatal startup error has occurred. Please fix the problem to continue. ***
|
|
|
|
***
|
2018-08-13 23:35:16 +02:00
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
%s
|
|
|
|
)"};
|
|
|
|
|
2016-08-31 10:03:12 +02:00
|
|
|
bool printversion;
|
2016-09-03 18:16:04 +02:00
|
|
|
bool cmdline;
|
2018-12-06 04:00:43 +01:00
|
|
|
bool quietmode;
|
2018-12-09 00:17:13 +01:00
|
|
|
bool debugmode;
|
|
|
|
bool nolisten;
|
|
|
|
bool noautomod;
|
|
|
|
bool checkdb;
|
|
|
|
bool pitrecdb;
|
|
|
|
bool nojs;
|
|
|
|
bool nodirect;
|
|
|
|
bool noaio;
|
2016-09-01 17:50:48 +02:00
|
|
|
const char *configfile;
|
2017-11-16 02:33:43 +01:00
|
|
|
const char *execute;
|
2016-08-31 10:03:12 +02:00
|
|
|
lgetopt opts[] =
|
|
|
|
{
|
2018-12-09 00:17:13 +01:00
|
|
|
{ "help", nullptr, lgetopt::USAGE, "Print this text" },
|
|
|
|
{ "version", &printversion, lgetopt::BOOL, "Print version and exit" },
|
|
|
|
{ "configfile", &configfile, lgetopt::STRING, "File to use for ircd.conf" },
|
|
|
|
{ "debug", &debugmode, lgetopt::BOOL, "Enable options for debugging" },
|
|
|
|
{ "quiet", &quietmode, lgetopt::BOOL, "Suppress log messages at the terminal." },
|
|
|
|
{ "console", &cmdline, lgetopt::BOOL, "Drop to a command line immediately after startup" },
|
|
|
|
{ "execute", &execute, lgetopt::STRING, "Execute command lines immediately after startup" },
|
|
|
|
{ "nolisten", &nolisten, lgetopt::BOOL, "Normal execution but without listening sockets" },
|
|
|
|
{ "noautomod", &noautomod, lgetopt::BOOL, "Normal execution but without autoloading modules" },
|
|
|
|
{ "checkdb", &checkdb, lgetopt::BOOL, "Perform complete checks of databases when opening" },
|
|
|
|
{ "pitrecdb", &pitrecdb, lgetopt::BOOL, "Allow Point-In-Time-Recover if DB reports corruption after crash" },
|
|
|
|
{ "nojs", &nojs, lgetopt::BOOL, "Disable SpiderMonkey JS subsystem from initializing. (noop when not available)." },
|
|
|
|
{ "nodirect", &nodirect, lgetopt::BOOL, "Disable direct IO (O_DIRECT) for unsupporting filesystems." },
|
|
|
|
{ "noaio", &noaio, lgetopt::BOOL, "Disable the AIO interface in favor of traditional syscalls. " },
|
|
|
|
{ nullptr, nullptr, lgetopt::STRING, nullptr },
|
2016-08-31 10:03:12 +02:00
|
|
|
};
|
|
|
|
|
2017-12-29 23:53:39 +01:00
|
|
|
std::unique_ptr<boost::asio::io_context> ios
|
2016-09-10 03:35:44 +02:00
|
|
|
{
|
2018-07-02 11:34:28 +02:00
|
|
|
// Having trouble with static dtor in clang so this has tp be dynamic
|
2017-12-29 23:53:39 +01:00
|
|
|
std::make_unique<boost::asio::io_context>()
|
2016-09-10 03:35:44 +02:00
|
|
|
};
|
|
|
|
|
2016-08-31 10:03:12 +02:00
|
|
|
int main(int argc, char *const *argv)
|
|
|
|
try
|
2016-08-13 05:05:54 +02:00
|
|
|
{
|
2016-08-31 10:03:12 +02:00
|
|
|
umask(077); // better safe than sorry --SRB
|
2018-08-13 23:36:12 +02:00
|
|
|
|
|
|
|
// '-' switched arguments come first; this function incs argv and decs argc
|
2016-08-31 10:03:12 +02:00
|
|
|
parseargs(&argc, &argv, opts);
|
2018-12-09 00:17:13 +01:00
|
|
|
applyargs();
|
2016-08-31 10:03:12 +02:00
|
|
|
|
2018-01-10 09:56:33 +01:00
|
|
|
// cores are not dumped without consent of the user to maintain the privacy
|
|
|
|
// of cryptographic key material in memory at the time of the crash.
|
|
|
|
if(RB_DEBUG_LEVEL || ircd::debugmode)
|
|
|
|
enable_coredumps();
|
|
|
|
|
2018-08-13 23:36:12 +02:00
|
|
|
if(!startup_checks())
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2016-08-31 10:03:12 +02:00
|
|
|
if(printversion)
|
2018-08-13 23:36:12 +02:00
|
|
|
return print_version();
|
2016-08-31 10:03:12 +02:00
|
|
|
|
2018-12-06 04:00:43 +01:00
|
|
|
if(quietmode)
|
|
|
|
ircd::log::console_disable();
|
|
|
|
|
2018-12-01 01:46:34 +01:00
|
|
|
// The matrix origin is the first positional argument after any switched
|
|
|
|
// arguments. The matrix origin is the hostpart of MXID's for the server.
|
|
|
|
const ircd::string_view origin
|
|
|
|
{
|
|
|
|
argc > 0?
|
|
|
|
argv[0]:
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
// The hostname is the unique name for this specific server. This is
|
|
|
|
// generally the same as origin; but if origin is example.org with an
|
|
|
|
// SRV record redirecting to matrix.example.org then hostname is
|
|
|
|
// matrix.example.org. In clusters serving a single origin, all
|
|
|
|
// hostnames must be different.
|
2018-08-13 23:36:12 +02:00
|
|
|
const ircd::string_view hostname
|
2017-08-23 23:46:35 +02:00
|
|
|
{
|
2018-12-01 01:46:34 +01:00
|
|
|
argc > 1? // hostname given on command line
|
|
|
|
argv[1]:
|
|
|
|
argc > 0? // hostname matches origin
|
|
|
|
argv[0]:
|
|
|
|
nullptr
|
2017-08-23 23:46:35 +02:00
|
|
|
};
|
|
|
|
|
2018-08-13 23:36:12 +02:00
|
|
|
// at least one hostname argument is required for now.
|
|
|
|
if(!hostname)
|
|
|
|
throw ircd::user_error
|
|
|
|
{
|
2018-12-01 01:46:34 +01:00
|
|
|
"Must specify the origin after any switched parameters."
|
2018-08-13 23:36:12 +02:00
|
|
|
};
|
|
|
|
|
2017-12-29 23:53:39 +01:00
|
|
|
// Associates libircd with our io_context and posts the initial routines
|
|
|
|
// to that io_context. Execution of IRCd will then occur during ios::run()
|
2018-08-13 23:36:12 +02:00
|
|
|
// note: only supports service for one hostname at this time.
|
2018-12-01 01:46:34 +01:00
|
|
|
ircd::init(*ios, origin, hostname);
|
2016-09-08 22:29:02 +02:00
|
|
|
|
2017-08-23 23:46:35 +02:00
|
|
|
// libircd does no signal handling (or at least none that you ever have to
|
|
|
|
// care about); reaction to all signals happens out here instead. Handling
|
2017-12-29 23:53:39 +01:00
|
|
|
// is done properly through the io_context which registers the handler for
|
|
|
|
// the platform and then safely posts the received signal to the io_context
|
2017-08-23 23:46:35 +02:00
|
|
|
// event loop. This means we lose the true instant hardware-interrupt gratitude
|
|
|
|
// of signals but with the benefit of unconditional safety and cross-
|
|
|
|
// platformness with windows etc.
|
2018-12-11 23:55:40 +01:00
|
|
|
const construct::signals signals{*ios};
|
2016-09-01 17:50:48 +02:00
|
|
|
|
2017-08-23 23:46:35 +02:00
|
|
|
// If the user wants to immediately drop to a command line without having to
|
|
|
|
// send a ctrl-c for it, that is provided here.
|
2016-09-03 18:16:04 +02:00
|
|
|
if(cmdline)
|
2018-12-11 23:55:40 +01:00
|
|
|
construct::console::spawn();
|
2016-09-03 18:16:04 +02:00
|
|
|
|
2017-11-16 02:33:43 +01:00
|
|
|
if(execute)
|
2018-12-11 23:55:40 +01:00
|
|
|
construct::console::execute({execute});
|
2017-11-16 02:33:43 +01:00
|
|
|
|
2017-08-23 23:46:35 +02:00
|
|
|
// Execution.
|
2018-02-22 10:33:25 +01:00
|
|
|
// Blocks until a clean exit from a quit() or an exception comes out of it.
|
2017-08-23 23:46:35 +02:00
|
|
|
ios->run();
|
2016-08-13 05:05:54 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
catch(const ircd::user_error &e)
|
2016-09-08 22:29:02 +02:00
|
|
|
{
|
|
|
|
if(ircd::debugmode)
|
|
|
|
throw;
|
|
|
|
|
|
|
|
fprintf(stderr, usererrstr, e.what());
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2016-08-31 10:03:12 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
if(ircd::debugmode)
|
|
|
|
throw;
|
2012-02-18 04:54:44 +01:00
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
/*
|
|
|
|
* Why EXIT_FAILURE here?
|
|
|
|
* Because if ircd_die_cb() is called it's because of a fatal
|
|
|
|
* error inside libcharybdis, and we don't know how to handle the
|
|
|
|
* exception, so it is logical to return a FAILURE exit code here.
|
|
|
|
* --nenolod
|
|
|
|
*
|
|
|
|
* left the comment but the code is gone -jzk
|
|
|
|
*/
|
2016-08-31 10:03:12 +02:00
|
|
|
fprintf(stderr, fatalerrstr, e.what());
|
2016-08-31 12:55:27 +02:00
|
|
|
return EXIT_FAILURE;
|
2016-08-31 10:03:12 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:36:12 +02:00
|
|
|
int
|
2018-05-08 04:21:21 +02:00
|
|
|
print_version()
|
2016-08-31 10:03:12 +02:00
|
|
|
{
|
|
|
|
printf("VERSION :%s\n",
|
2018-02-22 22:33:53 +01:00
|
|
|
RB_VERSION);
|
2016-08-31 10:03:12 +02:00
|
|
|
|
|
|
|
#ifdef CUSTOM_BRANDING
|
|
|
|
printf("VERSION :based on %s-%s\n",
|
|
|
|
PACKAGE_NAME,
|
|
|
|
PACKAGE_VERSION);
|
|
|
|
#endif
|
2018-08-13 23:36:12 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2016-08-31 10:03:12 +02:00
|
|
|
}
|
|
|
|
|
2018-05-08 04:21:21 +02:00
|
|
|
bool
|
|
|
|
startup_checks()
|
2016-08-31 10:03:12 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2016-07-01 05:04:00 +02:00
|
|
|
{
|
2016-08-31 10:03:12 +02:00
|
|
|
fprintf(stderr, usererrstr, e.what());
|
|
|
|
return false;
|
2016-07-01 05:04:00 +02:00
|
|
|
}
|
2016-08-31 12:55:27 +02:00
|
|
|
|
2018-01-10 09:56:33 +01:00
|
|
|
void
|
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
enable_coredumps()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Setup corefile size immediately after boot -kre
|
|
|
|
//
|
|
|
|
|
|
|
|
rlimit rlim; // resource limits
|
|
|
|
ircd::syscall(getrlimit, RLIMIT_CORE, &rlim);
|
|
|
|
|
|
|
|
// Set corefilesize to maximum
|
|
|
|
rlim.rlim_cur = rlim.rlim_max;
|
|
|
|
ircd::syscall(setrlimit, RLIMIT_CORE, &rlim);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to adjust rlimit: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
enable_coredumps()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-09 00:17:13 +01:00
|
|
|
void
|
|
|
|
applyargs()
|
|
|
|
{
|
|
|
|
if(debugmode)
|
|
|
|
ircd::debugmode.set("true");
|
|
|
|
else
|
|
|
|
ircd::debugmode.set("false");
|
|
|
|
|
|
|
|
if(nolisten)
|
|
|
|
ircd::net::listen.set("false");
|
|
|
|
else
|
|
|
|
ircd::net::listen.set("true");
|
|
|
|
|
|
|
|
if(noautomod)
|
|
|
|
ircd::mods::autoload.set("false");
|
|
|
|
else
|
|
|
|
ircd::mods::autoload.set("true");
|
|
|
|
|
|
|
|
if(checkdb)
|
|
|
|
ircd::db::open_check.set("true");
|
|
|
|
else
|
|
|
|
ircd::db::open_check.set("false");
|
|
|
|
|
|
|
|
if(pitrecdb)
|
|
|
|
ircd::db::open_recover.set("point");
|
|
|
|
else
|
|
|
|
ircd::db::open_recover.set("absolute");
|
|
|
|
|
|
|
|
if(nodirect)
|
|
|
|
ircd::fs::fd::opts::direct_io_enable.set("false");
|
|
|
|
else
|
|
|
|
ircd::fs::fd::opts::direct_io_enable.set("true");
|
|
|
|
|
|
|
|
if(noaio)
|
|
|
|
ircd::fs::aio::enable.set("false");
|
|
|
|
else
|
|
|
|
ircd::fs::aio::enable.set("true");
|
|
|
|
}
|