NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
main.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2017, 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.
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/>.
24  */
25 
26 #include "nfd.hpp"
27 #include "rib/service.hpp"
28 
30 #include "core/global-io.hpp"
31 #include "core/logger.hpp"
32 #include "core/logger-factory.hpp"
34 #include "core/version.hpp"
35 
36 #include <string.h>
37 
38 #include <boost/filesystem.hpp>
39 #include <boost/program_options/options_description.hpp>
40 #include <boost/program_options/parsers.hpp>
41 #include <boost/program_options/variables_map.hpp>
42 
43 // boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage,
44 // see http://www.boost.org/doc/libs/1_54_0/doc/html/thread/thread_local_storage.html
45 #include <boost/thread.hpp>
46 
47 #include <atomic>
48 #include <condition_variable>
49 #include <iostream>
50 
51 namespace po = boost::program_options;
52 
53 NFD_LOG_INIT("NFD");
54 
55 namespace nfd {
56 
66 class NfdRunner : noncopyable
67 {
68 public:
69  explicit
70  NfdRunner(const std::string& configFile)
71  : m_nfd(configFile, m_nfdKeyChain)
72  , m_configFile(configFile)
73  , m_terminationSignalSet(getGlobalIoService())
74  , m_reloadSignalSet(getGlobalIoService())
75  {
76  m_terminationSignalSet.add(SIGINT);
77  m_terminationSignalSet.add(SIGTERM);
78  m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
79 
80  m_reloadSignalSet.add(SIGHUP);
81  m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
82  }
83 
84  void
86  {
87  m_nfd.initialize();
88  }
89 
90  int
91  run()
92  {
93  // Return value: a non-zero value is assigned when either NFD or RIB manager (running in
94  // a separate thread) fails.
95  std::atomic_int retval(0);
96 
97  boost::asio::io_service* const mainIo = &getGlobalIoService();
98  boost::asio::io_service* ribIo = nullptr;
99 
100  // Mutex and conditional variable to implement synchronization between main and RIB manager
101  // threads:
102  // - to block main thread until RIB manager thread starts and initializes ribIo (to allow
103  // stopping it later)
104  std::mutex m;
105  std::condition_variable cv;
106 
107  std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables
108  boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
109  {
110  std::lock_guard<std::mutex> lock(m);
111  ribIo = &getGlobalIoService();
112  BOOST_ASSERT(ribIo != mainIo);
113  }
114  cv.notify_all(); // notify that ribIo has been assigned
115 
116  try {
117  ndn::KeyChain ribKeyChain;
118  // must be created inside a separate thread
119  rib::Service ribService(configFile, ribKeyChain);
120  ribService.initialize();
121  getGlobalIoService().run(); // ribIo is not thread-safe to use here
122  }
123  catch (const std::exception& e) {
124  NFD_LOG_FATAL(e.what());
125  retval = 1;
126  mainIo->stop();
127  }
128 
129  {
130  std::lock_guard<std::mutex> lock(m);
131  ribIo = nullptr;
132  }
133  });
134 
135  {
136  // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate
137  // RIB manager thread.
138  std::unique_lock<std::mutex> lock(m);
139  cv.wait(lock, [&ribIo] { return ribIo != nullptr; });
140  }
141 
142  try {
143  mainIo->run();
144  }
145  catch (const std::exception& e) {
147  retval = 1;
148  }
149  catch (const PrivilegeHelper::Error& e) {
150  NFD_LOG_FATAL(e.what());
151  retval = 4;
152  }
153 
154  {
155  // ribIo is guaranteed to be alive at this point
156  std::lock_guard<std::mutex> lock(m);
157  if (ribIo != nullptr) {
158  ribIo->stop();
159  ribIo = nullptr;
160  }
161  }
162  ribThread.join();
163 
164  return retval;
165  }
166 
167  void
168  terminate(const boost::system::error_code& error, int signalNo)
169  {
170  if (error)
171  return;
172 
173  NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
174  getGlobalIoService().stop();
175  }
176 
177  void
178  reload(const boost::system::error_code& error, int signalNo)
179  {
180  if (error)
181  return;
182 
183  NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading...");
184  m_nfd.reloadConfigFile();
185 
186  m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
187  }
188 
189 private:
190  ndn::KeyChain m_nfdKeyChain;
191  Nfd m_nfd;
192  std::string m_configFile;
193 
194  boost::asio::signal_set m_terminationSignalSet;
195  boost::asio::signal_set m_reloadSignalSet;
196 };
197 
198 static void
199 printUsage(std::ostream& os, const char* programName,
200  const po::options_description& opts)
201 {
202  os << "Usage: " << programName << " [options]\n"
203  << "Run the NDN Forwarding Daemon (NFD)\n"
204  << "\n"
205  << opts;
206 }
207 
208 static void
209 printLogModules(std::ostream& os)
210 {
211  const auto& factory = LoggerFactory::getInstance();
212  for (const auto& module : factory.getModules()) {
213  os << module << "\n";
214  }
215 }
216 
217 } // namespace nfd
218 
219 int
220 main(int argc, char** argv)
221 {
222  using namespace nfd;
223 
224  std::string configFile = DEFAULT_CONFIG_FILE;
225 
226  po::options_description description("Options");
227  description.add_options()
228  ("help,h", "print this message and exit")
229  ("version,V", "show version information and exit")
230  ("config,c", po::value<std::string>(&configFile),
231  "path to configuration file (default: " DEFAULT_CONFIG_FILE ")")
232  ("modules,m", "list available logging modules")
233  ;
234 
235  po::variables_map vm;
236  try {
237  po::store(po::parse_command_line(argc, argv, description), vm);
238  po::notify(vm);
239  }
240  catch (const std::exception& e) {
241  // avoid NFD_LOG_FATAL to ensure that errors related to command-line parsing always appear on the
242  // terminal and are not littered with timestamps and other things added by the logging subsystem
243  std::cerr << "ERROR: " << e.what() << "\n\n";
244  printUsage(std::cerr, argv[0], description);
245  return 2;
246  }
247 
248  if (vm.count("help") > 0) {
249  printUsage(std::cout, argv[0], description);
250  return 0;
251  }
252 
253  if (vm.count("version") > 0) {
254  std::cout << NFD_VERSION_BUILD_STRING << std::endl;
255  return 0;
256  }
257 
258  if (vm.count("modules") > 0) {
259  printLogModules(std::cout);
260  return 0;
261  }
262 
263  NFD_LOG_INFO("Version " NFD_VERSION_BUILD_STRING " starting");
264 
265  NfdRunner runner(configFile);
266  try {
267  runner.initialize();
268  }
269  catch (const boost::filesystem::filesystem_error& e) {
270  if (e.code() == boost::system::errc::permission_denied) {
271  NFD_LOG_FATAL("Permission denied for " << e.path1() <<
272  ". This program should be run as superuser");
273  return 4;
274  }
275  else {
277  return 1;
278  }
279  }
280  catch (const std::exception& e) {
282  return 1;
283  }
284  catch (const PrivilegeHelper::Error& e) {
285  // PrivilegeHelper::Errors do not inherit from std::exception
286  // and represent seteuid/gid failures
287  NFD_LOG_FATAL(e.what());
288  return 4;
289  }
290 
291  return runner.run();
292 }
void initialize()
Definition: main.cpp:85
std::string getExtendedErrorMessage(const E &exception)
static void printLogModules(std::ostream &os)
Definition: main.cpp:209
The interface of signing key management.
Definition: key-chain.hpp:46
initializes and executes NFD-RIB service thread
Definition: service.hpp:45
represents a serious seteuid/gid failure
int main(int argc, char **argv)
Definition: main.cpp:220
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
void initialize()
Perform initialization of NFD-RIB instance.
Definition: service.cpp:63
NfdRunner(const std::string &configFile)
Definition: main.cpp:70
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
void reload(const boost::system::error_code &error, int signalNo)
Definition: main.cpp:178
int run()
Definition: main.cpp:91
#define NFD_LOG_FATAL(expression)
Definition: logger.hpp:59
void reloadConfigFile()
Reload configuration file and apply update (if any)
Definition: nfd.cpp:177
Class representing NFD instance This class can be used to initialize all components of NFD...
Definition: nfd.hpp:63
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
void initialize()
Perform initialization of NFD instance After initialization, NFD instance can be started by invoking ...
Definition: nfd.cpp:74
Executes NFD with RIB manager.
Definition: main.cpp:66
static void printUsage(std::ostream &os, const char *programName, const po::options_description &opts)
Definition: main.cpp:199
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
void terminate(const boost::system::error_code &error, int signalNo)
Definition: main.cpp:168