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