2012-02-18 04:54:44 +01:00
|
|
|
/*
|
2016-07-01 05:04:00 +02:00
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
2012-02-18 04:54:44 +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
|
|
|
|
* 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.
|
2016-07-01 05:04:00 +02:00
|
|
|
*
|
2012-02-18 04:54:44 +01:00
|
|
|
*/
|
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
#include <ircd/ircd.h>
|
2016-08-31 12:55:27 +02:00
|
|
|
#include <boost/asio.hpp>
|
2016-10-21 01:56:16 +02:00
|
|
|
#include <ircd/ctx/ctx.h>
|
2016-09-22 00:07:14 +02:00
|
|
|
#include "lgetopt.h"
|
2016-07-01 05:04:00 +02:00
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
namespace path = ircd::path;
|
2016-08-31 10:03:12 +02:00
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
static void console_spawn();
|
|
|
|
static void sigfd_handler(const boost::system::error_code &, int);
|
|
|
|
static bool startup_checks();
|
|
|
|
static void print_version();
|
|
|
|
|
|
|
|
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.
|
|
|
|
***
|
|
|
|
%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. ***
|
|
|
|
***
|
|
|
|
%s
|
|
|
|
)"};
|
|
|
|
|
2016-08-31 10:03:12 +02:00
|
|
|
bool printversion;
|
2016-08-31 12:55:27 +02:00
|
|
|
bool testing_conf;
|
2016-09-03 18:16:04 +02:00
|
|
|
bool cmdline;
|
2016-09-01 17:50:48 +02:00
|
|
|
const char *configfile;
|
2016-08-31 10:03:12 +02:00
|
|
|
lgetopt opts[] =
|
|
|
|
{
|
2016-09-01 17:50:48 +02: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" },
|
|
|
|
{ "conftest", &testing_conf, lgetopt::YESNO, "Test the configuration files and exit" },
|
|
|
|
{ "debug", &ircd::debugmode, lgetopt::BOOL, "Enable options for debugging" },
|
2016-09-03 18:16:04 +02:00
|
|
|
{ "cmd", &cmdline, lgetopt::BOOL, "Interrupt to a command line immediately after startup" },
|
2016-09-01 17:50:48 +02:00
|
|
|
{ nullptr, nullptr, lgetopt::STRING, nullptr },
|
2016-08-31 10:03:12 +02:00
|
|
|
};
|
|
|
|
|
2016-09-10 03:35:44 +02:00
|
|
|
boost::asio::io_service *ios
|
|
|
|
{
|
|
|
|
// Having trouble with static destruction in clang so this
|
|
|
|
// has to become still-reachable
|
|
|
|
new boost::asio::io_service
|
|
|
|
};
|
|
|
|
|
|
|
|
boost::asio::signal_set sigs
|
|
|
|
{
|
|
|
|
*ios
|
|
|
|
};
|
2016-08-31 10:03:12 +02:00
|
|
|
|
2016-09-10 21:57:33 +02:00
|
|
|
// TODO: XXX: This has to be declared first before any modules
|
|
|
|
// are loaded otherwise the module will not be unloadable.
|
|
|
|
boost::asio::ip::tcp::acceptor _dummy_acceptor_ { *ios };
|
|
|
|
boost::asio::ip::tcp::socket _dummy_sock_ { *ios };
|
2016-10-29 14:09:20 +02:00
|
|
|
boost::asio::ip::tcp::resolver _dummy_resolver_ { *ios };
|
2016-09-10 21:57:33 +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
|
|
|
|
|
|
|
|
parseargs(&argc, &argv, opts);
|
|
|
|
if(!startup_checks())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if(printversion)
|
|
|
|
{
|
|
|
|
print_version();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
const std::string confpath(configfile?: path::get(path::IRCD_CONF));
|
2016-09-10 03:35:44 +02:00
|
|
|
ircd::init(*ios, confpath);
|
2016-09-08 22:29:02 +02:00
|
|
|
|
2016-09-08 05:31:26 +02:00
|
|
|
sigs.add(SIGHUP);
|
2016-08-31 12:55:27 +02:00
|
|
|
sigs.add(SIGINT);
|
2016-09-07 02:16:44 +02:00
|
|
|
sigs.add(SIGTSTP);
|
2016-09-06 22:11:55 +02:00
|
|
|
sigs.add(SIGQUIT);
|
|
|
|
sigs.add(SIGTERM);
|
2016-10-26 08:25:04 +02:00
|
|
|
sigs.add(SIGUSR1);
|
|
|
|
sigs.add(SIGUSR2);
|
2016-09-09 19:48:33 +02:00
|
|
|
ircd::at_main_exit([]
|
|
|
|
{
|
|
|
|
// Entered when IRCd's main context has finished. ios.run() won't
|
|
|
|
// return because our signal handler out here is still using it.
|
|
|
|
sigs.cancel();
|
|
|
|
});
|
2016-08-31 12:55:27 +02:00
|
|
|
sigs.async_wait(sigfd_handler);
|
2016-09-01 17:50:48 +02:00
|
|
|
|
2016-09-03 18:16:04 +02:00
|
|
|
if(cmdline)
|
2016-09-07 02:16:44 +02:00
|
|
|
console_spawn();
|
2016-09-03 18:16:04 +02:00
|
|
|
|
2016-09-10 03:35:44 +02:00
|
|
|
ios->run(); // Blocks until a clean exit or an exception comes out of it.
|
2016-08-13 05:05:54 +02:00
|
|
|
}
|
2016-09-08 22:29:02 +02:00
|
|
|
catch(const ircd::conf::newconf::syntax_error &e)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void print_version()
|
|
|
|
{
|
|
|
|
printf("VERSION :%s\n",
|
2016-08-31 12:55:27 +02:00
|
|
|
ircd::info::version.c_str());
|
2016-08-31 10:03:12 +02:00
|
|
|
|
|
|
|
#ifdef CUSTOM_BRANDING
|
|
|
|
printf("VERSION :based on %s-%s\n",
|
|
|
|
PACKAGE_NAME,
|
|
|
|
PACKAGE_VERSION);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("VERSION :%s\n", rb_lib_version());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool startup_checks()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
if(geteuid() == 0)
|
2016-08-31 12:55:27 +02:00
|
|
|
throw ircd::error("Don't run ircd as root!!!");
|
2016-08-31 10:03:12 +02:00
|
|
|
#endif
|
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
path::chdir(path::get(path::PREFIX));
|
2016-08-31 10:03:12 +02:00
|
|
|
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
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
const char *const generic_message
|
|
|
|
{R"(
|
|
|
|
*** - To end the console session, type ctrl-d -> EOF
|
|
|
|
*** - To exit cleanly, type DIE or ctrl-\ -> SIGQUIT
|
|
|
|
*** - To exit immediately, type EXIT -> exit(0)
|
|
|
|
*** - To generate a coredump for developers, type ABORT -> abort()
|
|
|
|
***
|
|
|
|
)"};
|
|
|
|
|
|
|
|
bool console_active;
|
|
|
|
ircd::ctx::ctx *console_ctx;
|
|
|
|
boost::asio::posix::stream_descriptor *console_in;
|
|
|
|
|
|
|
|
static void handle_line(const std::string &line);
|
|
|
|
static void console();
|
|
|
|
static void console_cancel();
|
2016-10-26 08:25:04 +02:00
|
|
|
static void handle_usr2();
|
|
|
|
static void handle_usr1();
|
2016-09-07 02:16:44 +02:00
|
|
|
static void handle_quit();
|
|
|
|
static void handle_interruption();
|
|
|
|
static void handle_termstop();
|
2016-09-08 05:31:26 +02:00
|
|
|
static void handle_hangup();
|
2016-09-07 02:16:44 +02:00
|
|
|
|
2016-08-31 12:55:27 +02:00
|
|
|
void
|
|
|
|
sigfd_handler(const boost::system::error_code &ec,
|
|
|
|
int signum)
|
|
|
|
{
|
|
|
|
switch(ec.value())
|
|
|
|
{
|
|
|
|
using namespace boost::system::errc;
|
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
case success:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case operation_canceled:
|
|
|
|
console_cancel();
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
console_cancel();
|
|
|
|
throw std::runtime_error(ec.message());
|
2016-08-31 12:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(signum)
|
|
|
|
{
|
2016-10-26 08:25:04 +02:00
|
|
|
case SIGUSR1: handle_usr1(); break;
|
|
|
|
case SIGUSR2: handle_usr2(); break;
|
2016-09-07 02:16:44 +02:00
|
|
|
case SIGINT: handle_interruption(); break;
|
|
|
|
case SIGTSTP: handle_termstop(); break;
|
|
|
|
case SIGHUP: handle_hangup(); break;
|
|
|
|
case SIGQUIT: handle_quit(); return;
|
2016-10-26 08:25:04 +02:00
|
|
|
case SIGTERM: handle_quit(); return;
|
2016-09-07 02:16:44 +02:00
|
|
|
default: break;
|
|
|
|
}
|
2016-08-31 12:55:27 +02:00
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
sigs.async_wait(sigfd_handler);
|
|
|
|
}
|
2016-08-31 12:55:27 +02:00
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
void
|
|
|
|
handle_quit()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
console_cancel();
|
2016-10-26 08:25:04 +02:00
|
|
|
ircd::stop();
|
2016-09-07 02:16:44 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
ircd::log::error("SIGQUIT handler: %s", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-26 08:25:04 +02:00
|
|
|
handle_usr1()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Do ircd rehash config
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
ircd::log::error("SIGUSR1 handler: %s", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_usr2()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Do ircd rehash bans
|
|
|
|
// Do refresh motd
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
ircd::log::error("SIGUSR2 handler: %s", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-07 02:16:44 +02:00
|
|
|
handle_hangup()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using namespace ircd;
|
|
|
|
using log::console_quiet;
|
|
|
|
|
|
|
|
console_cancel();
|
|
|
|
|
|
|
|
static console_quiet *quieted;
|
|
|
|
if(!quieted)
|
|
|
|
{
|
|
|
|
log::notice("Suppressing console log output after terminal hangup");
|
|
|
|
quieted = new console_quiet;
|
|
|
|
return;
|
2016-08-31 12:55:27 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
log::notice("Reactivating console logging after second hangup");
|
|
|
|
delete quieted;
|
|
|
|
quieted = nullptr;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
ircd::log::error("SIGHUP handler: %s", e.what());
|
2016-08-31 12:55:27 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
const char *const termstop_message
|
2016-08-31 12:55:27 +02:00
|
|
|
{R"(
|
|
|
|
***
|
2016-09-07 02:16:44 +02:00
|
|
|
*** The server has been paused and will resume when you hit enter.
|
|
|
|
*** This is an IRC client and your commands will originate from the
|
|
|
|
*** server itself.
|
|
|
|
***)"};
|
2016-08-31 12:55:27 +02:00
|
|
|
|
|
|
|
void
|
2016-09-07 02:16:44 +02:00
|
|
|
handle_termstop()
|
2016-09-06 01:05:16 +02:00
|
|
|
try
|
2016-08-31 12:55:27 +02:00
|
|
|
{
|
2016-09-07 02:16:44 +02:00
|
|
|
console_cancel();
|
|
|
|
|
|
|
|
std::cout << termstop_message << generic_message;
|
2016-08-31 12:55:27 +02:00
|
|
|
|
|
|
|
std::string line;
|
|
|
|
std::cout << "\n> " << std::flush;
|
|
|
|
std::getline(std::cin, line);
|
|
|
|
if(std::cin.eof())
|
|
|
|
{
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cin.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
handle_line(line);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
std::cerr << "\033[1;31merror\033[0m: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_interruption()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
console_cancel();
|
|
|
|
console_spawn();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
std::cerr << "\033[1;31merror\033[0m: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
console_spawn()
|
|
|
|
{
|
|
|
|
if(console_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The console function is executed asynchronously.
|
2016-09-10 07:23:07 +02:00
|
|
|
// The SELF_DESTRUCT indicates it will clean itself up.
|
|
|
|
ircd::context(std::bind(&console), ircd::ctx::SELF_DESTRUCT);
|
2016-09-07 02:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *const console_message
|
|
|
|
{R"(
|
|
|
|
***
|
|
|
|
*** The server is still running in the background. A command line
|
|
|
|
*** is now available below. This is an IRC client and your commands
|
|
|
|
*** will originate from the server itself.
|
|
|
|
***)"};
|
|
|
|
|
|
|
|
void
|
|
|
|
console()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
const scope atexit([]
|
|
|
|
{
|
|
|
|
console_active = false;
|
|
|
|
console_in = nullptr;
|
|
|
|
});
|
|
|
|
|
|
|
|
console_active = true;
|
|
|
|
console_ctx = &ctx::cur();
|
|
|
|
|
|
|
|
std::cout << console_message << generic_message;
|
|
|
|
|
2016-09-10 03:35:44 +02:00
|
|
|
boost::asio::posix::stream_descriptor in{*::ios, dup(STDIN_FILENO)};
|
2016-09-07 02:16:44 +02:00
|
|
|
console_in = ∈
|
|
|
|
|
|
|
|
boost::asio::streambuf buf{BUFSIZE};
|
|
|
|
std::istream is{&buf};
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
while(1) try
|
|
|
|
{
|
|
|
|
std::cout << "\n> " << std::flush;
|
|
|
|
|
|
|
|
// Suppression scope ends after the command is entered
|
|
|
|
// so the output of the command (log messages) can be seen.
|
|
|
|
{
|
|
|
|
const log::console_quiet quiet(false);
|
|
|
|
boost::asio::async_read_until(in, buf, '\n', yield(continuation()));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::getline(is, line);
|
|
|
|
if(!line.empty())
|
|
|
|
handle_line(line);
|
|
|
|
}
|
|
|
|
catch(const ircd::cmds::not_found &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout << "\n***" << std::endl;
|
|
|
|
std::cout << "*** The console session has ended: " << e.what() << std::endl;
|
|
|
|
std::cout << "***" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
console_cancel()
|
|
|
|
{
|
|
|
|
if(!console_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!console_in)
|
|
|
|
return;
|
|
|
|
|
|
|
|
console_in->cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_line(const std::string &line)
|
|
|
|
{
|
2016-08-31 12:55:27 +02:00
|
|
|
if(line == "ABORT")
|
|
|
|
abort();
|
|
|
|
|
|
|
|
if(line == "EXIT")
|
|
|
|
exit(0);
|
2016-09-06 01:05:16 +02:00
|
|
|
|
2016-09-10 21:57:33 +02:00
|
|
|
if(unlikely(!ircd::me))
|
|
|
|
throw ircd::error("IRCd `me' not available to execute on");
|
|
|
|
|
|
|
|
ircd::execute(*ircd::me, line);
|
2016-08-31 12:55:27 +02:00
|
|
|
}
|