general-config-section.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
27 
28 #include "core/logger.hpp"
30 #include "core/config-file.hpp"
31 
32 namespace nfd {
33 
34 namespace general {
35 
36 NFD_LOG_INIT("GeneralConfigSection");
37 
38 static void
39 onConfig(const ConfigSection& configSection,
40  bool isDryRun,
41  const std::string& filename)
42 {
43  // general
44  // {
45  // ; user "ndn-user"
46  // ; group "ndn-user"
47  // }
48 
49  std::string user;
50  std::string group;
51 
52  for (ConfigSection::const_iterator i = configSection.begin();
53  i != configSection.end();
54  ++i)
55  {
56  if (i->first == "user")
57  {
58  try
59  {
60  user = i->second.get_value<std::string>("user");
61 
62  if (user.empty())
63  {
64  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\""
65  " in \"general\" section"));
66  }
67  }
68  catch (const boost::property_tree::ptree_error& error)
69  {
70  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\""
71  " in \"general\" section"));
72  }
73  }
74  else if (i->first == "group")
75  {
76  try
77  {
78  group = i->second.get_value<std::string>("group");
79 
80  if (group.empty())
81  {
82  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\""
83  " in \"general\" section"));
84  }
85  }
86  catch (const boost::property_tree::ptree_error& error)
87  {
88  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\""
89  " in \"general\" section"));
90  }
91  }
92  }
93  NFD_LOG_TRACE("using user \"" << user << "\" group \"" << group << "\"");
94 
95  PrivilegeHelper::initialize(user, group);
96 }
97 
98 void
100 {
101  configFile.addSectionHandler("general", &onConfig);
102 }
103 
104 } // namespace general
105 
106 } // namespace nfd
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:76
configuration file parsing utility
Definition: config-file.hpp:58
void setConfigFile(ConfigFile &configFile)
static void onConfig(const ConfigSection &configSection, bool isDryRun, const std::string &filename)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
static void initialize(const std::string &userName, const std::string &groupName)
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:160