Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 26 | #include "nfd.hpp" |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 27 | #include "rib/service.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 29 | #include "core/extended-error-message.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 30 | #include "core/global-io.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 31 | #include "core/logger.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 32 | #include "core/privilege-helper.hpp" |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 33 | #include "core/version.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 34 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 35 | #include <string.h> // for strsignal() |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 36 | |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 37 | #include <boost/config.hpp> |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 38 | #include <boost/filesystem.hpp> |
| 39 | #include <boost/program_options/options_description.hpp> |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 40 | #include <boost/program_options/parsers.hpp> |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 41 | #include <boost/program_options/variables_map.hpp> |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 42 | // boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage, |
Davide Pesavento | 5f35f64 | 2018-05-10 19:36:03 -0400 | [diff] [blame] | 43 | // see https://www.boost.org/doc/libs/1_58_0/doc/html/thread/thread_local_storage.html |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 44 | #include <boost/thread.hpp> |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 45 | #include <boost/version.hpp> |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 46 | |
| 47 | #include <atomic> |
| 48 | #include <condition_variable> |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 49 | #include <iostream> |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 50 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 51 | #include <ndn-cxx/util/logging.hpp> |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 52 | #include <ndn-cxx/version.hpp> |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 53 | |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 54 | #ifdef HAVE_LIBPCAP |
| 55 | #include <pcap/pcap.h> |
| 56 | #endif |
| 57 | #ifdef HAVE_WEBSOCKET |
| 58 | #include <websocketpp/version.hpp> |
| 59 | #endif |
| 60 | |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 61 | namespace po = boost::program_options; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 62 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 63 | NFD_LOG_INIT(Main); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 64 | |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 65 | namespace nfd { |
| 66 | |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 67 | /** \brief Executes NFD with RIB manager |
| 68 | * |
| 69 | * NFD (main forwarding procedure) and RIB manager execute in two different threads. |
Junxiao Shi | 71d1214 | 2016-07-23 20:10:18 +0000 | [diff] [blame] | 70 | * Each thread has its own instances of global io_service and global scheduler. |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 71 | * |
| 72 | * When either of the daemons fails, execution of non-failed daemon will be terminated as |
| 73 | * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager |
| 74 | * fails, NFD will be terminated. |
| 75 | */ |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 76 | class NfdRunner : noncopyable |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 77 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 78 | public: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 79 | explicit |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 80 | NfdRunner(const std::string& configFile) |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 81 | : m_nfd(configFile, m_nfdKeyChain) |
| 82 | , m_configFile(configFile) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 83 | , m_terminationSignalSet(getGlobalIoService()) |
| 84 | , m_reloadSignalSet(getGlobalIoService()) |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 85 | { |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 86 | m_terminationSignalSet.add(SIGINT); |
| 87 | m_terminationSignalSet.add(SIGTERM); |
| 88 | m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2)); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 89 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 90 | m_reloadSignalSet.add(SIGHUP); |
| 91 | m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2)); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 94 | void |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 95 | initialize() |
| 96 | { |
| 97 | m_nfd.initialize(); |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | run() |
| 102 | { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 103 | // Return value: a non-zero value is assigned when either NFD or RIB manager (running in |
| 104 | // a separate thread) fails. |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 105 | std::atomic_int retval(0); |
| 106 | |
| 107 | boost::asio::io_service* const mainIo = &getGlobalIoService(); |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 108 | boost::asio::io_service* ribIo = nullptr; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 109 | |
| 110 | // Mutex and conditional variable to implement synchronization between main and RIB manager |
| 111 | // threads: |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 112 | // - to block main thread until RIB manager thread starts and initializes ribIo (to allow |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 113 | // stopping it later) |
| 114 | std::mutex m; |
| 115 | std::condition_variable cv; |
| 116 | |
| 117 | std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 118 | boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] { |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 119 | { |
| 120 | std::lock_guard<std::mutex> lock(m); |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 121 | ribIo = &getGlobalIoService(); |
| 122 | BOOST_ASSERT(ribIo != mainIo); |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 123 | } |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 124 | cv.notify_all(); // notify that ribIo has been assigned |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 125 | |
| 126 | try { |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 127 | ndn::KeyChain ribKeyChain; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 128 | // must be created inside a separate thread |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 129 | rib::Service ribService(configFile, ribKeyChain); |
| 130 | ribService.initialize(); |
| 131 | getGlobalIoService().run(); // ribIo is not thread-safe to use here |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 132 | } |
| 133 | catch (const std::exception& e) { |
| 134 | NFD_LOG_FATAL(e.what()); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 135 | retval = 1; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 136 | mainIo->stop(); |
| 137 | } |
| 138 | |
| 139 | { |
| 140 | std::lock_guard<std::mutex> lock(m); |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 141 | ribIo = nullptr; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 142 | } |
| 143 | }); |
| 144 | |
| 145 | { |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 146 | // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 147 | // RIB manager thread. |
| 148 | std::unique_lock<std::mutex> lock(m); |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 149 | cv.wait(lock, [&ribIo] { return ribIo != nullptr; }); |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | try { |
| 153 | mainIo->run(); |
| 154 | } |
| 155 | catch (const std::exception& e) { |
Junxiao Shi | 71d1214 | 2016-07-23 20:10:18 +0000 | [diff] [blame] | 156 | NFD_LOG_FATAL(getExtendedErrorMessage(e)); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 157 | retval = 1; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 158 | } |
| 159 | catch (const PrivilegeHelper::Error& e) { |
| 160 | NFD_LOG_FATAL(e.what()); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 161 | retval = 4; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | { |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 165 | // ribIo is guaranteed to be alive at this point |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 166 | std::lock_guard<std::mutex> lock(m); |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 167 | if (ribIo != nullptr) { |
| 168 | ribIo->stop(); |
| 169 | ribIo = nullptr; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 172 | ribThread.join(); |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 173 | |
| 174 | return retval; |
| 175 | } |
| 176 | |
| 177 | void |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 178 | terminate(const boost::system::error_code& error, int signalNo) |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 179 | { |
| 180 | if (error) |
| 181 | return; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 183 | NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting..."); |
| 184 | getGlobalIoService().stop(); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 185 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 186 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 187 | void |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 188 | reload(const boost::system::error_code& error, int signalNo) |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 189 | { |
| 190 | if (error) |
| 191 | return; |
| 192 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 193 | NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading..."); |
| 194 | m_nfd.reloadConfigFile(); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 195 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 196 | m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2)); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 197 | } |
| 198 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 199 | private: |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 200 | ndn::KeyChain m_nfdKeyChain; |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 201 | Nfd m_nfd; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 202 | std::string m_configFile; |
| 203 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 204 | boost::asio::signal_set m_terminationSignalSet; |
| 205 | boost::asio::signal_set m_reloadSignalSet; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 206 | }; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 207 | |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 208 | static void |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 209 | printUsage(std::ostream& os, const char* programName, const po::options_description& opts) |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 210 | { |
| 211 | os << "Usage: " << programName << " [options]\n" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 212 | << "\n" |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 213 | << "Run the NDN Forwarding Daemon (NFD)\n" |
| 214 | << "\n" |
| 215 | << opts; |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | printLogModules(std::ostream& os) |
| 220 | { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 221 | const auto& modules = ndn::util::Logging::getLoggerNames(); |
| 222 | std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os, "\n")); |
| 223 | os << std::endl; |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 226 | } // namespace nfd |
| 227 | |
| 228 | int |
| 229 | main(int argc, char** argv) |
| 230 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 231 | using namespace nfd; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 232 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 233 | std::string configFile = DEFAULT_CONFIG_FILE; |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 234 | |
| 235 | po::options_description description("Options"); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 236 | description.add_options() |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 237 | ("help,h", "print this message and exit") |
| 238 | ("version,V", "show version information and exit") |
| 239 | ("config,c", po::value<std::string>(&configFile), |
| 240 | "path to configuration file (default: " DEFAULT_CONFIG_FILE ")") |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 241 | ("modules,m", "list available logging modules") |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 242 | ; |
| 243 | |
| 244 | po::variables_map vm; |
| 245 | try { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 246 | po::store(po::parse_command_line(argc, argv, description), vm); |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 247 | po::notify(vm); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 248 | } |
| 249 | catch (const std::exception& e) { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 250 | // Cannot use NFD_LOG_* macros here, because the logging subsystem is not initialized yet |
| 251 | // at this point. Moreover, we don't want to clutter error messages related to command-line |
| 252 | // parsing with timestamps and other useless text added by the macros. |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 253 | std::cerr << "ERROR: " << e.what() << "\n\n"; |
| 254 | printUsage(std::cerr, argv[0], description); |
| 255 | return 2; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 256 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 257 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 258 | if (vm.count("help") > 0) { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 259 | printUsage(std::cout, argv[0], description); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 263 | if (vm.count("version") > 0) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 264 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 265 | return 0; |
| 266 | } |
| 267 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 268 | if (vm.count("modules") > 0) { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 269 | printLogModules(std::cout); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 273 | const std::string boostBuildInfo = |
| 274 | "with Boost version " + to_string(BOOST_VERSION / 100000) + |
| 275 | "." + to_string(BOOST_VERSION / 100 % 1000) + |
| 276 | "." + to_string(BOOST_VERSION % 100); |
| 277 | const std::string pcapBuildInfo = |
| 278 | #ifdef HAVE_LIBPCAP |
| 279 | "with " + std::string(pcap_lib_version()); |
| 280 | #else |
| 281 | "without libpcap"; |
| 282 | #endif |
| 283 | const std::string wsBuildInfo = |
| 284 | #ifdef HAVE_WEBSOCKET |
| 285 | "with WebSocket++ version " + to_string(websocketpp::major_version) + |
| 286 | "." + to_string(websocketpp::minor_version) + |
| 287 | "." + to_string(websocketpp::patch_version); |
| 288 | #else |
| 289 | "without WebSocket++"; |
| 290 | #endif |
| 291 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 292 | std::clog << "NFD version " NFD_VERSION_BUILD_STRING " starting\n" |
| 293 | << "Built with " BOOST_COMPILER ", with " BOOST_STDLIB |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 294 | ", " << boostBuildInfo << |
| 295 | ", " << pcapBuildInfo << |
| 296 | ", " << wsBuildInfo << |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 297 | ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING |
| 298 | << std::endl; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 299 | |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 300 | NfdRunner runner(configFile); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 301 | try { |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 302 | runner.initialize(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 303 | } |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 304 | catch (const boost::filesystem::filesystem_error& e) { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 305 | if (e.code() == boost::system::errc::permission_denied) { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 306 | NFD_LOG_FATAL("Permission denied for " << e.path1() << |
| 307 | ". This program should be run as superuser"); |
| 308 | return 4; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 309 | } |
| 310 | else { |
Spyridon Mastorakis | d374c40 | 2015-09-09 15:07:00 -0700 | [diff] [blame] | 311 | NFD_LOG_FATAL(getExtendedErrorMessage(e)); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 312 | return 1; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 313 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 314 | } |
| 315 | catch (const std::exception& e) { |
Spyridon Mastorakis | d374c40 | 2015-09-09 15:07:00 -0700 | [diff] [blame] | 316 | NFD_LOG_FATAL(getExtendedErrorMessage(e)); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 317 | return 1; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 318 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 319 | catch (const PrivilegeHelper::Error& e) { |
| 320 | // PrivilegeHelper::Errors do not inherit from std::exception |
| 321 | // and represent seteuid/gid failures |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 322 | NFD_LOG_FATAL(e.what()); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 323 | return 4; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 324 | } |
| 325 | |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 326 | return runner.run(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 327 | } |