face: change default Unix socket path

Refs: #5304
Change-Id: I5c7415874131b156e54a655960d9de47c1eae91d
diff --git a/daemon/face/unix-stream-channel.cpp b/daemon/face/unix-stream-channel.cpp
index 906d23e..150fad4 100644
--- a/daemon/face/unix-stream-channel.cpp
+++ b/daemon/face/unix-stream-channel.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2023,  Regents of the University of California,
+ * Copyright (c) 2014-2024,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -49,11 +49,10 @@
 UnixStreamChannel::~UnixStreamChannel()
 {
   if (isListening()) {
-    // use the non-throwing variants during destruction
-    // and ignore any errors
+    // use the non-throwing variants during destruction and ignore any errors
     boost::system::error_code error;
     m_acceptor.close(error);
-    NFD_LOG_CHAN_DEBUG("Removing socket file");
+    NFD_LOG_CHAN_TRACE("Removing socket file");
     boost::filesystem::remove(m_endpoint.path(), error);
   }
 }
@@ -70,7 +69,7 @@
 
   namespace fs = boost::filesystem;
 
-  fs::path socketPath(m_endpoint.path());
+  fs::path socketPath = m_endpoint.path();
   fs::file_type type = fs::symlink_status(socketPath).type();
 
   if (type == fs::socket_file) {
@@ -80,7 +79,7 @@
     NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << error.message());
     if (!error) {
       // someone answered, leave the socket alone
-      NDN_THROW(Error("Socket file at " + m_endpoint.path() + " belongs to another NFD process"));
+      NDN_THROW(Error("Socket file at " + m_endpoint.path() + " belongs to another process"));
     }
     else if (error == boost::asio::error::connection_refused ||
              error == boost::asio::error::timed_out) {
@@ -94,6 +93,12 @@
     NDN_THROW(Error(m_endpoint.path() + " already exists and is not a socket file"));
   }
 
+  // ensure parent directory exists before creating socket
+  fs::path parent = socketPath.parent_path();
+  if (!parent.empty() && fs::create_directories(parent)) {
+    NFD_LOG_CHAN_TRACE("Created directory " << parent);
+  }
+
   m_acceptor.open();
   m_acceptor.bind(m_endpoint);
   m_acceptor.listen(backlog);
diff --git a/daemon/face/unix-stream-factory.cpp b/daemon/face/unix-stream-factory.cpp
index a275fde..27719eb 100644
--- a/daemon/face/unix-stream-factory.cpp
+++ b/daemon/face/unix-stream-factory.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2024,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -45,8 +45,8 @@
 {
   // unix
   // {
-  //   path /run/nfd.sock        ; on Linux
-  //   path /var/run/nfd.sock    ; on other platforms
+  //   path /run/nfd/nfd.sock       ; on Linux
+  //   path /var/run/nfd/nfd.sock   ; on other platforms
   // }
 
   m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
@@ -59,15 +59,12 @@
   }
 
 #ifdef __linux__
-  std::string path = "/run/nfd.sock";
+  std::string path = "/run/nfd/nfd.sock";
 #else
-  std::string path = "/var/run/nfd.sock";
+  std::string path = "/var/run/nfd/nfd.sock";
 #endif // __linux__
 
-  for (const auto& pair : *configSection) {
-    const std::string& key = pair.first;
-    const ConfigSection& value = pair.second;
-
+  for (const auto& [key, value] : *configSection) {
     if (key == "path") {
       path = value.get_value<std::string>();
     }
@@ -87,11 +84,10 @@
 }
 
 shared_ptr<UnixStreamChannel>
-UnixStreamFactory::createChannel(const std::string& unixSocketPath)
+UnixStreamFactory::createChannel(const std::string& socketPath)
 {
-  boost::filesystem::path p(unixSocketPath);
-  p = boost::filesystem::canonical(p.parent_path()) / p.filename();
-  unix_stream::Endpoint endpoint(p.string());
+  auto normalizedPath = boost::filesystem::weakly_canonical(boost::filesystem::absolute(socketPath));
+  unix_stream::Endpoint endpoint(normalizedPath.string());
 
   auto it = m_channels.find(endpoint);
   if (it != m_channels.end())
diff --git a/daemon/face/unix-stream-factory.hpp b/daemon/face/unix-stream-factory.hpp
index 76bbfc9..51bc51d 100644
--- a/daemon/face/unix-stream-factory.hpp
+++ b/daemon/face/unix-stream-factory.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2023,  Regents of the University of California,
+ * Copyright (c) 2014-2024,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -53,7 +53,7 @@
    *          an exception will be thrown if the channel cannot be created.
    */
   shared_ptr<UnixStreamChannel>
-  createChannel(const std::string& unixSocketPath);
+  createChannel(const std::string& socketPath);
 
 private:
   void
diff --git a/daemon/rib/service.cpp b/daemon/rib/service.cpp
index 1634ef9..814feb0 100644
--- a/daemon/rib/service.cpp
+++ b/daemon/rib/service.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2024,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -68,9 +68,9 @@
   if (config.get_child_optional("face_system.unix")) {
     // default socket path should be the same as in UnixStreamFactory::processConfig
 #ifdef __linux__
-    auto path = config.get<std::string>("face_system.unix.path", "/run/nfd.sock");
+    auto path = config.get<std::string>("face_system.unix.path", "/run/nfd/nfd.sock");
 #else
-    auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
+    auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd/nfd.sock");
 #endif // __linux__
     return make_shared<ndn::UnixTransport>(path);
   }