36 #include <boost/asio/signal_set.hpp>
37 #include <boost/config.hpp>
38 #include <boost/exception/diagnostic_information.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/system/system_error.hpp>
43 #include <boost/version.hpp>
46 #include <condition_variable>
50 #include <ndn-cxx/util/logging.hpp>
51 #include <ndn-cxx/util/ostream-joiner.hpp>
52 #include <ndn-cxx/version.hpp>
54 #ifdef NFD_HAVE_LIBPCAP
55 #include <pcap/pcap.h>
57 #ifdef NFD_HAVE_SYSTEMD
58 #include <systemd/sd-daemon.h>
60 #ifdef NFD_HAVE_WEBSOCKET
61 #include <websocketpp/version.hpp>
64 namespace po = boost::program_options;
79 class NfdRunner : noncopyable
83 NfdRunner(
const std::string& configFile)
84 : m_nfd(configFile, m_nfdKeyChain)
85 , m_configFile(configFile)
89 m_terminateSignals.async_wait([
this] (
auto&&... args) {
90 terminate(std::forward<decltype(args)>(args)...);
92 m_reloadSignals.async_wait([
this] (
auto&&... args) {
93 reload(std::forward<decltype(args)>(args)...);
108 std::atomic_int retval(0);
112 boost::asio::io_context* ribIo =
nullptr;
119 std::condition_variable cv;
121 std::thread ribThread([configFile = m_configFile, &retval, &ribIo, mainIo, &cv, &m] {
123 std::lock_guard<std::mutex> lock(m);
125 BOOST_ASSERT(ribIo != mainIo);
131 ndn::KeyChain ribKeyChain;
133 rib::Service ribService(configFile, ribKeyChain);
136 catch (
const std::exception& e) {
143 std::lock_guard<std::mutex> lock(m);
151 std::unique_lock<std::mutex> lock(m);
152 cv.wait(lock, [&ribIo] {
return ribIo !=
nullptr; });
156 systemdNotify(
"READY=1");
159 catch (
const std::exception& e) {
163 catch (
const PrivilegeHelper::Error& e) {
170 std::lock_guard<std::mutex> lock(m);
171 if (ribIo !=
nullptr) {
182 systemdNotify(
const char* state)
184 #ifdef NFD_HAVE_SYSTEMD
191 terminate(
const boost::system::error_code& error,
int signalNo)
196 NFD_LOG_INFO(
"Caught signal " << signalNo <<
" (" << ::strsignal(signalNo) <<
"), exiting...");
198 systemdNotify(
"STOPPING=1");
203 reload(
const boost::system::error_code& error,
int signalNo)
208 NFD_LOG_INFO(
"Caught signal " << signalNo <<
" (" << ::strsignal(signalNo) <<
"), reloading...");
210 systemdNotify(
"RELOADING=1");
211 m_nfd.reloadConfigFile();
212 systemdNotify(
"READY=1");
214 m_reloadSignals.async_wait([
this] (
auto&&... args) {
215 reload(std::forward<decltype(args)>(args)...);
220 ndn::KeyChain m_nfdKeyChain;
222 std::string m_configFile;
224 boost::asio::signal_set m_terminateSignals;
225 boost::asio::signal_set m_reloadSignals;
229 printUsage(std::ostream& os,
const char* programName,
const po::options_description& opts)
231 os <<
"Usage: " << programName <<
" [options]\n"
233 <<
"Run the NDN Forwarding Daemon (NFD)\n"
241 const auto& modules = ndn::util::Logging::getLoggerNames();
242 std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os,
"\n"));
253 std::string configFile = NFD_DEFAULT_CONFIG_FILE;
255 po::options_description description(
"Options");
256 description.add_options()
257 (
"help,h",
"print this message and exit")
258 (
"version,V",
"show version information and exit")
259 (
"config,c", po::value<std::string>(&configFile),
260 "path to configuration file (default: " NFD_DEFAULT_CONFIG_FILE
")")
261 (
"modules,m",
"list available logging modules")
264 po::variables_map vm;
266 po::store(po::parse_command_line(argc, argv, description), vm);
269 catch (
const std::exception& e) {
273 std::cerr <<
"ERROR: " << e.what() <<
"\n\n";
278 if (vm.count(
"help") > 0) {
283 if (vm.count(
"version") > 0) {
288 if (vm.count(
"modules") > 0) {
293 const std::string boostBuildInfo =
294 "with Boost version " + std::to_string(BOOST_VERSION / 100000) +
295 "." + std::to_string(BOOST_VERSION / 100 % 1000) +
296 "." + std::to_string(BOOST_VERSION % 100);
297 const std::string pcapBuildInfo =
298 #ifdef NFD_HAVE_LIBPCAP
299 "with " + std::string(pcap_lib_version());
303 const std::string wsBuildInfo =
304 #ifdef NFD_HAVE_WEBSOCKET
305 "with WebSocket++ version " + std::to_string(websocketpp::major_version) +
306 "." + std::to_string(websocketpp::minor_version) +
307 "." + std::to_string(websocketpp::patch_version);
309 "without WebSocket++";
313 <<
"Built with " BOOST_COMPILER
", with " BOOST_STDLIB
314 ", " << boostBuildInfo <<
315 ", " << pcapBuildInfo <<
316 ", " << wsBuildInfo <<
317 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING
320 auto flush = ndn::make_scope_exit([] { ndn::util::Logging::flush(); });
322 NfdRunner runner(configFile);
326 catch (
const boost::system::system_error& e) {
328 if (e.code() == boost::system::errc::operation_not_permitted ||
329 e.code() == boost::system::errc::permission_denied) {
334 catch (
const std::exception& e) {
338 catch (
const PrivilegeHelper::Error& e) {
int main(int argc, char **argv)
#define NFD_LOG_INIT(name)
static void printUsage(std::ostream &os, const char *programName, const po::options_description &opts)
void setMainIoService(boost::asio::io_context *mainIo)
void setRibIoService(boost::asio::io_context *ribIo)
static void printLogModules(std::ostream &os)
boost::asio::io_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.
const char NFD_VERSION_BUILD_STRING[]
NFD version string, including git commit information if NFD is build from a specific git commit.