36 #include <boost/config.hpp>
37 #include <boost/exception/diagnostic_information.hpp>
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 #include <boost/version.hpp>
45 #include <condition_variable>
49 #include <ndn-cxx/util/logging.hpp>
50 #include <ndn-cxx/util/ostream-joiner.hpp>
51 #include <ndn-cxx/version.hpp>
53 #ifdef NFD_HAVE_LIBPCAP
54 #include <pcap/pcap.h>
56 #ifdef NFD_HAVE_SYSTEMD
57 #include <systemd/sd-daemon.h>
59 #ifdef NFD_HAVE_WEBSOCKET
60 #include <websocketpp/version.hpp>
63 namespace po = boost::program_options;
78 class NfdRunner : noncopyable
82 NfdRunner(
const std::string& configFile)
83 : m_nfd(configFile, m_nfdKeyChain)
84 , m_configFile(configFile)
88 m_terminateSignals.async_wait([
this] (
auto&&... args) {
89 terminate(std::forward<decltype(args)>(args)...);
91 m_reloadSignals.async_wait([
this] (
auto&&... args) {
92 reload(std::forward<decltype(args)>(args)...);
107 std::atomic_int retval(0);
111 boost::asio::io_service* ribIo =
nullptr;
118 std::condition_variable cv;
120 std::thread ribThread([configFile = m_configFile, &retval, &ribIo, mainIo, &cv, &m] {
122 std::lock_guard<std::mutex> lock(m);
124 BOOST_ASSERT(ribIo != mainIo);
130 ndn::KeyChain ribKeyChain;
132 rib::Service ribService(configFile, ribKeyChain);
135 catch (
const std::exception& e) {
142 std::lock_guard<std::mutex> lock(m);
150 std::unique_lock<std::mutex> lock(m);
151 cv.wait(lock, [&ribIo] {
return ribIo !=
nullptr; });
155 systemdNotify(
"READY=1");
158 catch (
const std::exception& e) {
162 catch (
const PrivilegeHelper::Error& e) {
169 std::lock_guard<std::mutex> lock(m);
170 if (ribIo !=
nullptr) {
181 systemdNotify(
const char* state)
183 #ifdef NFD_HAVE_SYSTEMD
190 terminate(
const boost::system::error_code& error,
int signalNo)
195 NFD_LOG_INFO(
"Caught signal " << signalNo <<
" (" << ::strsignal(signalNo) <<
"), exiting...");
197 systemdNotify(
"STOPPING=1");
202 reload(
const boost::system::error_code& error,
int signalNo)
207 NFD_LOG_INFO(
"Caught signal " << signalNo <<
" (" << ::strsignal(signalNo) <<
"), reloading...");
209 systemdNotify(
"RELOADING=1");
210 m_nfd.reloadConfigFile();
211 systemdNotify(
"READY=1");
213 m_reloadSignals.async_wait([
this] (
auto&&... args) {
214 reload(std::forward<decltype(args)>(args)...);
219 ndn::KeyChain m_nfdKeyChain;
221 std::string m_configFile;
223 boost::asio::signal_set m_terminateSignals;
224 boost::asio::signal_set m_reloadSignals;
228 printUsage(std::ostream& os,
const char* programName,
const po::options_description& opts)
230 os <<
"Usage: " << programName <<
" [options]\n"
232 <<
"Run the NDN Forwarding Daemon (NFD)\n"
240 const auto& modules = ndn::util::Logging::getLoggerNames();
241 std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os,
"\n"));
252 std::string configFile = NFD_DEFAULT_CONFIG_FILE;
254 po::options_description description(
"Options");
255 description.add_options()
256 (
"help,h",
"print this message and exit")
257 (
"version,V",
"show version information and exit")
258 (
"config,c", po::value<std::string>(&configFile),
259 "path to configuration file (default: " NFD_DEFAULT_CONFIG_FILE
")")
260 (
"modules,m",
"list available logging modules")
263 po::variables_map vm;
265 po::store(po::parse_command_line(argc, argv, description), vm);
268 catch (
const std::exception& e) {
272 std::cerr <<
"ERROR: " << e.what() <<
"\n\n";
277 if (vm.count(
"help") > 0) {
282 if (vm.count(
"version") > 0) {
287 if (vm.count(
"modules") > 0) {
292 const std::string boostBuildInfo =
293 "with Boost version " + to_string(BOOST_VERSION / 100000) +
294 "." + to_string(BOOST_VERSION / 100 % 1000) +
295 "." + to_string(BOOST_VERSION % 100);
296 const std::string pcapBuildInfo =
297 #ifdef NFD_HAVE_LIBPCAP
298 "with " + std::string(pcap_lib_version());
302 const std::string wsBuildInfo =
303 #ifdef NFD_HAVE_WEBSOCKET
304 "with WebSocket++ version " + to_string(websocketpp::major_version) +
305 "." + to_string(websocketpp::minor_version) +
306 "." + to_string(websocketpp::patch_version);
308 "without WebSocket++";
312 <<
"Built with " BOOST_COMPILER
", with " BOOST_STDLIB
313 ", " << boostBuildInfo <<
314 ", " << pcapBuildInfo <<
315 ", " << wsBuildInfo <<
316 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING
319 NfdRunner runner(configFile);
323 catch (
const boost::filesystem::filesystem_error& e) {
325 return e.code() == boost::system::errc::permission_denied ? 4 : 1;
327 catch (
const std::exception& e) {
331 catch (
const PrivilegeHelper::Error& e) {
int main(int argc, char **argv)
#define NFD_LOG_INIT(name)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents,...
void setMainIoService(boost::asio::io_service *mainIo)
static void printUsage(std::ostream &os, const char *programName, const po::options_description &opts)
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
static void printLogModules(std::ostream &os)
void setRibIoService(boost::asio::io_service *ribIo)
const char NFD_VERSION_BUILD_STRING[]
NFD version string, including git commit information if NFD is build from a specific git commit.