Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 24 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 25 | #include <getopt.h> |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 26 | #include <boost/filesystem.hpp> |
| 27 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 28 | #include "version.hpp" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 29 | #include "core/logger.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 30 | #include "core/global-io.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 31 | #include "core/privilege-helper.hpp" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 32 | #include "fw/forwarder.hpp" |
| 33 | #include "mgmt/internal-face.hpp" |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 34 | #include "mgmt/fib-manager.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 35 | #include "mgmt/face-manager.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 36 | #include "mgmt/strategy-choice-manager.hpp" |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 37 | #include "mgmt/status-server.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 38 | #include "core/config-file.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 39 | #include "mgmt/general-config-section.hpp" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 40 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 41 | namespace nfd { |
| 42 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 43 | NFD_LOG_INIT("NFD"); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 44 | |
| 45 | struct ProgramOptions |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 46 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 47 | bool showUsage; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 48 | bool showVersion; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 49 | bool showModules; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 50 | std::string config; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 53 | class Nfd : noncopyable |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 54 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 55 | public: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 56 | explicit |
| 57 | Nfd(const std::string& configFile) |
| 58 | : m_configFile(configFile) |
| 59 | , m_originalStreamBuf(0) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | ~Nfd() |
| 64 | { |
| 65 | if (static_cast<bool>(m_originalStreamBuf)) { |
| 66 | std::clog.rdbuf(m_originalStreamBuf); |
| 67 | } |
| 68 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 69 | |
| 70 | void |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 71 | initialize() |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 72 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 73 | initializeLogging(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 74 | |
| 75 | m_forwarder = make_shared<Forwarder>(); |
| 76 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 77 | initializeManagement(); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 78 | |
| 79 | PrivilegeHelper::drop(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
| 83 | void |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 84 | initializeLogging() |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 85 | { |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 86 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 87 | LoggerFactory::getInstance().setConfigFile(config); |
| 88 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 89 | config.parse(m_configFile, true); |
| 90 | config.parse(m_configFile, false); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 91 | } |
| 92 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 93 | class IgnoreRibAndLogSections |
| 94 | { |
| 95 | public: |
| 96 | void |
| 97 | operator()(const std::string& filename, |
| 98 | const std::string& sectionName, |
| 99 | const ConfigSection& section, |
| 100 | bool isDryRun) |
| 101 | |
| 102 | { |
| 103 | // Ignore "log" and sections beginning with "rib_" (intended for rib manager), |
| 104 | // but raise an error if we're missing a handler for an NFD section. |
| 105 | |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 106 | if (sectionName.find("rib") == 0 || sectionName == "log") |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 107 | { |
| 108 | // do nothing |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | // missing NFD section |
| 113 | ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun); |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 118 | void |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 119 | initializeManagement() |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 120 | { |
| 121 | m_internalFace = make_shared<InternalFace>(); |
| 122 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 123 | m_fibManager = make_shared<FibManager>(ref(m_forwarder->getFib()), |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 124 | bind(&Forwarder::getFace, m_forwarder.get(), _1), |
| 125 | m_internalFace); |
| 126 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 127 | m_faceManager = make_shared<FaceManager>(ref(m_forwarder->getFaceTable()), |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 128 | m_internalFace); |
| 129 | |
| 130 | m_strategyChoiceManager = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 131 | make_shared<StrategyChoiceManager>(ref(m_forwarder->getStrategyChoice()), |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 132 | m_internalFace); |
| 133 | |
| 134 | m_statusServer = make_shared<StatusServer>(m_internalFace, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 135 | ref(*m_forwarder)); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 136 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 137 | ConfigFile config((IgnoreRibAndLogSections())); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 138 | |
| 139 | general::setConfigFile(config); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 140 | m_internalFace->getValidator().setConfigFile(config); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 141 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 142 | m_forwarder->addFace(m_internalFace); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 143 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 144 | m_faceManager->setConfigFile(config); |
| 145 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 146 | // parse config file |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 147 | config.parse(m_configFile, true); |
| 148 | config.parse(m_configFile, false); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 149 | |
| 150 | // add FIB entry for NFD Management Protocol |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 151 | shared_ptr<fib::Entry> entry = m_forwarder->getFib().insert("/localhost/nfd").first; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 152 | entry->addNextHop(m_internalFace, 0); |
| 153 | } |
| 154 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 155 | static void |
| 156 | printUsage(std::ostream& os, const std::string& programName) |
| 157 | { |
| 158 | os << "Usage: \n" |
| 159 | << " " << programName << " [options]\n" |
| 160 | << "\n" |
| 161 | << "Run NFD forwarding daemon\n" |
| 162 | << "\n" |
| 163 | << "Options:\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 164 | << " [--help] - print this help message\n" |
| 165 | << " [--version] - print version and exit\n" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 166 | << " [--modules] - list available logging modules\n" |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 167 | << " [--config /path/to/nfd.conf] - path to configuration file " |
| 168 | << "(default: " << DEFAULT_CONFIG_FILE << ")\n" |
| 169 | ; |
| 170 | } |
| 171 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 172 | static void |
| 173 | printModules(std::ostream& os) |
| 174 | { |
| 175 | using namespace std; |
| 176 | |
| 177 | os << "Available logging modules: \n"; |
| 178 | |
| 179 | list<string> modules(LoggerFactory::getInstance().getModules()); |
| 180 | for (list<string>::const_iterator i = modules.begin(); i != modules.end(); ++i) |
| 181 | { |
| 182 | os << *i << "\n"; |
| 183 | } |
| 184 | } |
| 185 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 186 | static bool |
| 187 | parseCommandLine(int argc, char** argv, ProgramOptions& options) |
| 188 | { |
| 189 | options.showUsage = false; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 190 | options.showVersion = false; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 191 | options.showModules = false; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 192 | options.config = DEFAULT_CONFIG_FILE; |
| 193 | |
| 194 | while (true) { |
| 195 | int optionIndex = 0; |
| 196 | static ::option longOptions[] = { |
| 197 | { "help" , no_argument , 0, 0 }, |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 198 | { "modules", no_argument , 0, 0 }, |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 199 | { "config" , required_argument, 0, 0 }, |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 200 | { "version", no_argument , 0, 0 }, |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 201 | { 0 , 0 , 0, 0 } |
| 202 | }; |
| 203 | int c = getopt_long_only(argc, argv, "", longOptions, &optionIndex); |
| 204 | if (c == -1) |
| 205 | break; |
| 206 | |
| 207 | switch (c) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 208 | case 0: |
| 209 | switch (optionIndex) { |
| 210 | case 0: // help |
| 211 | options.showUsage = true; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 212 | break; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 213 | case 1: // modules |
| 214 | options.showModules = true; |
| 215 | break; |
| 216 | case 2: // config |
| 217 | options.config = ::optarg; |
| 218 | break; |
| 219 | case 3: // version |
| 220 | options.showVersion = true; |
| 221 | break; |
| 222 | default: |
| 223 | return false; |
| 224 | } |
| 225 | break; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 226 | } |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 227 | } |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 228 | return true; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 229 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 230 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 231 | void |
| 232 | terminate(const boost::system::error_code& error, |
| 233 | int signalNo, |
| 234 | boost::asio::signal_set& signalSet) |
| 235 | { |
| 236 | if (error) |
| 237 | return; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 238 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 239 | if (signalNo == SIGINT || |
| 240 | signalNo == SIGTERM) |
| 241 | { |
| 242 | getGlobalIoService().stop(); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 243 | NFD_LOG_INFO("Caught signal '" << strsignal(signalNo) << "', exiting..."); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 244 | } |
| 245 | else |
| 246 | { |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 247 | signalSet.async_wait(bind(&Nfd::terminate, this, _1, _2, ref(signalSet))); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 248 | } |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 249 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 250 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 251 | void |
| 252 | reload(const boost::system::error_code& error, |
| 253 | int signalNo, |
| 254 | boost::asio::signal_set& signalSet) |
| 255 | { |
| 256 | if (error) |
| 257 | return; |
| 258 | |
| 259 | NFD_LOG_INFO("Caught signal '" << strsignal(signalNo)); |
| 260 | |
| 261 | //////////////////////// |
| 262 | // Reload config file // |
| 263 | //////////////////////// |
| 264 | |
| 265 | // Logging |
| 266 | initializeLogging(); |
| 267 | /// \todo Reopen log file |
| 268 | |
| 269 | // Other stuff |
| 270 | ConfigFile config((IgnoreRibAndLogSections())); |
| 271 | |
| 272 | general::setConfigFile(config); |
| 273 | |
| 274 | m_internalFace->getValidator().setConfigFile(config); |
| 275 | m_faceManager->setConfigFile(config); |
| 276 | |
| 277 | config.parse(m_configFile, false); |
| 278 | |
| 279 | //////////////////////// |
| 280 | |
| 281 | signalSet.async_wait(bind(&Nfd::reload, this, _1, _2, ref(signalSet))); |
| 282 | } |
| 283 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 284 | private: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 285 | std::string m_configFile; |
| 286 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 287 | shared_ptr<Forwarder> m_forwarder; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 288 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 289 | shared_ptr<InternalFace> m_internalFace; |
| 290 | shared_ptr<FibManager> m_fibManager; |
| 291 | shared_ptr<FaceManager> m_faceManager; |
| 292 | shared_ptr<StrategyChoiceManager> m_strategyChoiceManager; |
| 293 | shared_ptr<StatusServer> m_statusServer; |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 294 | |
| 295 | shared_ptr<std::ofstream> m_logFile; |
| 296 | std::basic_streambuf<char>* m_originalStreamBuf; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 297 | }; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 298 | |
| 299 | } // namespace nfd |
| 300 | |
| 301 | int |
| 302 | main(int argc, char** argv) |
| 303 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 304 | using namespace nfd; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 305 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 306 | ProgramOptions options; |
| 307 | bool isCommandLineValid = Nfd::parseCommandLine(argc, argv, options); |
| 308 | if (!isCommandLineValid) { |
| 309 | Nfd::printUsage(std::cerr, argv[0]); |
| 310 | return 1; |
| 311 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 312 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 313 | if (options.showUsage) { |
| 314 | Nfd::printUsage(std::cout, argv[0]); |
| 315 | return 0; |
| 316 | } |
| 317 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 318 | if (options.showVersion) { |
| 319 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 320 | return 0; |
| 321 | } |
| 322 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 323 | if (options.showModules) { |
| 324 | Nfd::printModules(std::cout); |
| 325 | return 0; |
| 326 | } |
| 327 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 328 | Nfd nfdInstance(options.config); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 329 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 330 | try { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 331 | nfdInstance.initialize(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 332 | } |
| 333 | catch (boost::filesystem::filesystem_error& e) { |
| 334 | if (e.code() == boost::system::errc::permission_denied) { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 335 | NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " << |
| 336 | argv[0] << " should be run as superuser"); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 337 | } |
| 338 | else { |
| 339 | NFD_LOG_FATAL(e.what()); |
| 340 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 341 | return 1; |
| 342 | } |
| 343 | catch (const std::exception& e) { |
| 344 | NFD_LOG_FATAL(e.what()); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 345 | return 2; |
| 346 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 347 | catch (const PrivilegeHelper::Error& e) { |
| 348 | // PrivilegeHelper::Errors do not inherit from std::exception |
| 349 | // and represent seteuid/gid failures |
| 350 | |
| 351 | NFD_LOG_FATAL(e.what()); |
| 352 | return 3; |
| 353 | } |
| 354 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 355 | boost::asio::signal_set terminationSignalSet(getGlobalIoService()); |
| 356 | terminationSignalSet.add(SIGINT); |
| 357 | terminationSignalSet.add(SIGTERM); |
| 358 | terminationSignalSet.async_wait(bind(&Nfd::terminate, &nfdInstance, _1, _2, |
| 359 | ref(terminationSignalSet))); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 360 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 361 | boost::asio::signal_set reloadSignalSet(getGlobalIoService()); |
| 362 | reloadSignalSet.add(SIGHUP); |
| 363 | reloadSignalSet.async_wait(bind(&Nfd::reload, &nfdInstance, _1, _2, |
| 364 | ref(reloadSignalSet))); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 365 | |
| 366 | try { |
| 367 | getGlobalIoService().run(); |
| 368 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 369 | catch (const std::exception& e) { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 370 | NFD_LOG_FATAL(e.what()); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 371 | return 4; |
| 372 | } |
| 373 | catch (const PrivilegeHelper::Error& e) { |
| 374 | NFD_LOG_FATAL(e.what()); |
| 375 | return 5; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |