daemon+rib: Merge nrd and nfd into a single process (separate threads)

Change-Id: I41952d5b8ee29f109130c570e0d13ccad6970d2f
Refs: #2489
diff --git a/contrib/osx-launchd/README.md b/contrib/osx-launchd/README.md
index 68d3b9b..6b453aa 100644
--- a/contrib/osx-launchd/README.md
+++ b/contrib/osx-launchd/README.md
@@ -7,23 +7,17 @@
 Initial setup
 -------------
 
-Edit `net.named-data.nfd` and `net.named-data.nrd` correcting paths for `nfd` and `nfd`
-binaries, configuration file, and log files.
+Edit `net.named-data.nfd` correcting paths for `nfd` binary, configuration and log files.
 
-    # Copy launchd.plist for nfd (forwarding daemon)
+    # Copy launchd.plist for NFD
     sudo cp net.named-data.nfd.plist /Library/LaunchDaemons/
     sudo chown root /Library/LaunchDaemons/net.named-data.nfd.plist
 
-    # Copy launchd.plist for nrd (RIB management daemon)
-    sudo cp net.named-data.nrd.plist /Library/LaunchDaemons/
-    sudo chown root /Library/LaunchDaemons/net.named-data.nrd.plist
-
 ### Assumptions in the default scripts
 
-* `nfd` and `nrd` are installed into `/usr/local/bin`
+* `nfd` is installed into `/usr/local/bin`
 * Configuration file is `/usr/local/etc/ndn/nfd.conf`
 * `nfd` will be run as root
-* `nrd` will be run as user `ndn` and group `ndn`
 * Log files will be written to `/usr/local/var/log/ndn` folder, which is owned by user `ndn`
 
 ### Creating users
@@ -62,29 +56,24 @@
     sudo mkdir -p /usr/local/var/log/ndn
     sudo chown -R ndn:ndn /usr/local/var/log/ndn
 
-`HOME` directories for `nfd` and `nrd` should be created and configured with correct
-library's config file and contain proper NDN security credentials for signing Data
-packets.  This is necessary since default private key storage on OSX (`osx-keychain`) does
-not support non-interactive access, and file-based private key storage needs to be used:
+`HOME` directory for `nfd` should be created and configured with correct library's config file
+and contain proper NDN security credentials for signing Data packets.  This is necessary since
+default private key storage on OSX (`osx-keychain`) does not support non-interactive access,
+and file-based private key storage needs to be used:
 
-    # Generate self-signed NDN certificate for nfd (owned by root)
-    sudo mkdir -p /usr/local/var/lib/ndn/nfd/.ndn
-    sudo sh -c 'echo tpm=file > /usr/local/var/lib/ndn/nfd/.ndn/client.conf'
-    sudo HOME=/usr/local/var/lib/ndn/nfd ndnsec-keygen /localhost/daemons/nfd | \
-      sudo HOME=/usr/local/var/lib/ndn/nfd ndnsec-install-cert -
-
-    # Generate self-signed NDN certificate for nrd (owned by ndn)
-    sudo mkdir -p /usr/local/var/lib/ndn/nrd/.ndn
-    sudo chown -R ndn:ndn /usr/local/var/lib/ndn/nrd
-    sudo -u ndn -g ndn sh -c 'echo tpm=file > /usr/local/var/lib/ndn/nrd/.ndn/client.conf'
-    sudo -u ndn -g ndn HOME=/usr/local/var/lib/ndn/nrd ndnsec-keygen /localhost/daemons/nrd | \
-      sudo -u ndn -g ndn HOME=/usr/local/var/lib/ndn/nrd ndnsec-install-cert -
+    # Create HOME and generate self-signed NDN certificate for nfd
+    sudo -s -- ' \
+      mkdir -p /usr/local/var/lib/ndn/nfd/.ndn; \
+      export HOME=/usr/local/var/lib/ndn/nfd; \
+      echo tpm=tpm-file > /usr/local/var/lib/ndn/nfd/.ndn/client.conf; \
+      ndnsec-keygen /localhost/daemons/nfd | ndnsec-install-cert -; \
+    '
 
 ### Configuring NFD's security
 
-NFD sample configuration allows anybody to create faces, add nexthops to FIB,
-and set strategy choice for namespaces.  While such settings could be a good start, it is
-generally not a good idea to run NFD in this mode.
+NFD sample configuration allows anybody to create faces, add nexthops to FIB, and set strategy
+choice for namespaces.  While such settings could be a good start, it is generally not a good
+idea to run NFD in this mode.
 
 While thorough discussion about security configuration of NFD is outside the scope of this
 document, at least the following change should be done to nfd.conf in authorize section:
@@ -93,7 +82,7 @@
     {
       authorize
       {
-        certfile certs/localhost_daemons_nrd.ndncert
+        certfile certs/localhost_daemons_nfd.ndncert
         privileges
         {
             faces
@@ -114,25 +103,25 @@
     }
 
 While this configuration still allows management of faces and updating strategy choice by
-anybody, only NFD's RIB Manager Daemon (`nrd`) is allowed to manage FIB.
+anybody, only NFD's RIB Manager (i.e., NFD itself) is allowed to manage FIB.
 
-As the final step to make this configuration work, nrd's self-signed certificate needs to
-be exported into `localhost_daemons_nrd.ndncert` file:
+As the final step to make this configuration work, NFD's self-signed certificate needs to
+be exported into `localhost_daemons_nfd.ndncert` file:
 
-    sudo mkdir /usr/local/etc/ndn/certs
-    sudo sh -c 'sudo -u ndn -g ndn HOME=/usr/local/var/lib/ndn/nrd \
-      ndnsec-dump-certificate -i /localhost/daemons/nrd \
-      > /usr/local/etc/ndn/certs/localhost_daemons_nrd.ndncert'
+    sudo -s -- '\
+      mkdir -p /usr/local/etc/ndn/certs || true; \
+      export HOME=/usr/local/var/lib/ndn/nfd; \
+      ndnsec-dump-certificate -i /localhost/daemons/nfd > \
+        /usr/local/etc/ndn/certs/localhost_daemons_nfd.ndncert; \
+      '
 
 
 Enable auto-start
 -----------------
 
     sudo launchctl load -w /Library/LaunchDaemons/net.named-data.nfd.plist
-    sudo launchctl load -w /Library/LaunchDaemons/net.named-data.nrd.plist
 
 Disable auto-start
 ------------------
 
     sudo launchctl unload -w /Library/LaunchDaemons/net.named-data.nfd.plist
-    sudo launchctl unload -w /Library/LaunchDaemons/net.named-data.nrd.plist
diff --git a/contrib/osx-launchd/net.named-data.nrd.plist b/contrib/osx-launchd/net.named-data.nrd.plist
deleted file mode 100644
index 3cd7aaa..0000000
--- a/contrib/osx-launchd/net.named-data.nrd.plist
+++ /dev/null
@@ -1,25 +0,0 @@
-<?xml version='1.0' encoding='UTF-8'?>
-<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
-"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
-<plist version='1.0'>
-<dict>
-<key>Label</key><string>net.named-data.nrd</string>
-<key>ProgramArguments</key>
-<array>
-  <string>/usr/local/bin/nrd</string>
-  <string>--config</string>
-  <string>/usr/local/etc/ndn/nfd.conf</string>
-</array>
-<key>UserName</key><string>ndn</string>
-<key>GroupName</key><string>ndn</string>
-<key>EnvironmentVariables</key>
-<dict>
-  <key>HOME</key><string>/usr/local/var/lib/ndn/nrd</string>
-</dict>
-<key>Debug</key><true/>
-<key>Disabled</key><true/>
-<key>KeepAlive</key><true/>
-<key>StandardErrorPath</key><string>/usr/local/var/log/ndn/nrd.log</string>
-<key>ProcessType</key><string>Background</string>
-</dict>
-</plist>
diff --git a/contrib/upstart/README.md b/contrib/upstart/README.md
index 760e8a8..ff0042e 100644
--- a/contrib/upstart/README.md
+++ b/contrib/upstart/README.md
@@ -8,26 +8,18 @@
 Initial setup
 -------------
 
-Edit `nfd.conf` and `nrd.conf` correcting paths for `nfd` and `nfd` binaries,
-configuration file, and log files.
+* Edit `nfd.conf` correcting paths for `nfd` binary, configuration and log files.
 
-    # Copy upstart config file for nfd (forwarding daemon)
-    sudo cp nfd.conf /etc/init/
+* Copy upstart config file for NFD
 
-    # Copy upstart config file for nrd (RIB management daemon)
-    sudo cp nrd.conf /etc/init/
-
-    # Copy upstart config file for nfd-watcher (will restart NFD when network change detected)
-    sudo cp nfd-watcher.conf /etc/init/
+        sudo cp nfd.conf /etc/init/
 
 ### Assumptions in the default scripts
 
-* `nfd` and `nrd` are installed into `/usr/local/bin`
+* `nfd` is installed into `/usr/local/bin`
 * Configuration file is `/usr/local/etc/ndn/nfd.conf`
 * `nfd` will be run as root
-* `nrd` will be run as user `ndn` and group `ndn`
 * Log files will be written to `/usr/local/var/log/ndn` folder, which is owned by user `ndn`
-* Whenever network connectivity changes, both `nfd` and `nrd` are restarted
 
 ### Creating users
 
@@ -53,19 +45,15 @@
     sudo mkdir -p /usr/local/var/log/ndn
     sudo chown -R ndn:ndn /usr/local/var/log/ndn
 
-`HOME` directories for `nfd` and `nrd` should be created prior to starting.  This is
-necessary to manage unique security credentials for the deamons.
+`HOME` directory for `nfd` should be created prior to starting.  This is necessary to manage
+unique security credentials for the deamon.
 
     # Create HOME and generate self-signed NDN certificate for nfd
-    sudo mkdir -p /usr/local/var/lib/ndn/nfd/.ndn
-    sudo HOME=/usr/local/var/lib/ndn/nfd ndnsec-keygen /localhost/daemons/nfd | \
-      sudo HOME=/usr/local/var/lib/ndn/nfd ndnsec-install-cert -
-
-    # Create HOME and generate self-signed NDN certificate for nrd
-    sudo mkdir -p /usr/local/var/lib/ndn/nrd/.ndn
-    sudo chown -R ndn:ndn /usr/local/var/lib/ndn/nrd
-    sudo -u ndn -g ndn HOME=/usr/local/var/lib/ndn/nrd ndnsec-keygen /localhost/daemons/nrd | \
-      sudo -u ndn -g ndn HOME=/usr/local/var/lib/ndn/nrd ndnsec-install-cert -
+    sudo -s -- ' \
+      mkdir -p /usr/local/var/lib/ndn/nfd/.ndn; \
+      export HOME=/usr/local/var/lib/ndn/nfd; \
+      ndnsec-keygen /localhost/daemons/nfd | ndnsec-install-cert -; \
+    '
 
 ### Configuring NFD's security
 
@@ -81,7 +69,7 @@
     {
       authorize
       {
-        certfile certs/localhost_daemons_nrd.ndncert
+        certfile certs/localhost_daemons_nfd.ndncert
         privileges
         {
             faces
@@ -102,43 +90,36 @@
     }
 
 While this configuration still allows management of faces and updating strategy choice by
-anybody, only NFD's RIB Manager Daemon (`nrd`) is allowed to manage FIB.
+anybody, only NFD's RIB Manager (i.e., NFD itself) is allowed to manage FIB.
 
-As the final step to make this configuration work, nrd's self-signed certificate needs to
-be exported into `localhost_daemons_nrd.ndncert` file:
+As the final step to make this configuration work, nfd's self-signed certificate needs to
+be exported into `localhost_daemons_nfd.ndncert` file:
 
-    sudo mkdir /usr/local/etc/ndn/certs
-    sudo sh -c 'sudo -u ndn -g ndn HOME=/usr/local/var/lib/ndn/nrd \
-      ndnsec-dump-certificate -i /localhost/daemons/nrd \
-      > /usr/local/etc/ndn/certs/localhost_daemons_nrd.ndncert'
+    sudo -s -- '\
+      mkdir -p /usr/local/etc/ndn/certs || true; \
+      export HOME=/usr/local/var/lib/ndn/nfd; \
+      ndnsec-dump-certificate -i /localhost/daemons/nfd > \
+        /usr/local/etc/ndn/certs/localhost_daemons_nfd.ndncert; \
+      '
 
 
 Enable auto-start
 -----------------
 
-After copying the provided upstart scripts, `nfd` and `nrd` daemons will automatically run
-after the reboot.  To manually start them, use the following commands:
+After copying the provided upstart script, `nfd` daemon will automatically run after the reboot.
+To manually start them, use the following commands:
 
     sudo start nfd
-    # nrd will be automatically started by upstart
-
-Note that an additional upstart job, ``nfd-watcher``, will automatically monitor for
-network connectivity changes, such as when network interface gets connected, disconnected,
-or IP addresses of the network interface get updated.  When ``nfd-watcher`` detects the
-event, it will restart `nfd` and `nrd`.
 
 Disable auto-start
 ------------------
 
-To stop `nrd` and `nfd` daemon, use the following commands:
+To stop `nfd` daemon, use the following commands:
 
     sudo stop nfd
-    # nrd will be automatically stopped by upstart
 
-Note that as long as upstart files are present in `/etc/init/`, the daemons will
-automatically start after the reboot.  To permanently stop `nfd` and `nrd` daemons, delete
+Note that as long as upstart files are present in `/etc/init/`, the daemon will
+automatically start after the reboot.  To permanently stop `nfd` daemon, delete
 the upstart files:
 
     sudo rm /etc/init/nfd.conf
-    sudo rm /etc/init/nrd.conf
-    sudo rm /etc/init/nfd-watcher.conf
diff --git a/contrib/upstart/nfd-watcher.conf b/contrib/upstart/nfd-watcher.conf
deleted file mode 100644
index 392af52..0000000
--- a/contrib/upstart/nfd-watcher.conf
+++ /dev/null
@@ -1,13 +0,0 @@
-# nfd-watcher.conf
-#
-# Restarting NDN Forwarding Daemon on network connectivity changes
-
-start on (net-device-up or
-          net-device-removed or
-          net-device-changed)
-task
-
-script
-  status nfd | grep -q start/ || stop
-  restart nfd
-end script
diff --git a/contrib/upstart/nfd.conf b/contrib/upstart/nfd.conf
index 97179a2..d3673f5 100644
--- a/contrib/upstart/nfd.conf
+++ b/contrib/upstart/nfd.conf
@@ -14,4 +14,13 @@
 export HOME
 
 exec /usr/local/bin/nfd --config /usr/local/etc/ndn/nfd.conf 2>> /usr/local/var/log/ndn/nfd.log
+
+post-start script
+  if [ -f /usr/local/etc/ndn/nfd-init.sh ]; then
+      sleep 2 # post-start is executed just after nfd process starts, but there is no guarantee
+              # that all initialization has been finished
+      . /usr/local/etc/ndn/nfd-init.sh
+  fi
+end script
+
 post-stop exec sleep 2
diff --git a/contrib/upstart/nrd.conf b/contrib/upstart/nrd.conf
deleted file mode 100644
index 574b81c..0000000
--- a/contrib/upstart/nrd.conf
+++ /dev/null
@@ -1,22 +0,0 @@
-# nrd.conf
-#
-# NDN RIB Manager Daemon
-
-description "NDN RIB Manager Daemon"
-
-start on started  nfd
-stop  on stopping nfd
-
-respawn
-respawn limit unlimited
-
-setuid ndn
-setgid ndn
-
-pre-start exec sleep 2
-script
-  export HOME=/usr/local/var/lib/ndn/nrd
-  /usr/local/bin/nrd --config /usr/local/etc/ndn/nfd.conf 2>> /usr/local/var/log/ndn/nrd.log
-end script
-
-post-stop exec sleep 2
diff --git a/daemon/main.cpp b/daemon/main.cpp
index eaa4e26..e6b9ae7 100644
--- a/daemon/main.cpp
+++ b/daemon/main.cpp
@@ -24,6 +24,7 @@
  */
 
 #include "nfd.hpp"
+#include "rib/nrd.hpp"
 
 #include "version.hpp"
 #include "core/global-io.hpp"
@@ -37,16 +38,33 @@
 #include <boost/program_options/variables_map.hpp>
 #include <boost/program_options/parsers.hpp>
 
+// boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage,
+// see http://www.boost.org/doc/libs/1_48_0/doc/html/thread/thread_local_storage.html
+#include <boost/thread.hpp>
+
+#include <atomic>
+#include <condition_variable>
+
 namespace nfd {
 
 NFD_LOG_INIT("NFD");
 
+/** \brief Executes NFD with RIB manager
+ *
+ *  NFD (main forwarding procedure) and RIB manager execute in two different threads.
+ *  Each thread has its own instances global io_service and global scheduler.
+ *
+ *  When either of the daemons fails, execution of non-failed daemon will be terminated as
+ *  well.  In other words, when NFD fails, RIB manager will be terminated; when RIB manager
+ *  fails, NFD will be terminated.
+ */
 class NfdRunner : noncopyable
 {
 public:
   explicit
   NfdRunner(const std::string& configFile)
-    : m_nfd(configFile, m_keyChain)
+    : m_nfd(configFile, m_nfdKeyChain)
+    , m_configFile(configFile)
     , m_terminationSignalSet(getGlobalIoService())
     , m_reloadSignalSet(getGlobalIoService())
   {
@@ -86,6 +104,91 @@
   }
 
   void
+  initialize()
+  {
+    m_nfd.initialize();
+  }
+
+  int
+  run()
+  {
+    /** \brief return value
+     *  A non-zero value is assigned when either NFD or RIB manager (running in a separate
+     *  thread) fails.
+     */
+    std::atomic_int retval(0);
+
+    boost::asio::io_service* const mainIo = &getGlobalIoService();
+    boost::asio::io_service* nrdIo = nullptr;
+
+    // Mutex and conditional variable to implement synchronization between main and RIB manager
+    // threads:
+    // - to block main thread until RIB manager thread starts and initializes nrdIo (to allow
+    //   stopping it later)
+    std::mutex m;
+    std::condition_variable cv;
+
+    std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables
+    boost::thread nrdThread([configFile, &retval, &nrdIo, mainIo, &cv, &m] {
+        {
+          std::lock_guard<std::mutex> lock(m);
+          nrdIo = &getGlobalIoService();
+          BOOST_ASSERT(nrdIo != mainIo);
+        }
+        cv.notify_all(); // notify that nrdIo has been assigned
+
+        try {
+          ndn::KeyChain nrdKeyChain;
+          // must be created inside a separate thread
+          rib::Nrd nrd(configFile, nrdKeyChain);
+          nrd.initialize();
+          getGlobalIoService().run(); // nrdIo is not thread-safe to use here
+        }
+        catch (const std::exception& e) {
+          NFD_LOG_FATAL(e.what());
+          retval = 6;
+          mainIo->stop();
+        }
+
+        {
+          std::lock_guard<std::mutex> lock(m);
+          nrdIo = nullptr;
+        }
+      });
+
+    {
+      // Wait to guarantee that nrdIo is properly initialized, so it can be used to terminate
+      // RIB manager thread.
+      std::unique_lock<std::mutex> lock(m);
+      cv.wait(lock, [&nrdIo] { return nrdIo != nullptr; });
+    }
+
+    try {
+      mainIo->run();
+    }
+    catch (const std::exception& e) {
+      NFD_LOG_FATAL(e.what());
+      retval = 4;
+    }
+    catch (const PrivilegeHelper::Error& e) {
+      NFD_LOG_FATAL(e.what());
+      retval = 5;
+    }
+
+    {
+      // nrdIo is guaranteed to be alive at this point
+      std::lock_guard<std::mutex> lock(m);
+      if (nrdIo != nullptr) {
+        nrdIo->stop();
+        nrdIo = nullptr;
+      }
+    }
+    nrdThread.join();
+
+    return retval;
+  }
+
+  void
   terminate(const boost::system::error_code& error, int signalNo)
   {
     if (error)
@@ -96,12 +199,6 @@
   }
 
   void
-  initialize()
-  {
-    m_nfd.initialize();
-  }
-
-  void
   reload(const boost::system::error_code& error, int signalNo)
   {
     if (error)
@@ -114,8 +211,10 @@
   }
 
 private:
-  ndn::KeyChain           m_keyChain;
+  ndn::KeyChain           m_nfdKeyChain;
   Nfd                     m_nfd;
+  std::string             m_configFile;
+
   boost::asio::signal_set m_terminationSignalSet;
   boost::asio::signal_set m_reloadSignalSet;
 };
@@ -192,17 +291,5 @@
     return 3;
   }
 
-  try {
-    getGlobalIoService().run();
-  }
-  catch (const std::exception& e) {
-    NFD_LOG_FATAL(e.what());
-    return 4;
-  }
-  catch (const PrivilegeHelper::Error& e) {
-    NFD_LOG_FATAL(e.what());
-    return 5;
-  }
-
-  return 0;
+  return runner.run();
 }
diff --git a/nfd.conf.sample.in b/nfd.conf.sample.in
index 2f1e31f..908ad7b 100644
--- a/nfd.conf.sample.in
+++ b/nfd.conf.sample.in
@@ -32,7 +32,6 @@
   ;
   ; Run:
   ;   nfd --modules
-  ;   nrd --modules
   ;
   ; Or look for NFD_LOG_INIT(<module name>) statements in .cpp files
   ;
diff --git a/rib/main.cpp b/rib/main.cpp
deleted file mode 100644
index 62b2ffc..0000000
--- a/rib/main.cpp
+++ /dev/null
@@ -1,187 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2015,  Regents of the University of California,
- *                           Arizona Board of Regents,
- *                           Colorado State University,
- *                           University Pierre & Marie Curie, Sorbonne University,
- *                           Washington University in St. Louis,
- *                           Beijing Institute of Technology,
- *                           The University of Memphis.
- *
- * This file is part of NFD (Named Data Networking Forwarding Daemon).
- * See AUTHORS.md for complete list of NFD authors and contributors.
- *
- * NFD is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "nrd.hpp"
-
-#include "version.hpp"
-#include "core/logger.hpp"
-#include "core/global-io.hpp"
-
-#include <string.h>
-
-#include <boost/filesystem.hpp>
-#include <boost/program_options/options_description.hpp>
-#include <boost/program_options/variables_map.hpp>
-#include <boost/program_options/parsers.hpp>
-
-namespace nfd {
-namespace rib {
-
-NFD_LOG_INIT("NRD");
-
-class NrdRunner : noncopyable
-{
-public:
-  explicit
-  NrdRunner(const std::string& configFile)
-    : m_nrd(configFile, m_keyChain)
-    , m_terminationSignalSet(getGlobalIoService())
-  {
-    m_terminationSignalSet.add(SIGINT);
-    m_terminationSignalSet.add(SIGTERM);
-    m_terminationSignalSet.async_wait(bind(&NrdRunner::terminate, this, _1, _2));
-  }
-
-  static void
-  printUsage(std::ostream& os, const std::string& programName)
-  {
-    os << "Usage: \n"
-       << "  " << programName << " [options]\n"
-       << "\n"
-       << "Run NRD daemon\n"
-       << "\n"
-       << "Options:\n"
-       << "  [--help]    - print this help message\n"
-       << "  [--version] - print version and exit\n"
-       << "  [--modules] - list available logging modules\n"
-       << "  [--config /path/to/nfd.conf] - path to configuration file "
-       << "(default: " << DEFAULT_CONFIG_FILE << ")\n"
-      ;
-  }
-
-  static void
-  printModules(std::ostream& os)
-  {
-    os << "Available logging modules: \n";
-
-    for (const auto& module : LoggerFactory::getInstance().getModules()) {
-      os << module << "\n";
-    }
-  }
-
-  void
-  run()
-  {
-    getGlobalIoService().run();
-  }
-
-  void
-  initialize()
-  {
-    m_nrd.initialize();
-  }
-
-  void
-  terminate(const boost::system::error_code& error, int signalNo)
-  {
-    if (error)
-      return;
-
-    NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
-    getGlobalIoService().stop();
-  }
-
-private:
-  ndn::KeyChain           m_keyChain;
-  Nrd                     m_nrd; // must be after m_io and m_keyChain
-  boost::asio::signal_set m_terminationSignalSet;
-};
-
-} // namespace nfd
-} // namespace rib
-
-int
-main(int argc, char** argv)
-{
-  using namespace nfd::rib;
-
-  namespace po = boost::program_options;
-
-  po::options_description description;
-
-  std::string configFile = DEFAULT_CONFIG_FILE;
-  description.add_options()
-    ("help,h",    "print this help message")
-    ("version,V", "print version and exit")
-    ("modules,m", "list available logging modules")
-    ("config,c",  po::value<std::string>(&configFile), "path to configuration file")
-    ;
-
-  po::variables_map vm;
-  try {
-      po::store(po::command_line_parser(argc, argv).options(description).run(), vm);
-      po::notify(vm);
-  }
-  catch (const std::exception& e) {
-    std::cerr << "ERROR: " << e.what() << std::endl;
-    NrdRunner::printUsage(std::cerr, argv[0]);
-    return 1;
-  }
-
-  if (vm.count("help") > 0) {
-    NrdRunner::printUsage(std::cout, argv[0]);
-    return 0;
-  }
-
-  if (vm.count("version") > 0) {
-    std::cout << NFD_VERSION_BUILD_STRING << std::endl;
-    return 0;
-  }
-
-  if (vm.count("modules") > 0) {
-    NrdRunner::printModules(std::cout);
-    return 0;
-  }
-
-  NrdRunner runner(configFile);
-
-  try {
-    runner.initialize();
-  }
-  catch (const boost::filesystem::filesystem_error& e) {
-    if (e.code() == boost::system::errc::permission_denied) {
-      NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " <<
-                    argv[0] << " should be run as superuser");
-    }
-    else {
-      NFD_LOG_FATAL(e.what());
-    }
-    return 1;
-  }
-  catch (const std::exception& e) {
-    NFD_LOG_FATAL(e.what());
-    return 2;
-  }
-
-  try {
-    runner.run();
-  }
-  catch (const std::exception& e) {
-    NFD_LOG_FATAL(e.what());
-    return 4;
-  }
-
-  return 0;
-}
diff --git a/tools/nfd-start.sh b/tools/nfd-start.sh
index 4df12d7..4c14e07 100755
--- a/tools/nfd-start.sh
+++ b/tools/nfd-start.sh
@@ -6,7 +6,7 @@
   -h)
     echo Usage
     echo $0
-    echo "  Start NFD and RIB Management daemon"
+    echo "  Start NFD"
     exit 0
     ;;
   -V)
@@ -30,11 +30,10 @@
 }
 
 hasNFD=$(hasProcess nfd)
-hasNRD=$(hasProcess nrd)
 
-if [[ -n $hasNFD$hasNRD ]]
+if [[ -n $hasNFD ]]
 then
-  echo 'NFD or NRD is already running...'
+  echo 'NFD is already running...'
   exit 1
 fi
 
@@ -49,7 +48,10 @@
   exit 2
 fi
 
-sudo nfd &
-sleep 2
-nrd &
-sleep 2
+sudo @BINDIR@/nfd &
+
+if [ -f @SYSCONFDIR@/ndn/nfd-init.sh ]; then
+    sleep 2 # post-start is executed just after nfd process starts, but there is no guarantee
+    # that all initialization has been finished
+    . @SYSCONFDIR@/ndn/nfd-init.sh
+fi
diff --git a/tools/nfd-stop.sh b/tools/nfd-stop.sh
index ed55929..bdbcf12 100755
--- a/tools/nfd-stop.sh
+++ b/tools/nfd-stop.sh
@@ -6,7 +6,7 @@
   -h)
     echo Usage
     echo $0
-    echo "  Stop NFD and RIB Management daemon"
+    echo "  Stop NFD"
     exit 0
     ;;
   -V)
@@ -20,5 +20,4 @@
     ;;
 esac
 
-sudo killall nrd
 sudo killall nfd
diff --git a/tools/nrd.sh b/tools/nrd.sh
new file mode 100644
index 0000000..52e7112
--- /dev/null
+++ b/tools/nrd.sh
@@ -0,0 +1,5 @@
+#!@BASH@
+
+echo "NDN RIB Management Daemon does not need to be started separately anymore"
+sleep 10
+exit 0
diff --git a/wscript b/wscript
index 955266b..01d69a7 100644
--- a/wscript
+++ b/wscript
@@ -208,25 +208,18 @@
     if bld.env['HAVE_WEBSOCKET']:
         nfd_objects.source += bld.path.ant_glob('daemon/face/websocket-*.cpp')
 
-    bld(target='bin/nfd',
-        features='cxx cxxprogram',
-        source='daemon/main.cpp',
-        use='daemon-objects',
-        )
-
     rib_objects = bld(
         target='rib-objects',
         name='rib-objects',
         features='cxx',
-        source=bld.path.ant_glob(['rib/**/*.cpp'],
-                                 excl=['rib/main.cpp']),
+        source=bld.path.ant_glob(['rib/**/*.cpp']),
         use='core-objects',
         )
 
-    bld(target='bin/nrd',
+    bld(target='bin/nfd',
         features='cxx cxxprogram',
-        source='rib/main.cpp',
-        use='rib-objects',
+        source='daemon/main.cpp',
+        use='daemon-objects rib-objects',
         )
 
     bld.recurse("tools")