main.cpp 6.67 KB
Newer Older
1
#include <xmpp/biboumi_component.hpp>
2
#include <utils/timed_events.hpp>
3 4
#include <network/poller.hpp>
#include <config/config.hpp>
louiz’'s avatar
louiz’ committed
5
#include <logger/logger.hpp>
louiz’'s avatar
louiz’ committed
6
#include <utils/xdg.hpp>
7
#include <utils/reload.hpp>
louiz’'s avatar
louiz’ committed
8

louiz’'s avatar
louiz’ committed
9
#ifdef UDNS_FOUND
10 11 12
# include <network/dns_handler.hpp>
#endif

13
#include <atomic>
14
#include <csignal>
15 16 17
#ifdef USE_DATABASE
# include <litesql.hpp>
#endif
18

louiz’'s avatar
louiz’ committed
19 20
#include <identd/identd_server.hpp>

louiz’'s avatar
louiz’ committed
21
// A flag set by the SIGINT signal handler.
louiz’'s avatar
louiz’ committed
22
static std::atomic<bool> stop(false);
louiz’'s avatar
louiz’ committed
23
// Flag set by the SIGUSR1/2 signal handler.
louiz’'s avatar
louiz’ committed
24
static std::atomic<bool> reload(false);
louiz’'s avatar
louiz’ committed
25 26 27
// A flag indicating that we are wanting to exit the process. i.e: if this
// flag is set and all connections are closed, we can exit properly.
static bool exiting = false;
28

29 30 31 32 33 34 35
/**
 * Provide an helpful message to help the user write a minimal working
 * configuration file.
 */
int config_help(const std::string& missing_option)
{
  if (!missing_option.empty())
louiz’'s avatar
louiz’ committed
36
    log_error("Configuration error: empty value for option ", missing_option, ".");
louiz’'s avatar
louiz’ committed
37 38
  log_error("Please provide a configuration file filled like this:\n\n"
            "hostname=irc.example.com\npassword=S3CR3T");
39 40 41
  return 1;
}

louiz’'s avatar
louiz’ committed
42 43 44 45 46 47
int display_help()
{
  std::cout << "Usage: biboumi [configuration_file]" << std::endl;
  return 0;
}

48
static void sigint_handler(int sig, siginfo_t*, void*)
louiz’'s avatar
louiz’ committed
49
{
50
  // In 2 seconds, repeat the same signal, to force the exit
51
  TimedEventsManager::instance().add_event(TimedEvent(std::chrono::steady_clock::now() + 2s,
52
                                    [sig]() { raise(sig); }));
53
  stop.store(true);
louiz’'s avatar
louiz’ committed
54 55
}

louiz’'s avatar
louiz’ committed
56 57
static void sigusr_handler(int, siginfo_t*, void*)
{
58
  reload.store(true);
louiz’'s avatar
louiz’ committed
59 60
}

61
int main(int ac, char** av)
louiz’'s avatar
louiz’ committed
62
{
louiz’'s avatar
louiz’ committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76
  if (ac > 1)
    {
      const std::string arg = av[1];
      if (arg.size() >= 2 && arg[0] == '-' && arg[1] == '-')
        {
          if (arg == "--help")
            return display_help();
          else
            {
              std::cerr << "Unknow command line option: " << arg << std::endl;
              return 1;
            }
        }
    }
77
  const std::string conf_filename = ac > 1 ? av[1] : xdg_config_path("biboumi.cfg");
78
  std::cout << "Using configuration file: " << conf_filename << std::endl;
79

80
  if (!Config::read_conf(conf_filename))
81
    return config_help("");
82 83

  const std::string password = Config::get("password", "");
84
  if (password.empty())
85
    return config_help("password");
86
  const std::string hostname = Config::get("hostname", "");
87 88
  if (hostname.empty())
    return config_help("hostname");
89

90 91

#ifdef USE_DATABASE
92
  try {
93 94 95 96 97
    open_database();
  } catch (const litesql::DatabaseError&) {
    return 1;
  }
#endif
98

99 100 101
  // Block the signals we want to manage. They will be unblocked only during
  // the epoll_pwait or ppoll calls. This avoids some race conditions,
  // explained in man 2 pselect on linux
102
  sigset_t mask{};
103 104 105 106 107 108 109
  sigemptyset(&mask);
  sigaddset(&mask, SIGINT);
  sigaddset(&mask, SIGTERM);
  sigaddset(&mask, SIGUSR1);
  sigaddset(&mask, SIGUSR2);
  sigprocmask(SIG_BLOCK, &mask, nullptr);

louiz’'s avatar
louiz’ committed
110 111
  // Install the signals used to exit the process cleanly, or reload the
  // config
louiz’'s avatar
louiz’ committed
112 113
  struct sigaction on_sigint;
  on_sigint.sa_sigaction = &sigint_handler;
114 115
  // All signals must be blocked while a signal handler is running
  sigfillset(&on_sigint.sa_mask);
louiz’'s avatar
louiz’ committed
116 117
  // we want to catch that signal only once.
  // Sending SIGINT again will "force" an exit
louiz’'s avatar
louiz’ committed
118 119 120 121 122 123 124
  on_sigint.sa_flags = SA_RESETHAND;
  sigaction(SIGINT, &on_sigint, nullptr);
  sigaction(SIGTERM, &on_sigint, nullptr);

  // Install a signal to reload the config on SIGUSR1/2
  struct sigaction on_sigusr;
  on_sigusr.sa_sigaction = &sigusr_handler;
125
  sigfillset(&on_sigusr.sa_mask);
louiz’'s avatar
louiz’ committed
126
  on_sigusr.sa_flags = 0;
louiz’'s avatar
louiz’ committed
127 128
  sigaction(SIGUSR1, &on_sigusr, nullptr);
  sigaction(SIGUSR2, &on_sigusr, nullptr);
louiz’'s avatar
louiz’ committed
129

louiz’'s avatar
louiz’ committed
130
  auto p = std::make_shared<Poller>();
louiz’'s avatar
louiz’ committed
131

louiz’'s avatar
louiz’ committed
132 133 134 135
#ifdef UDNS_FOUND
  DNSHandler dns_handler(p);
#endif

louiz’'s avatar
louiz’ committed
136 137
  auto xmpp_component =
    std::make_shared<BiboumiComponent>(p, hostname, password);
138 139
  xmpp_component->start();

louiz’'s avatar
louiz’ committed
140 141
  IdentdServer identd(*xmpp_component, p, static_cast<uint16_t>(Config::get_int("identd_port", 113)));

142
  auto timeout = TimedEventsManager::instance().get_timeout();
143
  while (p->poll(timeout) != -1)
louiz’'s avatar
louiz’ committed
144
  {
145
    TimedEventsManager::instance().execute_expired_events();
louiz’'s avatar
louiz’ committed
146 147 148
    // Check for empty irc_clients (not connected, or with no joined
    // channel) and remove them
    xmpp_component->clean();
louiz’'s avatar
louiz’ committed
149
    identd.clean();
louiz’'s avatar
louiz’ committed
150 151 152
    if (stop)
    {
      log_info("Signal received, exiting...");
153 154 155
#ifdef SYSTEMD_FOUND
      sd_notify(0, "STOPPING=1");
#endif
louiz’'s avatar
louiz’ committed
156
      exiting = true;
157
      stop.store(false);
louiz’'s avatar
louiz’ committed
158
      xmpp_component->shutdown();
louiz’'s avatar
louiz’ committed
159 160 161
#ifdef UDNS_FOUND
      dns_handler.destroy();
#endif
louiz’'s avatar
louiz’ committed
162
      identd.shutdown();
louiz’'s avatar
louiz’ committed
163
      // Cancel the timer for a potential reconnection
164
      TimedEventsManager::instance().cancel("XMPP reconnection");
louiz’'s avatar
louiz’ committed
165
    }
louiz’'s avatar
louiz’ committed
166 167 168
    if (reload)
    {
      log_info("Signal received, reloading the config...");
louiz’'s avatar
louiz’ committed
169
      ::reload_process();
170
      reload.store(false);
louiz’'s avatar
louiz’ committed
171
    }
172 173 174 175
    // Reconnect to the XMPP server if this was not intended.  This may have
    // happened because we sent something invalid to it and it decided to
    // close the connection.  This is a bug that should be fixed, but we
    // still reconnect automatically instead of dropping everything
louiz’'s avatar
louiz’ committed
176
    if (!exiting &&
177
        !xmpp_component->is_connected() &&
178
        !xmpp_component->is_connecting())
179
    {
louiz’'s avatar
louiz’ committed
180
      if (xmpp_component->ever_auth)
181
        {
louiz’'s avatar
louiz’ committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
          if (xmpp_component->first_connection_try == true)
            { // immediately re-try to connect
              xmpp_component->reset();
              xmpp_component->start();
            }
          else
            { // Re-connecting failed, we now try only each few seconds
              auto reconnect_later = [xmpp_component]()
              {
                xmpp_component->reset();
                xmpp_component->start();
              };
              TimedEvent event(std::chrono::steady_clock::now() + 2s, reconnect_later, "XMPP reconnection");
              TimedEventsManager::instance().add_event(std::move(event));
            }
        }
      else
199 200 201 202 203 204
        {
#ifdef UDNS_FOUND
          dns_handler.destroy();
#endif
          identd.shutdown();
        }
205
    }
louiz’'s avatar
louiz’ committed
206 207
    // If the only existing connection is the one to the XMPP component:
    // close the XMPP stream.
208 209
    if (exiting && xmpp_component->is_connecting())
      xmpp_component->close();
210
    if (exiting && p->size() == 1 && xmpp_component->is_document_open())
louiz’'s avatar
louiz’ committed
211
      xmpp_component->close_document();
212 213 214 215
    if (exiting) // If we are exiting, do not wait for any timed event
      timeout = utils::no_timeout;
    else
      timeout = TimedEventsManager::instance().get_timeout();
louiz’'s avatar
louiz’ committed
216
  }
217 218
  if (!xmpp_component->ever_auth)
    return 1; // To signal that the process did not properly start
louiz’'s avatar
louiz’ committed
219
  log_info("All connections cleanly closed, have a nice day.");
louiz’'s avatar
louiz’ committed
220 221
  return 0;
}