blob: aa2d822b4d20af41c74a0fffe3968b7bcc9e100e [file] [log] [blame]
Alexander Afanasyev2aa39622014-01-22 11:51:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev31c781e2015-02-09 17:39:59 -08003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 DiBenedetto3a4f83d2014-06-02 14:58:54 -060024 */
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080025
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080026#include "nfd.hpp"
Junxiao Shi98e29f42014-03-31 10:27:26 -070027
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070028#include "version.hpp"
Junxiao Shi98e29f42014-03-31 10:27:26 -070029#include "core/global-io.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080030#include "core/logger.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060031#include "core/privilege-helper.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080032
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 Afanasyevc78b1412014-02-19 14:08:26 -080039
Junxiao Shi09bf7c42014-01-31 11:10:25 -070040namespace nfd {
41
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070042NFD_LOG_INIT("NFD");
Junxiao Shi09bf7c42014-01-31 11:10:25 -070043
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080044class NfdRunner
Junxiao Shi09bf7c42014-01-31 11:10:25 -070045{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070046public:
Alexander Afanasyev5959b012014-06-02 19:18:12 +030047 explicit
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080048 NfdRunner(const std::string& configFile)
49 : m_nfd(configFile, m_keyChain)
50 , m_terminationSignalSet(getGlobalIoService())
51 , m_reloadSignalSet(getGlobalIoService())
Alexander Afanasyev5959b012014-06-02 19:18:12 +030052 {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080053 m_terminationSignalSet.add(SIGINT);
54 m_terminationSignalSet.add(SIGTERM);
55 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +030056
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080057 m_reloadSignalSet.add(SIGHUP);
58 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070059 }
60
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070061 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 Afanasyevb47d5382014-05-05 14:35:03 -070070 << " [--help] - print this help message\n"
71 << " [--version] - print version and exit\n"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060072 << " [--modules] - list available logging modules\n"
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070073 << " [--config /path/to/nfd.conf] - path to configuration file "
74 << "(default: " << DEFAULT_CONFIG_FILE << ")\n"
75 ;
76 }
77
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060078 static void
79 printModules(std::ostream& os)
80 {
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060081 os << "Available logging modules: \n";
82
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080083 for (const auto& module : LoggerFactory::getInstance().getModules()) {
84 os << module << "\n";
Junxiao Shi09bf7c42014-01-31 11:10:25 -070085 }
86 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070087
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070088 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080089 terminate(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070090 {
91 if (error)
92 return;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070093
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080094 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
95 getGlobalIoService().stop();
Junxiao Shiea48d8b2014-03-16 13:53:47 -070096 }
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060097
Alexander Afanasyev5959b012014-06-02 19:18:12 +030098 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080099 initialize()
100 {
101 m_nfd.initialize();
102 }
103
104 void
105 reload(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300106 {
107 if (error)
108 return;
109
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800110 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading...");
111 m_nfd.reloadConfigFile();
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300112
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800113 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300114 }
115
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700116private:
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800117 ndn::KeyChain m_keyChain;
118 Nfd m_nfd;
119 boost::asio::signal_set m_terminationSignalSet;
120 boost::asio::signal_set m_reloadSignalSet;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700121};
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700122
123} // namespace nfd
124
125int
126main(int argc, char** argv)
127{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700128 using namespace nfd;
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700129
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800130 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 Afanasyev5a4388a2014-03-27 18:02:55 -0700150 return 1;
151 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700152
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800153 if (vm.count("help") > 0) {
154 NfdRunner::printUsage(std::cout, argv[0]);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700155 return 0;
156 }
157
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800158 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700159 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
160 return 0;
161 }
162
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800163 if (vm.count("modules") > 0) {
164 NfdRunner::printModules(std::cout);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600165 return 0;
166 }
167
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800168 NfdRunner runner(configFile);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600169
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700170 try {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800171 runner.initialize();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700172 }
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800173 catch (const boost::filesystem::filesystem_error& e) {
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700174 if (e.code() == boost::system::errc::permission_denied) {
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600175 NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " <<
176 argv[0] << " should be run as superuser");
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700177 }
178 else {
179 NFD_LOG_FATAL(e.what());
180 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600181 return 1;
182 }
183 catch (const std::exception& e) {
184 NFD_LOG_FATAL(e.what());
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700185 return 2;
186 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600187 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 DiBenedettobf6a93d2014-03-21 14:03:02 -0600195 try {
196 getGlobalIoService().run();
197 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600198 catch (const std::exception& e) {
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700199 NFD_LOG_FATAL(e.what());
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600200 return 4;
201 }
202 catch (const PrivilegeHelper::Error& e) {
203 NFD_LOG_FATAL(e.what());
204 return 5;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700205 }
206
207 return 0;
208}