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 | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, 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 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 26 | #include "nfd.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 28 | #include "version.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 29 | #include "core/global-io.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 30 | #include "core/logger.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 31 | #include "core/privilege-helper.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 32 | |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include <boost/filesystem.hpp> |
| 36 | #include <boost/program_options/options_description.hpp> |
| 37 | #include <boost/program_options/variables_map.hpp> |
| 38 | #include <boost/program_options/parsers.hpp> |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 39 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 40 | namespace nfd { |
| 41 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 42 | NFD_LOG_INIT("NFD"); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 44 | class NfdRunner : noncopyable |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 45 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 46 | public: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 47 | explicit |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 48 | NfdRunner(const std::string& configFile) |
| 49 | : m_nfd(configFile, m_keyChain) |
| 50 | , m_terminationSignalSet(getGlobalIoService()) |
| 51 | , m_reloadSignalSet(getGlobalIoService()) |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 52 | { |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 53 | m_terminationSignalSet.add(SIGINT); |
| 54 | m_terminationSignalSet.add(SIGTERM); |
| 55 | m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2)); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 56 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 57 | m_reloadSignalSet.add(SIGHUP); |
| 58 | m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2)); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 61 | static void |
| 62 | printUsage(std::ostream& os, const std::string& programName) |
| 63 | { |
| 64 | os << "Usage: \n" |
| 65 | << " " << programName << " [options]\n" |
| 66 | << "\n" |
| 67 | << "Run NFD forwarding daemon\n" |
| 68 | << "\n" |
| 69 | << "Options:\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 70 | << " [--help] - print this help message\n" |
| 71 | << " [--version] - print version and exit\n" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 72 | << " [--modules] - list available logging modules\n" |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 73 | << " [--config /path/to/nfd.conf] - path to configuration file " |
| 74 | << "(default: " << DEFAULT_CONFIG_FILE << ")\n" |
| 75 | ; |
| 76 | } |
| 77 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 78 | static void |
| 79 | printModules(std::ostream& os) |
| 80 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 81 | os << "Available logging modules: \n"; |
| 82 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 83 | for (const auto& module : LoggerFactory::getInstance().getModules()) { |
| 84 | os << module << "\n"; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 87 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 88 | void |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 89 | terminate(const boost::system::error_code& error, int signalNo) |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 90 | { |
| 91 | if (error) |
| 92 | return; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 94 | NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting..."); |
| 95 | getGlobalIoService().stop(); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 96 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 97 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 98 | void |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 99 | initialize() |
| 100 | { |
| 101 | m_nfd.initialize(); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | reload(const boost::system::error_code& error, int signalNo) |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 106 | { |
| 107 | if (error) |
| 108 | return; |
| 109 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 110 | NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading..."); |
| 111 | m_nfd.reloadConfigFile(); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 112 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 113 | m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2)); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 114 | } |
| 115 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 116 | private: |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 117 | ndn::KeyChain m_keyChain; |
| 118 | Nfd m_nfd; |
| 119 | boost::asio::signal_set m_terminationSignalSet; |
| 120 | boost::asio::signal_set m_reloadSignalSet; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 121 | }; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 122 | |
| 123 | } // namespace nfd |
| 124 | |
| 125 | int |
| 126 | main(int argc, char** argv) |
| 127 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 128 | using namespace nfd; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 130 | namespace po = boost::program_options; |
| 131 | |
| 132 | po::options_description description; |
| 133 | |
| 134 | std::string configFile = DEFAULT_CONFIG_FILE; |
| 135 | description.add_options() |
| 136 | ("help,h", "print this help message") |
| 137 | ("version,V", "print version and exit") |
| 138 | ("modules,m", "list available logging modules") |
| 139 | ("config,c", po::value<std::string>(&configFile), "path to configuration file") |
| 140 | ; |
| 141 | |
| 142 | po::variables_map vm; |
| 143 | try { |
| 144 | po::store(po::command_line_parser(argc, argv).options(description).run(), vm); |
| 145 | po::notify(vm); |
| 146 | } |
| 147 | catch (const std::exception& e) { |
| 148 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 149 | NfdRunner::printUsage(std::cerr, argv[0]); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 150 | return 1; |
| 151 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 152 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 153 | if (vm.count("help") > 0) { |
| 154 | NfdRunner::printUsage(std::cout, argv[0]); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 158 | if (vm.count("version") > 0) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 159 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 160 | return 0; |
| 161 | } |
| 162 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 163 | if (vm.count("modules") > 0) { |
| 164 | NfdRunner::printModules(std::cout); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 168 | NfdRunner runner(configFile); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 169 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 170 | try { |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 171 | runner.initialize(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 172 | } |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 173 | catch (const boost::filesystem::filesystem_error& e) { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 174 | if (e.code() == boost::system::errc::permission_denied) { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 175 | NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " << |
| 176 | argv[0] << " should be run as superuser"); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 177 | } |
| 178 | else { |
| 179 | NFD_LOG_FATAL(e.what()); |
| 180 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | catch (const std::exception& e) { |
| 184 | NFD_LOG_FATAL(e.what()); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 185 | return 2; |
| 186 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 187 | catch (const PrivilegeHelper::Error& e) { |
| 188 | // PrivilegeHelper::Errors do not inherit from std::exception |
| 189 | // and represent seteuid/gid failures |
| 190 | |
| 191 | NFD_LOG_FATAL(e.what()); |
| 192 | return 3; |
| 193 | } |
| 194 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 195 | try { |
| 196 | getGlobalIoService().run(); |
| 197 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 198 | catch (const std::exception& e) { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 199 | NFD_LOG_FATAL(e.what()); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 200 | return 4; |
| 201 | } |
| 202 | catch (const PrivilegeHelper::Error& e) { |
| 203 | NFD_LOG_FATAL(e.what()); |
| 204 | return 5; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |