build: Add conditional compilation
Two conditionals are introduced in this commit:
- if <ifaddrs.h> is not available, NetworkInterface helper will always return
an empty set of interfaces
- If dropping/elevating effective user/group is not supported, an error
will be thrown if used (e.g., if general.user or general.group is
configured)
Both conditionals are necessary on Android platform.
Change-Id: Ib360e03514af97ed2d68032fbcbe279a8dc84682
diff --git a/core/network-interface.cpp b/core/network-interface.cpp
index 1aad63e..c6d23a3 100644
--- a/core/network-interface.cpp
+++ b/core/network-interface.cpp
@@ -31,9 +31,11 @@
#include <type_traits>
#include <unordered_map>
+#ifdef HAVE_IFADDRS_H
+#include <ifaddrs.h> // for getifaddrs()
+
#include <arpa/inet.h> // for inet_ntop()
#include <netinet/in.h> // for struct sockaddr_in{,6}
-#include <ifaddrs.h> // for getifaddrs()
#if defined(__linux__)
#include <net/if_arp.h> // for ARPHRD_* constants
@@ -43,6 +45,9 @@
#include <net/if_types.h> // for IFT_* constants
#endif
+#endif // HAVE_IFADDRS_H
+
+
NFD_LOG_INIT("NetworkInterfaceInfo");
namespace nfd {
@@ -73,6 +78,7 @@
}
#endif
+#ifdef HAVE_IFADDRS_H
using namespace boost::asio::ip;
using std::strerror;
@@ -170,6 +176,9 @@
}
return v;
+#else
+ return {};
+#endif // HAVE_IFADDRS_H
}
} // namespace nfd
diff --git a/core/privilege-helper.cpp b/core/privilege-helper.cpp
index 6f87f43..1296331 100644
--- a/core/privilege-helper.cpp
+++ b/core/privilege-helper.cpp
@@ -32,15 +32,18 @@
NFD_LOG_INIT("PrivilegeHelper");
+#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
uid_t PrivilegeHelper::s_normalUid = ::geteuid();
gid_t PrivilegeHelper::s_normalGid = ::getegid();
uid_t PrivilegeHelper::s_privilegedUid = ::geteuid();
gid_t PrivilegeHelper::s_privilegedGid = ::getegid();
+#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
void
PrivilegeHelper::initialize(const std::string& userName, const std::string& groupName)
{
+#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
static const size_t MAX_GROUP_BUFFER_SIZE = 16384; // 16kB
static const size_t MAX_PASSWD_BUFFER_SIZE = 16384;
@@ -126,11 +129,15 @@
s_normalUid = passwd.pw_uid;
}
+#else
+ throw Error("Dropping and raising privileges is not supported on this platform");
+#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
}
void
PrivilegeHelper::drop()
{
+#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
NFD_LOG_TRACE("dropping to effective gid=" << s_normalGid);
if (::setegid(s_normalGid) != 0)
{
@@ -150,11 +157,15 @@
}
NFD_LOG_INFO("dropped to effective uid=" << ::geteuid() << " gid=" << ::getegid());
+#else
+ throw Error("Dropping privileges is not supported on this platform");
+#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
}
void
PrivilegeHelper::raise()
{
+#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
NFD_LOG_TRACE("elevating to effective uid=" << s_privilegedUid);
if (::seteuid(s_privilegedUid) != 0)
{
@@ -173,6 +184,9 @@
throw PrivilegeHelper::Error(error.str());
}
NFD_LOG_INFO("elevated to effective uid=" << ::geteuid() << " gid=" << ::getegid());
+#else
+ throw Error("Elevating privileges is not supported on this platform");
+#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
}
void
diff --git a/core/privilege-helper.hpp b/core/privilege-helper.hpp
index 92bd53d..db22177 100644
--- a/core/privilege-helper.hpp
+++ b/core/privilege-helper.hpp
@@ -65,18 +65,19 @@
static void
runElevated(function<void()> f);
-private:
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
static void
raise();
private:
-
+#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
static uid_t s_normalUid;
static gid_t s_normalGid;
static uid_t s_privilegedUid;
static gid_t s_privilegedGid;
+#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
};
} // namespace nfd